author  haftmann 
Thu, 14 May 2009 08:22:07 +0200  
changeset 31144  bdc1504ad456 
parent 31139  0b615ac7eeaf 
child 31178  27afaaa6547a 
permissions  rwrr 
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 
val cty = Thm.ctyp_of thy ty; 

76 
in 

77 
@{thm term_of_anything} 

78 
> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs] 

79 
> Thm.varifyT 

28228  80 
end; 
31139  81 
fun add_term_of_code tyco raw_vs raw_cs thy = 
28228  82 
let 
31139  83 
val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs; 
84 
val ty = Type (tyco, map TFree vs); 

85 
val cs = (map o apsnd o map o map_atyps) 

86 
(fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs; 

87 
val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco); 

88 
val eqs = map (mk_term_of_eq thy ty vs tyco) cs; 

89 
in 

90 
thy 

91 
> Code.del_eqns const 

92 
> fold Code.add_eqn eqs 

93 
end; 

94 
fun ensure_term_of_code (tyco, (raw_vs, cs)) thy = 

95 
let 

96 
val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of}; 

28228  97 
in 
98 
thy 

31139  99 
> has_inst ? add_term_of_code tyco raw_vs cs 
28228  100 
end; 
101 
in 

31139  102 
Code.type_interpretation ensure_term_of_code 
28228  103 
end 
104 
*} 

105 

106 

107 
subsubsection {* Code generator setup *} 

108 

28562  109 
lemmas [code del] = term.recs term.cases term.size 
110 
lemma [code, code del]: "eq_class.eq (t1\<Colon>term) t2 \<longleftrightarrow> eq_class.eq t1 t2" .. 

28228  111 

28562  112 
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" .. 
113 
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" .. 

114 
lemma [code, code del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" .. 

30427  115 
lemma [code, code del]: 
116 
"(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. 

117 
lemma [code, code del]: 

118 
"(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. 

28228  119 

28562  120 
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]: "Code_Eval.term_of c = 
28243  121 
(let (n, m) = nibble_pair_of_char c 
28335  122 
in Code_Eval.App (Code_Eval.App (Code_Eval.Const (STR ''Pair'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char))) 
28243  123 
(Code_Eval.term_of n)) (Code_Eval.term_of m))" 
124 
by (subst term_of_anything) rule 

125 

28228  126 
code_type "term" 
31031  127 
(Eval "Term.term") 
28228  128 

129 
code_const Const and App 

31031  130 
(Eval "Term.Const/ (_, _)" and "Term.$/ (_, _)") 
28228  131 

132 
code_const "term_of \<Colon> message_string \<Rightarrow> term" 

31048
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31031
diff
changeset

133 
(Eval "HOLogic.mk'_message'_string") 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31031
diff
changeset

134 

ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31031
diff
changeset

135 
code_reserved Eval HOLogic 
28228  136 

137 

138 
subsection {* Evaluation setup *} 

139 

140 
ML {* 

141 
signature EVAL = 

142 
sig 

143 
val eval_ref: (unit > term) option ref 

144 
val eval_term: theory > term > term 

145 
end; 

146 

147 
structure Eval : EVAL = 

148 
struct 

149 

150 
val eval_ref = ref (NONE : (unit > term) option); 

151 

152 
fun eval_term thy t = 

31139  153 
Code_ML.eval NONE ("Eval.eval_ref", eval_ref) I thy (HOLogic.mk_term_of (fastype_of t) t) []; 
28228  154 

155 
end; 

156 
*} 

157 

158 
setup {* 

159 
Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of) 

160 
*} 

161 

28243  162 

163 
subsubsection {* Syntax *} 

164 

165 
print_translation {* 

166 
let 

167 
val term = Const ("<TERM>", dummyT); 

168 
fun tr1' [_, _] = term; 

169 
fun tr2' [] = term; 

170 
in 

171 
[(@{const_syntax Const}, tr1'), 

172 
(@{const_syntax App}, tr1'), 

173 
(@{const_syntax dummy_term}, tr2')] 

28228  174 
end 
28243  175 
*} 
176 

177 
hide const dummy_term 

178 
hide (open) const Const App 

179 
hide (open) const term_of 

180 

181 
end 