src/Tools/isac/BaseDefinitions/termC.sml
author wneuper <walther.neuper@jku.at>
Thu, 15 Jul 2021 14:10:18 +0200
changeset 60324 5c7128feb370
parent 60322 2220bafba61f
child 60331 40eb8aa2b0d6
permissions -rw-r--r--
ewrite.sml + poly.sml + rational.sml: ok, repair rewrite-orders
neuper@38025
     1
(* Title: extends Isabelle/src/Pure/term.ML
e0726734@41962
     2
   Author: Walther Neuper 1999, Mathias Lehnfeld
neuper@38025
     3
   (c) due to copyright terms
neuper@38025
     4
*)
wneuper@59507
     5
infix contains_one_of
wneuper@59507
     6
wneuper@59577
     7
(* TERM_C extends Isabelle's naming conventions: "C" indicates Isac add-ons to an Isabelle module *)
walther@59861
     8
signature TERM_ISAC =
walther@59861
     9
sig
walther@59868
    10
  type as_string
walther@59861
    11
  val empty: term
walther@59878
    12
  val typ_empty: typ
walther@59865
    13
walther@59861
    14
  datatype lrd = D | L | R
walther@59861
    15
  type path
walther@59861
    16
  val string_of_path: path -> string
walther@60023
    17
  val sub_at: path -> term -> term
walther@59861
    18
  val go_up: path -> term -> term
walther@59767
    19
walther@59861
    20
  val contains_Var: term -> bool
walther@59861
    21
  val dest_binop_typ: typ -> typ * typ * typ
walther@59861
    22
  val dest_equals: term -> term * term
walther@59861
    23
  val free2str: term -> string
walther@59861
    24
  val ids2str: term -> string list
walther@59861
    25
  val ins_concl: term -> term -> term
walther@59861
    26
  val inst_abs: term -> term
walther@59861
    27
  val inst_bdv: (term * term) list -> term -> term
wneuper@59390
    28
walther@60317
    29
  val mk_frac: typ -> int * (int * int) -> term
walther@60324
    30
  val numerals_to_Free: term -> term
walther@59861
    31
  val term_of_num: typ -> int -> term
walther@59861
    32
  val num_of_term: term -> int
walther@60322
    33
  val to_string: term -> string
walther@59861
    34
  val int_of_str: string -> int
walther@59861
    35
  val isastr_of_int: int -> string
walther@60317
    36
  val int_opt_of_string: string -> int option
wneuper@59390
    37
walther@59861
    38
  val isalist2list: term -> term list
walther@59861
    39
  val list2isalist: typ -> term list -> term
walther@59861
    40
  val isapair2pair: term -> term * term (* rename to dest_pair, compare HOLogic.dest_string *)
wneuper@59390
    41
walther@59861
    42
  val is_atom: term -> bool
walther@60322
    43
  val string_of_atom: term -> string
walther@59861
    44
  val is_const: term -> bool
walther@59861
    45
  val is_variable: term -> bool
walther@59861
    46
  val is_bdv: string -> bool
walther@59861
    47
  val is_bdv_subst: term -> bool
walther@59861
    48
  val guess_bdv_typ: term -> typ
walther@59861
    49
  val is_equality: term -> bool
walther@59861
    50
  val is_expliceq: term -> bool
walther@59861
    51
  val is_f_x: term -> bool
walther@59861
    52
  val is_list: term -> bool
walther@59861
    53
  val is_bool_list: term -> bool
walther@59861
    54
  val dest_listT: typ -> typ
walther@59861
    55
  val is_num: term -> bool
walther@59861
    56
  val is_num': string -> bool
walther@60317
    57
  val string_of_num: term -> string
walther@59861
    58
  val variable_constant_pair: term * term -> bool
wneuper@59390
    59
walther@59861
    60
  val mk_add: term -> term -> term
walther@59861
    61
  val mk_free: typ -> string -> term
walther@59861
    62
  val mk_equality: term * term -> term
walther@59861
    63
  val mk_factroot: string -> typ -> int -> int -> term
walther@59861
    64
  val mk_Free: string * typ -> term
walther@59861
    65
  val mk_thmid: string -> string -> string -> string
walther@59861
    66
  val mk_num_op_num: typ -> typ -> string * typ -> int -> int -> term
walther@59861
    67
  val mk_num_op_var: term -> string -> typ -> typ -> int -> term
walther@59861
    68
  val mk_var_op_num: term -> string -> typ -> typ -> int -> term
wneuper@59390
    69
walther@59861
    70
  val matches: theory -> term -> term -> bool
walther@59861
    71
  val parse: theory -> string -> cterm option
walther@59861
    72
  val parseN: theory -> string -> cterm option
walther@59861
    73
  val parseNEW: Proof.context -> string -> term option
walther@59861
    74
  val parseNEW': Proof.context -> string -> term
walther@59952
    75
  val parseNEW'': theory -> string -> term
walther@59861
    76
  val parseold: theory -> string -> cterm option
walther@59861
    77
  val parse_patt: theory -> string -> term
walther@59861
    78
  val perm: term -> term -> bool
wneuper@59390
    79
walther@59861
    80
  val str_of_free_opt: term -> string option
walther@59861
    81
  val str_of_int: int -> string
walther@59861
    82
  val str2term: string -> term
walther@59861
    83
  val strip_imp_prems': term -> term option
walther@59861
    84
  val subst_atomic_all: (term * term) list -> term -> bool * term
walther@59861
    85
  val term_detail2str: term -> string
walther@59861
    86
walther@59861
    87
  val pairt: term -> term -> term
walther@59861
    88
  val pairT: typ -> typ -> typ
walther@59861
    89
  val raise_type_conflicts: term list -> unit
walther@59861
    90
  val strip_trueprop: term -> term
walther@59861
    91
walther@59861
    92
  val numbers_to_string: term -> term
walther@59861
    93
  val uminus_to_string: term -> term
walther@59871
    94
walther@59861
    95
  val var2free: term -> term
walther@60317
    96
  val vars: term -> term list  (* recognises numerals, should replace "fun vars_of" TODOO*)
walther@59998
    97
  val vars': term list -> term list
walther@60317
    98
  val vars_of: term -> term list   (* deprecated TODOO: see differences in test/../termC.sml*)
walther@59861
    99
  val dest_list': term -> term list
walther@59861
   100
  val negates: term -> term -> bool
wneuper@59532
   101
wenzelm@60223
   102
\<^isac_test>\<open>
walther@60317
   103
  val mk_negative: typ -> term -> term
walther@59861
   104
  val scala_of_term: term -> string
walther@59861
   105
  val atomtyp(*<-- atom_typ TODO*): typ -> unit
walther@59861
   106
  val atomty: term -> unit
walther@59861
   107
  val atomw: term -> unit
walther@59861
   108
  val atomt: term -> unit
walther@59861
   109
  val atomwy: term -> unit
walther@59879
   110
  val atomty_thy: ThyC.id -> term -> unit
walther@59861
   111
  val free2var: term -> term
wenzelm@60223
   112
  val typ_a2real: term -> term
wenzelm@60223
   113
\<close>
walther@59861
   114
  val contains_one_of: thm * (string * typ) list -> bool
walther@59861
   115
  val contains_Const_typeless: term list -> term -> bool
walther@59916
   116
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
walther@59916
   117
  val sym_trm : term -> term
walther@59861
   118
end
wneuper@59389
   119
wneuper@59389
   120
(**)
walther@59861
   121
structure TermC(**): TERM_ISAC(**) =
wneuper@59389
   122
struct
wneuper@59389
   123
(**)
neuper@38025
   124
walther@59865
   125
type as_string = UnparseC.term_as_string
walther@59868
   126
val empty = UnparseC.term_empty
walther@59878
   127
val typ_empty = UnparseC.typ_empty
walther@59861
   128
walther@59767
   129
datatype lrd = L (*t1 in "t1$t2"*)
walther@59767
   130
             | R (*t2 in "t1$t2"*) | D; (*b in Abs(_,_,b*)
walther@59767
   131
type path = lrd list; 
walther@59767
   132
fun ldr2str L = "L"
walther@59767
   133
  | ldr2str R = "R"
walther@59767
   134
  | ldr2str D = "D";
walther@59774
   135
fun string_of_path k = (strs2str' o (map ldr2str)) k;
walther@59768
   136
(*go to a location in a term and fetch the resective sub-term*)
walther@60023
   137
fun sub_at [] t = t
walther@60023
   138
  | sub_at (D :: p) (Abs(_, _, body)) = sub_at p body
walther@60023
   139
  | sub_at (L :: p) (t1 $ _) = sub_at p t1
walther@60023
   140
  | sub_at (R :: p) (_ $ t2) = sub_at p t2
walther@60023
   141
  | sub_at l t = raise TERM ("sub_at: no " ^ string_of_path l ^ " for ", [t]);
walther@59768
   142
fun go_up l t =
walther@60023
   143
  if length l > 1 then sub_at (drop_last l) t else raise ERROR ("go_up [] " ^ UnparseC.term t)
walther@59767
   144
neuper@52087
   145
fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i)
neuper@52087
   146
neuper@38025
   147
fun matches thy tm pa = 
neuper@38025
   148
    (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
wneuper@59392
   149
    handle Pattern.MATCH => false
neuper@38025
   150
wneuper@59392
   151
(** transform  typ / term to a String to be parsed by Scala after transport via libisabelle **)
wneuper@59392
   152
wenzelm@60223
   153
\<^isac_test>\<open>
wneuper@59318
   154
fun scala_of_typ (Type (s, typs)) =
wneuper@59318
   155
    enclose "Type(" ")" (quote s ^ ", " ^
wneuper@59318
   156
      (typs |> map scala_of_typ |> commas |> enclose "List(" ")"))
wneuper@59318
   157
  | scala_of_typ (TFree (s, sort)) =
wneuper@59318
   158
    enclose "TFree(" ")" (quote s ^ ", " ^ (sort |> map quote |> commas |> enclose "List(" ")"))
wneuper@59318
   159
  | scala_of_typ (TVar ((s, i), sort)) =
wneuper@59318
   160
    enclose "TVar(" ")" (
walther@59997
   161
      enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^ 
wneuper@59318
   162
      (sort |> map quote |> commas |> enclose "List(" ")"))
wenzelm@60223
   163
wneuper@59318
   164
fun scala_of_term (Const (s, T)) =
wneuper@59318
   165
    enclose "Const(" ")" (quote s ^ ", " ^ scala_of_typ T)
wneuper@59318
   166
  | scala_of_term (Free (s, T)) =
wneuper@59318
   167
    enclose "Free(" ")" (quote s ^ ", " ^ scala_of_typ T)
wneuper@59318
   168
  | scala_of_term (Var ((s, i), T)) =
wneuper@59318
   169
    enclose "TVar(" ")" (
walther@59997
   170
      enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^ 
wneuper@59318
   171
      scala_of_typ T)
wneuper@59318
   172
  | scala_of_term (Bound i) = enclose "Bound(" ")" (string_of_int i)
wneuper@59318
   173
  | scala_of_term (Abs (s, T, t)) =
wneuper@59318
   174
    enclose "Abs(" ")" (
wneuper@59318
   175
      quote s ^ ", " ^
wneuper@59318
   176
      scala_of_typ T ^ ", " ^
wneuper@59318
   177
      scala_of_term t)
wneuper@59318
   178
  | scala_of_term (t1 $ t2) =
wneuper@59318
   179
    enclose "App(" ")" (scala_of_term t1 ^ ", " ^ scala_of_term t2)
wneuper@59318
   180
wneuper@59392
   181
(* see structure's bare bones.
wneuper@59392
   182
   for Isabelle standard output compare 2017 "structure ML_PP" *)
wneuper@59392
   183
fun atomtyp t =
neuper@38025
   184
  let
wneuper@59392
   185
    fun ato n (Type (s, [])) = "\n*** " ^ indent n ^ "Type (" ^ s ^",[])"
wneuper@59392
   186
      | ato n (Type (s, Ts)) = "\n*** " ^ indent n ^ "Type (" ^ s ^ ",[" ^ atol (n + 1) Ts
walther@59997
   187
      | ato n (TFree (s, sort)) = "\n*** " ^ indent n ^ "TFree (" ^ s ^ ", " ^ strs2str' sort
wneuper@59392
   188
      | ato n (TVar ((s, i), sort)) =
walther@59997
   189
        "\n*** " ^ indent n ^ "TVar ((" ^ s ^ ", " ^ string_of_int i ^ strs2str' sort
wneuper@59392
   190
    and atol n [] = "\n*** " ^ indent n ^ "]"
wneuper@59392
   191
      | atol n (T :: Ts) = (ato n T ^ atol n Ts)
neuper@38048
   192
in tracing (ato 0 t ^ "\n") end;
neuper@38025
   193
neuper@38067
   194
local 
wneuper@59392
   195
  fun ato (Const (a, _)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
wneuper@59392
   196
	  | ato (Free (a, _)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
neuper@38025
   197
	  | ato (Var ((a, i), _)) n =
wneuper@59392
   198
	    "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ string_of_int i ^ "), _)"
wneuper@59392
   199
	  | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
wneuper@59392
   200
	  | ato (Abs (a, _, body)) n = "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
neuper@38025
   201
	  | ato (f $ t) n = (ato f n ^ ato t (n + 1))
neuper@38067
   202
in
wneuper@59392
   203
  fun atomw t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***");
wneuper@59392
   204
  fun atomt t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***");
neuper@38067
   205
end;
wenzelm@60223
   206
\<close>
neuper@38025
   207
neuper@38025
   208
fun term_detail2str t =
wneuper@59392
   209
  let 
walther@59868
   210
    fun ato (Const (a, T)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ T ^ ")"
walther@59868
   211
      | ato (Free (a, T)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ T ^ ")"
wneuper@59392
   212
      | ato (Var ((a, i), T)) n =
walther@59868
   213
        "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^ UnparseC.typ T ^ ")"
wneuper@59392
   214
      | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
wneuper@59392
   215
      | ato (Abs(a, T, body))  n = 
walther@59868
   216
        "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ T ^ ",.." ^ ato body (n + 1)
wneuper@59392
   217
      | ato (f $ t) n = ato f n ^ ato t (n + 1)
wneuper@59392
   218
  in "\n*** " ^ ato t 0 ^ "\n***" end;
wenzelm@60223
   219
wenzelm@60223
   220
\<^isac_test>\<open>
neuper@42376
   221
fun term_detail2str_thy thy t =
wneuper@59392
   222
  let
wneuper@59392
   223
    fun ato (Const (a, T)) n =
walther@59870
   224
        "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ")"
wneuper@59392
   225
  	  | ato (Free (a, T)) n =
walther@59870
   226
  	     "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ")"
wneuper@59392
   227
  	  | ato (Var ((a, i), T)) n =
wneuper@59392
   228
  	    "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^
walther@59870
   229
  	    UnparseC.typ_by_thyID thy T ^ ")"
wneuper@59392
   230
  	  | ato (Bound i) n = 
wneuper@59392
   231
  	    "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
wneuper@59392
   232
  	  | ato (Abs(a, T, body))  n = 
walther@59870
   233
  	    "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ_by_thyID thy T ^ ",.." ^
wneuper@59392
   234
  	    ato body (n + 1)
wneuper@59392
   235
  	  | ato (f $ t) n = ato f n ^ ato t (n + 1)
wneuper@59392
   236
  in "\n*** " ^ ato t 0 ^ "\n***" end;
neuper@38067
   237
fun atomwy t = (writeln o term_detail2str) t;
neuper@38067
   238
fun atomty t = (tracing o term_detail2str) t;
neuper@42376
   239
fun atomty_thy thy t = (tracing o (term_detail2str_thy thy)) t;
wenzelm@60223
   240
\<close>
neuper@38025
   241
wneuper@59392
   242
(* contains the term a VAR(("*",_),_) ? *)
neuper@38025
   243
fun contains_Var (Abs(_,_,body)) = contains_Var body
neuper@38025
   244
  | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
neuper@38025
   245
  | contains_Var (Var _) = true
neuper@38025
   246
  | contains_Var _ = false;
neuper@38025
   247
wneuper@59392
   248
fun str_of_int n = 
wneuper@59392
   249
  if n < 0 then "-" ^ ((string_of_int o abs) n)
wneuper@59392
   250
  else string_of_int n;
wneuper@59457
   251
val int_of_str = Value.parse_int;
walther@60317
   252
val int_opt_of_string = ThmC_Def.int_opt_of_string
walther@60317
   253
fun is_num' str = case int_opt_of_string str of SOME _ => true | NONE => false;
walther@59871
   254
walther@60317
   255
fun is_num (Const ("Num.numeral_class.numeral", _) $ _) = true
walther@60317
   256
  | is_num (Const ("Groups.uminus_class.uminus", _) $
walther@60317
   257
    (Const ("Num.numeral_class.numeral", _) $ _)) = true
walther@60317
   258
  | is_num (Const ("Groups.one_class.one", _)) = true
walther@60317
   259
  | is_num (Const ("Groups.uminus_class.uminus", _) $ Const ("Groups.one_class.one", _)) = true
walther@60317
   260
  | is_num (Const ("Groups.zero_class.zero", _)) = true
walther@60317
   261
  | is_num (Const ("Groups.uminus_class.uminus", _) $ Const ("Groups.zero_class.zero", _)) = true
walther@60317
   262
  | is_num _ = false;
walther@59871
   263
walther@60317
   264
fun string_of_num n = (n |> HOLogic.dest_number |> snd |> string_of_int)
walther@60317
   265
walther@60317
   266
fun mk_negative T t = Const ("Groups.uminus_class.uminus", T --> T) $ t
walther@60317
   267
fun mk_frac T (sg, (i1, i2)) =
walther@60317
   268
  if sg = 1 then
walther@60317
   269
    if i2 = 1 then HOLogic.mk_number T i1
walther@60317
   270
    else Const ("Rings.divide_class.divide", T --> T --> T) $
walther@60317
   271
      HOLogic.mk_number T i1 $ HOLogic.mk_number T i2
walther@60317
   272
  else (*take negative*)
walther@60317
   273
    if i2 = 1 then mk_negative T (HOLogic.mk_number T i1)
walther@60317
   274
    else Const ("Rings.divide_class.divide", T --> T --> T) $
walther@60317
   275
      mk_negative T (HOLogic.mk_number T i1) $ HOLogic.mk_number T i2
walther@60317
   276
walther@60324
   277
val numerals_to_Free = (* Makarius 100308 *)
walther@60324
   278
  let
walther@60324
   279
    fun dest_num t =
walther@60324
   280
      (case try HOLogic.dest_number t of
walther@60324
   281
        SOME (T, i) => SOME (Free (signed_string_of_int i, T))
walther@60324
   282
      | NONE => NONE);
walther@60324
   283
    fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
walther@60324
   284
      | to_str (t as (u1 $ u2)) =
walther@60324
   285
          (case dest_num t of SOME t' => t' | NONE => to_str u1 $ to_str u2)
walther@60324
   286
      | to_str t = perhaps dest_num t;
walther@60324
   287
  in to_str end
walther@60324
   288
walther@60317
   289
val term_of_num = HOLogic.mk_number;
walther@60317
   290
fun num_of_term t = t |> HOLogic.dest_number |> snd;
walther@60324
   291
(* accomodate string-representation for int to term-orders *)
walther@60322
   292
fun to_string t = t |> num_of_term |> LibraryC.string_of_int'
neuper@52064
   293
walther@59841
   294
fun is_const (Const _) = true | is_const _ = false;
walther@59841
   295
fun is_variable (t as Free _) = not (is_num t)
walther@59841
   296
  | is_variable _ = false;
wneuper@59392
   297
fun is_Free (Free _) = true | is_Free _ = false;
neuper@38025
   298
fun is_fun_id (Const _) = true
neuper@38025
   299
  | is_fun_id (Free _) = true
neuper@38025
   300
  | is_fun_id _ = false;
neuper@38025
   301
fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
neuper@38025
   302
  | is_f_x _ = false;
walther@59841
   303
(* precondition: TermC.is_atom v andalso TermC.is_atom c *)
walther@59841
   304
fun variable_constant_pair (v, c) =
walther@59841
   305
  if (is_variable v andalso (is_const c orelse is_num c)) orelse
walther@59841
   306
     (is_variable c andalso (is_const v orelse is_num v))
walther@59841
   307
  then true
walther@59841
   308
  else false
wneuper@59392
   309
wneuper@59392
   310
fun vars t =
neuper@38025
   311
  let
wneuper@59392
   312
    fun scan vs (Const _) = vs
walther@60317
   313
      | scan vs (t as Free _) = (*if is_num' s then vs else*) t :: vs
wneuper@59392
   314
      | scan vs (t as Var _) = t :: vs
wneuper@59392
   315
      | scan vs (Bound _) = vs 
wneuper@59392
   316
      | scan vs (Abs (_, _, t)) = scan vs t
neuper@38025
   317
      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
walther@60017
   318
  in ((distinct op =) o (scan [])) t end;
walther@60017
   319
fun vars' ts = ts |> map vars |> flat |> distinct op =
walther@59998
   320
wneuper@59392
   321
(* bypass Isabelle's Pretty, which requires ctxt *)
neuper@38025
   322
fun ids2str t =
neuper@38025
   323
  let
wneuper@59392
   324
    fun scan vs (Const (s, _)) = if is_num' s then vs else s :: vs
wneuper@59392
   325
      | scan vs (Free (s, _)) = if is_num' s then vs else s :: vs
wneuper@59392
   326
      | scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs
wneuper@59392
   327
      | scan vs (Bound _) = vs 
wneuper@59392
   328
      | scan vs (Abs (s, _, t)) = scan (s :: vs) t
neuper@38025
   329
      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
walther@60017
   330
  in ((distinct op =) o (scan [])) t end;
wneuper@59392
   331
fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false;
wneuper@59394
   332
(* instantiate #prop thm with bound variables (as Free) *)
wneuper@59394
   333
fun inst_bdv [] t = t
wneuper@59394
   334
  | inst_bdv (instl: (term*term) list) t =
wneuper@59394
   335
    let
wneuper@59394
   336
      fun subst (v as Var((s, _), T)) = 
wneuper@59394
   337
          (case Symbol.explode s of
wneuper@59394
   338
            "b"::"d"::"v"::_ => if_none (assoc(instl,Free(s,T))) (Free(s,T))
wneuper@59394
   339
          | _ => v)
wneuper@59394
   340
        | subst (Abs(a, T, body)) = Abs(a, T, subst body)
wneuper@59394
   341
        | subst (f $ t') = subst f $ subst t'
wneuper@59394
   342
        | subst t = if_none (assoc (instl, t)) t
wneuper@59394
   343
    in  subst t  end;
neuper@38025
   344
wneuper@59494
   345
(* is a term a substitution for a bdv as found in programs and tactics *)
neuper@42426
   346
fun is_bdv_subst (Const ("List.list.Cons", _) $
wneuper@59494
   347
      (Const ("Product_Type.Pair", _) $ str $ _) $ _) = is_bdv (HOLogic.dest_string str)
neuper@42426
   348
  | is_bdv_subst _ = false;
neuper@42426
   349
walther@59636
   350
(* this shall be improved due to future requirements *)
walther@59636
   351
fun guess_bdv_typ t = t |> vars |> hd |> type_of
walther@59636
   352
wneuper@59392
   353
fun free2str (Free (s, _)) = s
walther@59962
   354
  | free2str t = raise ERROR ("free2str not for " ^ UnparseC.term t);
wneuper@59392
   355
fun str_of_free_opt (Free (s, _)) = SOME s
neuper@52103
   356
  | str_of_free_opt _ = NONE
neuper@38025
   357
wneuper@59392
   358
(* compare Logic.unvarify_global, which rejects Free *)
wneuper@59392
   359
fun var2free (t as Const _) = t
wneuper@59392
   360
  | var2free (t as Free _) = t
wneuper@59392
   361
  | var2free (Var((s, _), T)) = Free (s,T)
wneuper@59392
   362
  | var2free (t as Bound _) = t 
wneuper@59392
   363
  | var2free (Abs(s, T, t)) = Abs(s, T, var2free t)
neuper@38025
   364
  | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
neuper@38025
   365
  
wenzelm@60223
   366
\<^isac_test>\<open>
wneuper@59392
   367
(* Logic.varify does NOT take care of 'Free ("1", _)'*)
wneuper@59392
   368
fun free2var (t as Const _) = t
wneuper@59390
   369
  | free2var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T)
wneuper@59392
   370
  | free2var (t as Var _) = t
wneuper@59392
   371
  | free2var (t as Bound _) = t 
neuper@38025
   372
  | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
neuper@38025
   373
  | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
wenzelm@60223
   374
\<close>
neuper@38025
   375
neuper@38025
   376
fun mk_listT T = Type ("List.list", [T]);
wneuper@59392
   377
fun list_const T = Const ("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
wneuper@59392
   378
fun list2isalist T [] = Const ("List.list.Nil", mk_listT T)
wneuper@59392
   379
  | list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts);
neuper@38025
   380
wneuper@59392
   381
fun isapair2pair (Const ("Product_Type.Pair",_) $ a $ b) = (a, b)
neuper@38025
   382
  | isapair2pair t = 
walther@59962
   383
    raise ERROR ("isapair2pair called with " ^ UnparseC.term t);
neuper@38025
   384
fun isalist2list ls =
neuper@38025
   385
  let
wneuper@59392
   386
    fun get es (Const("List.list.Cons", _) $ t $ ls) = get (t :: es) ls
wneuper@59392
   387
      | get es (Const("List.list.Nil", _)) = es
wneuper@59403
   388
      | get _ t = raise TERM ("isalist2list applied to NON-list: ", [t])
neuper@38025
   389
  in (rev o (get [])) ls end;
neuper@38025
   390
wneuper@59392
   391
fun is_list ((Const ("List.list.Cons", _)) $ _ $ _) = true
wneuper@59392
   392
  | is_list _ = false;
walther@59842
   393
fun dest_listT (Type ("List.list", [T])) = T
walther@59842
   394
  | dest_listT T = raise TYPE ("dest_listT: list type expected", [T], []);
walther@59842
   395
fun is_bool_list t =
walther@59842
   396
  (if dest_listT (Term.type_of t) = HOLogic.boolT then true else false)
walther@59842
   397
  handle TYPE _ => false
walther@59842
   398
walther@59842
   399
wneuper@59392
   400
fun dest_binop_typ (Type ("fun", [range, Type ("fun", [arg2, arg1])])) = (arg1, arg2, range)
wneuper@59392
   401
  | dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary";
wneuper@59392
   402
fun dest_equals (Const("HOL.eq", _) $ t $ u)  =  (t, u) (* Pure/logic.ML: Const ("==", ..*)
wneuper@59390
   403
  | dest_equals t = raise TERM ("dest_equals'", [t]);
wneuper@59390
   404
fun is_equality (Const("HOL.eq",_) $ _ $ _)  =  true  (* logic.ML: Const("=="*)
neuper@38025
   405
  | is_equality _ = false;
wneuper@59392
   406
fun mk_equality (t, u) = (Const("HOL.eq", [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u); 
wneuper@59390
   407
fun is_expliceq (Const("HOL.eq",_) $ (Free _) $ _)  =  true
neuper@38025
   408
  | is_expliceq _ = false;
wneuper@59390
   409
fun strip_trueprop (Const ("HOL.Trueprop", _) $ t) = t
neuper@38025
   410
  | strip_trueprop t = t;
neuper@38025
   411
wneuper@59392
   412
(* (A1==>...An==>B) goes to (A1==>...An==>)   Pure/logic.ML: term -> term list*)
wneuper@59403
   413
fun strip_imp_prems' (Const ("Pure.imp", _) $ A $ t) = 
wneuper@59392
   414
    let
wneuper@59403
   415
      fun coll_prems As (Const("Pure.imp", _) $ A $ t) = 
wneuper@59392
   416
          coll_prems (As $ (Logic.implies $ A)) t
wneuper@59392
   417
        | coll_prems As _ = SOME As
neuper@38025
   418
    in coll_prems (Logic.implies $ A) t end
wneuper@59392
   419
  | strip_imp_prems' _ = NONE;  (* *)
neuper@38025
   420
wneuper@59392
   421
(* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *)
wneuper@59403
   422
fun ins_concl (Const ("Pure.imp", _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
wneuper@59403
   423
  | ins_concl (Const ("Pure.imp", _) $ A    ) B = Logic.implies $ A $ B
wneuper@59390
   424
  | ins_concl t B =  raise TERM ("ins_concl", [t, B]);
neuper@38025
   425
wneuper@59392
   426
fun vperm (Var _, Var _) = true  (* 2002 Pure/thm.ML *)
neuper@38025
   427
  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
neuper@38025
   428
  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
neuper@38025
   429
  | vperm (t, u) = (t = u);
neuper@38025
   430
neuper@38025
   431
(*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
neuper@38025
   432
fun mem_term (_, []) = false
wneuper@59392
   433
  | mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts);
wneuper@59390
   434
fun subset_term ([], _) = true
wneuper@59390
   435
  | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys);
neuper@38025
   436
fun eq_set_term (xs, ys) =
neuper@38025
   437
    xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
neuper@38025
   438
(*a total, irreflexive ordering on index names*)
wneuper@59392
   439
fun xless ((a, i), (b, j): indexname) = i<j  orelse  (i = j andalso a < b);
neuper@38025
   440
(*a partial ordering (not reflexive) for atomic terms*)
wneuper@59392
   441
fun atless (Const (a, _), Const (b, _)) = a < b
wneuper@59392
   442
  | atless (Free (a, _), Free (b, _)) = a < b
wneuper@59392
   443
  | atless (Var (v, _), Var (w, _)) = xless (v, w)
wneuper@59392
   444
  | atless (Bound i, Bound j) =  i < j
wneuper@59392
   445
  | atless _ = false;
neuper@38025
   446
(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
neuper@38025
   447
fun insert_aterm (t,us) =
neuper@38025
   448
  let fun inserta [] = [t]
neuper@38025
   449
        | inserta (us as u::us') =
neuper@38025
   450
              if atless(t,u) then t::us
neuper@38025
   451
              else if t=u then us (*duplicate*)
wneuper@59392
   452
              else u :: inserta us'
wneuper@59392
   453
  in inserta us end;
neuper@38025
   454
wneuper@59390
   455
(* Accumulates the Vars in the term, suppressing duplicates *)
neuper@38025
   456
fun add_term_vars (t, vars: term list) = case t of
wneuper@59392
   457
    Var   _ => insert_aterm (t, vars)
wneuper@59392
   458
  | Abs (_, _, body) => add_term_vars (body, vars)
wneuper@59392
   459
  | f$t =>  add_term_vars (f, add_term_vars (t, vars))
neuper@38025
   460
  | _ => vars;
wneuper@59390
   461
fun term_vars t = add_term_vars (t, []);
neuper@38025
   462
wneuper@59392
   463
(*2002 Pure/thm.ML *)
wneuper@59392
   464
fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
neuper@38025
   465
(*2002 fun decomp_simp, Pure/thm.ML *)
wneuper@59390
   466
fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs);
neuper@38025
   467
neuper@38025
   468
neuper@38025
   469
fun pairT T1 T2 = Type ("*", [T1, T2]);
neuper@38025
   470
fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
wneuper@59392
   471
fun pairt t1 t2 = Const ("Product_Type.Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
neuper@38025
   472
neuper@38025
   473
fun mk_factroot op_(*=thy.sqrt*) T fact root = 
neuper@38034
   474
  Const ("Groups.times_class.times", [T, T] ---> T) $ (term_of_num T fact) $
wneuper@59392
   475
    (Const (op_, T --> T) $ term_of_num T root);
walther@60317
   476
fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ HOLogic.mk_number ntyp n;
walther@60317
   477
fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ HOLogic.mk_number ntyp n $ v;
wneuper@59390
   478
fun mk_num_op_num T1 T2 (op_, Top) n1 n2 =
walther@60317
   479
  Const (op_, Top) $ HOLogic.mk_number T1 n1 $ HOLogic.mk_number T2 n2;
wneuper@59392
   480
fun mk_thmid thmid n1 n2 = 
wneuper@59392
   481
  thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
wneuper@59392
   482
fun mk_add t1 t2 =
wneuper@59392
   483
  let
wneuper@59392
   484
    val (T1, T2) = (type_of t1, type_of t2)
wneuper@59392
   485
  in
wneuper@59392
   486
    if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2])
wneuper@59392
   487
    else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2)
wneuper@59392
   488
  end;
neuper@38025
   489
neuper@38025
   490
(** transform binary numeralsstrings **)
walther@59875
   491
val numbers_to_string = ThmC_Def.num_to_Free
walther@59872
   492
val uminus_to_string = ThmC_Def.uminus_to_string
neuper@38025
   493
wneuper@59392
   494
fun mk_Free (s,T) = Free (s, T);
wneuper@59392
   495
fun mk_free T s =  Free (s, T);
neuper@38025
   496
neuper@38025
   497
(*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
wneuper@59392
   498
fun subst_bound (arg, t) =
wneuper@59392
   499
  let
wneuper@59392
   500
    fun subst (t as Bound i, lev) =
wneuper@59392
   501
        if i < lev then t (*var is locally bound*)
wneuper@59392
   502
        else if i = lev then incr_boundvars lev arg
wneuper@59392
   503
        else Bound (i - 1) (*loose: change it*)
wneuper@59392
   504
      | subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
wneuper@59392
   505
      | subst (f$t, lev) =  subst(f, lev)  $  subst(t, lev)
wneuper@59392
   506
      | subst (t, _) = t
wneuper@59392
   507
  in subst (t, 0)  end;
neuper@38025
   508
walther@59691
   509
(* instantiate let; necessary for scan_up1 *)
wneuper@59394
   510
fun inst_abs (Const sT) = Const sT
wneuper@59393
   511
  | inst_abs (Free sT) = Free sT
wneuper@59393
   512
  | inst_abs (Bound n) = Bound n
wneuper@59393
   513
  | inst_abs (Var iT) = Var iT
wneuper@59393
   514
  | inst_abs (Const ("HOL.Let",T1) $ e $ (Abs (v, T2, b))) = 
wneuper@59394
   515
    let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*)
wneuper@59393
   516
    in Const ("HOL.Let", T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end
wneuper@59393
   517
  | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2
wneuper@59393
   518
  | inst_abs t = t;
neuper@38025
   519
neuper@38037
   520
(* for parse and parse_patt: fix all types to real *)
neuper@38025
   521
fun T_a2real (Type (s, [])) = 
wneuper@59394
   522
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
neuper@38025
   523
  | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
neuper@38025
   524
  | T_a2real (TFree (s, srt)) = 
wneuper@59394
   525
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
wneuper@59394
   526
  | T_a2real (TVar (("DUMMY", _), _)) = HOLogic.realT
neuper@38037
   527
  | T_a2real (TVar ((s, i), srt)) = 
wneuper@59394
   528
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TVar ((s, i), srt)
neuper@38025
   529
fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) 
neuper@38025
   530
  | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
neuper@38025
   531
  | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
neuper@38025
   532
  | typ_a2real (Bound i) = (Bound i)
neuper@38025
   533
  | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
neuper@38025
   534
  | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
neuper@38025
   535
wneuper@59394
   536
(* TODO clarify parse with Test_Isac *)
wneuper@59394
   537
fun parseold thy str = (* before 2002 *)
wenzelm@60226
   538
  \<^try>\<open>
wenzelm@60226
   539
    let val t = ((*typ_a2real o*) numbers_to_string) (Syntax.read_term_global thy str)
wenzelm@60226
   540
    in Thm.global_cterm_of thy t end\<close>;
wneuper@59394
   541
fun parseN thy str = (* introduced 2002 *)
wenzelm@60226
   542
  \<^try>\<open>
wenzelm@60226
   543
    let val t = (*(typ_a2real o numbers_to_string)*) (Syntax.read_term_global thy str)
wenzelm@60226
   544
    in Thm.global_cterm_of thy t end\<close>;
wneuper@59394
   545
fun parse thy str = (* introduced 2010 *)
wenzelm@60226
   546
  \<^try>\<open>
wenzelm@60226
   547
    let val t = (typ_a2real o numbers_to_string) (Syntax.read_term_global thy str)
wenzelm@60226
   548
    in Thm.global_cterm_of thy t end\<close>;
neuper@38025
   549
neuper@41931
   550
(*WN110317 parseNEW will replace parse after introduction of ctxt completed*)
wenzelm@60226
   551
fun parseNEW ctxt str = \<^try>\<open>Syntax.read_term ctxt str |> numbers_to_string\<close>;
wneuper@59582
   552
fun parseNEW' ctxt str = 
wneuper@59582
   553
  case parseNEW ctxt str of
wneuper@59582
   554
    SOME t => t
wneuper@59582
   555
  | NONE => raise TERM ("NO parseNEW' for " ^ str, [])
walther@59952
   556
fun parseNEW'' thy str =
walther@59952
   557
  case parseNEW (ThyC.to_ctxt thy) str of
walther@59952
   558
    SOME t => t
walther@59952
   559
  | NONE => raise TERM ("NO parseNEW'' for " ^ str, [])
wneuper@59582
   560
neuper@48879
   561
(* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation
neuper@48879
   562
  WN130613 probably compare to 
neuper@48879
   563
  http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*)
walther@59875
   564
fun parse_patt thy str = (thy, str)
walther@59879
   565
  |>> ThyC.to_ctxt 
walther@59875
   566
  |-> Proof_Context.read_term_pattern
walther@60317
   567
(*|> numbers_to_string   TODO drop*)
walther@59875
   568
  |> typ_a2real;       (*TODO drop*)
walther@59879
   569
fun str2term str = parse_patt (ThyC.get_theory "Isac_Knowledge") str
neuper@38025
   570
walther@60320
   571
fun is_atom (Const ("Num.numeral_class.numeral", _) $ _) = true
walther@60320
   572
  | is_atom (Const ("Groups.one_class.one", _)) = true
walther@60320
   573
  | is_atom (Const ("Groups.zero_class.zero", _)) = true
neuper@38025
   574
  | is_atom (Const _) = true
neuper@38025
   575
  | is_atom (Free _) = true
neuper@38025
   576
  | is_atom (Var _) = true
neuper@38025
   577
  | is_atom _ = false;
walther@60322
   578
fun string_of_atom (t as Const ("Num.numeral_class.numeral", _) $ _) = to_string t
walther@60322
   579
  | string_of_atom (t as Const ("Groups.one_class.one", _)) = to_string t
walther@60322
   580
  | string_of_atom (t as Const ("Groups.zero_class.zero", _)) = to_string t
walther@60322
   581
  | string_of_atom (Const (str, _)) = str
walther@60322
   582
  | string_of_atom (Free (str, _)) = str
walther@60322
   583
  | string_of_atom (Var ((str, int), _)) = str ^ "_" ^ string_of_int int
walther@60322
   584
  | string_of_atom _ = "DUMMY-string_of_atom";
neuper@38025
   585
wneuper@59394
   586
(* from Pure/term.ML; reports if ALL Free's have found a substitution
wneuper@59394
   587
   (required for evaluating the preconditions of _incomplete_ models) *)
wneuper@59394
   588
fun subst_atomic_all [] t = (false (*TODO may be 'true' for some terms ?*), t)
wneuper@59394
   589
  | subst_atomic_all instl t =
wneuper@59394
   590
    let
wneuper@59394
   591
      fun subst (Abs (a, T, body)) = 
wneuper@59394
   592
          let
wneuper@59394
   593
            val (all, body') = subst body
wneuper@59394
   594
          in (all, Abs(a, T, body')) end
wneuper@59394
   595
        | subst (f$tt) = 
wneuper@59394
   596
	        let
wneuper@59394
   597
	          val (all1, f') = subst f
wneuper@59394
   598
	          val (all2, tt') = subst tt
wneuper@59394
   599
	        in (all1 andalso all2, f' $ tt') end
wneuper@59394
   600
        | subst (t as Free _) = 
wneuper@59394
   601
	        if is_num t then (true, t) (*numerals cannot be subst*)
wneuper@59394
   602
	        else (case assoc (instl, t) of
wneuper@59394
   603
					  SOME t' => (true, t')
wneuper@59394
   604
				  | NONE => (false, t))
wneuper@59394
   605
        | subst t = (true, if_none (assoc(instl,t)) t)
wneuper@59394
   606
    in subst t end;
wneuper@59389
   607
wneuper@59507
   608
fun op contains_one_of (thm, ids) =
wneuper@59507
   609
  Term.exists_Const (fn id => member op= ids id) (Thm.prop_of thm)
wneuper@59507
   610
wneuper@59532
   611
fun var_for vs (t as Const (str, _)) id = if id = strip_thy str then t :: vs else vs
wneuper@59532
   612
  | var_for vs (t as Free (str, _)) id = if id = str then t :: vs else vs
wneuper@59532
   613
  | var_for vs (t as Var (idn, _)) id = if id = Term.string_of_vname idn then t :: vs else vs
wneuper@59532
   614
  | var_for vs (Bound _) _ = vs
wneuper@59532
   615
  | var_for vs (Abs (_, _, t)) id = var_for vs t id
wneuper@59532
   616
  | var_for vs (t1 $ t2) id = (var_for vs t1 id) @ (var_for vs t2 id)
wneuper@59532
   617
wneuper@59532
   618
val poly_consts = (* TODO: adopt syntax-const from Isabelle*)
wneuper@59532
   619
  ["Groups.plus_class.plus", "Groups.minus_class.minus",
wneuper@59532
   620
  "Rings.divide_class.divide", "Groups.times_class.times",
walther@60317
   621
  "Transcendental.powr",
walther@60317
   622
  "Num.numeral_class.numeral", "Num.num.Bit0", "Num.num.Bit1", "Num.num.One",
walther@60317
   623
  "Groups.uminus_class.uminus", "Groups.one_class.one"];
wneuper@59532
   624
(* treat Free, Const, Var as variables in polynomials *)
wneuper@59532
   625
fun vars_of t =
wneuper@59532
   626
  let
wneuper@59532
   627
    val var_ids = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord
walther@60017
   628
  in (map (var_for [] t) var_ids) |> flat |> distinct op = end
wneuper@59532
   629
wneuper@59577
   630
(* this may decompose an object-language isa-list;
wneuper@59577
   631
   use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
wneuper@59577
   632
fun dest_list' t = if is_list t then isalist2list t  else [t];
wneuper@59577
   633
walther@59842
   634
fun negat (Const ("HOL.Not", _) $ P, P') = P = P'
walther@59842
   635
  | negat _ = false
walther@59842
   636
fun negates p1 p2 = negat (p1, p2) orelse negat (swap (p1, p2));
walther@59842
   637
wneuper@59580
   638
fun raise_type_conflicts ts =
wneuper@59580
   639
  let
wneuper@59580
   640
    val dups = duplicates (op =) (map (fst o dest_Free) ts)
walther@60266
   641
    val confl = filter (fn Free (str, _) => member op = dups str
walther@60266
   642
                         | _ => false) ts
wneuper@59580
   643
  in
wneuper@59580
   644
    if confl = []
wneuper@59580
   645
    then ()
wneuper@59582
   646
    else raise TYPE ("formalisation inconsistent w.r.t. type inference: ",
wneuper@59580
   647
      map (snd o dest_Free)confl, confl)
wneuper@59580
   648
  end
wneuper@59580
   649
walther@59716
   650
(* expects t as Const *)
walther@59717
   651
fun contains_Const_typeless ts t = (t
walther@59716
   652
  |> strip_comb |> fst
walther@59716
   653
  |> member (fn (t1, t2) => fst (dest_Const t1) = fst (dest_Const t2)) ts
walther@59717
   654
) handle TERM("dest_Const", _) => raise TERM ("contains_Const_typeless", [t])
walther@59716
   655
walther@59916
   656
(* WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02 *)
walther@59916
   657
fun sym_trm trm =
walther@59916
   658
  let
walther@59916
   659
    val (lhs, rhs) = (dest_equals o strip_trueprop o Logic.strip_imp_concl) trm
walther@59916
   660
    val trm' = case strip_imp_prems' trm of
walther@59916
   661
	      NONE => mk_equality (rhs, lhs)
walther@59916
   662
	    | SOME cs => ins_concl cs (mk_equality (rhs, lhs))
walther@59916
   663
  in trm' end
walther@59916
   664
walther@59716
   665
walther@60317
   666
end