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