src/Tools/isac/ProgLang/termC.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 28 Sep 2010 10:10:26 +0200
branchisac-update-Isa09-2
changeset 38034 928cebc9c4aa
parent 38031 460c24a6a6ba
child 38036 02a9b2540eb7
permissions -rw-r--r--
updated "op *" --> Groups.times_class.times in src and test

find . -type f -exec sed -i s/"\"op \*\""/"\"Groups.times_class.times\""/g {} \;
neuper@38025
     1
(* Title: extends Isabelle/src/Pure/term.ML
neuper@38025
     2
   Author: Walther Neuper 1999
neuper@38025
     3
   (c) due to copyright terms
neuper@38025
     4
*)
neuper@38025
     5
neuper@38025
     6
(*
neuper@38025
     7
> (cterm_of thy) a_term; 
neuper@38025
     8
val it = "empty" : cterm        *)
neuper@38025
     9
neuper@38025
    10
(*2003 fun match thy t pat =
neuper@38025
    11
    (snd (Pattern.match (Sign.tsig_of (sign_of thy)) (pat, t)))
neuper@38025
    12
    handle _ => [];
neuper@38025
    13
fn : theory ->
neuper@38025
    14
     Term.term -> Term.term -> (Term.indexname * Term.term) list*)
neuper@38025
    15
(*see src/Tools/eqsubst.ML fun clean_match*)
neuper@38025
    16
(*2003 fun matches thy tm pa = if match thy tm pa = [] then false else true;*)
neuper@38025
    17
