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