22023
|
1 |
(* Title: Pure/Tools/codegen_func.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
22033
|
5 |
Handling defining equations ("func"s) for code generator framework.
|
22023
|
6 |
*)
|
|
7 |
|
|
8 |
signature CODEGEN_FUNC =
|
|
9 |
sig
|
22033
|
10 |
val check_rew: thm -> thm
|
|
11 |
val mk_rew: thm -> thm list
|
|
12 |
val check_func: thm -> (CodegenConsts.const * thm) option
|
|
13 |
val mk_func: thm -> (CodegenConsts.const * thm) list
|
|
14 |
val dest_func: thm -> (string * typ) * term list
|
|
15 |
val mk_head: thm -> CodegenConsts.const * thm
|
|
16 |
val typ_func: thm -> typ
|
|
17 |
val legacy_mk_func: thm -> (CodegenConsts.const * thm) list
|
|
18 |
val expand_eta: int -> thm -> thm
|
|
19 |
val rewrite_func: thm list -> thm -> thm
|
|
20 |
val get_prim_def_funcs: theory -> string * typ list -> thm list
|
22023
|
21 |
end;
|
|
22 |
|
|
23 |
structure CodegenFunc : CODEGEN_FUNC =
|
|
24 |
struct
|
|
25 |
|
22033
|
26 |
fun lift_thm_thy f thm = f (Thm.theory_of_thm thm) thm;
|
|
27 |
|
|
28 |
fun bad_thm msg thm =
|
|
29 |
error (msg ^ ": " ^ string_of_thm thm);
|
|
30 |
|
|
31 |
|
|
32 |
(* making rewrite theorems *)
|
|
33 |
|
|
34 |
fun check_rew thm =
|
|
35 |
let
|
|
36 |
val thy = Thm.theory_of_thm thm;
|
|
37 |
val (lhs, rhs) = (Logic.dest_equals o Thm.prop_of) thm;
|
|
38 |
fun vars_of t = fold_aterms
|
|
39 |
(fn Var (v, _) => insert (op =) v
|
|
40 |
| Free _ => bad_thm "Illegal free variable in rewrite theorem" thm
|
|
41 |
| _ => I) t [];
|
|
42 |
fun tvars_of t = fold_term_types
|
|
43 |
(fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v
|
|
44 |
| TFree _ => bad_thm "Illegal free type variable in rewrite theorem" thm)) t [];
|
|
45 |
val lhs_vs = vars_of lhs;
|
|
46 |
val rhs_vs = vars_of rhs;
|
|
47 |
val lhs_tvs = tvars_of lhs;
|
|
48 |
val rhs_tvs = tvars_of lhs;
|
|
49 |
val _ = if null (subtract (op =) lhs_vs rhs_vs)
|
|
50 |
then ()
|
|
51 |
else bad_thm "Free variables on right hand side of rewrite theorems" thm
|
|
52 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
|
|
53 |
then ()
|
|
54 |
else bad_thm "Free type variables on right hand side of rewrite theorems" thm
|
|
55 |
in thm end;
|
|
56 |
|
|
57 |
fun mk_rew thm =
|
22023
|
58 |
let
|
22033
|
59 |
val thy = Thm.theory_of_thm thm;
|
|
60 |
val thms = (#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy thm;
|
|
61 |
in
|
|
62 |
map check_rew thms
|
|
63 |
end;
|
|
64 |
|
|
65 |
|
|
66 |
(* making function theorems *)
|
|
67 |
|
|
68 |
val typ_func = lift_thm_thy (fn thy => snd o dest_Const o fst o strip_comb
|
|
69 |
o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of);
|
|
70 |
|
|
71 |
val dest_func = lift_thm_thy (fn thy => apfst dest_Const o strip_comb
|
|
72 |
o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of
|
|
73 |
o Drule.fconv_rule Drule.beta_eta_conversion);
|
|
74 |
|
|
75 |
val mk_head = lift_thm_thy (fn thy => fn thm =>
|
|
76 |
((CodegenConsts.norm_of_typ thy o fst o dest_func) thm, thm));
|
22023
|
77 |
|
22033
|
78 |
fun gen_check_func strict_functyp thm = case try dest_func thm
|
|
79 |
of SOME (c_ty as (c, ty), args) =>
|
|
80 |
let
|
|
81 |
val thy = Thm.theory_of_thm thm;
|
|
82 |
val _ =
|
|
83 |
if has_duplicates (op =)
|
|
84 |
((fold o fold_aterms) (fn Var (v, _) => cons v
|
|
85 |
| _ => I
|
|
86 |
) args [])
|
|
87 |
then bad_thm "Repeated variables on left hand side of function equation" thm
|
|
88 |
else ()
|
|
89 |
fun no_abs (Abs _) = bad_thm "Abstraction on left hand side of function equation" thm
|
|
90 |
| no_abs (t1 $ t2) = (no_abs t1; no_abs t2)
|
|
91 |
| no_abs _ = ();
|
|
92 |
val _ = map no_abs args;
|
|
93 |
val is_classop = (is_some o AxClass.class_of_param thy) c;
|
|
94 |
val const = CodegenConsts.norm_of_typ thy c_ty;
|
|
95 |
val ty_decl = CodegenConsts.disc_typ_of_const thy
|
|
96 |
(snd o CodegenConsts.typ_of_inst thy) const;
|
|
97 |
val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy);
|
|
98 |
val error_warning = if strict_functyp
|
|
99 |
then error
|
|
100 |
else warning #> K NONE
|
|
101 |
in if Sign.typ_equiv thy (ty_decl, ty)
|
|
102 |
then SOME (const, thm)
|
|
103 |
else (if is_classop
|
|
104 |
then error_warning
|
|
105 |
else if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
|
|
106 |
then warning #> (K o SOME) (const, thm)
|
|
107 |
else error_warning)
|
|
108 |
("Type\n" ^ string_of_typ ty
|
|
109 |
^ "\nof function theorem\n"
|
|
110 |
^ string_of_thm thm
|
|
111 |
^ "\nis strictly less general than declared function type\n"
|
|
112 |
^ string_of_typ ty_decl)
|
|
113 |
end
|
|
114 |
| NONE => bad_thm "Not a function equation" thm;
|
22023
|
115 |
|
22033
|
116 |
val check_func = gen_check_func true;
|
|
117 |
val legacy_check_func = gen_check_func false;
|
22023
|
118 |
|
22033
|
119 |
fun check_typ_classop thm =
|
22023
|
120 |
let
|
22033
|
121 |
val thy = Thm.theory_of_thm thm;
|
|
122 |
val (c_ty as (c, ty), _) = dest_func thm;
|
|
123 |
in case AxClass.class_of_param thy c
|
|
124 |
of SOME class => let
|
|
125 |
val const = CodegenConsts.norm_of_typ thy c_ty;
|
|
126 |
val ty_decl = CodegenConsts.disc_typ_of_const thy
|
|
127 |
(snd o CodegenConsts.typ_of_inst thy) const;
|
|
128 |
val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy);
|
|
129 |
in if Sign.typ_equiv thy (ty_decl, ty)
|
|
130 |
then thm
|
|
131 |
else error
|
|
132 |
("Type\n" ^ string_of_typ ty
|
|
133 |
^ "\nof function theorem\n"
|
|
134 |
^ string_of_thm thm
|
|
135 |
^ "\nis strictly less general than declared function type\n"
|
|
136 |
^ string_of_typ ty_decl)
|
|
137 |
end
|
|
138 |
| NONE => thm
|
|
139 |
end;
|
|
140 |
|
|
141 |
fun gen_mk_func check_func = map_filter check_func o mk_rew;
|
|
142 |
val mk_func = gen_mk_func check_func;
|
|
143 |
val legacy_mk_func = gen_mk_func legacy_check_func;
|
|
144 |
|
|
145 |
|
|
146 |
(* utilities *)
|
|
147 |
|
|
148 |
fun expand_eta k thm =
|
|
149 |
let
|
|
150 |
val thy = Thm.theory_of_thm thm;
|
22023
|
151 |
val (lhs, rhs) = (Logic.dest_equals o Drule.plain_prop_of) thm;
|
|
152 |
val (head, args) = strip_comb lhs;
|
|
153 |
val l = if k = ~1
|
|
154 |
then (length o fst o strip_abs) rhs
|
|
155 |
else Int.max (0, k - length args);
|
|
156 |
val used = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
|
|
157 |
fun get_name _ 0 used = ([], used)
|
|
158 |
| get_name (Abs (v, ty, t)) k used =
|
|
159 |
used
|
22033
|
160 |
|> Name.variants [v]
|
22023
|
161 |
||>> get_name t (k - 1)
|
|
162 |
|>> (fn ([v'], vs') => (v', ty) :: vs')
|
|
163 |
| get_name t k used =
|
|
164 |
let
|
|
165 |
val (tys, _) = (strip_type o fastype_of) t
|
|
166 |
in case tys
|
|
167 |
of [] => raise TERM ("expand_eta", [t])
|
|
168 |
| ty :: _ =>
|
|
169 |
used
|
|
170 |
|> Name.variants [""]
|
|
171 |
|-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1)
|
|
172 |
#>> (fn vs' => (v, ty) :: vs'))
|
|
173 |
end;
|
|
174 |
val (vs, _) = get_name rhs l used;
|
|
175 |
val vs_refl = map (fn (v, ty) => Thm.reflexive (Thm.cterm_of thy (Var ((v, 0), ty)))) vs;
|
|
176 |
in
|
|
177 |
fold (fn refl => fn thm => Thm.combination thm refl) vs_refl thm
|
|
178 |
end;
|
|
179 |
|
22033
|
180 |
fun get_prim_def_funcs thy c =
|
|
181 |
let
|
|
182 |
fun constrain thm0 thm = case AxClass.class_of_param thy (fst c)
|
|
183 |
of SOME _ =>
|
|
184 |
let
|
|
185 |
val ty_decl = CodegenConsts.disc_typ_of_classop thy c;
|
|
186 |
val max = maxidx_of_typ ty_decl + 1;
|
|
187 |
val thm = Thm.incr_indexes max thm;
|
|
188 |
val ty = typ_func thm;
|
|
189 |
val (env, _) = Sign.typ_unify thy (ty_decl, ty) (Vartab.empty, max);
|
|
190 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
|
|
191 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
|
|
192 |
in Thm.instantiate (instT, []) thm end
|
|
193 |
| NONE => thm
|
|
194 |
in case CodegenConsts.find_def thy c
|
|
195 |
of SOME ((_, thm), _) =>
|
|
196 |
thm
|
|
197 |
|> Thm.transfer thy
|
|
198 |
|> try (map snd o mk_func)
|
|
199 |
|> these
|
|
200 |
|> map (constrain thm)
|
|
201 |
|> map (expand_eta ~1)
|
|
202 |
| NONE => []
|
|
203 |
end;
|
|
204 |
|
|
205 |
fun rewrite_func rewrites thm =
|
|
206 |
let
|
|
207 |
val rewrite = MetaSimplifier.rewrite false rewrites;
|
|
208 |
val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm;
|
|
209 |
val Const ("==", _) = Thm.term_of ct_eq;
|
|
210 |
val (ct_f, ct_args) = Drule.strip_comb ct_lhs;
|
|
211 |
val rhs' = rewrite ct_rhs;
|
|
212 |
val args' = map rewrite ct_args;
|
|
213 |
val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1)
|
|
214 |
args' (Thm.reflexive ct_f));
|
|
215 |
in
|
|
216 |
Thm.transitive (Thm.transitive lhs' thm) rhs'
|
|
217 |
end handle Bind => raise ERROR "rewrite_func"
|
|
218 |
|
22023
|
219 |
end;
|