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