src/Tools/isac/IsacKnowledge/Atools.ML
branchisac-update-Isa09-2
changeset 37926 e6fc98fbcb85
parent 37922 30eff896074c
child 37930 f2b8d1b3fcc2
equal deleted inserted replaced
37925:d957275620d4 37926:e6fc98fbcb85
    69 fun eval_occurs_in _ "Atools.occurs'_in"
    69 fun eval_occurs_in _ "Atools.occurs'_in"
    70 	     (p as (Const ("Atools.occurs'_in",_) $ v $ t)) _ =
    70 	     (p as (Const ("Atools.occurs'_in",_) $ v $ t)) _ =
    71     ((*writeln("@@@ eval_occurs_in: v= "^(term2str v));
    71     ((*writeln("@@@ eval_occurs_in: v= "^(term2str v));
    72      writeln("@@@ eval_occurs_in: t= "^(term2str t));*)
    72      writeln("@@@ eval_occurs_in: t= "^(term2str t));*)
    73      if occurs_in v t
    73      if occurs_in v t
    74     then Some ((term2str p) ^ " = True",
    74     then SOME ((term2str p) ^ " = True",
    75 	  Trueprop $ (mk_equality (p, HOLogic.true_const)))
    75 	  Trueprop $ (mk_equality (p, HOLogic.true_const)))
    76     else Some ((term2str p) ^ " = False",
    76     else SOME ((term2str p) ^ " = False",
    77 	  Trueprop $ (mk_equality (p, HOLogic.false_const))))
    77 	  Trueprop $ (mk_equality (p, HOLogic.false_const))))
    78   | eval_occurs_in _ _ _ _ = None;
    78   | eval_occurs_in _ _ _ _ = NONE;
    79 
    79 
    80 (*some of the (bound) variables (eg. in an eqsys) "vs" occur in term "t"*)   
    80 (*some of the (bound) variables (eg. in an eqsys) "vs" occur in term "t"*)   
    81 fun some_occur_in vs t = 
    81 fun some_occur_in vs t = 
    82     let fun occurs_in' a b = occurs_in b a
    82     let fun occurs_in' a b = occurs_in b a
    83     in foldl or_ (false, map (occurs_in' t) vs) end;
    83     in foldl or_ (false, map (occurs_in' t) vs) end;
    86 			eval_some_occur_in "#eval_some_occur_in_"))*)
    86 			eval_some_occur_in "#eval_some_occur_in_"))*)
    87 fun eval_some_occur_in _ "Atools.some'_occur'_in"
    87 fun eval_some_occur_in _ "Atools.some'_occur'_in"
    88 			  (p as (Const ("Atools.some'_occur'_in",_) 
    88 			  (p as (Const ("Atools.some'_occur'_in",_) 
    89 				       $ vs $ t)) _ =
    89 				       $ vs $ t)) _ =
    90     if some_occur_in (isalist2list vs) t
    90     if some_occur_in (isalist2list vs) t
    91     then Some ((term2str p) ^ " = True",
    91     then SOME ((term2str p) ^ " = True",
    92 	       Trueprop $ (mk_equality (p, HOLogic.true_const)))
    92 	       Trueprop $ (mk_equality (p, HOLogic.true_const)))
    93     else Some ((term2str p) ^ " = False",
    93     else SOME ((term2str p) ^ " = False",
    94 	       Trueprop $ (mk_equality (p, HOLogic.false_const)))
    94 	       Trueprop $ (mk_equality (p, HOLogic.false_const)))
    95   | eval_some_occur_in _ _ _ _ = None;
    95   | eval_some_occur_in _ _ _ _ = NONE;
    96 
    96 
    97 
    97 
    98 
    98 
    99 
    99 
   100 (*evaluate 'is_atom'*)
   100 (*evaluate 'is_atom'*)
   101 (*("is_atom",("Atools.is'_atom",eval_is_atom "#is_atom_"))*)
   101 (*("is_atom",("Atools.is'_atom",eval_is_atom "#is_atom_"))*)
   102 fun eval_is_atom (thmid:string) "Atools.is'_atom"
   102 fun eval_is_atom (thmid:string) "Atools.is'_atom"
   103 		 (t as (Const(op0,_) $ arg)) thy = 
   103 		 (t as (Const(op0,_) $ arg)) thy = 
   104     (case arg of 
   104     (case arg of 
   105 	 Free (n,_) => Some (mk_thmid thmid op0 n "", 
   105 	 Free (n,_) => SOME (mk_thmid thmid op0 n "", 
   106 			      Trueprop $ (mk_equality (t, true_as_term)))
   106 			      Trueprop $ (mk_equality (t, true_as_term)))
   107        | _ => Some (mk_thmid thmid op0 "" "", 
   107        | _ => SOME (mk_thmid thmid op0 "" "", 
   108 		    Trueprop $ (mk_equality (t, false_as_term))))
   108 		    Trueprop $ (mk_equality (t, false_as_term))))
   109   | eval_is_atom _ _ _ _ = None;
   109   | eval_is_atom _ _ _ _ = NONE;
   110 
   110 
   111 (*evaluate 'is_even'*)
   111 (*evaluate 'is_even'*)
   112 fun even i = (i div 2) * 2 = i;
   112 fun even i = (i div 2) * 2 = i;
   113 (*("is_even",("Atools.is'_even",eval_is_even "#is_even_"))*)
   113 (*("is_even",("Atools.is'_even",eval_is_even "#is_even_"))*)
   114 fun eval_is_even (thmid:string) "Atools.is'_even"
   114 fun eval_is_even (thmid:string) "Atools.is'_even"
   115 		 (t as (Const(op0,_) $ arg)) thy = 
   115 		 (t as (Const(op0,_) $ arg)) thy = 
   116     (case arg of 
   116     (case arg of 
   117 	Free (n,_) =>
   117 	Free (n,_) =>
   118 	 (case int_of_str n of
   118 	 (case int_of_str n of
   119 	      Some i =>
   119 	      SOME i =>
   120 	      if even i then Some (mk_thmid thmid op0 n "", 
   120 	      if even i then SOME (mk_thmid thmid op0 n "", 
   121 				   Trueprop $ (mk_equality (t, true_as_term)))
   121 				   Trueprop $ (mk_equality (t, true_as_term)))
   122 	      else Some (mk_thmid thmid op0 "" "", 
   122 	      else SOME (mk_thmid thmid op0 "" "", 
   123 			 Trueprop $ (mk_equality (t, false_as_term)))
   123 			 Trueprop $ (mk_equality (t, false_as_term)))
   124 	    | _ => None)
   124 	    | _ => NONE)
   125        | _ => None)
   125        | _ => NONE)
   126   | eval_is_even _ _ _ _ = None; 
   126   | eval_is_even _ _ _ _ = NONE; 
   127 
   127 
   128 (*evaluate 'is_const'*)
   128 (*evaluate 'is_const'*)
   129 (*("is_const",("Atools.is'_const",eval_const "#is_const_"))*)
   129 (*("is_const",("Atools.is'_const",eval_const "#is_const_"))*)
   130 fun eval_const (thmid:string) _(*"Atools.is'_const" WN050820 diff.beh. rooteq*)
   130 fun eval_const (thmid:string) _(*"Atools.is'_const" WN050820 diff.beh. rooteq*)
   131 	       (t as (Const(op0,t0) $ arg)) (thy:theory) = 
   131 	       (t as (Const(op0,t0) $ arg)) (thy:theory) = 
   132     (*eval_const FIXXXXXME.WN.16.5.03 still forgets ComplexI*)
   132     (*eval_const FIXXXXXME.WN.16.5.03 still forgets ComplexI*)
   133     (case arg of 
   133     (case arg of 
   134        Const (n1,_) =>
   134        Const (n1,_) =>
   135 	 Some (mk_thmid thmid op0 n1 "", 
   135 	 SOME (mk_thmid thmid op0 n1 "", 
   136 	       Trueprop $ (mk_equality (t, false_as_term)))
   136 	       Trueprop $ (mk_equality (t, false_as_term)))
   137      | Free (n1,_) =>
   137      | Free (n1,_) =>
   138 	 if is_numeral n1
   138 	 if is_numeral n1
   139 	   then Some (mk_thmid thmid op0 n1 "", 
   139 	   then SOME (mk_thmid thmid op0 n1 "", 
   140 		      Trueprop $ (mk_equality (t, true_as_term)))
   140 		      Trueprop $ (mk_equality (t, true_as_term)))
   141 	 else Some (mk_thmid thmid op0 n1 "", 
   141 	 else SOME (mk_thmid thmid op0 n1 "", 
   142 		    Trueprop $ (mk_equality (t, false_as_term)))
   142 		    Trueprop $ (mk_equality (t, false_as_term)))
   143      | Const ("Float.Float",_) =>
   143      | Const ("Float.Float",_) =>
   144        Some (mk_thmid thmid op0 (term2str arg) "", 
   144        SOME (mk_thmid thmid op0 (term2str arg) "", 
   145 	     Trueprop $ (mk_equality (t, true_as_term)))
   145 	     Trueprop $ (mk_equality (t, true_as_term)))
   146      | _ => (*None*)
   146      | _ => (*NONE*)
   147        Some (mk_thmid thmid op0 (term2str arg) "", 
   147        SOME (mk_thmid thmid op0 (term2str arg) "", 
   148 		    Trueprop $ (mk_equality (t, false_as_term))))
   148 		    Trueprop $ (mk_equality (t, false_as_term))))
   149   | eval_const _ _ _ _ = None; 
   149   | eval_const _ _ _ _ = NONE; 
   150 
   150 
   151 (*. evaluate binary, associative, commutative operators: *,+,^ .*)
   151 (*. evaluate binary, associative, commutative operators: *,+,^ .*)
   152 (*("PLUS"    ,("op +"        ,eval_binop "#add_")),
   152 (*("PLUS"    ,("op +"        ,eval_binop "#add_")),
   153   ("TIMES"   ,("op *"        ,eval_binop "#mult_")),
   153   ("TIMES"   ,("op *"        ,eval_binop "#mult_")),
   154   ("POWER"  ,("Atools.pow"  ,eval_binop "#power_"))*)
   154   ("POWER"  ,("Atools.pow"  ,eval_binop "#power_"))*)
   164     (string_of_int p21)^","^(string_of_int p22)^"))";
   164     (string_of_int p21)^","^(string_of_int p22)^"))";
   165 
   165 
   166 (*.convert int and float to internal floatingpoint prepresentation.*)
   166 (*.convert int and float to internal floatingpoint prepresentation.*)
   167 fun numeral (Free (str, T)) = 
   167 fun numeral (Free (str, T)) = 
   168     (case int_of_str str of
   168     (case int_of_str str of
   169 	 Some i => Some ((i, 0), (0, 0))
   169 	 SOME i => SOME ((i, 0), (0, 0))
   170        | None => None)
   170        | NONE => NONE)
   171   | numeral (Const ("Float.Float", _) $
   171   | numeral (Const ("Float.Float", _) $
   172 		   (Const ("Pair", _) $
   172 		   (Const ("Pair", _) $
   173 			  (Const ("Pair", T) $ Free (v1, _) $ Free (v2,_)) $
   173 			  (Const ("Pair", T) $ Free (v1, _) $ Free (v2,_)) $
   174 			  (Const ("Pair", _) $ Free (p1, _) $ Free (p2,_))))=
   174 			  (Const ("Pair", _) $ Free (p1, _) $ Free (p2,_))))=
   175     (case (int_of_str v1, int_of_str v2, int_of_str p1, int_of_str p2) of
   175     (case (int_of_str v1, int_of_str v2, int_of_str p1, int_of_str p2) of
   176 	(Some v1', Some v2', Some p1', Some p2') =>
   176 	(SOME v1', SOME v2', SOME p1', SOME p2') =>
   177 	Some ((v1', v2'), (p1', p2'))
   177 	SOME ((v1', v2'), (p1', p2'))
   178       | _ => None)
   178       | _ => NONE)
   179   | numeral _ = None;
   179   | numeral _ = NONE;
   180 
   180 
   181 (*.evaluate binary associative operations.*)
   181 (*.evaluate binary associative operations.*)
   182 fun eval_binop (thmid:string) (op_:string) 
   182 fun eval_binop (thmid:string) (op_:string) 
   183 	       (t as ( Const(op0,t0) $ 
   183 	       (t as ( Const(op0,t0) $ 
   184 			    (Const(op0',t0') $ v $ t1) $ t2)) 
   184 			    (Const(op0',t0') $ v $ t1) $ t2)) 
   185 	       thy =                                     (*binary . (v.n1).n2*)
   185 	       thy =                                     (*binary . (v.n1).n2*)
   186     if op0 = op0' then
   186     if op0 = op0' then
   187 	case (numeral t1, numeral t2) of
   187 	case (numeral t1, numeral t2) of
   188 	    (Some n1, Some n2) =>
   188 	    (SOME n1, SOME n2) =>
   189 	    let val (T1,T2,Trange) = dest_binop_typ t0
   189 	    let val (T1,T2,Trange) = dest_binop_typ t0
   190 		val res = calc (if op0 = "op -" then "op +" else op0) n1 n2
   190 		val res = calc (if op0 = "op -" then "op +" else op0) n1 n2
   191 		(*WN071229 "HOL.divide" never tried*)
   191 		(*WN071229 "HOL.divide" never tried*)
   192 		val rhs = var_op_float v op_ t0 T1 res
   192 		val rhs = var_op_float v op_ t0 T1 res
   193 		val prop = Trueprop $ (mk_equality (t, rhs))
   193 		val prop = Trueprop $ (mk_equality (t, rhs))
   194 	    in Some (mk_thmid_f thmid n1 n2, prop) end
   194 	    in SOME (mk_thmid_f thmid n1 n2, prop) end
   195 	  | _ => None
   195 	  | _ => NONE
   196     else None
   196     else NONE
   197   | eval_binop (thmid:string) (op_:string) 
   197   | eval_binop (thmid:string) (op_:string) 
   198 	       (t as 
   198 	       (t as 
   199 		  (Const (op0, t0) $ t1 $ 
   199 		  (Const (op0, t0) $ t1 $ 
   200 			 (Const (op0', t0') $ t2 $ v))) 
   200 			 (Const (op0', t0') $ t2 $ v))) 
   201 	       thy =                                     (*binary . n1.(n2.v)*)
   201 	       thy =                                     (*binary . n1.(n2.v)*)
   202   if op0 = op0' then
   202   if op0 = op0' then
   203 	case (numeral t1, numeral t2) of
   203 	case (numeral t1, numeral t2) of
   204 	    (Some n1, Some n2) =>
   204 	    (SOME n1, SOME n2) =>
   205 	    if op0 = "op -" then None else
   205 	    if op0 = "op -" then NONE else
   206 	    let val (T1,T2,Trange) = dest_binop_typ t0
   206 	    let val (T1,T2,Trange) = dest_binop_typ t0
   207 		val res = calc op0 n1 n2
   207 		val res = calc op0 n1 n2
   208 		val rhs = float_op_var v op_ t0 T1 res
   208 		val rhs = float_op_var v op_ t0 T1 res
   209 		val prop = Trueprop $ (mk_equality (t, rhs))
   209 		val prop = Trueprop $ (mk_equality (t, rhs))
   210 	    in Some (mk_thmid_f thmid n1 n2, prop) end
   210 	    in SOME (mk_thmid_f thmid n1 n2, prop) end
   211 	  | _ => None
   211 	  | _ => NONE
   212   else None
   212   else NONE
   213     
   213     
   214   | eval_binop (thmid:string) (op_:string)
   214   | eval_binop (thmid:string) (op_:string)
   215 	       (t as (Const (op0,t0) $ t1 $ t2)) thy =       (*binary . n1.n2*)
   215 	       (t as (Const (op0,t0) $ t1 $ t2)) thy =       (*binary . n1.n2*)
   216     (case (numeral t1, numeral t2) of
   216     (case (numeral t1, numeral t2) of
   217 	 (Some n1, Some n2) =>
   217 	 (SOME n1, SOME n2) =>
   218 	 let val (T1,T2,Trange) = dest_binop_typ t0;
   218 	 let val (T1,T2,Trange) = dest_binop_typ t0;
   219 	     val res = calc op0 n1 n2;
   219 	     val res = calc op0 n1 n2;
   220 	     val rhs = term_of_float Trange res;
   220 	     val rhs = term_of_float Trange res;
   221 	     val prop = Trueprop $ (mk_equality (t, rhs));
   221 	     val prop = Trueprop $ (mk_equality (t, rhs));
   222 	 in Some (mk_thmid_f thmid n1 n2, prop) end
   222 	 in SOME (mk_thmid_f thmid n1 n2, prop) end
   223        | _ => None)
   223        | _ => NONE)
   224   | eval_binop _ _ _ _ = None; 
   224   | eval_binop _ _ _ _ = NONE; 
   225 (*
   225 (*
   226 > val Some (thmid, t) = eval_binop "#add_" "op +" (str2term "-1 + 2") thy;
   226 > val SOME (thmid, t) = eval_binop "#add_" "op +" (str2term "-1 + 2") thy;
   227 > term2str t;
   227 > term2str t;
   228 val it = "-1 + 2 = 1"
   228 val it = "-1 + 2 = 1"
   229 > val t = str2term "-1 * (-1 * a)";
   229 > val t = str2term "-1 * (-1 * a)";
   230 > val Some (thmid, t) = eval_binop "#mult_" "op *" t thy;
   230 > val SOME (thmid, t) = eval_binop "#mult_" "op *" t thy;
   231 > term2str t;
   231 > term2str t;
   232 val it = "-1 * (-1 * a) = 1 * a"*)
   232 val it = "-1 * (-1 * a) = 1 * a"*)
   233 
   233 
   234 
   234 
   235 
   235 
   237 (*("le"      ,("op <"        ,eval_equ "#less_")),
   237 (*("le"      ,("op <"        ,eval_equ "#less_")),
   238   ("leq"     ,("op <="       ,eval_equ "#less_equal_"))*)
   238   ("leq"     ,("op <="       ,eval_equ "#less_equal_"))*)
   239 fun eval_equ (thmid:string) (op_:string) (t as 
   239 fun eval_equ (thmid:string) (op_:string) (t as 
   240 	       (Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2))) thy = 
   240 	       (Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2))) thy = 
   241     (case (int_of_str n1, int_of_str n2) of
   241     (case (int_of_str n1, int_of_str n2) of
   242 	 (Some n1', Some n2') =>
   242 	 (SOME n1', SOME n2') =>
   243   if calc_equ (strip_thy op0) (n1', n2')
   243   if calc_equ (strip_thy op0) (n1', n2')
   244     then Some (mk_thmid thmid op0 n1 n2, 
   244     then SOME (mk_thmid thmid op0 n1 n2, 
   245 	  Trueprop $ (mk_equality (t, true_as_term)))
   245 	  Trueprop $ (mk_equality (t, true_as_term)))
   246   else Some (mk_thmid thmid op0 n1 n2,  
   246   else SOME (mk_thmid thmid op0 n1 n2,  
   247 	  Trueprop $ (mk_equality (t, false_as_term)))
   247 	  Trueprop $ (mk_equality (t, false_as_term)))
   248        | _ => None)
   248        | _ => NONE)
   249     
   249     
   250   | eval_equ _ _ _ _ = None;
   250   | eval_equ _ _ _ _ = NONE;
   251 
   251 
   252 
   252 
   253 (*evaluate identity
   253 (*evaluate identity
   254 > reflI;
   254 > reflI;
   255 val it = "(?t = ?t) = True"
   255 val it = "(?t = ?t) = True"
   256 > val t = str2term "x = 0";
   256 > val t = str2term "x = 0";
   257 > val None = rewrite_ thy dummy_ord e_rls false reflI t;
   257 > val NONE = rewrite_ thy dummy_ord e_rls false reflI t;
   258 
   258 
   259 > val t = str2term "1 = 0";
   259 > val t = str2term "1 = 0";
   260 > val None = rewrite_ thy dummy_ord e_rls false reflI t;
   260 > val NONE = rewrite_ thy dummy_ord e_rls false reflI t;
   261 ----------- thus needs Calc !
   261 ----------- thus needs Calc !
   262 > val t = str2term "0 = 0";
   262 > val t = str2term "0 = 0";
   263 > val Some (t',_) = rewrite_ thy dummy_ord e_rls false reflI t;
   263 > val SOME (t',_) = rewrite_ thy dummy_ord e_rls false reflI t;
   264 > term2str t';
   264 > term2str t';
   265 val it = "True"
   265 val it = "True"
   266 
   266 
   267 val t = str2term "Not (x = 0)";
   267 val t = str2term "Not (x = 0)";
   268 atomt t; term2str t;
   268 atomt t; term2str t;
   277   the arguments: thus special handling by 'fun eval_binop'*)
   277   the arguments: thus special handling by 'fun eval_binop'*)
   278 (*("ident"   ,("Atools.ident",eval_ident "#ident_")):calc*)
   278 (*("ident"   ,("Atools.ident",eval_ident "#ident_")):calc*)
   279 fun eval_ident (thmid:string) "Atools.ident" (t as 
   279 fun eval_ident (thmid:string) "Atools.ident" (t as 
   280 	       (Const (op0,t0) $ t1 $ t2 )) thy = 
   280 	       (Const (op0,t0) $ t1 $ t2 )) thy = 
   281   if t1 = t2
   281   if t1 = t2
   282     then Some (mk_thmid thmid op0 
   282     then SOME (mk_thmid thmid op0 
   283 	       ("("^(Sign.string_of_term (sign_of thy) t1)^")")
   283 	       ("("^(Sign.string_of_term (sign_of thy) t1)^")")
   284 	       ("("^(Sign.string_of_term (sign_of thy) t2)^")"), 
   284 	       ("("^(Sign.string_of_term (sign_of thy) t2)^")"), 
   285 	  Trueprop $ (mk_equality (t, true_as_term)))
   285 	  Trueprop $ (mk_equality (t, true_as_term)))
   286   else Some (mk_thmid thmid op0  
   286   else SOME (mk_thmid thmid op0  
   287 	       ("("^(Sign.string_of_term (sign_of thy) t1)^")")
   287 	       ("("^(Sign.string_of_term (sign_of thy) t1)^")")
   288 	       ("("^(Sign.string_of_term (sign_of thy) t2)^")"),  
   288 	       ("("^(Sign.string_of_term (sign_of thy) t2)^")"),  
   289 	  Trueprop $ (mk_equality (t, false_as_term)))
   289 	  Trueprop $ (mk_equality (t, false_as_term)))
   290   | eval_ident _ _ _ _ = None;
   290   | eval_ident _ _ _ _ = NONE;
   291 (* TODO
   291 (* TODO
   292 > val t = str2term "x =!= 0";
   292 > val t = str2term "x =!= 0";
   293 > val Some (str, t') = eval_ident "ident_" "b" t thy;
   293 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
   294 > term2str t';
   294 > term2str t';
   295 val str = "ident_(x)_(0)" : string
   295 val str = "ident_(x)_(0)" : string
   296 val it = "(x =!= 0) = False" : string                                
   296 val it = "(x =!= 0) = False" : string                                
   297 > val t = str2term "1 =!= 0";
   297 > val t = str2term "1 =!= 0";
   298 > val Some (str, t') = eval_ident "ident_" "b" t thy;
   298 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
   299 > term2str t';
   299 > term2str t';
   300 val str = "ident_(1)_(0)" : string 
   300 val str = "ident_(1)_(0)" : string 
   301 val it = "(1 =!= 0) = False" : string                                       
   301 val it = "(1 =!= 0) = False" : string                                       
   302 > val t = str2term "0 =!= 0";
   302 > val t = str2term "0 =!= 0";
   303 > val Some (str, t') = eval_ident "ident_" "b" t thy;
   303 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
   304 > term2str t';
   304 > term2str t';
   305 val str = "ident_(0)_(0)" : string
   305 val str = "ident_(0)_(0)" : string
   306 val it = "(0 =!= 0) = True" : string
   306 val it = "(0 =!= 0) = True" : string
   307 *)
   307 *)
   308 
   308 
   311 (*("equal"   ,("op =",eval_equal "#equal_")):calc*)
   311 (*("equal"   ,("op =",eval_equal "#equal_")):calc*)
   312 fun eval_equal (thmid:string) "op =" (t as 
   312 fun eval_equal (thmid:string) "op =" (t as 
   313 	       (Const (op0,t0) $ t1 $ t2 )) thy = 
   313 	       (Const (op0,t0) $ t1 $ t2 )) thy = 
   314   if t1 = t2
   314   if t1 = t2
   315     then ((*writeln"... eval_equal: t1 = t2  --> True";*)
   315     then ((*writeln"... eval_equal: t1 = t2  --> True";*)
   316 	  Some (mk_thmid thmid op0 
   316 	  SOME (mk_thmid thmid op0 
   317 	       ("("^(Sign.string_of_term (sign_of thy) t1)^")")
   317 	       ("("^(Sign.string_of_term (sign_of thy) t1)^")")
   318 	       ("("^(Sign.string_of_term (sign_of thy) t2)^")"), 
   318 	       ("("^(Sign.string_of_term (sign_of thy) t2)^")"), 
   319 	  Trueprop $ (mk_equality (t, true_as_term)))
   319 	  Trueprop $ (mk_equality (t, true_as_term)))
   320 	  )
   320 	  )
   321   else (case (is_atom t1, is_atom t2) of
   321   else (case (is_atom t1, is_atom t2) of
   322 	    (true, true) => 
   322 	    (true, true) => 
   323 	    ((*writeln"... eval_equal: t1<>t2, is_atom t1,t2 --> False";*)
   323 	    ((*writeln"... eval_equal: t1<>t2, is_atom t1,t2 --> False";*)
   324 	     Some (mk_thmid thmid op0  
   324 	     SOME (mk_thmid thmid op0  
   325 			   ("("^(term2str t1)^")") ("("^(term2str t2)^")"),
   325 			   ("("^(term2str t1)^")") ("("^(term2str t2)^")"),
   326 		  Trueprop $ (mk_equality (t, false_as_term)))
   326 		  Trueprop $ (mk_equality (t, false_as_term)))
   327 	     )
   327 	     )
   328 	  | _ => ((*writeln"... eval_equal: t1<>t2, NOT is_atom t1,t2 --> go-on";*)
   328 	  | _ => ((*writeln"... eval_equal: t1<>t2, NOT is_atom t1,t2 --> go-on";*)
   329 		  None))
   329 		  NONE))
   330   | eval_equal _ _ _ _ = (writeln"... eval_equal: error-exit";
   330   | eval_equal _ _ _ _ = (writeln"... eval_equal: error-exit";
   331 			  None);
   331 			  NONE);
   332 (*
   332 (*
   333 val t = str2term "x ~= 0";
   333 val t = str2term "x ~= 0";
   334 val None = eval_equal "equal_" "b" t thy;
   334 val NONE = eval_equal "equal_" "b" t thy;
   335 
   335 
   336 
   336 
   337 > val t = str2term "(x + 1) = (x + 1)";
   337 > val t = str2term "(x + 1) = (x + 1)";
   338 > val Some (str, t') = eval_equal "equal_" "b" t thy;
   338 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
   339 > term2str t';
   339 > term2str t';
   340 val str = "equal_(x + 1)_(x + 1)" : string
   340 val str = "equal_(x + 1)_(x + 1)" : string
   341 val it = "(x + 1 = x + 1) = True" : string
   341 val it = "(x + 1 = x + 1) = True" : string
   342 > val t = str2term "x = 0";
   342 > val t = str2term "x = 0";
   343 > val None = eval_equal "equal_" "b" t thy;
   343 > val NONE = eval_equal "equal_" "b" t thy;
   344 
   344 
   345 > val t = str2term "1 = 0";
   345 > val t = str2term "1 = 0";
   346 > val Some (str, t') = eval_equal "equal_" "b" t thy;
   346 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
   347 > term2str t';
   347 > term2str t';
   348 val str = "equal_(1)_(0)" : string 
   348 val str = "equal_(1)_(0)" : string 
   349 val it = "(1 = 0) = False" : string
   349 val it = "(1 = 0) = False" : string
   350 > val t = str2term "0 = 0";
   350 > val t = str2term "0 = 0";
   351 > val Some (str, t') = eval_equal "equal_" "b" t thy;
   351 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
   352 > term2str t';
   352 > term2str t';
   353 val str = "equal_(0)_(0)" : string
   353 val str = "equal_(0)_(0)" : string
   354 val it = "(0 = 0) = True" : string
   354 val it = "(0 = 0) = True" : string
   355 *)
   355 *)
   356 
   356 
   360 (*. evaluate HOL.divide .*)
   360 (*. evaluate HOL.divide .*)
   361 (*("DIVIDE" ,("HOL.divide"  ,eval_cancel "#divide_"))*)
   361 (*("DIVIDE" ,("HOL.divide"  ,eval_cancel "#divide_"))*)
   362 fun eval_cancel (thmid:string) "HOL.divide" (t as 
   362 fun eval_cancel (thmid:string) "HOL.divide" (t as 
   363 	       (Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2))) thy = 
   363 	       (Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2))) thy = 
   364     (case (int_of_str n1, int_of_str n2) of
   364     (case (int_of_str n1, int_of_str n2) of
   365 	 (Some n1', Some n2') =>
   365 	 (SOME n1', SOME n2') =>
   366   let 
   366   let 
   367     val sg = sign2 n1' n2';
   367     val sg = sign2 n1' n2';
   368     val (T1,T2,Trange) = dest_binop_typ t0;
   368     val (T1,T2,Trange) = dest_binop_typ t0;
   369     val gcd' = gcd (abs n1') (abs n2');
   369     val gcd' = gcd (abs n1') (abs n2');
   370   in if gcd' = abs n2' 
   370   in if gcd' = abs n2' 
   371      then let val rhs = term_of_num Trange (sg * (abs n1') div gcd')
   371      then let val rhs = term_of_num Trange (sg * (abs n1') div gcd')
   372 	      val prop = Trueprop $ (mk_equality (t, rhs))
   372 	      val prop = Trueprop $ (mk_equality (t, rhs))
   373 	  in Some (mk_thmid thmid op0 n1 n2, prop) end     
   373 	  in SOME (mk_thmid thmid op0 n1 n2, prop) end     
   374      else if 0 < n2' andalso gcd' = 1 then None
   374      else if 0 < n2' andalso gcd' = 1 then NONE
   375      else let val rhs = num_op_num T1 T2 (op0,t0) (sg * (abs n1') div gcd')
   375      else let val rhs = num_op_num T1 T2 (op0,t0) (sg * (abs n1') div gcd')
   376 				   ((abs n2') div gcd')
   376 				   ((abs n2') div gcd')
   377 	      val prop = Trueprop $ (mk_equality (t, rhs))
   377 	      val prop = Trueprop $ (mk_equality (t, rhs))
   378 	  in Some (mk_thmid thmid op0 n1 n2, prop) end
   378 	  in SOME (mk_thmid thmid op0 n1 n2, prop) end
   379   end
   379   end
   380        | _ => ((*writeln"@@@ eval_cancel None";*)None))
   380        | _ => ((*writeln"@@@ eval_cancel NONE";*)NONE))
   381 
   381 
   382   | eval_cancel _ _ _ _ = None;
   382   | eval_cancel _ _ _ _ = NONE;
   383 
   383 
   384 (*. get the argument from a function-definition.*)
   384 (*. get the argument from a function-definition.*)
   385 (*("argument_in" ,("Atools.argument'_in",
   385 (*("argument_in" ,("Atools.argument'_in",
   386 		   eval_argument_in "Atools.argument'_in"))*)
   386 		   eval_argument_in "Atools.argument'_in"))*)
   387 fun eval_argument_in _ "Atools.argument'_in" 
   387 fun eval_argument_in _ "Atools.argument'_in" 
   388 		     (t as (Const ("Atools.argument'_in", _) $ (f $ arg))) _ =
   388 		     (t as (Const ("Atools.argument'_in", _) $ (f $ arg))) _ =
   389     if is_Free arg (*could be something to be simplified before*)
   389     if is_Free arg (*could be something to be simplified before*)
   390     then Some (term2str t ^ " = " ^ term2str arg,
   390     then SOME (term2str t ^ " = " ^ term2str arg,
   391 	       Trueprop $ (mk_equality (t, arg)))
   391 	       Trueprop $ (mk_equality (t, arg)))
   392     else None
   392     else NONE
   393   | eval_argument_in _ _ _ _ = None;
   393   | eval_argument_in _ _ _ _ = NONE;
   394 
   394 
   395 (*.check if the function-identifier of the first argument matches 
   395 (*.check if the function-identifier of the first argument matches 
   396    the function-identifier of the lhs of the second argument.*)
   396    the function-identifier of the lhs of the second argument.*)
   397 (*("sameFunId" ,("Atools.sameFunId",
   397 (*("sameFunId" ,("Atools.sameFunId",
   398 		   eval_same_funid "Atools.sameFunId"))*)
   398 		   eval_same_funid "Atools.sameFunId"))*)
   399 fun eval_sameFunId _ "Atools.sameFunId" 
   399 fun eval_sameFunId _ "Atools.sameFunId" 
   400 		     (p as Const ("Atools.sameFunId",_) $ 
   400 		     (p as Const ("Atools.sameFunId",_) $ 
   401 			(f1 $ _) $ 
   401 			(f1 $ _) $ 
   402 			(Const ("op =", _) $ (f2 $ _) $ _)) _ =
   402 			(Const ("op =", _) $ (f2 $ _) $ _)) _ =
   403     if f1 = f2 
   403     if f1 = f2 
   404     then Some ((term2str p) ^ " = True",
   404     then SOME ((term2str p) ^ " = True",
   405 	       Trueprop $ (mk_equality (p, HOLogic.true_const)))
   405 	       Trueprop $ (mk_equality (p, HOLogic.true_const)))
   406     else Some ((term2str p) ^ " = False",
   406     else SOME ((term2str p) ^ " = False",
   407 	       Trueprop $ (mk_equality (p, HOLogic.false_const)))
   407 	       Trueprop $ (mk_equality (p, HOLogic.false_const)))
   408 | eval_sameFunId _ _ _ _ = None;
   408 | eval_sameFunId _ _ _ _ = NONE;
   409 
   409 
   410 
   410 
   411 (*.from a list of fun-definitions "f x = ..." as 2nd argument
   411 (*.from a list of fun-definitions "f x = ..." as 2nd argument
   412    filter the elements with the same fun-identfier in "f y"
   412    filter the elements with the same fun-identfier in "f y"
   413    as the fst argument;
   413    as the fst argument;
   420 fun eval_filter_sameFunId _ "Atools.filter'_sameFunId" 
   420 fun eval_filter_sameFunId _ "Atools.filter'_sameFunId" 
   421 		     (p as Const ("Atools.filter'_sameFunId",_) $ 
   421 		     (p as Const ("Atools.filter'_sameFunId",_) $ 
   422 			(fid $ _) $ fs) _ =
   422 			(fid $ _) $ fs) _ =
   423     let val fs' = ((list2isalist HOLogic.boolT) o 
   423     let val fs' = ((list2isalist HOLogic.boolT) o 
   424 		   (filter (same_funid fid))) (isalist2list fs)
   424 		   (filter (same_funid fid))) (isalist2list fs)
   425     in Some (term2str (mk_equality (p, fs')),
   425     in SOME (term2str (mk_equality (p, fs')),
   426 	       Trueprop $ (mk_equality (p, fs'))) end
   426 	       Trueprop $ (mk_equality (p, fs'))) end
   427 | eval_filter_sameFunId _ _ _ _ = None;
   427 | eval_filter_sameFunId _ _ _ _ = NONE;
   428 
   428 
   429 
   429 
   430 (*make a list of terms to a sum*)
   430 (*make a list of terms to a sum*)
   431 fun list2sum [] = error ("list2sum called with []")
   431 fun list2sum [] = error ("list2sum called with []")
   432   | list2sum [s] = s
   432   | list2sum [s] = s
   445 		      (p as Const ("Atools.boollist2sum", _) $ 
   445 		      (p as Const ("Atools.boollist2sum", _) $ 
   446 			 (l as Const ("List.list.Cons", _) $ _ $ _)) _ =
   446 			 (l as Const ("List.list.Cons", _) $ _ $ _)) _ =
   447     let val isal = isalist2list l
   447     let val isal = isalist2list l
   448 	val lhss = map lhs isal
   448 	val lhss = map lhs isal
   449 	val sum = list2sum lhss
   449 	val sum = list2sum lhss
   450     in Some ((term2str p) ^ " = " ^ (term2str sum),
   450     in SOME ((term2str p) ^ " = " ^ (term2str sum),
   451 	  Trueprop $ (mk_equality (p, sum)))
   451 	  Trueprop $ (mk_equality (p, sum)))
   452     end
   452     end
   453 | eval_boollist2sum _ _ _ _ = None;
   453 | eval_boollist2sum _ _ _ _ = NONE;
   454 
   454 
   455 
   455 
   456 
   456 
   457 local
   457 local
   458 
   458