src/Tools/isac/BaseDefinitions/termC.sml
author wenzelm
Thu, 16 Sep 2021 17:23:54 +0200
changeset 60405 d4ebe139100d
parent 60400 2d97d160a183
child 60414 b25aaadac5f3
permissions -rw-r--r--
separate realpow constant, with additional cases not covered by Transcendental.powr;
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@60391
    29
  val lhs: term -> term
walther@60391
    30
  val rhs: term -> term
walther@60391
    31
walther@60317
    32
  val mk_frac: typ -> int * (int * int) -> term
walther@60324
    33
  val numerals_to_Free: term -> term
walther@59861
    34
  val term_of_num: typ -> int -> term
walther@59861
    35
  val num_of_term: term -> int
walther@60322
    36
  val to_string: term -> string
walther@59861
    37
  val int_of_str: string -> int
walther@59861
    38
  val isastr_of_int: int -> string
walther@60317
    39
  val int_opt_of_string: string -> int option
wneuper@59390
    40
walther@59861
    41
  val isalist2list: term -> term list
walther@59861
    42
  val list2isalist: typ -> term list -> term
walther@59861
    43
  val isapair2pair: term -> term * term (* rename to dest_pair, compare HOLogic.dest_string *)
wneuper@59390
    44
walther@59861
    45
  val is_atom: term -> bool
walther@60322
    46
  val string_of_atom: term -> string
walther@59861
    47
  val is_variable: term -> bool
walther@59861
    48
  val is_bdv: string -> bool
walther@59861
    49
  val is_bdv_subst: term -> bool
walther@59861
    50
  val guess_bdv_typ: term -> typ
walther@59861
    51
  val is_equality: term -> bool
walther@59861
    52
  val is_expliceq: term -> bool
walther@59861
    53
  val is_f_x: term -> bool
walther@59861
    54
  val is_list: term -> bool
walther@59861
    55
  val is_bool_list: term -> bool
walther@59861
    56
  val dest_listT: typ -> typ
walther@59861
    57
  val is_num: term -> bool
walther@59861
    58
  val is_num': string -> bool
walther@60317
    59
  val string_of_num: term -> string
walther@59861
    60
  val variable_constant_pair: term * term -> bool
wneuper@59390
    61
walther@59861
    62
  val mk_add: term -> term -> term
walther@59861
    63
  val mk_free: typ -> string -> term
walther@59861
    64
  val mk_equality: term * term -> term
walther@59861
    65
  val mk_factroot: string -> typ -> int -> int -> term
walther@59861
    66
  val mk_Free: string * typ -> term
walther@59861
    67
  val mk_thmid: string -> string -> string -> string
walther@59861
    68
  val mk_num_op_num: typ -> typ -> string * typ -> int -> int -> term
walther@59861
    69
  val mk_num_op_var: term -> string -> typ -> typ -> int -> term
walther@59861
    70
  val mk_var_op_num: term -> string -> typ -> typ -> int -> term
wneuper@59390
    71
walther@59861
    72
  val matches: theory -> term -> term -> bool
walther@60340
    73
  val parse: theory -> string -> cterm option
walther@60340
    74
  val parseN: theory -> string -> cterm option
walther@59861
    75
  val parseNEW: Proof.context -> string -> term option
walther@59861
    76
  val parseNEW': Proof.context -> string -> term
walther@59952
    77
  val parseNEW'': theory -> string -> term
walther@60340
    78
  val parseold: theory -> string -> cterm option
walther@60333
    79
  val parse_strict: theory -> string -> term
walther@59861
    80
  val parse_patt: theory -> string -> term
walther@59861
    81
  val perm: term -> term -> bool
wneuper@59390
    82
walther@59861
    83
  val str_of_free_opt: term -> string option
walther@59861
    84
  val str_of_int: int -> string
walther@59861
    85
  val str2term: string -> term
walther@59861
    86
  val strip_imp_prems': term -> term option
walther@59861
    87
  val subst_atomic_all: (term * term) list -> term -> bool * term
walther@59861
    88
  val term_detail2str: term -> string
walther@59861
    89
walther@59861
    90
  val pairt: term -> term -> term
walther@59861
    91
  val pairT: typ -> typ -> typ
walther@59861
    92
  val raise_type_conflicts: term list -> unit
walther@59861
    93
  val strip_trueprop: term -> term
walther@59861
    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)) =
