author | haftmann |
Fri, 06 Sep 2013 20:55:14 +0200 | |
changeset 53437 | b098d53152d9 |
parent 53147 | 8e8941fea278 |
child 54895 | 515630483010 |
permissions | -rw-r--r-- |
37744 | 1 |
(* Title: Tools/Code/code_simp.ML |
37442 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
4 |
Connecting the simplifier and the code generator. |
|
5 |
*) |
|
6 |
||
7 |
signature CODE_SIMP = |
|
8 |
sig |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
48072
diff
changeset
|
9 |
val map_ss: (Proof.context -> Proof.context) -> theory -> theory |
41247 | 10 |
val dynamic_conv: theory -> conv |
41188 | 11 |
val dynamic_tac: theory -> int -> tactic |
41184 | 12 |
val dynamic_value: theory -> term -> term |
13 |
val static_conv: theory -> simpset option -> string list -> conv |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
14 |
val static_tac: theory -> simpset option -> string list -> Proof.context -> int -> tactic |
37442 | 15 |
val setup: theory -> theory |
53147 | 16 |
val trace: bool Config.T |
37442 | 17 |
end; |
18 |
||
19 |
structure Code_Simp : CODE_SIMP = |
|
20 |
struct |
|
21 |
||
22 |
(* dedicated simpset *) |
|
23 |
||
38759
37a9092de102
simplification/standardization of some theory data;
wenzelm
parents:
38671
diff
changeset
|
24 |
structure Simpset = Theory_Data |
37a9092de102
simplification/standardization of some theory data;
wenzelm
parents:
38671
diff
changeset
|
25 |
( |
37442 | 26 |
type T = simpset; |
27 |
val empty = empty_ss; |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
48072
diff
changeset
|
28 |
val extend = I; |
37442 | 29 |
val merge = merge_ss; |
30 |
); |
|
31 |
||
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
48072
diff
changeset
|
32 |
fun map_ss f thy = Simpset.map (simpset_map (Proof_Context.init_global thy) f) thy; |
37442 | 33 |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
34 |
fun simpset_default thy = the_default (Simpset.get thy); |
37461 | 35 |
|
37442 | 36 |
|
53147 | 37 |
(* diagonstic *) |
38 |
||
39 |
val trace = Attrib.setup_config_bool @{binding "code_simp_trace"} (K false); |
|
40 |
||
41 |
fun set_trace ctxt = |
|
42 |
let |
|
43 |
val global = Config.get ctxt trace; |
|
44 |
in |
|
45 |
ctxt |> Config.map Simplifier.simp_trace (fn b => b orelse global) |
|
46 |
end; |
|
47 |
||
48 |
||
37442 | 49 |
(* build simpset and conversion from program *) |
50 |
||
51 |
fun add_stmt (Code_Thingol.Fun (_, ((_, eqs), some_cong))) ss = |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
52 |
ss |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
53 |
|> fold Simplifier.add_simp ((map_filter (fst o snd)) eqs) |
45620
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents:
43619
diff
changeset
|
54 |
|> fold Simplifier.add_cong (the_list some_cong) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
47576
diff
changeset
|
55 |
| add_stmt (Code_Thingol.Classinst { inst_params, ... }) ss = |
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
56 |
ss |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
57 |
|> fold Simplifier.add_simp (map (fst o snd) inst_params) |
37442 | 58 |
| add_stmt _ ss = ss; |
59 |
||
37839 | 60 |
val add_program = Graph.fold (add_stmt o fst o snd); |
37442 | 61 |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
62 |
fun simpset_program thy some_ss program = |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
63 |
simpset_map (Proof_Context.init_global thy) (add_program program) (simpset_default thy some_ss); |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
64 |
|
53147 | 65 |
fun lift_ss_conv f ss ct = |
53437 | 66 |
f (Simplifier.global_context (theory_of_cterm ct) ss |> set_trace) ct; |
37461 | 67 |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
68 |
fun rewrite_modulo thy some_ss program = |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
69 |
lift_ss_conv Simplifier.full_rewrite (simpset_program thy some_ss program); |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
70 |
|
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
71 |
fun conclude_tac thy some_ss = |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
72 |
let |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
73 |
val ss = simpset_default thy some_ss; |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
74 |
in fn ctxt => Simplifier.full_simp_tac (Simplifier.put_simpset ss ctxt) end; |
37442 | 75 |
|
76 |
||
39486 | 77 |
(* evaluation with dynamic code context *) |
37442 | 78 |
|
41247 | 79 |
fun dynamic_conv thy = Code_Thingol.dynamic_conv thy |
47576 | 80 |
(fn _ => fn program => fn _ => fn _ => rewrite_modulo thy NONE program); |
37442 | 81 |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
82 |
fun dynamic_tac thy = CONVERSION (dynamic_conv thy) THEN' conclude_tac thy NONE (Proof_Context.init_global thy); |
37442 | 83 |
|
41247 | 84 |
fun dynamic_value thy = snd o Logic.dest_equals o Thm.prop_of o dynamic_conv thy o Thm.cterm_of thy; |
37444 | 85 |
|
43619
3803869014aa
proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents:
42361
diff
changeset
|
86 |
val setup = |
3803869014aa
proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents:
42361
diff
changeset
|
87 |
Method.setup @{binding code_simp} |
3803869014aa
proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents:
42361
diff
changeset
|
88 |
(Scan.succeed (SIMPLE_METHOD' o (CHANGED_PROP oo dynamic_tac o Proof_Context.theory_of))) |
3803869014aa
proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents:
42361
diff
changeset
|
89 |
"simplification with code equations" |
42361 | 90 |
#> Value.add_evaluator ("simp", dynamic_value o Proof_Context.theory_of); |
37442 | 91 |
|
92 |
||
39486 | 93 |
(* evaluation with static code context *) |
37442 | 94 |
|
41184 | 95 |
fun static_conv thy some_ss consts = |
96 |
Code_Thingol.static_conv_simple thy consts |
|
41346 | 97 |
(fn program => fn _ => fn _ => rewrite_modulo thy some_ss program); |
37442 | 98 |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
99 |
fun static_tac thy some_ss consts = |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
100 |
let |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
101 |
val conv = static_conv thy some_ss consts; |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
102 |
val tac = conclude_tac thy some_ss; |
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
103 |
in fn ctxt => CONVERSION conv THEN' tac ctxt end; |
37442 | 104 |
|
105 |
end; |