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