src/Tools/isac/ProgLang/Prog_Expr.thy
author Walther Neuper <walther.neuper@jku.at>
Fri, 13 Sep 2019 18:35:51 +0200
changeset 59618 80efccb7e5c1
parent 59603 30cd47104ad7
child 59619 a2f67c777ccd
permissions -rw-r--r--
lucin: cleanup thys in ProgLang, in particular Aut_Prog.thy
     1 (* title:  ProgLang/Prog_Expr.thy
     2            functions for expressions (which do NOT contain Prog_Tac and Tactical, by definition))
     3    author: Walther Neuper, Aug.2019
     4    (c) copyright due to lincense terms.
     5 *)
     6 
     7 theory Prog_Expr imports Calculate
     8 begin
     9 
    10 subsection \<open>General constants\<close>
    11 consts
    12 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
    13   EmptyList :: "bool list" 
    14   UniversalList :: "bool list" 
    15 (*\\------------------------- from Tools .thy ------------------------------------------------//*)
    16 (*\\------------------------- from Atools .thy -----------------------------------------------//*)
    17   Arbfix :: "real"
    18   Undef :: "real"  (* WN190823 probably an outdated design for DiffApp *)
    19 
    20   pow              :: "[real, real] => real"    (infixr "^^^" 80)
    21 (*\\------------------------- from Atools.thy -----------------------------------------------//*)
    22 
    23 subsection \<open>consts of functions in pre-conditions and program-expressions\<close>
    24 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
    25 consts
    26   lhs             :: "bool => real"           (*of an equality*)
    27   rhs             :: "bool => real"           (*of an equality*)
    28   Vars            :: "'a => real list"        (*get the variables of a term *)
    29   matches         :: "['a, 'a] => bool"
    30   matchsub        :: "['a, 'a] => bool"
    31 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
    32   some'_occur'_in  :: "[real list, 'a] => bool" ("some'_of _ occur'_in _")
    33   occurs'_in       :: "[real     , 'a] => bool" ("_ occurs'_in _")
    34 
    35   abs              :: "real => real"            ("(|| _ ||)")
    36 (* ~~~ FIXXXME Isabelle2002 has abs already !!!*)
    37   absset           :: "real set => real"        ("(||| _ |||)")
    38   (*is numeral constant ?*)
    39   is'_const        :: "real => bool"            ("_ is'_const" 10)
    40   (*is_const rename to is_num FIXXXME.WN.16.5.03 *)
    41   is'_atom         :: "real => bool"            ("_ is'_atom" 10)
    42   is'_even         :: "real => bool"            ("_ is'_even" 10)
    43 		
    44   (* identity on term level*)
    45   ident            :: "['a, 'a] => bool"        ("(_ =!=/ _)" [51, 51] 50)
    46 
    47   argument'_in     :: "real => real"            ("argument'_in _" 10)
    48   sameFunId        :: "[real, bool] => bool"    (**"same'_funid _ _" 10
    49 	WN0609 changed the id, because ".. _ _" inhibits currying**)
    50   filter'_sameFunId:: "[real, bool list] => bool list" 
    51 					        ("filter'_sameFunId _ _" 10)
    52   boollist2sum     :: "bool list => real"
    53   lastI            :: "'a list \<Rightarrow> 'a"
    54 
    55 subsection \<open>Theorems for rule-sets TODO: find a better location in code\<close>
    56 axiomatization where
    57   rle_refl:           "(n::real) <= n" and
    58   not_true:           "(~ True) = False" and
    59   not_false:          "(~ False) = True" and
    60   and_true:           "(a & True) = a" and
    61   and_false:          "(a & False) = False" and
    62   or_true:            "(a | True) = True" and
    63   or_false:           "(a | False) = a" and
    64   and_commute:        "(a & b) = (b & a)" and
    65   or_commute:         "(a | b) = (b | a)" 
    66 
    67 subsection \<open>ML code for functions in pre-conditions and program-expressions\<close>
    68 ML \<open>
    69 signature PROG_EXPR =
    70   sig
    71 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
    72     val EmptyList: term
    73     val UniversalList: term
    74 
    75     val lhs: term -> term
    76     val eval_lhs: 'a -> string -> term -> 'b -> (string * term) option
    77     val eval_matches: string -> string -> term -> theory -> (string * term) option
    78     val matchsub: theory -> term -> term -> bool
    79     val eval_matchsub: string -> string -> term -> theory -> (string * term) option
    80     val rhs: term -> term
    81     val eval_rhs: 'a -> string -> term -> 'b -> (string * term) option
    82     val eval_var: string -> string -> term -> theory -> (string * term) option
    83 
    84     val or2list: term -> term
    85 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
    86 (*//------------------------- from Atools .thy------------------------------------------------\\*)
    87     val occurs_in: term -> term -> bool
    88     val eval_occurs_in: 'a -> string -> term -> 'b -> (string * term) option
    89     val some_occur_in: term list -> term -> bool
    90     val eval_some_occur_in: 'a -> string -> term -> 'b -> (string * term) option
    91     val eval_is_atom: string -> string -> term -> 'a -> (string * term) option
    92     val even: int -> bool
    93     val eval_is_even: string -> string -> term -> 'a -> (string * term) option
    94     val eval_const: string -> string -> term -> 'a -> (string * term) option
    95     val eval_equ: string -> string -> term -> 'a -> (string * term) option
    96     val eval_ident: string -> string -> term -> theory -> (string * term) option
    97     val eval_equal: string -> string -> term -> theory -> (string * term) option
    98     val eval_cancel: string -> string -> term -> 'a -> (string * term) option
    99     val eval_argument_in: string -> string -> term -> 'a -> (string * term) option
   100     val same_funid: term -> term -> bool
   101     val eval_sameFunId: string -> string -> term -> 'a -> (string * term) option
   102     val eval_filter_sameFunId: string -> string -> term -> 'a -> (string * term) option
   103     val list2sum: term list -> term
   104     val eval_boollist2sum: string -> string -> term -> 'a -> (string * term) option
   105 
   106     val mk_thmid_f: string -> (int * int) * (int * int) -> (int * int) * (int * int) -> string
   107 (*\\------------------------- from Atools.thy------------------------------------------------//*)
   108   end
   109 
   110 (**)
   111 structure Prog_Expr(**): PROG_EXPR =(**)
   112 struct
   113 (**)
   114 (*//------------------------- from Tools .thy-------------------------------------------------\\*)
   115 (* auxiliary functions for scripts  WN.9.00*)
   116 (*11.02: for equation solving only*)
   117 val UniversalList = (Thm.term_of o the o (TermC.parse @{theory})) "UniversalList";
   118 val EmptyList = (Thm.term_of o the o (TermC.parse @{theory}))  "[]::bool list";     
   119 
   120 (*+ for Or_to_List +*)
   121 fun or2list (Const ("HOL.True",_)) = (tracing"### or2list True";UniversalList)
   122   | or2list (Const ("HOL.False",_)) = (tracing"### or2list False";EmptyList)
   123   | or2list (t as Const ("HOL.eq",_) $ _ $ _) = TermC.list2isalist HOLogic.boolT [t]
   124   | or2list ors =
   125     let
   126       fun get ls (Const ("HOL.disj",_) $ o1 $ o2) =
   127 	        (case o2 of
   128 	         	Const ("HOL.disj",_) $ _ $ _ => get (ls @ [o1]) o2
   129 	        | _ => ls @ [o1, o2])
   130         | get _ t = raise TERM ("or2list: missing pattern in fun.def", [t])
   131     in (((TermC.list2isalist HOLogic.boolT) o (get [])) ors) end
   132 (*>val t = @{term True};
   133 > val t' = or2list t;
   134 > term2str t';
   135 "Prog_Expr.UniversalList"
   136 > val t = @{term False};
   137 > val t' = or2list t;
   138 > term2str t';
   139 "[]"
   140 > val t=(Thm.term_of o the o (parse thy)) "x=3";
   141 > val t' = or2list t;
   142 > term2str t';
   143 "[x = 3]"
   144 > val t=(Thm.term_of o the o (parse thy))"(x=3) | (x=-3) | (x=0)";
   145 > val t' = or2list t;
   146 > term2str t';
   147 "[x = #3, x = #-3, x = #0]" : string *)
   148 
   149 
   150 (** evaluation on the meta-level **)
   151 
   152 (*. evaluate the predicate matches (match on whole term only) .*)
   153 (*("matches",("Prog_Expr.matches", eval_matches "#matches_")):calc*)
   154 fun eval_matches (_:string) "Prog_Expr.matches"
   155 		  (t as Const ("Prog_Expr.matches",_) $ pat $ tst) thy = 
   156     if TermC.matches thy tst pat
   157     then 
   158       let
   159         val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
   160 	    in SOME (Rule.term_to_string''' thy prop, prop) end
   161     else 
   162       let 
   163         val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
   164 	    in SOME (Rule.term_to_string''' thy prop, prop) end
   165   | eval_matches _ _ _ _ = NONE; 
   166 (*
   167 > val t  = (Thm.term_of o the o (parse thy)) 
   168 	      "matches (?x = 0) (1 * x ^^^ 2 = 0)";
   169 > eval_matches "/thmid/" "/op_/" t thy;
   170 val it =
   171   SOME
   172     ("matches (x = 0) (1 * x ^^^ 2 = 0) = False",
   173      Const (#,#) $ (# $ # $ Const #)) : (string * term) option
   174 
   175 > val t  = (Thm.term_of o the o (parse thy)) 
   176 	      "matches (?a = #0) (#1 * x ^^^ #2 = #0)";
   177 > eval_matches "/thmid/" "/op_/" t thy;
   178 val it =
   179   SOME
   180     ("matches (?a = #0) (#1 * x ^^^ #2 = #0) = True",
   181      Const (#,#) $ (# $ # $ Const #)) : (string * term) option
   182 
   183 > val t  = (Thm.term_of o the o (parse thy)) 
   184 	      "matches (?a * x = #0) (#1 * x ^^^ #2 = #0)";
   185 > eval_matches "/thmid/" "/op_/" t thy;
   186 val it =
   187   SOME
   188     ("matches (?a * x = #0) (#1 * x ^^^ #2 = #0) = False",
   189      Const (#,#) $ (# $ # $ Const #)) : (string * term) option
   190 
   191 > val t  = (Thm.term_of o the o (parse thy)) 
   192 	      "matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0)";
   193 > eval_matches "/thmid/" "/op_/" t thy;
   194 val it =
   195   SOME
   196     ("matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0) = True",
   197      Const (#,#) $ (# $ # $ Const #)) : (string * term) option                  
   198 ----- before ?patterns ---:
   199 > val t  = (Thm.term_of o the o (parse thy)) 
   200 	      "matches (a * b^^^#2 = c) (#3 * x^^^#2 = #1)";
   201 > eval_matches "/thmid/" "/op_/" t thy;
   202 SOME
   203     ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2 = #1) = True",
   204      Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
   205   : (string * term) option 
   206 
   207 > val t = (Thm.term_of o the o (parse thy)) 
   208 	      "matches (a * b^^^#2 = c) (#3 * x^^^#2222 = #1)";
   209 > eval_matches "/thmid/" "/op_/" t thy;
   210 SOME ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2222 = #1) = False",
   211      Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
   212 
   213 > val t = (Thm.term_of o the o (parse thy)) 
   214                "matches (a = b) (x + #1 + #-1 * #2 = #0)";
   215 > eval_matches "/thmid/" "/op_/" t thy;
   216 SOME ("matches (a = b) (x + #1 + #-1 * #2 = #0) = True",Const # $ (# $ #))
   217 *)
   218 
   219 (*.does a pattern match some subterm ?.*)
   220 fun matchsub thy t pat =  
   221   let
   222     fun matchs (t as Const _) = TermC.matches thy t pat
   223 	      | matchs (t as Free _) = TermC.matches thy t pat
   224 	      | matchs (t as Var _) = TermC.matches thy t pat
   225 	      | matchs (Bound _) = false
   226 	      | matchs (t as Abs (_, _, body)) = 
   227 	          if TermC.matches thy t pat then true else TermC.matches thy body pat
   228 	      | matchs (t as f1 $ f2) =
   229 	          if TermC.matches thy t pat then true 
   230 	            else if matchs f1 then true else matchs f2
   231   in matchs t end;
   232 
   233 (*("matchsub",("Prog_Expr.matchsub", eval_matchsub "#matchsub_")):calc*)
   234 fun eval_matchsub (_:string) "Prog_Expr.matchsub"
   235 		  (t as Const ("Prog_Expr.matchsub",_) $ pat $ tst) thy = 
   236     if matchsub thy tst pat
   237     then 
   238       let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True}))
   239       in SOME (Rule.term_to_string''' thy prop, prop) end
   240     else 
   241       let val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))
   242       in SOME (Rule.term_to_string''' thy prop, prop) end
   243   | eval_matchsub _ _ _ _ = NONE; 
   244 
   245 (*get the variables in an isabelle-term*)
   246 (*("Vars"    ,("Prog_Expr.Vars"    , eval_var "#Vars_")):calc*)
   247 fun eval_var (thmid:string) "Prog_Expr.Vars" (t as (Const _ $ arg)) thy = 
   248     let 
   249       val t' = ((TermC.list2isalist HOLogic.realT) o TermC.vars) t;
   250       val thmId = thmid ^ Rule.term_to_string''' thy arg;
   251     in SOME (thmId, HOLogic.Trueprop $ (TermC.mk_equality (t, t'))) end
   252   | eval_var _ _ _ _ = NONE;
   253 
   254 fun lhs (Const ("HOL.eq",_) $ l $ _) = l
   255   | lhs t = error("lhs called with (" ^ Rule.term2str t ^ ")");
   256 (*("lhs"    ,("Prog_Expr.lhs"    , eval_lhs "")):calc*)
   257 fun eval_lhs _ "Prog_Expr.lhs"
   258 	     (t as (Const ("Prog_Expr.lhs",_) $ (Const ("HOL.eq",_) $ l $ _))) _ = 
   259     SOME ((Rule.term2str t) ^ " = " ^ (Rule.term2str l),
   260 	  HOLogic.Trueprop $ (TermC.mk_equality (t, l)))
   261   | eval_lhs _ _ _ _ = NONE;
   262 (*
   263 > val t = (Thm.term_of o the o (parse thy)) "lhs (1 * x ^^^ 2 = 0)";
   264 > val SOME (id,t') = eval_lhs 0 0 t 0;
   265 val id = "Prog_Expr.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
   266 > term2str t';
   267 val it = "Prog_Expr.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
   268 *)
   269 
   270 fun rhs (Const ("HOL.eq",_) $ _ $ r) = r
   271   | rhs t = error("rhs called with (" ^ Rule.term2str t ^ ")");
   272 (*("rhs"    ,("Prog_Expr.rhs"    , eval_rhs "")):calc*)
   273 fun eval_rhs _ "Prog_Expr.rhs"
   274 	     (t as (Const ("Prog_Expr.rhs",_) $ (Const ("HOL.eq",_) $ _ $ r))) _ = 
   275     SOME (Rule.term2str t ^ " = " ^ Rule.term2str r,
   276 	  HOLogic.Trueprop $ (TermC.mk_equality (t, r)))
   277   | eval_rhs _ _ _ _ = NONE;
   278 (*\\------------------------- from Prog_Expr.thy-------------------------------------------------//*)
   279 
   280 (*//------------------------- from Atools.thy------------------------------------------------\\*)
   281 fun occurs_in v t = member op = (((map strip_thy) o TermC.ids2str) t) (Term.term_name v);
   282 
   283 (*("occurs_in", ("Prog_Expr.occurs'_in", Prog_Expr.eval_occurs_in ""))*)
   284 fun eval_occurs_in _ "Prog_Expr.occurs'_in"
   285 	     (p as (Const ("Prog_Expr.occurs'_in",_) $ v $ t)) _ =
   286     ((*tracing("@@@ eval_occurs_in: v= "^(Rule.term2str v));
   287      tracing("@@@ eval_occurs_in: t= "^(Rule.term2str t));*)
   288      if occurs_in v t
   289     then SOME ((Rule.term2str p) ^ " = True",
   290 	  HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
   291     else SOME ((Rule.term2str p) ^ " = False",
   292 	  HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False}))))
   293   | eval_occurs_in _ _ _ _ = NONE;
   294 
   295 (*some of the (bound) variables (eg. in an eqsys) "vs" occur in term "t"*)   
   296 fun some_occur_in vs t = 
   297     let fun occurs_in' a b = occurs_in b a
   298     in foldl or_ (false, map (occurs_in' t) vs) end;
   299 
   300 (*("some_occur_in", ("Prog_Expr.some'_occur'_in", 
   301 			eval_some_occur_in "#eval_some_occur_in_"))*)
   302 fun eval_some_occur_in _ "Prog_Expr.some'_occur'_in"
   303 			  (p as (Const ("Prog_Expr.some'_occur'_in",_) 
   304 				       $ vs $ t)) _ =
   305     if some_occur_in (TermC.isalist2list vs) t
   306     then SOME ((Rule.term2str p) ^ " = True",
   307 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
   308     else SOME ((Rule.term2str p) ^ " = False",
   309 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
   310   | eval_some_occur_in _ _ _ _ = NONE;
   311 
   312 (*("is_atom",("Prog_Expr.is'_atom", Prog_Expr.eval_is_atom "#is_atom_"))*)
   313 fun eval_is_atom (thmid:string) "Prog_Expr.is'_atom"
   314 		 (t as (Const _ $ arg)) _ = 
   315     (case arg of 
   316 	 Free (n,_) => SOME (TermC.mk_thmid thmid n "", 
   317 			      HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
   318        | _ => SOME (TermC.mk_thmid thmid "" "", 
   319 		    HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
   320   | eval_is_atom _ _ _ _ = NONE;
   321 
   322 fun even i = (i div 2) * 2 = i;
   323 (*("is_even",("Prog_Expr.is'_even", eval_is_even "#is_even_"))*)
   324 fun eval_is_even (thmid:string) "Prog_Expr.is'_even"
   325 		 (t as (Const _ $ arg)) _ = 
   326     (case arg of 
   327 	Free (n,_) =>
   328 	 (case TermC.int_of_str_opt n of
   329 	      SOME i =>
   330 	      if even i then SOME (TermC.mk_thmid thmid n "", 
   331 				   HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
   332 	      else SOME (TermC.mk_thmid thmid "" "", 
   333 			 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
   334 	    | _ => NONE)
   335        | _ => NONE)
   336   | eval_is_even _ _ _ _ = NONE; 
   337 
   338 (*evaluate 'is_const'*)
   339 (*("is_const",("Prog_Expr.is'_const", Prog_Expr.eval_const "#is_const_"))*)
   340 fun eval_const (thmid:string) _ (t as (Const _ $ arg)) _ = 
   341     (case arg of 
   342        Const (n1, _) =>
   343 	     SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
   344      | Free (n1, _) =>
   345 	     if TermC.is_num' n1
   346 	     then SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
   347 	     else SOME (TermC.mk_thmid thmid n1 "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
   348      | _ => (*NONE*)
   349        SOME (TermC.mk_thmid thmid (Rule.term2str arg) "", HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False}))))
   350   | eval_const _ _ _ _ = NONE; 
   351 
   352 (*. evaluate binary, associative, commutative operators: *,+,^ .*)
   353 (*("PLUS"    ,("Groups.plus_class.plus"        , (**)eval_binop "#add_")),
   354   ("TIMES"   ,("Groups.times_class.times"        , (**)eval_binop "#mult_")),
   355   ("POWER"  ,("Prog_Expr.pow"  , (**)eval_binop "#power_"))*)
   356 
   357 (* val (thmid,op_,t as(Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2)),thy) =
   358        ("xxxxxx",op_,t,thy);
   359    *)
   360 fun mk_thmid_f thmid ((v11, v12), (p11, p12)) ((v21, v22), (p21, p22)) = (* dropped *)
   361   thmid ^ "Float ((" ^ 
   362   (string_of_int v11)^","^(string_of_int v12)^"), ("^
   363   (string_of_int p11)^","^(string_of_int p12)^")) __ (("^
   364   (string_of_int v21)^","^(string_of_int v22)^"), ("^
   365   (string_of_int p21)^","^(string_of_int p22)^"))";
   366 
   367 (*.evaluate < and <= for numerals.*)
   368 (*("le"      ,("Orderings.ord_class.less"        , Prog_Expr.eval_equ "#less_")),
   369   ("leq"     ,("Orderings.ord_class.less_eq"       , Prog_Expr.eval_equ "#less_equal_"))*)
   370 fun eval_equ (thmid:string) (_(*op_*)) (t as 
   371 	       (Const (op0, _) $ Free (n1, _) $ Free(n2, _))) _ = 
   372     (case (TermC.int_of_str_opt n1, TermC.int_of_str_opt n2) of
   373 	 (SOME n1', SOME n2') =>
   374   if Calc.calc_equ (strip_thy op0) (n1', n2')
   375     then SOME (TermC.mk_thmid thmid n1 n2, 
   376 	  HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
   377   else SOME (TermC.mk_thmid thmid n1 n2,  
   378 	  HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
   379        | _ => NONE)
   380     
   381   | eval_equ _ _ _ _ = NONE;
   382 
   383 
   384 (*evaluate identity
   385 > reflI;
   386 val it = "(?t = ?t) = True"
   387 > val t = str2term "x = 0";
   388 > val NONE = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
   389 
   390 > val t = str2term "1 = 0";
   391 > val NONE = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
   392 ----------- thus needs Rule.Calc !
   393 > val t = str2term "0 = 0";
   394 > val SOME (t',_) = rewrite_ thy Rule.dummy_ord Rule.e_rls false reflI t;
   395 > Rule.term2str t';
   396 val it = "HOL.True"
   397 
   398 val t = str2term "Not (x = 0)";
   399 atomt t; Rule.term2str t;
   400 *** -------------
   401 *** Const ( Not)
   402 *** . Const ( op =)
   403 *** . . Free ( x, )
   404 *** . . Free ( 0, )
   405 val it = "x ~= 0" : string*)
   406 
   407 (*.evaluate identity on the term-level, =!= ,i.e. without evaluation of 
   408   the arguments: thus special handling by 'fun eval_binop'*)
   409 (*("ident"   ,("Prog_Expr.ident", Prog_Expr.eval_ident "#ident_")):calc*)
   410 fun eval_ident (thmid:string) "Prog_Expr.ident" (t as 
   411 	       (Const _(*op0, t0*) $ t1 $ t2 )) thy = 
   412   if t1 = t2
   413   then SOME (TermC.mk_thmid thmid 
   414 	              ("(" ^ (Rule.term_to_string''' thy t1) ^ ")")
   415 	              ("(" ^ (Rule.term_to_string''' thy t2) ^ ")"), 
   416 	     HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
   417   else SOME (TermC.mk_thmid thmid  
   418 	              ("(" ^ (Rule.term_to_string''' thy t1) ^ ")")
   419 	              ("(" ^ (Rule.term_to_string''' thy t2) ^ ")"),  
   420 	     HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
   421   | eval_ident _ _ _ _ = NONE;
   422 (* TODO
   423 > val t = str2term "x =!= 0";
   424 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
   425 > Rule.term2str t';
   426 val str = "ident_(x)_(0)" : string
   427 val it = "(x =!= 0) = False" : string                                
   428 > val t = str2term "1 =!= 0";
   429 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
   430 > Rule.term2str t';
   431 val str = "ident_(1)_(0)" : string 
   432 val it = "(1 =!= 0) = False" : string                                       
   433 > val t = str2term "0 =!= 0";
   434 > val SOME (str, t') = eval_ident "ident_" "b" t thy;
   435 > Rule.term2str t';
   436 val str = "ident_(0)_(0)" : string
   437 val it = "(0 =!= 0) = True" : string
   438 *)
   439 
   440 (*.evaluate identity of terms, which stay ready for evaluation in turn;
   441   thus returns False only for atoms.*)
   442 (*("equal"   ,("HOL.eq", Prog_Expr.eval_equal "#equal_")):calc*)
   443 fun eval_equal (thmid : string) "HOL.eq" (t as (Const _(*op0,t0*) $ t1 $ t2 )) thy = 
   444   if t1 = t2
   445   then SOME (TermC.mk_thmid thmid 
   446                 ("(" ^ Rule.term_to_string''' thy t1 ^ ")")
   447                 ("(" ^ Rule.term_to_string''' thy t2 ^ ")"), 
   448        HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
   449   else (case (TermC.is_atom t1, TermC.is_atom t2) of
   450       (true, true) => 
   451       SOME (TermC.mk_thmid thmid  
   452          ("(" ^ Rule.term_to_string''' thy t1 ^ ")")
   453          ("(" ^ Rule.term_to_string''' thy t2 ^ ")"),
   454       HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
   455     | _ => NONE)                             (* NOT is_atom t1,t2 --> rew_sub *)
   456   | eval_equal _ _ _ _ = NONE;                                  (* error-exit *)
   457 (*
   458 val t = str2term "x ~= 0";
   459 val NONE = eval_equal "equal_" "b" t thy;
   460 
   461 
   462 > val t = str2term "(x + 1) = (x + 1)";
   463 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
   464 > Rule.term2str t';
   465 val str = "equal_(x + 1)_(x + 1)" : string
   466 val it = "(x + 1 = x + 1) = True" : string
   467 > val t = str2term "x = 0";
   468 > val NONE = eval_equal "equal_" "b" t thy;
   469 
   470 > val t = str2term "1 = 0";
   471 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
   472 > Rule.term2str t';
   473 val str = "equal_(1)_(0)" : string 
   474 val it = "(1 = 0) = False" : string
   475 > val t = str2term "0 = 0";
   476 > val SOME (str, t') = eval_equal "equal_" "b" t thy;
   477 > Rule.term2str t';
   478 val str = "equal_(0)_(0)" : string
   479 val it = "(0 = 0) = True" : string
   480 *)
   481 
   482 (*. evaluate HOL.divide .*)
   483 (*("DIVIDE" ,("Rings.divide_class.divide"  , eval_cancel "#divide_e"))*)
   484 fun eval_cancel (thmid: string) "Rings.divide_class.divide" (t as 
   485 	       (Const (op0,t0) $ Free (n1, _) $ Free(n2, _))) _ = 
   486     (case (TermC.int_of_str_opt n1, TermC.int_of_str_opt n2) of
   487     	(SOME n1', SOME n2') =>
   488         let 
   489           val sg = Calc.sign_mult n1' n2';
   490           val (T1,T2,Trange) = TermC.dest_binop_typ t0;
   491           val gcd' = Calc.gcd (abs n1') (abs n2');
   492         in if gcd' = abs n2' 
   493            then let val rhs = TermC.term_of_num Trange (sg * (abs n1') div gcd')
   494     	        val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
   495     	    in SOME (TermC.mk_thmid thmid n1 n2, prop) end     
   496            else if 0 < n2' andalso gcd' = 1 then NONE
   497            else let val rhs = TermC.mk_num_op_num T1 T2 (op0,t0) (sg * (abs n1') div gcd')
   498     	  			   ((abs n2') div gcd')
   499     	        val prop = HOLogic.Trueprop $ (TermC.mk_equality (t, rhs))
   500     	    in SOME (TermC.mk_thmid thmid n1 n2, prop) end
   501         end
   502       | _ => ((*tracing"@@@ eval_cancel NONE";*)NONE))
   503   | eval_cancel _ _ _ _ = NONE;
   504 
   505 (* get the argument from a function-definition *)
   506 (*("argument_in" ,("Prog_Expr.argument'_in", Prog_Expr.eval_argument_in "Prog_Expr.argument'_in"))*)
   507 fun eval_argument_in _ "Prog_Expr.argument'_in" 
   508 		 (t as (Const ("Prog_Expr.argument'_in", _) $ (_(*f*) $ arg))) _ =
   509     if is_Free arg (*could be something to be simplified before*)
   510     then
   511       SOME (Rule.term2str t ^ " = " ^ Rule.term2str arg,
   512 	      HOLogic.Trueprop $ (TermC.mk_equality (t, arg)))
   513     else NONE
   514   | eval_argument_in _ _ _ _ = NONE;
   515 
   516 (* check if the function-identifier of the first argument matches 
   517    the function-identifier of the lhs of the second argument *)
   518 (*("sameFunId" ,("Prog_Expr.sameFunId", eval_same_funid "Prog_Expr.sameFunId"))*)
   519 fun eval_sameFunId _ "Prog_Expr.sameFunId" 
   520 		     (p as Const ("Prog_Expr.sameFunId",_) $  (f1 $ _) $ (Const ("HOL.eq", _) $ (f2 $ _) $ _)) _ =
   521     if f1 = f2 
   522     then SOME ((Rule.term2str p) ^ " = True",
   523 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
   524     else SOME ((Rule.term2str p) ^ " = False",
   525 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
   526 | eval_sameFunId _ _ _ _ = NONE;
   527 
   528 
   529 (*.from a list of fun-definitions "f x = ..." as 2nd argument
   530    filter the elements with the same fun-identfier in "f y"
   531    as the fst argument;
   532    this is, because Isabelles filter takes more than 1 sec.*)
   533 fun same_funid f1 (Const ("HOL.eq", _) $ (f2 $ _) $ _) = f1 = f2
   534   | same_funid f1 t = error ("same_funid called with t = ("
   535 				   ^Rule.term2str f1^") ("^Rule.term2str t^")");
   536 (*("filter_sameFunId" ,("Prog_Expr.filter'_sameFunId",
   537 		   eval_filter_sameFunId "Prog_Expr.filter'_sameFunId"))*)
   538 fun eval_filter_sameFunId _ "Prog_Expr.filter'_sameFunId" 
   539 		     (p as Const ("Prog_Expr.filter'_sameFunId",_) $ 
   540 			(fid $ _) $ fs) _ =
   541     let val fs' = ((TermC.list2isalist HOLogic.boolT) o 
   542 		   (filter (same_funid fid))) (TermC.isalist2list fs)
   543     in SOME (Rule.term2str (TermC.mk_equality (p, fs')),
   544 	       HOLogic.Trueprop $ (TermC.mk_equality (p, fs'))) end
   545 | eval_filter_sameFunId _ _ _ _ = NONE;
   546 
   547 (* make a list of terms to a sum *)
   548 fun list2sum [] = error ("list2sum called with []")
   549   | list2sum [s] = s
   550   | list2sum (s::ss) = 
   551     let
   552       fun sum su [s'] = Const ("Groups.plus_class.plus",
   553            [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s'
   554     	  | sum su (s'::ss') = sum (Const ("Groups.plus_class.plus",
   555            [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ su $ s') ss'
   556     	  | sum _ _ = raise ERROR "list2sum: pattern in fun.def is missing" 
   557     in sum s ss end;
   558 
   559 (* make a list of equalities to the sum of the lhs *)
   560 (*("boollist2sum"    ,("Prog_Expr.boollist2sum"    , eval_boollist2sum "")):calc*)
   561 fun eval_boollist2sum _ "Prog_Expr.boollist2sum" 
   562 		  (p as Const ("Prog_Expr.boollist2sum", _) $ (l as Const ("List.list.Cons", _) $ _ $ _)) _ =
   563     let
   564       val isal = TermC.isalist2list l
   565 	    val lhss = map lhs isal
   566 	    val sum = list2sum lhss
   567     in
   568       SOME ((Rule.term2str p) ^ " = " ^ (Rule.term2str sum),
   569 	      HOLogic.Trueprop $ (TermC.mk_equality (p, sum)))
   570     end
   571   | eval_boollist2sum _ _ _ _ = NONE;
   572 
   573 (**) end (*struct*)
   574 \<close> text \<open>
   575 open Prog_Expr
   576 \<close> ML \<open>
   577 
   578 \<close> ML \<open>
   579 \<close> ML \<open>
   580 \<close> ML \<open>
   581 \<close>
   582 
   583 subsection \<open>extend rule-set for evaluating pre-conditions and program-expressions\<close>
   584 ML \<open>
   585 val list_rls = Rule.append_rls "list_rls" list_rls [Rule.Calc ("Prog_Expr.rhs", Prog_Expr.eval_rhs "")];
   586 \<close> ML \<open>
   587 \<close> ML \<open>
   588 \<close>
   589 
   590 subsection \<open>setup for rule-sets and ML-functions\<close>
   591 setup \<open>KEStore_Elems.add_rlss [("list_rls", (Context.theory_name @{theory}, list_rls))]\<close>
   592 setup \<open>KEStore_Elems.add_calcs
   593   [("matches", ("Prog_Expr.matches", Prog_Expr.eval_matches "#matches_")),
   594     ("matchsub", ("Prog_Expr.matchsub", Prog_Expr.eval_matchsub "#matchsub_")),
   595     ("Vars", ("Prog_Expr.Vars", Prog_Expr.eval_var "#Vars_")),
   596     ("lhs", ("Prog_Expr.lhs", Prog_Expr.eval_lhs "")),
   597     ("rhs", ("Prog_Expr.rhs", Prog_Expr.eval_rhs ""))]\<close>
   598 (*\\------------------------- from Tools .thy-------------------------------------------------//*)
   599 ML \<open>
   600 \<close> ML \<open>
   601 \<close> ML \<open>
   602 \<close>
   603 
   604 end