author | haftmann |
Mon, 17 Dec 2007 22:40:14 +0100 | |
changeset 25684 | 2b3cce7d22b8 |
parent 25597 | 34860182b250 |
child 26112 | ac2ce7242eae |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Pure/Isar/code_unit.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
5 |
Basic notions of code generation. Auxiliary. |
24219 | 6 |
*) |
7 |
||
8 |
signature CODE_UNIT = |
|
9 |
sig |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
10 |
(*generic non-sense*) |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
11 |
val bad_thm: string -> 'a |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
12 |
val error_thm: (thm -> thm) -> thm -> thm |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
13 |
val warning_thm: (thm -> thm) -> thm -> thm option |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
14 |
val try_thm: (thm -> thm) -> thm -> thm option |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
15 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
16 |
(*typ instantiations*) |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
17 |
val typ_sort_inst: Sorts.algebra -> typ * sort |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
18 |
-> sort Vartab.table -> sort Vartab.table |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
19 |
val inst_thm: sort Vartab.table -> thm -> thm |
25540 | 20 |
val constrain_thm: sort -> thm -> thm |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
21 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
22 |
(*constants*) |
24219 | 23 |
val string_of_typ: theory -> typ -> string |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
24 |
val string_of_const: theory -> string -> string |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
25 |
val no_args: theory -> string -> int |
24219 | 26 |
val read_bare_const: theory -> string -> string * typ |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
27 |
val read_const: theory -> string -> string |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
28 |
val read_const_exprs: theory -> (string list -> string list) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
29 |
-> string list -> bool * string list |
24219 | 30 |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
31 |
(*constructor sets*) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
32 |
val constrset_of_consts: theory -> (string * typ) list |
24219 | 33 |
-> string * ((string * sort) list * (string * typ list) list) |
34 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
35 |
(*defining equations*) |
24219 | 36 |
val assert_rew: thm -> thm |
37 |
val mk_rew: thm -> thm |
|
38 |
val mk_func: thm -> thm |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
39 |
val head_func: thm -> string * typ |
24219 | 40 |
val expand_eta: int -> thm -> thm |
41 |
val rewrite_func: thm list -> thm -> thm |
|
42 |
val norm_args: thm list -> thm list |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
43 |
val norm_varnames: (string -> string) -> (string -> string) -> thm list -> thm list |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
44 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
45 |
(*case certificates*) |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
46 |
val case_cert: thm -> string * (int * string list) |
24219 | 47 |
end; |
48 |
||
49 |
structure CodeUnit: CODE_UNIT = |
|
50 |
struct |
|
51 |
||
52 |
||
53 |
(* auxiliary *) |
|
54 |
||
55 |
exception BAD_THM of string; |
|
56 |
fun bad_thm msg = raise BAD_THM msg; |
|
57 |
fun error_thm f thm = f thm handle BAD_THM msg => error msg; |
|
58 |
fun warning_thm f thm = SOME (f thm) handle BAD_THM msg |
|
59 |
=> (warning ("code generator: " ^ msg); NONE); |
|
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24423
diff
changeset
|
60 |
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE; |
24219 | 61 |
|
62 |
fun string_of_typ thy = setmp show_sorts true (Sign.string_of_typ thy); |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25540
diff
changeset
|
63 |
fun string_of_const thy c = case AxClass.inst_of_param thy c |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
64 |
of SOME (c, tyco) => Sign.extern_const thy c ^ " " ^ enclose "[" "]" (Sign.extern_type thy tyco) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
65 |
| NONE => Sign.extern_const thy c; |
24219 | 66 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
67 |
fun no_args thy = length o fst o strip_type o Sign.the_const_type thy; |
24219 | 68 |
|
69 |
(* reading constants as terms and wildcards pattern *) |
|
70 |
||
71 |
fun read_bare_const thy raw_t = |
|
72 |
let |
|
24707 | 73 |
val t = Syntax.read_term_global thy raw_t; |
24219 | 74 |
in case try dest_Const t |
75 |
of SOME c_ty => c_ty |
|
76 |
| NONE => error ("Not a constant: " ^ Sign.string_of_term thy t) |
|
77 |
end; |
|
78 |
||
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25540
diff
changeset
|
79 |
fun read_const thy = AxClass.unoverload_const thy o read_bare_const thy; |
24219 | 80 |
|
81 |
local |
|
82 |
||
83 |
fun consts_of thy some_thyname = |
|
84 |
let |
|
85 |
val this_thy = Option.map theory some_thyname |> the_default thy; |
|
86 |
val cs = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I) |
|
87 |
((snd o #constants o Consts.dest o #consts o Sign.rep_sg) this_thy) []; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
88 |
fun belongs_here thyname c = |
24219 | 89 |
not (exists (fn thy' => Sign.declared_const thy' c) (Theory.parents_of this_thy)) |
90 |
in case some_thyname |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
91 |
of NONE => cs |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
92 |
| SOME thyname => filter (belongs_here thyname) cs |
24219 | 93 |
end; |
94 |
||
95 |
fun read_const_expr thy "*" = ([], consts_of thy NONE) |
|
96 |
| read_const_expr thy s = if String.isSuffix ".*" s |
|
97 |
then ([], consts_of thy (SOME (unsuffix ".*" s))) |
|
98 |
else ([read_const thy s], []); |
|
99 |
||
100 |
in |
|
101 |
||
102 |
fun read_const_exprs thy select exprs = |
|
103 |
case (pairself flat o split_list o map (read_const_expr thy)) exprs |
|
104 |
of (consts, []) => (false, consts) |
|
105 |
| (consts, consts') => (true, consts @ select consts'); |
|
106 |
||
107 |
end; (*local*) |
|
108 |
||
109 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
110 |
(* constructor sets *) |
24219 | 111 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
112 |
fun constrset_of_consts thy cs = |
24219 | 113 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
114 |
fun no_constr (c, ty) = error ("Not a datatype constructor: " ^ string_of_const thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
115 |
^ " :: " ^ string_of_typ thy ty); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
116 |
fun last_typ c_ty ty = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
117 |
let |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
118 |
val frees = typ_tfrees ty; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
119 |
val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) ty |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
120 |
handle TYPE _ => no_constr c_ty |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
121 |
val _ = if has_duplicates (eq_fst (op =)) vs then no_constr c_ty else (); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
122 |
val _ = if length frees <> length vs then no_constr c_ty else (); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
123 |
in (tyco, vs) end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
124 |
fun ty_sorts (c, ty) = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
125 |
let |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
126 |
val ty_decl = (Logic.unvarifyT o Sign.the_const_type thy) c; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
127 |
val (tyco, vs_decl) = last_typ (c, ty) ty_decl; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
128 |
val (_, vs) = last_typ (c, ty) ty; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
129 |
in ((tyco, map snd vs), (c, (map fst vs, ty_decl))) end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
130 |
fun add ((tyco', sorts'), c) ((tyco, sorts), cs) = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
131 |
let |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
132 |
val _ = if tyco' <> tyco |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
133 |
then error "Different type constructors in constructor set" |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
134 |
else (); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
135 |
val sorts'' = map2 (curry (Sorts.inter_sort (Sign.classes_of thy))) sorts' sorts |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
136 |
in ((tyco, sorts), c :: cs) end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
137 |
fun inst vs' (c, (vs, ty)) = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
138 |
let |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
139 |
val the_v = the o AList.lookup (op =) (vs ~~ vs'); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
140 |
val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
141 |
in (c, (fst o strip_type) ty') end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
142 |
val c' :: cs' = map ty_sorts cs; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
143 |
val ((tyco, sorts), cs'') = fold add cs' (apsnd single c'); |
24848 | 144 |
val vs = Name.names Name.context Name.aT sorts; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
145 |
val cs''' = map (inst vs) cs''; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
146 |
in (tyco, (vs, cs''')) end; |
24219 | 147 |
|
148 |
||
149 |
(* dictionary values *) |
|
150 |
||
151 |
fun typ_sort_inst algebra = |
|
152 |
let |
|
153 |
val inters = Sorts.inter_sort algebra; |
|
154 |
fun match _ [] = I |
|
155 |
| match (TVar (v, S)) S' = Vartab.map_default (v, []) (fn S'' => inters (S, inters (S', S''))) |
|
156 |
| match (Type (a, Ts)) S = |
|
157 |
fold2 match Ts (Sorts.mg_domain algebra a S) |
|
158 |
in uncurry match end; |
|
159 |
||
160 |
||
161 |
(* making rewrite theorems *) |
|
162 |
||
163 |
fun assert_rew thm = |
|
164 |
let |
|
165 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm |
|
166 |
handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm) |
|
167 |
| THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm); |
|
168 |
fun vars_of t = fold_aterms |
|
169 |
(fn Var (v, _) => insert (op =) v |
|
170 |
| Free _ => bad_thm ("Illegal free variable in rewrite theorem\n" |
|
171 |
^ Display.string_of_thm thm) |
|
172 |
| _ => I) t []; |
|
173 |
fun tvars_of t = fold_term_types |
|
174 |
(fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v |
|
175 |
| TFree _ => bad_thm |
|
176 |
("Illegal free type variable in rewrite theorem\n" ^ Display.string_of_thm thm))) t []; |
|
177 |
val lhs_vs = vars_of lhs; |
|
178 |
val rhs_vs = vars_of rhs; |
|
179 |
val lhs_tvs = tvars_of lhs; |
|
180 |
val rhs_tvs = tvars_of lhs; |
|
181 |
val _ = if null (subtract (op =) lhs_vs rhs_vs) |
|
182 |
then () |
|
183 |
else bad_thm ("Free variables on right hand side of rewrite theorem\n" |
|
184 |
^ Display.string_of_thm thm); |
|
185 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs) |
|
186 |
then () |
|
187 |
else bad_thm ("Free type variables on right hand side of rewrite theorem\n" |
|
188 |
^ Display.string_of_thm thm) |
|
189 |
in thm end; |
|
190 |
||
191 |
fun mk_rew thm = |
|
192 |
let |
|
193 |
val thy = Thm.theory_of_thm thm; |
|
194 |
val ctxt = ProofContext.init thy; |
|
195 |
in |
|
196 |
thm |
|
197 |
|> LocalDefs.meta_rewrite_rule ctxt |
|
198 |
|> assert_rew |
|
199 |
end; |
|
200 |
||
201 |
||
202 |
(* making defining equations *) |
|
203 |
||
204 |
fun assert_func thm = |
|
205 |
let |
|
206 |
val thy = Thm.theory_of_thm thm; |
|
207 |
val (head, args) = (strip_comb o fst o Logic.dest_equals |
|
208 |
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm; |
|
209 |
val _ = case head of Const _ => () | _ => |
|
210 |
bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm thm); |
|
211 |
val _ = |
|
212 |
if has_duplicates (op =) |
|
213 |
((fold o fold_aterms) (fn Var (v, _) => cons v |
|
214 |
| _ => I |
|
215 |
) args []) |
|
216 |
then bad_thm ("Duplicated variables on left hand side of equation\n" |
|
217 |
^ Display.string_of_thm thm) |
|
218 |
else () |
|
219 |
fun check _ (Abs _) = bad_thm |
|
220 |
("Abstraction on left hand side of equation\n" |
|
221 |
^ Display.string_of_thm thm) |
|
222 |
| check 0 (Var _) = () |
|
223 |
| check _ (Var _) = bad_thm |
|
224 |
("Variable with application on left hand side of defining equation\n" |
|
225 |
^ Display.string_of_thm thm) |
|
226 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2) |
|
227 |
| check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty |
|
228 |
then bad_thm |
|
229 |
("Partially applied constant on left hand side of equation\n" |
|
230 |
^ Display.string_of_thm thm) |
|
231 |
else (); |
|
232 |
val _ = map (check 0) args; |
|
233 |
in thm end; |
|
234 |
||
235 |
val mk_func = assert_func o mk_rew; |
|
236 |
||
237 |
fun head_func thm = |
|
238 |
let |
|
239 |
val thy = Thm.theory_of_thm thm; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
240 |
val Const (c, ty) = (fst o strip_comb o fst o Logic.dest_equals |
24219 | 241 |
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
242 |
in (c, ty) end; |
24219 | 243 |
|
244 |
||
245 |
(* utilities *) |
|
246 |
||
247 |
fun inst_thm tvars' thm = |
|
248 |
let |
|
249 |
val thy = Thm.theory_of_thm thm; |
|
250 |
val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
251 |
fun mk_inst (tvar as (v, _)) = case Vartab.lookup tvars' v |
|
252 |
of SOME sort => SOME (pairself (Thm.ctyp_of thy o TVar) (tvar, (v, sort))) |
|
253 |
| NONE => NONE; |
|
254 |
val insts = map_filter mk_inst tvars; |
|
255 |
in Thm.instantiate (insts, []) thm end; |
|
256 |
||
25540 | 257 |
fun constrain_thm sort thm = |
258 |
let |
|
259 |
val thy = Thm.theory_of_thm thm; |
|
260 |
val constrain = curry (Sorts.inter_sort (Sign.classes_of thy)) sort |
|
261 |
val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
262 |
fun mk_inst (tvar as (v, sort)) = pairself (Thm.ctyp_of thy o TVar o pair v) |
|
263 |
(sort, constrain sort) |
|
264 |
val insts = map mk_inst tvars; |
|
265 |
in Thm.instantiate (insts, []) thm end; |
|
266 |
||
24219 | 267 |
fun expand_eta k thm = |
268 |
let |
|
269 |
val thy = Thm.theory_of_thm thm; |
|
270 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm; |
|
271 |
val (head, args) = strip_comb lhs; |
|
272 |
val l = if k = ~1 |
|
273 |
then (length o fst o strip_abs) rhs |
|
274 |
else Int.max (0, k - length args); |
|
275 |
val used = Name.make_context (map (fst o fst) (Term.add_vars lhs [])); |
|
25336 | 276 |
fun get_name _ 0 = pair [] |
277 |
| get_name (Abs (v, ty, t)) k = |
|
278 |
Name.variants [v] |
|
279 |
##>> get_name t (k - 1) |
|
280 |
#>> (fn ([v'], vs') => (v', ty) :: vs') |
|
281 |
| get_name t k = |
|
24219 | 282 |
let |
283 |
val (tys, _) = (strip_type o fastype_of) t |
|
284 |
in case tys |
|
285 |
of [] => raise TERM ("expand_eta", [t]) |
|
286 |
| ty :: _ => |
|
25336 | 287 |
Name.variants [""] |
288 |
#-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1) |
|
24219 | 289 |
#>> (fn vs' => (v, ty) :: vs')) |
290 |
end; |
|
291 |
val (vs, _) = get_name rhs l used; |
|
25336 | 292 |
fun expand (v, ty) thm = Drule.fun_cong_rule thm |
293 |
(Thm.cterm_of thy (Var ((v, 0), ty))); |
|
24219 | 294 |
in |
295 |
thm |
|
25336 | 296 |
|> fold expand vs |
24219 | 297 |
|> Conv.fconv_rule Drule.beta_eta_conversion |
298 |
end; |
|
299 |
||
25336 | 300 |
fun func_conv conv = |
24219 | 301 |
let |
25336 | 302 |
fun lhs_conv ct = if can Thm.dest_comb ct |
303 |
then (Conv.combination_conv lhs_conv conv) ct |
|
304 |
else Conv.all_conv ct; |
|
305 |
in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end; |
|
306 |
||
307 |
val rewrite_func = Conv.fconv_rule o func_conv o MetaSimplifier.rewrite false; |
|
24219 | 308 |
|
309 |
fun norm_args thms = |
|
310 |
let |
|
311 |
val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals; |
|
312 |
val k = fold (curry Int.max o num_args_of o Thm.plain_prop_of) thms 0; |
|
313 |
in |
|
314 |
thms |
|
315 |
|> map (expand_eta k) |
|
316 |
|> map (Conv.fconv_rule Drule.beta_eta_conversion) |
|
317 |
end; |
|
318 |
||
319 |
fun canonical_tvars purify_tvar thm = |
|
320 |
let |
|
321 |
val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm); |
|
322 |
fun tvars_subst_for thm = (fold_types o fold_atyps) |
|
323 |
(fn TVar (v_i as (v, _), sort) => let |
|
324 |
val v' = purify_tvar v |
|
325 |
in if v = v' then I |
|
326 |
else insert (op =) (v_i, (v', sort)) end |
|
327 |
| _ => I) (prop_of thm) []; |
|
328 |
fun mk_inst (v_i, (v', sort)) (maxidx, acc) = |
|
329 |
let |
|
330 |
val ty = TVar (v_i, sort) |
|
331 |
in |
|
332 |
(maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc) |
|
333 |
end; |
|
334 |
val maxidx = Thm.maxidx_of thm + 1; |
|
335 |
val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []); |
|
336 |
in Thm.instantiate (inst, []) thm end; |
|
337 |
||
338 |
fun canonical_vars purify_var thm = |
|
339 |
let |
|
340 |
val cterm = Thm.cterm_of (Thm.theory_of_thm thm); |
|
341 |
fun vars_subst_for thm = fold_aterms |
|
342 |
(fn Var (v_i as (v, _), ty) => let |
|
343 |
val v' = purify_var v |
|
344 |
in if v = v' then I |
|
345 |
else insert (op =) (v_i, (v', ty)) end |
|
346 |
| _ => I) (prop_of thm) []; |
|
347 |
fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) = |
|
348 |
let |
|
349 |
val t = Var (v_i, ty) |
|
350 |
in |
|
351 |
(maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc) |
|
352 |
end; |
|
353 |
val maxidx = Thm.maxidx_of thm + 1; |
|
354 |
val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []); |
|
355 |
in Thm.instantiate ([], inst) thm end; |
|
356 |
||
357 |
fun canonical_absvars purify_var thm = |
|
358 |
let |
|
359 |
val t = Thm.plain_prop_of thm; |
|
360 |
val t' = Term.map_abs_vars purify_var t; |
|
361 |
in Thm.rename_boundvars t t' thm end; |
|
362 |
||
363 |
fun norm_varnames purify_tvar purify_var thms = |
|
364 |
let |
|
365 |
fun burrow_thms f [] = [] |
|
366 |
| burrow_thms f thms = |
|
367 |
thms |
|
368 |
|> Conjunction.intr_balanced |
|
369 |
|> f |
|
370 |
|> Conjunction.elim_balanced (length thms) |
|
371 |
in |
|
372 |
thms |
|
373 |
|> burrow_thms (canonical_tvars purify_tvar) |
|
374 |
|> map (canonical_vars purify_var) |
|
375 |
|> map (canonical_absvars purify_var) |
|
376 |
|> map Drule.zero_var_indexes |
|
377 |
end; |
|
378 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
379 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
380 |
(* case cerificates *) |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
381 |
|
24917 | 382 |
fun case_certificate thm = |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
383 |
let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
384 |
val thy = Thm.theory_of_thm thm; |
24917 | 385 |
val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals |
386 |
o apsnd Logic.dest_conjunctions o Logic.dest_implies o Thm.prop_of) thm; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
387 |
val _ = case head of Free _ => true |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
388 |
| Var _ => true |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
389 |
| _ => raise TERM ("case_cert", []); |
24917 | 390 |
val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr; |
391 |
val (Const (case_const, _), raw_params) = strip_comb case_expr; |
|
392 |
val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params; |
|
393 |
val _ = if n = ~1 then raise TERM ("case_cert", []) else (); |
|
394 |
val params = map (fst o dest_Var) (nth_drop n raw_params); |
|
395 |
fun dest_case t = |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
396 |
let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
397 |
val (head' $ t_co, rhs) = Logic.dest_equals t; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
398 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
399 |
val (Const (co, _), args) = strip_comb t_co; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
400 |
val (Var (param, _), args') = strip_comb rhs; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
401 |
val _ = if args' = args then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
402 |
in (param, co) end; |
24917 | 403 |
fun analyze_cases cases = |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
404 |
let |
24917 | 405 |
val co_list = fold (AList.update (op =) o dest_case) cases []; |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
406 |
in map (the o AList.lookup (op =) co_list) params end; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
407 |
fun analyze_let t = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
408 |
let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
409 |
val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
410 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
411 |
val _ = if arg' = arg then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
412 |
val _ = if [param'] = params then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
413 |
in [] end; |
24917 | 414 |
fun analyze (cases as [let_case]) = |
415 |
(analyze_cases cases handle Bind => analyze_let let_case) |
|
416 |
| analyze cases = analyze_cases cases; |
|
417 |
in (case_const, (n, analyze cases)) end; |
|
418 |
||
419 |
fun case_cert thm = case_certificate thm |
|
420 |
handle Bind => error "bad case certificate" |
|
421 |
| TERM _ => error "bad case certificate"; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
422 |
|
24219 | 423 |
end; |