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