author | haftmann |
Mon, 11 May 2009 11:53:21 +0200 | |
changeset 31092 | 27a6558e64b6 |
parent 31090 | 3be41b271023 |
child 31138 | a51ce445d498 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Pure/Isar/code_unit.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
4 |
Basic notions of code generation. Auxiliary. |
24219 | 5 |
*) |
6 |
||
7 |
signature CODE_UNIT = |
|
8 |
sig |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
9 |
(*typ instantiations*) |
26970 | 10 |
val typscheme: theory -> string * typ -> (string * sort) list * typ |
28423 | 11 |
val inst_thm: theory -> sort Vartab.table -> thm -> thm |
12 |
val constrain_thm: theory -> sort -> thm -> thm |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
13 |
|
26747
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
14 |
(*constant aliasses*) |
26354 | 15 |
val add_const_alias: thm -> theory -> theory |
28346
b8390cd56b8f
discontinued special treatment of op = vs. eq_class.eq
haftmann
parents:
28310
diff
changeset
|
16 |
val triv_classes: theory -> class list |
26747
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
17 |
val resubst_alias: theory -> string -> string |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
18 |
|
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
19 |
(*constants*) |
24219 | 20 |
val string_of_typ: theory -> typ -> string |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
21 |
val string_of_const: theory -> string -> string |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
22 |
val no_args: theory -> string -> int |
26112 | 23 |
val check_const: theory -> term -> string |
24219 | 24 |
val read_bare_const: theory -> string -> string * typ |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
25 |
val read_const: theory -> string -> string |
24219 | 26 |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
27 |
(*constructor sets*) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
28 |
val constrset_of_consts: theory -> (string * typ) list |
24219 | 29 |
-> string * ((string * sort) list * (string * typ list) list) |
30 |
||
30022
1d8b8fa19074
maintain order of constructors in datatypes; clarified conventions for type schemes
haftmann
parents:
29288
diff
changeset
|
31 |
(*code equations*) |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
32 |
val mk_eqn: theory -> (string -> bool) -> thm * bool -> thm * bool |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
33 |
val mk_eqn_liberal: theory -> (string -> bool) -> thm -> (thm * bool) option |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
34 |
val assert_eqn: theory -> (string -> bool) -> thm * bool -> thm * bool |
31089 | 35 |
val const_eqn: theory -> thm -> string |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
36 |
val const_typ_eqn: thm -> string * typ |
31089 | 37 |
val typscheme_eqn: theory -> thm -> (string * sort) list * typ |
28423 | 38 |
val expand_eta: theory -> int -> thm -> thm |
28368 | 39 |
val rewrite_eqn: simpset -> thm -> thm |
27582 | 40 |
val rewrite_head: thm list -> thm -> thm |
28423 | 41 |
val norm_args: theory -> thm list -> thm list |
31036 | 42 |
val norm_varnames: theory -> thm list -> thm list |
24844
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 |
||
28054 | 48 |
structure Code_Unit: CODE_UNIT = |
24219 | 49 |
struct |
50 |
||
51 |
||
52 |
(* auxiliary *) |
|
53 |
||
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26747
diff
changeset
|
54 |
fun string_of_typ thy = setmp show_sorts true (Syntax.string_of_typ_global thy); |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25540
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
| NONE => Sign.extern_const thy c; |
24219 | 58 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
59 |
fun no_args thy = length o fst o strip_type o Sign.the_const_type thy; |
24219 | 60 |
|
26354 | 61 |
|
62 |
(* utilities *) |
|
63 |
||
26970 | 64 |
fun typscheme thy (c, ty) = |
65 |
let |
|
30022
1d8b8fa19074
maintain order of constructors in datatypes; clarified conventions for type schemes
haftmann
parents:
29288
diff
changeset
|
66 |
val ty' = Logic.unvarifyT ty; |
1d8b8fa19074
maintain order of constructors in datatypes; clarified conventions for type schemes
haftmann
parents:
29288
diff
changeset
|
67 |
fun dest (TFree (v, sort)) = (v, sort) |
26970 | 68 |
| dest ty = error ("Illegal type parameter in type scheme: " ^ Syntax.string_of_typ_global thy ty); |
30022
1d8b8fa19074
maintain order of constructors in datatypes; clarified conventions for type schemes
haftmann
parents:
29288
diff
changeset
|
69 |
val vs = map dest (Sign.const_typargs thy (c, ty')); |
1d8b8fa19074
maintain order of constructors in datatypes; clarified conventions for type schemes
haftmann
parents:
29288
diff
changeset
|
70 |
in (vs, Type.strip_sorts ty') end; |
26970 | 71 |
|
28423 | 72 |
fun inst_thm thy tvars' thm = |
26354 | 73 |
let |
74 |
val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
28310 | 75 |
val inter_sort = Sorts.inter_sort (Sign.classes_of thy); |
76 |
fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v |
|
77 |
of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar) |
|
78 |
(tvar, (v, inter_sort (sort, sort')))) |
|
26354 | 79 |
| NONE => NONE; |
80 |
val insts = map_filter mk_inst tvars; |
|
81 |
in Thm.instantiate (insts, []) thm end; |
|
82 |
||
28423 | 83 |
fun constrain_thm thy sort thm = |
26354 | 84 |
let |
85 |
val constrain = curry (Sorts.inter_sort (Sign.classes_of thy)) sort |
|
86 |
val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
87 |
fun mk_inst (tvar as (v, sort)) = pairself (Thm.ctyp_of thy o TVar o pair v) |
|
88 |
(sort, constrain sort) |
|
89 |
val insts = map mk_inst tvars; |
|
90 |
in Thm.instantiate (insts, []) thm end; |
|
91 |
||
28423 | 92 |
fun expand_eta thy k thm = |
26354 | 93 |
let |
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 |
||
28368 | 124 |
fun eqn_conv conv = |
26354 | 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 |
||
28368 | 138 |
val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite; |
26354 | 139 |
val rewrite_head = Conv.fconv_rule o head_conv o MetaSimplifier.rewrite false; |
140 |
||
28423 | 141 |
fun norm_args thy thms = |
26354 | 142 |
let |
143 |
val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals; |
|
28310 | 144 |
val k = fold (curry Int.max o num_args_of o Thm.prop_of) thms 0; |
26354 | 145 |
in |
146 |
thms |
|
28423 | 147 |
|> map (expand_eta thy k) |
26354 | 148 |
|> map (Conv.fconv_rule Drule.beta_eta_conversion) |
149 |
end; |
|
150 |
||
31036 | 151 |
fun canonical_tvars thy thm = |
26354 | 152 |
let |
28423 | 153 |
val ctyp = Thm.ctyp_of thy; |
31036 | 154 |
val purify_tvar = unprefix "'" #> Name.desymbolize false #> prefix "'"; |
26354 | 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 |
||
31036 | 171 |
fun canonical_vars thy thm = |
26354 | 172 |
let |
28423 | 173 |
val cterm = Thm.cterm_of thy; |
31036 | 174 |
val purify_var = Name.desymbolize false; |
26354 | 175 |
fun vars_subst_for thm = fold_aterms |
176 |
(fn Var (v_i as (v, _), ty) => let |
|
177 |
val v' = purify_var v |
|
178 |
in if v = v' then I |
|
179 |
else insert (op =) (v_i, (v', ty)) end |
|
180 |
| _ => I) (prop_of thm) []; |
|
181 |
fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) = |
|
182 |
let |
|
183 |
val t = Var (v_i, ty) |
|
184 |
in |
|
185 |
(maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc) |
|
186 |
end; |
|
187 |
val maxidx = Thm.maxidx_of thm + 1; |
|
188 |
val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []); |
|
189 |
in Thm.instantiate ([], inst) thm end; |
|
190 |
||
31036 | 191 |
fun canonical_absvars thm = |
26354 | 192 |
let |
193 |
val t = Thm.plain_prop_of thm; |
|
31036 | 194 |
val purify_var = Name.desymbolize false; |
26354 | 195 |
val t' = Term.map_abs_vars purify_var t; |
196 |
in Thm.rename_boundvars t t' thm end; |
|
197 |
||
31036 | 198 |
fun norm_varnames thy thms = |
26354 | 199 |
let |
200 |
fun burrow_thms f [] = [] |
|
201 |
| burrow_thms f thms = |
|
202 |
thms |
|
203 |
|> Conjunction.intr_balanced |
|
204 |
|> f |
|
205 |
|> Conjunction.elim_balanced (length thms) |
|
206 |
in |
|
207 |
thms |
|
31036 | 208 |
|> map (canonical_vars thy) |
209 |
|> map canonical_absvars |
|
30739 | 210 |
|> map Drule.zero_var_indexes |
31036 | 211 |
|> burrow_thms (canonical_tvars thy) |
30688 | 212 |
|> Drule.zero_var_indexes_list |
26354 | 213 |
end; |
214 |
||
28346
b8390cd56b8f
discontinued special treatment of op = vs. eq_class.eq
haftmann
parents:
28310
diff
changeset
|
215 |
|
26354 | 216 |
(* const aliasses *) |
217 |
||
218 |
structure ConstAlias = TheoryDataFun |
|
219 |
( |
|
26747
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
220 |
type T = ((string * string) * thm) list * class list; |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
221 |
val empty = ([], []); |
26354 | 222 |
val copy = I; |
26618 | 223 |
val extend = I; |
29288 | 224 |
fun merge _ ((alias1, classes1), (alias2, classes2)) : T = |
26747
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
225 |
(Library.merge (eq_snd Thm.eq_thm_prop) (alias1, alias2), |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
226 |
Library.merge (op =) (classes1, classes2)); |
26354 | 227 |
); |
228 |
||
28423 | 229 |
fun add_const_alias thm thy = |
26354 | 230 |
let |
28423 | 231 |
val lhs_rhs = case try Logic.dest_equals (Thm.prop_of thm) |
26354 | 232 |
of SOME lhs_rhs => lhs_rhs |
233 |
| _ => error ("Not an equation: " ^ Display.string_of_thm thm); |
|
234 |
val c_c' = case try (pairself (AxClass.unoverload_const thy o dest_Const)) lhs_rhs |
|
235 |
of SOME c_c' => c_c' |
|
236 |
| _ => error ("Not an equation with two constants: " ^ Display.string_of_thm thm); |
|
26747
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
237 |
val some_class = the_list (AxClass.class_of_param thy (snd c_c')); |
28423 | 238 |
in thy |> |
26747
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
239 |
ConstAlias.map (fn (alias, classes) => |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
240 |
((c_c', thm) :: alias, fold (insert (op =)) some_class classes)) |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
241 |
end; |
26354 | 242 |
|
26747
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
243 |
fun resubst_alias thy = |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
244 |
let |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
245 |
val alias = fst (ConstAlias.get thy); |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
246 |
val subst_inst_param = Option.map fst o AxClass.inst_of_param thy; |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
247 |
fun subst_alias c = |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
248 |
get_first (fn ((c', c''), _) => if c = c'' then SOME c' else NONE) alias; |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
249 |
in |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
250 |
perhaps subst_inst_param |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
251 |
#> perhaps subst_alias |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
252 |
end; |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
253 |
|
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
254 |
val triv_classes = snd o ConstAlias.get; |
f32fa5f5bdd1
moved 'trivial classes' to foundation of code generator
haftmann
parents:
26618
diff
changeset
|
255 |
|
28346
b8390cd56b8f
discontinued special treatment of op = vs. eq_class.eq
haftmann
parents:
28310
diff
changeset
|
256 |
|
26610 | 257 |
(* reading constants as terms *) |
24219 | 258 |
|
26112 | 259 |
fun check_bare_const thy t = case try dest_Const t |
260 |
of SOME c_ty => c_ty |
|
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26747
diff
changeset
|
261 |
| NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t); |
26112 | 262 |
|
28346
b8390cd56b8f
discontinued special treatment of op = vs. eq_class.eq
haftmann
parents:
28310
diff
changeset
|
263 |
fun check_const thy = AxClass.unoverload_const thy o check_bare_const thy; |
26112 | 264 |
|
265 |
fun read_bare_const thy = check_bare_const thy o Syntax.read_term_global thy; |
|
24219 | 266 |
|
28346
b8390cd56b8f
discontinued special treatment of op = vs. eq_class.eq
haftmann
parents:
28310
diff
changeset
|
267 |
fun read_const thy = AxClass.unoverload_const thy o read_bare_const thy; |
24219 | 268 |
|
269 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
270 |
(* constructor sets *) |
24219 | 271 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
272 |
fun constrset_of_consts thy cs = |
24219 | 273 |
let |
28704
8703d17c5e68
assert that no class parameter is used as constructor
haftmann
parents:
28423
diff
changeset
|
274 |
val _ = map (fn (c, _) => if (is_some o AxClass.class_of_param thy) c |
8703d17c5e68
assert that no class parameter is used as constructor
haftmann
parents:
28423
diff
changeset
|
275 |
then error ("Is a class parameter: " ^ string_of_const thy c) else ()) cs; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
276 |
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
|
277 |
^ " :: " ^ string_of_typ thy ty); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
278 |
fun last_typ c_ty ty = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
279 |
let |
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
28708
diff
changeset
|
280 |
val frees = OldTerm.typ_tfrees ty; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
281 |
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
|
282 |
handle TYPE _ => no_constr c_ty |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
283 |
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
|
284 |
val _ = if length frees <> length vs then no_constr c_ty else (); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
285 |
in (tyco, vs) end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
286 |
fun ty_sorts (c, 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 ty_decl = (Logic.unvarifyT o Sign.the_const_type thy) c; |
26970 | 289 |
val (tyco, _) = last_typ (c, ty) ty_decl; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
290 |
val (_, vs) = last_typ (c, ty) ty; |
28015 | 291 |
in ((tyco, map snd vs), (c, (map fst vs, ty))) end; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
292 |
fun add ((tyco', sorts'), c) ((tyco, sorts), cs) = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
293 |
let |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
294 |
val _ = if tyco' <> tyco |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
295 |
then error "Different type constructors in constructor set" |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
296 |
else (); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
297 |
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
|
298 |
in ((tyco, sorts), c :: cs) end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
299 |
fun inst vs' (c, (vs, ty)) = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
300 |
let |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
301 |
val the_v = the o AList.lookup (op =) (vs ~~ vs'); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
302 |
val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
303 |
in (c, (fst o strip_type) ty') end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
304 |
val c' :: cs' = map ty_sorts cs; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
305 |
val ((tyco, sorts), cs'') = fold add cs' (apsnd single c'); |
24848 | 306 |
val vs = Name.names Name.context Name.aT sorts; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
307 |
val cs''' = map (inst vs) cs''; |
30022
1d8b8fa19074
maintain order of constructors in datatypes; clarified conventions for type schemes
haftmann
parents:
29288
diff
changeset
|
308 |
in (tyco, (vs, rev cs''')) end; |
24219 | 309 |
|
310 |
||
30022
1d8b8fa19074
maintain order of constructors in datatypes; clarified conventions for type schemes
haftmann
parents:
29288
diff
changeset
|
311 |
(* code equations *) |
24219 | 312 |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
313 |
exception BAD_THM of string; |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
314 |
fun bad_thm msg = raise BAD_THM msg; |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
315 |
fun error_thm f thm = f thm handle BAD_THM msg => error msg; |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
316 |
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE; |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
317 |
|
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
318 |
fun is_linear thm = |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
319 |
let val (_, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
320 |
in not (has_duplicates (op =) ((fold o fold_aterms) |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
321 |
(fn Var (v, _) => cons v | _ => I) args [])) end; |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
322 |
|
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
323 |
fun gen_assert_eqn thy is_constr_head is_constr_pat (thm, proper) = |
24219 | 324 |
let |
325 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm |
|
326 |
handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm) |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
327 |
| THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm); |
30766
44561a14a4c5
corrected check for additional type variables on rhs of code equations
haftmann
parents:
30739
diff
changeset
|
328 |
fun vars_of t = fold_aterms (fn Var (v, _) => insert (op =) v |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
329 |
| Free _ => bad_thm ("Illegal free variable in equation\n" |
30766
44561a14a4c5
corrected check for additional type variables on rhs of code equations
haftmann
parents:
30739
diff
changeset
|
330 |
^ Display.string_of_thm thm) |
44561a14a4c5
corrected check for additional type variables on rhs of code equations
haftmann
parents:
30739
diff
changeset
|
331 |
| _ => I) t []; |
44561a14a4c5
corrected check for additional type variables on rhs of code equations
haftmann
parents:
30739
diff
changeset
|
332 |
fun tvars_of t = fold_term_types (fn _ => |
44561a14a4c5
corrected check for additional type variables on rhs of code equations
haftmann
parents:
30739
diff
changeset
|
333 |
fold_atyps (fn TVar (v, _) => insert (op =) v |
44561a14a4c5
corrected check for additional type variables on rhs of code equations
haftmann
parents:
30739
diff
changeset
|
334 |
| TFree _ => bad_thm |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
335 |
("Illegal free type variable in equation\n" ^ Display.string_of_thm thm))) t []; |
24219 | 336 |
val lhs_vs = vars_of lhs; |
337 |
val rhs_vs = vars_of rhs; |
|
338 |
val lhs_tvs = tvars_of lhs; |
|
30766
44561a14a4c5
corrected check for additional type variables on rhs of code equations
haftmann
parents:
30739
diff
changeset
|
339 |
val rhs_tvs = tvars_of rhs; |
24219 | 340 |
val _ = if null (subtract (op =) lhs_vs rhs_vs) |
341 |
then () |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
342 |
else bad_thm ("Free variables on right hand side of equation\n" |
24219 | 343 |
^ Display.string_of_thm thm); |
344 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs) |
|
345 |
then () |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
346 |
else bad_thm ("Free type variables on right hand side of equation\n" |
28423 | 347 |
^ Display.string_of_thm thm) val (head, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm; |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
348 |
val (c, ty) = case head |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
349 |
of Const (c_ty as (_, ty)) => (AxClass.unoverload_const thy c_ty, ty) |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
350 |
| _ => bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm thm); |
24219 | 351 |
fun check _ (Abs _) = bad_thm |
352 |
("Abstraction on left hand side of equation\n" |
|
353 |
^ Display.string_of_thm thm) |
|
354 |
| check 0 (Var _) = () |
|
355 |
| check _ (Var _) = bad_thm |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
356 |
("Variable with application on left hand side of equation\n" |
24219 | 357 |
^ Display.string_of_thm thm) |
358 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2) |
|
31092
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
359 |
| check n (Const (c_ty as (c, ty))) = if n = (length o fst o strip_type) ty |
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
360 |
then if not proper orelse is_constr_pat (AxClass.unoverload_const thy c_ty) |
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
361 |
then () |
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
362 |
else bad_thm (quote c ^ " is not a constructor, on left hand side of equation\n" |
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
363 |
^ Display.string_of_thm thm) |
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
364 |
else bad_thm |
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
365 |
("Partially applied constant " ^ quote c ^ " on left hand side of equation\n" |
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
366 |
^ Display.string_of_thm thm); |
24219 | 367 |
val _ = map (check 0) args; |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
368 |
val _ = if not proper orelse is_linear thm then () |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
369 |
else bad_thm ("Duplicate variables on left hand side of equation\n" |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
370 |
^ Display.string_of_thm thm); |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
371 |
val _ = if (is_none o AxClass.class_of_param thy) c |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
372 |
then () |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
373 |
else bad_thm ("Polymorphic constant as head in equation\n" |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
374 |
^ Display.string_of_thm thm) |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
375 |
val _ = if not (is_constr_head c) |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
376 |
then () |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
377 |
else bad_thm ("Constructor as head in equation\n" |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
378 |
^ Display.string_of_thm thm) |
28423 | 379 |
val ty_decl = Sign.the_const_type thy c; |
380 |
val _ = if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) |
|
381 |
then () else bad_thm ("Type\n" ^ string_of_typ thy ty |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
382 |
^ "\nof equation\n" |
28423 | 383 |
^ Display.string_of_thm thm |
384 |
^ "\nis incompatible with declared function type\n" |
|
385 |
^ string_of_typ thy ty_decl) |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
386 |
in (thm, proper) end; |
28423 | 387 |
|
31092
27a6558e64b6
proper error handling for malformed code equations
haftmann
parents:
31090
diff
changeset
|
388 |
fun assert_eqn thy is_constr = error_thm (gen_assert_eqn thy is_constr is_constr); |
28423 | 389 |
|
31089 | 390 |
val const_typ_eqn = dest_Const o fst o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of; |
28423 | 391 |
|
31089 | 392 |
fun typscheme_eqn thy = typscheme thy o const_typ_eqn; |
28423 | 393 |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
394 |
(*these are permissive wrt. to overloaded constants!*) |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
395 |
fun mk_eqn thy is_constr_head = error_thm (gen_assert_eqn thy is_constr_head (K true)) o |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
396 |
apfst (LocalDefs.meta_rewrite_rule (ProofContext.init thy)); |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
397 |
|
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
398 |
fun mk_eqn_liberal thy is_constr_head = Option.map (fn (thm, _) => (thm, is_linear thm)) |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
399 |
o try_thm (gen_assert_eqn thy is_constr_head (K true)) |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
400 |
o rpair false o LocalDefs.meta_rewrite_rule (ProofContext.init thy); |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31089
diff
changeset
|
401 |
|
31089 | 402 |
fun const_eqn thy = AxClass.unoverload_const thy o const_typ_eqn; |
24219 | 403 |
|
404 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
405 |
(* case cerificates *) |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
406 |
|
24917 | 407 |
fun case_certificate thm = |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
408 |
let |
24917 | 409 |
val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals |
410 |
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
|
411 |
val _ = case head of Free _ => true |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
412 |
| Var _ => true |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
413 |
| _ => raise TERM ("case_cert", []); |
24917 | 414 |
val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr; |
415 |
val (Const (case_const, _), raw_params) = strip_comb case_expr; |
|
416 |
val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params; |
|
417 |
val _ = if n = ~1 then raise TERM ("case_cert", []) else (); |
|
418 |
val params = map (fst o dest_Var) (nth_drop n raw_params); |
|
419 |
fun dest_case t = |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
420 |
let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
421 |
val (head' $ t_co, rhs) = Logic.dest_equals t; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
422 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
423 |
val (Const (co, _), args) = strip_comb t_co; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
424 |
val (Var (param, _), args') = strip_comb rhs; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
425 |
val _ = if args' = args then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
426 |
in (param, co) end; |
24917 | 427 |
fun analyze_cases cases = |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
428 |
let |
24917 | 429 |
val co_list = fold (AList.update (op =) o dest_case) cases []; |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
430 |
in map (the o AList.lookup (op =) co_list) params end; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
431 |
fun analyze_let t = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
432 |
let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
433 |
val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
434 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
435 |
val _ = if arg' = arg then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
436 |
val _ = if [param'] = params then () else raise TERM ("case_cert", []); |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
437 |
in [] end; |
24917 | 438 |
fun analyze (cases as [let_case]) = |
439 |
(analyze_cases cases handle Bind => analyze_let let_case) |
|
440 |
| analyze cases = analyze_cases cases; |
|
441 |
in (case_const, (n, analyze cases)) end; |
|
442 |
||
443 |
fun case_cert thm = case_certificate thm |
|
444 |
handle Bind => error "bad case certificate" |
|
28346
b8390cd56b8f
discontinued special treatment of op = vs. eq_class.eq
haftmann
parents:
28310
diff
changeset
|
445 |
| TERM _ => error "bad case certificate"; |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24707
diff
changeset
|
446 |
|
24219 | 447 |
end; |