166 val bn_fun_strs' = add_raws bn_fun_strs |
164 val bn_fun_strs' = add_raws bn_fun_strs |
167 val bn_fun_env = bn_fun_strs ~~ bn_fun_strs' |
165 val bn_fun_env = bn_fun_strs ~~ bn_fun_strs' |
168 val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) |
166 val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) |
169 (bn_fun_strs ~~ bn_fun_strs') |
167 (bn_fun_strs ~~ bn_fun_strs') |
170 |
168 |
171 val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env |
169 val (raw_full_dt_names, raw_dts) = rawify_dts dt_names dts dts_env |
172 val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs |
170 val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs |
173 val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env bclauses |
171 val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env bclauses |
174 |
172 |
175 val (raw_dt_full_names, thy1) = |
173 val (raw_full_dt_names', thy1) = |
176 Datatype.add_datatype Datatype.default_config raw_dt_names raw_dts thy |
174 Datatype.add_datatype Datatype.default_config raw_full_dt_names raw_dts thy |
177 |
175 |
178 val lthy1 = Named_Target.theory_init thy1 |
176 val lthy1 = Named_Target.theory_init thy1 |
179 in |
177 |
180 (raw_dt_full_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, cnstr_names, lthy1) |
178 val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy1)) raw_full_dt_names' |
181 end |
|
182 *} |
|
183 |
|
184 |
|
185 ML {* |
|
186 fun nominal_datatype2 opt_thms_name dts bn_funs bn_eqs bclauses lthy = |
|
187 let |
|
188 val _ = trace_msg (K "Defining raw datatypes...") |
|
189 val (raw_dt_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, cnstr_names, lthy0) = |
|
190 define_raw_dts dts bn_funs bn_eqs bclauses lthy |
|
191 |
|
192 val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy0)) raw_dt_names |
|
193 val {descr, sorts, ...} = hd dtinfos |
179 val {descr, sorts, ...} = hd dtinfos |
194 |
180 |
195 val raw_tys = Datatype_Aux.get_rec_types descr sorts |
181 val raw_tys = Datatype_Aux.get_rec_types descr sorts |
196 val tvs = hd raw_tys |
182 val raw_ty_args = hd raw_tys |
197 |> snd o dest_Type |
183 |> snd o dest_Type |
198 |> map dest_TFree |
184 |> map dest_TFree |
199 |
185 |
200 val raw_cns_info = all_dtyp_constrs_types descr sorts |
186 val raw_cns_info = all_dtyp_constrs_types descr sorts |
201 val raw_constrs = (map o map) (fn (c, _, _, _) => c) raw_cns_info |
187 val raw_all_cns = (map o map) (fn (c, _, _, _) => c) raw_cns_info |
202 |
188 |
203 val raw_inject_thms = flat (map #inject dtinfos) |
189 val raw_inject_thms = flat (map #inject dtinfos) |
204 val raw_distinct_thms = flat (map #distinct dtinfos) |
190 val raw_distinct_thms = flat (map #distinct dtinfos) |
205 val raw_induct_thm = #induct (hd dtinfos) |
191 val raw_induct_thm = #induct (hd dtinfos) |
206 val raw_induct_thms = #inducts (hd dtinfos) |
192 val raw_induct_thms = #inducts (hd dtinfos) |
207 val raw_exhaust_thms = map #exhaust dtinfos |
193 val raw_exhaust_thms = map #exhaust dtinfos |
208 val raw_size_trms = map HOLogic.size_const raw_tys |
194 val raw_size_trms = map HOLogic.size_const raw_tys |
209 val raw_size_thms = Size.size_thms (ProofContext.theory_of lthy0) (hd raw_dt_names) |
195 val raw_size_thms = Size.size_thms (ProofContext.theory_of lthy1) (hd raw_full_dt_names') |
210 |> `(fn thms => (length thms) div 2) |
196 |> `(fn thms => (length thms) div 2) |
211 |> uncurry drop |
197 |> uncurry drop |
|
198 |
|
199 val raw_result = RawDtInfo |
|
200 {raw_dt_names = raw_full_dt_names', |
|
201 raw_dts = raw_dts, |
|
202 raw_tys = raw_tys, |
|
203 raw_ty_args = raw_ty_args, |
|
204 raw_cns_info = raw_cns_info, |
|
205 raw_all_cns = raw_all_cns, |
|
206 raw_inject_thms = raw_inject_thms, |
|
207 raw_distinct_thms = raw_distinct_thms, |
|
208 raw_induct_thm = raw_induct_thm, |
|
209 raw_induct_thms = raw_induct_thms, |
|
210 raw_exhaust_thms = raw_exhaust_thms, |
|
211 raw_size_trms = raw_size_trms, |
|
212 raw_size_thms = raw_size_thms} |
|
213 in |
|
214 (raw_bclauses, raw_bn_funs, raw_bn_eqs, raw_result, lthy1) |
|
215 end |
|
216 *} |
|
217 |
|
218 |
|
219 ML {* |
|
220 fun nominal_datatype2 opt_thms_name dts bn_funs bn_eqs bclauses lthy = |
|
221 let |
|
222 val cnstr_names = get_cnstr_strs dts |
|
223 val cnstr_tys = get_typed_cnstrs dts |
|
224 |
|
225 val _ = trace_msg (K "Defining raw datatypes...") |
|
226 val (raw_bclauses, raw_bn_funs, raw_bn_eqs, raw_dt_info, lthy0) = |
|
227 define_raw_dts dts cnstr_names cnstr_tys bn_funs bn_eqs bclauses lthy |
|
228 |
|
229 val RawDtInfo |
|
230 {raw_dt_names, |
|
231 raw_tys, |
|
232 raw_ty_args, |
|
233 raw_all_cns, |
|
234 raw_inject_thms, |
|
235 raw_distinct_thms, |
|
236 raw_induct_thm, |
|
237 raw_induct_thms, |
|
238 raw_exhaust_thms, |
|
239 raw_size_trms, |
|
240 raw_size_thms, ...} = raw_dt_info |
212 |
241 |
213 val _ = trace_msg (K "Defining raw permutations...") |
242 val _ = trace_msg (K "Defining raw permutations...") |
214 val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2a) = |
243 val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2a) = define_raw_perms raw_dt_info lthy0 |
215 define_raw_perms raw_dt_names raw_tys tvs (flat raw_constrs) raw_induct_thm lthy0 |
|
216 |
244 |
217 (* noting the raw permutations as eqvt theorems *) |
245 (* noting the raw permutations as eqvt theorems *) |
218 val (_, lthy3) = Local_Theory.note ((Binding.empty, [eqvt_attr]), raw_perm_simps) lthy2a |
246 val (_, lthy3) = Local_Theory.note ((Binding.empty, [eqvt_attr]), raw_perm_simps) lthy2a |
219 |
247 |
220 val _ = trace_msg (K "Defining raw fv- and bn-functions...") |
248 val _ = trace_msg (K "Defining raw fv- and bn-functions...") |
221 val (raw_bns, raw_bn_defs, raw_bn_info, raw_bn_inducts, lthy3a) = |
249 val (raw_bns, raw_bn_defs, raw_bn_info, raw_bn_inducts, lthy3a) = |
222 define_raw_bns raw_dt_names raw_dts raw_bn_funs raw_bn_eqs |
250 define_raw_bns raw_dt_info raw_bn_funs raw_bn_eqs lthy3 |
223 (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3 |
|
224 |
251 |
225 (* defining the permute_bn functions *) |
252 (* defining the permute_bn functions *) |
226 val (raw_perm_bns, raw_perm_bn_simps, lthy3b) = |
253 val (raw_perm_bns, raw_perm_bn_simps, lthy3b) = |
227 define_raw_bn_perms raw_tys raw_bn_info raw_cns_info |
254 define_raw_bn_perms raw_dt_info raw_bn_info lthy3a |
228 (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3a |
|
229 |
255 |
230 val (raw_fvs, raw_fv_bns, raw_fv_defs, raw_fv_bns_induct, lthy3c) = |
256 val (raw_fvs, raw_fv_bns, raw_fv_defs, raw_fv_bns_induct, lthy3c) = |
231 define_raw_fvs raw_dt_names raw_tys raw_cns_info raw_bn_info raw_bclauses |
257 define_raw_fvs raw_dt_info raw_bn_info raw_bclauses lthy3b |
232 (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3b |
|
233 |
258 |
234 val _ = trace_msg (K "Defining alpha relations...") |
259 val _ = trace_msg (K "Defining alpha relations...") |
235 val (alpha_result, lthy4) = |
260 val (alpha_result, lthy4) = |
236 define_raw_alpha raw_dt_names raw_tys raw_cns_info raw_bn_info raw_bclauses raw_fvs lthy3c |
261 define_raw_alpha raw_dt_info raw_bn_info raw_bclauses raw_fvs lthy3c |
237 |
262 |
238 val _ = trace_msg (K "Proving distinct theorems...") |
263 val _ = trace_msg (K "Proving distinct theorems...") |
239 val alpha_distincts = |
264 val alpha_distincts = raw_prove_alpha_distincts lthy4 alpha_result raw_dt_info |
240 raw_prove_alpha_distincts lthy4 alpha_result raw_distinct_thms raw_dt_names |
|
241 |
265 |
242 val _ = trace_msg (K "Proving eq-iff theorems...") |
266 val _ = trace_msg (K "Proving eq-iff theorems...") |
243 val alpha_eq_iff = raw_prove_alpha_eq_iff lthy4 alpha_result raw_distinct_thms raw_inject_thms |
267 val alpha_eq_iff = raw_prove_alpha_eq_iff lthy4 alpha_result raw_dt_info |
244 |
268 |
245 val _ = trace_msg (K "Proving equivariance of bns, fvs, size and alpha...") |
269 val _ = trace_msg (K "Proving equivariance of bns, fvs, size and alpha...") |
246 val raw_bn_eqvt = |
270 val raw_bn_eqvt = |
247 raw_prove_eqvt raw_bns raw_bn_inducts (raw_bn_defs @ raw_perm_simps) lthy4 |
271 raw_prove_eqvt raw_bns raw_bn_inducts (raw_bn_defs @ raw_perm_simps) lthy4 |
248 |
272 |