author | haftmann |
Thu, 06 Dec 2007 15:10:09 +0100 | |
changeset 25557 | ea6b11021e79 |
parent 25536 | 01753a944433 |
child 25559 | f14305fb698c |
permissions | -rw-r--r-- |
22525 | 1 |
(* Title: HOL/Library/Eval.thy |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
*) |
|
5 |
||
6 |
header {* A simple term evaluation mechanism *} |
|
7 |
||
8 |
theory Eval |
|
24280 | 9 |
imports Main Pure_term |
22525 | 10 |
begin |
11 |
||
22527 | 12 |
subsection {* @{text typ_of} class *} |
22525 | 13 |
|
23062 | 14 |
class typ_of = |
15 |
fixes typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ" |
|
22525 | 16 |
|
17 |
ML {* |
|
18 |
structure TypOf = |
|
19 |
struct |
|
20 |
||
25536 | 21 |
fun mk ty = |
22525 | 22 |
Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ}) |
23 |
$ Logic.mk_type ty; |
|
24 |
||
25536 | 25 |
end |
22525 | 26 |
*} |
27 |
||
28 |
setup {* |
|
29 |
let |
|
25536 | 30 |
fun define_typ_of ty lthy = |
31 |
let |
|
32 |
val lhs = Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ}) |
|
33 |
$ Free ("T", Term.itselfT ty); |
|
34 |
val rhs = Pure_term.mk_typ (fn v => TypOf.mk (TFree v)) ty; |
|
35 |
val eq = Class.prep_spec lthy (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))) |
|
36 |
in lthy |> Specification.definition (NONE, (("", []), eq)) end; |
|
37 |
fun interpretator tyco thy = |
|
38 |
let |
|
39 |
val sorts = replicate (Sign.arity_number thy tyco) @{sort typ_of}; |
|
40 |
val ty = Type (tyco, map TFree (Name.names Name.context "'a" sorts)); |
|
41 |
in |
|
42 |
thy |
|
43 |
|> Instance.instantiate ([tyco], sorts, @{sort typ_of}) |
|
44 |
(define_typ_of ty) ((K o K) (Class.intro_classes_tac [])) |
|
45 |
end; |
|
46 |
in TypedefPackage.interpretation interpretator end |
|
22525 | 47 |
*} |
48 |
||
25536 | 49 |
instantiation "prop" :: typ_of |
50 |
begin |
|
51 |
||
52 |
definition |
|
53 |
"typ_of (T\<Colon>prop itself) = STR ''prop'' {\<struct>} []" |
|
54 |
||
55 |
instance .. |
|
56 |
||
57 |
end |
|
58 |
||
59 |
instantiation itself :: (typ_of) typ_of |
|
60 |
begin |
|
61 |
||
62 |
definition |
|
63 |
"typ_of (T\<Colon>'a itself itself) = STR ''itself'' {\<struct>} [typ_of TYPE('a\<Colon>typ_of)]" |
|
64 |
||
65 |
instance .. |
|
66 |
||
67 |
end |
|
68 |
||
69 |
instantiation set :: (typ_of) typ_of |
|
70 |
begin |
|
71 |
||
72 |
definition |
|
73 |
"typ_of (T\<Colon>'a set itself) = STR ''set'' {\<struct>} [typ_of TYPE('a\<Colon>typ_of)]" |
|
74 |
||
75 |
instance .. |
|
76 |
||
77 |
end |
|
78 |
||
22525 | 79 |
|
22527 | 80 |
subsection {* @{text term_of} class *} |
22525 | 81 |
|
82 |
class term_of = typ_of + |
|
23062 | 83 |
constrains typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ" |
22525 | 84 |
fixes term_of :: "'a \<Rightarrow> term" |
85 |
||
86 |
ML {* |
|
87 |
structure TermOf = |
|
88 |
struct |
|
89 |
||
90 |
local |
|
91 |
fun term_term_of ty = |
|
92 |
Const (@{const_name term_of}, ty --> @{typ term}); |
|
93 |
in |
|
94 |
val class_term_of = Sign.intern_class @{theory} "term_of"; |
|
95 |
fun mk_terms_of_defs vs (tyco, cs) = |
|
96 |
let |
|
97 |
val dty = Type (tyco, map TFree vs); |
|
98 |
fun mk_eq c = |
|
99 |
let |
|
100 |
val lhs : term = term_term_of dty $ c; |
|
101 |
val rhs : term = Pure_term.mk_term |
|
102 |
(fn (v, ty) => term_term_of ty $ Free (v, ty)) |
|
25536 | 103 |
(Pure_term.mk_typ (fn (v, sort) => TypOf.mk (TFree (v, sort)))) c |
22525 | 104 |
in |
105 |
HOLogic.mk_eq (lhs, rhs) |
|
106 |
end; |
|
107 |
in map mk_eq cs end; |
|
108 |
fun mk_term_of t = |
|
109 |
term_term_of (Term.fastype_of t) $ t; |
|
110 |
end; |
|
111 |
||
112 |
end; |
|
113 |
*} |
|
114 |
||
115 |
setup {* |
|
116 |
let |
|
117 |
fun thy_note ((name, atts), thms) = |
|
118 |
PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms)); |
|
119 |
fun thy_def ((name, atts), t) = |
|
120 |
PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm)); |
|
25536 | 121 |
fun prep_dtyp thy vs tyco = |
122 |
let |
|
123 |
val (_, cs) = DatatypePackage.the_datatype_spec thy tyco; |
|
124 |
val prep_typ = map_atyps (fn TFree (v, sort) => |
|
125 |
TFree (v, (the o AList.lookup (op =) vs) v)); |
|
126 |
fun prep_c (c, tys) = list_comb (Const (c, tys ---> Type (tyco, map TFree vs)), |
|
127 |
map Free (Name.names Name.context "a" tys)); |
|
128 |
in (tyco, map (prep_c o (apsnd o map) prep_typ) cs) end; |
|
129 |
fun prep thy tycos = |
|
22525 | 130 |
let |
25536 | 131 |
val inter_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of}; |
132 |
val tyco = hd tycos; |
|
133 |
val (vs_proto, _) = DatatypePackage.the_datatype_spec thy tyco; |
|
134 |
val all_typs = maps (maps snd o snd o DatatypePackage.the_datatype_spec thy) tycos; |
|
135 |
fun add_tycos (Type (tyco, tys)) = insert (op =) tyco #> |
|
136 |
fold add_tycos tys |
|
137 |
| add_tycos _ = I; |
|
138 |
val dep_tycos = [] |> fold add_tycos all_typs |> subtract (op =) tycos; |
|
139 |
val sorts = map (inter_sort o snd) vs_proto; |
|
140 |
val vs = map fst vs_proto ~~ sorts; |
|
141 |
val css = map (prep_dtyp thy vs) tycos; |
|
22525 | 142 |
val defs = map (TermOf.mk_terms_of_defs vs) css; |
143 |
val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs; |
|
25536 | 144 |
in if forall (fn tyco => can (Sign.arity_sorts thy tyco) @{sort term_of}) dep_tycos |
145 |
andalso not (tycos = [@{type_name typ}]) |
|
146 |
then SOME (sorts, defs') |
|
147 |
else NONE |
|
22525 | 148 |
end; |
25536 | 149 |
fun interpretator tycos thy = case prep thy tycos |
150 |
of SOME (sorts, defs) => |
|
151 |
thy |
|
152 |
|> Instance.instantiate (tycos, sorts, @{sort term_of}) |
|
153 |
(pair ()) ((K o K) (Class.intro_classes_tac [])) |
|
25557 | 154 |
|> OldPrimrecPackage.gen_primrec thy_note thy_def "" defs |
25536 | 155 |
|> snd |
156 |
| NONE => thy; |
|
157 |
in DatatypePackage.interpretation interpretator end |
|
22525 | 158 |
*} |
159 |
||
23062 | 160 |
abbreviation |
161 |
intT :: "typ" |
|
162 |
where |
|
163 |
"intT \<equiv> STR ''IntDef.int'' {\<struct>} []" |
|
164 |
||
23133 | 165 |
abbreviation |
166 |
bitT :: "typ" |
|
167 |
where |
|
168 |
"bitT \<equiv> STR ''Numeral.bit'' {\<struct>} []" |
|
169 |
||
23062 | 170 |
function |
171 |
mk_int :: "int \<Rightarrow> term" |
|
172 |
where |
|
173 |
"mk_int k = (if k = 0 then STR ''Numeral.Pls'' \<Colon>\<subseteq> intT |
|
174 |
else if k = -1 then STR ''Numeral.Min'' \<Colon>\<subseteq> intT |
|
175 |
else let (l, m) = divAlg (k, 2) |
|
23133 | 176 |
in STR ''Numeral.Bit'' \<Colon>\<subseteq> intT \<rightarrow> bitT \<rightarrow> intT \<bullet> mk_int l \<bullet> |
177 |
(if m = 0 then STR ''Numeral.bit.B0'' \<Colon>\<subseteq> bitT else STR ''Numeral.bit.B1'' \<Colon>\<subseteq> bitT))" |
|
23062 | 178 |
by pat_completeness auto |
179 |
termination by (relation "measure (nat o abs)") (auto simp add: divAlg_mod_div) |
|
180 |
||
181 |
instance int :: term_of |
|
23133 | 182 |
"term_of k \<equiv> STR ''Numeral.number_class.number_of'' \<Colon>\<subseteq> intT \<rightarrow> intT \<bullet> mk_int k" .. |
23062 | 183 |
|
184 |
||
24994
c385c4eabb3b
consolidated naming conventions for code generator theories
haftmann
parents:
24920
diff
changeset
|
185 |
text {* Adaption for @{typ message_string}s *} |
22525 | 186 |
|
24994
c385c4eabb3b
consolidated naming conventions for code generator theories
haftmann
parents:
24920
diff
changeset
|
187 |
lemmas [code func, code func del] = term_of_message_string_def |
22525 | 188 |
|
189 |
||
190 |
subsection {* Evaluation infrastructure *} |
|
191 |
||
192 |
ML {* |
|
193 |
signature EVAL = |
|
194 |
sig |
|
24587 | 195 |
val eval_ref: (unit -> term) option ref |
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
196 |
val eval_term: theory -> term -> term |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
197 |
val evaluate: Proof.context -> term -> unit |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
198 |
val evaluate': string -> Proof.context -> term -> unit |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
199 |
val evaluate_cmd: string option -> Toplevel.state -> unit |
22525 | 200 |
end; |
201 |
||
24280 | 202 |
structure Eval = |
22525 | 203 |
struct |
204 |
||
24587 | 205 |
val eval_ref = ref (NONE : (unit -> term) option); |
22525 | 206 |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
207 |
fun eval_invoke thy code ((_, ty), t) deps _ = |
24916 | 208 |
CodeTarget.eval_invoke thy ("Eval.eval_ref", eval_ref) code (t, ty) []; |
24280 | 209 |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
210 |
fun eval_term thy = |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
211 |
TermOf.mk_term_of |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
212 |
#> CodePackage.eval_term thy (eval_invoke thy) |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
213 |
#> Code.postprocess_term thy; |
24280 | 214 |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
215 |
val evaluators = [ |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
216 |
("code", eval_term), |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
217 |
("SML", Codegen.eval_term), |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
218 |
("normal_form", Nbe.norm_term) |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
219 |
]; |
22525 | 220 |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
221 |
fun gen_evaluate evaluators ctxt t = |
24280 | 222 |
let |
223 |
val thy = ProofContext.theory_of ctxt; |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
224 |
val (evls, evl) = split_last evaluators; |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
225 |
val t' = case get_first (fn f => try (f thy) t) evls |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
226 |
of SOME t' => t' |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
227 |
| NONE => evl thy t; |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
228 |
val ty' = Term.type_of t'; |
24920 | 229 |
val p = Pretty.block [Pretty.quote (Syntax.pretty_term ctxt t'), |
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
230 |
Pretty.fbrk, Pretty.str "::", Pretty.brk 1, |
24920 | 231 |
Pretty.quote (Syntax.pretty_typ ctxt ty')]; |
24280 | 232 |
in Pretty.writeln p end; |
233 |
||
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
234 |
val evaluate = gen_evaluate (map snd evaluators); |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
235 |
|
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
236 |
fun evaluate' name = gen_evaluate |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
237 |
[(the o AList.lookup (op =) evaluators) name]; |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
238 |
|
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
239 |
fun evaluate_cmd some_name raw_t state = |
22525 | 240 |
let |
22804 | 241 |
val ctxt = Toplevel.context_of state; |
24508
c8b82fec6447
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24423
diff
changeset
|
242 |
val t = Syntax.read_term ctxt raw_t; |
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
243 |
in case some_name |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
244 |
of NONE => evaluate ctxt t |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
245 |
| SOME name => evaluate' name ctxt t |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
246 |
end; |
22525 | 247 |
|
248 |
end; |
|
249 |
*} |
|
250 |
||
22804 | 251 |
ML {* |
252 |
OuterSyntax.improper_command "value" "read, evaluate and print term" OuterKeyword.diag |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
253 |
(Scan.option (OuterParse.$$$ "(" |-- OuterParse.name --| OuterParse.$$$ ")") |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
254 |
-- OuterParse.term |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
255 |
>> (fn (some_name, t) => Toplevel.no_timing o Toplevel.keep |
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24659
diff
changeset
|
256 |
(Eval.evaluate_cmd some_name t))); |
22804 | 257 |
*} |
258 |
||
22665 | 259 |
end |