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