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