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