author | krauss |
Wed, 13 Sep 2006 12:05:50 +0200 | |
changeset 20523 | 36a59e5d0039 |
parent 20453 | 855f07fabd76 |
permissions | -rw-r--r-- |
20427 | 1 |
(* ID: $Id$ |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
*) |
|
4 |
||
5 |
header {* Operational equality for code generation *} |
|
6 |
||
7 |
theory CodeOperationalEquality |
|
8 |
imports Main |
|
9 |
begin |
|
10 |
||
11 |
section {* Operational equality for code generation *} |
|
12 |
||
13 |
subsection {* eq class *} |
|
14 |
||
15 |
class eq = |
|
16 |
fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" |
|
17 |
||
18 |
defs |
|
19 |
eq_def: "eq x y == (x = y)" |
|
20 |
||
21 |
ML {* |
|
22 |
local |
|
23 |
val thy = the_context (); |
|
24 |
val const_eq = Sign.intern_const thy "eq"; |
|
25 |
val type_bool = Type (Sign.intern_type thy "bool", []); |
|
26 |
in |
|
27 |
val eq_def_sym = Thm.symmetric (thm "eq_def"); |
|
28 |
val class_eq = Sign.intern_class thy "eq"; |
|
29 |
end |
|
30 |
*} |
|
31 |
||
32 |
||
33 |
subsection {* preprocessor *} |
|
34 |
||
35 |
ML {* |
|
36 |
fun constrain_op_eq thy thms = |
|
37 |
let |
|
20435 | 38 |
fun add_eq (Const ("op =", ty)) = |
39 |
fold (insert (eq_fst (op = : indexname * indexname -> bool))) |
|
40 |
(Term.add_tvarsT ty []) |
|
41 |
| add_eq _ = |
|
42 |
I |
|
43 |
val eqs = fold (fold_aterms add_eq o Thm.prop_of) thms []; |
|
20427 | 44 |
val instT = map (fn (v_i, sort) => |
45 |
(Thm.ctyp_of thy (TVar (v_i, sort)), |
|
46 |
Thm.ctyp_of thy (TVar (v_i, Sorts.inter_sort (Sign.classes_of thy) (sort, [class_eq]))))) eqs; |
|
47 |
in |
|
48 |
thms |
|
49 |
|> map (Thm.instantiate (instT, [])) |
|
50 |
end; |
|
51 |
*} |
|
52 |
||
53 |
||
54 |
subsection {* codetypes hook *} |
|
55 |
||
56 |
setup {* |
|
57 |
let |
|
58 |
fun add_eq_instance specs = |
|
59 |
DatatypeCodegen.prove_codetypes_arities |
|
60 |
(K (ClassPackage.intro_classes_tac [])) |
|
61 |
(map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs) |
|
62 |
[class_eq] ((K o K o K) []); |
|
63 |
(* fun add_eq_thms dtco thy = |
|
64 |
let |
|
65 |
val thms = |
|
66 |
DatatypeCodegen.get_eq thy dtco |
|
67 |
|> maps ((#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy) |
|
68 |
|> constrain_op_eq thy |
|
69 |
|> map (Tactic.rewrite_rule [eq_def_sym]) |
|
70 |
in fold CodegenTheorems.add_fun thms thy end; *) |
|
71 |
fun hook dtcos = |
|
72 |
add_eq_instance dtcos (* #> fold add_eq_thms dtcos *); |
|
73 |
in |
|
74 |
DatatypeCodegen.add_codetypes_hook_bootstrap hook |
|
75 |
end |
|
76 |
*} |
|
77 |
||
78 |
||
79 |
subsection {* extractor *} |
|
80 |
||
81 |
setup {* |
|
82 |
let |
|
20435 | 83 |
val lift_not_thm = thm "HOL.Eq_FalseI"; |
84 |
val lift_thm = thm "HOL.eq_reflection"; |
|
85 |
val eq_def_thm = Thm.symmetric (thm "eq_def"); |
|
20427 | 86 |
fun get_eq_thms thy tyco = case DatatypePackage.get_datatype thy tyco |
87 |
of SOME _ => DatatypeCodegen.get_eq thy tyco |
|
88 |
| NONE => case TypedefCodegen.get_triv_typedef thy tyco |
|
89 |
of SOME (_ ,(_, thm)) => [thm] |
|
90 |
| NONE => []; |
|
20435 | 91 |
fun get_eq_thms_eq thy ("CodeOperationalEquality.eq", ty) = (case strip_type ty |
20427 | 92 |
of (Type (tyco, _) :: _, _) => |
93 |
get_eq_thms thy tyco |
|
20435 | 94 |
|> map (perhaps (try (fn thm => lift_not_thm OF [thm]))) |
95 |
|> map (perhaps (try (fn thm => lift_thm OF [thm]))) |
|
96 |
(*|> tap (writeln o cat_lines o map string_of_thm)*) |
|
97 |
|> constrain_op_eq thy |
|
98 |
(*|> tap (writeln o cat_lines o map string_of_thm)*) |
|
99 |
|> map (Tactic.rewrite_rule [eq_def_thm]) |
|
100 |
(*|> tap (writeln o cat_lines o map string_of_thm)*) |
|
20427 | 101 |
| _ => []) |
102 |
| get_eq_thms_eq _ _ = []; |
|
103 |
in |
|
104 |
CodegenTheorems.add_fun_extr get_eq_thms_eq |
|
105 |
end |
|
106 |
*} |
|
107 |
||
108 |
||
109 |
subsection {* integers *} |
|
110 |
||
111 |
definition |
|
112 |
"eq_integer (k\<Colon>int) l = (k = l)" |
|
113 |
||
114 |
instance int :: eq .. |
|
115 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20453
diff
changeset
|
116 |
lemma [code func]: |
20427 | 117 |
"eq k l = eq_integer k l" |
118 |
unfolding eq_integer_def eq_def .. |
|
119 |
||
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
120 |
code_const eq_integer |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
121 |
(SML infixl 6 "=") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
122 |
(Haskell infixl 4 "==") |
20427 | 123 |
|
124 |
||
125 |
subsection {* codegenerator setup *} |
|
126 |
||
127 |
setup {* |
|
128 |
CodegenTheorems.add_preproc constrain_op_eq |
|
129 |
*} |
|
130 |
||
131 |
declare eq_def [symmetric, code inline] |
|
132 |
||
133 |
||
134 |
subsection {* haskell setup *} |
|
135 |
||
136 |
code_class eq |
|
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
137 |
(Haskell "Eq" where eq \<equiv> "(==)") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
138 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
139 |
code_instance eq :: bool and eq :: unit and eq :: * |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
140 |
and eq :: option and eq :: list and eq :: char and eq :: int |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
141 |
(Haskell - and - and - and - and - and - and -) |
20427 | 142 |
|
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
143 |
code_const "eq \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
144 |
(Haskell infixl 4 "==") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
145 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
146 |
code_const "eq \<Colon> unit \<Rightarrow> unit \<Rightarrow> bool" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
147 |
(Haskell infixl 4 "==") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
148 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
149 |
code_const "eq \<Colon> 'a\<Colon>eq \<times> 'b\<Colon>eq \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
150 |
(Haskell infixl 4 "==") |
20427 | 151 |
|
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
152 |
code_const "eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
153 |
(Haskell infixl 4 "==") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
154 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
155 |
code_const "eq \<Colon> 'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> bool" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
156 |
(Haskell infixl 4 "==") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
157 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
158 |
code_const "eq \<Colon> char \<Rightarrow> char \<Rightarrow> bool" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
159 |
(Haskell infixl 4 "==") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
160 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
161 |
code_const "eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
162 |
(Haskell infixl 4 "==") |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
163 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
164 |
code_const "eq" |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20435
diff
changeset
|
165 |
(Haskell infixl 4 "==") |
20427 | 166 |
|
167 |
end |