walther@60335
   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@60278
   252
walther@60317
   253
val int_opt_of_string = ThmC_Def.int_opt_of_string
walther@60317
   254
fun is_num' str = case int_opt_of_string str of SOME _ => true | NONE => false;
walther@59871
   255
walther@60335
   256
fun is_num (Const (\<^const_name>\<open>numeral\<close>, _) $ _) = true
walther@60335
   257
  | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $
walther@60335
   258
    (Const (\<^const_name>\<open>numeral\<close>, _) $ _)) = true
walther@60335
   259
  | is_num (Const (\<^const_name>\<open>one_class.one\<close>, _)) = true
walther@60335
   260
  | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $ Const (\<^const_name>\<open>one_class.one\<close>, _)) = true
walther@60335
   261
  | is_num (Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = true
walther@60335
   262
  | is_num (Const (\<^const_name>\<open>uminus\<close>, _) $ Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = true
walther@60317
   263
  | is_num _ = false;
walther@59871
   264
walther@60317
   265
fun string_of_num n = (n |> HOLogic.dest_number |> snd |> string_of_int)
walther@60317
   266
walther@60391
   267
(* refer to Thm.lhs_of *)
walther@60391
   268
fun lhs (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ l $ _) = l
walther@60391
   269
  | lhs t = raise ERROR("lhs called with (" ^ UnparseC.term t ^ ")");
walther@60391
   270
fun rhs (Const (\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ r) = r
walther@60391
   271
  | rhs t = raise ERROR("rhs called with (" ^ UnparseC.term t ^ ")");
walther@60391
   272
walther@60391
   273
walther@60335
   274
fun mk_negative T t = Const (\<^const_name>\<open>uminus\<close>, T --> T) $ t
walther@60317
   275
fun mk_frac T (sg, (i1, i2)) =
walther@60317
   276
  if sg = 1 then
walther@60317
   277
    if i2 = 1 then HOLogic.mk_number T i1
walther@60335
   278
    else Const (\<^const_name>\<open>divide_class.divide\<close>, T --> T --> T) $
walther@60317
   279
      HOLogic.mk_number T i1 $ HOLogic.mk_number T i2
walther@60317
   280
  else (*take negative*)
walther@60317
   281
    if i2 = 1 then mk_negative T (HOLogic.mk_number T i1)
walther@60335
   282
    else Const (\<^const_name>\<open>divide_class.divide\<close>, T --> T --> T) $
walther@60317
   283
      mk_negative T (HOLogic.mk_number T i1) $ HOLogic.mk_number T i2
walther@60317
   284
walther@60324
   285
val numerals_to_Free = (* Makarius 100308 *)
walther@60324
   286
  let
walther@60324
   287
    fun dest_num t =
walther@60324
   288
      (case try HOLogic.dest_number t of
walther@60324
   289
        SOME (T, i) => SOME (Free (signed_string_of_int i, T))
walther@60324
   290
      | NONE => NONE);
walther@60324
   291
    fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
walther@60324
   292
      | to_str (t as (u1 $ u2)) =
walther@60324
   293
          (case dest_num t of SOME t' => t' | NONE => to_str u1 $ to_str u2)
walther@60324
   294
      | to_str t = perhaps dest_num t;
walther@60324
   295
  in to_str end
walther@60324
   296
walther@60317
   297
val term_of_num = HOLogic.mk_number;
walther@60317
   298
fun num_of_term t = t |> HOLogic.dest_number |> snd;
walther@60324
   299
(* accomodate string-representation for int to term-orders *)
wenzelm@60383
   300
fun to_string t = t |> num_of_term |> signed_string_of_int
neuper@52064
   301
walther@59841
   302
fun is_variable (t as Free _) = not (is_num t)
walther@59841
   303
  | is_variable _ = false;
wneuper@59392
   304
fun is_Free (Free _) = true | is_Free _ = false;
neuper@38025
   305
fun is_fun_id (Const _) = true
neuper@38025
   306
  | is_fun_id (Free _) = true
neuper@38025
   307
  | is_fun_id _ = false;
neuper@38025
   308
fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
neuper@38025
   309
  | is_f_x _ = false;
walther@59841
   310
(* precondition: TermC.is_atom v andalso TermC.is_atom c *)
walther@60400
   311
fun is_const (Const _) = true | is_const _ = false;
walther@59841
   312
fun variable_constant_pair (v, c) =
walther@59841
   313
  if (is_variable v andalso (is_const c orelse is_num c)) orelse
walther@59841
   314
     (is_variable c andalso (is_const v orelse is_num v))
walther@59841
   315
  then true
walther@59841
   316
  else false
wneuper@59392
   317
wneuper@59392
   318
fun vars t =
neuper@38025
   319
  let
wneuper@59392
   320
    fun scan vs (Const _) = vs
walther@60317
   321
      | scan vs (t as Free _) = (*if is_num' s then vs else*) t :: vs
wneuper@59392
   322
      | scan vs (t as Var _) = t :: vs
wneuper@59392
   323
      | scan vs (Bound _) = vs 
wneuper@59392
   324
      | scan vs (Abs (_, _, t)) = scan vs t
neuper@38025
   325
      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
walther@60017
   326
  in ((distinct op =) o (scan [])) t end;
walther@60017
   327
fun vars' ts = ts |> map vars |> flat |> distinct op =
walther@59998
   328
wneuper@59392
   329
(* bypass Isabelle's Pretty, which requires ctxt *)
neuper@38025
   330
fun ids2str t =
neuper@38025
   331
  let
walther@60333
   332
    fun scan vs (t as Const (s, _) $ arg) =
walther@60333
   333
        if is_num t then vs else scan (s :: vs) arg
walther@60333
   334
      | scan vs (Const (s as "Partial_Fractions.AA", _)) = s :: vs (*how get rid of spec.case?*)
walther@60356
   335
      | scan vs (Const (s as "Partial_Fractions.BB", _)) = s :: vs (*how get rid of spec.case?*)
walther@60333
   336
      | scan vs (Const _) = vs
wneuper@59392
   337
      | scan vs (Free (s, _)) = if is_num' s then vs else s :: vs
wneuper@59392
   338
      | scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs
wneuper@59392
   339
      | scan vs (Bound _) = vs 
wneuper@59392
   340
      | scan vs (Abs (s, _, t)) = scan (s :: vs) t
neuper@38025
   341
      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
walther@60333
   342
  in ((distinct op =) o (scan [])) t
walther@60333
   343
  end;
wneuper@59392
   344
fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false;
wneuper@59394
   345
(* instantiate #prop thm with bound variables (as Free) *)
wneuper@59394
   346
fun inst_bdv [] t = t
wneuper@59394
   347
  | inst_bdv (instl: (term*term) list) t =
wneuper@59394
   348
    let
wneuper@59394
   349
      fun subst (v as Var((s, _), T)) = 
wneuper@59394
   350
          (case Symbol.explode s of
wneuper@59394
   351
            "b"::"d"::"v"::_ => if_none (assoc(instl,Free(s,T))) (Free(s,T))
wneuper@59394
   352
          | _ => v)
wneuper@59394
   353
        | subst (Abs(a, T, body)) = Abs(a, T, subst body)
wneuper@59394
   354
        | subst (f $ t') = subst f $ subst t'
wneuper@59394
   355
        | subst t = if_none (assoc (instl, t)) t
wneuper@59394
   356
    in  subst t  end;
neuper@38025
   357
wneuper@59494
   358
(* is a term a substitution for a bdv as found in programs and tactics *)
wenzelm@60309
   359
fun is_bdv_subst (Const (\<^const_name>\<open>Cons\<close>, _) $
wenzelm@60309
   360
      (Const (\<^const_name>\<open>Pair\<close>, _) $ str $ _) $ _) = is_bdv (HOLogic.dest_string str)
neuper@42426
   361
  | is_bdv_subst _ = false;
neuper@42426
   362
walther@59636
   363
(* this shall be improved due to future requirements *)
walther@59636
   364
fun guess_bdv_typ t = t |> vars |> hd |> type_of
walther@59636
   365
wneuper@59392
   366
fun free2str (Free (s, _)) = s
walther@59962
   367
  | free2str t = raise ERROR ("free2str not for " ^ UnparseC.term t);
wneuper@59392
   368
fun str_of_free_opt (Free (s, _)) = SOME s
neuper@52103
   369
  | str_of_free_opt _ = NONE
neuper@38025
   370
wneuper@59392
   371
(* compare Logic.unvarify_global, which rejects Free *)
wneuper@59392
   372
fun var2free (t as Const _) = t
wneuper@59392
   373
  | var2free (t as Free _) = t
wneuper@59392
   374
  | var2free (Var((s, _), T)) = Free (s,T)
wneuper@59392
   375
  | var2free (t as Bound _) = t 
wneuper@59392
   376
  | var2free (Abs(s, T, t)) = Abs(s, T, var2free t)
neuper@38025
   377
  | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
neuper@38025
   378
  
wenzelm@60223
   379
\<^isac_test>\<open>
wneuper@59392
   380
(* Logic.varify does NOT take care of 'Free ("1", _)'*)
wneuper@59392
   381
fun free2var (t as Const _) = t
wneuper@59390
   382
  | free2var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T)
wneuper@59392
   383
  | free2var (t as Var _) = t
wneuper@59392
   384
  | free2var (t as Bound _) = t 
neuper@38025
   385
  | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
neuper@38025
   386
  | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
wenzelm@60223
   387
\<close>
neuper@38025
   388
wenzelm@60309
   389
fun mk_listT T = Type (\<^type_name>\<open>list\<close>, [T]);
wenzelm@60309
   390
fun list_const T = Const (\<^const_name>\<open>Cons\<close>, [T, mk_listT T] ---> mk_listT T);
wenzelm@60309
   391
fun list2isalist T [] = Const (\<^const_name>\<open>Nil\<close>, mk_listT T)
wneuper@59392
   392
  | list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts);
neuper@38025
   393
wenzelm@60309
   394
fun isapair2pair (Const (\<^const_name>\<open>Pair\<close>,_) $ a $ b) = (a, b)
neuper@38025
   395
  | isapair2pair t = 
walther@59962
   396
    raise ERROR ("isapair2pair called with " ^ UnparseC.term t);
neuper@38025
   397
fun isalist2list ls =
neuper@38025
   398
  let
wenzelm@60309
   399
    fun get es (Const(\<^const_name>\<open>Cons\<close>, _) $ t $ ls) = get (t :: es) ls
wenzelm@60309
   400
      | get es (Const(\<^const_name>\<open>Nil\<close>, _)) = es
wneuper@59403
   401
      | get _ t = raise TERM ("isalist2list applied to NON-list: ", [t])
neuper@38025
   402
  in (rev o (get [])) ls end;
neuper@38025
   403
wenzelm@60309
   404
fun is_list ((Const (\<^const_name>\<open>Cons\<close>, _)) $ _ $ _) = true
wneuper@59392
   405
  | is_list _ = false;
wenzelm@60309
   406
fun dest_listT (Type (\<^type_name>\<open>list\<close>, [T])) = T
walther@59842
   407
  | dest_listT T = raise TYPE ("dest_listT: list type expected", [T], []);
walther@59842
   408
fun is_bool_list t =
walther@59842
   409
  (if dest_listT (Term.type_of t) = HOLogic.boolT then true else false)
walther@59842
   410
  handle TYPE _ => false
walther@59842
   411
walther@59842
   412
wenzelm@60309
   413
fun dest_binop_typ (Type (\<^type_name>\<open>fun\<close>, [range, Type (\<^type_name>\<open>fun\<close>, [arg2, arg1])])) = (arg1, arg2, range)
wneuper@59392
   414
  | dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary";
wenzelm@60309
   415
fun dest_equals (Const(\<^const_name>\<open>HOL.eq\<close>, _) $ t $ u)  =  (t, u) (* Pure/logic.ML: Const ("==", ..*)
wneuper@59390
   416
  | dest_equals t = raise TERM ("dest_equals'", [t]);
walther@60335
   417
fun is_equality (Const(\<^const_name>\<open>HOL.eq\<close>,_) $ _ $ _)  =  true  (* logic.ML: Const ("=="*)
neuper@38025
   418
  | is_equality _ = false;
wenzelm@60309
   419
fun mk_equality (t, u) = (Const(\<^const_name>\<open>HOL.eq\<close>, [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u); 
wenzelm@60309
   420
fun is_expliceq (Const(\<^const_name>\<open>HOL.eq\<close>,_) $ (Free _) $ _)  =  true
neuper@38025
   421
  | is_expliceq _ = false;
wenzelm@60309
   422
fun strip_trueprop (Const (\<^const_name>\<open>Trueprop\<close>, _) $ t) = t
neuper@38025
   423
  | strip_trueprop t = t;
neuper@38025
   424
wneuper@59392
   425
(* (A1==>...An==>B) goes to (A1==>...An==>)   Pure/logic.ML: term -> term list*)
wenzelm@60309
   426
fun strip_imp_prems' (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) = 
wneuper@59392
   427
    let
wenzelm@60309
   428
      fun coll_prems As (Const(\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) = 
wneuper@59392
   429
          coll_prems (As $ (Logic.implies $ A)) t
wneuper@59392
   430
        | coll_prems As _ = SOME As
neuper@38025
   431
    in coll_prems (Logic.implies $ A) t end
wneuper@59392
   432
  | strip_imp_prems' _ = NONE;  (* *)
neuper@38025
   433
wneuper@59392
   434
(* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *)
wenzelm@60309
   435
fun ins_concl (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
wenzelm@60309
   436
  | ins_concl (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ A    ) B = Logic.implies $ A $ B
wneuper@59390
   437
  | ins_concl t B =  raise TERM ("ins_concl", [t, B]);
neuper@38025
   438
wneuper@59392
   439
fun vperm (Var _, Var _) = true  (* 2002 Pure/thm.ML *)
neuper@38025
   440
  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
neuper@38025
   441
  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
neuper@38025
   442
  | vperm (t, u) = (t = u);
neuper@38025
   443
neuper@38025
   444
(*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
neuper@38025
   445
fun mem_term (_, []) = false
wneuper@59392
   446
  | mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts);
wneuper@59390
   447
fun subset_term ([], _) = true
wneuper@59390
   448
  | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys);
neuper@38025
   449
fun eq_set_term (xs, ys) =
neuper@38025
   450
    xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
neuper@38025
   451
(*a total, irreflexive ordering on index names*)
wneuper@59392
   452
fun xless ((a, i), (b, j): indexname) = i<j  orelse  (i = j andalso a < b);
neuper@38025
   453
(*a partial ordering (not reflexive) for atomic terms*)
wneuper@59392
   454
fun atless (Const (a, _), Const (b, _)) = a < b
wneuper@59392
   455
  | atless (Free (a, _), Free (b, _)) = a < b
wneuper@59392
   456
  | atless (Var (v, _), Var (w, _)) = xless (v, w)
wneuper@59392
   457
  | atless (Bound i, Bound j) =  i < j
wneuper@59392
   458
  | atless _ = false;
neuper@38025
   459
(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
neuper@38025
   460
fun insert_aterm (t,us) =
neuper@38025
   461
  let fun inserta [] = [t]
neuper@38025
   462
        | inserta (us as u::us') =
neuper@38025
   463
              if atless(t,u) then t::us
neuper@38025
   464
              else if t=u then us (*duplicate*)
wneuper@59392
   465
              else u :: inserta us'
wneuper@59392
   466
  in inserta us end;
neuper@38025
   467
wneuper@59390
   468
(* Accumulates the Vars in the term, suppressing duplicates *)
neuper@38025
   469
fun add_term_vars (t, vars: term list) = case t of
wneuper@59392
   470
    Var   _ => insert_aterm (t, vars)
wneuper@59392
   471
  | Abs (_, _, body) => add_term_vars (body, vars)
wneuper@59392
   472
  | f$t =>  add_term_vars (f, add_term_vars (t, vars))
neuper@38025
   473
  | _ => vars;
wneuper@59390
   474
fun term_vars t = add_term_vars (t, []);
neuper@38025
   475
wneuper@59392
   476
(*2002 Pure/thm.ML *)
wneuper@59392
   477
fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
neuper@38025
   478
(*2002 fun decomp_simp, Pure/thm.ML *)
wneuper@59390
   479
fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs);
neuper@38025
   480
neuper@38025
   481
wenzelm@60310
   482
fun pairT T1 T2 = Type (\<^type_name>\<open>prod\<close>, [T1, T2]);
wenzelm@60310
   483
fun PairT T1 T2 = ([T1, T2] ---> Type (\<^type_name>\<open>prod\<close>, [T1, T2]));
wenzelm@60309
   484
fun pairt t1 t2 = Const (\<^const_name>\<open>Pair\<close>, PairT (type_of t1) (type_of t2)) $ t1 $ t2;
neuper@38025
   485
neuper@38025
   486
fun mk_factroot op_(*=thy.sqrt*) T fact root = 
wenzelm@60309
   487
  Const (\<^const_name>\<open>times\<close>, [T, T] ---> T) $ (term_of_num T fact) $
wneuper@59392
   488
    (Const (op_, T --> T) $ term_of_num T root);
walther@60317
   489
fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ HOLogic.mk_number ntyp n;
walther@60317
   490
fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ HOLogic.mk_number ntyp n $ v;
wneuper@59390
   491
fun mk_num_op_num T1 T2 (op_, Top) n1 n2 =
walther@60317
   492
  Const (op_, Top) $ HOLogic.mk_number T1 n1 $ HOLogic.mk_number T2 n2;
wneuper@59392
   493
fun mk_thmid thmid n1 n2 = 
wneuper@59392
   494
  thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
wneuper@59392
   495
fun mk_add t1 t2 =
wneuper@59392
   496
  let
wneuper@59392
   497
    val (T1, T2) = (type_of t1, type_of t2)
wneuper@59392
   498
  in
wneuper@59392
   499
    if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2])
wenzelm@60309
   500
    else (Const (\<^const_name>\<open>plus\<close>, [T1, T2] ---> T1) $ t1 $ t2)
wneuper@59392
   501
  end;
neuper@38025
   502
neuper@38025
   503
(** transform binary numeralsstrings **)
wneuper@59392
   504
fun mk_Free (s,T) = Free (s, T);
wneuper@59392
   505
fun mk_free T s =  Free (s, T);
neuper@38025
   506
neuper@38025
   507
(*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
wneuper@59392
   508
fun subst_bound (arg, t) =
wneuper@59392
   509
  let
wneuper@59392
   510
    fun subst (t as Bound i, lev) =
wneuper@59392
   511
        if i < lev then t (*var is locally bound*)
wneuper@59392
   512
        else if i = lev then incr_boundvars lev arg
wneuper@59392
   513
        else Bound (i - 1) (*loose: change it*)
wneuper@59392
   514
      | subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
wneuper@59392
   515
      | subst (f$t, lev) =  subst(f, lev)  $  subst(t, lev)
wneuper@59392
   516
      | subst (t, _) = t
wneuper@59392
   517
  in subst (t, 0)  end;
neuper@38025
   518
walther@59691
   519
(* instantiate let; necessary for scan_up1 *)
wneuper@59394
   520
fun inst_abs (Const sT) = Const sT
wneuper@59393
   521
  | inst_abs (Free sT) = Free sT
wneuper@59393
   522
  | inst_abs (Bound n) = Bound n
wneuper@59393
   523
  | inst_abs (Var iT) = Var iT
wenzelm@60309
   524
  | inst_abs (Const (\<^const_name>\<open>Let\<close>,T1) $ e $ (Abs (v, T2, b))) = 
wneuper@59394
   525
    let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*)
wenzelm@60309
   526
    in Const (\<^const_name>\<open>Let\<close>, T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end
wneuper@59393
   527
  | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2
wneuper@59393
   528
  | inst_abs t = t;
neuper@38025
   529
neuper@38037
   530
(* for parse and parse_patt: fix all types to real *)
neuper@38025
   531
fun T_a2real (Type (s, [])) = 
wneuper@59394
   532
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
neuper@38025
   533
  | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
neuper@38025
   534
  | T_a2real (TFree (s, srt)) = 
wneuper@59394
   535
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
wneuper@59394
   536
  | T_a2real (TVar (("DUMMY", _), _)) = HOLogic.realT
neuper@38037
   537
  | T_a2real (TVar ((s, i), srt)) = 
wneuper@59394
   538
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TVar ((s, i), srt)
neuper@38025
   539
fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) 
neuper@38025
   540
  | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
neuper@38025
   541
  | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
neuper@38025
   542
  | typ_a2real (Bound i) = (Bound i)
neuper@38025
   543
  | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
neuper@38025
   544
  | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
neuper@38025
   545
walther@60340
   546
(* TODO clarify parse with Test_Isac *)
wneuper@59394
   547
fun parseold thy str = (* before 2002 *)
wenzelm@60226
   548
  \<^try>\<open>
walther@60337
   549
    let val t = Syntax.read_term_global thy str
wenzelm@60226
   550
    in Thm.global_cterm_of thy t end\<close>;
wneuper@59394
   551
fun parseN thy str = (* introduced 2002 *)
wenzelm@60226
   552
  \<^try>\<open>
walther@60340
   553
    let val t = (*(typ_a2real o numbers_to_string)*) (Syntax.read_term_global thy str)
wenzelm@60226
   554
    in Thm.global_cterm_of thy t end\<close>;
wenzelm@60303
   555
walther@60340
   556
fun parse_strict thy str = typ_a2real (Syntax.read_term_global thy str);
wneuper@59394
   557
fun parse thy str = (* introduced 2010 *)
wenzelm@60226
   558
  \<^try>\<open>
wenzelm@60303
   559
    let val t = parse_strict thy str
wenzelm@60226
   560
    in Thm.global_cterm_of thy t end\<close>;
neuper@38025
   561
neuper@41931
   562
(*WN110317 parseNEW will replace parse after introduction of ctxt completed*)
walther@60337
   563
fun parseNEW ctxt str = \<^try>\<open>Syntax.read_term ctxt str\<close>;
wneuper@59582
   564
fun parseNEW' ctxt str = 
wneuper@59582
   565
  case parseNEW ctxt str of
wneuper@59582
   566
    SOME t => t
wneuper@59582
   567
  | NONE => raise TERM ("NO parseNEW' for " ^ str, [])
walther@59952
   568
fun parseNEW'' thy str =
walther@60360
   569
  case parseNEW (Proof_Context.init_global thy) str of
walther@59952
   570
    SOME t => t
walther@59952
   571
  | NONE => raise TERM ("NO parseNEW'' for " ^ str, [])
wneuper@59582
   572
neuper@48879
   573
(* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation
neuper@48879
   574
  WN130613 probably compare to 
neuper@48879
   575
  http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*)
walther@59875
   576
fun parse_patt thy str = (thy, str)
walther@60360
   577
  |>> Proof_Context.init_global
walther@59875
   578
  |-> Proof_Context.read_term_pattern
walther@60317
   579
(*|> numbers_to_string   TODO drop*)
walther@59875
   580
  |> typ_a2real;       (*TODO drop*)
walther@59879
   581
fun str2term str = parse_patt (ThyC.get_theory "Isac_Knowledge") str
neuper@38025
   582
walther@60343
   583
fun is_atom (Const _) = true
neuper@38025
   584
  | is_atom (Free _) = true
neuper@38025
   585
  | is_atom (Var _) = true
walther@60343
   586
  | is_atom t = is_num t;
walther@60335
   587
fun string_of_atom (t as Const (\<^const_name>\<open>numeral\<close>, _) $ _) = to_string t
walther@60335
   588
  | string_of_atom (t as Const (\<^const_name>\<open>one_class.one\<close>, _)) = to_string t
walther@60335
   589
  | string_of_atom (t as Const (\<^const_name>\<open>zero_class.zero\<close>, _)) = to_string t
walther@60333
   590
  | string_of_atom (Const (str, _)) = str
walther@60333
   591
  | string_of_atom (Free (str, _)) = str
walther@60333
   592
  | string_of_atom (Var ((str, int), _)) = str ^ "_" ^ string_of_int int
walther@60333
   593
  | string_of_atom _ = "DUMMY-string_of_atom";
neuper@38025
   594
wneuper@59394
   595
(* from Pure/term.ML; reports if ALL Free's have found a substitution
wneuper@59394
   596
   (required for evaluating the preconditions of _incomplete_ models) *)
wneuper@59394
   597
fun subst_atomic_all [] t = (false (*TODO may be 'true' for some terms ?*), t)
wneuper@59394
   598
  | subst_atomic_all instl t =
wneuper@59394
   599
    let
wneuper@59394
   600
      fun subst (Abs (a, T, body)) = 
wneuper@59394
   601
          let
wneuper@59394
   602
            val (all, body') = subst body
wneuper@59394
   603
          in (all, Abs(a, T, body')) end
wneuper@59394
   604
        | subst (f$tt) = 
wneuper@59394
   605
	        let
wneuper@59394
   606
	          val (all1, f') = subst f
wneuper@59394
   607
	          val (all2, tt') = subst tt
wneuper@59394
   608
	        in (all1 andalso all2, f' $ tt') end
wneuper@59394
   609
        | subst (t as Free _) = 
wneuper@59394
   610
	        if is_num t then (true, t) (*numerals cannot be subst*)
wneuper@59394
   611
	        else (case assoc (instl, t) of
wneuper@59394
   612
					  SOME t' => (true, t')
wneuper@59394
   613
				  | NONE => (false, t))
wneuper@59394
   614
        | subst t = (true, if_none (assoc(instl,t)) t)
wneuper@59394
   615
    in subst t end;
wneuper@59389
   616
wneuper@59507
   617
fun op contains_one_of (thm, ids) =
wneuper@59507
   618
  Term.exists_Const (fn id => member op= ids id) (Thm.prop_of thm)
wneuper@59507
   619
walther@60333
   620
fun var_for vs (t as Const (str, _)) id =
walther@60333
   621
    if is_num t then vs
walther@60333
   622
    else if id = strip_thy str then t :: vs else vs
wneuper@59532
   623
  | var_for vs (t as Free (str, _)) id = if id = str then t :: vs else vs
wneuper@59532
   624
  | var_for vs (t as Var (idn, _)) id = if id = Term.string_of_vname idn then t :: vs else vs
wneuper@59532
   625
  | var_for vs (Bound _) _ = vs
wneuper@59532
   626
  | var_for vs (Abs (_, _, t)) id = var_for vs t id
wneuper@59532
   627
  | var_for vs (t1 $ t2) id = (var_for vs t1 id) @ (var_for vs t2 id)
wneuper@59532
   628
wneuper@59532
   629
val poly_consts = (* TODO: adopt syntax-const from Isabelle*)
wenzelm@60309
   630
  [\<^const_name>\<open>plus\<close>, \<^const_name>\<open>minus\<close>,
wenzelm@60309
   631
  \<^const_name>\<open>divide\<close>, \<^const_name>\<open>times\<close>,
wenzelm@60405
   632
  \<^const_name>\<open>realpow\<close>];
wneuper@59532
   633
(* treat Free, Const, Var as variables in polynomials *)
wneuper@59532
   634
fun vars_of t =
wneuper@59532
   635
  let
wneuper@59532
   636
    val var_ids = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord
walther@60017
   637
  in (map (var_for [] t) var_ids) |> flat |> distinct op = end
wneuper@59532
   638
wneuper@59577
   639
(* this may decompose an object-language isa-list;
wneuper@59577
   640
   use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
wneuper@59577
   641
fun dest_list' t = if is_list t then isalist2list t  else [t];
wneuper@59577
   642
wenzelm@60309
   643
fun negat (Const (\<^const_name>\<open>Not\<close>, _) $ P, P') = P = P'
walther@59842
   644
  | negat _ = false
walther@59842
   645
fun negates p1 p2 = negat (p1, p2) orelse negat (swap (p1, p2));
walther@59842
   646
wneuper@59580
   647
fun raise_type_conflicts ts =
wneuper@59580
   648
  let
wneuper@59580
   649
    val dups = duplicates (op =) (map (fst o dest_Free) ts)
walther@60266
   650
    val confl = filter (fn Free (str, _) => member op = dups str
walther@60266
   651
                         | _ => false) ts
wneuper@59580
   652
  in
wneuper@59580
   653
    if confl = []
wneuper@59580
   654
    then ()
wneuper@59582
   655
    else raise TYPE ("formalisation inconsistent w.r.t. type inference: ",
wneuper@59580
   656
      map (snd o dest_Free)confl, confl)
wneuper@59580
   657
  end
wneuper@59580
   658
walther@59716
   659
(* expects t as Const *)
walther@59717
   660
fun contains_Const_typeless ts t = (t
walther@59716
   661
  |> strip_comb |> fst
walther@59716
   662
  |> member (fn (t1, t2) => fst (dest_Const t1) = fst (dest_Const t2)) ts
walther@59717
   663
) handle TERM("dest_Const", _) => raise TERM ("contains_Const_typeless", [t])
walther@59716
   664
walther@59916
   665
(* WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02 *)
walther@59916
   666
fun sym_trm trm =
walther@59916
   667
  let
walther@59916
   668
    val (lhs, rhs) = (dest_equals o strip_trueprop o Logic.strip_imp_concl) trm
walther@59916
   669
    val trm' = case strip_imp_prems' trm of
walther@59916
   670
	      NONE => mk_equality (rhs, lhs)
walther@59916
   671
	    | SOME cs => ins_concl cs (mk_equality (rhs, lhs))
walther@59916
   672
  in trm' end
walther@59916
   673
walther@59716
   674
walther@60317
   675
end