39564
|
1 |
(* Title: HOL/Tools/code_evaluation.ML
|
|
2 |
Author: Florian Haftmann, TU Muenchen
|
|
3 |
|
|
4 |
Evaluation and reconstruction of terms in ML.
|
|
5 |
*)
|
|
6 |
|
|
7 |
signature CODE_EVALUATION =
|
|
8 |
sig
|
|
9 |
val dynamic_value_strict: theory -> term -> term
|
|
10 |
val put_term: (unit -> term) -> Proof.context -> Proof.context
|
|
11 |
val tracing: string -> 'a -> 'a
|
|
12 |
val setup: theory -> theory
|
|
13 |
end;
|
|
14 |
|
|
15 |
structure Code_Evaluation : CODE_EVALUATION =
|
|
16 |
struct
|
|
17 |
|
|
18 |
(** term_of instances **)
|
|
19 |
|
|
20 |
(* formal definition *)
|
|
21 |
|
|
22 |
fun add_term_of tyco raw_vs thy =
|
|
23 |
let
|
|
24 |
val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
|
|
25 |
val ty = Type (tyco, map TFree vs);
|
|
26 |
val lhs = Const (@{const_name term_of}, ty --> @{typ term})
|
|
27 |
$ Free ("x", ty);
|
|
28 |
val rhs = @{term "undefined :: term"};
|
|
29 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
|
|
30 |
fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
|
|
31 |
o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
|
|
32 |
in
|
|
33 |
thy
|
|
34 |
|> Class.instantiation ([tyco], vs, @{sort term_of})
|
|
35 |
|> `(fn lthy => Syntax.check_term lthy eq)
|
|
36 |
|-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
|
|
37 |
|> snd
|
|
38 |
|> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
|
|
39 |
end;
|
|
40 |
|
|
41 |
fun ensure_term_of (tyco, (raw_vs, _)) thy =
|
|
42 |
let
|
|
43 |
val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of})
|
|
44 |
andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
|
|
45 |
in if need_inst then add_term_of tyco raw_vs thy else thy end;
|
|
46 |
|
|
47 |
|
|
48 |
(* code equations for datatypes *)
|
|
49 |
|
|
50 |
fun mk_term_of_eq thy ty vs tyco (c, tys) =
|
|
51 |
let
|
|
52 |
val t = list_comb (Const (c, tys ---> ty),
|
|
53 |
map Free (Name.names Name.context "a" tys));
|
|
54 |
val (arg, rhs) =
|
|
55 |
pairself (Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
|
|
56 |
(t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t)
|
|
57 |
val cty = Thm.ctyp_of thy ty;
|
|
58 |
in
|
|
59 |
@{thm term_of_anything}
|
|
60 |
|> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
|
|
61 |
|> Thm.varifyT_global
|
|
62 |
end;
|
|
63 |
|
|
64 |
fun add_term_of_code tyco raw_vs raw_cs thy =
|
|
65 |
let
|
|
66 |
val algebra = Sign.classes_of thy;
|
|
67 |
val vs = map (fn (v, sort) =>
|
|
68 |
(v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
|
|
69 |
val ty = Type (tyco, map TFree vs);
|
|
70 |
val cs = (map o apsnd o map o map_atyps)
|
|
71 |
(fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
|
|
72 |
val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
|
|
73 |
val eqs = map (mk_term_of_eq thy ty vs tyco) cs;
|
|
74 |
in
|
|
75 |
thy
|
|
76 |
|> Code.del_eqns const
|
|
77 |
|> fold Code.add_eqn eqs
|
|
78 |
end;
|
|
79 |
|
|
80 |
fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
|
|
81 |
let
|
|
82 |
val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
|
|
83 |
in if has_inst then add_term_of_code tyco raw_vs cs thy else thy end;
|
|
84 |
|
|
85 |
|
|
86 |
(* code equations for abstypes *)
|
|
87 |
|
|
88 |
fun mk_abs_term_of_eq thy ty vs tyco abs ty_rep proj =
|
|
89 |
let
|
|
90 |
val arg = Var (("x", 0), ty);
|
|
91 |
val rhs = Abs ("y", @{typ term}, HOLogic.reflect_term (Const (abs, ty_rep --> ty) $ Bound 0)) $
|
|
92 |
(HOLogic.mk_term_of ty_rep (Const (proj, ty --> ty_rep) $ arg))
|
|
93 |
|> Thm.cterm_of thy;
|
|
94 |
val cty = Thm.ctyp_of thy ty;
|
|
95 |
in
|
|
96 |
@{thm term_of_anything}
|
|
97 |
|> Drule.instantiate' [SOME cty] [SOME (Thm.cterm_of thy arg), SOME rhs]
|
|
98 |
|> Thm.varifyT_global
|
|
99 |
end;
|
|
100 |
|
|
101 |
fun add_abs_term_of_code tyco raw_vs abs raw_ty_rep proj thy =
|
|
102 |
let
|
|
103 |
val algebra = Sign.classes_of thy;
|
|
104 |
val vs = map (fn (v, sort) =>
|
|
105 |
(v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
|
|
106 |
val ty = Type (tyco, map TFree vs);
|
|
107 |
val ty_rep = map_atyps
|
|
108 |
(fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_ty_rep;
|
|
109 |
val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
|
|
110 |
val eq = mk_abs_term_of_eq thy ty vs tyco abs ty_rep proj;
|
|
111 |
in
|
|
112 |
thy
|
|
113 |
|> Code.del_eqns const
|
|
114 |
|> Code.add_eqn eq
|
|
115 |
end;
|
|
116 |
|
|
117 |
fun ensure_abs_term_of_code (tyco, (raw_vs, ((abs, ty), (proj, _)))) thy =
|
|
118 |
let
|
|
119 |
val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
|
|
120 |
in if has_inst then add_abs_term_of_code tyco raw_vs abs ty proj thy else thy end;
|
|
121 |
|
|
122 |
|
|
123 |
(** termifying syntax **)
|
|
124 |
|
|
125 |
fun map_default f xs =
|
|
126 |
let val ys = map f xs
|
|
127 |
in if exists is_some ys
|
|
128 |
then SOME (map2 the_default xs ys)
|
|
129 |
else NONE
|
|
130 |
end;
|
|
131 |
|
|
132 |
fun subst_termify_app (Const (@{const_name termify}, T), [t]) =
|
|
133 |
if not (Term.has_abs t)
|
|
134 |
then if fold_aterms (fn Const _ => I | _ => K false) t true
|
|
135 |
then SOME (HOLogic.reflect_term t)
|
|
136 |
else error "Cannot termify expression containing variables"
|
|
137 |
else error "Cannot termify expression containing abstraction"
|
|
138 |
| subst_termify_app (t, ts) = case map_default subst_termify ts
|
|
139 |
of SOME ts' => SOME (list_comb (t, ts'))
|
|
140 |
| NONE => NONE
|
|
141 |
and subst_termify (Abs (v, T, t)) = (case subst_termify t
|
|
142 |
of SOME t' => SOME (Abs (v, T, t'))
|
|
143 |
| NONE => NONE)
|
|
144 |
| subst_termify t = subst_termify_app (strip_comb t)
|
|
145 |
|
|
146 |
fun check_termify ts ctxt = map_default subst_termify ts
|
|
147 |
|> Option.map (rpair ctxt)
|
|
148 |
|
|
149 |
|
|
150 |
(** evaluation **)
|
|
151 |
|
|
152 |
structure Evaluation = Proof_Data (
|
|
153 |
type T = unit -> term
|
|
154 |
fun init _ () = error "Evaluation"
|
|
155 |
);
|
|
156 |
val put_term = Evaluation.put;
|
|
157 |
|
|
158 |
fun tracing s x = (Output.tracing s; x);
|
|
159 |
|
|
160 |
fun dynamic_value_strict thy t = Code_Runtime.dynamic_value_strict (Evaluation.get, put_term, "Code_Evaluation.put_term")
|
|
161 |
thy NONE I (HOLogic.mk_term_of (fastype_of t) t) [];
|
|
162 |
|
|
163 |
|
|
164 |
(** setup **)
|
|
165 |
|
|
166 |
val setup =
|
|
167 |
Code.datatype_interpretation ensure_term_of
|
|
168 |
#> Code.abstype_interpretation ensure_term_of
|
|
169 |
#> Code.datatype_interpretation ensure_term_of_code
|
|
170 |
#> Code.abstype_interpretation ensure_abs_term_of_code
|
|
171 |
#> Context.theory_map (Syntax.add_term_check 0 "termify" check_termify)
|
|
172 |
#> Value.add_evaluator ("code", dynamic_value_strict o ProofContext.theory_of);
|
|
173 |
|
|
174 |
end;
|