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 =
|
|
41 |
(maps (fn ((tyco, asorts), _) => [(("", []), TypOf.mk_typ_of_def
|
|
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
|
|
95 |
val vs = (Name.names Name.context "'a" o snd o fst o hd) arities;
|
20835
|
96 |
val defs = map (TermOf.mk_terms_of_defs vs) css;
|
|
97 |
val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs;
|
|
98 |
in
|
|
99 |
thy
|
|
100 |
|> PrimrecPackage.gen_primrec thy_note thy_def "" defs'
|
|
101 |
|> snd
|
|
102 |
end;
|
20427
|
103 |
fun hook specs =
|
|
104 |
if (fst o hd) specs = (fst o dest_Type) Embed.typ_typ then I
|
|
105 |
else
|
21924
|
106 |
DatatypeCodegen.prove_codetypes_arities (ClassPackage.intro_classes_tac [])
|
20427
|
107 |
(map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
|
20835
|
108 |
[TermOf.class_term_of] ((K o K o pair) []) mk
|
20427
|
109 |
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
|
|
110 |
*}
|
|
111 |
|
|
112 |
|
|
113 |
subsection {* Evaluation infrastructure *}
|
|
114 |
|
|
115 |
ML {*
|
|
116 |
signature EVAL =
|
|
117 |
sig
|
20597
|
118 |
val eval_term: theory -> term -> term
|
20427
|
119 |
val term: string -> unit
|
20597
|
120 |
val eval_ref: term option ref
|
20427
|
121 |
end;
|
|
122 |
|
|
123 |
structure Eval : EVAL =
|
|
124 |
struct
|
|
125 |
|
20655
|
126 |
val eval_ref = ref (NONE : term option);
|
20427
|
127 |
|
20597
|
128 |
fun eval_term thy t =
|
|
129 |
CodegenPackage.eval_term
|
|
130 |
thy (("Eval.eval_ref", eval_ref), TermOf.mk_term_of t);
|
20427
|
131 |
|
|
132 |
fun term t =
|
|
133 |
let
|
|
134 |
val thy = the_context ();
|
20597
|
135 |
val t = eval_term thy (Sign.read_term thy t);
|
|
136 |
in (writeln o Sign.string_of_term thy) t end;
|
20427
|
137 |
|
|
138 |
end;
|
|
139 |
*}
|
|
140 |
|
|
141 |
|
|
142 |
subsection {* Small examples *}
|
|
143 |
|
21117
|
144 |
ML {* Eval.term "(Suc 2 + Suc 0) * Suc 3" *}
|
20427
|
145 |
ML {* Eval.term "[]::nat list" *}
|
|
146 |
ML {* Eval.term "fst ([]::nat list, Suc 0) = []" *}
|
|
147 |
|
|
148 |
text {* a fancy datatype *}
|
|
149 |
|
|
150 |
datatype ('a, 'b) bair =
|
|
151 |
Bair "'a\<Colon>order" 'b
|
|
152 |
| Shift "('a, 'b) cair"
|
|
153 |
| Dummy unit
|
|
154 |
and ('a, 'b) cair =
|
|
155 |
Cair 'a 'b
|
|
156 |
|
|
157 |
ML {* Eval.term "Shift (Cair (4::nat) [Suc 0])" *}
|
|
158 |
|
20597
|
159 |
end |