author | haftmann |
Wed, 13 May 2009 18:41:54 +0200 | |
changeset 31139 | 0b615ac7eeaf |
parent 31048 | ac146fc38b51 |
child 31144 | bdc1504ad456 |
permissions | -rw-r--r-- |
28228 | 1 |
(* Title: HOL/Code_Eval.thy |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
*) |
|
4 |
||
5 |
header {* Term evaluation using the generic code generator *} |
|
6 |
||
7 |
theory Code_Eval |
|
28952
15a4b2cf8c34
made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents:
28661
diff
changeset
|
8 |
imports Plain Typerep |
28228 | 9 |
begin |
10 |
||
11 |
subsection {* Term representation *} |
|
12 |
||
13 |
subsubsection {* Terms and class @{text term_of} *} |
|
14 |
||
15 |
datatype "term" = dummy_term |
|
16 |
||
28661 | 17 |
definition Const :: "message_string \<Rightarrow> typerep \<Rightarrow> term" where |
28228 | 18 |
"Const _ _ = dummy_term" |
19 |
||
28661 | 20 |
definition App :: "term \<Rightarrow> term \<Rightarrow> term" where |
28228 | 21 |
"App _ _ = dummy_term" |
22 |
||
23 |
code_datatype Const App |
|
24 |
||
28335 | 25 |
class term_of = typerep + |
31031 | 26 |
fixes term_of :: "'a \<Rightarrow> term" |
28228 | 27 |
|
28 |
lemma term_of_anything: "term_of x \<equiv> t" |
|
29 |
by (rule eq_reflection) (cases "term_of x", cases t, simp) |
|
30 |
||
31 |
||
32 |
subsubsection {* @{text term_of} instances *} |
|
33 |
||
34 |
setup {* |
|
35 |
let |
|
31139 | 36 |
fun add_term_of tyco raw_vs thy = |
28228 | 37 |
let |
31139 | 38 |
val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs; |
39 |
val ty = Type (tyco, map TFree vs); |
|
28228 | 40 |
val lhs = Const (@{const_name term_of}, ty --> @{typ term}) |
41 |
$ Free ("x", ty); |
|
42 |
val rhs = @{term "undefined \<Colon> term"}; |
|
43 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)); |
|
28243 | 44 |
fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst |
45 |
o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv"; |
|
28228 | 46 |
in |
47 |
thy |
|
48 |
|> TheoryTarget.instantiation ([tyco], vs, @{sort term_of}) |
|
49 |
|> `(fn lthy => Syntax.check_term lthy eq) |
|
28965 | 50 |
|-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq))) |
28228 | 51 |
|> snd |
31139 | 52 |
|> Class.prove_instantiation_exit (K (Class.intro_classes_tac [])) |
28228 | 53 |
end; |
31139 | 54 |
fun ensure_term_of (tyco, (raw_vs, _)) thy = |
55 |
let |
|
56 |
val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of}) |
|
57 |
andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep}; |
|
58 |
in |
|
59 |
thy |
|
60 |
|> need_inst ? add_term_of tyco raw_vs |
|
61 |
end; |
|
28228 | 62 |
in |
31139 | 63 |
Code.type_interpretation ensure_term_of |
28228 | 64 |
end |
65 |
*} |
|
66 |
||
67 |
setup {* |
|
68 |
let |
|
31139 | 69 |
fun mk_term_of_eq thy ty vs tyco (c, tys) = |
28228 | 70 |
let |
71 |
val t = list_comb (Const (c, tys ---> ty), |
|
72 |
map Free (Name.names Name.context "a" tys)); |
|
31139 | 73 |
val (arg, rhs) = pairself (Thm.cterm_of thy o map_types Logic.unvarifyT o Logic.varify) |
74 |
(t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t) |
|
75 |
handle TYPE (msg, tys, ts) => (tracing msg; error "") |
|
76 |
val cty = Thm.ctyp_of thy ty; |
|
77 |
val _ = tracing (cat_lines [makestring arg, makestring rhs, makestring cty]) |
|
78 |
in |
|
79 |
@{thm term_of_anything} |
|
80 |
|> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs] |
|
81 |
|> Thm.varifyT |
|
28228 | 82 |
end; |
31139 | 83 |
fun add_term_of_code tyco raw_vs raw_cs thy = |
28228 | 84 |
let |
31139 | 85 |
val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs; |
86 |
val ty = Type (tyco, map TFree vs); |
|
87 |
val cs = (map o apsnd o map o map_atyps) |
|
88 |
(fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs; |
|
89 |
val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco); |
|
90 |
val eqs = map (mk_term_of_eq thy ty vs tyco) cs; |
|
91 |
in |
|
92 |
thy |
|
93 |
|> Code.del_eqns const |
|
94 |
|> fold Code.add_eqn eqs |
|
95 |
end; |
|
96 |
fun ensure_term_of_code (tyco, (raw_vs, cs)) thy = |
|
97 |
let |
|
98 |
val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of}; |
|
28228 | 99 |
in |
100 |
thy |
|
31139 | 101 |
|> has_inst ? add_term_of_code tyco raw_vs cs |
28228 | 102 |
end; |
103 |
in |
|
31139 | 104 |
Code.type_interpretation ensure_term_of_code |
28228 | 105 |
end |
106 |
*} |
|
107 |
||
108 |
||
109 |
subsubsection {* Code generator setup *} |
|
110 |
||
28562 | 111 |
lemmas [code del] = term.recs term.cases term.size |
112 |
lemma [code, code del]: "eq_class.eq (t1\<Colon>term) t2 \<longleftrightarrow> eq_class.eq t1 t2" .. |
|
28228 | 113 |
|
28562 | 114 |
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" .. |
115 |
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" .. |
|
116 |
lemma [code, code del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" .. |
|
30427 | 117 |
lemma [code, code del]: |
118 |
"(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. |
|
119 |
lemma [code, code del]: |
|
120 |
"(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. |
|
28228 | 121 |
|
28562 | 122 |
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]: "Code_Eval.term_of c = |
28243 | 123 |
(let (n, m) = nibble_pair_of_char c |
28335 | 124 |
in Code_Eval.App (Code_Eval.App (Code_Eval.Const (STR ''Pair'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char))) |
28243 | 125 |
(Code_Eval.term_of n)) (Code_Eval.term_of m))" |
126 |
by (subst term_of_anything) rule |
|
127 |
||
28228 | 128 |
code_type "term" |
31031 | 129 |
(Eval "Term.term") |
28228 | 130 |
|
131 |
code_const Const and App |
|
31031 | 132 |
(Eval "Term.Const/ (_, _)" and "Term.$/ (_, _)") |
28228 | 133 |
|
134 |
code_const "term_of \<Colon> message_string \<Rightarrow> term" |
|
31048
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31031
diff
changeset
|
135 |
(Eval "HOLogic.mk'_message'_string") |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31031
diff
changeset
|
136 |
|
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31031
diff
changeset
|
137 |
code_reserved Eval HOLogic |
28228 | 138 |
|
139 |
||
140 |
subsection {* Evaluation setup *} |
|
141 |
||
142 |
ML {* |
|
143 |
signature EVAL = |
|
144 |
sig |
|
145 |
val eval_ref: (unit -> term) option ref |
|
146 |
val eval_term: theory -> term -> term |
|
147 |
end; |
|
148 |
||
149 |
structure Eval : EVAL = |
|
150 |
struct |
|
151 |
||
152 |
val eval_ref = ref (NONE : (unit -> term) option); |
|
153 |
||
154 |
fun eval_term thy t = |
|
31139 | 155 |
Code_ML.eval NONE ("Eval.eval_ref", eval_ref) I thy (HOLogic.mk_term_of (fastype_of t) t) []; |
28228 | 156 |
|
157 |
end; |
|
158 |
*} |
|
159 |
||
160 |
setup {* |
|
161 |
Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of) |
|
162 |
*} |
|
163 |
||
28243 | 164 |
|
165 |
subsubsection {* Syntax *} |
|
166 |
||
167 |
print_translation {* |
|
168 |
let |
|
169 |
val term = Const ("<TERM>", dummyT); |
|
170 |
fun tr1' [_, _] = term; |
|
171 |
fun tr2' [] = term; |
|
172 |
in |
|
173 |
[(@{const_syntax Const}, tr1'), |
|
174 |
(@{const_syntax App}, tr1'), |
|
175 |
(@{const_syntax dummy_term}, tr2')] |
|
28228 | 176 |
end |
28243 | 177 |
*} |
178 |
||
179 |
hide const dummy_term |
|
180 |
hide (open) const Const App |
|
181 |
hide (open) const term_of |
|
182 |
||
183 |
end |