author | haftmann |
Fri, 09 May 2014 08:13:26 +0200 | |
changeset 56920 | d651b944c67e |
parent 56808 | d4a790cb47e8 |
child 56924 | 2f94c9a50f06 |
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 |
55757 | 10 |
val dynamic_conv: Proof.context -> conv |
11 |
val dynamic_tac: Proof.context -> int -> tactic |
|
12 |
val dynamic_value: Proof.context -> term -> term |
|
13 |
val static_conv: Proof.context -> simpset option -> string list -> Proof.context -> conv |
|
14 |
val static_tac: Proof.context -> 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 |
|
55757 | 34 |
fun simpset_default ctxt some_ss = the_default (Simpset.get (Proof_Context.theory_of ctxt)) some_ss; |
54929
f1ded3cea58d
proper context for simplifier invocations in code generation stack
haftmann
parents:
54928
diff
changeset
|
35 |
|
37442 | 36 |
|
54928 | 37 |
(* diagnostic *) |
53147 | 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 |
||
55757 | 49 |
(* build simpset context and conversion from program *) |
37442 | 50 |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54929
diff
changeset
|
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 |
||
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
54929
diff
changeset
|
60 |
val add_program = Code_Symbol.Graph.fold (add_stmt o fst o snd); |
37442 | 61 |
|
55757 | 62 |
fun simpset_program ctxt some_ss program = |
63 |
simpset_map ctxt (add_program program) (simpset_default ctxt some_ss); |
|
37461 | 64 |
|
55757 | 65 |
fun rewrite_modulo ctxt some_ss program = |
66 |
let |
|
67 |
val ss = simpset_program ctxt some_ss program; |
|
68 |
in fn ctxt => Simplifier.full_rewrite (ctxt |> put_simpset ss |> set_trace) end; |
|
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
69 |
|
55757 | 70 |
fun conclude_tac ctxt some_ss = |
71 |
let |
|
72 |
val ss = simpset_default ctxt some_ss |
|
73 |
in fn ctxt => Simplifier.full_simp_tac (ctxt |> put_simpset ss) end; |
|
37442 | 74 |
|
75 |
||
39486 | 76 |
(* evaluation with dynamic code context *) |
37442 | 77 |
|
55757 | 78 |
fun dynamic_conv ctxt = Code_Thingol.dynamic_conv ctxt |
56920
d651b944c67e
normalizing of type variables before evaluation with explicit resubstitution function: make nbe work with funny type variables like \<AA>;
haftmann
parents:
56808
diff
changeset
|
79 |
(fn program => fn _ => fn _ => fn _ => rewrite_modulo ctxt NONE program ctxt); |
37442 | 80 |
|
55757 | 81 |
fun dynamic_tac ctxt = CONVERSION (dynamic_conv ctxt) |
82 |
THEN' conclude_tac ctxt NONE ctxt; |
|
37442 | 83 |
|
55757 | 84 |
fun dynamic_value ctxt = |
85 |
snd o Logic.dest_equals o Thm.prop_of o dynamic_conv ctxt o Thm.cterm_of (Proof_Context.theory_of ctxt); |
|
37444 | 86 |
|
43619
3803869014aa
proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents:
42361
diff
changeset
|
87 |
val setup = |
3803869014aa
proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents:
42361
diff
changeset
|
88 |
Method.setup @{binding code_simp} |
55757 | 89 |
(Scan.succeed (SIMPLE_METHOD' o (CHANGED_PROP oo dynamic_tac))) |
43619
3803869014aa
proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents:
42361
diff
changeset
|
90 |
"simplification with code equations" |
55757 | 91 |
#> Value.add_evaluator ("simp", dynamic_value); |
37442 | 92 |
|
93 |
||
39486 | 94 |
(* evaluation with static code context *) |
37442 | 95 |
|
55757 | 96 |
fun static_conv ctxt some_ss consts = |
97 |
Code_Thingol.static_conv_simple ctxt consts |
|
98 |
(fn program => let val conv' = rewrite_modulo ctxt some_ss program |
|
56920
d651b944c67e
normalizing of type variables before evaluation with explicit resubstitution function: make nbe work with funny type variables like \<AA>;
haftmann
parents:
56808
diff
changeset
|
99 |
in fn ctxt' => fn _ => conv' ctxt' end); |
37442 | 100 |
|
55757 | 101 |
fun static_tac ctxt some_ss consts = |
52736
317663b422bb
more correct context for dynamic invocations of static code conversions etc.
haftmann
parents:
51717
diff
changeset
|
102 |
let |
55757 | 103 |
val conv = static_conv ctxt some_ss consts; |
104 |
val tac = conclude_tac ctxt some_ss; |
|
105 |
in fn ctxt' => CONVERSION (conv ctxt') THEN' (tac ctxt') end; |
|
37442 | 106 |
|
107 |
end; |