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