221 in |
221 in |
222 Drule.instantiate' [SOME cty] [SOME ct] @{thm equivp_hack} |
222 Drule.instantiate' [SOME cty] [SOME ct] @{thm equivp_hack} |
223 end |
223 end |
224 *} |
224 *} |
225 |
225 |
226 |
226 ML {* val restricted_nominal=ref true *} |
227 ML {* |
227 |
|
228 ML {* |
228 fun nominal_datatype2 dts bn_funs bn_eqs binds lthy = |
229 fun nominal_datatype2 dts bn_funs bn_eqs binds lthy = |
229 let |
230 let |
230 val thy = ProofContext.theory_of lthy |
231 val thy = ProofContext.theory_of lthy |
231 val thy_name = Context.theory_name thy |
232 val thy_name = Context.theory_name thy |
232 val (((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_binds), lthy2) = |
233 val (((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_binds), lthy2) = |
238 val descr = #descr dtinfo; |
239 val descr = #descr dtinfo; |
239 val sorts = #sorts dtinfo; |
240 val sorts = #sorts dtinfo; |
240 val all_typs = map (fn i => typ_of_dtyp descr sorts (DtRec i)) (map fst descr) |
241 val all_typs = map (fn i => typ_of_dtyp descr sorts (DtRec i)) (map fst descr) |
241 val all_full_tnames = map (fn (_, (n, _, _)) => n) descr; |
242 val all_full_tnames = map (fn (_, (n, _, _)) => n) descr; |
242 val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy2)) all_full_tnames; |
243 val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy2)) all_full_tnames; |
|
244 val rel_dtinfos = List.take (dtinfos, (length dts)); |
243 val inject = flat (map #inject dtinfos); |
245 val inject = flat (map #inject dtinfos); |
244 val distinct = flat (map #distinct dtinfos); |
246 val distinct = flat (map #distinct dtinfos); |
|
247 val rel_distinct = map #distinct rel_dtinfos; |
245 val induct = #induct dtinfo; |
248 val induct = #induct dtinfo; |
246 val inducts = #inducts dtinfo; |
249 val inducts = #inducts dtinfo; |
247 val ((raw_perm_def, raw_perm_simps, perms), lthy3) = |
250 val ((raw_perm_def, raw_perm_simps, perms), lthy3) = |
248 Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2; |
251 Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2; |
249 val raw_binds_flat = map (map flat) raw_binds; |
252 val raw_binds_flat = map (map flat) raw_binds; |
259 val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo); |
262 val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo); |
260 val bn_tys = map (domain_type o fastype_of) raw_bn_funs; |
263 val bn_tys = map (domain_type o fastype_of) raw_bn_funs; |
261 val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys; |
264 val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys; |
262 val bns = raw_bn_funs ~~ bn_nos; |
265 val bns = raw_bn_funs ~~ bn_nos; |
263 val alpha_intros = #intrs alpha; |
266 val alpha_intros = #intrs alpha; |
264 val alpha_cases = #elims alpha |
267 val alpha_cases_loc = #elims alpha |
265 val alpha_inj_loc = build_alpha_inj alpha_intros (inject @ distinct) alpha_cases lthy4 |
268 val alpha_cases = ProofContext.export lthy4 lthy3 alpha_cases_loc |
|
269 val alpha_inj_loc = build_alpha_inj alpha_intros (inject @ distinct) alpha_cases_loc lthy4 |
266 val alpha_inj = ProofContext.export lthy4 lthy3 alpha_inj_loc |
270 val alpha_inj = ProofContext.export lthy4 lthy3 alpha_inj_loc |
267 (* val (bv_eqvts, lthy5) = fold_map (build_bv_eqvt perms (raw_bn_eqs @ raw_perm_def) inducts) bns lthy4; |
271 in |
|
272 if !restricted_nominal then |
|
273 ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy4) |
|
274 else |
|
275 let |
|
276 val (bv_eqvts, lthy5) = fold_map (build_bv_eqvt perms (raw_bn_eqs @ raw_perm_def) inducts) bns lthy4; |
268 val (fv_eqvts, lthy6) = build_eqvts Binding.empty fv_ts_loc perms |
277 val (fv_eqvts, lthy6) = build_eqvts Binding.empty fv_ts_loc perms |
269 ((flat (map snd bv_eqvts)) @ fv_def_loc @ raw_perm_def) induct lthy5; |
278 ((flat (map snd bv_eqvts)) @ fv_def_loc @ raw_perm_def) induct lthy5; |
|
279 val raw_fv_bv_eqvt_loc = flat (map snd bv_eqvts) @ (snd fv_eqvts) |
|
280 val raw_fv_bv_eqvt = ProofContext.export lthy6 lthy3 raw_fv_bv_eqvt_loc; |
270 val alpha_eqvt_loc = build_alpha_eqvts alpha_ts_loc perms |
281 val alpha_eqvt_loc = build_alpha_eqvts alpha_ts_loc perms |
271 (raw_perm_def @ alpha_inj_loc) alpha_induct_loc lthy6; |
282 (raw_perm_def @ alpha_inj_loc) alpha_induct_loc lthy6; |
272 val alpha_eqvt = ProofContext.export lthy6 lthy2 alpha_eqvt_loc; |
283 val alpha_eqvt = ProofContext.export lthy6 lthy2 alpha_eqvt_loc; |
273 val alpha_equivp_loc = map (equivp_hack lthy6) alpha_ts_loc |
284 val alpha_equivp_loc = map (equivp_hack lthy6) alpha_ts_loc |
274 val alpha_equivp_loc = build_equivps alpha_ts_loc induct alpha_induct_loc |
285 val alpha_equivp_loc = build_equivps alpha_ts_loc induct alpha_induct_loc |
275 inject alpha_inj_loc distinct alpha_cases alpha_eqvt_loc lthy6; |
286 inject alpha_inj_loc distinct alpha_cases_loc alpha_eqvt_loc lthy6; |
276 val alpha_equivp = ProofContext.export lthy6 lthy2 alpha_equivp_loc; |
287 val alpha_equivp = ProofContext.export lthy6 lthy2 alpha_equivp_loc; |
277 val qty_binds = map (fn (_, b, _, _) => b) dts; |
288 val qty_binds = map (fn (_, b, _, _) => b) dts; |
278 val qty_names = map Name.of_binding qty_binds; |
289 val qty_names = map Name.of_binding qty_binds; |
279 val qty_full_names = map (Long_Name.qualify thy_name) qty_names |
290 val qty_full_names = map (Long_Name.qualify thy_name) qty_names |
280 val lthy7 = define_quotient_type |
291 val lthy7 = define_quotient_type |
316 val q_bn = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy16, th))) raw_bn_eqs; |
327 val q_bn = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy16, th))) raw_bn_eqs; |
317 val (_, lthy17) = Local_Theory.note ((Binding.name (q_name ^ "_bn"), []), q_bn) lthy16; |
328 val (_, lthy17) = Local_Theory.note ((Binding.name (q_name ^ "_bn"), []), q_bn) lthy16; |
318 val inj_unfolded = map (LocalDefs.unfold lthy17 @{thms alpha_gen}) alpha_inj |
329 val inj_unfolded = map (LocalDefs.unfold lthy17 @{thms alpha_gen}) alpha_inj |
319 val q_inj_pre = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy17, th))) inj_unfolded; |
330 val q_inj_pre = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy17, th))) inj_unfolded; |
320 val q_inj = map (LocalDefs.fold lthy17 @{thms alpha_gen}) q_inj_pre |
331 val q_inj = map (LocalDefs.fold lthy17 @{thms alpha_gen}) q_inj_pre |
321 val (_, lthy18) = Local_Theory.note ((Binding.name (q_name ^ "_inject"), []), q_inj) lthy17;*) |
332 val (_, lthy18) = Local_Theory.note ((Binding.name (q_name ^ "_inject"), []), q_inj) lthy17; |
322 in |
333 val rel_dists = flat (map (distinct_rel lthy18 alpha_cases) |
323 ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy4) |
334 (rel_distinct ~~ (List.take (alpha_ts, (length dts))))) |
324 end |
335 val q_dis = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy18, th))) rel_dists; |
325 *} |
336 val (_, lthy19) = Local_Theory.note ((Binding.name (q_name ^ "_distinct"), []), q_dis) lthy18; |
326 ML fold |
337 val q_eqvt = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy19, th))) raw_fv_bv_eqvt; |
327 ML name_of_typ |
338 val (_, lthy20) = Local_Theory.note ((Binding.empty, |
|
339 [Attrib.internal (fn _ => Nominal_ThmDecls.eqvt_add)]), q_eqvt) lthy19; |
|
340 in |
|
341 ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy20) |
|
342 end |
|
343 end |
|
344 *} |
328 |
345 |
329 ML {* |
346 ML {* |
330 (* parsing the datatypes and declaring *) |
347 (* parsing the datatypes and declaring *) |
331 (* constructors in the local theory *) |
348 (* constructors in the local theory *) |
332 fun prepare_dts dt_strs lthy = |
349 fun prepare_dts dt_strs lthy = |