fun matches thy tm pa = 
neuper@38025
    18
    (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
neuper@38025
    19
    handle _ => false
neuper@38025
    20
neuper@38025
    21
fun atomtyp t = (*WN10 see raw_pp_typ*)
neuper@38025
    22
  let
neuper@38025
    23
    fun ato n (Type (s,[])) = 
neuper@38025
    24
      ("\n*** "^indent n^"Type ("^s^",[])")
neuper@38025
    25
      | ato n (Type (s,Ts)) =
neuper@38025
    26
      ("\n*** "^indent n^"Type ("^s^",["^ atol (n+1) Ts)
neuper@38025
    27
neuper@38025
    28
      | ato n (TFree (s,sort)) =
neuper@38025
    29
      ("\n*** "^indent n^"TFree ("^s^",["^ strs2str' sort)
neuper@38025
    30
neuper@38025
    31
      | ato n (TVar ((s,i),sort)) =
neuper@38025
    32
      ("\n*** "^indent n^"TVar (("^s^","^ 
neuper@38025
    33
       string_of_int i ^ strs2str' sort)
neuper@38025
    34
    and atol n [] = 
neuper@38025
    35
      ("\n*** "^indent n^"]")
neuper@38025
    36
      | atol n (T::Ts) = (ato n T ^ atol n Ts)
neuper@38031
    37
in writeln (ato 0 t ^ "\n") end;
neuper@38025
    38
(*
neuper@38025
    39
> val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat";
neuper@38025
    40
> atomtyp T;
neuper@38025
    41
*** Type (fun,[
neuper@38025
    42
***   Type (RealDef.real,[])
neuper@38025
    43
***   Type (fun,[
neuper@38025
    44
***     Type (IntDef.int,[])
neuper@38025
    45
***     Type (nat,[])
neuper@38025
    46
***     ]
neuper@38025
    47
***   ]
neuper@38025
    48
*)
neuper@38025
    49
neuper@38025
    50
(*Prog.Tutorial.p.34, Makarius 1005 does the above like this..*)
neuper@38025
    51
local
neuper@38025
    52
   fun pp_pair (x, y) = Pretty.list "(" ")" [x, y]
neuper@38025
    53
   fun pp_list xs = Pretty.list "[" "]" xs
neuper@38025
    54
   fun pp_str s   = Pretty.str s
neuper@38025
    55
   fun pp_qstr s = Pretty.quote (pp_str s)
neuper@38025
    56
   fun pp_int i   = pp_str (string_of_int i)
neuper@38025
    57
   fun pp_sort S = pp_list (map pp_qstr S)
neuper@38025
    58
   fun pp_constr a args = Pretty.block [pp_str a, Pretty.brk 1, args]
neuper@38025
    59
in
neuper@38025
    60
fun raw_pp_typ (TVar ((a, i), S)) =
neuper@38025
    61
       pp_constr "TVar" (pp_pair (pp_pair (pp_qstr a, pp_int i), pp_sort S))
neuper@38025
    62
   | raw_pp_typ (TFree (a, S)) =
neuper@38025
    63
       pp_constr "TFree" (pp_pair (pp_qstr a, pp_sort S))
neuper@38025
    64
   | raw_pp_typ (Type (a, tys)) =
neuper@38025
    65
       pp_constr "Type" (pp_pair (pp_qstr a, pp_list (map raw_pp_typ tys)))
neuper@38025
    66
end
neuper@38025
    67
(* install
neuper@38025
    68
PolyML.addPrettyPrinter
neuper@38025
    69
  (fn _ => fn _ => ml_pretty o Pretty.to_ML o raw_pp_typ);
neuper@38025
    70
de-install
neuper@38025
    71
PolyML.addPrettyPrinter
neuper@38025
    72
  (fn _ => fn _ => ml_pretty o Pretty.to_ML o Proof_Display.pp_typ Pure.thy);
neuper@38025
    73
*)
neuper@38025
    74
neuper@38025
    75
fun atomt t =
neuper@38025
    76
    let fun ato (Const (a, _)) n = 
neuper@38025
    77
	           "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
neuper@38025
    78
	  | ato (Free (a, _)) n =  
neuper@38025
    79
	           "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
neuper@38025
    80
	  | ato (Var ((a, i), _)) n =
neuper@38025
    81
	           "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ 
neuper@38025
    82
                                               string_of_int i ^ "), _)"
neuper@38025
    83
	  | ato (Bound i) n = 
neuper@38025
    84
	           "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
neuper@38025
    85
	  | ato (Abs (a, _, body)) n = 
neuper@38025
    86
	           "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
neuper@38025
    87
	  | ato (f $ t) n = (ato f n ^ ato t (n + 1))
neuper@38031
    88
    in writeln ("\n*** -------------" ^ ato t 0 ^ "\n***") end;
neuper@38025
    89
neuper@38025
    90
fun term_detail2str t =
neuper@38025
    91
    let fun ato (Const (a, T)) n = 
neuper@38025
    92
	    "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ T ^ ")"
neuper@38025
    93
	  | ato (Free (a, T)) n =  
neuper@38025
    94
	    "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ string_of_typ T ^ ")"
neuper@38025
    95
	  | ato (Var ((a, i), T)) n =
neuper@38025
    96
	    "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), "^
neuper@38025
    97
	    string_of_typ T ^ ")"
neuper@38025
    98
	  | ato (Bound i) n = 
neuper@38025
    99
	    "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
neuper@38025
   100
	  | ato (Abs(a, T, body))  n = 
neuper@38025
   101
	    "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ T ^ ",.."
neuper@38025
   102
	    ^ ato body (n + 1)
neuper@38025
   103
	  | ato (f $ t) n = ato f n ^ ato t (n + 1)
neuper@38025
   104
    in "\n*** " ^ ato t 0 ^ "\n***" end;
neuper@38031
   105
fun atomty t = (writeln o term_detail2str) t; (*WN100907 broken*)
neuper@38025
   106
neuper@38025
   107
fun term_str thy (Const(s,_)) = s
neuper@38025
   108
  | term_str thy (Free(s,_)) = s
neuper@38025
   109
  | term_str thy (Var((s,i),_)) = s^(string_of_int i)
neuper@38025
   110
  | term_str thy (Bound i) = "B."^(string_of_int i)
neuper@38025
   111
  | term_str thy (Abs(s,_,_)) = s
neuper@38031
   112
  | term_str thy t = error("term_str not for "^term2str t);
neuper@38025
   113
neuper@38025
   114
(*.contains the fst argument the second argument (a leave! of term).*)
neuper@38025
   115
fun contains_term (Abs(_,_,body)) t = contains_term body t 
neuper@38025
   116
  | contains_term (f $ f') t = 
neuper@38025
   117
    contains_term f t orelse contains_term f' t
neuper@38025
   118
  | contains_term s t = t = s;
neuper@38025
   119
(*.contains the term a VAR(("*",_),_) ?.*)
neuper@38025
   120
fun contains_Var (Abs(_,_,body)) = contains_Var body
neuper@38025
   121
  | contains_Var (f $ f') = contains_Var f orelse contains_Var f'
neuper@38025
   122
  | contains_Var (Var _) = true
neuper@38025
   123
  | contains_Var _ = false;
neuper@38025
   124
(* contains_Var (str2term "?z = 3") (*true*);
neuper@38025
   125
   contains_Var (str2term "z = 3")  (*false*);
neuper@38025
   126
   *)
neuper@38025
   127
neuper@38025
   128
(*fun int_of_str str =
neuper@38025
   129
    let val ss = explode str
neuper@38025
   130
	val str' = case ss of
neuper@38025
   131
	   "("::s => drop_last s | _ => ss
neuper@38025
   132
    in case BasisLibrary.Int.fromString (implode str') of
neuper@38025
   133
	     SOME i => SOME i
neuper@38025
   134
	   | NONE => NONE end;*)
neuper@38025
   135
fun int_of_str str =
neuper@38025
   136
    let val ss = explode str
neuper@38025
   137
	val str' = case ss of
neuper@38025
   138
	   "("::s => drop_last s | _ => ss
neuper@38025
   139
    in (SOME (Thy_Output.integer (implode str'))) handle _ => NONE end;
neuper@38025
   140
(*
neuper@38025
   141
> int_of_str "123";
neuper@38025
   142
val it = SOME 123 : int option
neuper@38025
   143
> int_of_str "(-123)";
neuper@38025
   144
val it = SOME 123 : int option
neuper@38025
   145
> int_of_str "#123";
neuper@38025
   146
val it = NONE : int option
neuper@38025
   147
> int_of_str "-123";
neuper@38025
   148
val it = SOME ~123 : int option
neuper@38025
   149
*)
neuper@38025
   150
fun int_of_str' str = 
neuper@38025
   151
    case int_of_str str of
neuper@38025
   152
	SOME i => i
neuper@38025
   153
      | NONE => raise TERM ("int_of_string: no int-string",[]);
neuper@38025
   154
val str2int = int_of_str';
neuper@38025
   155
    
neuper@38025
   156
fun is_numeral str = case int_of_str str of
neuper@38025
   157
			 SOME _ => true
neuper@38025
   158
		       | NONE => false;
neuper@38025
   159
val is_no = is_numeral;
neuper@38025
   160
fun is_num (Free (s,_)) = if is_numeral s then true else false
neuper@38025
   161
  | is_num _ = false;
neuper@38025
   162
(*>
neuper@38025
   163
> is_num ((term_of o the o (parse thy)) "#1");
neuper@38025
   164
val it = true : bool
neuper@38025
   165
> is_num ((term_of o the o (parse thy)) "#-1");
neuper@38025
   166
val it = true : bool
neuper@38025
   167
> is_num ((term_of o the o (parse thy)) "a123");
neuper@38025
   168
val it = false : bool
neuper@38025
   169
*)
neuper@38025
   170
neuper@38025
   171
(*fun int_of_Free (Free (intstr, _)) =
neuper@38025
   172
    (case BasisLibrary.Int.fromString intstr of
neuper@38025
   173
	     SOME i => i
neuper@38031
   174
	   | NONE => error ("int_of_Free ( "^ intstr ^", _)"))
neuper@38031
   175
  | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");*)
neuper@38025
   176
fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr
neuper@38031
   177
    handle _ => error ("int_of_Free ( "^ intstr ^", _)"))
neuper@38031
   178
  | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");
neuper@38025
   179
neuper@38025
   180
fun vars t =
neuper@38025
   181
  let
neuper@38025
   182
    fun scan vs (Const(s,T)) = vs
neuper@38025
   183
      | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs
neuper@38025
   184
      | scan vs (t as Var((s,i),T)) = t::vs
neuper@38025
   185
      | scan vs (Bound i) = vs 
neuper@38025
   186
      | scan vs (Abs(s,T,t)) = scan vs t
neuper@38025
   187
      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
neuper@38025
   188
  in (distinct o (scan [])) t end;
neuper@38025
   189
neuper@38025
   190
fun is_Free (Free _) = true
neuper@38025
   191
  | is_Free _ = false;
neuper@38025
   192
fun is_fun_id (Const _) = true
neuper@38025
   193
  | is_fun_id (Free _) = true
neuper@38025
   194
  | is_fun_id _ = false;
neuper@38025
   195
fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
neuper@38025
   196
  | is_f_x _ = false;
neuper@38025
   197
(* is_f_x (str2term "q_0/2 * L * x") (*false*);
neuper@38025
   198
   is_f_x (str2term "M_b x") (*true*);
neuper@38025
   199
  *)
neuper@38025
   200
fun vars_str t =
neuper@38025
   201
  let
neuper@38025
   202
    fun scan vs (Const(s,T)) = vs
neuper@38025
   203
      | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
neuper@38025
   204
      | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
neuper@38025
   205
      | scan vs (Bound i) = vs 
neuper@38025
   206
      | scan vs (Abs(s,T,t)) = scan vs t
neuper@38025
   207
      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
neuper@38025
   208
  in (distinct o (scan [])) t end;
neuper@38025
   209
neuper@38025
   210
fun ids2str t =
neuper@38025
   211
  let
neuper@38025
   212
    fun scan vs (Const(s,T)) = if is_no s then vs else s::vs
neuper@38025
   213
      | scan vs (t as Free(s,T)) = if is_no s then vs else s::vs
neuper@38025
   214
      | scan vs (t as Var((s,i),T)) = (s^"_"^(string_of_int i))::vs
neuper@38025
   215
      | scan vs (Bound i) = vs 
neuper@38025
   216
      | scan vs (Abs(s,T,t)) = scan (s::vs) t
neuper@38025
   217
      | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
neuper@38025
   218
  in (distinct o (scan [])) t end;
neuper@38025
   219
fun is_bdv str =
neuper@38025
   220
    case explode str of
neuper@38025
   221
	"b"::"d"::"v"::_ => true
neuper@38025
   222
      | _ => false;
neuper@38025
   223
fun is_bdv_ (Free (s,_)) = is_bdv s
neuper@38025
   224
  | is_bdv_ _ = false;
neuper@38025
   225
neuper@38025
   226
fun free2str (Free (s,_)) = s
neuper@38031
   227
  | free2str t = error ("free2str not for "^ term2str t);
neuper@38025
   228
fun free2int (t as Free (s, _)) = ((str2int s)
neuper@38031
   229
    handle _ => error ("free2int: "^term_detail2str t))
neuper@38031
   230
  | free2int t = error ("free2int: "^term_detail2str t);
neuper@38025
   231
neuper@38025
   232
(*27.8.01: unused*)
neuper@38025
   233
fun var2free (t as Const(s,T)) = t
neuper@38025
   234
  | var2free (t as Free(s,T)) = t
neuper@38025
   235
  | var2free (Var((s,i),T)) = Free(s,T)
neuper@38025
   236
  | var2free (t as Bound i) = t 
neuper@38025
   237
  | var2free (Abs(s,T,t)) = Abs(s,T,var2free t)
neuper@38025
   238
  | var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
neuper@38025
   239
  
neuper@38025
   240
(*27.8.01: doesn't find some subterm ???!???*)
neuper@38025
   241
(*2010 free2var -> Logic.varify, but take care of 'Free ("1",_)'*)
neuper@38025
   242
fun free2var (t as Const (s, T)) = t
neuper@38025
   243
  | free2var (t as Free (s, T)) = if is_no s then t else Var ((s, 0), T)
neuper@38025
   244
  | free2var (t as Var ((s, i), T)) = t
neuper@38025
   245
  | free2var (t as Bound i) = t 
neuper@38025
   246
  | free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
neuper@38025
   247
  | free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
neuper@38025
   248
  
neuper@38025
   249
neuper@38025
   250
fun mk_listT T = Type ("List.list", [T]);
neuper@38025
   251
fun list_const T = 
neuper@38025
   252
  Const("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
neuper@38025
   253
(*28.8.01: TODO: get type from head of list: 1 arg less!!!*)
neuper@38025
   254
fun list2isalist T [] = Const("List.list.Nil",mk_listT T)
neuper@38025
   255
  | list2isalist T (t::ts) = (list_const T) $ t $ (list2isalist T ts);
neuper@38025
   256
(*
neuper@38025
   257
> val tt = (term_of o the o (parse thy)) "R=(R::real)";
neuper@38025
   258
> val TT = type_of tt;
neuper@38025
   259
> val ss = list2isalist TT [tt,tt,tt];
neuper@38025
   260
> (cterm_of thy) ss;
neuper@38025
   261
val it = "[R = R, R = R, R = R]" : cterm  *)
neuper@38025
   262
neuper@38025
   263
fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b)
neuper@38025
   264
  | isapair2pair t = 
neuper@38031
   265
    error ("isapair2pair called with "^term2str t);
neuper@38025
   266
neuper@38025
   267
val listType = Type ("List.list",[Type ("bool",[])]);
neuper@38025
   268
fun isalist2list ls =
neuper@38025
   269
  let
neuper@38025
   270
    fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls
neuper@38025
   271
      | get es (Const("List.list.Nil",_)) = es
neuper@38025
   272
      | get _ t = 
neuper@38031
   273
	error ("isalist2list applied to NON-list '"^term2str t^"'")
neuper@38025
   274
  in (rev o (get [])) ls end;
neuper@38025
   275
(*      
neuper@38025
   276
> val il = str2term "[a=b,c=d,e=f]";
neuper@38025
   277
> val l = isalist2list il;
neuper@38025
   278
> (tracing o terms2str) l;
neuper@38025
   279
["a = b","c = d","e = f"]
neuper@38025
   280
neuper@38025
   281
> val il = str2term "ss___::bool list";
neuper@38025
   282
> val l = isalist2list il;
neuper@38025
   283
[Free ("ss___", "bool List.list")]
neuper@38025
   284
*)
neuper@38025
   285
neuper@38025
   286
neuper@38025
   287
(*review Isabelle2009/src/HOL/Tools/hologic.ML*)
neuper@38025
   288
val prop = Type ("prop",[]);     (* ~/Diss.99/Integers-Isa/tools.sml*)
neuper@38025
   289
val bool = Type ("bool",[]);     (* 2002 Integ.int *)
neuper@38025
   290
val Trueprop = Const("Trueprop",bool-->prop);
neuper@38025
   291
fun mk_prop t = Trueprop $ t;
neuper@38025
   292
val true_as_term = Const("True",bool);
neuper@38025
   293
val false_as_term = Const("False",bool);
neuper@38025
   294
val true_as_cterm = cterm_of (theory "HOL") true_as_term;
neuper@38025
   295
val false_as_cterm = cterm_of (theory "HOL") false_as_term;
neuper@38025
   296
neuper@38025
   297
infixr 5 -->;                    (*2002 /Pure/term.ML *)
neuper@38025
   298
infixr --->;			 (*2002 /Pure/term.ML *)
neuper@38025
   299
fun S --> T = Type("fun",[S,T]); (*2002 /Pure/term.ML *)
neuper@38025
   300
val op ---> = foldr (op -->);    (*2002 /Pure/term.ML *)
neuper@38025
   301
fun list_implies ([], B) = B : term (*2002 /term.ML *)
neuper@38025
   302
  | list_implies (A::AS, B) = Logic.implies $ A $ list_implies(AS,B);
neuper@38025
   303
neuper@38025
   304
neuper@38025
   305
neuper@38025
   306
(** substitution **)
neuper@38025
   307
neuper@38025
   308
fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) =      (* = thm.ML *)
neuper@38025
   309
      match_bvs(s, t, if x="" orelse y="" then al
neuper@38025
   310
                                          else (x,y)::al)
neuper@38025
   311
  | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
neuper@38025
   312
  | match_bvs(_,_,al) = al;
neuper@38025
   313
fun ren_inst(insts,prop,pat,obj) =              (* = thm.ML *)
neuper@38025
   314
  let val ren = match_bvs(pat,obj,[])
neuper@38025
   315
      fun renAbs(Abs(x,T,b)) =
neuper@38025
   316
            Abs(case assoc_string(ren,x) of NONE => x 
neuper@38025
   317
	  | SOME(y) => y, T, renAbs(b))
neuper@38025
   318
        | renAbs(f$t) = renAbs(f) $ renAbs(t)
neuper@38025
   319
        | renAbs(t) = t
neuper@38025
   320
  in subst_vars insts (if null(ren) then prop else renAbs(prop)) end;
neuper@38025
   321
neuper@38025
   322
neuper@38025
   323
neuper@38025
   324
neuper@38025
   325
neuper@38025
   326
neuper@38025
   327
fun dest_equals' (Const("op =",_) $ t $ u)  =  (t,u)(* logic.ML: Const("=="*)
neuper@38025
   328
  | dest_equals' t = raise TERM("dest_equals'", [t]);
neuper@38025
   329
val lhs_ = (fst o dest_equals');
neuper@38025
   330
val rhs_ = (snd o dest_equals');
neuper@38025
   331
neuper@38025
   332
fun is_equality (Const("op =",_) $ t $ u)  =  true  (* logic.ML: Const("=="*)
neuper@38025
   333
  | is_equality _ = false;
neuper@38025
   334
fun mk_equality (t,u) = (Const("op =",[type_of t,type_of u]--->bool) $ t $ u); 
neuper@38025
   335
fun is_expliceq (Const("op =",_) $ (Free _) $ u)  =  true
neuper@38025
   336
  | is_expliceq _ = false;
neuper@38025
   337
fun strip_trueprop (Const("Trueprop",_) $ t) = t
neuper@38025
   338
  | strip_trueprop t = t;
neuper@38025
   339
(*  | strip_trueprop t = raise TERM("strip_trueprop", [t]);
neuper@38025
   340
*)
neuper@38025
   341
neuper@38025
   342
(*.(A1==>...An==>B) goes to (A1==>...An==>).*)
neuper@38025
   343
fun strip_imp_prems' (Const("==>", T) $ A $ t) = 
neuper@38025
   344
    let fun coll_prems As (Const("==>", _) $ A $ t) = 
neuper@38025
   345
	    coll_prems (As $ (Logic.implies $ A)) t
neuper@38025
   346
	  | coll_prems As _ = SOME As
neuper@38025
   347
    in coll_prems (Logic.implies $ A) t end
neuper@38025
   348
  | strip_imp_prems' _ = NONE;  (* logic.ML: term -> term list*)
neuper@38025
   349
(*
neuper@38025
   350
  val thm = real_mult_div_cancel1;
neuper@38025
   351
  val prop = (#prop o rep_thm) thm;
neuper@38025
   352
  atomt prop;
neuper@38025
   353
*** -------------
neuper@38025
   354
*** Const ( ==>)
neuper@38025
   355
*** . Const ( Trueprop)
neuper@38025
   356
*** . . Const ( Not)
neuper@38025
   357
*** . . . Const ( op =)
neuper@38025
   358
*** . . . . Var ((k, 0), )
neuper@38025
   359
*** . . . . Const ( 0)
neuper@38025
   360
*** . Const ( Trueprop)
neuper@38025
   361
*** . . Const ( op =)                                                          *** .............
neuper@38025
   362
  val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
neuper@38025
   363
  atomt t;
neuper@38025
   364
*** -------------
neuper@38025
   365
*** Const ( ==>)
neuper@38025
   366
*** . Const ( Trueprop)
neuper@38025
   367
*** . . Const ( Not)
neuper@38025
   368
*** . . . Const ( op =)
neuper@38025
   369
*** . . . . Var ((k, 0), )
neuper@38025
   370
*** . . . . Const ( 0)
neuper@38025
   371
neuper@38025
   372
  val thm = real_le_anti_sym;
neuper@38025
   373
  val prop = (#prop o rep_thm) thm;
neuper@38025
   374
  atomt prop;
neuper@38025
   375
*** -------------
neuper@38025
   376
*** Const ( ==>)
neuper@38025
   377
*** . Const ( Trueprop)
neuper@38025
   378
*** . . Const ( op <=)
neuper@38025
   379
*** . . . Var ((z, 0), )
neuper@38025
   380
*** . . . Var ((w, 0), )
neuper@38025
   381
*** . Const ( ==>)
neuper@38025
   382
*** . . Const ( Trueprop)
neuper@38025
   383
*** . . . Const ( op <=)
neuper@38025
   384
*** . . . . Var ((w, 0), )
neuper@38025
   385
*** . . . . Var ((z, 0), )
neuper@38025
   386
*** . . Const ( Trueprop)
neuper@38025
   387
*** . . . Const ( op =)
neuper@38025
   388
*** .............
neuper@38025
   389
  val SOME t = strip_imp_prems' ((#prop o rep_thm) thm);
neuper@38025
   390
  atomt t;
neuper@38025
   391
*** -------------
neuper@38025
   392
*** Const ( ==>)
neuper@38025
   393
*** . Const ( Trueprop)
neuper@38025
   394
*** . . Const ( op <=)
neuper@38025
   395
*** . . . Var ((z, 0), )
neuper@38025
   396
*** . . . Var ((w, 0), )
neuper@38025
   397
*** . Const ( ==>)
neuper@38025
   398
*** . . Const ( Trueprop)
neuper@38025
   399
*** . . . Const ( op <=)
neuper@38025
   400
*** . . . . Var ((w, 0), )
neuper@38025
   401
*** . . . . Var ((z, 0), )
neuper@38025
   402
*)
neuper@38025
   403
neuper@38025
   404
(*. (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch.*)
neuper@38025
   405
fun ins_concl (Const("==>", T) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
neuper@38025
   406
  | ins_concl (Const("==>", T) $ A    ) B = Logic.implies $ A $ B
neuper@38025
   407
  | ins_concl t B =  raise TERM("ins_concl", [t, B]);
neuper@38025
   408
(*
neuper@38025
   409
  val thm = real_le_anti_sym;
neuper@38025
   410
  val prop = (#prop o rep_thm) thm;
neuper@38025
   411
  val concl = Logic.strip_imp_concl prop;
neuper@38025
   412
  val SOME prems = strip_imp_prems' prop;
neuper@38025
   413
  val prop' = ins_concl prems concl;
neuper@38025
   414
  prop = prop';
neuper@38025
   415
  atomt prop;
neuper@38025
   416
  atomt prop';
neuper@38025
   417
*)
neuper@38025
   418
neuper@38025
   419
neuper@38025
   420
fun vperm (Var _, Var _) = true  (*2002 Pure/thm.ML *)
neuper@38025
   421
  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
neuper@38025
   422
  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
neuper@38025
   423
  | vperm (t, u) = (t = u);
neuper@38025
   424
neuper@38025
   425
(*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
neuper@38025
   426
fun mem_term (_, []) = false
neuper@38025
   427
  | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts);
neuper@38025
   428
fun subset_term ([], ys) = true
neuper@38025
   429
  | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys);
neuper@38025
   430
fun eq_set_term (xs, ys) =
neuper@38025
   431
    xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
neuper@38025
   432
(*a total, irreflexive ordering on index names*)
neuper@38025
   433
fun xless ((a,i), (b,j): indexname) = i<j  orelse  (i=j andalso a<b);
neuper@38025
   434
(*a partial ordering (not reflexive) for atomic terms*)
neuper@38025
   435
fun atless (Const (a,_), Const (b,_))  =  a<b
neuper@38025
   436
  | atless (Free (a,_), Free (b,_)) =  a<b
neuper@38025
   437
  | atless (Var(v,_), Var(w,_))  =  xless(v,w)
neuper@38025
   438
  | atless (Bound i, Bound j)  =   i<j
neuper@38025
   439
  | atless _  =  false;
neuper@38025
   440
(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
neuper@38025
   441
fun insert_aterm (t,us) =
neuper@38025
   442
  let fun inserta [] = [t]
neuper@38025
   443
        | inserta (us as u::us') =
neuper@38025
   444
              if atless(t,u) then t::us
neuper@38025
   445
              else if t=u then us (*duplicate*)
neuper@38025
   446
              else u :: inserta(us')
neuper@38025
   447
  in  inserta us  end;
neuper@38025
   448
neuper@38025
   449
(*Accumulates the Vars in the term, suppressing duplicates*)
neuper@38025
   450
fun add_term_vars (t, vars: term list) = case t of
neuper@38025
   451
    Var   _ => insert_aterm(t,vars)
neuper@38025
   452
  | Abs (_,_,body) => add_term_vars(body,vars)
neuper@38025
   453
  | f$t =>  add_term_vars (f, add_term_vars(t, vars))
neuper@38025
   454
  | _ => vars;
neuper@38025
   455
fun term_vars t = add_term_vars(t,[]);
neuper@38025
   456
neuper@38025
   457
neuper@38025
   458
fun var_perm (t, u) = (*2002 Pure/thm.ML *)
neuper@38025
   459
  vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
neuper@38025
   460
    
neuper@38025
   461
(*2002 fun decomp_simp, Pure/thm.ML *)
neuper@38025
   462
fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs)
neuper@38025
   463
    andalso not (is_Var lhs);
neuper@38025
   464
neuper@38025
   465
neuper@38025
   466
fun str_of_int n = 
neuper@38025
   467
  if n < 0 then "-"^((string_of_int o abs) n)
neuper@38025
   468
  else string_of_int n;
neuper@38025
   469
(*
neuper@38025
   470
> str_of_int 1;
neuper@38025
   471
val it = "1" : string                                                          > str_of_int ~1;
neuper@38025
   472
val it = "-1" : string
neuper@38025
   473
*)
neuper@38025
   474
neuper@38025
   475
neuper@38025
   476
fun power b 0 = 1
neuper@38025
   477
  | power b n = 
neuper@38025
   478
  if n>0 then b*(power b (n-1))
neuper@38031
   479
  else error ("power "^(str_of_int b)^" "^(str_of_int n));
neuper@38025
   480
(*
neuper@38025
   481
> power 2 3;
neuper@38025
   482
val it = 8 : int
neuper@38025
   483
> power ~2 3;
neuper@38025
   484
val it = ~8 : int
neuper@38025
   485
> power ~3 2;
neuper@38025
   486
val it = 9 : int
neuper@38025
   487
> power 3 ~2;
neuper@38025
   488
*)
neuper@38025
   489
fun gcd 0 b = b
neuper@38025
   490
  | gcd a b = if a < b then gcd (b mod a) a
neuper@38025
   491
	      else gcd (a mod b) b;
neuper@38025
   492
fun sign n = if n < 0 then ~1
neuper@38025
   493
	     else if n = 0 then 0 else 1;
neuper@38025
   494
fun sign2 n1 n2 = (sign n1) * (sign n2);
neuper@38025
   495
neuper@38025
   496
infix dvd;
neuper@38025
   497
fun d dvd n = n mod d = 0;
neuper@38025
   498
neuper@38025
   499
fun divisors n =
neuper@38025
   500
  let fun pdiv ds d n = 
neuper@38025
   501
    if d=n then d::ds
neuper@38025
   502
    else if d dvd n then pdiv (d::ds) d (n div d)
neuper@38025
   503
	 else pdiv ds (d+1) n
neuper@38025
   504
  in pdiv [] 2 n end;
neuper@38025
   505
neuper@38025
   506
divisors 30;
neuper@38025
   507
divisors 32;
neuper@38025
   508
divisors 60;
neuper@38025
   509
divisors 11;
neuper@38025
   510
neuper@38025
   511
fun doubles ds = (* ds is ordered *)
neuper@38025
   512
  let fun dbls ds [] = ds
neuper@38025
   513
	| dbls ds [i] = ds
neuper@38025
   514
	| dbls ds (i::i'::is) = if i=i' then dbls (i::ds) is
neuper@38025
   515
				else dbls ds (i'::is)
neuper@38025
   516
  in dbls [] ds end;
neuper@38025
   517
(*> doubles [2,3,4];
neuper@38025
   518
val it = [] : int list
neuper@38025
   519
> doubles [2,3,3,5,5,7];
neuper@38025
   520
val it = [5,3] : int list*)
neuper@38025
   521
neuper@38025
   522
fun squfact 0 = 0
neuper@38025
   523
  | squfact 1 = 1
neuper@38025
   524
  | squfact n = foldl op* (1, (doubles o divisors) n);
neuper@38025
   525
(*> squfact 30;
neuper@38025
   526
val it = 1 : int
neuper@38025
   527
> squfact 32;
neuper@38025
   528
val it = 4 : int
neuper@38025
   529
> squfact 60;
neuper@38025
   530
val it = 2 : int
neuper@38025
   531
> squfact 11;
neuper@38025
   532
val it = 1 : int*)
neuper@38025
   533
neuper@38025
   534
neuper@38025
   535
fun dest_type (Type(T,[])) = T
neuper@38025
   536
  | dest_type T = 
neuper@38025
   537
    (atomtyp T;
neuper@38031
   538
     error ("... dest_type: not impl. for this type"));
neuper@38025
   539
neuper@38025
   540
fun term_of_num ntyp n = Free (str_of_int n, ntyp);
neuper@38025
   541
neuper@38025
   542
fun pairT T1 T2 = Type ("*", [T1, T2]);
neuper@38025
   543
(*> val t = str2term "(1,2)";
neuper@38025
   544
> type_of t = pairT HOLogic.realT HOLogic.realT;
neuper@38025
   545
val it = true : bool
neuper@38025
   546
*)
neuper@38025
   547
fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
neuper@38025
   548
(*> val t = str2term "(1,2)";
neuper@38025
   549
> val Const ("Pair",pT) $ _ $ _ = t;
neuper@38025
   550
> pT = PairT HOLogic.realT HOLogic.realT;
neuper@38025
   551
val it = true : bool
neuper@38025
   552
*)
neuper@38025
   553
fun pairt t1 t2 =
neuper@38025
   554
    Const ("Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
neuper@38025
   555
(*> val t = str2term "(1,2)";
neuper@38025
   556
> val (t1, t2) = (str2term "1", str2term "2");
neuper@38025
   557
> t = pairt t1 t2;
neuper@38025
   558
val it = true : bool*)
neuper@38025
   559
neuper@38025
   560
neuper@38025
   561
fun num_of_term (t as Free (s,_)) = 
neuper@38025
   562
    (case int_of_str s of
neuper@38025
   563
	 SOME s' => s'
neuper@38031
   564
       | NONE => error ("num_of_term not for "^ term2str t))
neuper@38031
   565
  | num_of_term t = error ("num_of_term not for "^term2str t);
neuper@38025
   566
neuper@38025
   567
fun mk_factroot op_(*=thy.sqrt*) T fact root = 
neuper@38034
   568
  Const ("Groups.times_class.times", [T, T] ---> T) $ (term_of_num T fact) $
neuper@38025
   569
  (Const (op_, T --> T) $ term_of_num T root);
neuper@38025
   570
(*
neuper@38025
   571
val T =  (type_of o term_of o the) (parse thy "#12::real");
neuper@38025
   572
val t = mk_factroot "SqRoot.sqrt" T 2 3;
neuper@38025
   573
(cterm_of thy) t;
neuper@38025
   574
val it = "#2 * sqrt #3 " : cterm
neuper@38025
   575
*)
neuper@38025
   576
fun var_op_num v op_ optype ntyp n =
neuper@38025
   577
  Const (op_, optype) $ v $ 
neuper@38025
   578
   Free (str_of_int  n, ntyp);
neuper@38025
   579
neuper@38025
   580
fun num_op_var v op_ optype ntyp n =
neuper@38025
   581
  Const (op_,optype) $  
neuper@38025
   582
   Free (str_of_int n, ntyp) $ v;
neuper@38025
   583
neuper@38025
   584
fun num_op_num T1 T2 (op_,Top) n1 n2 = 
neuper@38025
   585
  Const (op_,Top) $ 
neuper@38025
   586
  Free (str_of_int n1, T1) $ Free (str_of_int n2, T2);
neuper@38025
   587
(*
neuper@38025
   588
> val t = num_op_num "Int" 3 4;
neuper@38025
   589
> atomty t;
neuper@38025
   590
> string_of_cterm ((cterm_of thy) t);
neuper@38025
   591
*)
neuper@38025
   592
neuper@38025
   593
fun const_in str (Const _) = false
neuper@38025
   594
  | const_in str (Free (s,_)) = if strip_thy s = str then true else false
neuper@38025
   595
  | const_in str (Bound _) = false
neuper@38025
   596
  | const_in str (Var _) = false
neuper@38025
   597
  | const_in str (Abs (_,_,body)) = const_in str body
neuper@38025
   598
  | const_in str (f$u) = const_in str f orelse const_in str u;
neuper@38025
   599
(*
neuper@38025
   600
> val t = (term_of o the o (parse thy)) "6 + 5 * sqrt 4 + 3";
neuper@38025
   601
> const_in "sqrt" t;
neuper@38025
   602
val it = true : bool
neuper@38025
   603
> val t = (term_of o the o (parse thy)) "6 + 5 * 4 + 3";
neuper@38025
   604
> const_in "sqrt" t;
neuper@38025
   605
val it = false : bool
neuper@38025
   606
*)
neuper@38025
   607
neuper@38025
   608
(*used for calculating built in binary operations in Isabelle2002->Float.ML*)
neuper@38025
   609
(*fun calc "Groups.plus_class.plus"  (n1, n2) = n1+n2
neuper@38025
   610
  | calc "Groups.minus_class.minus"  (n1, n2) = n1-n2
neuper@38034
   611
  | calc "Groups.times_class.times"  (n1, n2) = n1*n2
neuper@38025
   612
  | calc "Rings.inverse_class.divide"(n1, n2) = n1 div n2
neuper@38025
   613
  | calc "Atools.pow"(n1, n2) = power n1 n2
neuper@38031
   614
  | calc op_ _ = error ("calc: operator = "^op_^" not defined");-----*)
neuper@38025
   615
fun calc_equ "op <"  (n1, n2) = n1 < n2
neuper@38025
   616
  | calc_equ "op <=" (n1, n2) = n1 <= n2
neuper@38025
   617
  | calc_equ op_ _ = 
neuper@38031
   618
  error ("calc_equ: operator = "^op_^" not defined");
neuper@38025
   619
fun sqrt (n:int) = if n < 0 then 0
neuper@38025
   620
    (*FIXME ~~~*)  else (trunc o Math.sqrt o Real.fromInt) n;
neuper@38025
   621
neuper@38025
   622
fun mk_thmid thmid op_ n1 n2 = 
neuper@38025
   623
  thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
neuper@38025
   624
neuper@38025
   625
fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) =
neuper@38025
   626
  (arg1,arg2,range)
neuper@38031
   627
  | dest_binop_typ _ = error "dest_binop_typ: not binary";
neuper@38025
   628
(* -----
neuper@38025
   629
> val t = (term_of o the o (parse thy)) "#3^#4";
neuper@38025
   630
> val hT = type_of (head_of t);
neuper@38025
   631
> dest_binop_typ hT;
neuper@38025
   632
val it = ("'a","nat","'a") : typ * typ * typ
neuper@38025
   633
 ----- *)
neuper@38025
   634
neuper@38025
   635
neuper@38025
   636
(** transform binary numeralsstrings **)
neuper@38025
   637
(*Makarius 100308, hacked by WN*)
neuper@38025
   638
val numbers_to_string =
neuper@38025
   639
  let
neuper@38025
   640
    fun dest_num t =
neuper@38025
   641
      (case try HOLogic.dest_number t of
neuper@38025
   642
        SOME (T, i) =>
neuper@38025
   643
          (*if T = @{typ int} orelse T = @{typ real} then WN*)
neuper@38025
   644
            SOME (Free (signed_string_of_int i, T))
neuper@38025
   645
          (*else NONE  WN*)
neuper@38025
   646
      | NONE => NONE);
neuper@38025
   647
neuper@38025
   648
    fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
neuper@38025
   649
      | to_str (t as (u1 $ u2)) =
neuper@38025
   650
          (case dest_num t of
neuper@38025
   651
            SOME t' => t'
neuper@38025
   652
          | NONE => to_str u1 $ to_str u2)
neuper@38025
   653
      | to_str t = perhaps dest_num t;
neuper@38025
   654
  in to_str end
neuper@38025
   655
neuper@38025
   656
(*.make uminus uniform: 
neuper@38025
   657
   Const ("uminus", _) $ Free ("2", "RealDef.real") --> Free ("-2", _)
neuper@38025
   658
to be used immediately before evaluation of numerals; 
neuper@38025
   659
see Scripts/calculate.sml .*)
neuper@38025
   660
(*2002 fun(*app_num_tr'2 (Const("0",T)) = Free("0",T)
neuper@38025
   661
  | app_num_tr'2 (Const("1",T)) = Free("1",T)
neuper@38025
   662
  |*)app_num_tr'2 (t as Const("uminus",_) $ Free(s,T)) = 
neuper@38025
   663
    (case int_of_str s of SOME i => 
neuper@38025
   664
			  if i > 0 then Free("-"^s,T) else Free(s,T)
neuper@38025
   665
		       | NONE => t)
neuper@38025
   666
(*| app_num_tr'2 (t as Const(s,T)) = t
neuper@38025
   667
  | app_num_tr'2 (Const("Numeral.number_of",Type ("fun", [_, T])) $ t) = 
neuper@38025
   668
    Free(NumeralSyntax.dest_bin_str t, T)
neuper@38025
   669
  | app_num_tr'2 (t as Free(s,T)) = t
neuper@38025
   670
  | app_num_tr'2 (t as Var(n,T)) = t
neuper@38025
   671
  | app_num_tr'2 (t as Bound i) = t
neuper@38025
   672
*)| app_num_tr'2 (Abs(s,T,body)) = Abs(s,T, app_num_tr'2 body)
neuper@38025
   673
  | app_num_tr'2 (t1 $ t2) = (app_num_tr'2 t1) $ (app_num_tr'2 t2)
neuper@38025
   674
  | app_num_tr'2 t = t;
neuper@38025
   675
*)
neuper@38025
   676
val uminus_to_string =
neuper@38025
   677
    let
neuper@38025
   678
	fun dest_num t =
neuper@38025
   679
	    (case t of
neuper@38025
   680
		 (Const ("Groups.uminus_class.uminus", _) $ Free (s, T)) => 
neuper@38025
   681
		 (case int_of_str s of
neuper@38025
   682
		      SOME i => 
neuper@38025
   683
		      SOME (Free (signed_string_of_int (~1 * i), T))
neuper@38025
   684
		    | NONE => NONE)
neuper@38025
   685
	       | _ => NONE);
neuper@38025
   686
	    
neuper@38025
   687
	fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
neuper@38025
   688
	  | to_str (t as (u1 $ u2)) =
neuper@38025
   689
            (case dest_num t of
neuper@38025
   690
		 SOME t' => t'
neuper@38025
   691
               | NONE => to_str u1 $ to_str u2)
neuper@38025
   692
	  | to_str t = perhaps dest_num t;
neuper@38025
   693
    in to_str end;
neuper@38025
   694
neuper@38025
   695
neuper@38025
   696
(*2002 fun num_str thm =
neuper@38025
   697
  let 
neuper@38025
   698
    val {sign_ref = sign_ref, der = der, maxidx = maxidx,
neuper@38025
   699
	    shyps = shyps, hyps = hyps, (*tpairs = tpairs,*) prop = prop} = 
neuper@38025
   700
	rep_thm_G thm;
neuper@38025
   701
    val prop' = app_num_tr'1 prop;
neuper@38025
   702
  in assbl_thm sign_ref der maxidx shyps hyps (*tpairs*) prop' end;*)
neuper@38025
   703
fun num_str thm =
neuper@38025
   704
  let val (deriv, 
neuper@38025
   705
	   {thy_ref = thy_ref, tags = tags, maxidx = maxidx, shyps = shyps, 
neuper@38025
   706
	    hyps = hyps, tpairs = tpairs, prop = prop}) = rep_thm_G thm
neuper@38025
   707
    val prop' = numbers_to_string prop;
neuper@38025
   708
  in assbl_thm deriv thy_ref tags maxidx shyps hyps tpairs prop' end;
neuper@38025
   709
neuper@38025
   710
fun get_thm' xstring = (*?covers 2009 Thm?!, replaces 2002 fun get_thm :
neuper@38025
   711
val it = fn : theory -> xstring -> Thm.thm*)
neuper@38025
   712
    Thm (xstring, 
neuper@38025
   713
	 num_str (ProofContext.get_thm (thy2ctxt' "Isac") xstring)); 
neuper@38025
   714
neuper@38025
   715
(** get types of Free and Abs for parse' **)
neuper@38025
   716
(*11.1.00: not used, fix-typed +,*,-,^ instead *)
neuper@38025
   717
neuper@38025
   718
val dummyT = Type ("dummy",[]);
neuper@38025
   719
val dummyT = TVar (("DUMMY",0),[]);
neuper@38025
   720
neuper@38025
   721
(* assumes only 1 type for numerals 
neuper@38025
   722
   and different identifiers for Const, Free and Abs *)
neuper@38025
   723
fun get_types t = 
neuper@38025
   724
  let
neuper@38025
   725
    fun get ts  (Const(s,T)) = (s,T)::ts
neuper@38025
   726
      | get ts  (Free(s,T)) = if is_no s 
neuper@38025
   727
				then ("#",T)::ts else (s,T)::ts
neuper@38025
   728
      | get ts  (Var(n,T)) = ts
neuper@38025
   729
      | get ts  (Bound i) = ts
neuper@38025
   730
      | get ts  (Abs(s,T,body)) = get ((s,T)::ts)  body
neuper@38025
   731
      | get ts  (t1 $ t2) = (get ts  t1) @ (get ts  t2)
neuper@38025
   732
  in distinct (get [] t) end;
neuper@38025
   733
(*
neuper@38025
   734
val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
neuper@38025
   735
get_types t;
neuper@38025
   736
*)
neuper@38025
   737
neuper@38025
   738
(*11.1.00: not used, fix-typed +,*,-,^ instead *)
neuper@38025
   739
fun set_types al (Const(s,T)) = 
neuper@38025
   740
    (case assoc (al,s) of
neuper@38025
   741
       SOME T' => Const(s,T')
neuper@38025
   742
     | NONE => (warning ("set_types: no type for "^s); Const(s,dummyT)))
neuper@38025
   743
  | set_types al (Free(s,T)) = 
neuper@38025
   744
  if is_no s then
neuper@38025
   745
    (case assoc (al,"#") of
neuper@38025
   746
      SOME T' => Free(s,T')
neuper@38025
   747
    | NONE => (warning ("set_types: no type for numerals"); Free(s,T)))
neuper@38025
   748
  else (case assoc (al,s) of
neuper@38025
   749
	       SOME T' => Free(s,T')
neuper@38025
   750
	     | NONE => (warning ("set_types: no type for "^s); Free(s,T)))
neuper@38025
   751
  | set_types al (Var(n,T)) = Var(n,T)
neuper@38025
   752
  | set_types al (Bound i) = Bound i
neuper@38025
   753
  | set_types al (Abs(s,T,body)) = 
neuper@38025
   754
		 (case assoc (al,s) of
neuper@38025
   755
		    SOME T'  => Abs(s,T', set_types al body)
neuper@38025
   756
		  | NONE => (warning ("set_types: no type for "^s);
neuper@38025
   757
			     Abs(s,T, set_types al body)))
neuper@38025
   758
  | set_types al (t1 $ t2) = (set_types al t1) $ (set_types al t2);
neuper@38025
   759
(*
neuper@38025
   760
val t = (term_of o the o (parse thy)) "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
neuper@38025
   761
val al = get_types t;
neuper@38025
   762
neuper@38025
   763
val t = (term_of o the o (parse thy)) "x = #0 + #-1 * #-4";
neuper@38025
   764
atomty t;                         (* 'a *)
neuper@38025
   765
val t' = set_types al t;
neuper@38025
   766
atomty t';                        (*real*)
neuper@38025
   767
(cterm_of thy) t';
neuper@38025
   768
val it = "x = #0 + #-1 * #-4" : cterm
neuper@38025
   769
neuper@38025
   770
val t = (term_of o the o (parse thy)) 
neuper@38025
   771
  "#5 * x + x ^^^ #2 = (#2 + x) ^^^ #2";
neuper@38025
   772
atomty t;
neuper@38025
   773
val t' = set_types al t;
neuper@38025
   774
atomty t';
neuper@38025
   775
(cterm_of thy) t';
neuper@38025
   776
uncaught exception TYPE               (*^^^ is new, NOT in al*)
neuper@38025
   777
*)
neuper@38025
   778
      
neuper@38025
   779
neuper@38025
   780
(** from Descript.ML **)
neuper@38025
   781
neuper@38025
   782
(** decompose an isa-list to an ML-list 
neuper@38025
   783
    i.e. [] belong to the meta-language, too **)
neuper@38025
   784
neuper@38025
   785
fun is_list ((Const("List.list.Cons",_)) $ _ $ _) = true
neuper@38025
   786
  | is_list _ = false;
neuper@38025
   787
(* val (SOME ct) = parse thy "lll::real list";
neuper@38025
   788
> val ty = (#t o rep_cterm) ct;
neuper@38025
   789
> is_list ty;
neuper@38025
   790
val it = false : bool
neuper@38025
   791
> val (SOME ct) = parse thy "[lll]";
neuper@38025
   792
> val ty = (#t o rep_cterm) ct;
neuper@38025
   793
> is_list ty;
neuper@38025
   794
val it = true : bool *)
neuper@38025
   795
neuper@38025
   796
neuper@38025
   797
neuper@38025
   798
fun mk_Free (s,T) = Free(s,T);
neuper@38025
   799
fun mk_free T s =  Free(s,T);
neuper@38025
   800
neuper@38025
   801
(*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
neuper@38025
   802
fun subst_bound (arg, t) : term = (*WN100908 neglects 'raise Same.SAME'*)
neuper@38025
   803
  let fun subst (t as Bound i, lev) =
neuper@38025
   804
            if i<lev then  t    (*var is locally bound*)
neuper@38025
   805
            else  if i=lev then incr_boundvars lev arg
neuper@38025
   806
                           else Bound(i-1)  (*loose: change it*)
neuper@38025
   807
        | subst (Abs(a,T,body), lev) = Abs(a, T,  subst(body,lev+1))
neuper@38025
   808
        | subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
neuper@38025
   809
        | subst (t,lev) = t
neuper@38025
   810
  in  subst (t,0)  end;
neuper@38025
   811
neuper@38025
   812
(*instantiate let; necessary for ass_up*)
neuper@38025
   813
fun inst_abs thy (Const sT) = Const sT  (*TODO.WN100907 drop thy*)
neuper@38025
   814
  | inst_abs thy (Free sT) = Free sT
neuper@38025
   815
  | inst_abs thy (Bound n) = Bound n
neuper@38025
   816
  | inst_abs thy (Var iT) = Var iT
neuper@38025
   817
  | inst_abs thy (Const ("Let",T1) $ e $ (Abs (v, T2, b))) = 
neuper@38025
   818
    let val b' = subst_bound (Free (v, T2), b);
neuper@38025
   819
    (*fun variant_abs: term.ML*)
neuper@38025
   820
    in Const ("Let", T1) $ inst_abs thy e $ (Abs (v, T2, inst_abs thy b')) end
neuper@38025
   821
  | inst_abs thy (t1 $ t2) = inst_abs thy t1 $ inst_abs thy t2
neuper@38025
   822
  | inst_abs thy t = t;
neuper@38025
   823
(*val scr =    
neuper@38025
   824
   "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \
neuper@38025
   825
   \ (let h_ = (hd o (filterVar f_)) eqs_;                    \
neuper@38025
   826
   \      e_1 = hd (dropWhile (ident h_) eqs_);       \
neuper@38025
   827
   \      vs_ = dropWhile (ident f_) (Vars h_);                \
neuper@38025
   828
   \      v_1 = hd (dropWhile (ident v_) vs_);                \
neuper@38025
   829
   \      (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\
neuper@38025
   830
   \                          [BOOL e_1, REAL v_1])\
neuper@38025
   831
   \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)";
neuper@38025
   832
> val ttt = (term_of o the o (parse thy)) scr;
neuper@38025
   833
> tracing(term2str ttt);
neuper@38025
   834
> atomt ttt;
neuper@38025
   835
*** -------------
neuper@38025
   836
*** Const ( DiffApp.Make'_fun'_by'_explicit)
neuper@38025
   837
*** . Free ( f_, )
neuper@38025
   838
*** . Free ( v_, )
neuper@38025
   839
*** . Free ( eqs_, )
neuper@38025
   840
*** . Const ( Let)
neuper@38025
   841
*** . . Const ( Fun.op o)
neuper@38025
   842
*** . . . Const ( List.hd)
neuper@38025
   843
*** . . . Const ( DiffApp.filterVar)
neuper@38025
   844
*** . . . . Free ( f_, )
neuper@38025
   845
*** . . . Free ( eqs_, )
neuper@38025
   846
*** . . Abs( h_,..
neuper@38025
   847
*** . . . Const ( Let)
neuper@38025
   848
*** . . . . Const ( List.hd)
neuper@38025
   849
*** . . . . . Const ( List.dropWhile)
neuper@38025
   850
*** . . . . . . Const ( Atools.ident)
neuper@38025
   851
*** . . . . . . . Bound 0                     <---- Free ( h_, )
neuper@38025
   852
*** . . . . . . Free ( eqs_, )
neuper@38025
   853
*** . . . . Abs( e_1,..
neuper@38025
   854
*** . . . . . Const ( Let)
neuper@38025
   855
*** . . . . . . Const ( List.dropWhile)
neuper@38025
   856
*** . . . . . . . Const ( Atools.ident)
neuper@38025
   857
*** . . . . . . . . Free ( f_, )
neuper@38025
   858
*** . . . . . . . Const ( Tools.Vars)
neuper@38025
   859
*** . . . . . . . . Bound 1                       <---- Free ( h_, )
neuper@38025
   860
*** . . . . . . Abs( vs_,..
neuper@38025
   861
*** . . . . . . . Const ( Let)
neuper@38025
   862
*** . . . . . . . . Const ( List.hd)
neuper@38025
   863
*** . . . . . . . . . Const ( List.dropWhile)
neuper@38025
   864
*** . . . . . . . . . . Const ( Atools.ident)
neuper@38025
   865
*** . . . . . . . . . . . Free ( v_, )
neuper@38025
   866
*** . . . . . . . . . . Bound 0                   <---- Free ( vs_, )
neuper@38025
   867
*** . . . . . . . . Abs( v_1,..
neuper@38025
   868
*** . . . . . . . . . Const ( Let)
neuper@38025
   869
*** . . . . . . . . . . Const ( Script.SubProblem)
neuper@38025
   870
*** . . . . . . . . . . . Const ( Pair)
neuper@38025
   871
*** . . . . . . . . . . . . Free ( DiffApp_, )
neuper@38025
   872
*** . . . . . . . . . . . . Const ( Pair)
neuper@38025
   873
*** . . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   874
*** . . . . . . . . . . . . . . Free ( univar, )
neuper@38025
   875
*** . . . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   876
*** . . . . . . . . . . . . . . . Free ( equation, )
neuper@38025
   877
*** . . . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   878
*** . . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   879
*** . . . . . . . . . . . . . . Free ( no_met, )
neuper@38025
   880
*** . . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   881
*** . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   882
*** . . . . . . . . . . . . Const ( Script.BOOL)
neuper@38025
   883
*** . . . . . . . . . . . . . Bound 2                   <----- Free ( e_1, )
neuper@38025
   884
*** . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   885
*** . . . . . . . . . . . . . Const ( Script.real_)
neuper@38025
   886
*** . . . . . . . . . . . . . . Bound 0                 <----- Free ( v_1, )
neuper@38025
   887
*** . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   888
*** . . . . . . . . . . Abs( s_1,..
neuper@38025
   889
*** . . . . . . . . . . . Const ( Script.Substitute)
neuper@38025
   890
*** . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   891
*** . . . . . . . . . . . . . Const ( Pair)
neuper@38025
   892
*** . . . . . . . . . . . . . . Bound 1                 <----- Free ( v_1, )
neuper@38025
   893
*** . . . . . . . . . . . . . . Const ( Fun.op o)
neuper@38025
   894
*** . . . . . . . . . . . . . . . Const ( Tools.rhs)
neuper@38025
   895
*** . . . . . . . . . . . . . . . Const ( List.hd)
neuper@38025
   896
*** . . . . . . . . . . . . . . . Bound 0               <----- Free ( s_1, )
neuper@38025
   897
*** . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   898
*** . . . . . . . . . . . . Bound 4                     <----- Free ( h_, )
neuper@38025
   899
neuper@38025
   900
> val ttt' = inst_abs thy ttt;
neuper@38025
   901
> tracing(term2str ttt');
neuper@38025
   902
Script Make_fun_by_explicit f_ v_ eqs_ =  
neuper@38025
   903
  ... as above ...
neuper@38025
   904
> atomt ttt';
neuper@38025
   905
*** -------------
neuper@38025
   906
*** Const ( DiffApp.Make'_fun'_by'_explicit)
neuper@38025
   907
*** . Free ( f_, )
neuper@38025
   908
*** . Free ( v_, )
neuper@38025
   909
*** . Free ( eqs_, )
neuper@38025
   910
*** . Const ( Let)
neuper@38025
   911
*** . . Const ( Fun.op o)
neuper@38025
   912
*** . . . Const ( List.hd)
neuper@38025
   913
*** . . . Const ( DiffApp.filterVar)
neuper@38025
   914
*** . . . . Free ( f_, )
neuper@38025
   915
*** . . . Free ( eqs_, )
neuper@38025
   916
*** . . Abs( h_,..
neuper@38025
   917
*** . . . Const ( Let)
neuper@38025
   918
*** . . . . Const ( List.hd)
neuper@38025
   919
*** . . . . . Const ( List.dropWhile)
neuper@38025
   920
*** . . . . . . Const ( Atools.ident)
neuper@38025
   921
*** . . . . . . . Free ( h_, )                <---- Bound 0
neuper@38025
   922
*** . . . . . . Free ( eqs_, )
neuper@38025
   923
*** . . . . Abs( e_1,..
neuper@38025
   924
*** . . . . . Const ( Let)
neuper@38025
   925
*** . . . . . . Const ( List.dropWhile)
neuper@38025
   926
*** . . . . . . . Const ( Atools.ident)
neuper@38025
   927
*** . . . . . . . . Free ( f_, )
neuper@38025
   928
*** . . . . . . . Const ( Tools.Vars)
neuper@38025
   929
*** . . . . . . . . Free ( h_, )                  <---- Bound 1
neuper@38025
   930
*** . . . . . . Abs( vs_,..
neuper@38025
   931
*** . . . . . . . Const ( Let)
neuper@38025
   932
*** . . . . . . . . Const ( List.hd)
neuper@38025
   933
*** . . . . . . . . . Const ( List.dropWhile)
neuper@38025
   934
*** . . . . . . . . . . Const ( Atools.ident)
neuper@38025
   935
*** . . . . . . . . . . . Free ( v_, )
neuper@38025
   936
*** . . . . . . . . . . Free ( vs_, )             <---- Bound 0
neuper@38025
   937
*** . . . . . . . . Abs( v_1,..
neuper@38025
   938
*** . . . . . . . . . Const ( Let)
neuper@38025
   939
*** . . . . . . . . . . Const ( Script.SubProblem)
neuper@38025
   940
*** . . . . . . . . . . . Const ( Pair)
neuper@38025
   941
*** . . . . . . . . . . . . Free ( DiffApp_, )
neuper@38025
   942
*** . . . . . . . . . . . . Const ( Pair)
neuper@38025
   943
*** . . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   944
*** . . . . . . . . . . . . . . Free ( univar, )
neuper@38025
   945
*** . . . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   946
*** . . . . . . . . . . . . . . . Free ( equation, )
neuper@38025
   947
*** . . . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   948
*** . . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   949
*** . . . . . . . . . . . . . . Free ( no_met, )
neuper@38025
   950
*** . . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   951
*** . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   952
*** . . . . . . . . . . . . Const ( Script.BOOL)
neuper@38025
   953
*** . . . . . . . . . . . . . Free ( e_1, )             <----- Bound 2
neuper@38025
   954
*** . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   955
*** . . . . . . . . . . . . . Const ( Script.real_)
neuper@38025
   956
*** . . . . . . . . . . . . . . Free ( v_1, )           <----- Bound 0
neuper@38025
   957
*** . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   958
*** . . . . . . . . . . Abs( s_1,..
neuper@38025
   959
*** . . . . . . . . . . . Const ( Script.Substitute)
neuper@38025
   960
*** . . . . . . . . . . . . Const ( List.list.Cons)
neuper@38025
   961
*** . . . . . . . . . . . . . Const ( Pair)
neuper@38025
   962
*** . . . . . . . . . . . . . . Free ( v_1, )           <----- Bound 1
neuper@38025
   963
*** . . . . . . . . . . . . . . Const ( Fun.op o)
neuper@38025
   964
*** . . . . . . . . . . . . . . . Const ( Tools.rhs)
neuper@38025
   965
*** . . . . . . . . . . . . . . . Const ( List.hd)
neuper@38025
   966
*** . . . . . . . . . . . . . . . Free ( s_1, )         <----- Bound 0
neuper@38025
   967
*** . . . . . . . . . . . . . Const ( List.list.Nil)
neuper@38025
   968
*** . . . . . . . . . . . . Free ( h_, )                <----- Bound 4
neuper@38025
   969
neuper@38025
   970
Note numbering of de Bruijn indexes !
neuper@38025
   971
neuper@38025
   972
Script Make_fun_by_explicit f_ v_ eqs_ =
neuper@38025
   973
 let h_ = (hd o filterVar f_) eqs_; 
neuper@38025
   974
     e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_);
neuper@38025
   975
     vs_ = dropWhile (ident f_) (Vars h_ BOUND_1);
neuper@38025
   976
     v_1 = hd (dropWhile (ident v_) vs_ BOUND_0);
neuper@38025
   977
     s_1 =
neuper@38025
   978
       SubProblem (DiffApp_, [univar, equation], [no_met])
neuper@38025
   979
        [BOOL e_1 BOUND_2, REAL v_1 BOUND_0]
neuper@38025
   980
 in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4
neuper@38025
   981
*)
neuper@38025
   982
neuper@38025
   983
neuper@38025
   984
fun T_a2real (Type (s, [])) = 
neuper@38025
   985
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
neuper@38025
   986
  | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
neuper@38025
   987
  | T_a2real (TFree (s, srt)) = 
neuper@38025
   988
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
neuper@38025
   989
  | T_a2real (TVar (("DUMMY",_),srt)) = HOLogic.realT;
neuper@38025
   990
neuper@38025
   991
(*FIXME .. fixes the type (+see Typefix.thy*)
neuper@38025
   992
fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) 
neuper@38025
   993
  | typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
neuper@38025
   994
  | typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
neuper@38025
   995
  | typ_a2real (Bound i) = (Bound i)
neuper@38025
   996
  | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
neuper@38025
   997
  | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
neuper@38025
   998
(*
neuper@38025
   999
----------------6.8.02---------------------------------------------------
neuper@38025
  1000
 val str = "1";
neuper@38025
  1001
 val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[])));
neuper@38025
  1002
 atomty (term_of t);
neuper@38025
  1003
*** -------------
neuper@38025
  1004
*** Const ( 1, 'a)
neuper@38025
  1005
 val t = (app_num_tr' o term_of) t;
neuper@38025
  1006
 atomty t;
neuper@38025
  1007
*** ------------- 
neuper@38025
  1008
*** Const ( 1, 'a)                                                              
neuper@38025
  1009
 val t = typ_a2real t;
neuper@38025
  1010
 atomty t;
neuper@38025
  1011
*** -------------   
neuper@38025
  1012
*** Const ( 1, real)                                                            
neuper@38025
  1013
neuper@38025
  1014
 val str = "2";
neuper@38025
  1015
 val t = read_cterm (sign_of thy) (str,(TVar(("DUMMY",0),[])));
neuper@38025
  1016
 atomty (term_of t);
neuper@38025
  1017
*** -------------
neuper@38025
  1018
*** Const ( Numeral.number_of, bin => 'a)
neuper@38025
  1019
*** . Const ( Numeral.bin.Bit, [bin, bool] => bin)
neuper@38025
  1020
*** . . Const ( Numeral.bin.Bit, [bin, bool] => bin)
neuper@38025
  1021
*** . . . Const ( Numeral.bin.Pls, bin)
neuper@38025
  1022
*** . . . Const ( True, bool)
neuper@38025
  1023
*** . . Const ( False, bool)
neuper@38025
  1024
 val t = (app_num_tr' o term_of) t;
neuper@38025
  1025
 atomty t;
neuper@38025
  1026
*** -------------
neuper@38025
  1027
*** Free ( 2, 'a)
neuper@38025
  1028
 val t = typ_a2real t;
neuper@38025
  1029
 atomty t;
neuper@38025
  1030
*** -------------
neuper@38025
  1031
*** Free ( 2, real)
neuper@38025
  1032
----------------6.8.02---------------------------------------------------
neuper@38025
  1033
neuper@38025
  1034
neuper@38025
  1035
> val str = "R";
neuper@38025
  1036
> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
neuper@38025
  1037
val t = Free ("R","?DUMMY") : term
neuper@38025
  1038
> val t' = typ_a2real t;
neuper@38025
  1039
> (cterm_of thy) t';
neuper@38025
  1040
val it = "R::RealDef.real" : cterm
neuper@38025
  1041
neuper@38025
  1042
> val str = "R=R";
neuper@38025
  1043
> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
neuper@38025
  1044
> atomty (typ_a2real t);
neuper@38025
  1045
*** -------------
neuper@38025
  1046
*** Const ( op =, [RealDef.real, RealDef.real] => bool)
neuper@38025
  1047
***   Free ( R, RealDef.real)
neuper@38025
  1048
***   Free ( R, RealDef.real)
neuper@38025
  1049
> val t' = typ_a2real t;
neuper@38025
  1050
> (cterm_of thy) t';
neuper@38025
  1051
val it = "(R::RealDef.real) = R" : cterm
neuper@38025
  1052
neuper@38025
  1053
> val str = "fixed_values [R=R]";
neuper@38025
  1054
> val t = term_of (read_cterm(sign_of thy)(str,(TVar(("DUMMY",0),[]))));
neuper@38025
  1055
> val t' = typ_a2real t;
neuper@38025
  1056
> (cterm_of thy) t';
neuper@38025
  1057
val it = "fixed_values [(R::RealDef.real) = R]" : cterm
neuper@38025
  1058
*)
neuper@38025
  1059
neuper@38025
  1060
(*TODO.WN0609: parse should return a term or a string 
neuper@38025
  1061
	     (or even more comprehensive datastructure for error-messages)
neuper@38025
  1062
 i.e. in wrapping with SOME term or NONE the latter is not sufficient*)
neuper@38025
  1063
(*2002 fun parseold thy str = 
neuper@38025
  1064
  (let 
neuper@38025
  1065
     val sgn = sign_of thy;
neuper@38025
  1066
     val t = ((*typ_a2real o*) app_num_tr'1 o term_of) 
neuper@38025
  1067
       (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
neuper@38025
  1068
   in SOME (cterm_of sgn t) end)
neuper@38025
  1069
     handle _ => NONE;*)
neuper@38025
  1070
neuper@38025
  1071
neuper@38025
  1072
neuper@38025
  1073
fun parseold thy str = 
neuper@38025
  1074
  (let val t = ((*typ_a2real o*) numbers_to_string) 
neuper@38025
  1075
		   (Syntax.read_term_global thy str)
neuper@38025
  1076
   in SOME (cterm_of thy t) end)
neuper@38025
  1077
    handle _ => NONE;
neuper@38025
  1078
(*2002 fun parseN thy str = 
neuper@38025
  1079
  (let 
neuper@38025
  1080
     val sgn = sign_of thy;
neuper@38025
  1081
     val t = ((*typ_a2real o app_num_tr'1 o*) term_of) 
neuper@38025
  1082
       (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
neuper@38025
  1083
   in SOME (cterm_of sgn t) end)
neuper@38025
  1084
     handle _ => NONE;*)
neuper@38025
  1085
fun parseN thy str = 
neuper@38025
  1086
  (let val t = (*(typ_a2real o numbers_to_string)*) 
neuper@38025
  1087
	   (Syntax.read_term_global thy str)
neuper@38025
  1088
   in SOME (cterm_of thy t) end)
neuper@38025
  1089
    handle _ => NONE;
neuper@38025
  1090
(*2002 fun parse thy str = 
neuper@38025
  1091
  (let 
neuper@38025
  1092
     val sgn = sign_of thy;
neuper@38025
  1093
     val t = (typ_a2real o app_num_tr'1 o term_of) 
neuper@38025
  1094
       (read_cterm sgn (str,(TVar(("DUMMY",0),[]))));
neuper@38025
  1095
   in SOME (cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*)
neuper@38025
  1096
     handle _ => NONE;*)
neuper@38025
  1097
(*2010 fun parse thy str = 
neuper@38025
  1098
  (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str)
neuper@38025
  1099
   in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
neuper@38025
  1100
     handle _ => NONE;*)
neuper@38025
  1101
fun parse thy str = 
neuper@38025
  1102
  (let val t = (typ_a2real o numbers_to_string) 
neuper@38025
  1103
		   (Syntax.read_term_global thy str)
neuper@38025
  1104
   in SOME (cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*)
neuper@38025
  1105
     handle _ => NONE;
neuper@38025
  1106
(*
neuper@38025
  1107
> val (SOME ct) = parse thy "(-#5)^^^#3"; 
neuper@38025
  1108
> atomty (term_of ct);
neuper@38025
  1109
*** -------------
neuper@38025
  1110
*** Const ( Nat.op ^, ['a, nat] => 'a)
neuper@38025
  1111
***   Const ( uminus, 'a => 'a)
neuper@38025
  1112
***     Free ( #5, 'a)
neuper@38025
  1113
***   Free ( #3, nat)                
neuper@38025
  1114
> val (SOME ct) = parse thy "R=R"; 
neuper@38025
  1115
> atomty (term_of ct);
neuper@38025
  1116
*** -------------
neuper@38025
  1117
*** Const ( op =, [real, real] => bool)
neuper@38025
  1118
***   Free ( R, real)
neuper@38025
  1119
***   Free ( R, real)
neuper@38025
  1120
neuper@38025
  1121
THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!!
neuper@38025
  1122
*** -------------
neuper@38025
  1123
*** Const ( op =, [RealDef.real, RealDef.real] => bool)
neuper@38025
  1124
***   Free ( R, RealDef.real)
neuper@38025
  1125
***   Free ( R, RealDef.real)                  *)
neuper@38025
  1126
neuper@38025
  1127
fun parse_patt thy str = 
neuper@38025
  1128
    ProofContext.read_term_pattern (thy2ctxt thy) str;
neuper@38025
  1129
neuper@38025
  1130
(*version for testing local to theories*)
neuper@38025
  1131
fun str2term_ thy str = (term_of o the o (parse thy)) str;
neuper@38025
  1132
fun str2term str = (term_of o the o (parse (theory "Isac"))) str;
neuper@38025
  1133
fun strs2terms ss = map str2term ss;
neuper@38025
  1134
fun str2termN str = (term_of o the o (parseN (theory "Isac"))) str;
neuper@38025
  1135
neuper@38025
  1136
(*+ makes a substitution from the output of Pattern.match +*)
neuper@38025
  1137
(*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*)
neuper@38025
  1138
fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) =
neuper@38025
  1139
let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in
neuper@38025
  1140
map mk_sub subs end;
neuper@38025
  1141
neuper@38025
  1142
val atomthm = atomt o #prop o rep_thm;
neuper@38025
  1143
neuper@38025
  1144
(*.instantiate #prop thm with bound variables (as Free).*)
neuper@38025
  1145
fun inst_bdv [] t = t : term
neuper@38025
  1146
  | inst_bdv (instl: (term*term) list) t =
neuper@38025
  1147
      let fun subst (v as Var((s,_),T)) = 
neuper@38025
  1148
	      (case explode s of
neuper@38025
  1149
		   "b"::"d"::"v"::_ => 
neuper@38025
  1150
		   if_none (assoc(instl,Free(s,T))) (Free(s,T))
neuper@38025
  1151
		 | _ => v)
neuper@38025
  1152
            | subst (Abs(a,T,body)) = Abs(a, T, subst body)
neuper@38025
  1153
            | subst (f$t') = subst f $ subst t'
neuper@38025
  1154
            | subst t = if_none (assoc(instl,t)) t
neuper@38025
  1155
      in  subst t  end;
neuper@38025
  1156
neuper@38025
  1157
neuper@38025
  1158
(*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!!
neuper@38025
  1159
  use length (vars term) = 1 instead*)
neuper@38025
  1160
fun is_atom (Const ("Float.Float",_) $ _) = true
neuper@38025
  1161
  | is_atom (Const ("ComplexI.I'_'_",_)) = true
neuper@38034
  1162
  | is_atom (Const ("Groups.times_class.times",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t
neuper@38025
  1163
  | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1
neuper@38025
  1164
  | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ 
neuper@38034
  1165
		   (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_))) = 
neuper@38025
  1166
    is_atom t1 andalso is_atom t2
neuper@38025
  1167
  | is_atom (Const _) = true
neuper@38025
  1168
  | is_atom (Free _) = true
neuper@38025
  1169
  | is_atom (Var _) = true
neuper@38025
  1170
  | is_atom _ = false;
neuper@38025
  1171
(* val t = str2term "q_0/2 * L * x";
neuper@38025
  1172
neuper@38025
  1173
neuper@38025
  1174
*)
neuper@38025
  1175
(*val t = str2term "Float ((1,2),(0,0))";
neuper@38025
  1176
> is_atom t;
neuper@38025
  1177
val it = true : bool
neuper@38025
  1178
> val t = str2term "Float ((1,2),(0,0)) * I__";
neuper@38025
  1179
> is_atom t;
neuper@38025
  1180
val it = true : bool
neuper@38025
  1181
> val t = str2term "Float ((1,2),(0,0)) + Float ((3,4),(0,0)) * I__";
neuper@38025
  1182
> is_atom t;
neuper@38025
  1183
val it = true : bool
neuper@38025
  1184
> val t = str2term "1 + 2*I__";
neuper@38034
  1185
> val Const ("Groups.plus_class.plus",_) $ t1 $ (Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_)) = t;
neuper@38025
  1186
*)
neuper@38025
  1187
neuper@38025
  1188
(*.adaption from Isabelle/src/Pure/term.ML; reports if ALL Free's
neuper@38025
  1189
   have found a substitution (required for evaluating the preconditions
neuper@38025
  1190
   of _incomplete_ models).*)
neuper@38025
  1191
fun subst_atomic_all [] t = (false, (*TODO may be 'true' for some terms ?*)
neuper@38025
  1192
			     t : term)
neuper@38025
  1193
  | subst_atomic_all (instl: (term*term) list) t =
neuper@38025
  1194
      let fun subst (Abs(a,T,body)) = 
neuper@38025
  1195
	      let val (all, body') = subst body
neuper@38025
  1196
	      in (all, Abs(a, T, body')) end
neuper@38025
  1197
            | subst (f$tt) = 
neuper@38025
  1198
	      let val (all1, f') = subst f
neuper@38025
  1199
		  val (all2, tt') = subst tt
neuper@38025
  1200
	      in (all1 andalso all2, f' $ tt') end
neuper@38025
  1201
            | subst (t as Free _) = 
neuper@38025
  1202
	      if is_num t then (true, t) (*numerals cannot be subst*)
neuper@38025
  1203
	      else (case assoc(instl,t) of
neuper@38025
  1204
					 SOME t' => (true, t')
neuper@38025
  1205
				       | NONE => (false, t))
neuper@38025
  1206
            | subst t = (true, if_none (assoc(instl,t)) t)
neuper@38025
  1207
      in  subst t  end;
neuper@38025
  1208
neuper@38025
  1209
(*.add two terms with a type given.*)
neuper@38025
  1210
fun mk_add t1 t2 =
neuper@38025
  1211
    let val T1 = type_of t1
neuper@38025
  1212
	val T2 = type_of t2
neuper@38025
  1213
    in if T1 <> T2 then raise TYPE ("mk_add gets ",[T1, T2],[t1,t2])
neuper@38025
  1214
       else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2)
neuper@38025
  1215
    end;
neuper@38025
  1216