27 val dynamic_eval_conv: theory |
27 val dynamic_eval_conv: theory |
28 -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv |
28 -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv |
29 val dynamic_eval_value: theory -> ((term -> term) -> 'a -> 'a) |
29 val dynamic_eval_value: theory -> ((term -> term) -> 'a -> 'a) |
30 -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a |
30 -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a |
31 val static_eval_conv: theory -> string list |
31 val static_eval_conv: theory -> string list |
32 -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv |
32 -> (code_algebra -> code_graph -> theory -> (string * sort) list -> term -> conv) -> conv |
|
33 val static_eval_value: theory -> ((term -> term) -> 'a -> 'a) -> string list |
|
34 -> (code_algebra -> code_graph -> theory -> (string * sort) list -> term -> 'a) -> term -> 'a |
33 |
35 |
34 val setup: theory -> theory |
36 val setup: theory -> theory |
35 end |
37 end |
36 |
38 |
37 structure Code_Preproc : CODE_PREPROC = |
39 structure Code_Preproc : CODE_PREPROC = |
136 #> (map o apfst) (rewrite_eqn pre) |
138 #> (map o apfst) (rewrite_eqn pre) |
137 end; |
139 end; |
138 |
140 |
139 fun preprocess_conv thy ct = |
141 fun preprocess_conv thy ct = |
140 let |
142 let |
|
143 val ctxt = ProofContext.init_global thy; |
|
144 val _ = (Sign.no_frees ctxt o map_types (K dummyT) o Sign.no_vars ctxt) (Thm.term_of ct); |
141 val pre = (Simplifier.global_context thy o #pre o the_thmproc) thy; |
145 val pre = (Simplifier.global_context thy o #pre o the_thmproc) thy; |
142 in |
146 in |
143 ct |
147 ct |
144 |> Simplifier.rewrite pre |
148 |> Simplifier.rewrite pre |
145 |> trans_conv_rule (AxClass.unoverload_conv thy) |
149 |> trans_conv_rule (AxClass.unoverload_conv thy) |
146 end; |
150 end; |
|
151 |
|
152 fun preprocess_term thy = term_of_conv thy (preprocess_conv thy); |
147 |
153 |
148 fun postprocess_conv thy ct = |
154 fun postprocess_conv thy ct = |
149 let |
155 let |
150 val post = (Simplifier.global_context thy o #post o the_thmproc) thy; |
156 val post = (Simplifier.global_context thy o #post o the_thmproc) thy; |
151 in |
157 in |
425 fun obtain ignore_cache thy consts ts = apsnd snd |
431 fun obtain ignore_cache thy consts ts = apsnd snd |
426 (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy) (extend_arities_eqngr thy consts ts)); |
432 (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy) (extend_arities_eqngr thy consts ts)); |
427 |
433 |
428 fun dest_cterm ct = let val t = Thm.term_of ct in (Term.add_tfrees t [], t) end; |
434 fun dest_cterm ct = let val t = Thm.term_of ct in (Term.add_tfrees t [], t) end; |
429 |
435 |
430 fun dynamic_eval thy cterm_of conclude_evaluation evaluator proto_ct = |
436 fun dynamic_eval_conv thy conv ct = |
431 let |
437 let |
432 val ctxt = Syntax.init_pretty_global thy; |
438 val thm1 = preprocess_conv thy ct; |
433 val ct = cterm_of proto_ct; |
439 val ct' = Thm.rhs_of thm1; |
434 val _ = (Sign.no_frees ctxt o map_types (K dummyT) o Sign.no_vars ctxt) (Thm.term_of ct); |
|
435 val thm = preprocess_conv thy ct; |
|
436 val ct' = Thm.rhs_of thm; |
|
437 val (vs', t') = dest_cterm ct'; |
440 val (vs', t') = dest_cterm ct'; |
438 val consts = fold_aterms |
441 val consts = fold_aterms |
439 (fn Const (c, _) => insert (op =) c | _ => I) t' []; |
442 (fn Const (c, _) => insert (op =) c | _ => I) t' []; |
440 val (algebra', eqngr') = obtain false thy consts [t']; |
443 val (algebra', eqngr') = obtain false thy consts [t']; |
441 in conclude_evaluation (evaluator algebra' eqngr' vs' t' ct') thm end; |
444 val thm2 = conv algebra' eqngr' vs' t' ct'; |
442 |
445 val thm3 = postprocess_conv thy (Thm.rhs_of thm2); |
443 fun dynamic_eval_conv thy = |
446 in |
444 let |
447 Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ => |
445 fun conclude_evaluation thm2 thm1 = |
448 error ("could not construct evaluation proof:\n" |
446 let |
449 ^ (cat_lines o map (Display.string_of_thm_global thy)) [thm1, thm2, thm3]) |
447 val thm3 = postprocess_conv thy (Thm.rhs_of thm2); |
450 end; |
448 in |
451 |
449 Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ => |
452 fun dynamic_eval_value thy postproc evaluator t = |
450 error ("could not construct evaluation proof:\n" |
453 let |
451 ^ (cat_lines o map (Display.string_of_thm_global thy)) [thm1, thm2, thm3]) |
454 val t' = preprocess_term thy t; |
452 end; |
455 val vs' = Term.add_tfrees t' []; |
453 in dynamic_eval thy I conclude_evaluation end; |
456 val consts = fold_aterms |
454 |
457 (fn Const (c, _) => insert (op =) c | _ => I) t' []; |
455 fun dynamic_eval_value thy postproc evaluator = dynamic_eval thy (Thm.cterm_of thy) |
458 val (algebra', eqngr') = obtain false thy consts [t']; |
456 (K o postproc (postprocess_term thy)) (K oooo evaluator); |
459 val result = evaluator algebra' eqngr' vs' t'; |
|
460 in |
|
461 evaluator algebra' eqngr' vs' t' |
|
462 |> postproc (postprocess_term thy) |
|
463 end; |
457 |
464 |
458 fun static_eval_conv thy consts conv = |
465 fun static_eval_conv thy consts conv = |
459 let |
466 let |
460 val (algebra, eqngr) = obtain true thy consts []; |
467 val (algebra, eqngr) = obtain true thy consts []; |
|
468 val conv' = conv algebra eqngr; |
461 in |
469 in |
462 Conv.tap_thy (fn thy => (preprocess_conv thy) |
470 Conv.tap_thy (fn thy => (preprocess_conv thy) |
463 then_conv (fn ct => uncurry (conv algebra eqngr) (dest_cterm ct) ct) |
471 then_conv (fn ct => uncurry (conv' thy) (dest_cterm ct) ct) |
464 then_conv (postprocess_conv thy)) |
472 then_conv (postprocess_conv thy)) |
|
473 end; |
|
474 |
|
475 fun static_eval_value thy postproc consts evaluator = |
|
476 let |
|
477 val (algebra, eqngr) = obtain true thy consts []; |
|
478 val evaluator' = evaluator algebra eqngr; |
|
479 in fn t => |
|
480 t |
|
481 |> preprocess_term thy |
|
482 |> (fn t => evaluator' thy (Term.add_tfrees t []) t) |
|
483 |> postproc (postprocess_term thy) |
465 end; |
484 end; |
466 |
485 |
467 |
486 |
468 (** setup **) |
487 (** setup **) |
469 |
488 |