20427
|
1 |
(* ID: $Id$
|
|
2 |
Author: Florian Haftmann, TU Muenchen
|
|
3 |
*)
|
|
4 |
|
|
5 |
header {* A simple embedded term evaluation mechanism *}
|
|
6 |
|
|
7 |
theory CodeEval
|
|
8 |
imports CodeEmbed
|
|
9 |
begin
|
|
10 |
|
|
11 |
subsection {* The typ_of class *}
|
|
12 |
|
|
13 |
class typ_of =
|
20597
|
14 |
fixes typ_of :: "'a itself \<Rightarrow> typ"
|
20427
|
15 |
|
|
16 |
ML {*
|
|
17 |
structure TypOf =
|
|
18 |
struct
|
|
19 |
|
|
20 |
local
|
|
21 |
val thy = the_context ();
|
|
22 |
val const_typ_of = Sign.intern_const thy "typ_of";
|
20597
|
23 |
fun term_typ_of ty = Const (const_typ_of, Term.itselfT ty --> Embed.typ_typ);
|
20427
|
24 |
in
|
|
25 |
val class_typ_of = Sign.intern_class thy "typ_of";
|
20597
|
26 |
fun term_typ_of_type ty =
|
|
27 |
term_typ_of ty $ Logic.mk_type ty;
|
20427
|
28 |
fun mk_typ_of_def ty =
|
|
29 |
let
|
20597
|
30 |
val lhs = term_typ_of ty $ Free ("x", Term.itselfT ty)
|
|
31 |
val rhs = Embed.term_typ (fn v => term_typ_of_type (TFree v)) ty
|
20427
|
32 |
in Logic.mk_equals (lhs, rhs) end;
|
|
33 |
end;
|
|
34 |
|
|
35 |
end;
|
|
36 |
*}
|
|
37 |
|
|
38 |
setup {*
|
|
39 |
let
|
20835
|
40 |
fun mk arities _ thy =
|
22333
|
41 |
(maps (fn (tyco, asorts, _) => [(("", []), TypOf.mk_typ_of_def
|
20835
|
42 |
(Type (tyco, map TFree (Name.names Name.context "'a" asorts))))]) arities, thy);
|
20427
|
43 |
fun hook specs =
|
21924
|
44 |
DatatypeCodegen.prove_codetypes_arities (ClassPackage.intro_classes_tac [])
|
20427
|
45 |
(map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
|
20835
|
46 |
[TypOf.class_typ_of] mk ((K o K) I)
|
20427
|
47 |
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
|
|
48 |
*}
|
|
49 |
|
|
50 |
|
|
51 |
subsection {* term_of class *}
|
|
52 |
|
|
53 |
class term_of = typ_of +
|
20597
|
54 |
constrains typ_of :: "'a itself \<Rightarrow> typ"
|
20427
|
55 |
fixes term_of :: "'a \<Rightarrow> term"
|
|
56 |
|
|
57 |
ML {*
|
|
58 |
structure TermOf =
|
|
59 |
struct
|
|
60 |
|
|
61 |
local
|
|
62 |
val thy = the_context ();
|
|
63 |
val const_term_of = Sign.intern_const thy "term_of";
|
|
64 |
fun term_term_of ty = Const (const_term_of, ty --> Embed.typ_term);
|
|
65 |
in
|
|
66 |
val class_term_of = Sign.intern_class thy "term_of";
|
|
67 |
fun mk_terms_of_defs vs (tyco, cs) =
|
|
68 |
let
|
|
69 |
val dty = Type (tyco, map TFree vs);
|
|
70 |
fun mk_eq c =
|
|
71 |
let
|
|
72 |
val lhs : term = term_term_of dty $ c;
|
|
73 |
val rhs : term = Embed.term_term
|
|
74 |
(fn (v, ty) => term_term_of ty $ Free (v, ty))
|
20597
|
75 |
(Embed.term_typ (fn (v, sort) => TypOf.term_typ_of_type (TFree (v, sort)))) c
|
20427
|
76 |
in
|
|
77 |
HOLogic.mk_eq (lhs, rhs)
|
|
78 |
end;
|
|
79 |
in map mk_eq cs end;
|
|
80 |
fun mk_term_of t =
|
|
81 |
term_term_of (Term.fastype_of t) $ t;
|
|
82 |
end;
|
|
83 |
|
|
84 |
end;
|
|
85 |
*}
|
|
86 |
|
|
87 |
setup {*
|
|
88 |
let
|
20835
|
89 |
fun thy_note ((name, atts), thms) =
|
|
90 |
PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
|
|
91 |
fun thy_def ((name, atts), t) =
|
|
92 |
PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
|
|
93 |
fun mk arities css thy =
|
20427
|
94 |
let
|
22333
|
95 |
val (_, asorts, _) :: _ = arities;
|
|
96 |
val vs = Name.names Name.context "'a" asorts;
|
20835
|
97 |
val defs = map (TermOf.mk_terms_of_defs vs) css;
|
|
98 |
val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs;
|
|
99 |
in
|
|
100 |
thy
|
|
101 |
|> PrimrecPackage.gen_primrec thy_note thy_def "" defs'
|
|
102 |
|> snd
|
|
103 |
end;
|
20427
|
104 |
fun hook specs =
|
|
105 |
if (fst o hd) specs = (fst o dest_Type) Embed.typ_typ then I
|
|
106 |
else
|
21924
|
107 |
DatatypeCodegen.prove_codetypes_arities (ClassPackage.intro_classes_tac [])
|
20427
|
108 |
(map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
|
20835
|
109 |
[TermOf.class_term_of] ((K o K o pair) []) mk
|
20427
|
110 |
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
|
|
111 |
*}
|
|
112 |
|
22017
|
113 |
text {* Disable for characters and ml_strings *}
|
|
114 |
|
|
115 |
code_const "typ_of \<Colon> char itself \<Rightarrow> typ" and "term_of \<Colon> char \<Rightarrow> term"
|
|
116 |
(SML "!((_); raise Fail \"typ'_of'_char\")"
|
|
117 |
and "!((_); raise Fail \"term'_of'_char\")")
|
|
118 |
(OCaml "!((_); failwith \"typ'_of'_char\")"
|
|
119 |
and "!((_); failwith \"term'_of'_char\")")
|
|
120 |
(Haskell "error/ \"typ'_of'_char\""
|
|
121 |
and "error/ \"term'_of'_char\"")
|
|
122 |
|
|
123 |
code_const "term_of \<Colon> ml_string \<Rightarrow> term"
|
|
124 |
(SML "!((_); raise Fail \"term'_of'_ml'_string\")")
|
|
125 |
(OCaml "!((_); failwith \"term'_of'_ml'_string\")")
|
20427
|
126 |
|
|
127 |
subsection {* Evaluation infrastructure *}
|
|
128 |
|
|
129 |
ML {*
|
|
130 |
signature EVAL =
|
|
131 |
sig
|
20597
|
132 |
val eval_term: theory -> term -> term
|
20427
|
133 |
val term: string -> unit
|
20597
|
134 |
val eval_ref: term option ref
|
20427
|
135 |
end;
|
|
136 |
|
|
137 |
structure Eval : EVAL =
|
|
138 |
struct
|
|
139 |
|
20655
|
140 |
val eval_ref = ref (NONE : term option);
|
20427
|
141 |
|
20597
|
142 |
fun eval_term thy t =
|
|
143 |
CodegenPackage.eval_term
|
|
144 |
thy (("Eval.eval_ref", eval_ref), TermOf.mk_term_of t);
|
20427
|
145 |
|
|
146 |
fun term t =
|
|
147 |
let
|
|
148 |
val thy = the_context ();
|
20597
|
149 |
val t = eval_term thy (Sign.read_term thy t);
|
|
150 |
in (writeln o Sign.string_of_term thy) t end;
|
20427
|
151 |
|
|
152 |
end;
|
|
153 |
*}
|
|
154 |
|
|
155 |
|
|
156 |
subsection {* Small examples *}
|
|
157 |
|
22319
|
158 |
ML {* Eval.term "(Suc 2 + 1) * 4" *}
|
21117
|
159 |
ML {* Eval.term "(Suc 2 + Suc 0) * Suc 3" *}
|
20427
|
160 |
ML {* Eval.term "[]::nat list" *}
|
|
161 |
ML {* Eval.term "fst ([]::nat list, Suc 0) = []" *}
|
|
162 |
|
|
163 |
text {* a fancy datatype *}
|
|
164 |
|
|
165 |
datatype ('a, 'b) bair =
|
|
166 |
Bair "'a\<Colon>order" 'b
|
|
167 |
| Shift "('a, 'b) cair"
|
|
168 |
| Dummy unit
|
|
169 |
and ('a, 'b) cair =
|
|
170 |
Cair 'a 'b
|
|
171 |
|
|
172 |
ML {* Eval.term "Shift (Cair (4::nat) [Suc 0])" *}
|
|
173 |
|
22005
|
174 |
text {* also test evaluation oracle *}
|
|
175 |
|
|
176 |
lemma "True \<or> False" by eval
|
|
177 |
lemma "\<not> (Suc 0 = Suc 1)" by eval
|
|
178 |
|
20597
|
179 |
end |