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