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