1 (* Title: Tools/code/code_simp.ML
2 Author: Florian Haftmann, TU Muenchen
4 Connecting the simplifier and the code generator.
9 val no_frees_conv: conv -> conv
10 val map_ss: (simpset -> simpset) -> theory -> theory
11 val current_conv: theory -> conv
12 val current_tac: theory -> int -> tactic
13 val current_value: theory -> term -> term
14 val make_conv: theory -> simpset option -> string list -> conv
15 val make_tac: theory -> simpset option -> string list -> int -> tactic
16 val setup: theory -> theory
19 structure Code_Simp : CODE_SIMP =
22 (* avoid free variables during conversion *)
24 fun no_frees_conv conv ct =
26 val frees = Thm.add_cterm_frees ct [];
27 fun apply_beta free thm = Thm.combination thm (Thm.reflexive free)
28 |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false)))
29 |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false));
32 |> fold_rev Thm.cabs frees
34 |> fold apply_beta frees
38 (* dedicated simpset *)
40 structure Simpset = Theory_Data (
43 fun extend ss = MetaSimplifier.inherit_context empty_ss ss;
47 val map_ss = Simpset.map;
49 fun simpset_default thy = Simplifier.global_context thy o the_default (Simpset.get thy);
52 (* build simpset and conversion from program *)
54 fun add_stmt (Code_Thingol.Fun (_, ((_, eqs), some_cong))) ss =
55 ss addsimps (map_filter (fst o snd)) eqs addcongs (the_list some_cong)
56 | add_stmt (Code_Thingol.Classinst (_, (_, (classparam_instances, _)))) ss =
57 ss addsimps (map (fst o snd) classparam_instances)
60 val add_program = Graph.fold (add_stmt o fst o snd)
62 fun rewrite_modulo thy some_ss program = Simplifier.full_rewrite
63 (add_program program (simpset_default thy some_ss));
65 fun conclude_tac thy some_ss = Simplifier.full_simp_tac (simpset_default thy some_ss);
68 (* evaluation with current code context *)
70 fun current_conv thy = no_frees_conv (Code_Thingol.eval_conv thy
71 (fn naming => fn program => fn t => fn deps => rewrite_modulo thy NONE program));
73 fun current_tac thy = CONVERSION (current_conv thy) THEN' conclude_tac thy NONE;
75 fun current_value thy = snd o Logic.dest_equals o Thm.prop_of o current_conv thy o Thm.cterm_of thy;
77 val setup = Method.setup (Binding.name "code_simp")
78 (Scan.succeed (SIMPLE_METHOD' o current_tac o ProofContext.theory_of))
79 "simplification with code equations"
80 #> Value.add_evaluator ("simp", current_value o ProofContext.theory_of);
83 (* evaluation with freezed code context *)
85 fun make_conv thy some_ss consts = no_frees_conv (Code_Preproc.pre_post_conv thy
86 ((rewrite_modulo thy some_ss o snd o snd o Code_Thingol.consts_program thy false) consts));
88 fun make_tac thy some_ss consts = CONVERSION (make_conv thy some_ss consts)
89 THEN' conclude_tac thy some_ss;