1.1 --- a/src/HOL/BNF/Tools/ctr_sugar.ML Thu Dec 05 17:52:12 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,956 +0,0 @@
1.4 -(* Title: HOL/BNF/Tools/ctr_sugar.ML
1.5 - Author: Jasmin Blanchette, TU Muenchen
1.6 - Copyright 2012
1.7 -
1.8 -Wrapping existing freely generated type's constructors.
1.9 -*)
1.10 -
1.11 -signature CTR_SUGAR =
1.12 -sig
1.13 - type ctr_sugar =
1.14 - {ctrs: term list,
1.15 - casex: term,
1.16 - discs: term list,
1.17 - selss: term list list,
1.18 - exhaust: thm,
1.19 - nchotomy: thm,
1.20 - injects: thm list,
1.21 - distincts: thm list,
1.22 - case_thms: thm list,
1.23 - case_cong: thm,
1.24 - weak_case_cong: thm,
1.25 - split: thm,
1.26 - split_asm: thm,
1.27 - disc_thmss: thm list list,
1.28 - discIs: thm list,
1.29 - sel_thmss: thm list list,
1.30 - disc_exhausts: thm list,
1.31 - sel_exhausts: thm list,
1.32 - collapses: thm list,
1.33 - expands: thm list,
1.34 - sel_splits: thm list,
1.35 - sel_split_asms: thm list,
1.36 - case_conv_ifs: thm list};
1.37 -
1.38 - val morph_ctr_sugar: morphism -> ctr_sugar -> ctr_sugar
1.39 - val ctr_sugar_of: Proof.context -> string -> ctr_sugar option
1.40 - val ctr_sugars_of: Proof.context -> ctr_sugar list
1.41 -
1.42 - val rep_compat_prefix: string
1.43 -
1.44 - val mk_half_pairss: 'a list * 'a list -> ('a * 'a) list list
1.45 - val join_halves: int -> 'a list list -> 'a list list -> 'a list * 'a list list list
1.46 -
1.47 - val mk_ctr: typ list -> term -> term
1.48 - val mk_case: typ list -> typ -> term -> term
1.49 - val mk_disc_or_sel: typ list -> term -> term
1.50 - val name_of_ctr: term -> string
1.51 - val name_of_disc: term -> string
1.52 - val dest_ctr: Proof.context -> string -> term -> term * term list
1.53 - val dest_case: Proof.context -> string -> typ list -> term -> (term list * term list) option
1.54 -
1.55 - val wrap_free_constructors: ({prems: thm list, context: Proof.context} -> tactic) list list ->
1.56 - (((bool * bool) * term list) * binding) *
1.57 - (binding list * (binding list list * (binding * term) list list)) -> local_theory ->
1.58 - ctr_sugar * local_theory
1.59 - val parse_wrap_free_constructors_options: (bool * bool) parser
1.60 - val parse_bound_term: (binding * string) parser
1.61 -end;
1.62 -
1.63 -structure Ctr_Sugar : CTR_SUGAR =
1.64 -struct
1.65 -
1.66 -open Ctr_Sugar_Util
1.67 -open Ctr_Sugar_Tactics
1.68 -
1.69 -type ctr_sugar =
1.70 - {ctrs: term list,
1.71 - casex: term,
1.72 - discs: term list,
1.73 - selss: term list list,
1.74 - exhaust: thm,
1.75 - nchotomy: thm,
1.76 - injects: thm list,
1.77 - distincts: thm list,
1.78 - case_thms: thm list,
1.79 - case_cong: thm,
1.80 - weak_case_cong: thm,
1.81 - split: thm,
1.82 - split_asm: thm,
1.83 - disc_thmss: thm list list,
1.84 - discIs: thm list,
1.85 - sel_thmss: thm list list,
1.86 - disc_exhausts: thm list,
1.87 - sel_exhausts: thm list,
1.88 - collapses: thm list,
1.89 - expands: thm list,
1.90 - sel_splits: thm list,
1.91 - sel_split_asms: thm list,
1.92 - case_conv_ifs: thm list};
1.93 -
1.94 -fun eq_ctr_sugar ({ctrs = ctrs1, casex = case1, discs = discs1, selss = selss1, ...} : ctr_sugar,
1.95 - {ctrs = ctrs2, casex = case2, discs = discs2, selss = selss2, ...} : ctr_sugar) =
1.96 - ctrs1 = ctrs2 andalso case1 = case2 andalso discs1 = discs2 andalso selss1 = selss2;
1.97 -
1.98 -fun morph_ctr_sugar phi {ctrs, casex, discs, selss, exhaust, nchotomy, injects, distincts,
1.99 - case_thms, case_cong, weak_case_cong, split, split_asm, disc_thmss, discIs, sel_thmss,
1.100 - disc_exhausts, sel_exhausts, collapses, expands, sel_splits, sel_split_asms, case_conv_ifs} =
1.101 - {ctrs = map (Morphism.term phi) ctrs,
1.102 - casex = Morphism.term phi casex,
1.103 - discs = map (Morphism.term phi) discs,
1.104 - selss = map (map (Morphism.term phi)) selss,
1.105 - exhaust = Morphism.thm phi exhaust,
1.106 - nchotomy = Morphism.thm phi nchotomy,
1.107 - injects = map (Morphism.thm phi) injects,
1.108 - distincts = map (Morphism.thm phi) distincts,
1.109 - case_thms = map (Morphism.thm phi) case_thms,
1.110 - case_cong = Morphism.thm phi case_cong,
1.111 - weak_case_cong = Morphism.thm phi weak_case_cong,
1.112 - split = Morphism.thm phi split,
1.113 - split_asm = Morphism.thm phi split_asm,
1.114 - disc_thmss = map (map (Morphism.thm phi)) disc_thmss,
1.115 - discIs = map (Morphism.thm phi) discIs,
1.116 - sel_thmss = map (map (Morphism.thm phi)) sel_thmss,
1.117 - disc_exhausts = map (Morphism.thm phi) disc_exhausts,
1.118 - sel_exhausts = map (Morphism.thm phi) sel_exhausts,
1.119 - collapses = map (Morphism.thm phi) collapses,
1.120 - expands = map (Morphism.thm phi) expands,
1.121 - sel_splits = map (Morphism.thm phi) sel_splits,
1.122 - sel_split_asms = map (Morphism.thm phi) sel_split_asms,
1.123 - case_conv_ifs = map (Morphism.thm phi) case_conv_ifs};
1.124 -
1.125 -val transfer_ctr_sugar =
1.126 - morph_ctr_sugar o Morphism.thm_morphism o Thm.transfer o Proof_Context.theory_of;
1.127 -
1.128 -structure Data = Generic_Data
1.129 -(
1.130 - type T = ctr_sugar Symtab.table;
1.131 - val empty = Symtab.empty;
1.132 - val extend = I;
1.133 - val merge = Symtab.merge eq_ctr_sugar;
1.134 -);
1.135 -
1.136 -fun ctr_sugar_of ctxt =
1.137 - Symtab.lookup (Data.get (Context.Proof ctxt))
1.138 - #> Option.map (transfer_ctr_sugar ctxt);
1.139 -
1.140 -fun ctr_sugars_of ctxt =
1.141 - Symtab.fold (cons o transfer_ctr_sugar ctxt o snd) (Data.get (Context.Proof ctxt)) [];
1.142 -
1.143 -fun register_ctr_sugar key ctr_sugar =
1.144 - Local_Theory.declaration {syntax = false, pervasive = true}
1.145 - (fn phi => Data.map (Symtab.default (key, morph_ctr_sugar phi ctr_sugar)));
1.146 -
1.147 -val rep_compat_prefix = "new";
1.148 -
1.149 -val isN = "is_";
1.150 -val unN = "un_";
1.151 -fun mk_unN 1 1 suf = unN ^ suf
1.152 - | mk_unN _ l suf = unN ^ suf ^ string_of_int l;
1.153 -
1.154 -val caseN = "case";
1.155 -val case_congN = "case_cong";
1.156 -val case_conv_ifN = "case_conv_if";
1.157 -val collapseN = "collapse";
1.158 -val disc_excludeN = "disc_exclude";
1.159 -val disc_exhaustN = "disc_exhaust";
1.160 -val discN = "disc";
1.161 -val discIN = "discI";
1.162 -val distinctN = "distinct";
1.163 -val exhaustN = "exhaust";
1.164 -val expandN = "expand";
1.165 -val injectN = "inject";
1.166 -val nchotomyN = "nchotomy";
1.167 -val selN = "sel";
1.168 -val sel_exhaustN = "sel_exhaust";
1.169 -val sel_splitN = "sel_split";
1.170 -val sel_split_asmN = "sel_split_asm";
1.171 -val splitN = "split";
1.172 -val splitsN = "splits";
1.173 -val split_asmN = "split_asm";
1.174 -val weak_case_cong_thmsN = "weak_case_cong";
1.175 -
1.176 -val cong_attrs = @{attributes [cong]};
1.177 -val dest_attrs = @{attributes [dest]};
1.178 -val safe_elim_attrs = @{attributes [elim!]};
1.179 -val iff_attrs = @{attributes [iff]};
1.180 -val induct_simp_attrs = @{attributes [induct_simp]};
1.181 -val nitpick_attrs = @{attributes [nitpick_simp]};
1.182 -val simp_attrs = @{attributes [simp]};
1.183 -val code_nitpick_simp_simp_attrs = Code.add_default_eqn_attrib :: nitpick_attrs @ simp_attrs;
1.184 -
1.185 -fun unflat_lookup eq xs ys = map (fn xs' => permute_like eq xs xs' ys);
1.186 -
1.187 -fun mk_half_pairss' _ ([], []) = []
1.188 - | mk_half_pairss' indent (x :: xs, _ :: ys) =
1.189 - indent @ fold_rev (cons o single o pair x) ys (mk_half_pairss' ([] :: indent) (xs, ys));
1.190 -
1.191 -fun mk_half_pairss p = mk_half_pairss' [[]] p;
1.192 -
1.193 -fun join_halves n half_xss other_half_xss =
1.194 - let
1.195 - val xsss =
1.196 - map2 (map2 append) (Library.chop_groups n half_xss)
1.197 - (transpose (Library.chop_groups n other_half_xss))
1.198 - val xs = splice (flat half_xss) (flat other_half_xss);
1.199 - in (xs, xsss) end;
1.200 -
1.201 -fun mk_undefined T = Const (@{const_name undefined}, T);
1.202 -
1.203 -fun mk_ctr Ts t =
1.204 - let val Type (_, Ts0) = body_type (fastype_of t) in
1.205 - Term.subst_atomic_types (Ts0 ~~ Ts) t
1.206 - end;
1.207 -
1.208 -fun mk_case Ts T t =
1.209 - let val (Type (_, Ts0), body) = strip_type (fastype_of t) |>> List.last in
1.210 - Term.subst_atomic_types ((body, T) :: (Ts0 ~~ Ts)) t
1.211 - end;
1.212 -
1.213 -fun mk_disc_or_sel Ts t =
1.214 - Term.subst_atomic_types (snd (Term.dest_Type (domain_type (fastype_of t))) ~~ Ts) t;
1.215 -
1.216 -fun name_of_const what t =
1.217 - (case head_of t of
1.218 - Const (s, _) => s
1.219 - | Free (s, _) => s
1.220 - | _ => error ("Cannot extract name of " ^ what));
1.221 -
1.222 -val name_of_ctr = name_of_const "constructor";
1.223 -
1.224 -val notN = "not_";
1.225 -val eqN = "eq_";
1.226 -val neqN = "neq_";
1.227 -
1.228 -fun name_of_disc t =
1.229 - (case head_of t of
1.230 - Abs (_, _, @{const Not} $ (t' $ Bound 0)) =>
1.231 - Long_Name.map_base_name (prefix notN) (name_of_disc t')
1.232 - | Abs (_, _, Const (@{const_name HOL.eq}, _) $ Bound 0 $ t') =>
1.233 - Long_Name.map_base_name (prefix eqN) (name_of_disc t')
1.234 - | Abs (_, _, @{const Not} $ (Const (@{const_name HOL.eq}, _) $ Bound 0 $ t')) =>
1.235 - Long_Name.map_base_name (prefix neqN) (name_of_disc t')
1.236 - | t' => name_of_const "destructor" t');
1.237 -
1.238 -val base_name_of_ctr = Long_Name.base_name o name_of_ctr;
1.239 -
1.240 -fun dest_ctr ctxt s t =
1.241 - let
1.242 - val (f, args) = Term.strip_comb t;
1.243 - in
1.244 - (case ctr_sugar_of ctxt s of
1.245 - SOME {ctrs, ...} =>
1.246 - (case find_first (can (fo_match ctxt f)) ctrs of
1.247 - SOME f' => (f', args)
1.248 - | NONE => raise Fail "dest_ctr")
1.249 - | NONE => raise Fail "dest_ctr")
1.250 - end;
1.251 -
1.252 -fun dest_case ctxt s Ts t =
1.253 - (case Term.strip_comb t of
1.254 - (Const (c, _), args as _ :: _) =>
1.255 - (case ctr_sugar_of ctxt s of
1.256 - SOME {casex = Const (case_name, _), discs = discs0, selss = selss0, ...} =>
1.257 - if case_name = c then
1.258 - let val n = length discs0 in
1.259 - if n < length args then
1.260 - let
1.261 - val (branches, obj :: leftovers) = chop n args;
1.262 - val discs = map (mk_disc_or_sel Ts) discs0;
1.263 - val selss = map (map (mk_disc_or_sel Ts)) selss0;
1.264 - val conds = map (rapp obj) discs;
1.265 - val branch_argss = map (fn sels => map (rapp obj) sels @ leftovers) selss;
1.266 - val branches' = map2 (curry Term.betapplys) branches branch_argss;
1.267 - in
1.268 - SOME (conds, branches')
1.269 - end
1.270 - else
1.271 - NONE
1.272 - end
1.273 - else
1.274 - NONE
1.275 - | _ => NONE)
1.276 - | _ => NONE);
1.277 -
1.278 -fun eta_expand_arg xs f_xs = fold_rev Term.lambda xs f_xs;
1.279 -
1.280 -fun prepare_wrap_free_constructors prep_term ((((no_discs_sels, rep_compat), raw_ctrs),
1.281 - raw_case_binding), (raw_disc_bindings, (raw_sel_bindingss, raw_sel_defaultss))) no_defs_lthy =
1.282 - let
1.283 - (* TODO: sanity checks on arguments *)
1.284 -
1.285 - val n = length raw_ctrs;
1.286 - val ks = 1 upto n;
1.287 -
1.288 - val _ = if n > 0 then () else error "No constructors specified";
1.289 -
1.290 - val ctrs0 = map (prep_term no_defs_lthy) raw_ctrs;
1.291 - val sel_defaultss =
1.292 - pad_list [] n (map (map (apsnd (prep_term no_defs_lthy))) raw_sel_defaultss);
1.293 -
1.294 - val Type (fcT_name, As0) = body_type (fastype_of (hd ctrs0));
1.295 - val fc_b_name = Long_Name.base_name fcT_name;
1.296 - val fc_b = Binding.name fc_b_name;
1.297 -
1.298 - fun qualify mandatory =
1.299 - Binding.qualify mandatory fc_b_name o (rep_compat ? Binding.qualify false rep_compat_prefix);
1.300 -
1.301 - fun dest_TFree_or_TVar (TFree p) = p
1.302 - | dest_TFree_or_TVar (TVar ((s, _), S)) = (s, S)
1.303 - | dest_TFree_or_TVar _ = error "Invalid type argument";
1.304 -
1.305 - val (unsorted_As, B) =
1.306 - no_defs_lthy
1.307 - |> variant_tfrees (map (fst o dest_TFree_or_TVar) As0)
1.308 - ||> the_single o fst o mk_TFrees 1;
1.309 -
1.310 - val As = map2 (resort_tfree o snd o dest_TFree_or_TVar) As0 unsorted_As;
1.311 -
1.312 - val fcT = Type (fcT_name, As);
1.313 - val ctrs = map (mk_ctr As) ctrs0;
1.314 - val ctr_Tss = map (binder_types o fastype_of) ctrs;
1.315 -
1.316 - val ms = map length ctr_Tss;
1.317 -
1.318 - val raw_disc_bindings' = pad_list Binding.empty n raw_disc_bindings;
1.319 -
1.320 - fun can_definitely_rely_on_disc k = not (Binding.is_empty (nth raw_disc_bindings' (k - 1)));
1.321 - fun can_rely_on_disc k =
1.322 - can_definitely_rely_on_disc k orelse (k = 1 andalso not (can_definitely_rely_on_disc 2));
1.323 - fun should_omit_disc_binding k = n = 1 orelse (n = 2 andalso can_rely_on_disc (3 - k));
1.324 -
1.325 - fun is_disc_binding_valid b =
1.326 - not (Binding.is_empty b orelse Binding.eq_name (b, equal_binding));
1.327 -
1.328 - val standard_disc_binding = Binding.name o prefix isN o base_name_of_ctr;
1.329 -
1.330 - val disc_bindings =
1.331 - raw_disc_bindings'
1.332 - |> map4 (fn k => fn m => fn ctr => fn disc =>
1.333 - qualify false
1.334 - (if Binding.is_empty disc then
1.335 - if should_omit_disc_binding k then disc else standard_disc_binding ctr
1.336 - else if Binding.eq_name (disc, equal_binding) then
1.337 - if m = 0 then disc
1.338 - else error "Cannot use \"=\" syntax for discriminating nonnullary constructor"
1.339 - else if Binding.eq_name (disc, standard_binding) then
1.340 - standard_disc_binding ctr
1.341 - else
1.342 - disc)) ks ms ctrs0;
1.343 -
1.344 - fun standard_sel_binding m l = Binding.name o mk_unN m l o base_name_of_ctr;
1.345 -
1.346 - val sel_bindingss =
1.347 - pad_list [] n raw_sel_bindingss
1.348 - |> map3 (fn ctr => fn m => map2 (fn l => fn sel =>
1.349 - qualify false
1.350 - (if Binding.is_empty sel orelse Binding.eq_name (sel, standard_binding) then
1.351 - standard_sel_binding m l ctr
1.352 - else
1.353 - sel)) (1 upto m) o pad_list Binding.empty m) ctrs0 ms;
1.354 -
1.355 - val case_Ts = map (fn Ts => Ts ---> B) ctr_Tss;
1.356 -
1.357 - val ((((((((xss, xss'), yss), fs), gs), [u', v']), [w]), (p, p')), names_lthy) = no_defs_lthy |>
1.358 - mk_Freess' "x" ctr_Tss
1.359 - ||>> mk_Freess "y" ctr_Tss
1.360 - ||>> mk_Frees "f" case_Ts
1.361 - ||>> mk_Frees "g" case_Ts
1.362 - ||>> (apfst (map (rpair fcT)) oo Variable.variant_fixes) [fc_b_name, fc_b_name ^ "'"]
1.363 - ||>> mk_Frees "z" [B]
1.364 - ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "P") HOLogic.boolT;
1.365 -
1.366 - val u = Free u';
1.367 - val v = Free v';
1.368 - val q = Free (fst p', mk_pred1T B);
1.369 -
1.370 - val xctrs = map2 (curry Term.list_comb) ctrs xss;
1.371 - val yctrs = map2 (curry Term.list_comb) ctrs yss;
1.372 -
1.373 - val xfs = map2 (curry Term.list_comb) fs xss;
1.374 - val xgs = map2 (curry Term.list_comb) gs xss;
1.375 -
1.376 - (* TODO: Eta-expension is for compatibility with the old datatype package (but it also provides
1.377 - nicer names). Consider removing. *)
1.378 - val eta_fs = map2 eta_expand_arg xss xfs;
1.379 - val eta_gs = map2 eta_expand_arg xss xgs;
1.380 -
1.381 - val case_binding =
1.382 - qualify false
1.383 - (if Binding.is_empty raw_case_binding orelse
1.384 - Binding.eq_name (raw_case_binding, standard_binding) then
1.385 - Binding.suffix_name ("_" ^ caseN) fc_b
1.386 - else
1.387 - raw_case_binding);
1.388 -
1.389 - fun mk_case_disj xctr xf xs =
1.390 - list_exists_free xs (HOLogic.mk_conj (HOLogic.mk_eq (u, xctr), HOLogic.mk_eq (w, xf)));
1.391 -
1.392 - val case_rhs = fold_rev (fold_rev Term.lambda) [fs, [u]]
1.393 - (Const (@{const_name The}, (B --> HOLogic.boolT) --> B) $
1.394 - Term.lambda w (Library.foldr1 HOLogic.mk_disj (map3 mk_case_disj xctrs xfs xss)));
1.395 -
1.396 - val ((raw_case, (_, raw_case_def)), (lthy', lthy)) = no_defs_lthy
1.397 - |> Local_Theory.define ((case_binding, NoSyn), ((Thm.def_binding case_binding, []), case_rhs))
1.398 - ||> `Local_Theory.restore;
1.399 -
1.400 - val phi = Proof_Context.export_morphism lthy lthy';
1.401 -
1.402 - val case_def = Morphism.thm phi raw_case_def;
1.403 -
1.404 - val case0 = Morphism.term phi raw_case;
1.405 - val casex = mk_case As B case0;
1.406 -
1.407 - val fcase = Term.list_comb (casex, fs);
1.408 -
1.409 - val ufcase = fcase $ u;
1.410 - val vfcase = fcase $ v;
1.411 -
1.412 - val eta_fcase = Term.list_comb (casex, eta_fs);
1.413 - val eta_gcase = Term.list_comb (casex, eta_gs);
1.414 -
1.415 - val eta_ufcase = eta_fcase $ u;
1.416 - val eta_vgcase = eta_gcase $ v;
1.417 -
1.418 - fun mk_uu_eq () = HOLogic.mk_eq (u, u);
1.419 -
1.420 - val uv_eq = mk_Trueprop_eq (u, v);
1.421 -
1.422 - val exist_xs_u_eq_ctrs =
1.423 - map2 (fn xctr => fn xs => list_exists_free xs (HOLogic.mk_eq (u, xctr))) xctrs xss;
1.424 -
1.425 - val unique_disc_no_def = TrueI; (*arbitrary marker*)
1.426 - val alternate_disc_no_def = FalseE; (*arbitrary marker*)
1.427 -
1.428 - fun alternate_disc_lhs get_udisc k =
1.429 - HOLogic.mk_not
1.430 - (let val b = nth disc_bindings (k - 1) in
1.431 - if is_disc_binding_valid b then get_udisc b (k - 1) else nth exist_xs_u_eq_ctrs (k - 1)
1.432 - end);
1.433 -
1.434 - val (all_sels_distinct, discs, selss, disc_defs, sel_defs, sel_defss, lthy') =
1.435 - if no_discs_sels then
1.436 - (true, [], [], [], [], [], lthy)
1.437 - else
1.438 - let
1.439 - fun disc_free b = Free (Binding.name_of b, mk_pred1T fcT);
1.440 -
1.441 - fun disc_spec b exist_xs_u_eq_ctr = mk_Trueprop_eq (disc_free b $ u, exist_xs_u_eq_ctr);
1.442 -
1.443 - fun alternate_disc k =
1.444 - Term.lambda u (alternate_disc_lhs (K o rapp u o disc_free) (3 - k));
1.445 -
1.446 - fun mk_sel_case_args b proto_sels T =
1.447 - map2 (fn Ts => fn k =>
1.448 - (case AList.lookup (op =) proto_sels k of
1.449 - NONE =>
1.450 - (case AList.lookup Binding.eq_name (rev (nth sel_defaultss (k - 1))) b of
1.451 - NONE => fold_rev (Term.lambda o curry Free Name.uu) Ts (mk_undefined T)
1.452 - | SOME t => t |> Type.constraint (Ts ---> T) |> Syntax.check_term lthy)
1.453 - | SOME (xs, x) => fold_rev Term.lambda xs x)) ctr_Tss ks;
1.454 -
1.455 - fun sel_spec b proto_sels =
1.456 - let
1.457 - val _ =
1.458 - (case duplicates (op =) (map fst proto_sels) of
1.459 - k :: _ => error ("Duplicate selector name " ^ quote (Binding.name_of b) ^
1.460 - " for constructor " ^
1.461 - quote (Syntax.string_of_term lthy (nth ctrs (k - 1))))
1.462 - | [] => ())
1.463 - val T =
1.464 - (case distinct (op =) (map (fastype_of o snd o snd) proto_sels) of
1.465 - [T] => T
1.466 - | T :: T' :: _ => error ("Inconsistent range type for selector " ^
1.467 - quote (Binding.name_of b) ^ ": " ^ quote (Syntax.string_of_typ lthy T) ^ " vs. "
1.468 - ^ quote (Syntax.string_of_typ lthy T')));
1.469 - in
1.470 - mk_Trueprop_eq (Free (Binding.name_of b, fcT --> T) $ u,
1.471 - Term.list_comb (mk_case As T case0, mk_sel_case_args b proto_sels T) $ u)
1.472 - end;
1.473 -
1.474 - val sel_bindings = flat sel_bindingss;
1.475 - val uniq_sel_bindings = distinct Binding.eq_name sel_bindings;
1.476 - val all_sels_distinct = (length uniq_sel_bindings = length sel_bindings);
1.477 -
1.478 - val sel_binding_index =
1.479 - if all_sels_distinct then 1 upto length sel_bindings
1.480 - else map (fn b => find_index (curry Binding.eq_name b) uniq_sel_bindings) sel_bindings;
1.481 -
1.482 - val proto_sels = flat (map3 (fn k => fn xs => map (fn x => (k, (xs, x)))) ks xss xss);
1.483 - val sel_infos =
1.484 - AList.group (op =) (sel_binding_index ~~ proto_sels)
1.485 - |> sort (int_ord o pairself fst)
1.486 - |> map snd |> curry (op ~~) uniq_sel_bindings;
1.487 - val sel_bindings = map fst sel_infos;
1.488 -
1.489 - fun unflat_selss xs = unflat_lookup Binding.eq_name sel_bindings xs sel_bindingss;
1.490 -
1.491 - val (((raw_discs, raw_disc_defs), (raw_sels, raw_sel_defs)), (lthy', lthy)) =
1.492 - lthy
1.493 - |> apfst split_list o fold_map3 (fn k => fn exist_xs_u_eq_ctr => fn b =>
1.494 - if Binding.is_empty b then
1.495 - if n = 1 then pair (Term.lambda u (mk_uu_eq ()), unique_disc_no_def)
1.496 - else pair (alternate_disc k, alternate_disc_no_def)
1.497 - else if Binding.eq_name (b, equal_binding) then
1.498 - pair (Term.lambda u exist_xs_u_eq_ctr, refl)
1.499 - else
1.500 - Specification.definition (SOME (b, NONE, NoSyn),
1.501 - ((Thm.def_binding b, []), disc_spec b exist_xs_u_eq_ctr)) #>> apsnd snd)
1.502 - ks exist_xs_u_eq_ctrs disc_bindings
1.503 - ||>> apfst split_list o fold_map (fn (b, proto_sels) =>
1.504 - Specification.definition (SOME (b, NONE, NoSyn),
1.505 - ((Thm.def_binding b, []), sel_spec b proto_sels)) #>> apsnd snd) sel_infos
1.506 - ||> `Local_Theory.restore;
1.507 -
1.508 - val phi = Proof_Context.export_morphism lthy lthy';
1.509 -
1.510 - val disc_defs = map (Morphism.thm phi) raw_disc_defs;
1.511 - val sel_defs = map (Morphism.thm phi) raw_sel_defs;
1.512 - val sel_defss = unflat_selss sel_defs;
1.513 -
1.514 - val discs0 = map (Morphism.term phi) raw_discs;
1.515 - val selss0 = unflat_selss (map (Morphism.term phi) raw_sels);
1.516 -
1.517 - val discs = map (mk_disc_or_sel As) discs0;
1.518 - val selss = map (map (mk_disc_or_sel As)) selss0;
1.519 - in
1.520 - (all_sels_distinct, discs, selss, disc_defs, sel_defs, sel_defss, lthy')
1.521 - end;
1.522 -
1.523 - fun mk_imp_p Qs = Logic.list_implies (Qs, HOLogic.mk_Trueprop p);
1.524 -
1.525 - val exhaust_goal =
1.526 - let fun mk_prem xctr xs = fold_rev Logic.all xs (mk_imp_p [mk_Trueprop_eq (u, xctr)]) in
1.527 - fold_rev Logic.all [p, u] (mk_imp_p (map2 mk_prem xctrs xss))
1.528 - end;
1.529 -
1.530 - val inject_goalss =
1.531 - let
1.532 - fun mk_goal _ _ [] [] = []
1.533 - | mk_goal xctr yctr xs ys =
1.534 - [fold_rev Logic.all (xs @ ys) (mk_Trueprop_eq (HOLogic.mk_eq (xctr, yctr),
1.535 - Library.foldr1 HOLogic.mk_conj (map2 (curry HOLogic.mk_eq) xs ys)))];
1.536 - in
1.537 - map4 mk_goal xctrs yctrs xss yss
1.538 - end;
1.539 -
1.540 - val half_distinct_goalss =
1.541 - let
1.542 - fun mk_goal ((xs, xc), (xs', xc')) =
1.543 - fold_rev Logic.all (xs @ xs')
1.544 - (HOLogic.mk_Trueprop (HOLogic.mk_not (HOLogic.mk_eq (xc, xc'))));
1.545 - in
1.546 - map (map mk_goal) (mk_half_pairss (`I (xss ~~ xctrs)))
1.547 - end;
1.548 -
1.549 - val goalss = [exhaust_goal] :: inject_goalss @ half_distinct_goalss;
1.550 -
1.551 - fun after_qed thmss lthy =
1.552 - let
1.553 - val ([exhaust_thm], (inject_thmss, half_distinct_thmss)) = (hd thmss, chop n (tl thmss));
1.554 -
1.555 - val inject_thms = flat inject_thmss;
1.556 -
1.557 - val rho_As = map (pairself (certifyT lthy)) (map Logic.varifyT_global As ~~ As);
1.558 -
1.559 - fun inst_thm t thm =
1.560 - Drule.instantiate' [] [SOME (certify lthy t)]
1.561 - (Thm.instantiate (rho_As, []) (Drule.zero_var_indexes thm));
1.562 -
1.563 - val uexhaust_thm = inst_thm u exhaust_thm;
1.564 -
1.565 - val exhaust_cases = map base_name_of_ctr ctrs;
1.566 -
1.567 - val other_half_distinct_thmss = map (map (fn thm => thm RS not_sym)) half_distinct_thmss;
1.568 -
1.569 - val (distinct_thms, (distinct_thmsss', distinct_thmsss)) =
1.570 - join_halves n half_distinct_thmss other_half_distinct_thmss ||> `transpose;
1.571 -
1.572 - val nchotomy_thm =
1.573 - let
1.574 - val goal =
1.575 - HOLogic.mk_Trueprop (HOLogic.mk_all (fst u', snd u',
1.576 - Library.foldr1 HOLogic.mk_disj exist_xs_u_eq_ctrs));
1.577 - in
1.578 - Goal.prove_sorry lthy [] [] goal (fn _ => mk_nchotomy_tac n exhaust_thm)
1.579 - |> Thm.close_derivation
1.580 - end;
1.581 -
1.582 - val case_thms =
1.583 - let
1.584 - val goals =
1.585 - map3 (fn xctr => fn xf => fn xs =>
1.586 - fold_rev Logic.all (fs @ xs) (mk_Trueprop_eq (fcase $ xctr, xf))) xctrs xfs xss;
1.587 - in
1.588 - map4 (fn k => fn goal => fn injects => fn distinctss =>
1.589 - Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} =>
1.590 - mk_case_tac ctxt n k case_def injects distinctss)
1.591 - |> Thm.close_derivation)
1.592 - ks goals inject_thmss distinct_thmsss
1.593 - end;
1.594 -
1.595 - val (case_cong_thm, weak_case_cong_thm) =
1.596 - let
1.597 - fun mk_prem xctr xs xf xg =
1.598 - fold_rev Logic.all xs (Logic.mk_implies (mk_Trueprop_eq (v, xctr),
1.599 - mk_Trueprop_eq (xf, xg)));
1.600 -
1.601 - val goal =
1.602 - Logic.list_implies (uv_eq :: map4 mk_prem xctrs xss xfs xgs,
1.603 - mk_Trueprop_eq (eta_ufcase, eta_vgcase));
1.604 - val weak_goal = Logic.mk_implies (uv_eq, mk_Trueprop_eq (ufcase, vfcase));
1.605 - in
1.606 - (Goal.prove_sorry lthy [] [] goal (fn _ => mk_case_cong_tac lthy uexhaust_thm case_thms),
1.607 - Goal.prove_sorry lthy [] [] weak_goal (K (etac arg_cong 1)))
1.608 - |> pairself (Thm.close_derivation #> singleton (Proof_Context.export names_lthy lthy))
1.609 - end;
1.610 -
1.611 - val split_lhs = q $ ufcase;
1.612 -
1.613 - fun mk_split_conjunct xctr xs f_xs =
1.614 - list_all_free xs (HOLogic.mk_imp (HOLogic.mk_eq (u, xctr), q $ f_xs));
1.615 - fun mk_split_disjunct xctr xs f_xs =
1.616 - list_exists_free xs (HOLogic.mk_conj (HOLogic.mk_eq (u, xctr),
1.617 - HOLogic.mk_not (q $ f_xs)));
1.618 -
1.619 - fun mk_split_goal xctrs xss xfs =
1.620 - mk_Trueprop_eq (split_lhs, Library.foldr1 HOLogic.mk_conj
1.621 - (map3 mk_split_conjunct xctrs xss xfs));
1.622 - fun mk_split_asm_goal xctrs xss xfs =
1.623 - mk_Trueprop_eq (split_lhs, HOLogic.mk_not (Library.foldr1 HOLogic.mk_disj
1.624 - (map3 mk_split_disjunct xctrs xss xfs)));
1.625 -
1.626 - fun prove_split selss goal =
1.627 - Goal.prove_sorry lthy [] [] goal (fn _ =>
1.628 - mk_split_tac lthy uexhaust_thm case_thms selss inject_thmss distinct_thmsss)
1.629 - |> Thm.close_derivation
1.630 - |> singleton (Proof_Context.export names_lthy lthy);
1.631 -
1.632 - fun prove_split_asm asm_goal split_thm =
1.633 - Goal.prove_sorry lthy [] [] asm_goal (fn {context = ctxt, ...} =>
1.634 - mk_split_asm_tac ctxt split_thm)
1.635 - |> Thm.close_derivation
1.636 - |> singleton (Proof_Context.export names_lthy lthy);
1.637 -
1.638 - val (split_thm, split_asm_thm) =
1.639 - let
1.640 - val goal = mk_split_goal xctrs xss xfs;
1.641 - val asm_goal = mk_split_asm_goal xctrs xss xfs;
1.642 -
1.643 - val thm = prove_split (replicate n []) goal;
1.644 - val asm_thm = prove_split_asm asm_goal thm;
1.645 - in
1.646 - (thm, asm_thm)
1.647 - end;
1.648 -
1.649 - val (all_sel_thms, sel_thmss, disc_thmss, nontriv_disc_thms, discI_thms, nontriv_discI_thms,
1.650 - disc_exclude_thms, disc_exhaust_thms, sel_exhaust_thms, all_collapse_thms,
1.651 - safe_collapse_thms, expand_thms, sel_split_thms, sel_split_asm_thms,
1.652 - case_conv_if_thms) =
1.653 - if no_discs_sels then
1.654 - ([], [], [], [], [], [], [], [], [], [], [], [], [], [], [])
1.655 - else
1.656 - let
1.657 - val udiscs = map (rapp u) discs;
1.658 - val uselss = map (map (rapp u)) selss;
1.659 - val usel_ctrs = map2 (curry Term.list_comb) ctrs uselss;
1.660 - val usel_fs = map2 (curry Term.list_comb) fs uselss;
1.661 -
1.662 - val vdiscs = map (rapp v) discs;
1.663 - val vselss = map (map (rapp v)) selss;
1.664 -
1.665 - fun make_sel_thm xs' case_thm sel_def =
1.666 - zero_var_indexes (Drule.gen_all (Drule.rename_bvars' (map (SOME o fst) xs')
1.667 - (Drule.forall_intr_vars (case_thm RS (sel_def RS trans)))));
1.668 -
1.669 - val sel_thmss = map3 (map oo make_sel_thm) xss' case_thms sel_defss;
1.670 -
1.671 - fun has_undefined_rhs thm =
1.672 - (case snd (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of thm))) of
1.673 - Const (@{const_name undefined}, _) => true
1.674 - | _ => false);
1.675 -
1.676 - val all_sel_thms =
1.677 - (if all_sels_distinct andalso forall null sel_defaultss then
1.678 - flat sel_thmss
1.679 - else
1.680 - map_product (fn s => fn (xs', c) => make_sel_thm xs' c s) sel_defs
1.681 - (xss' ~~ case_thms))
1.682 - |> filter_out has_undefined_rhs;
1.683 -
1.684 - fun mk_unique_disc_def () =
1.685 - let
1.686 - val m = the_single ms;
1.687 - val goal = mk_Trueprop_eq (mk_uu_eq (), the_single exist_xs_u_eq_ctrs);
1.688 - in
1.689 - Goal.prove_sorry lthy [] [] goal (fn _ => mk_unique_disc_def_tac m uexhaust_thm)
1.690 - |> Thm.close_derivation
1.691 - |> singleton (Proof_Context.export names_lthy lthy)
1.692 - end;
1.693 -
1.694 - fun mk_alternate_disc_def k =
1.695 - let
1.696 - val goal =
1.697 - mk_Trueprop_eq (alternate_disc_lhs (K (nth udiscs)) (3 - k),
1.698 - nth exist_xs_u_eq_ctrs (k - 1));
1.699 - in
1.700 - Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} =>
1.701 - mk_alternate_disc_def_tac ctxt k (nth disc_defs (2 - k))
1.702 - (nth distinct_thms (2 - k)) uexhaust_thm)
1.703 - |> Thm.close_derivation
1.704 - |> singleton (Proof_Context.export names_lthy lthy)
1.705 - end;
1.706 -
1.707 - val has_alternate_disc_def =
1.708 - exists (fn def => Thm.eq_thm_prop (def, alternate_disc_no_def)) disc_defs;
1.709 -
1.710 - val disc_defs' =
1.711 - map2 (fn k => fn def =>
1.712 - if Thm.eq_thm_prop (def, unique_disc_no_def) then mk_unique_disc_def ()
1.713 - else if Thm.eq_thm_prop (def, alternate_disc_no_def) then mk_alternate_disc_def k
1.714 - else def) ks disc_defs;
1.715 -
1.716 - val discD_thms = map (fn def => def RS iffD1) disc_defs';
1.717 - val discI_thms =
1.718 - map2 (fn m => fn def => funpow m (fn thm => exI RS thm) (def RS iffD2)) ms
1.719 - disc_defs';
1.720 - val not_discI_thms =
1.721 - map2 (fn m => fn def => funpow m (fn thm => allI RS thm)
1.722 - (unfold_thms lthy @{thms not_ex} (def RS @{thm ssubst[of _ _ Not]})))
1.723 - ms disc_defs';
1.724 -
1.725 - val (disc_thmss', disc_thmss) =
1.726 - let
1.727 - fun mk_thm discI _ [] = refl RS discI
1.728 - | mk_thm _ not_discI [distinct] = distinct RS not_discI;
1.729 - fun mk_thms discI not_discI distinctss = map (mk_thm discI not_discI) distinctss;
1.730 - in
1.731 - map3 mk_thms discI_thms not_discI_thms distinct_thmsss' |> `transpose
1.732 - end;
1.733 -
1.734 - val nontriv_disc_thms =
1.735 - flat (map2 (fn b => if is_disc_binding_valid b then I else K [])
1.736 - disc_bindings disc_thmss);
1.737 -
1.738 - fun is_discI_boring b =
1.739 - (n = 1 andalso Binding.is_empty b) orelse Binding.eq_name (b, equal_binding);
1.740 -
1.741 - val nontriv_discI_thms =
1.742 - flat (map2 (fn b => if is_discI_boring b then K [] else single) disc_bindings
1.743 - discI_thms);
1.744 -
1.745 - val (disc_exclude_thms, (disc_exclude_thmsss', disc_exclude_thmsss)) =
1.746 - let
1.747 - fun mk_goal [] = []
1.748 - | mk_goal [((_, udisc), (_, udisc'))] =
1.749 - [Logic.all u (Logic.mk_implies (HOLogic.mk_Trueprop udisc,
1.750 - HOLogic.mk_Trueprop (HOLogic.mk_not udisc')))];
1.751 -
1.752 - fun prove tac goal =
1.753 - Goal.prove_sorry lthy [] [] goal (K tac)
1.754 - |> Thm.close_derivation;
1.755 -
1.756 - val half_pairss = mk_half_pairss (`I (ms ~~ discD_thms ~~ udiscs));
1.757 -
1.758 - val half_goalss = map mk_goal half_pairss;
1.759 - val half_thmss =
1.760 - map3 (fn [] => K (K []) | [goal] => fn [(((m, discD), _), _)] =>
1.761 - fn disc_thm => [prove (mk_half_disc_exclude_tac lthy m discD disc_thm) goal])
1.762 - half_goalss half_pairss (flat disc_thmss');
1.763 -
1.764 - val other_half_goalss = map (mk_goal o map swap) half_pairss;
1.765 - val other_half_thmss =
1.766 - map2 (map2 (prove o mk_other_half_disc_exclude_tac)) half_thmss
1.767 - other_half_goalss;
1.768 - in
1.769 - join_halves n half_thmss other_half_thmss ||> `transpose
1.770 - |>> has_alternate_disc_def ? K []
1.771 - end;
1.772 -
1.773 - val disc_exhaust_thm =
1.774 - let
1.775 - fun mk_prem udisc = mk_imp_p [HOLogic.mk_Trueprop udisc];
1.776 - val goal = fold_rev Logic.all [p, u] (mk_imp_p (map mk_prem udiscs));
1.777 - in
1.778 - Goal.prove_sorry lthy [] [] goal (fn _ =>
1.779 - mk_disc_exhaust_tac n exhaust_thm discI_thms)
1.780 - |> Thm.close_derivation
1.781 - end;
1.782 -
1.783 - val (safe_collapse_thms, all_collapse_thms) =
1.784 - let
1.785 - fun mk_goal m udisc usel_ctr =
1.786 - let
1.787 - val prem = HOLogic.mk_Trueprop udisc;
1.788 - val concl = mk_Trueprop_eq ((usel_ctr, u) |> m = 0 ? swap);
1.789 - in
1.790 - (prem aconv concl, Logic.all u (Logic.mk_implies (prem, concl)))
1.791 - end;
1.792 - val (trivs, goals) = map3 mk_goal ms udiscs usel_ctrs |> split_list;
1.793 - val thms =
1.794 - map5 (fn m => fn discD => fn sel_thms => fn triv => fn goal =>
1.795 - Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} =>
1.796 - mk_collapse_tac ctxt m discD sel_thms ORELSE HEADGOAL atac)
1.797 - |> Thm.close_derivation
1.798 - |> not triv ? perhaps (try (fn thm => refl RS thm)))
1.799 - ms discD_thms sel_thmss trivs goals;
1.800 - in
1.801 - (map_filter (fn (true, _) => NONE | (false, thm) => SOME thm) (trivs ~~ thms),
1.802 - thms)
1.803 - end;
1.804 -
1.805 - val swapped_all_collapse_thms =
1.806 - map2 (fn m => fn thm => if m = 0 then thm else thm RS sym) ms all_collapse_thms;
1.807 -
1.808 - val sel_exhaust_thm =
1.809 - let
1.810 - fun mk_prem usel_ctr = mk_imp_p [mk_Trueprop_eq (u, usel_ctr)];
1.811 - val goal = fold_rev Logic.all [p, u] (mk_imp_p (map mk_prem usel_ctrs));
1.812 - in
1.813 - Goal.prove_sorry lthy [] [] goal (fn _ =>
1.814 - mk_sel_exhaust_tac n disc_exhaust_thm swapped_all_collapse_thms)
1.815 - |> Thm.close_derivation
1.816 - end;
1.817 -
1.818 - val expand_thm =
1.819 - let
1.820 - fun mk_prems k udisc usels vdisc vsels =
1.821 - (if k = n then [] else [mk_Trueprop_eq (udisc, vdisc)]) @
1.822 - (if null usels then
1.823 - []
1.824 - else
1.825 - [Logic.list_implies
1.826 - (if n = 1 then [] else map HOLogic.mk_Trueprop [udisc, vdisc],
1.827 - HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
1.828 - (map2 (curry HOLogic.mk_eq) usels vsels)))]);
1.829 -
1.830 - val goal =
1.831 - Library.foldr Logic.list_implies
1.832 - (map5 mk_prems ks udiscs uselss vdiscs vselss, uv_eq);
1.833 - val uncollapse_thms =
1.834 - map2 (fn thm => fn [] => thm | _ => thm RS sym) all_collapse_thms uselss;
1.835 - in
1.836 - Goal.prove_sorry lthy [] [] goal (fn _ =>
1.837 - mk_expand_tac lthy n ms (inst_thm u disc_exhaust_thm)
1.838 - (inst_thm v disc_exhaust_thm) uncollapse_thms disc_exclude_thmsss
1.839 - disc_exclude_thmsss')
1.840 - |> Thm.close_derivation
1.841 - |> singleton (Proof_Context.export names_lthy lthy)
1.842 - end;
1.843 -
1.844 - val (sel_split_thm, sel_split_asm_thm) =
1.845 - let
1.846 - val zss = map (K []) xss;
1.847 - val goal = mk_split_goal usel_ctrs zss usel_fs;
1.848 - val asm_goal = mk_split_asm_goal usel_ctrs zss usel_fs;
1.849 -
1.850 - val thm = prove_split sel_thmss goal;
1.851 - val asm_thm = prove_split_asm asm_goal thm;
1.852 - in
1.853 - (thm, asm_thm)
1.854 - end;
1.855 -
1.856 - val case_conv_if_thm =
1.857 - let
1.858 - val goal = mk_Trueprop_eq (ufcase, mk_IfN B udiscs usel_fs);
1.859 - in
1.860 - Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} =>
1.861 - mk_case_conv_if_tac ctxt n uexhaust_thm case_thms disc_thmss' sel_thmss)
1.862 - |> Thm.close_derivation
1.863 - |> singleton (Proof_Context.export names_lthy lthy)
1.864 - end;
1.865 - in
1.866 - (all_sel_thms, sel_thmss, disc_thmss, nontriv_disc_thms, discI_thms,
1.867 - nontriv_discI_thms, disc_exclude_thms, [disc_exhaust_thm], [sel_exhaust_thm],
1.868 - all_collapse_thms, safe_collapse_thms, [expand_thm], [sel_split_thm],
1.869 - [sel_split_asm_thm], [case_conv_if_thm])
1.870 - end;
1.871 -
1.872 - val exhaust_case_names_attr = Attrib.internal (K (Rule_Cases.case_names exhaust_cases));
1.873 - val cases_type_attr = Attrib.internal (K (Induct.cases_type fcT_name));
1.874 -
1.875 - val notes =
1.876 - [(caseN, case_thms, code_nitpick_simp_simp_attrs),
1.877 - (case_congN, [case_cong_thm], []),
1.878 - (case_conv_ifN, case_conv_if_thms, []),
1.879 - (collapseN, safe_collapse_thms, simp_attrs),
1.880 - (discN, nontriv_disc_thms, simp_attrs),
1.881 - (discIN, nontriv_discI_thms, []),
1.882 - (disc_excludeN, disc_exclude_thms, dest_attrs),
1.883 - (disc_exhaustN, disc_exhaust_thms, [exhaust_case_names_attr]),
1.884 - (distinctN, distinct_thms, simp_attrs @ induct_simp_attrs),
1.885 - (exhaustN, [exhaust_thm], [exhaust_case_names_attr, cases_type_attr]),
1.886 - (expandN, expand_thms, []),
1.887 - (injectN, inject_thms, iff_attrs @ induct_simp_attrs),
1.888 - (nchotomyN, [nchotomy_thm], []),
1.889 - (selN, all_sel_thms, code_nitpick_simp_simp_attrs),
1.890 - (sel_exhaustN, sel_exhaust_thms, [exhaust_case_names_attr]),
1.891 - (sel_splitN, sel_split_thms, []),
1.892 - (sel_split_asmN, sel_split_asm_thms, []),
1.893 - (splitN, [split_thm], []),
1.894 - (split_asmN, [split_asm_thm], []),
1.895 - (splitsN, [split_thm, split_asm_thm], []),
1.896 - (weak_case_cong_thmsN, [weak_case_cong_thm], cong_attrs)]
1.897 - |> filter_out (null o #2)
1.898 - |> map (fn (thmN, thms, attrs) =>
1.899 - ((qualify true (Binding.name thmN), attrs), [(thms, [])]));
1.900 -
1.901 - val notes' =
1.902 - [(map (fn th => th RS notE) distinct_thms, safe_elim_attrs)]
1.903 - |> map (fn (thms, attrs) => ((Binding.empty, attrs), [(thms, [])]));
1.904 -
1.905 - val ctr_sugar =
1.906 - {ctrs = ctrs, casex = casex, discs = discs, selss = selss, exhaust = exhaust_thm,
1.907 - nchotomy = nchotomy_thm, injects = inject_thms, distincts = distinct_thms,
1.908 - case_thms = case_thms, case_cong = case_cong_thm, weak_case_cong = weak_case_cong_thm,
1.909 - split = split_thm, split_asm = split_asm_thm, disc_thmss = disc_thmss,
1.910 - discIs = discI_thms, sel_thmss = sel_thmss, disc_exhausts = disc_exhaust_thms,
1.911 - sel_exhausts = sel_exhaust_thms, collapses = all_collapse_thms, expands = expand_thms,
1.912 - sel_splits = sel_split_thms, sel_split_asms = sel_split_asm_thms,
1.913 - case_conv_ifs = case_conv_if_thms};
1.914 - in
1.915 - (ctr_sugar,
1.916 - lthy
1.917 - |> not rep_compat ?
1.918 - (Local_Theory.declaration {syntax = false, pervasive = true}
1.919 - (fn phi => Case_Translation.register
1.920 - (Morphism.term phi casex) (map (Morphism.term phi) ctrs)))
1.921 - |> Local_Theory.notes (notes' @ notes) |> snd
1.922 - |> register_ctr_sugar fcT_name ctr_sugar)
1.923 - end;
1.924 - in
1.925 - (goalss, after_qed, lthy')
1.926 - end;
1.927 -
1.928 -fun wrap_free_constructors tacss = (fn (goalss, after_qed, lthy) =>
1.929 - map2 (map2 (Thm.close_derivation oo Goal.prove_sorry lthy [] [])) goalss tacss
1.930 - |> (fn thms => after_qed thms lthy)) oo prepare_wrap_free_constructors (K I);
1.931 -
1.932 -val wrap_free_constructors_cmd = (fn (goalss, after_qed, lthy) =>
1.933 - Proof.theorem NONE (snd oo after_qed) (map (map (rpair [])) goalss) lthy) oo
1.934 - prepare_wrap_free_constructors Syntax.read_term;
1.935 -
1.936 -fun parse_bracket_list parser = @{keyword "["} |-- Parse.list parser --| @{keyword "]"};
1.937 -
1.938 -val parse_bindings = parse_bracket_list parse_binding;
1.939 -val parse_bindingss = parse_bracket_list parse_bindings;
1.940 -
1.941 -val parse_bound_term = (parse_binding --| @{keyword ":"}) -- Parse.term;
1.942 -val parse_bound_terms = parse_bracket_list parse_bound_term;
1.943 -val parse_bound_termss = parse_bracket_list parse_bound_terms;
1.944 -
1.945 -val parse_wrap_free_constructors_options =
1.946 - Scan.optional (@{keyword "("} |-- Parse.list1 ((@{keyword "no_discs_sels"} >> K (true, false)) ||
1.947 - (@{keyword "rep_compat"} >> K (false, true))) --| @{keyword ")"}
1.948 - >> (pairself (exists I) o split_list)) (false, false);
1.949 -
1.950 -val _ =
1.951 - Outer_Syntax.local_theory_to_proof @{command_spec "wrap_free_constructors"}
1.952 - "wrap an existing freely generated type's constructors"
1.953 - ((parse_wrap_free_constructors_options -- (@{keyword "["} |-- Parse.list Parse.term --|
1.954 - @{keyword "]"}) --
1.955 - parse_binding -- Scan.optional (parse_bindings -- Scan.optional (parse_bindingss --
1.956 - Scan.optional parse_bound_termss []) ([], [])) ([], ([], [])))
1.957 - >> wrap_free_constructors_cmd);
1.958 -
1.959 -end;