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