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