neuper@37906
|
1 |
(* isac's rewriter
|
neuper@37906
|
2 |
(c) Walther Neuper 2000
|
neuper@37906
|
3 |
*)
|
neuper@37906
|
4 |
|
wneuper@59380
|
5 |
signature REWRITE =
|
walther@59901
|
6 |
sig
|
walther@59919
|
7 |
val calculate_: theory -> string * Eval_Def.eval_fn -> term -> (term * (string * thm)) option
|
walther@59901
|
8 |
val eval__true: theory -> int -> term list -> (term * term) list -> Rule_Set.T -> term list * bool
|
walther@59901
|
9 |
val eval_prog_expr: theory -> Rule_Set.T -> term -> term
|
walther@59901
|
10 |
val eval_true_: theory -> Rule_Set.T -> term -> bool
|
walther@59901
|
11 |
val eval_true: theory -> term list -> Rule_Set.T -> bool
|
walther@59907
|
12 |
val rew_sub: theory -> int -> (term * term) list -> Rule_Def.rew_ord_
|
walther@59901
|
13 |
-> Rule_Set.T -> bool -> TermC.path -> term -> term -> term * term list * TermC.path * bool
|
walther@59907
|
14 |
val rewrite_: theory -> Rule_Def.rew_ord_ -> Rule_Set.T -> bool -> thm ->
|
walther@59901
|
15 |
term -> (term * term list) option
|
walther@59907
|
16 |
val rewrite_inst_: theory -> Rule_Def.rew_ord_ -> Rule_Set.T -> bool
|
walther@59901
|
17 |
-> (term * term) list -> thm -> term -> (term * term list) option
|
walther@59901
|
18 |
val rewrite_set_: theory -> bool -> Rule_Set.T -> term -> (term * term list) option
|
walther@59901
|
19 |
val rewrite_set_inst_: theory -> bool -> (term * term) list -> Rule_Set.T -> term -> (term * term list) option
|
walther@59907
|
20 |
val rewrite_terms_: theory -> Rule_Def.rew_ord_ -> Rule_Set.T -> term list
|
walther@59901
|
21 |
-> term -> (term * term list) option
|
walther@59901
|
22 |
|
walther@59901
|
23 |
val trace_on: bool Unsynchronized.ref
|
walther@59901
|
24 |
val depth: int Unsynchronized.ref
|
walther@59901
|
25 |
val lim_deriv: int Unsynchronized.ref
|
walther@59901
|
26 |
|
wenzelm@60223
|
27 |
\<^isac_test>\<open>
|
walther@60262
|
28 |
exception NO_REWRITE
|
walther@59907
|
29 |
val rewrite__: theory -> int -> (term * term) list -> Rule_Def.rew_ord_ ->
|
walther@59901
|
30 |
Rule_Set.T -> bool -> thm -> term -> (term * term list) option
|
walther@59901
|
31 |
val rewrite__set_: theory -> int -> bool -> (term * term) list -> Rule_Set.T -> term -> (term * term list) option
|
walther@59901
|
32 |
val app_rev: theory -> int -> Rule_Set.T -> term -> term * term list * bool
|
walther@59901
|
33 |
val app_sub: theory -> int -> Rule_Set.T -> term -> term * term list * bool
|
walther@59901
|
34 |
val trace1: int -> string -> unit
|
walther@60262
|
35 |
val trace_eq1 : int -> string -> Rule_Def.rule_set -> theory -> term -> unit;
|
walther@60262
|
36 |
val trace_eq2 : int -> string -> theory -> term -> term -> unit;
|
walther@60262
|
37 |
val trace_in1 : int -> string -> string -> unit;
|
walther@60262
|
38 |
val trace_in2 : int -> string -> theory -> term -> unit;
|
walther@60262
|
39 |
val trace_in3 : int -> string -> theory -> (term * 'a) option -> unit;
|
walther@60262
|
40 |
val trace_in4 : int -> string -> theory -> term list -> term list -> unit;
|
walther@60262
|
41 |
val trace_in5 : int -> string -> theory -> term list -> unit;
|
wenzelm@60223
|
42 |
\<close>
|
walther@59901
|
43 |
end
|
wneuper@59380
|
44 |
|
wneuper@59380
|
45 |
(**)
|
wneuper@59380
|
46 |
structure Rewrite(**): REWRITE(**) =
|
wneuper@59380
|
47 |
struct
|
wneuper@59380
|
48 |
(**)
|
neuper@37906
|
49 |
|
neuper@37906
|
50 |
exception NO_REWRITE;
|
neuper@37906
|
51 |
|
walther@59901
|
52 |
val trace_on = Unsynchronized.ref false;
|
walther@59901
|
53 |
(* depth of recursion in traces of the rewriter, if trace_on:=true *)
|
walther@59901
|
54 |
val depth = Unsynchronized.ref 99999;
|
walther@59901
|
55 |
(* no of rewrites exceeding this int -> NO rewrite *)
|
walther@59901
|
56 |
val lim_deriv = Unsynchronized.ref 100;
|
walther@59901
|
57 |
|
neuper@52101
|
58 |
fun trace i str =
|
walther@59901
|
59 |
if ! trace_on andalso i < ! depth then tracing (idt "#" i ^ str) else ()
|
walther@60262
|
60 |
fun trace_eq1 i str rrls thy t =
|
walther@60262
|
61 |
trace i (" " ^ str ^ ": " ^ Rule_Set.id rrls ^ " on: " ^ UnparseC.term_in_thy thy t)
|
walther@60262
|
62 |
fun trace_eq2 i str thy t t' =
|
walther@60262
|
63 |
trace i (" " ^ str ^ ": \"" ^
|
walther@60262
|
64 |
UnparseC.term_in_thy thy t ^ "\" > \"" ^ UnparseC.term_in_thy thy t' ^ "\"");
|
walther@60262
|
65 |
|
walther@60262
|
66 |
fun trace1 i str =
|
walther@59901
|
67 |
if ! trace_on andalso i < ! depth then tracing (idt "#" (i + 1) ^ str) else ()
|
walther@60262
|
68 |
fun trace_in1 i str thmid =
|
walther@60262
|
69 |
trace1 i (" " ^ str ^ ": \"" ^ thmid ^ "\"")
|
walther@60262
|
70 |
fun trace_in2 i str thy t =
|
walther@60262
|
71 |
trace1 i (" " ^ str ^ ": \"" ^ UnparseC.term_in_thy thy t ^ "\"");
|
walther@60262
|
72 |
fun trace_in3 i str thy pairopt =
|
walther@60262
|
73 |
trace1 i (" " ^ str ^ ": " ^ UnparseC.term_in_thy thy ((fst o the) pairopt));
|
walther@60262
|
74 |
fun trace_in4 i str thy ts ts' =
|
walther@60262
|
75 |
if ! trace_on andalso i < ! depth andalso ts <> []
|
walther@60262
|
76 |
then tracing (idt "#" (i + 1) ^ " " ^ str ^ ": " ^ UnparseC.terms_in_thy thy ts ^
|
walther@60262
|
77 |
" stored: " ^ UnparseC.terms_in_thy thy ts')
|
walther@60262
|
78 |
else ();
|
walther@60262
|
79 |
fun trace_in5 i str thy p' =
|
walther@60262
|
80 |
if ! trace_on andalso i < ! depth
|
walther@60262
|
81 |
then tracing (idt "#" (i + 1) ^ " " ^ str ^ ": " ^ UnparseC.terms_in_thy thy p')
|
walther@60262
|
82 |
else();
|
neuper@52101
|
83 |
|
neuper@37906
|
84 |
fun rewrite__ thy i bdv tless rls put_asm thm ct =
|
wneuper@59381
|
85 |
let
|
walther@60262
|
86 |
val (t', asms, _(*lrd*), rew) = rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
|
wenzelm@60203
|
87 |
(TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm))) ct
|
walther@60017
|
88 |
in if rew then SOME (t', distinct op = asms) else NONE end
|
walther@60262
|
89 |
(* one rewrite (possibly conditional, ordered) EXOR exn EXOR go into subterms *)
|
wneuper@59381
|
90 |
and rew_sub thy i bdv tless rls put_asm lrd r t =
|
neuper@38022
|
91 |
(let
|
wneuper@59381
|
92 |
val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
|
walther@60262
|
93 |
val r' = (Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r)
|
walther@60262
|
94 |
handle Pattern.MATCH => raise NO_REWRITE
|
wneuper@59381
|
95 |
val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
|
wneuper@59381
|
96 |
val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
|
walther@60262
|
97 |
val _ = trace_in2 i "eval asms" thy r';
|
walther@60262
|
98 |
val (t'', p'') = (*conditional rewriting*)
|
walther@60262
|
99 |
let val (simpl_p', nofalse) = eval__true thy (i + 1) p' bdv rls
|
wneuper@59381
|
100 |
in
|
wneuper@59381
|
101 |
if nofalse
|
walther@60262
|
102 |
then (trace_in4 i "asms accepted" thy p' simpl_p'; (t',simpl_p'))(* uncond.rew.from above*)
|
walther@60262
|
103 |
else (trace_in5 i "asms false" thy p'; raise NO_REWRITE) (* don't go into subtm.of cond*)
|
wneuper@59381
|
104 |
end
|
walther@60262
|
105 |
in
|
walther@60262
|
106 |
if TermC.perm lhs rhs andalso not (tless bdv (t', t)) (*ordered rewriting*)
|
walther@60262
|
107 |
then (trace_eq2 i "not >" thy t t'; raise NO_REWRITE)
|
walther@60262
|
108 |
else (t'', p'', [], true)
|
walther@60262
|
109 |
end
|
walther@60262
|
110 |
) handle NO_REWRITE =>
|
walther@60262
|
111 |
(case t of
|
walther@60262
|
112 |
Const(s, T) => (Const(s, T), [], lrd, false)
|
walther@60262
|
113 |
| Free(s, T) => (Free(s, T), [], lrd, false)
|
walther@60262
|
114 |
| Var(n, T) => (Var(n, T), [], lrd, false)
|
walther@60262
|
115 |
| Bound i => (Bound i, [], lrd, false)
|
walther@60262
|
116 |
| Abs(s, T, body) =>
|
walther@60262
|
117 |
let val (t', asms, _ (*lrd*), rew) = rew_sub thy i bdv tless rls put_asm (lrd @ [TermC.D]) r body
|
walther@60262
|
118 |
in (Abs(s, T, t'), asms, [], rew) end
|
walther@60262
|
119 |
| t1 $ t2 =>
|
walther@60262
|
120 |
let val (t2', asm2, lrd, rew2) = rew_sub thy i bdv tless rls put_asm (lrd @ [TermC.R]) r t2
|
walther@60262
|
121 |
in
|
walther@60262
|
122 |
if rew2 then (t1 $ t2', asm2, lrd, true)
|
walther@60262
|
123 |
else
|
walther@60262
|
124 |
let val (t1', asm1, lrd, rew1) = rew_sub thy i bdv tless rls put_asm (lrd @ [TermC.L]) r t1
|
walther@60262
|
125 |
in if rew1 then (t1' $ t2, asm1, lrd, true) else (t1 $ t2,[], lrd, false) end
|
walther@60262
|
126 |
end)
|
walther@60262
|
127 |
and eval__true thy i asms bdv rls = (* simplify asumptions until one evaluates to false*)
|
neuper@48760
|
128 |
if asms = [@{term True}] orelse asms = [] then ([], true)
|
wneuper@59381
|
129 |
else (* this allows to check Rrls with prepat = ([@{term True}], pat) *)
|
wneuper@59381
|
130 |
if asms = [@{term False}] then ([], false)
|
wneuper@59381
|
131 |
else
|
wneuper@59381
|
132 |
let
|
wneuper@59381
|
133 |
fun chk indets [] = (indets, true) (*return asms<>True until false*)
|
wneuper@59381
|
134 |
| chk indets (a :: asms) =
|
wneuper@59381
|
135 |
(case rewrite__set_ thy (i + 1) false bdv rls a of
|
wneuper@59381
|
136 |
NONE => (chk (indets @ [a]) asms)
|
wneuper@59381
|
137 |
| SOME (t, a') =>
|
wneuper@59381
|
138 |
if t = @{term True} then (chk (indets @ a') asms)
|
wneuper@59381
|
139 |
else if t = @{term False} then ([], false)
|
wneuper@59381
|
140 |
(*asm false .. thm not applied ^^^; continue until False vvv*)
|
wneuper@59381
|
141 |
else chk (indets @ [t] @ a') asms);
|
wneuper@59381
|
142 |
in chk [] asms end
|
walther@60262
|
143 |
and rewrite__set_ thy _ _ _ Rule_Set.Empty t = (* rewrite with a rule set*)
|
walther@59962
|
144 |
raise ERROR ("rewrite__set_ called with 'Erls' for '" ^ UnparseC.term_in_thy thy t ^ "'")
|
walther@60262
|
145 |
| rewrite__set_ thy i _ _ (rrls as Rule_Set.Rrls _) t = (* rewrite with a 'reverse rule set'*)
|
neuper@52101
|
146 |
let
|
walther@60262
|
147 |
val _= trace_eq1 i "rls" rrls thy t;
|
wneuper@59382
|
148 |
val (t', asm, rew) = app_rev thy (i + 1) rrls t
|
walther@60017
|
149 |
in if rew then SOME (t', distinct op = asm) else NONE end
|
walther@60262
|
150 |
| rewrite__set_ thy i put_asm bdv rls ct = (* Rls, Seq containing Thms or Eval, Cal1 *)
|
neuper@52101
|
151 |
let
|
walther@59841
|
152 |
(* attention with cp to test/..: unbound thy, i, bdv, rls; TODO1803? pull out to rewrite__*)
|
neuper@52101
|
153 |
datatype switch = Appl | Noap;
|
walther@60262
|
154 |
fun rew_once _ asm ct Noap [] = (ct, asm) (* ?TODO unify with Prog_Expr.rew_once? *)
|
neuper@52101
|
155 |
| rew_once ruls asm ct Appl [] =
|
walther@59851
|
156 |
(case rls of Rule_Def.Repeat _ => rew_once ruls asm ct Noap ruls
|
walther@59878
|
157 |
| Rule_Set.Sequence _ => (ct, asm)
|
walther@59867
|
158 |
| rls => raise ERROR ("rew_once not appl. to \"" ^ Rule_Set.id rls ^ "\""))
|
wneuper@59381
|
159 |
| rew_once ruls asm ct apno (rul :: thms) =
|
neuper@52101
|
160 |
case rul of
|
wneuper@59416
|
161 |
Rule.Thm (thmid, thm) =>
|
walther@60262
|
162 |
(trace_in1 i "try thm" thmid;
|
walther@59852
|
163 |
case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule_Set.rep) rls)
|
walther@59852
|
164 |
((#erls o Rule_Set.rep) rls) put_asm thm ct of
|
neuper@52101
|
165 |
NONE => rew_once ruls asm ct apno thms
|
wneuper@59381
|
166 |
| SOME (ct', asm') =>
|
walther@60262
|
167 |
(trace_in2 i "rewrites to" thy ct';
|
wneuper@59399
|
168 |
rew_once ruls (union (op =) asm asm') ct' Appl (rul :: thms)))
|
wneuper@59399
|
169 |
(* once again try the same rule, e.g. associativity against "()"*)
|
walther@59878
|
170 |
| Rule.Eval (cc as (op_, _)) =>
|
walther@60262
|
171 |
let val _= trace_in1 i "try calc" op_;
|
wneuper@59521
|
172 |
val ct = TermC.uminus_to_string ct (*WN190312: superfluous?*)
|
walther@59878
|
173 |
in case Eval.adhoc_thm thy cc ct of
|
neuper@52101
|
174 |
NONE => rew_once ruls asm ct apno thms
|
wneuper@59381
|
175 |
| SOME (_, thm') =>
|
neuper@52101
|
176 |
let
|
walther@59852
|
177 |
val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule_Set.rep) rls)
|
walther@59852
|
178 |
((#erls o Rule_Set.rep) rls) put_asm thm' ct;
|
walther@59962
|
179 |
val _ = if pairopt <> NONE then () else raise ERROR ("rewrite_set_, rewrite_ \"" ^
|
walther@59875
|
180 |
ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
|
walther@60262
|
181 |
val _ = trace_in3 i "calc. to" thy pairopt;
|
neuper@52101
|
182 |
in rew_once ruls asm ((fst o the) pairopt) Appl (rul :: thms) end
|
neuper@52101
|
183 |
end
|
wneuper@59416
|
184 |
| Rule.Cal1 (cc as (op_, _)) =>
|
walther@60262
|
185 |
let val _= trace_in1 i "try cal1" op_;
|
wneuper@59389
|
186 |
val ct = TermC.uminus_to_string ct
|
walther@59878
|
187 |
in case Eval.adhoc_thm1_ thy cc ct of
|
neuper@52101
|
188 |
NONE => (ct, asm)
|
wneuper@59381
|
189 |
| SOME (_, thm') =>
|
neuper@52101
|
190 |
let
|
walther@59852
|
191 |
val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule_Set.rep) rls)
|
walther@59852
|
192 |
((#erls o Rule_Set.rep) rls) put_asm thm' ct;
|
walther@59962
|
193 |
val _ = if pairopt <> NONE then () else raise ERROR ("rewrite_set_, rewrite_ \"" ^
|
walther@59875
|
194 |
ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
|
walther@60262
|
195 |
val _ = trace_in3 i "cal1. to" thy pairopt;
|
neuper@52101
|
196 |
in the pairopt end
|
neuper@52101
|
197 |
end
|
wneuper@59416
|
198 |
| Rule.Rls_ rls' =>
|
neuper@52101
|
199 |
(case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
|
neuper@52101
|
200 |
SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
|
wneuper@59381
|
201 |
| NONE => rew_once ruls asm ct apno thms)
|
walther@59867
|
202 |
| r => raise ERROR ("rew_once not appl. to \"" ^ Rule.to_string r ^ "\"");
|
walther@59852
|
203 |
val ruls = (#rules o Rule_Set.rep) rls;
|
walther@60262
|
204 |
val _ = trace_eq1 i "rls" rls thy ct
|
neuper@52101
|
205 |
val (ct', asm') = rew_once ruls [] ct Noap ruls;
|
walther@60017
|
206 |
in if ct = ct' then NONE else SOME (ct', distinct op = asm') end
|
walther@59865
|
207 |
(*-------------------------------------------------------------*)
|
walther@60262
|
208 |
and app_rev thy i rrls t = (* apply an Rrls; if not applicable proceed with subterms*)
|
neuper@52085
|
209 |
let (* check a (precond, pattern) of a rev-set; stops with 1st true *)
|
wneuper@59381
|
210 |
fun chk_prepat _ _ [] _ = true
|
neuper@52085
|
211 |
| chk_prepat thy erls prepat t =
|
neuper@52085
|
212 |
let
|
neuper@52085
|
213 |
fun chk (pres, pat) =
|
neuper@52085
|
214 |
(let
|
neuper@52085
|
215 |
val subst: Type.tyenv * Envir.tenv =
|
neuper@52085
|
216 |
Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
|
neuper@52085
|
217 |
in
|
neuper@52085
|
218 |
snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
|
walther@60262
|
219 |
end) handle STOP_REW_SUB => false
|
wneuper@59381
|
220 |
fun scan_ _ [] = false
|
wneuper@59398
|
221 |
| scan_ f (pp :: pps) =
|
neuper@52085
|
222 |
if f pp then true else scan_ f pps;
|
neuper@52085
|
223 |
in scan_ chk prepat end;
|
neuper@52085
|
224 |
(* apply the normal_form of a rev-set *)
|
walther@59850
|
225 |
fun app_rev' thy (Rule_Set.Rrls {erls, prepat, scr = Rule.Rfuns {normal_form, ...}, ...}) t =
|
wneuper@59381
|
226 |
if chk_prepat thy erls prepat t then normal_form t else NONE
|
walther@59867
|
227 |
| app_rev' _ r _ = raise ERROR ("app_rev' not appl. to \"" ^ Rule_Set.id r ^ "\"");
|
neuper@52085
|
228 |
val opt = app_rev' thy rrls t
|
neuper@52085
|
229 |
in
|
neuper@52085
|
230 |
case opt of
|
neuper@52085
|
231 |
SOME (t', asm) => (t', asm, true)
|
neuper@52085
|
232 |
| NONE => app_sub thy i rrls t
|
neuper@52085
|
233 |
end
|
walther@60262
|
234 |
and app_sub thy i rrls t = (* apply an Rrls to subterms*)
|
neuper@52085
|
235 |
case t of
|
neuper@52085
|
236 |
Const (s, T) => (Const(s, T), [], false)
|
neuper@52085
|
237 |
| Free (s, T) => (Free(s, T), [], false)
|
neuper@52085
|
238 |
| Var (n, T) => (Var(n, T), [], false)
|
neuper@52085
|
239 |
| Bound i => (Bound i, [], false)
|
neuper@52085
|
240 |
| Abs (s, T, body) =>
|
neuper@37906
|
241 |
let val (t', asm, rew) = app_rev thy i rrls body
|
neuper@37906
|
242 |
in (Abs(s, T, t'), asm, rew) end
|
neuper@52085
|
243 |
| t1 $ t2 =>
|
neuper@52085
|
244 |
let val (t2', asm2, rew2) = app_rev thy i rrls t2
|
neuper@52085
|
245 |
in
|
neuper@52085
|
246 |
if rew2 then (t1 $ t2', asm2, true)
|
neuper@52085
|
247 |
else
|
neuper@52085
|
248 |
let val (t1', asm1, rew1) = app_rev thy i rrls t1
|
neuper@52085
|
249 |
in if rew1 then (t1' $ t2, asm1, true)
|
neuper@52085
|
250 |
else (t1 $ t2, [], false)
|
neuper@52085
|
251 |
end
|
wneuper@59381
|
252 |
end;
|
neuper@37906
|
253 |
|
wneuper@59381
|
254 |
(* rewriting without argument [] for rew_ord; WN110603: shouldnt asm<>[] lead to false? *)
|
neuper@37906
|
255 |
fun eval_true thy terms rls = (snd o (eval__true thy 1 terms [])) rls;
|
neuper@37906
|
256 |
|
neuper@52101
|
257 |
(* rewriting without internal argument [] *)
|
neuper@52101
|
258 |
fun rewrite_ thy rew_ord erls bool thm term = rewrite__ thy 1 [] rew_ord erls bool thm term;
|
neuper@52101
|
259 |
fun rewrite_set_ thy bool rls term = rewrite__set_ thy 1 bool [] rls term;
|
neuper@37906
|
260 |
|
wneuper@59381
|
261 |
(* variants of rewrite; TODO del. put_asm *)
|
wneuper@59381
|
262 |
fun rewrite_inst_ thy rew_ord rls put_asm subst thm ct =
|
wneuper@59381
|
263 |
rewrite__ thy 1 subst rew_ord rls put_asm thm ct;
|
wneuper@59381
|
264 |
fun rewrite_set_inst_ thy put_asm subst rls ct = rewrite__set_ thy 1 put_asm subst rls ct;
|
neuper@37906
|
265 |
|
neuper@38025
|
266 |
(* given a list of equalities (lhs = rhs) and a term,
|
neuper@38025
|
267 |
replace all occurrences of lhs in the term with rhs;
|
neuper@38025
|
268 |
thus the order or equalities matters: put variables in lhs first. *)
|
neuper@38025
|
269 |
fun rewrite_terms_ thy ord erls equs t =
|
neuper@42359
|
270 |
let
|
neuper@42359
|
271 |
fun rew_ (t', asm') [] _ = (t', asm')
|
neuper@42359
|
272 |
| rew_ (t', asm') (rules as r::rs) t =
|
neuper@42359
|
273 |
let
|
wneuper@59390
|
274 |
val (t'', asm'', _(*lrd*), rew) = rew_sub thy 1 [] ord erls false [] (HOLogic.Trueprop $ r) t
|
neuper@42359
|
275 |
in
|
neuper@42359
|
276 |
if rew
|
neuper@42359
|
277 |
then rew_ (t'', asm' @ asm'') rules t''
|
neuper@42359
|
278 |
else rew_ (t', asm') rs t'
|
neuper@42359
|
279 |
end
|
walther@59861
|
280 |
val (t'', asm'') = rew_ (TermC.empty, []) equs t
|
walther@59861
|
281 |
in if t'' = TermC.empty then NONE else SOME (t'', asm'')
|
neuper@42359
|
282 |
end;
|
neuper@37906
|
283 |
|
wneuper@59381
|
284 |
(* search ct for adjacent numerals and calculate them by operator isa_fn *)
|
neuper@37906
|
285 |
fun calculate_ thy isa_fn ct =
|
wneuper@59389
|
286 |
let val ct = TermC.uminus_to_string ct
|
walther@59878
|
287 |
in case Eval.adhoc_thm thy isa_fn ct of
|
neuper@37906
|
288 |
NONE => NONE
|
wneuper@59381
|
289 |
| SOME (thmID, thm) =>
|
walther@59857
|
290 |
(let val rew = case rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false thm ct of
|
wneuper@59381
|
291 |
SOME (rew, _) => rew
|
wneuper@59381
|
292 |
| NONE => raise ERROR ""
|
wneuper@59381
|
293 |
in SOME (rew, (thmID, thm)) end)
|
walther@60263
|
294 |
handle NO_REWRITE => raise ERROR ("calculate_: " ^ thmID ^ " does not rewrite")
|
neuper@37906
|
295 |
end;
|
neuper@37906
|
296 |
|
walther@59718
|
297 |
fun eval_prog_expr thy srls t =
|
neuper@52139
|
298 |
let val rew = rewrite_set_ thy false srls t;
|
neuper@52139
|
299 |
in case rew of SOME (res,_) => res | NONE => t end;
|
neuper@37906
|
300 |
|
neuper@41928
|
301 |
fun eval_true_ _ _ (Const ("HOL.True",_)) = true
|
walther@59722
|
302 |
| eval_true_ thy rls t =
|
walther@59722
|
303 |
case rewrite_set_ thy false rls t of
|
neuper@41928
|
304 |
SOME (Const ("HOL.True",_),_) => true
|
neuper@37906
|
305 |
| _ => false;
|
neuper@37906
|
306 |
|
walther@60262
|
307 |
end
|