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