joint work with blanchet: intermediate typedef for the input to fp-operations
authortraytel
Tue Feb 25 18:14:26 2014 +0100 (2014-02-25)
changeset 5580374d3fe9031d8
parent 55802 f7ceebe2f1b5
child 55804 341fbb9bdda1
joint work with blanchet: intermediate typedef for the input to fp-operations
src/HOL/BNF_Comp.thy
src/HOL/BNF_Def.thy
src/HOL/BNF_FP_Base.thy
src/HOL/BNF_Util.thy
src/HOL/Lattices_Big.thy
src/HOL/Tools/BNF/bnf_comp.ML
src/HOL/Tools/BNF/bnf_fp_def_sugar.ML
src/HOL/Tools/BNF/bnf_fp_def_sugar_tactics.ML
src/HOL/Tools/BNF/bnf_fp_n2m.ML
src/HOL/Tools/BNF/bnf_fp_n2m_sugar.ML
src/HOL/Tools/BNF/bnf_fp_n2m_tactics.ML
src/HOL/Tools/BNF/bnf_fp_util.ML
src/HOL/Tools/BNF/bnf_gfp.ML
src/HOL/Tools/BNF/bnf_lfp.ML
src/HOL/Tools/BNF/bnf_util.ML
     1.1 --- a/src/HOL/BNF_Comp.thy	Fri Feb 28 12:04:40 2014 +0100
     1.2 +++ b/src/HOL/BNF_Comp.thy	Tue Feb 25 18:14:26 2014 +0100
     1.3 @@ -70,6 +70,49 @@
     1.4  lemma OO_Grp_cong: "A = B \<Longrightarrow> (Grp A f)^--1 OO Grp A g = (Grp B f)^--1 OO Grp B g"
     1.5  by (rule arg_cong)
     1.6  
     1.7 +lemma vimage2p_relcompp_mono: "R OO S \<le> T \<Longrightarrow>
     1.8 +  vimage2p f g R OO vimage2p g h S \<le> vimage2p f h T"
     1.9 +  unfolding vimage2p_def by auto
    1.10 +
    1.11 +lemma type_copy_map_cong0: "M (g x) = N (h x) \<Longrightarrow> (f o M o g) x = (f o N o h) x"
    1.12 +  by auto
    1.13 +
    1.14 +lemma type_copy_set_bd: "(\<And>y. |S y| \<le>o bd) \<Longrightarrow> |(S o Rep) x| \<le>o bd"
    1.15 +  by auto
    1.16 +
    1.17 +lemma vimage2p_cong: "R = S \<Longrightarrow> vimage2p f g R = vimage2p f g S"
    1.18 +  by simp
    1.19 +
    1.20 +context
    1.21 +fixes Rep Abs
    1.22 +assumes type_copy: "type_definition Rep Abs UNIV"
    1.23 +begin
    1.24 +
    1.25 +lemma type_copy_map_id0: "M = id \<Longrightarrow> Abs o M o Rep = id"
    1.26 +  using type_definition.Rep_inverse[OF type_copy] by auto
    1.27 +lemma type_copy_map_comp0: "M = M1 o M2 \<Longrightarrow> f o M o g = (f o M1 o Rep) o (Abs o M2 o g)"
    1.28 +  using type_definition.Abs_inverse[OF type_copy UNIV_I] by auto
    1.29 +lemma type_copy_set_map0: "S o M = image f o S' \<Longrightarrow> (S o Rep) o (Abs o M o g) = image f o (S' o g)"
    1.30 +  using type_definition.Abs_inverse[OF type_copy UNIV_I] by (auto simp: o_def fun_eq_iff)
    1.31 +lemma type_copy_wit: "x \<in> (S o Rep) (Abs y) \<Longrightarrow> x \<in> S y"
    1.32 +  using type_definition.Abs_inverse[OF type_copy UNIV_I] by auto
    1.33 +lemma type_copy_vimage2p_Grp_Rep: "vimage2p f Rep (Grp (Collect P) h) =
    1.34 +    Grp (Collect (\<lambda>x. P (f x))) (Abs o h o f)"
    1.35 +  unfolding vimage2p_def Grp_def fun_eq_iff
    1.36 +  by (auto simp: type_definition.Abs_inverse[OF type_copy UNIV_I]
    1.37 +   type_definition.Rep_inverse[OF type_copy] dest: sym)
    1.38 +lemma type_copy_vimage2p_Grp_Abs:
    1.39 +  "\<And>h. vimage2p g Abs (Grp (Collect P) h) = Grp (Collect (\<lambda>x. P (g x))) (Rep o h o g)"
    1.40 +  unfolding vimage2p_def Grp_def fun_eq_iff
    1.41 +  by (auto simp: type_definition.Abs_inverse[OF type_copy UNIV_I]
    1.42 +   type_definition.Rep_inverse[OF type_copy] dest: sym)
    1.43 +lemma vimage2p_relcompp_converse:
    1.44 +  "vimage2p f g (R^--1 OO S) = (vimage2p Rep f R)^--1 OO vimage2p Rep g S"
    1.45 +  unfolding vimage2p_def relcompp.simps conversep.simps fun_eq_iff image_def
    1.46 +  by (metis surjD[OF type_definition.Rep_range[OF type_copy]])
    1.47 +
    1.48 +end
    1.49 +
    1.50  ML_file "Tools/BNF/bnf_comp_tactics.ML"
    1.51  ML_file "Tools/BNF/bnf_comp.ML"
    1.52  
     2.1 --- a/src/HOL/BNF_Def.thy	Fri Feb 28 12:04:40 2014 +0100
     2.2 +++ b/src/HOL/BNF_Def.thy	Tue Feb 25 18:14:26 2014 +0100
     2.3 @@ -137,9 +137,6 @@
     2.4    ((\<lambda>i. if i \<in> g ` C then the_inv_into C g i else x) o g) i = id i"
     2.5  unfolding Func_def by (auto elim: the_inv_into_f_f)
     2.6  
     2.7 -definition vimage2p where
     2.8 -  "vimage2p f g R = (\<lambda>x y. R (f x) (g y))"
     2.9 -
    2.10  lemma vimage2pI: "R (f x) (g y) \<Longrightarrow> vimage2p f g R x y"
    2.11    unfolding vimage2p_def by -
    2.12  
     3.1 --- a/src/HOL/BNF_FP_Base.thy	Fri Feb 28 12:04:40 2014 +0100
     3.2 +++ b/src/HOL/BNF_FP_Base.thy	Tue Feb 25 18:14:26 2014 +0100
     3.3 @@ -64,6 +64,11 @@
     3.4  lemma obj_one_pointE: "\<forall>x. s = x \<longrightarrow> P \<Longrightarrow> P"
     3.5  by blast
     3.6  
     3.7 +lemma type_copy_obj_one_point_absE:
     3.8 +  assumes "type_definition Rep Abs UNIV"
     3.9 +  shows "\<forall>x. s = Abs x \<longrightarrow> P \<Longrightarrow> P"
    3.10 +  using type_definition.Rep_inverse[OF assms] by metis
    3.11 +
    3.12  lemma obj_sumE_f:
    3.13  "\<lbrakk>\<forall>x. s = f (Inl x) \<longrightarrow> P; \<forall>x. s = f (Inr x) \<longrightarrow> P\<rbrakk> \<Longrightarrow> \<forall>x. s = f x \<longrightarrow> P"
    3.14  by (rule allI) (metis sumE)
    3.15 @@ -132,7 +137,7 @@
    3.16    unfolding case_sum_o_sum_map id_comp comp_id ..
    3.17  
    3.18  lemma fun_rel_def_butlast:
    3.19 -  "(fun_rel R (fun_rel S T)) f g = (\<forall>x y. R x y \<longrightarrow> (fun_rel S T) (f x) (g y))"
    3.20 +  "fun_rel R (fun_rel S T) f g = (\<forall>x y. R x y \<longrightarrow> (fun_rel S T) (f x) (g y))"
    3.21    unfolding fun_rel_def ..
    3.22  
    3.23  lemma subst_eq_imp: "(\<forall>a b. a = b \<longrightarrow> P a b) \<equiv> (\<forall>a. P a a)"
    3.24 @@ -148,6 +153,30 @@
    3.25     (\<And>x. x \<in> P \<Longrightarrow> f x \<in> Q)"
    3.26    unfolding Grp_def by rule auto
    3.27  
    3.28 +lemma vimage2p_mono: "vimage2p f g R x y \<Longrightarrow> R \<le> S \<Longrightarrow> vimage2p f g S x y"
    3.29 +  unfolding vimage2p_def by blast
    3.30 +
    3.31 +lemma vimage2p_refl: "(\<And>x. R x x) \<Longrightarrow> vimage2p f f R x x"
    3.32 +  unfolding vimage2p_def by auto
    3.33 +
    3.34 +lemma
    3.35 +  assumes "type_definition Rep Abs UNIV"
    3.36 +  shows type_copy_Rep_o_Abs: "Rep \<circ> Abs = id" and type_copy_Abs_o_Rep: "Abs o Rep = id"
    3.37 +  unfolding fun_eq_iff comp_apply id_apply
    3.38 +    type_definition.Abs_inverse[OF assms UNIV_I] type_definition.Rep_inverse[OF assms] by simp_all
    3.39 +
    3.40 +lemma type_copy_map_comp0_undo:
    3.41 +  assumes "type_definition Rep Abs UNIV"
    3.42 +          "type_definition Rep' Abs' UNIV"
    3.43 +          "type_definition Rep'' Abs'' UNIV"
    3.44 +  shows "Abs' o M o Rep'' = (Abs' o M1 o Rep) o (Abs o M2 o Rep'') \<Longrightarrow> M1 o M2 = M"
    3.45 +  by (rule sym) (auto simp: fun_eq_iff type_definition.Abs_inject[OF assms(2) UNIV_I UNIV_I]
    3.46 +    type_definition.Abs_inverse[OF assms(1) UNIV_I]
    3.47 +    type_definition.Abs_inverse[OF assms(3) UNIV_I] dest: spec[of _ "Abs'' x" for x])
    3.48 +
    3.49 +lemma vimage2p_comp: "vimage2p (f1 o f2) (g1 o g2) = vimage2p f2 g2 o vimage2p f1 g1"
    3.50 +  unfolding fun_eq_iff vimage2p_def o_apply by simp
    3.51 +
    3.52  ML_file "Tools/BNF/bnf_fp_util.ML"
    3.53  ML_file "Tools/BNF/bnf_fp_def_sugar_tactics.ML"
    3.54  ML_file "Tools/BNF/bnf_fp_def_sugar.ML"
     4.1 --- a/src/HOL/BNF_Util.thy	Fri Feb 28 12:04:40 2014 +0100
     4.2 +++ b/src/HOL/BNF_Util.thy	Tue Feb 25 18:14:26 2014 +0100
     4.3 @@ -44,6 +44,9 @@
     4.4  
     4.5  definition "Grp A f = (\<lambda>a b. b = f a \<and> a \<in> A)"
     4.6  
     4.7 +definition vimage2p where
     4.8 +  "vimage2p f g R = (\<lambda>x y. R (f x) (g y))"
     4.9 +
    4.10  ML_file "Tools/BNF/bnf_util.ML"
    4.11  ML_file "Tools/BNF/bnf_tactics.ML"
    4.12  
     5.1 --- a/src/HOL/Lattices_Big.thy	Fri Feb 28 12:04:40 2014 +0100
     5.2 +++ b/src/HOL/Lattices_Big.thy	Tue Feb 25 18:14:26 2014 +0100
     5.3 @@ -792,4 +792,3 @@
     5.4  end
     5.5  
     5.6  end
     5.7 -
     6.1 --- a/src/HOL/Tools/BNF/bnf_comp.ML	Fri Feb 28 12:04:40 2014 +0100
     6.2 +++ b/src/HOL/Tools/BNF/bnf_comp.ML	Tue Feb 25 18:14:26 2014 +0100
     6.3 @@ -27,8 +27,23 @@
     6.4    val normalize_bnfs: (int -> binding -> binding) -> ''a list list -> ''a list ->
     6.5      (''a list list -> ''a list) -> BNF_Def.bnf list -> unfold_set -> Proof.context ->
     6.6      (int list list * ''a list) * (BNF_Def.bnf list * (unfold_set * Proof.context))
     6.7 +
     6.8 +  type absT_info =
     6.9 +    {absT: typ,
    6.10 +     repT: typ,
    6.11 +     abs: term,
    6.12 +     rep: term,
    6.13 +     abs_inject: thm,
    6.14 +     abs_inverse: thm,
    6.15 +     type_definition: thm}
    6.16 +
    6.17 +  val morph_absT_info: morphism -> absT_info -> absT_info
    6.18 +  val mk_absT: theory -> typ -> typ -> typ -> typ
    6.19 +  val mk_repT: typ -> typ -> typ -> typ
    6.20 +  val mk_abs: typ -> term -> term
    6.21 +  val mk_rep: typ -> term -> term
    6.22    val seal_bnf: (binding -> binding) -> unfold_set -> binding -> typ list -> BNF_Def.bnf ->
    6.23 -    Proof.context -> (BNF_Def.bnf * typ list) * local_theory
    6.24 +    Proof.context -> (BNF_Def.bnf * (typ list * absT_info)) * local_theory
    6.25  end;
    6.26  
    6.27  structure BNF_Comp : BNF_COMP =
    6.28 @@ -572,6 +587,41 @@
    6.29  
    6.30  (* Hide the type of the bound (optimization) and unfold the definitions (nicer to the user) *)
    6.31  
    6.32 +type absT_info =
    6.33 +  {absT: typ,
    6.34 +   repT: typ,
    6.35 +   abs: term,
    6.36 +   rep: term,
    6.37 +   abs_inject: thm,
    6.38 +   abs_inverse: thm,
    6.39 +   type_definition: thm};
    6.40 +
    6.41 +fun morph_absT_info phi
    6.42 +  {absT, repT, abs, rep, abs_inject, abs_inverse, type_definition} =
    6.43 +  {absT = Morphism.typ phi absT,
    6.44 +   repT = Morphism.typ phi repT,
    6.45 +   abs = Morphism.term phi abs,
    6.46 +   rep = Morphism.term phi rep,
    6.47 +   abs_inject = Morphism.thm phi abs_inject,
    6.48 +   abs_inverse = Morphism.thm phi abs_inverse,
    6.49 +   type_definition = Morphism.thm phi type_definition};
    6.50 +
    6.51 +fun mk_absT thy repT absT repU =
    6.52 +  let val rho = Vartab.fold (cons o apsnd snd) (Sign.typ_match thy (repT, repU) Vartab.empty) [];
    6.53 +  in Term.typ_subst_TVars rho absT end;
    6.54 +
    6.55 +fun mk_repT (t as Type (C, Ts)) repT (u as Type (C', Us)) =
    6.56 +    if C = C' andalso length Ts = length Us then Term.typ_subst_atomic (Ts ~~ Us) repT
    6.57 +    else raise Term.TYPE ("mk_repT", [t, repT, u], [])
    6.58 +  | mk_repT t repT u =  raise Term.TYPE ("mk_repT", [t, repT, u], []);
    6.59 +
    6.60 +fun mk_abs_or_rep getT (Type (_, Us)) abs =
    6.61 +  let val Ts = snd (dest_Type (getT (fastype_of abs)))
    6.62 +  in Term.subst_atomic_types (Ts ~~ Us) abs end;
    6.63 +
    6.64 +val mk_abs = mk_abs_or_rep range_type;
    6.65 +val mk_rep = mk_abs_or_rep domain_type;
    6.66 +
    6.67  fun seal_bnf qualify (unfold_set : unfold_set) b Ds bnf lthy =
    6.68    let
    6.69      val live = live_of_bnf bnf;
    6.70 @@ -582,6 +632,10 @@
    6.71      val (Bs, _) = apfst (map TFree)
    6.72        (Variable.invent_types (replicate live HOLogic.typeS) lthy1);
    6.73  
    6.74 +    val (((fs, fs'), (Rs, Rs')), _(*names_lthy*)) = lthy
    6.75 +      |> mk_Frees' "f" (map2 (curry op -->) As Bs)
    6.76 +      ||>> mk_Frees' "R" (map2 mk_pred2T As Bs)
    6.77 +
    6.78      val map_unfolds = #map_unfolds unfold_set;
    6.79      val set_unfoldss = #set_unfoldss unfold_set;
    6.80      val rel_unfolds = #rel_unfolds unfold_set;
    6.81 @@ -596,12 +650,35 @@
    6.82      fun unfold_sets ctxt = fold (unfold_thms ctxt) set_unfoldss;
    6.83      fun unfold_rels ctxt = unfold_thms ctxt rel_unfolds;
    6.84      fun unfold_all ctxt = unfold_sets ctxt o unfold_maps ctxt o unfold_rels ctxt;
    6.85 -    val bnf_map = expand_maps (mk_map_of_bnf Ds As Bs bnf);
    6.86 -    val bnf_sets = map (expand_maps o expand_sets)
    6.87 +
    6.88 +    val repTA = mk_T_of_bnf Ds As bnf;
    6.89 +    val repTB = mk_T_of_bnf Ds Bs bnf;
    6.90 +    val T_bind = qualify b;
    6.91 +    val TA_params = Term.add_tfreesT repTA [];
    6.92 +    val TB_params = Term.add_tfreesT repTB [];
    6.93 +    val ((T_name, (T_glob_info, T_loc_info)), lthy) =
    6.94 +      typedef (T_bind, TA_params, NoSyn)
    6.95 +        (HOLogic.mk_UNIV repTA) NONE (EVERY' [rtac exI, rtac UNIV_I] 1) lthy;
    6.96 +    val TA = Type (T_name, map TFree TA_params);
    6.97 +    val TB = Type (T_name, map TFree TB_params);
    6.98 +    val RepA = Const (#Rep_name T_glob_info, TA --> repTA);
    6.99 +    val RepB = Const (#Rep_name T_glob_info, TB --> repTB);
   6.100 +    val AbsA = Const (#Abs_name T_glob_info, repTA --> TA);
   6.101 +    val AbsB = Const (#Abs_name T_glob_info, repTB --> TB);
   6.102 +    val typedef_thm = #type_definition T_loc_info;
   6.103 +    val Abs_inject' = #Abs_inject T_loc_info OF @{thms UNIV_I UNIV_I};
   6.104 +    val Abs_inverse' = #Abs_inverse T_loc_info OF @{thms UNIV_I};
   6.105 +
   6.106 +    val absT_info = {absT = TA, repT = repTA, abs = AbsA, rep = RepA, abs_inject = Abs_inject',
   6.107 +      abs_inverse = Abs_inverse', type_definition = typedef_thm};
   6.108 +
   6.109 +    val bnf_map = fold_rev Term.absfree fs' (HOLogic.mk_comp (HOLogic.mk_comp (AbsB,
   6.110 +      Term.list_comb (expand_maps (mk_map_of_bnf Ds As Bs bnf), fs)), RepA));
   6.111 +    val bnf_sets = map ((fn t => HOLogic.mk_comp (t, RepA)) o expand_maps o expand_sets)
   6.112        (mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf);
   6.113      val bnf_bd = mk_bd_of_bnf Ds As bnf;
   6.114 -    val bnf_rel = expand_rels (mk_rel_of_bnf Ds As Bs bnf);
   6.115 -    val T = mk_T_of_bnf Ds As bnf;
   6.116 +    val bnf_rel = fold_rev Term.absfree Rs' (mk_vimage2p RepA RepB $
   6.117 +      (Term.list_comb (expand_rels (mk_rel_of_bnf Ds As Bs bnf), Rs)));
   6.118  
   6.119      (*bd may depend only on dead type variables*)
   6.120      val bd_repT = fst (dest_relT (fastype_of bnf_bd));
   6.121 @@ -626,30 +703,44 @@
   6.122      val bd_cinfinite =
   6.123        (@{thm Cinfinite_cong} OF [bd_ordIso, bd_Cinfinite_of_bnf bnf]) RS conjunct1;
   6.124  
   6.125 -    val set_bds =
   6.126 -      map (fn thm => @{thm ordLeq_ordIso_trans} OF [thm, bd_ordIso]) (set_bd_of_bnf bnf);
   6.127 -
   6.128 -    fun mk_tac thm ctxt =
   6.129 -      (rtac (unfold_all ctxt thm) THEN'
   6.130 -      SOLVE o REPEAT_DETERM o (atac ORELSE' Goal.assume_rule_tac ctxt)) 1;
   6.131 +    fun map_id0_tac ctxt =
   6.132 +      rtac (@{thm type_copy_map_id0} OF [typedef_thm, unfold_maps ctxt (map_id0_of_bnf bnf)]) 1;
   6.133 +    fun map_comp0_tac ctxt =
   6.134 +      rtac (@{thm type_copy_map_comp0} OF [typedef_thm, unfold_maps ctxt (map_comp0_of_bnf bnf)]) 1;
   6.135 +    fun map_cong0_tac ctxt =
   6.136 +      EVERY' (rtac @{thm type_copy_map_cong0} :: rtac (unfold_all ctxt (map_cong0_of_bnf bnf)) ::
   6.137 +        map (fn i => EVERY' [select_prem_tac live (dtac meta_spec) i, etac meta_mp,
   6.138 +          etac (o_apply RS equalityD2 RS set_mp)]) (1 upto live)) 1;
   6.139 +    fun set_map0_tac thm ctxt =
   6.140 +      rtac (@{thm type_copy_set_map0} OF [typedef_thm, unfold_all ctxt thm]) 1;
   6.141 +    val set_bd_tacs = map (fn thm => fn ctxt => rtac (@{thm ordLeq_ordIso_trans} OF
   6.142 +        [unfold_sets ctxt thm, bd_ordIso] RS @{thm type_copy_set_bd}) 1)
   6.143 +      (set_bd_of_bnf bnf);
   6.144 +    fun le_rel_OO_tac ctxt =
   6.145 +      rtac (unfold_rels ctxt (le_rel_OO_of_bnf bnf) RS @{thm vimage2p_relcompp_mono}) 1;
   6.146 +    fun rel_OO_Grp_tac ctxt =
   6.147 +      (rtac (unfold_all ctxt (rel_OO_Grp_of_bnf bnf) RS @{thm vimage2p_cong} RS trans) THEN'
   6.148 +      SELECT_GOAL (unfold_thms_tac ctxt [o_apply, typedef_thm RS @{thm type_copy_vimage2p_Grp_Rep},
   6.149 +        typedef_thm RS @{thm vimage2p_relcompp_converse}]) THEN' rtac refl) 1;
   6.150  
   6.151 -    val tacs = zip_axioms (mk_tac (map_id0_of_bnf bnf)) (mk_tac (map_comp0_of_bnf bnf))
   6.152 -      (mk_tac (map_cong0_of_bnf bnf)) (map mk_tac (set_map0_of_bnf bnf))
   6.153 -      (K (rtac bd_card_order 1)) (K (rtac bd_cinfinite 1)) (map mk_tac set_bds)
   6.154 -      (mk_tac (le_rel_OO_of_bnf bnf))
   6.155 -      (mk_tac (rel_OO_Grp_of_bnf bnf));
   6.156 +    val tacs = zip_axioms map_id0_tac map_comp0_tac map_cong0_tac
   6.157 +      (map set_map0_tac (set_map0_of_bnf bnf)) (K (rtac bd_card_order 1)) (K (rtac bd_cinfinite 1))
   6.158 +      set_bd_tacs le_rel_OO_tac rel_OO_Grp_tac;
   6.159  
   6.160 -    val bnf_wits = map snd (mk_wits_of_bnf (replicate nwits Ds) (replicate nwits As) bnf);
   6.161 +    val bnf_wits = map (fn (I, t) =>
   6.162 +        fold Term.absdummy (map (nth As) I)
   6.163 +          (AbsA $ Term.list_comb (t, map Bound (0 upto length I - 1))))
   6.164 +      (mk_wits_of_bnf (replicate nwits Ds) (replicate nwits As) bnf);
   6.165  
   6.166 -    fun wit_tac ctxt =
   6.167 +    fun wit_tac ctxt = ALLGOALS (dtac (typedef_thm RS @{thm type_copy_wit})) THEN
   6.168        mk_simple_wit_tac (map (unfold_all ctxt) (wit_thms_of_bnf bnf));
   6.169  
   6.170      val (bnf', lthy') =
   6.171        bnf_def Hardly_Inline (user_policy Dont_Note) qualify tacs wit_tac (SOME all_deads)
   6.172          Binding.empty Binding.empty []
   6.173 -        ((((((b, T), bnf_map), bnf_sets), bnf_bd'), bnf_wits), SOME bnf_rel) lthy;
   6.174 +        ((((((b, TA), bnf_map), bnf_sets), bnf_bd'), bnf_wits), SOME bnf_rel) lthy;
   6.175    in
   6.176 -    ((bnf', all_deads), lthy')
   6.177 +    ((bnf', (all_deads, absT_info)), lthy')
   6.178    end;
   6.179  
   6.180  fun key_of_types Ts = Type ("", Ts);
     7.1 --- a/src/HOL/Tools/BNF/bnf_fp_def_sugar.ML	Fri Feb 28 12:04:40 2014 +0100
     7.2 +++ b/src/HOL/Tools/BNF/bnf_fp_def_sugar.ML	Tue Feb 25 18:14:26 2014 +0100
     7.3 @@ -14,6 +14,7 @@
     7.4       fp_res_index: int,
     7.5       fp_res: BNF_FP_Util.fp_result,
     7.6       pre_bnf: BNF_Def.bnf,
     7.7 +     absT_info: BNF_Comp.absT_info,
     7.8       nested_bnfs: BNF_Def.bnf list,
     7.9       nesting_bnfs: BNF_Def.bnf list,
    7.10       ctrXs_Tss: typ list list,
    7.11 @@ -56,7 +57,7 @@
    7.12    val transfer_gfp_sugar_thms: Proof.context -> gfp_sugar_thms -> gfp_sugar_thms
    7.13  
    7.14    val mk_co_iters_prelims: BNF_Util.fp_kind -> typ list list list -> typ list -> typ list ->
    7.15 -    int list -> int list list -> term list list -> Proof.context ->
    7.16 +    typ list -> typ list -> int list -> int list list -> term list list -> Proof.context ->
    7.17      (term list list
    7.18       * (typ list list * typ list list list list * term list list
    7.19          * term list list list list) list option
    7.20 @@ -65,26 +66,30 @@
    7.21      * Proof.context
    7.22    val repair_nullary_single_ctr: typ list list -> typ list list
    7.23    val mk_coiter_p_pred_types: typ list -> int list -> typ list list
    7.24 +  val mk_coiter_fun_arg_types: typ list list list -> typ list -> typ list -> typ list -> int list ->
    7.25 +    int list list -> term ->
    7.26 +    typ list list
    7.27 +    * (typ list list list list * typ list list list * typ list list list list * typ list)
    7.28    val define_iters: string list ->
    7.29      (typ list list * typ list list list list * term list list * term list list list list) list ->
    7.30 -    (string -> binding) -> typ list -> typ list -> term list -> Proof.context ->
    7.31 +    (string -> binding) -> typ list -> typ list -> term list -> term list -> Proof.context ->
    7.32      (term list * thm list) * Proof.context
    7.33    val define_coiters: string list -> string * term list * term list list
    7.34      * ((term list list * term list list list) * typ list) list ->
    7.35 -    (string -> binding) -> typ list -> typ list -> term list -> Proof.context ->
    7.36 +    (string -> binding) -> typ list -> typ list -> term list -> term list -> Proof.context ->
    7.37      (term list * thm list) * Proof.context
    7.38    val derive_induct_iters_thms_for_types: BNF_Def.bnf list ->
    7.39      (typ list list * typ list list list list * term list list * term list list list list) list ->
    7.40      thm -> thm list list -> BNF_Def.bnf list -> BNF_Def.bnf list -> typ list -> typ list ->
    7.41 -    typ list -> typ list list list -> term list list -> thm list list -> term list list ->
    7.42 -    thm list list -> local_theory -> lfp_sugar_thms
    7.43 +    typ list -> typ list list list -> thm list -> thm list -> thm list -> term list list ->
    7.44 +    thm list list -> term list list -> thm list list -> local_theory -> lfp_sugar_thms
    7.45    val derive_coinduct_coiters_thms_for_types: BNF_Def.bnf list ->
    7.46      string * term list * term list list * ((term list list * term list list list)
    7.47        * typ list) list ->
    7.48      thm -> thm list -> thm list -> thm list list -> BNF_Def.bnf list -> typ list -> typ list ->
    7.49 -    typ list -> typ list list list -> int list list -> int list list -> int list -> thm list list ->
    7.50 -    Ctr_Sugar.ctr_sugar list -> term list list -> thm list list -> (thm list -> thm list) ->
    7.51 -    local_theory -> gfp_sugar_thms
    7.52 +    typ list -> typ list list list -> int list list -> int list list -> int list -> thm list ->
    7.53 +    thm list -> (thm -> thm) -> thm list list -> Ctr_Sugar.ctr_sugar list -> term list list ->
    7.54 +    thm list list -> (thm list -> thm list) -> local_theory -> gfp_sugar_thms
    7.55  
    7.56    type co_datatype_spec =
    7.57      ((((binding option * (typ * sort)) list * binding) * (binding * binding)) * mixfix)
    7.58 @@ -92,12 +97,14 @@
    7.59  
    7.60    val co_datatypes: BNF_Util.fp_kind -> (mixfix list -> binding list -> binding list ->
    7.61        binding list list -> binding list -> (string * sort) list -> typ list * typ list list ->
    7.62 -      BNF_Def.bnf list -> local_theory -> BNF_FP_Util.fp_result * local_theory) ->
    7.63 +      BNF_Def.bnf list -> BNF_Comp.absT_info list -> local_theory ->
    7.64 +      BNF_FP_Util.fp_result * local_theory) ->
    7.65      (bool * bool) * co_datatype_spec list ->
    7.66      local_theory -> local_theory
    7.67    val parse_co_datatype_cmd: BNF_Util.fp_kind -> (mixfix list -> binding list -> binding list ->
    7.68        binding list list -> binding list -> (string * sort) list -> typ list * typ list list ->
    7.69 -      BNF_Def.bnf list -> local_theory -> BNF_FP_Util.fp_result * local_theory) ->
    7.70 +      BNF_Def.bnf list -> BNF_Comp.absT_info list -> local_theory ->
    7.71 +      BNF_FP_Util.fp_result * local_theory) ->
    7.72      (local_theory -> local_theory) parser
    7.73  end;
    7.74  
    7.75 @@ -121,6 +128,7 @@
    7.76     fp_res_index: int,
    7.77     fp_res: fp_result,
    7.78     pre_bnf: bnf,
    7.79 +   absT_info: absT_info,
    7.80     nested_bnfs: bnf list,
    7.81     nesting_bnfs: bnf list,
    7.82     ctrXs_Tss: typ list list,
    7.83 @@ -134,15 +142,16 @@
    7.84     disc_co_iterss: thm list list,
    7.85     sel_co_itersss: thm list list list};
    7.86  
    7.87 -fun morph_fp_sugar phi ({T, X, fp, fp_res, fp_res_index, pre_bnf, nested_bnfs, nesting_bnfs,
    7.88 -    ctrXs_Tss, ctr_defs, ctr_sugar, co_iters, maps, common_co_inducts, co_inducts, co_iter_thmss,
    7.89 -    disc_co_iterss, sel_co_itersss} : fp_sugar) =
    7.90 +fun morph_fp_sugar phi ({T, X, fp, fp_res, fp_res_index, pre_bnf, absT_info, nested_bnfs,
    7.91 +    nesting_bnfs, ctrXs_Tss, ctr_defs, ctr_sugar, co_iters, maps, common_co_inducts, co_inducts,
    7.92 +    co_iter_thmss, disc_co_iterss, sel_co_itersss} : fp_sugar) =
    7.93    {T = Morphism.typ phi T,
    7.94     X = Morphism.typ phi X,
    7.95     fp = fp,
    7.96     fp_res = morph_fp_result phi fp_res,
    7.97     fp_res_index = fp_res_index,
    7.98     pre_bnf = morph_bnf phi pre_bnf,
    7.99 +   absT_info = morph_absT_info phi absT_info,
   7.100     nested_bnfs = map (morph_bnf phi) nested_bnfs,
   7.101     nesting_bnfs = map (morph_bnf phi) nesting_bnfs,
   7.102     ctrXs_Tss = map (map (Morphism.typ phi)) ctrXs_Tss,
   7.103 @@ -183,17 +192,18 @@
   7.104    Local_Theory.declaration {syntax = false, pervasive = true}
   7.105      (fn phi => Data.map (Symtab.update (key, morph_fp_sugar phi fp_sugar)));
   7.106  
   7.107 -fun register_fp_sugars Xs fp pre_bnfs nested_bnfs nesting_bnfs (fp_res as {Ts, ...}) ctrXs_Tsss
   7.108 -    ctr_defss ctr_sugars co_iterss mapss common_co_inducts co_inductss co_iter_thmsss
   7.109 +fun register_fp_sugars Xs fp pre_bnfs absT_infos nested_bnfs nesting_bnfs (fp_res as {Ts, ...})
   7.110 +    ctrXs_Tsss ctr_defss ctr_sugars co_iterss mapss common_co_inducts co_inductss co_iter_thmsss
   7.111      disc_co_itersss sel_co_iterssss lthy =
   7.112    (0, lthy)
   7.113    |> fold (fn T as Type (s, _) => fn (kk, lthy) => (kk + 1,
   7.114      register_fp_sugar s {T = T, X = nth Xs kk, fp = fp, fp_res = fp_res, fp_res_index = kk,
   7.115 -        pre_bnf = nth pre_bnfs kk, nested_bnfs = nested_bnfs, nesting_bnfs = nesting_bnfs,
   7.116 -        ctrXs_Tss = nth ctrXs_Tsss kk, ctr_defs = nth ctr_defss kk, ctr_sugar = nth ctr_sugars kk,
   7.117 -        co_iters = nth co_iterss kk, maps = nth mapss kk, common_co_inducts = common_co_inducts,
   7.118 -        co_inducts = nth co_inductss kk, co_iter_thmss = nth co_iter_thmsss kk,
   7.119 -        disc_co_iterss = nth disc_co_itersss kk, sel_co_itersss = nth sel_co_iterssss kk}
   7.120 +        pre_bnf = nth pre_bnfs kk, absT_info = nth absT_infos kk, nested_bnfs = nested_bnfs,
   7.121 +        nesting_bnfs = nesting_bnfs, ctrXs_Tss = nth ctrXs_Tsss kk, ctr_defs = nth ctr_defss kk,
   7.122 +        ctr_sugar = nth ctr_sugars kk, co_iters = nth co_iterss kk, maps = nth mapss kk,
   7.123 +        common_co_inducts = common_co_inducts, co_inducts = nth co_inductss kk,
   7.124 +        co_iter_thmss = nth co_iter_thmsss kk, disc_co_iterss = nth disc_co_itersss kk,
   7.125 +        sel_co_itersss = nth sel_co_iterssss kk}
   7.126        lthy)) Ts
   7.127    |> snd;
   7.128  
   7.129 @@ -227,12 +237,6 @@
   7.130    | flat_corec_preds_predsss_gettersss (p :: ps) (qss :: qsss) (fss :: fsss) =
   7.131      p :: flat_corec_predss_getterss qss fss @ flat_corec_preds_predsss_gettersss ps qsss fsss;
   7.132  
   7.133 -fun mk_tupled_fun x f xs =
   7.134 -  if xs = [x] then f else HOLogic.tupled_lambda x (Term.list_comb (f, xs));
   7.135 -
   7.136 -fun mk_uncurried2_fun f xss =
   7.137 -  mk_tupled_fun (HOLogic.mk_tuple (map HOLogic.mk_tuple xss)) f (flat_rec_arg_args xss);
   7.138 -
   7.139  fun mk_flip (x, Type (_, [T1, Type (_, [T2, T3])])) =
   7.140    Abs ("x", T1, Abs ("y", T2, Var (x, T2 --> T1 --> T3) $ Bound 0 $ Bound 1));
   7.141  
   7.142 @@ -350,12 +354,12 @@
   7.143  val transfer_gfp_sugar_thms =
   7.144    morph_gfp_sugar_thms o Morphism.transfer_morphism o Proof_Context.theory_of;
   7.145  
   7.146 -fun mk_iter_fun_arg_types n ms = map2 dest_tupleT ms o dest_sumTN_balanced n o domain_type;
   7.147 -
   7.148 -fun mk_iters_args_types ctr_Tsss Cs ns mss ctor_iter_fun_Tss lthy =
   7.149 +fun mk_iters_args_types ctr_Tsss Cs absTs repTs ns mss ctor_iter_fun_Tss lthy =
   7.150    let
   7.151      val Css = map2 replicate ns Cs;
   7.152 -    val y_Tsss = map3 mk_iter_fun_arg_types ns mss (map un_fold_of ctor_iter_fun_Tss);
   7.153 +    val y_Tsss = map5 (fn absT => fn repT => fn n => fn ms =>
   7.154 +        dest_absumprodT absT repT n ms o domain_type)
   7.155 +      absTs repTs ns mss (map un_fold_of ctor_iter_fun_Tss);
   7.156      val g_Tss = map2 (fn C => map (fn y_Ts => y_Ts ---> C)) Cs y_Tsss;
   7.157  
   7.158      val ((gss, ysss), lthy) =
   7.159 @@ -367,11 +371,10 @@
   7.160      val yssss = map (map (map single)) ysss;
   7.161  
   7.162      val z_Tssss =
   7.163 -      map4 (fn n => fn ms => fn ctr_Tss => fn ctor_iter_fun_Ts =>
   7.164 -          map3 (fn m => fn ctr_Ts => fn ctor_iter_fun_T =>
   7.165 -              map2 unzip_recT ctr_Ts (dest_tupleT m ctor_iter_fun_T))
   7.166 -            ms ctr_Tss (dest_sumTN_balanced n (domain_type (co_rec_of ctor_iter_fun_Ts))))
   7.167 -        ns mss ctr_Tsss ctor_iter_fun_Tss;
   7.168 +      map6 (fn absT => fn repT => fn n => fn ms => fn ctr_Tss => fn ctor_iter_fun_Ts =>
   7.169 +          map2 (map2 unzip_recT)
   7.170 +            ctr_Tss (dest_absumprodT absT repT n ms (domain_type (co_rec_of ctor_iter_fun_Ts))))
   7.171 +        absTs repTs ns mss ctr_Tsss ctor_iter_fun_Tss;
   7.172  
   7.173      val z_Tsss' = map (map flat_rec_arg_args) z_Tssss;
   7.174      val h_Tss = map2 (map2 (curry (op --->))) z_Tsss' Css;
   7.175 @@ -390,31 +393,36 @@
   7.176  fun repair_nullary_single_ctr [[]] = [[@{typ unit}]]
   7.177    | repair_nullary_single_ctr Tss = Tss;
   7.178  
   7.179 -fun mk_coiter_fun_arg_types0 ctr_Tsss Cs ns fun_Ts =
   7.180 +fun mk_coiter_fun_arg_types0 ctr_Tsss Cs absTs repTs ns mss fun_Ts =
   7.181    let
   7.182      val ctr_Tsss' = map repair_nullary_single_ctr ctr_Tsss;
   7.183 -    val f_sum_prod_Ts = map range_type fun_Ts;
   7.184 -    val f_prod_Tss = map2 dest_sumTN_balanced ns f_sum_prod_Ts;
   7.185 -    val f_Tsss = map2 (map2 (dest_tupleT o length)) ctr_Tsss' f_prod_Tss;
   7.186 +    val f_absTs = map range_type fun_Ts;
   7.187 +    val f_Tsss = map repair_nullary_single_ctr (map5 dest_absumprodT absTs repTs ns mss f_absTs);
   7.188      val f_Tssss = map3 (fn C => map2 (map2 (map (curry (op -->) C) oo unzip_corecT)))
   7.189        Cs ctr_Tsss' f_Tsss;
   7.190      val q_Tssss = map (map (map (fn [_] => [] | [_, T] => [mk_pred1T (domain_type T)]))) f_Tssss;
   7.191    in
   7.192 -    (q_Tssss, f_Tsss, f_Tssss, f_sum_prod_Ts)
   7.193 +    (q_Tssss, f_Tsss, f_Tssss, f_absTs)
   7.194    end;
   7.195  
   7.196  fun mk_coiter_p_pred_types Cs ns = map2 (fn n => replicate (Int.max (0, n - 1)) o mk_pred1T) ns Cs;
   7.197  
   7.198 -fun mk_coiters_args_types ctr_Tsss Cs ns dtor_coiter_fun_Tss lthy =
   7.199 +fun mk_coiter_fun_arg_types ctr_Tsss Cs absTs repTs ns mss dtor_coiter =
   7.200 +  (mk_coiter_p_pred_types Cs ns,
   7.201 +   mk_coiter_fun_arg_types0 ctr_Tsss Cs absTs repTs ns mss
   7.202 +     (binder_fun_types (fastype_of dtor_coiter)));
   7.203 +
   7.204 +fun mk_coiters_args_types ctr_Tsss Cs absTs repTs ns mss dtor_coiter_fun_Tss lthy =
   7.205    let
   7.206      val p_Tss = mk_coiter_p_pred_types Cs ns;
   7.207  
   7.208      fun mk_types get_Ts =
   7.209        let
   7.210          val fun_Ts = map get_Ts dtor_coiter_fun_Tss;
   7.211 -        val (q_Tssss, f_Tsss, f_Tssss, f_sum_prod_Ts) = mk_coiter_fun_arg_types0 ctr_Tsss Cs ns fun_Ts;
   7.212 +        val (q_Tssss, f_Tsss, f_Tssss, f_repTs) =
   7.213 +          mk_coiter_fun_arg_types0 ctr_Tsss Cs absTs repTs ns mss fun_Ts;
   7.214        in
   7.215 -        (q_Tssss, f_Tsss, f_Tssss, f_sum_prod_Ts)
   7.216 +        (q_Tssss, f_Tsss, f_Tssss, f_repTs)
   7.217        end;
   7.218  
   7.219      val (r_Tssss, g_Tsss, g_Tssss, unfold_types) = mk_types un_fold_of;
   7.220 @@ -458,7 +466,7 @@
   7.221      ((z, cs, cpss, [(unfold_args, unfold_types), (corec_args, corec_types)]), lthy)
   7.222    end;
   7.223  
   7.224 -fun mk_co_iters_prelims fp ctr_Tsss fpTs Cs ns mss xtor_co_iterss0 lthy =
   7.225 +fun mk_co_iters_prelims fp ctr_Tsss fpTs Cs absTs repTs ns mss xtor_co_iterss0 lthy =
   7.226    let
   7.227      val thy = Proof_Context.theory_of lthy;
   7.228  
   7.229 @@ -469,17 +477,21 @@
   7.230  
   7.231      val ((iters_args_types, coiters_args_types), lthy') =
   7.232        if fp = Least_FP then
   7.233 -        mk_iters_args_types ctr_Tsss Cs ns mss xtor_co_iter_fun_Tss lthy |>> (rpair NONE o SOME)
   7.234 +        mk_iters_args_types ctr_Tsss Cs absTs repTs ns mss xtor_co_iter_fun_Tss lthy
   7.235 +        |>> (rpair NONE o SOME)
   7.236        else
   7.237 -        mk_coiters_args_types ctr_Tsss Cs ns xtor_co_iter_fun_Tss lthy |>> (pair NONE o SOME);
   7.238 +        mk_coiters_args_types ctr_Tsss Cs absTs repTs ns mss xtor_co_iter_fun_Tss lthy
   7.239 +        |>> (pair NONE o SOME);
   7.240    in
   7.241      ((xtor_co_iterss, iters_args_types, coiters_args_types), lthy')
   7.242    end;
   7.243  
   7.244 -fun mk_preds_getterss_join c cps sum_prod_T cqfss =
   7.245 -  let val n = length cqfss in
   7.246 -    Term.lambda c (mk_IfN sum_prod_T cps
   7.247 -      (map2 (mk_InN_balanced sum_prod_T n) (map HOLogic.mk_tuple cqfss) (1 upto n)))
   7.248 +fun mk_preds_getterss_join c cps absT abs cqfss =
   7.249 +  let
   7.250 +    val n = length cqfss;
   7.251 +    val ts = map2 (mk_absumprod absT abs n) (1 upto n) cqfss;
   7.252 +  in
   7.253 +    Term.lambda c (mk_IfN absT cps ts)
   7.254    end;
   7.255  
   7.256  fun define_co_iters fp fpT Cs binding_specs lthy0 =
   7.257 @@ -503,39 +515,43 @@
   7.258      ((csts', defs'), lthy')
   7.259    end;
   7.260  
   7.261 -fun define_iters iterNs iter_args_typess' mk_binding fpTs Cs ctor_iters lthy =
   7.262 +fun define_iters iterNs iter_args_typess' mk_binding fpTs Cs reps ctor_iters lthy =
   7.263    let
   7.264      val nn = length fpTs;
   7.265 -
   7.266 -    val Type (_, [fpT, _]) = snd (strip_typeN nn (fastype_of (hd ctor_iters)));
   7.267 +    val fpT = domain_type (snd (strip_typeN nn (fastype_of (un_fold_of ctor_iters))));
   7.268  
   7.269      fun generate_iter pre (_, _, fss, xssss) ctor_iter =
   7.270 -      (mk_binding pre,
   7.271 -       fold_rev (fold_rev Term.lambda) fss (Term.list_comb (ctor_iter,
   7.272 -         map2 (mk_case_sumN_balanced oo map2 mk_uncurried2_fun) fss xssss)));
   7.273 +      let val ctor_iter_absTs = map domain_type (fst (strip_typeN nn (fastype_of ctor_iter))) in
   7.274 +        (mk_binding pre,
   7.275 +         fold_rev (fold_rev Term.lambda) fss (Term.list_comb (ctor_iter,
   7.276 +           map4 (fn ctor_iter_absT => fn rep => fn fs => fn xsss =>
   7.277 +               mk_case_absumprod ctor_iter_absT rep fs
   7.278 +                 (map (HOLogic.mk_tuple o map HOLogic.mk_tuple) xsss) (map flat_rec_arg_args xsss))
   7.279 +             ctor_iter_absTs reps fss xssss)))
   7.280 +      end;
   7.281    in
   7.282      define_co_iters Least_FP fpT Cs (map3 generate_iter iterNs iter_args_typess' ctor_iters) lthy
   7.283    end;
   7.284  
   7.285 -fun define_coiters coiterNs (_, cs, cpss, coiter_args_typess') mk_binding fpTs Cs dtor_coiters
   7.286 +fun define_coiters coiterNs (_, cs, cpss, coiter_args_typess') mk_binding fpTs Cs abss dtor_coiters
   7.287      lthy =
   7.288    let
   7.289      val nn = length fpTs;
   7.290  
   7.291      val Type (_, [_, fpT]) = snd (strip_typeN nn (fastype_of (hd dtor_coiters)));
   7.292  
   7.293 -    fun generate_coiter pre ((pfss, cqfsss), f_sum_prod_Ts) dtor_coiter =
   7.294 +    fun generate_coiter pre ((pfss, cqfsss), f_absTs) dtor_coiter =
   7.295        (mk_binding pre,
   7.296         fold_rev (fold_rev Term.lambda) pfss (Term.list_comb (dtor_coiter,
   7.297 -         map4 mk_preds_getterss_join cs cpss f_sum_prod_Ts cqfsss)));
   7.298 +         map5 mk_preds_getterss_join cs cpss f_absTs abss cqfsss)));
   7.299    in
   7.300      define_co_iters Greatest_FP fpT Cs
   7.301        (map3 generate_coiter coiterNs coiter_args_typess' dtor_coiters) lthy
   7.302    end;
   7.303  
   7.304  fun derive_induct_iters_thms_for_types pre_bnfs [fold_args_types, rec_args_types] ctor_induct
   7.305 -    ctor_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss ctrss ctr_defss iterss iter_defss
   7.306 -    lthy =
   7.307 +    ctor_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss fp_abs_inverses
   7.308 +    fp_type_definitions abs_inverses ctrss ctr_defss iterss iter_defss lthy =
   7.309    let
   7.310      val iterss' = transpose iterss;
   7.311      val iter_defss' = transpose iter_defss;
   7.312 @@ -632,12 +648,12 @@
   7.313  
   7.314          val kksss = map (map (map (fst o snd) o #2)) raw_premss;
   7.315  
   7.316 -        val ctor_induct' = ctor_induct OF (map mk_sumEN_tupled_balanced mss);
   7.317 +        val ctor_induct' = ctor_induct OF (map2 mk_absumprodE fp_type_definitions mss);
   7.318  
   7.319          val thm =
   7.320            Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} =>
   7.321 -            mk_induct_tac ctxt nn ns mss kksss (flat ctr_defss) ctor_induct' nested_set_maps
   7.322 -              pre_set_defss)
   7.323 +            mk_induct_tac ctxt nn ns mss kksss (flat ctr_defss) ctor_induct' fp_abs_inverses
   7.324 +              abs_inverses nested_set_maps pre_set_defss)
   7.325            |> singleton (Proof_Context.export names_lthy lthy)
   7.326            (* for "datatype_realizer.ML": *)
   7.327            |> Thm.name_derivation (fst (dest_Type (hd fpTs)) ^ Long_Name.separator ^
   7.328 @@ -678,8 +694,8 @@
   7.329          val goalss = map5 (map4 o mk_goal fss) fiters xctrss fss xsss fxsss;
   7.330  
   7.331          val tacss =
   7.332 -          map2 (map o mk_iter_tac pre_map_defs (nested_map_idents @ nesting_map_idents) iter_defs)
   7.333 -            ctor_iter_thms ctr_defss;
   7.334 +          map4 (map ooo mk_iter_tac pre_map_defs (nested_map_idents @ nesting_map_idents) iter_defs)
   7.335 +            ctor_iter_thms fp_abs_inverses abs_inverses ctr_defss;
   7.336  
   7.337          fun prove goal tac =
   7.338            Goal.prove_sorry lthy [] [] goal (tac o #context)
   7.339 @@ -698,7 +714,8 @@
   7.340  fun derive_coinduct_coiters_thms_for_types pre_bnfs (z, cs, cpss,
   7.341        coiters_args_types as [((pgss, crgsss), _), ((phss, cshsss), _)])
   7.342      dtor_coinduct dtor_injects dtor_ctors dtor_coiter_thmss nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss
   7.343 -    mss ns ctr_defss (ctr_sugars : ctr_sugar list) coiterss coiter_defss export_args lthy =
   7.344 +    mss ns fp_abs_inverses abs_inverses mk_vimage2p ctr_defss (ctr_sugars : ctr_sugar list)
   7.345 +    coiterss coiter_defss export_args lthy =
   7.346    let
   7.347      fun mk_ctor_dtor_coiter_thm dtor_inject dtor_ctor coiter =
   7.348        iffD1 OF [dtor_inject, trans OF [coiter, dtor_ctor RS sym]];
   7.349 @@ -787,8 +804,8 @@
   7.350  
   7.351          fun prove dtor_coinduct' goal =
   7.352            Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} =>
   7.353 -            mk_coinduct_tac ctxt nesting_rel_eqs nn ns dtor_coinduct' pre_rel_defs dtor_ctors
   7.354 -              exhausts ctr_defss disc_thmsss sel_thmsss)
   7.355 +            mk_coinduct_tac ctxt nesting_rel_eqs nn ns dtor_coinduct' pre_rel_defs fp_abs_inverses
   7.356 +              abs_inverses dtor_ctors exhausts ctr_defss disc_thmsss sel_thmsss)
   7.357            |> singleton (Proof_Context.export names_lthy lthy)
   7.358            |> Thm.close_derivation;
   7.359  
   7.360 @@ -801,7 +818,7 @@
   7.361          val rel_eqs = map rel_eq_of_bnf pre_bnfs;
   7.362          val rel_monos = map rel_mono_of_bnf pre_bnfs;
   7.363          val dtor_coinducts =
   7.364 -          [dtor_coinduct, mk_strong_coinduct_thm dtor_coinduct rel_eqs rel_monos lthy];
   7.365 +          [dtor_coinduct, mk_strong_coinduct_thm dtor_coinduct rel_eqs rel_monos mk_vimage2p lthy]
   7.366        in
   7.367          map2 (postproc nn oo prove) dtor_coinducts goals
   7.368        end;
   7.369 @@ -860,11 +877,11 @@
   7.370          val corec_goalss = map8 (map4 oooo mk_goal phss) cs cpss hcorecs ns kss ctrss mss cshsss';
   7.371  
   7.372          val unfold_tacss =
   7.373 -          map3 (map oo mk_coiter_tac unfold_defs nesting_map_idents)
   7.374 -            (map un_fold_of ctor_dtor_coiter_thmss) pre_map_defs ctr_defss;
   7.375 +          map4 (map ooo mk_coiter_tac unfold_defs nesting_map_idents)
   7.376 +            (map un_fold_of ctor_dtor_coiter_thmss) pre_map_defs abs_inverses ctr_defss;
   7.377          val corec_tacss =
   7.378 -          map3 (map oo mk_coiter_tac corec_defs nesting_map_idents)
   7.379 -            (map co_rec_of ctor_dtor_coiter_thmss) pre_map_defs ctr_defss;
   7.380 +          map4 (map ooo mk_coiter_tac corec_defs nesting_map_idents)
   7.381 +            (map co_rec_of ctor_dtor_coiter_thmss) pre_map_defs abs_inverses ctr_defss;
   7.382  
   7.383          fun prove goal tac =
   7.384            Goal.prove_sorry lthy [] [] goal (tac o #context)
   7.385 @@ -1048,10 +1065,10 @@
   7.386      val unfreeze_fp = Term.typ_subst_atomic (Xs ~~ fake_Ts);
   7.387  
   7.388      val ctrXs_Tsss = map (map (map freeze_fp)) fake_ctr_Tsss;
   7.389 -    val ctrXs_sum_prod_Ts = map (mk_sumTN_balanced o map HOLogic.mk_tupleT) ctrXs_Tsss;
   7.390 +    val ctrXs_repTs = map (mk_sumTN_balanced o map HOLogic.mk_tupleT) ctrXs_Tsss;
   7.391  
   7.392      val fp_eqs =
   7.393 -      map dest_TFree Xs ~~ map (Term.typ_subst_atomic (As ~~ unsorted_As)) ctrXs_sum_prod_Ts;
   7.394 +      map dest_TFree Xs ~~ map (Term.typ_subst_atomic (As ~~ unsorted_As)) ctrXs_repTs;
   7.395  
   7.396      val rhsXs_As' = fold (fold (fold Term.add_tfreesT)) ctrXs_Tsss [];
   7.397      val _ = (case subtract (op =) rhsXs_As' As' of [] => ()
   7.398 @@ -1062,9 +1079,10 @@
   7.399        map_filter (fn (A, set_bos) => if exists is_none set_bos then SOME A else NONE)
   7.400          (unsorted_As ~~ transpose set_boss);
   7.401  
   7.402 -    val (pre_bnfs, (fp_res as {bnfs = fp_bnfs as any_fp_bnf :: _, ctors = ctors0, dtors = dtors0,
   7.403 -           xtor_co_iterss = xtor_co_iterss0, xtor_co_induct, dtor_ctors, ctor_dtors, ctor_injects,
   7.404 -           dtor_injects, xtor_map_thms, xtor_set_thmss, xtor_rel_thms, xtor_co_iter_thmss, ...},
   7.405 +    val ((pre_bnfs, absT_infos), (fp_res as {bnfs = fp_bnfs as any_fp_bnf :: _, ctors = ctors0,
   7.406 +             dtors = dtors0, xtor_co_iterss = xtor_co_iterss0, xtor_co_induct, dtor_ctors,
   7.407 +             ctor_dtors, ctor_injects, dtor_injects, xtor_map_thms, xtor_set_thmss, xtor_rel_thms,
   7.408 +             xtor_co_iter_thmss, ...},
   7.409             lthy)) =
   7.410        fp_bnf (construct_fp mixfixes map_bs rel_bs set_bss) fp_bs (map dest_TFree unsorted_As)
   7.411          (map dest_TFree killed_As) fp_eqs no_defs_lthy0
   7.412 @@ -1094,6 +1112,14 @@
   7.413                  register_hint ())
   7.414            end);
   7.415  
   7.416 +    val abss = map #abs absT_infos;
   7.417 +    val reps = map #rep absT_infos;
   7.418 +    val absTs = map #absT absT_infos;
   7.419 +    val repTs = map #repT absT_infos;
   7.420 +    val abs_injects = map #abs_inject absT_infos;
   7.421 +    val abs_inverses = map #abs_inverse absT_infos;
   7.422 +    val type_definitions = map #type_definition absT_infos;
   7.423 +
   7.424      val time = time lthy;
   7.425      val timer = time (Timer.startRealTimer ());
   7.426  
   7.427 @@ -1142,34 +1168,30 @@
   7.428      val mss = map (map length) ctr_Tsss;
   7.429  
   7.430      val ((xtor_co_iterss, iters_args_types, coiters_args_types), lthy') =
   7.431 -      mk_co_iters_prelims fp ctr_Tsss fpTs Cs ns mss xtor_co_iterss0 lthy;
   7.432 +      mk_co_iters_prelims fp ctr_Tsss fpTs Cs absTs repTs ns mss xtor_co_iterss0 lthy;
   7.433  
   7.434 -    fun define_ctrs_dtrs_for_type (((((((((((((((((((((((fp_bnf, fp_b), fpT), ctor), dtor),
   7.435 -            xtor_co_iters), ctor_dtor), dtor_ctor), ctor_inject), pre_map_def), pre_set_defs),
   7.436 -          pre_rel_def), fp_map_thm), fp_set_thms), fp_rel_thm), n), ks), ms), ctr_bindings),
   7.437 -        ctr_mixfixes), ctr_Tss), disc_bindings), sel_bindingss), raw_sel_defaultss) no_defs_lthy =
   7.438 +    fun define_ctrs_dtrs_for_type (((((((((((((((((((((((((((fp_bnf, fp_b), fpT), ctor), dtor),
   7.439 +              xtor_co_iters), ctor_dtor), dtor_ctor), ctor_inject), pre_map_def), pre_set_defs),
   7.440 +            pre_rel_def), fp_map_thm), fp_set_thms), fp_rel_thm), n), ks), ms), abs),
   7.441 +          abs_inject), abs_inverse), type_definition), ctr_bindings), ctr_mixfixes), ctr_Tss),
   7.442 +        disc_bindings), sel_bindingss), raw_sel_defaultss) no_defs_lthy =
   7.443        let
   7.444          val fp_b_name = Binding.name_of fp_b;
   7.445  
   7.446 -        val dtorT = domain_type (fastype_of ctor);
   7.447 -        val ctr_prod_Ts = map HOLogic.mk_tupleT ctr_Tss;
   7.448 -        val ctr_sum_prod_T = mk_sumTN_balanced ctr_prod_Ts;
   7.449 +        val ctr_absT = domain_type (fastype_of ctor);
   7.450  
   7.451          val ((((w, xss), yss), u'), names_lthy) =
   7.452            no_defs_lthy
   7.453 -          |> yield_singleton (mk_Frees "w") dtorT
   7.454 +          |> yield_singleton (mk_Frees "w") ctr_absT
   7.455            ||>> mk_Freess "x" ctr_Tss
   7.456            ||>> mk_Freess "y" (map (map B_ify) ctr_Tss)
   7.457            ||>> yield_singleton Variable.variant_fixes fp_b_name;
   7.458  
   7.459          val u = Free (u', fpT);
   7.460  
   7.461 -        val tuple_xs = map HOLogic.mk_tuple xss;
   7.462 -        val tuple_ys = map HOLogic.mk_tuple yss;
   7.463 -
   7.464          val ctr_rhss =
   7.465 -          map3 (fn k => fn xs => fn tuple_x => fold_rev Term.lambda xs (ctor $
   7.466 -            mk_InN_balanced ctr_sum_prod_T n tuple_x k)) ks xss tuple_xs;
   7.467 +          map2 (fn k => fn xs => fold_rev Term.lambda xs (ctor $ mk_absumprod ctr_absT abs n k xs))
   7.468 +            ks xss;
   7.469  
   7.470          val maybe_conceal_def_binding = Thm.def_binding
   7.471            #> Config.get no_defs_lthy bnf_note_all = false ? Binding.conceal;
   7.472 @@ -1200,28 +1222,27 @@
   7.473                          (mk_Trueprop_eq (HOLogic.mk_eq (u, ctor $ w), HOLogic.mk_eq (dtor $ u, w)));
   7.474                    in
   7.475                      Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} =>
   7.476 -                      mk_ctor_iff_dtor_tac ctxt (map (SOME o certifyT lthy) [dtorT, fpT])
   7.477 +                      mk_ctor_iff_dtor_tac ctxt (map (SOME o certifyT lthy) [ctr_absT, fpT])
   7.478                          (certify lthy ctor) (certify lthy dtor) ctor_dtor dtor_ctor)
   7.479                      |> Morphism.thm phi
   7.480                      |> Thm.close_derivation
   7.481                    end;
   7.482  
   7.483                  val sumEN_thm' =
   7.484 -                  unfold_thms lthy @{thms unit_all_eq1}
   7.485 -                    (Drule.instantiate' (map (SOME o certifyT lthy) ctr_prod_Ts) []
   7.486 -                       (mk_sumEN_balanced n))
   7.487 +                  unfold_thms lthy @{thms unit_all_eq1} (mk_absumprodE type_definition ms)
   7.488                    |> Morphism.thm phi;
   7.489                in
   7.490                  mk_exhaust_tac ctxt n ctr_defs ctor_iff_dtor_thm sumEN_thm'
   7.491                end;
   7.492  
   7.493              val inject_tacss =
   7.494 -              map2 (fn 0 => K [] | _ => fn ctr_def => [fn {context = ctxt, ...} =>
   7.495 -                mk_inject_tac ctxt ctr_def ctor_inject]) ms ctr_defs;
   7.496 +              map2 (fn ctr_def => fn 0 => [] | _ => [fn {context = ctxt, ...} =>
   7.497 +                mk_inject_tac ctxt ctr_def ctor_inject abs_inject]) ctr_defs ms;
   7.498  
   7.499              val half_distinct_tacss =
   7.500                map (map (fn (def, def') => fn {context = ctxt, ...} =>
   7.501 -                mk_half_distinct_tac ctxt ctor_inject [def, def'])) (mk_half_pairss (`I ctr_defs));
   7.502 +                  mk_half_distinct_tac ctxt ctor_inject abs_inject [def, def']))
   7.503 +                (mk_half_pairss (`I ctr_defs));
   7.504  
   7.505              val tacss = [exhaust_tac] :: inject_tacss @ half_distinct_tacss;
   7.506  
   7.507 @@ -1249,17 +1270,21 @@
   7.508                      cterm_instantiate_pos [NONE, NONE, SOME (certify lthy ctor')] arg_cong
   7.509                    end;
   7.510  
   7.511 -              fun mk_cIn ify =
   7.512 -                certify lthy o (fp = Greatest_FP ? curry (op $) (map_types ify ctor)) oo
   7.513 -                mk_InN_balanced (ify ctr_sum_prod_T) n;
   7.514 +              fun mk_cIn ctor k xs =
   7.515 +                let val absT = domain_type (fastype_of ctor) in
   7.516 +                  mk_absumprod absT abs n k xs
   7.517 +                  |> fp = Greatest_FP ? curry (op $) ctor
   7.518 +                  |> certify lthy
   7.519 +                end;
   7.520  
   7.521 -              val cxIns = map2 (mk_cIn I) tuple_xs ks;
   7.522 -              val cyIns = map2 (mk_cIn B_ify) tuple_ys ks;
   7.523 +              val cxIns = map2 (mk_cIn ctor) ks xss;
   7.524 +              val cyIns = map2 (mk_cIn (map_types B_ify ctor)) ks yss;
   7.525  
   7.526                fun mk_map_thm ctr_def' cxIn =
   7.527                  fold_thms lthy [ctr_def']
   7.528 -                  (unfold_thms lthy (pre_map_def ::
   7.529 -                       (if fp = Least_FP then [] else [ctor_dtor, dtor_ctor]) @ sum_prod_thms_map)
   7.530 +                  (unfold_thms lthy (o_apply :: pre_map_def ::
   7.531 +                       (if fp = Least_FP then [] else [ctor_dtor, dtor_ctor]) @ sum_prod_thms_map @
   7.532 +                       abs_inverses)
   7.533                       (cterm_instantiate_pos (nones @ [SOME cxIn])
   7.534                          (if fp = Least_FP then fp_map_thm else fp_map_thm RS ctor_cong)))
   7.535                  |> singleton (Proof_Context.export names_lthy no_defs_lthy);
   7.536 @@ -1267,7 +1292,8 @@
   7.537                fun mk_set_thm fp_set_thm ctr_def' cxIn =
   7.538                  fold_thms lthy [ctr_def']
   7.539                    (unfold_thms lthy (pre_set_defs @ nested_set_maps @ nesting_set_maps @
   7.540 -                       (if fp = Least_FP then [] else [dtor_ctor]) @ sum_prod_thms_set)
   7.541 +                       (if fp = Least_FP then [] else [dtor_ctor]) @ sum_prod_thms_set @
   7.542 +                       abs_inverses)
   7.543                       (cterm_instantiate_pos [SOME cxIn] fp_set_thm))
   7.544                  |> singleton (Proof_Context.export names_lthy no_defs_lthy);
   7.545  
   7.546 @@ -1281,8 +1307,9 @@
   7.547  
   7.548                fun mk_rel_thm postproc ctr_defs' cxIn cyIn =
   7.549                  fold_thms lthy ctr_defs'
   7.550 -                  (unfold_thms lthy (@{thm Inl_Inr_False} :: pre_rel_def ::
   7.551 -                       (if fp = Least_FP then [] else [dtor_ctor]) @ sum_prod_thms_rel)
   7.552 +                  (unfold_thms lthy (pre_rel_def :: abs_inverse ::
   7.553 +                       (if fp = Least_FP then [] else [dtor_ctor]) @ sum_prod_thms_rel @
   7.554 +                       @{thms vimage2p_def Inl_Inr_False})
   7.555                       (cterm_instantiate_pos (nones @ [SOME cxIn, SOME cyIn]) fp_rel_thm))
   7.556                  |> postproc
   7.557                  |> singleton (Proof_Context.export names_lthy no_defs_lthy);
   7.558 @@ -1341,9 +1368,11 @@
   7.559          (wrap_ctrs
   7.560           #> derive_maps_sets_rels
   7.561           ##>>
   7.562 -           (if fp = Least_FP then define_iters [foldN, recN] (the iters_args_types)
   7.563 -            else define_coiters [unfoldN, corecN] (the coiters_args_types))
   7.564 -             mk_binding fpTs Cs xtor_co_iters
   7.565 +           (if fp = Least_FP then
   7.566 +             define_iters [foldN, recN] (the iters_args_types) mk_binding fpTs Cs reps xtor_co_iters
   7.567 +           else
   7.568 +             define_coiters [unfoldN, corecN] (the coiters_args_types) mk_binding fpTs Cs abss
   7.569 +               xtor_co_iters)
   7.570           #> massage_res, lthy')
   7.571        end;
   7.572  
   7.573 @@ -1363,8 +1392,8 @@
   7.574        let
   7.575          val ((induct_thms, induct_thm, induct_attrs), (fold_thmss, rec_thmss, iter_attrs)) =
   7.576            derive_induct_iters_thms_for_types pre_bnfs (the iters_args_types) xtor_co_induct
   7.577 -            xtor_co_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss ctrss ctr_defss iterss
   7.578 -            iter_defss lthy;
   7.579 +            xtor_co_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss abs_inverses
   7.580 +            type_definitions abs_inverses ctrss ctr_defss iterss iter_defss lthy;
   7.581  
   7.582          val induct_type_attr = Attrib.internal o K o Induct.induct_type;
   7.583  
   7.584 @@ -1386,8 +1415,8 @@
   7.585          |> Spec_Rules.add Spec_Rules.Equational (map un_fold_of iterss, flat fold_thmss)
   7.586          |> Spec_Rules.add Spec_Rules.Equational (map co_rec_of iterss, flat rec_thmss)
   7.587          |> Local_Theory.notes (common_notes @ notes) |> snd
   7.588 -        |> register_fp_sugars Xs Least_FP pre_bnfs nested_bnfs nesting_bnfs fp_res ctrXs_Tsss
   7.589 -          ctr_defss ctr_sugars iterss mapss [induct_thm] (map single induct_thms)
   7.590 +        |> register_fp_sugars Xs Least_FP pre_bnfs absT_infos nested_bnfs nesting_bnfs fp_res
   7.591 +          ctrXs_Tsss ctr_defss ctr_sugars iterss mapss [induct_thm] (map single induct_thms)
   7.592            (transpose [fold_thmss, rec_thmss]) (replicate nn []) (replicate nn [])
   7.593        end;
   7.594  
   7.595 @@ -1403,8 +1432,8 @@
   7.596               (sel_unfold_thmsss, sel_corec_thmsss, sel_coiter_attrs)) =
   7.597            derive_coinduct_coiters_thms_for_types pre_bnfs (the coiters_args_types) xtor_co_induct
   7.598              dtor_injects dtor_ctors xtor_co_iter_thmss nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss mss ns
   7.599 -            ctr_defss ctr_sugars coiterss coiter_defss (Proof_Context.export lthy' no_defs_lthy)
   7.600 -            lthy;
   7.601 +            abs_inverses abs_inverses I ctr_defss ctr_sugars coiterss coiter_defss
   7.602 +            (Proof_Context.export lthy' no_defs_lthy) lthy;
   7.603  
   7.604          val sel_unfold_thmss = map flat sel_unfold_thmsss;
   7.605          val sel_corec_thmss = map flat sel_corec_thmsss;
   7.606 @@ -1451,8 +1480,8 @@
   7.607          |> add_spec_rules un_fold_of sel_unfold_thmss unfold_thmss
   7.608          |> add_spec_rules co_rec_of sel_corec_thmss corec_thmss
   7.609          |> Local_Theory.notes (common_notes @ notes) |> snd
   7.610 -        |> register_fp_sugars Xs Greatest_FP pre_bnfs nested_bnfs nesting_bnfs fp_res ctrXs_Tsss
   7.611 -          ctr_defss ctr_sugars coiterss mapss [coinduct_thm, strong_coinduct_thm]
   7.612 +        |> register_fp_sugars Xs Greatest_FP pre_bnfs absT_infos nested_bnfs nesting_bnfs fp_res
   7.613 +          ctrXs_Tsss ctr_defss ctr_sugars coiterss mapss [coinduct_thm, strong_coinduct_thm]
   7.614            (transpose [coinduct_thms, strong_coinduct_thms])
   7.615            (transpose [unfold_thmss, corec_thmss]) (transpose [disc_unfold_thmss, disc_corec_thmss])
   7.616            (transpose [sel_unfold_thmsss, sel_corec_thmsss])
   7.617 @@ -1462,8 +1491,9 @@
   7.618        |> fold_map define_ctrs_dtrs_for_type (fp_bnfs ~~ fp_bs ~~ fpTs ~~ ctors ~~ dtors ~~
   7.619          xtor_co_iterss ~~ ctor_dtors ~~ dtor_ctors ~~ ctor_injects ~~ pre_map_defs ~~
   7.620          pre_set_defss ~~ pre_rel_defs ~~ xtor_map_thms ~~ xtor_set_thmss ~~ xtor_rel_thms ~~ ns ~~
   7.621 -        kss ~~ mss ~~ ctr_bindingss ~~ ctr_mixfixess ~~ ctr_Tsss ~~ disc_bindingss ~~
   7.622 -        sel_bindingsss ~~ raw_sel_defaultsss)
   7.623 +        kss ~~ mss ~~ abss ~~ abs_injects ~~ abs_inverses ~~ type_definitions ~~
   7.624 +        ctr_bindingss ~~ ctr_mixfixess ~~ ctr_Tsss ~~ disc_bindingss ~~ sel_bindingsss ~~
   7.625 +        raw_sel_defaultsss)
   7.626        |> wrap_types_etc
   7.627        |> fp_case fp derive_note_induct_iters_thms_for_types
   7.628             derive_note_coinduct_coiters_thms_for_types;
     8.1 --- a/src/HOL/Tools/BNF/bnf_fp_def_sugar_tactics.ML	Fri Feb 28 12:04:40 2014 +0100
     8.2 +++ b/src/HOL/Tools/BNF/bnf_fp_def_sugar_tactics.ML	Tue Feb 25 18:14:26 2014 +0100
     8.3 @@ -12,17 +12,19 @@
     8.4    val sum_prod_thms_rel: thm list
     8.5  
     8.6    val mk_coinduct_tac: Proof.context -> thm list -> int -> int list -> thm -> thm list ->
     8.7 -    thm list -> thm list -> thm list list -> thm list list list -> thm list list list -> tactic
     8.8 -  val mk_coiter_tac: thm list -> thm list -> thm -> thm -> thm -> Proof.context -> tactic
     8.9 +    thm list -> thm list -> thm list -> thm list -> thm list list -> thm list list list ->
    8.10 +    thm list list list -> tactic
    8.11 +  val mk_coiter_tac: thm list -> thm list -> thm -> thm -> thm -> thm -> Proof.context -> tactic
    8.12    val mk_ctor_iff_dtor_tac: Proof.context -> ctyp option list -> cterm -> cterm -> thm -> thm ->
    8.13      tactic
    8.14    val mk_disc_coiter_iff_tac: thm list -> thm list -> thm list -> Proof.context -> tactic
    8.15    val mk_exhaust_tac: Proof.context -> int -> thm list -> thm -> thm -> tactic
    8.16 -  val mk_half_distinct_tac: Proof.context -> thm -> thm list -> tactic
    8.17 +  val mk_half_distinct_tac: Proof.context -> thm -> thm -> thm list -> tactic
    8.18    val mk_induct_tac: Proof.context -> int -> int list -> int list list -> int list list list ->
    8.19 -    thm list -> thm -> thm list -> thm list list -> tactic
    8.20 -  val mk_inject_tac: Proof.context -> thm -> thm -> tactic
    8.21 -  val mk_iter_tac: thm list -> thm list -> thm list -> thm -> thm -> Proof.context -> tactic
    8.22 +    thm list -> thm -> thm list -> thm list -> thm list -> thm list list -> tactic
    8.23 +  val mk_inject_tac: Proof.context -> thm -> thm -> thm -> tactic
    8.24 +  val mk_iter_tac: thm list -> thm list -> thm list -> thm -> thm -> thm -> thm -> Proof.context ->
    8.25 +    tactic
    8.26  end;
    8.27  
    8.28  structure BNF_FP_Def_Sugar_Tactics : BNF_FP_DEF_SUGAR_TACTICS =
    8.29 @@ -75,31 +77,35 @@
    8.30        SELECT_GOAL (unfold_thms_tac ctxt [th]) THEN'
    8.31        atac) [rev cTs, cTs] [cdtor, cctor] [dtor_ctor, ctor_dtor]));
    8.32  
    8.33 -fun mk_half_distinct_tac ctxt ctor_inject ctr_defs =
    8.34 -  unfold_thms_tac ctxt (ctor_inject :: @{thms sum.inject} @ ctr_defs) THEN
    8.35 +fun mk_half_distinct_tac ctxt ctor_inject abs_inject ctr_defs =
    8.36 +  unfold_thms_tac ctxt (ctor_inject :: abs_inject :: @{thms sum.inject} @ ctr_defs) THEN
    8.37    HEADGOAL (rtac @{thm sum.distinct(1)});
    8.38  
    8.39 -fun mk_inject_tac ctxt ctr_def ctor_inject =
    8.40 -  unfold_thms_tac ctxt [ctr_def] THEN HEADGOAL (rtac (ctor_inject RS ssubst)) THEN
    8.41 -  unfold_thms_tac ctxt @{thms sum.inject Pair_eq conj_assoc} THEN HEADGOAL (rtac refl);
    8.42 +fun mk_inject_tac ctxt ctr_def ctor_inject abs_inject =
    8.43 +  unfold_thms_tac ctxt [ctr_def] THEN
    8.44 +  HEADGOAL (rtac (ctor_inject RS ssubst)) THEN
    8.45 +  unfold_thms_tac ctxt (abs_inject :: @{thms sum.inject Pair_eq conj_assoc}) THEN
    8.46 +  HEADGOAL (rtac refl);
    8.47  
    8.48  val iter_unfold_thms =
    8.49    @{thms comp_def convol_def fst_conv id_def case_prod_Pair_iden snd_conv split_conv
    8.50        case_unit_Unity} @ sum_prod_thms_map;
    8.51  
    8.52 -fun mk_iter_tac pre_map_defs map_idents iter_defs ctor_iter ctr_def ctxt =
    8.53 -  unfold_thms_tac ctxt (ctr_def :: ctor_iter :: iter_defs @ pre_map_defs @ map_idents @
    8.54 -    iter_unfold_thms) THEN HEADGOAL (rtac refl);
    8.55 +fun mk_iter_tac pre_map_defs map_idents iter_defs ctor_iter fp_abs_inverse abs_inverse ctr_def ctxt =
    8.56 +  unfold_thms_tac ctxt (ctr_def :: ctor_iter :: fp_abs_inverse :: abs_inverse :: iter_defs @
    8.57 +    pre_map_defs @ map_idents @ iter_unfold_thms) THEN HEADGOAL (rtac refl);
    8.58  
    8.59  val coiter_unfold_thms = @{thms id_def} @ sum_prod_thms_map;
    8.60 -val ss_if_True_False = simpset_of (ss_only @{thms if_True if_False} @{context});
    8.61  
    8.62 -fun mk_coiter_tac coiter_defs map_idents ctor_dtor_coiter pre_map_def ctr_def ctxt =
    8.63 -  unfold_thms_tac ctxt (ctr_def :: coiter_defs) THEN
    8.64 -  HEADGOAL (rtac (ctor_dtor_coiter RS trans) THEN'
    8.65 -    asm_simp_tac (put_simpset ss_if_True_False ctxt)) THEN_MAYBE
    8.66 -  (unfold_thms_tac ctxt (pre_map_def :: map_idents @ coiter_unfold_thms) THEN
    8.67 -   HEADGOAL (rtac refl ORELSE' rtac (@{thm unit_eq} RS arg_cong)));
    8.68 +fun mk_coiter_tac coiter_defs map_idents ctor_dtor_coiter pre_map_def abs_inverse ctr_def ctxt =
    8.69 +  let
    8.70 +    val ss = ss_only (pre_map_def :: abs_inverse :: map_idents @ coiter_unfold_thms @
    8.71 +      @{thms o_apply vimage2p_def if_True if_False}) ctxt;
    8.72 +  in
    8.73 +    unfold_thms_tac ctxt (ctr_def :: coiter_defs) THEN
    8.74 +    HEADGOAL (rtac (ctor_dtor_coiter RS trans) THEN' asm_simp_tac ss) THEN_MAYBE
    8.75 +    HEADGOAL (rtac refl ORELSE' rtac (@{thm unit_eq} RS arg_cong))
    8.76 +  end;
    8.77  
    8.78  fun mk_disc_coiter_iff_tac case_splits' coiters discs ctxt =
    8.79    EVERY (map3 (fn case_split_tac => fn coiter_thm => fn disc =>
    8.80 @@ -113,35 +119,43 @@
    8.81      hyp_subst_tac ctxt ORELSE' resolve_tac @{thms disjI1 disjI2}) THEN'
    8.82    (rtac refl ORELSE' atac ORELSE' rtac @{thm singletonI});
    8.83  
    8.84 -fun mk_induct_leverage_prem_prems_tac ctxt nn kks set_maps pre_set_defs =
    8.85 +fun mk_induct_leverage_prem_prems_tac ctxt nn kks fp_abs_inverses abs_inverses set_maps
    8.86 +    pre_set_defs =
    8.87    HEADGOAL (EVERY' (maps (fn kk => [select_prem_tac nn (dtac meta_spec) kk, etac meta_mp,
    8.88 -    SELECT_GOAL (unfold_thms_tac ctxt (pre_set_defs @ set_maps @ sum_prod_thms_set0)),
    8.89 +    SELECT_GOAL (unfold_thms_tac ctxt (pre_set_defs @ fp_abs_inverses @ abs_inverses @ set_maps @
    8.90 +      sum_prod_thms_set0)),
    8.91      solve_prem_prem_tac ctxt]) (rev kks)));
    8.92  
    8.93 -fun mk_induct_discharge_prem_tac ctxt nn n set_maps pre_set_defs m k kks =
    8.94 +fun mk_induct_discharge_prem_tac ctxt nn n fp_abs_inverses abs_inverses set_maps pre_set_defs m k
    8.95 +    kks =
    8.96    let val r = length kks in
    8.97      HEADGOAL (EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac ctxt,
    8.98        REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)]) THEN
    8.99      EVERY [REPEAT_DETERM_N r
   8.100          (HEADGOAL (rotate_tac ~1 THEN' dtac meta_mp THEN' rotate_tac 1) THEN prefer_tac 2),
   8.101        if r > 0 then ALLGOALS (Goal.norm_hhf_tac ctxt) else all_tac, HEADGOAL atac,
   8.102 -      mk_induct_leverage_prem_prems_tac ctxt nn kks set_maps pre_set_defs]
   8.103 +      mk_induct_leverage_prem_prems_tac ctxt nn kks fp_abs_inverses abs_inverses set_maps
   8.104 +        pre_set_defs]
   8.105    end;
   8.106  
   8.107 -fun mk_induct_tac ctxt nn ns mss kkss ctr_defs ctor_induct' set_maps pre_set_defss =
   8.108 +fun mk_induct_tac ctxt nn ns mss kkss ctr_defs ctor_induct' fp_abs_inverses abs_inverses set_maps
   8.109 +    pre_set_defss =
   8.110    let val n = Integer.sum ns in
   8.111      unfold_thms_tac ctxt ctr_defs THEN HEADGOAL (rtac ctor_induct') THEN
   8.112      co_induct_inst_as_projs_tac ctxt 0 THEN
   8.113 -    EVERY (map4 (EVERY oooo map3 o mk_induct_discharge_prem_tac ctxt nn n set_maps) pre_set_defss
   8.114 -      mss (unflat mss (1 upto n)) kkss)
   8.115 +    EVERY (map4 (EVERY oooo map3 o
   8.116 +        mk_induct_discharge_prem_tac ctxt nn n fp_abs_inverses abs_inverses set_maps)
   8.117 +      pre_set_defss mss (unflat mss (1 upto n)) kkss)
   8.118    end;
   8.119  
   8.120 -fun mk_coinduct_same_ctr_tac ctxt rel_eqs pre_rel_def dtor_ctor ctr_def discs sels =
   8.121 +fun mk_coinduct_same_ctr_tac ctxt rel_eqs pre_rel_def fp_abs_inverse abs_inverse dtor_ctor ctr_def
   8.122 +    discs sels =
   8.123    hyp_subst_tac ctxt THEN'
   8.124    CONVERSION (hhf_concl_conv
   8.125      (Conv.top_conv (K (Conv.try_conv (Conv.rewr_conv ctr_def))) ctxt) ctxt) THEN'
   8.126    SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels)) THEN'
   8.127 -  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels @ sum_prod_thms_rel)) THEN'
   8.128 +  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: fp_abs_inverse :: abs_inverse :: dtor_ctor ::
   8.129 +    sels @ sum_prod_thms_rel @ @{thms o_apply vimage2p_def})) THEN'
   8.130    (atac ORELSE' REPEAT o etac conjE THEN'
   8.131       full_simp_tac
   8.132         (ss_only (@{thm prod.inject} :: no_refl discs @ rel_eqs @ more_simp_thms) ctxt) THEN'
   8.133 @@ -157,8 +171,8 @@
   8.134      full_simp_tac (ss_only (refl :: no_refl discs'' @ basic_simp_thms) ctxt)
   8.135    end;
   8.136  
   8.137 -fun mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn kk n pre_rel_def dtor_ctor exhaust ctr_defs
   8.138 -    discss selss =
   8.139 +fun mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn kk n pre_rel_def fp_abs_inverse abs_inverse
   8.140 +    dtor_ctor exhaust ctr_defs discss selss =
   8.141    let val ks = 1 upto n in
   8.142      EVERY' ([rtac allI, rtac allI, rtac impI, select_prem_tac nn (dtac meta_spec) kk,
   8.143          dtac meta_spec, dtac meta_mp, atac, rtac exhaust, K (co_induct_inst_as_projs_tac ctxt 0),
   8.144 @@ -167,15 +181,17 @@
   8.145          EVERY' ([rtac exhaust, K (co_induct_inst_as_projs_tac ctxt 1)] @
   8.146            map2 (fn k' => fn discs' =>
   8.147              if k' = k then
   8.148 -              mk_coinduct_same_ctr_tac ctxt rel_eqs' pre_rel_def dtor_ctor ctr_def discs sels
   8.149 +              mk_coinduct_same_ctr_tac ctxt rel_eqs' pre_rel_def fp_abs_inverse abs_inverse
   8.150 +                dtor_ctor ctr_def discs sels
   8.151              else
   8.152                mk_coinduct_distinct_ctrs_tac ctxt discs discs') ks discss)) ks ctr_defs discss selss)
   8.153    end;
   8.154  
   8.155 -fun mk_coinduct_tac ctxt rel_eqs' nn ns dtor_coinduct' pre_rel_defs dtor_ctors exhausts ctr_defss
   8.156 -    discsss selsss =
   8.157 +fun mk_coinduct_tac ctxt rel_eqs' nn ns dtor_coinduct' pre_rel_defs fp_abs_inverses abs_inverses
   8.158 +    dtor_ctors exhausts ctr_defss discsss selsss =
   8.159    HEADGOAL (rtac dtor_coinduct' THEN'
   8.160 -    EVERY' (map8 (mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn)
   8.161 -      (1 upto nn) ns pre_rel_defs dtor_ctors exhausts ctr_defss discsss selsss));
   8.162 +    EVERY' (map10 (mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn)
   8.163 +      (1 upto nn) ns pre_rel_defs fp_abs_inverses abs_inverses dtor_ctors exhausts ctr_defss discsss
   8.164 +      selsss));
   8.165  
   8.166  end;
     9.1 --- a/src/HOL/Tools/BNF/bnf_fp_n2m.ML	Fri Feb 28 12:04:40 2014 +0100
     9.2 +++ b/src/HOL/Tools/BNF/bnf_fp_n2m.ML	Tue Feb 25 18:14:26 2014 +0100
     9.3 @@ -9,7 +9,7 @@
     9.4  sig
     9.5    val construct_mutualized_fp: BNF_Util.fp_kind -> typ list -> BNF_FP_Def_Sugar.fp_sugar list ->
     9.6      binding list -> (string * sort) list -> typ list * typ list list -> BNF_Def.bnf list ->
     9.7 -    local_theory -> BNF_FP_Util.fp_result * local_theory
     9.8 +    BNF_Comp.absT_info list -> local_theory -> BNF_FP_Util.fp_result * local_theory
     9.9  end;
    9.10  
    9.11  structure BNF_FP_N2M : BNF_FP_N2M =
    9.12 @@ -17,6 +17,7 @@
    9.13  
    9.14  open BNF_Def
    9.15  open BNF_Util
    9.16 +open BNF_Comp
    9.17  open BNF_FP_Util
    9.18  open BNF_FP_Def_Sugar
    9.19  open BNF_Tactics
    9.20 @@ -45,28 +46,16 @@
    9.21      Const (@{const_name sum_map}, fT --> gT --> mk_sumT (fAT, gAT) --> mk_sumT (fBT, gBT)) $ f $ g
    9.22    end;
    9.23  
    9.24 -fun construct_mutualized_fp fp fpTs (fp_sugars : fp_sugar list) bs resBs (resDs, Dss) bnfs lthy =
    9.25 +fun construct_mutualized_fp fp fpTs (fp_sugars : fp_sugar list) bs resBs (resDs, Dss) bnfs
    9.26 +    absT_infos lthy =
    9.27    let
    9.28 -    fun steal_fp_res get =
    9.29 +    fun of_fp_res get =
    9.30        map (fn {fp_res, fp_res_index, ...} => nth (get fp_res) fp_res_index) fp_sugars;
    9.31  
    9.32 -    val n = length bnfs;
    9.33 -    val deads = fold (union (op =)) Dss resDs;
    9.34 -    val As = subtract (op =) deads (map TFree resBs);
    9.35 -    val names_lthy = fold Variable.declare_typ (As @ deads) lthy;
    9.36 -    val m = length As;
    9.37 -    val live = m + n;
    9.38 -    val ((Xs, Bs), names_lthy) = names_lthy
    9.39 -      |> mk_TFrees n
    9.40 -      ||>> mk_TFrees m;
    9.41 -    val allAs = As @ Xs;
    9.42 -    val phiTs = map2 mk_pred2T As Bs;
    9.43 -    val theta = As ~~ Bs;
    9.44 -    val fpTs' = map (Term.typ_subst_atomic theta) fpTs;
    9.45 -    val pre_phiTs = map2 mk_pred2T fpTs fpTs';
    9.46 -
    9.47      fun mk_co_algT T U = fp_case fp (T --> U) (U --> T);
    9.48      fun co_swap pair = fp_case fp I swap pair;
    9.49 +    val mk_co_comp = HOLogic.mk_comp o co_swap;
    9.50 +
    9.51      val dest_co_algT = co_swap o dest_funT;
    9.52      val co_alg_argT = fp_case fp range_type domain_type;
    9.53      val co_alg_funT = fp_case fp domain_type range_type;
    9.54 @@ -75,30 +64,78 @@
    9.55      val co_proj1_const = fp_case fp (fst_const o fst) (uncurry Inl_const o dest_sumT o snd);
    9.56      val mk_co_productT = curry (fp_case fp HOLogic.mk_prodT mk_sumT);
    9.57      val dest_co_productT = fp_case fp HOLogic.dest_prodT dest_sumT;
    9.58 +    val rewrite_comp_comp = fp_case fp @{thm rewriteL_comp_comp} @{thm rewriteR_comp_comp};
    9.59 +
    9.60 +    val fp_absT_infos = map #absT_info fp_sugars;
    9.61 +    val fp_bnfs = of_fp_res #bnfs;
    9.62 +    val pre_bnfs = map #pre_bnf fp_sugars;
    9.63 +    val nesty_bnfss = map (fn sugar => #nested_bnfs sugar @ #nesting_bnfs sugar) fp_sugars;
    9.64 +    val fp_nesty_bnfss = fp_bnfs :: nesty_bnfss;
    9.65 +    val fp_nesty_bnfs = distinct (op = o pairself T_of_bnf) (flat fp_nesty_bnfss);
    9.66 +
    9.67 +    val fp_absTs = map #absT fp_absT_infos;
    9.68 +    val fp_repTs = map #repT fp_absT_infos;
    9.69 +    val fp_abss = map #abs fp_absT_infos;
    9.70 +    val fp_reps = map #rep fp_absT_infos;
    9.71 +    val fp_type_definitions = map #type_definition fp_absT_infos;
    9.72 +
    9.73 +    val absTs = map #absT absT_infos;
    9.74 +    val repTs = map #repT absT_infos;
    9.75 +    val absTs' = map (Logic.type_map (singleton (Variable.polymorphic lthy))) absTs;
    9.76 +    val repTs' = map (Logic.type_map (singleton (Variable.polymorphic lthy))) repTs;
    9.77 +    val abss = map #abs absT_infos;
    9.78 +    val reps = map #rep absT_infos;
    9.79 +    val abs_inverses = map #abs_inverse absT_infos;
    9.80 +    val type_definitions = map #type_definition absT_infos;
    9.81 +
    9.82 +    val n = length bnfs;
    9.83 +    val deads = fold (union (op =)) Dss resDs;
    9.84 +    val As = subtract (op =) deads (map TFree resBs);
    9.85 +    val names_lthy = fold Variable.declare_typ (As @ deads) lthy;
    9.86 +    val m = length As;
    9.87 +    val live = m + n;
    9.88 +
    9.89 +    val ((Xs, Bs), names_lthy) = names_lthy
    9.90 +      |> mk_TFrees n
    9.91 +      ||>> mk_TFrees m;
    9.92 +
    9.93 +    val allAs = As @ Xs;
    9.94 +    val allBs = Bs @ Xs;
    9.95 +    val phiTs = map2 mk_pred2T As Bs;
    9.96 +    val thetaBs = As ~~ Bs;
    9.97 +    val fpTs' = map (Term.typ_subst_atomic thetaBs) fpTs;
    9.98 +    val fold_thetaAs = Xs ~~ fpTs;
    9.99 +    val fold_thetaBs = Xs ~~ fpTs';
   9.100 +    val rec_theta = Xs ~~ map2 mk_co_productT fpTs Xs;
   9.101 +    val pre_phiTs = map2 mk_pred2T fpTs fpTs';
   9.102  
   9.103      val ((ctors, dtors), (xtor's, xtors)) =
   9.104        let
   9.105 -        val ctors = map2 (force_typ names_lthy o (fn T => dummyT --> T)) fpTs (steal_fp_res #ctors);
   9.106 -        val dtors = map2 (force_typ names_lthy o (fn T => T --> dummyT)) fpTs (steal_fp_res #dtors);
   9.107 +        val ctors = map2 (force_typ names_lthy o (fn T => dummyT --> T)) fpTs (of_fp_res #ctors);
   9.108 +        val dtors = map2 (force_typ names_lthy o (fn T => T --> dummyT)) fpTs (of_fp_res #dtors);
   9.109        in
   9.110 -        ((ctors, dtors), `(map (Term.subst_atomic_types theta)) (fp_case fp ctors dtors))
   9.111 +        ((ctors, dtors), `(map (Term.subst_atomic_types thetaBs)) (fp_case fp ctors dtors))
   9.112        end;
   9.113  
   9.114 +    val absATs = map (domain_type o fastype_of) ctors;
   9.115 +    val absBTs = map (Term.typ_subst_atomic thetaBs) absATs;
   9.116      val xTs = map (domain_type o fastype_of) xtors;
   9.117      val yTs = map (domain_type o fastype_of) xtor's;
   9.118  
   9.119 +    fun abs_of allAs Ds bnf = mk_abs (mk_T_of_bnf Ds allAs bnf) o #abs;
   9.120 +    fun rep_of absAT = mk_rep absAT o #rep;
   9.121 +
   9.122 +    val absAs = map3 (abs_of allAs) Dss bnfs absT_infos;
   9.123 +    val absBs = map3 (abs_of allBs) Dss bnfs absT_infos;
   9.124 +    val fp_repAs = map2 rep_of absATs fp_absT_infos;
   9.125 +    val fp_repBs = map2 rep_of absBTs fp_absT_infos;
   9.126 +
   9.127      val (((((phis, phis'), pre_phis), xs), ys), names_lthy) = names_lthy
   9.128        |> mk_Frees' "R" phiTs
   9.129        ||>> mk_Frees "S" pre_phiTs
   9.130        ||>> mk_Frees "x" xTs
   9.131        ||>> mk_Frees "y" yTs;
   9.132  
   9.133 -    val fp_bnfs = steal_fp_res #bnfs;
   9.134 -    val pre_bnfs = map #pre_bnf fp_sugars;
   9.135 -    val nesty_bnfss = map (fn sugar => #nested_bnfs sugar @ #nesting_bnfs sugar) fp_sugars;
   9.136 -    val fp_nesty_bnfss = fp_bnfs :: nesty_bnfss;
   9.137 -    val fp_nesty_bnfs = distinct (op = o pairself T_of_bnf) (flat fp_nesty_bnfss);
   9.138 -
   9.139      val rels =
   9.140        let
   9.141          fun find_rel T As Bs = fp_nesty_bnfss
   9.142 @@ -127,15 +164,28 @@
   9.143      val pre_rels = map2 (fn Ds => mk_rel_of_bnf Ds (As @ fpTs) (Bs @ fpTs')) Dss bnfs;
   9.144  
   9.145      val rel_unfolds = maps (no_refl o single o rel_def_of_bnf) pre_bnfs;
   9.146 -    val rel_xtor_co_inducts = steal_fp_res (split_conj_thm o #rel_xtor_co_induct_thm)
   9.147 +    val rel_xtor_co_inducts = of_fp_res (split_conj_thm o #rel_xtor_co_induct_thm)
   9.148        |> map (unfold_thms lthy (id_apply :: rel_unfolds));
   9.149  
   9.150      val rel_defs = map rel_def_of_bnf bnfs;
   9.151      val rel_monos = map rel_mono_of_bnf bnfs;
   9.152  
   9.153 +    fun cast castA castB pre_rel =
   9.154 +      let
   9.155 +        val castAB = mk_vimage2p (Term.subst_atomic_types fold_thetaAs castA)
   9.156 +          (Term.subst_atomic_types fold_thetaBs castB);
   9.157 +      in
   9.158 +        fold_rev (fold_rev Term.absdummy) [phiTs, pre_phiTs]
   9.159 +          (castAB $ Term.list_comb (pre_rel, map Bound (live - 1 downto 0)))
   9.160 +      end;
   9.161 +
   9.162 +    val castAs = map2 (curry HOLogic.mk_comp) absAs fp_repAs;
   9.163 +    val castBs = map2 (curry HOLogic.mk_comp) absBs fp_repBs;
   9.164 +
   9.165      val rel_xtor_co_induct_thm =
   9.166 -      mk_rel_xtor_co_induct_thm fp pre_rels pre_phis rels phis xs ys xtors xtor's
   9.167 -        (mk_rel_xtor_co_induct_tactic fp rel_xtor_co_inducts rel_defs rel_monos) lthy;
   9.168 +      mk_rel_xtor_co_induct_thm fp (map3 cast castAs castBs pre_rels) pre_phis rels phis xs ys xtors
   9.169 +        xtor's (mk_rel_xtor_co_induct_tactic fp abs_inverses rel_xtor_co_inducts rel_defs rel_monos)
   9.170 +        lthy;
   9.171  
   9.172      val rel_eqs = no_refl (map rel_eq_of_bnf fp_nesty_bnfs);
   9.173      val map_id0s = no_refl (map map_id0_of_bnf bnfs);
   9.174 @@ -150,16 +200,22 @@
   9.175                let val T = domain_type (fastype_of P);
   9.176                in mk_Grp (HOLogic.Collect_const T $ P) (HOLogic.id_const T) end;
   9.177              val cts = map (SOME o certify lthy) (map HOLogic.eq_const As @ map mk_Grp_id Ps);
   9.178 +            fun mk_fp_type_copy_thms thm = map (curry op RS thm)
   9.179 +              @{thms type_copy_Abs_o_Rep type_copy_vimage2p_Grp_Rep};
   9.180 +            fun mk_type_copy_thms thm = map (curry op RS thm)
   9.181 +              @{thms type_copy_Rep_o_Abs type_copy_vimage2p_Grp_Abs};
   9.182            in
   9.183              cterm_instantiate_pos cts rel_xtor_co_induct_thm
   9.184              |> singleton (Proof_Context.export names_lthy lthy)
   9.185              |> unfold_thms lthy (@{thms eq_le_Grp_id_iff all_simps(1,2)[symmetric]} @ rel_eqs)
   9.186              |> funpow n (fn thm => thm RS spec)
   9.187              |> unfold_thms lthy (@{thm eq_alt} :: map rel_Grp_of_bnf bnfs @ map_id0s)
   9.188 -            |> unfold_thms lthy @{thms Grp_id_mono_subst eqTrueI[OF subset_UNIV] simp_thms(22)}
   9.189 +            |> unfold_thms lthy (@{thms vimage2p_comp comp_apply comp_id
   9.190 +               Grp_id_mono_subst eqTrueI[OF subset_UNIV] simp_thms(22)} @
   9.191 +               maps mk_fp_type_copy_thms fp_type_definitions @
   9.192 +               maps mk_type_copy_thms type_definitions)
   9.193              |> unfold_thms lthy @{thms subset_iff mem_Collect_eq
   9.194                 atomize_conjL[symmetric] atomize_all[symmetric] atomize_imp[symmetric]}
   9.195 -            |> unfold_thms lthy (maps set_defs_of_bnf bnfs)
   9.196            end
   9.197        | Greatest_FP =>
   9.198            let
   9.199 @@ -173,8 +229,6 @@
   9.200            end);
   9.201  
   9.202      val fold_preTs = map2 (fn Ds => mk_T_of_bnf Ds allAs) Dss bnfs;
   9.203 -    val fold_pre_deads_only_Ts = map2 (fn Ds => mk_T_of_bnf Ds (replicate live dummyT)) Dss bnfs;
   9.204 -    val rec_theta = Xs ~~ map2 mk_co_productT fpTs Xs;
   9.205      val rec_preTs = map (Term.typ_subst_atomic rec_theta) fold_preTs;
   9.206  
   9.207      val fold_strTs = map2 mk_co_algT fold_preTs Xs;
   9.208 @@ -185,32 +239,52 @@
   9.209        |> mk_Frees' "s" fold_strTs
   9.210        ||>> mk_Frees' "s" rec_strTs;
   9.211  
   9.212 -    val co_iters = steal_fp_res #xtor_co_iterss;
   9.213 +    val co_iters = of_fp_res #xtor_co_iterss;
   9.214      val ns = map (length o #Ts o #fp_res) fp_sugars;
   9.215  
   9.216      fun substT rho (Type (@{type_name "fun"}, [T, U])) = substT rho T --> substT rho U
   9.217        | substT rho (Type (s, Ts)) = Type (s, map (typ_subst_nonatomic rho) Ts)
   9.218        | substT _ T = T;
   9.219  
   9.220 +    val typ_subst_nonatomic_sorted = fold_rev (typ_subst_nonatomic o single);
   9.221 +
   9.222      fun force_iter is_rec i TU TU_rec raw_iters =
   9.223        let
   9.224 +        val thy = Proof_Context.theory_of lthy;
   9.225 +
   9.226          val approx_fold = un_fold_of raw_iters
   9.227            |> force_typ names_lthy
   9.228              (replicate (nth ns i) dummyT ---> (if is_rec then TU_rec else TU));
   9.229 -        val subst = Term.typ_subst_atomic (Xs ~~ fpTs);
   9.230 +        val subst = Term.typ_subst_atomic fold_thetaAs;
   9.231 +
   9.232 +        fun mk_fp_absT_repT fp_repT fp_absT = mk_absT thy fp_repT fp_absT ooo mk_repT;
   9.233 +        val mk_fp_absT_repTs = map5 mk_fp_absT_repT fp_repTs fp_absTs absTs repTs;
   9.234 +
   9.235 +        val fold_preTs' = mk_fp_absT_repTs (map subst fold_preTs);
   9.236 +
   9.237 +        val fold_pre_deads_only_Ts =
   9.238 +          map (typ_subst_nonatomic_sorted (map (rpair dummyT) (As @ fpTs))) fold_preTs';
   9.239 +
   9.240          val TUs = map_split dest_co_algT (binder_fun_types (fastype_of approx_fold))
   9.241            |>> map subst
   9.242            |> uncurry (map2 mk_co_algT);
   9.243 -        val js = find_indices Type.could_unify TUs
   9.244 -          (map2 (fn T => fn U => mk_co_algT (subst T) U) fold_preTs Xs);
   9.245 +        val cands = map2 mk_co_algT fold_preTs' Xs;
   9.246 +
   9.247 +        val js = find_indices Type.could_unify TUs cands;
   9.248          val Tpats = map (fn j => mk_co_algT (nth fold_pre_deads_only_Ts j) (nth Xs j)) js;
   9.249          val iter = raw_iters |> (if is_rec then co_rec_of else un_fold_of);
   9.250        in
   9.251          force_typ names_lthy (Tpats ---> TU) iter
   9.252        end;
   9.253  
   9.254 +    fun mk_co_comp_abs_rep fp_absT absT fp_abs fp_rep abs rep t =
   9.255 +      fp_case fp (HOLogic.mk_comp (HOLogic.mk_comp (t, mk_abs absT abs), mk_rep fp_absT fp_rep))
   9.256 +        (HOLogic.mk_comp (mk_abs fp_absT fp_abs, HOLogic.mk_comp (mk_rep absT rep, t)));
   9.257 +
   9.258      fun mk_iter b_opt is_rec iters lthy TU =
   9.259        let
   9.260 +        val thy = Proof_Context.theory_of lthy;
   9.261 +
   9.262          val x = co_alg_argT TU;
   9.263          val i = find_index (fn T => x = T) Xs;
   9.264          val TUiter =
   9.265 @@ -220,20 +294,32 @@
   9.266                  (TU |> (is_none b_opt andalso not is_rec) ? substT (fpTs ~~ Xs))
   9.267                  (TU |> (is_none b_opt) ? substT (map2 mk_co_productT fpTs Xs ~~ Xs))
   9.268            | SOME f => f);
   9.269 +
   9.270          val TUs = binder_fun_types (fastype_of TUiter);
   9.271          val iter_preTs = if is_rec then rec_preTs else fold_preTs;
   9.272          val iter_strs = if is_rec then rec_strs else fold_strs;
   9.273 +
   9.274          fun mk_s TU' =
   9.275            let
   9.276 +            fun mk_absT_fp_repT repT absT = mk_absT thy repT absT ooo mk_repT;
   9.277 +
   9.278              val i = find_index (fn T => co_alg_argT TU' = T) Xs;
   9.279 +            val fp_abs = nth fp_abss i;
   9.280 +            val fp_rep = nth fp_reps i;
   9.281 +            val abs = nth abss i;
   9.282 +            val rep = nth reps i;
   9.283              val sF = co_alg_funT TU';
   9.284 +            val sF' =
   9.285 +              mk_absT_fp_repT (nth repTs' i) (nth absTs' i) (nth fp_absTs i) (nth fp_repTs i) sF
   9.286 +                handle Term.TYPE _ => sF;
   9.287              val F = nth iter_preTs i;
   9.288              val s = nth iter_strs i;
   9.289            in
   9.290 -            (if sF = F then s
   9.291 +            if sF = F then s
   9.292 +            else if sF' = F then mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep s
   9.293              else
   9.294                let
   9.295 -                val smapT = replicate live dummyT ---> mk_co_algT sF F;
   9.296 +                val smapT = replicate live dummyT ---> mk_co_algT sF' F;
   9.297                  fun hidden_to_unit t =
   9.298                    Term.subst_TVars (map (rpair HOLogic.unitT) (Term.add_tvar_names t [])) t;
   9.299                  val smap = map_of_bnf (nth bnfs i)
   9.300 @@ -260,8 +346,9 @@
   9.301                      fst (fst (mk_iter NONE is_rec iters lthy TU)))
   9.302                  val smap_args = map mk_smap_arg smap_argTs;
   9.303                in
   9.304 -                HOLogic.mk_comp (co_swap (s, Term.list_comb (smap, smap_args)))
   9.305 -              end)
   9.306 +                mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep
   9.307 +                  (mk_co_comp (s, Term.list_comb (smap, smap_args)))
   9.308 +              end
   9.309            end;
   9.310          val t = Term.list_comb (TUiter, map mk_s TUs);
   9.311        in
   9.312 @@ -306,13 +393,19 @@
   9.313                map HOLogic.id_const As @ map2 (mk_co_product o HOLogic.id_const) fpTs recs))
   9.314            Dss bnfs;
   9.315  
   9.316 -        fun mk_goals f xtor s smap =
   9.317 -          ((f, xtor), (s, smap))
   9.318 -          |> pairself (HOLogic.mk_comp o co_swap)
   9.319 -          |> HOLogic.mk_eq;
   9.320 +        fun mk_goals f xtor s smap fp_abs fp_rep abs rep =
   9.321 +          let
   9.322 +            val lhs = mk_co_comp (f, xtor);
   9.323 +            val rhs = mk_co_comp (s, smap);
   9.324 +          in
   9.325 +            HOLogic.mk_eq (lhs,
   9.326 +              mk_co_comp_abs_rep (co_alg_funT (fastype_of lhs)) (co_alg_funT (fastype_of rhs))
   9.327 +                fp_abs fp_rep abs rep rhs)
   9.328 +          end;
   9.329  
   9.330 -        val fold_goals = map4 mk_goals folds xtors fold_strs pre_fold_maps
   9.331 -        val rec_goals = map4 mk_goals recs xtors rec_strs pre_rec_maps;
   9.332 +        val fold_goals =
   9.333 +          map8 mk_goals folds xtors fold_strs pre_fold_maps fp_abss fp_reps abss reps;
   9.334 +        val rec_goals = map8 mk_goals recs xtors rec_strs pre_rec_maps fp_abss fp_reps abss reps;
   9.335  
   9.336          fun mk_thms ss goals tac =
   9.337            Library.foldr1 HOLogic.mk_conj goals
   9.338 @@ -327,27 +420,42 @@
   9.339          val pre_map_defs = no_refl (map map_def_of_bnf bnfs);
   9.340          val fp_pre_map_defs = no_refl (map map_def_of_bnf pre_bnfs);
   9.341  
   9.342 -        val map_unfolds = maps (fn bnf => no_refl [map_def_of_bnf bnf]) pre_bnfs;
   9.343 -        val unfold_map = map (unfold_thms lthy (id_apply :: map_unfolds));
   9.344 +        val unfold_map = map (unfold_thms lthy (id_apply :: pre_map_defs));
   9.345 +
   9.346 +        val fp_xtor_co_iterss = of_fp_res #xtor_co_iter_thmss;
   9.347 +        val fp_xtor_un_folds = map (mk_pointfree lthy o un_fold_of) fp_xtor_co_iterss;
   9.348 +        val fp_xtor_co_recs = map (mk_pointfree lthy o co_rec_of) fp_xtor_co_iterss;
   9.349  
   9.350 -        val fp_xtor_co_iterss = steal_fp_res #xtor_co_iter_thmss;
   9.351 -        val fp_xtor_un_folds = map (mk_pointfree lthy o un_fold_of) fp_xtor_co_iterss |> unfold_map;
   9.352 -        val fp_xtor_co_recs = map (mk_pointfree lthy o co_rec_of) fp_xtor_co_iterss |> unfold_map;
   9.353 -
   9.354 -        val fp_co_iter_o_mapss = steal_fp_res #xtor_co_iter_o_map_thmss;
   9.355 -        val fp_fold_o_maps = map un_fold_of fp_co_iter_o_mapss |> unfold_map;
   9.356 -        val fp_rec_o_maps = map co_rec_of fp_co_iter_o_mapss |> unfold_map;
   9.357 -        val fold_thms = fp_case fp @{thm comp_assoc} @{thm comp_assoc[symmetric]} :: @{thms id_apply
   9.358 -          o_apply comp_id id_comp map_pair.comp map_pair.id sum_map.comp sum_map.id};
   9.359 +        val fp_co_iter_o_mapss = of_fp_res #xtor_co_iter_o_map_thmss;
   9.360 +        val fp_fold_o_maps = map un_fold_of fp_co_iter_o_mapss;
   9.361 +        val fp_rec_o_maps = map co_rec_of fp_co_iter_o_mapss;
   9.362 +        val fold_thms = fp_case fp @{thm comp_assoc} @{thm comp_assoc[symmetric]} ::
   9.363 +          map (fn thm => thm RS rewrite_comp_comp) @{thms map_pair.comp sum_map.comp} @
   9.364 +          @{thms id_apply comp_id id_comp map_pair.comp map_pair.id sum_map.comp sum_map.id};
   9.365          val rec_thms = fold_thms @ fp_case fp
   9.366            @{thms fst_convol map_pair_o_convol convol_o}
   9.367            @{thms case_sum_o_inj(1) case_sum_o_sum_map o_case_sum};
   9.368 +
   9.369 +        val eq_thm_prop_untyped = op Term.aconv_untyped o pairself Thm.full_prop_of;
   9.370 +
   9.371          val map_thms = no_refl (maps (fn bnf =>
   9.372 -          [map_comp0_of_bnf bnf RS sym, map_id0_of_bnf bnf]) fp_nesty_bnfs);
   9.373 +           let val map_comp0 = map_comp0_of_bnf bnf RS sym
   9.374 +           in [map_comp0, map_comp0 RS rewrite_comp_comp, map_id0_of_bnf bnf] end) fp_nesty_bnfs) @
   9.375 +          remove eq_thm_prop_untyped (fp_case fp @{thm comp_assoc[symmetric]} @{thm comp_assoc})
   9.376 +          (map2 (fn thm => fn bnf =>
   9.377 +            @{thm type_copy_map_comp0_undo} OF
   9.378 +              (replicate 3 thm @ unfold_map [map_comp0_of_bnf bnf]) RS
   9.379 +              rewrite_comp_comp)
   9.380 +          type_definitions bnfs);
   9.381 +
   9.382 +        fun mk_Rep_o_Abs thm = thm RS @{thm type_copy_Rep_o_Abs} RS rewrite_comp_comp;
   9.383 +
   9.384 +        val fp_Rep_o_Abss = map mk_Rep_o_Abs fp_type_definitions;
   9.385 +        val Rep_o_Abss = map mk_Rep_o_Abs type_definitions;
   9.386  
   9.387          fun mk_tac defs o_map_thms xtor_thms thms {context = ctxt, prems = _} =
   9.388 -          unfold_thms_tac ctxt
   9.389 -            (flat [thms, defs, pre_map_defs, fp_pre_map_defs, xtor_thms, o_map_thms, map_thms]) THEN
   9.390 +          unfold_thms_tac ctxt (flat [thms, defs, pre_map_defs, fp_pre_map_defs,
   9.391 +            xtor_thms, o_map_thms, map_thms, fp_Rep_o_Abss, Rep_o_Abss]) THEN
   9.392            CONJ_WRAP (K (HEADGOAL (rtac refl))) bnfs;
   9.393  
   9.394          val fold_tac = mk_tac raw_un_fold_defs fp_fold_o_maps fp_xtor_un_folds fold_thms;
   9.395 @@ -360,20 +468,20 @@
   9.396         used by "primrec", "primcorecursive", and "datatype_compat". *)
   9.397      val fp_res =
   9.398        ({Ts = fpTs,
   9.399 -        bnfs = steal_fp_res #bnfs,
   9.400 +        bnfs = of_fp_res #bnfs,
   9.401          dtors = dtors,
   9.402          ctors = ctors,
   9.403          xtor_co_iterss = transpose [un_folds, co_recs],
   9.404          xtor_co_induct = xtor_co_induct_thm,
   9.405 -        dtor_ctors = steal_fp_res #dtor_ctors (*too general types*),
   9.406 -        ctor_dtors = steal_fp_res #ctor_dtors (*too general types*),
   9.407 -        ctor_injects = steal_fp_res #ctor_injects (*too general types*),
   9.408 -        dtor_injects = steal_fp_res #dtor_injects (*too general types*),
   9.409 -        xtor_map_thms = steal_fp_res #xtor_map_thms (*too general types and terms*),
   9.410 -        xtor_set_thmss = steal_fp_res #xtor_set_thmss (*too general types and terms*),
   9.411 -        xtor_rel_thms = steal_fp_res #xtor_rel_thms (*too general types and terms*),
   9.412 +        dtor_ctors = of_fp_res #dtor_ctors (*too general types*),
   9.413 +        ctor_dtors = of_fp_res #ctor_dtors (*too general types*),
   9.414 +        ctor_injects = of_fp_res #ctor_injects (*too general types*),
   9.415 +        dtor_injects = of_fp_res #dtor_injects (*too general types*),
   9.416 +        xtor_map_thms = of_fp_res #xtor_map_thms (*too general types and terms*),
   9.417 +        xtor_set_thmss = of_fp_res #xtor_set_thmss (*too general types and terms*),
   9.418 +        xtor_rel_thms = of_fp_res #xtor_rel_thms (*too general types and terms*),
   9.419          xtor_co_iter_thmss = transpose [xtor_un_fold_thms, xtor_co_rec_thms],
   9.420 -        xtor_co_iter_o_map_thmss = steal_fp_res #xtor_co_iter_o_map_thmss
   9.421 +        xtor_co_iter_o_map_thmss = of_fp_res #xtor_co_iter_o_map_thmss
   9.422            (*theorem about old constant*),
   9.423          rel_xtor_co_induct_thm = rel_xtor_co_induct_thm}
   9.424         |> morph_fp_result (Morphism.term_morphism "BNF" (singleton (Variable.polymorphic lthy))));
    10.1 --- a/src/HOL/Tools/BNF/bnf_fp_n2m_sugar.ML	Fri Feb 28 12:04:40 2014 +0100
    10.2 +++ b/src/HOL/Tools/BNF/bnf_fp_n2m_sugar.ML	Tue Feb 25 18:14:26 2014 +0100
    10.3 @@ -191,13 +191,13 @@
    10.4  
    10.5      val ctr_Tsss = map (map binder_types) ctr_Tss;
    10.6      val ctrXs_Tsss = map4 (map2 o map2 oo freeze_fpTs_call) kks fpTs callssss ctr_Tsss;
    10.7 -    val ctrXs_sum_prod_Ts = map (mk_sumTN_balanced o map HOLogic.mk_tupleT) ctrXs_Tsss;
    10.8 +    val ctrXs_repTs = map (mk_sumTN_balanced o map HOLogic.mk_tupleT) ctrXs_Tsss;
    10.9  
   10.10      val ns = map length ctr_Tsss;
   10.11      val kss = map (fn n => 1 upto n) ns;
   10.12      val mss = map (map length) ctr_Tsss;
   10.13  
   10.14 -    val fp_eqs = map dest_TFree Xs ~~ ctrXs_sum_prod_Ts;
   10.15 +    val fp_eqs = map dest_TFree Xs ~~ ctrXs_repTs;
   10.16      val key = key_of_fp_eqs fp fpTs fp_eqs;
   10.17    in
   10.18      (case n2m_sugar_of no_defs_lthy key of
   10.19 @@ -221,32 +221,45 @@
   10.20                  map_filter (fn (T, U) => if member (op =) deads T then SOME U else NONE) (Ts ~~ Us);
   10.21              in fold Term.add_tfreesT dead_Us [] end);
   10.22  
   10.23 -        val (pre_bnfs, (fp_res as {xtor_co_iterss = xtor_co_iterss0, xtor_co_induct, dtor_injects,
   10.24 -               dtor_ctors, xtor_co_iter_thmss, ...}, lthy)) =
   10.25 +        val fp_absT_infos = map #absT_info fp_sugars0;
   10.26 +
   10.27 +        val ((pre_bnfs, absT_infos), (fp_res as {xtor_co_iterss = xtor_co_iterss0, xtor_co_induct,
   10.28 +               dtor_injects, dtor_ctors, xtor_co_iter_thmss, ...}, lthy)) =
   10.29            fp_bnf (construct_mutualized_fp fp fpTs fp_sugars0) fp_bs As' killed_As' fp_eqs
   10.30 -            no_defs_lthy;
   10.31 +            no_defs_lthy0;
   10.32 +
   10.33 +        val fp_abs_inverses = map #abs_inverse fp_absT_infos;
   10.34 +        val fp_type_definitions = map #type_definition fp_absT_infos;
   10.35 +
   10.36 +        val abss = map #abs absT_infos;
   10.37 +        val reps = map #rep absT_infos;
   10.38 +        val absTs = map #absT absT_infos;
   10.39 +        val repTs = map #repT absT_infos;
   10.40 +        val abs_inverses = map #abs_inverse absT_infos;
   10.41  
   10.42          val nesting_bnfs = nesty_bnfs lthy ctrXs_Tsss As;
   10.43          val nested_bnfs = nesty_bnfs lthy ctrXs_Tsss Xs;
   10.44  
   10.45          val ((xtor_co_iterss, iters_args_types, coiters_args_types), _) =
   10.46 -          mk_co_iters_prelims fp ctr_Tsss fpTs Cs ns mss xtor_co_iterss0 lthy;
   10.47 +          mk_co_iters_prelims fp ctr_Tsss fpTs Cs absTs repTs ns mss xtor_co_iterss0 lthy;
   10.48  
   10.49          fun mk_binding b suf = Binding.suffix_name ("_" ^ suf) b;
   10.50  
   10.51          val ((co_iterss, co_iter_defss), lthy) =
   10.52            fold_map2 (fn b =>
   10.53 -            (if fp = Least_FP then define_iters [foldN, recN] (the iters_args_types)
   10.54 -             else define_coiters [unfoldN, corecN] (the coiters_args_types))
   10.55 -              (mk_binding b) fpTs Cs) fp_bs xtor_co_iterss lthy
   10.56 +            if fp = Least_FP then
   10.57 +              define_iters [foldN, recN] (the iters_args_types) (mk_binding b) fpTs Cs reps
   10.58 +            else
   10.59 +              define_coiters [unfoldN, corecN] (the coiters_args_types) (mk_binding b) fpTs Cs abss)
   10.60 +            fp_bs xtor_co_iterss lthy
   10.61            |>> split_list;
   10.62  
   10.63          val ((common_co_inducts, co_inductss, un_fold_thmss, co_rec_thmss, disc_unfold_thmss,
   10.64                disc_corec_thmss, sel_unfold_thmsss, sel_corec_thmsss), fp_sugar_thms) =
   10.65            if fp = Least_FP then
   10.66              derive_induct_iters_thms_for_types pre_bnfs (the iters_args_types) xtor_co_induct
   10.67 -              xtor_co_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss ctrss ctr_defss
   10.68 -              co_iterss co_iter_defss lthy
   10.69 +              xtor_co_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss fp_abs_inverses
   10.70 +              fp_type_definitions abs_inverses ctrss ctr_defss co_iterss co_iter_defss lthy
   10.71              |> `(fn ((inducts, induct, _), (fold_thmss, rec_thmss, _)) =>
   10.72                ([induct], [inducts], fold_thmss, rec_thmss, replicate nn [],
   10.73                 replicate nn [], replicate nn [], replicate nn []))
   10.74 @@ -254,8 +267,8 @@
   10.75            else
   10.76              derive_coinduct_coiters_thms_for_types pre_bnfs (the coiters_args_types) xtor_co_induct
   10.77                dtor_injects dtor_ctors xtor_co_iter_thmss nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss mss
   10.78 -              ns ctr_defss ctr_sugars co_iterss co_iter_defss
   10.79 -              (Proof_Context.export lthy no_defs_lthy) lthy
   10.80 +              ns fp_abs_inverses abs_inverses (fn thm => thm RS @{thm vimage2p_refl}) ctr_defss
   10.81 +              ctr_sugars co_iterss co_iter_defss (Proof_Context.export lthy no_defs_lthy) lthy
   10.82              |> `(fn ((coinduct_thms_pairs, _), (unfold_thmss, corec_thmss, _),
   10.83                      (disc_unfold_thmss, disc_corec_thmss, _), _,
   10.84                      (sel_unfold_thmsss, sel_corec_thmsss, _)) =>
   10.85 @@ -266,21 +279,21 @@
   10.86  
   10.87          val phi = Proof_Context.export_morphism no_defs_lthy no_defs_lthy0;
   10.88  
   10.89 -        fun mk_target_fp_sugar T X kk pre_bnf ctrXs_Tss ctr_defs ctr_sugar co_iters maps co_inducts
   10.90 -            un_fold_thms co_rec_thms disc_unfold_thms disc_corec_thms sel_unfold_thmss
   10.91 +        fun mk_target_fp_sugar T X kk pre_bnf absT_info ctrXs_Tss ctr_defs ctr_sugar co_iters maps
   10.92 +            co_inducts un_fold_thms co_rec_thms disc_unfold_thms disc_corec_thms sel_unfold_thmss
   10.93              sel_corec_thmss =
   10.94            {T = T, X = X, fp = fp, fp_res = fp_res, fp_res_index = kk, pre_bnf = pre_bnf,
   10.95 -           nested_bnfs = nested_bnfs, nesting_bnfs = nesting_bnfs, ctrXs_Tss = ctrXs_Tss,
   10.96 -           ctr_defs = ctr_defs, ctr_sugar = ctr_sugar, co_iters = co_iters, maps = maps,
   10.97 -           common_co_inducts = common_co_inducts, co_inducts = co_inducts,
   10.98 +           absT_info = absT_info, nested_bnfs = nested_bnfs, nesting_bnfs = nesting_bnfs,
   10.99 +           ctrXs_Tss = ctrXs_Tss, ctr_defs = ctr_defs, ctr_sugar = ctr_sugar, co_iters = co_iters,
  10.100 +           maps = maps, common_co_inducts = common_co_inducts, co_inducts = co_inducts,
  10.101             co_iter_thmss = [un_fold_thms, co_rec_thms],
  10.102             disc_co_iterss = [disc_unfold_thms, disc_corec_thms],
  10.103             sel_co_itersss = [sel_unfold_thmss, sel_corec_thmss]}
  10.104            |> morph_fp_sugar phi;
  10.105  
  10.106          val target_fp_sugars =
  10.107 -          map16 mk_target_fp_sugar fpTs Xs kks pre_bnfs ctrXs_Tsss ctr_defss ctr_sugars co_iterss
  10.108 -            mapss (transpose co_inductss) un_fold_thmss co_rec_thmss disc_unfold_thmss
  10.109 +          map17 mk_target_fp_sugar fpTs Xs kks pre_bnfs absT_infos ctrXs_Tsss ctr_defss ctr_sugars
  10.110 +            co_iterss mapss (transpose co_inductss) un_fold_thmss co_rec_thmss disc_unfold_thmss
  10.111              disc_corec_thmss sel_unfold_thmsss sel_corec_thmsss;
  10.112  
  10.113          val n2m_sugar = (target_fp_sugars, fp_sugar_thms);
    11.1 --- a/src/HOL/Tools/BNF/bnf_fp_n2m_tactics.ML	Fri Feb 28 12:04:40 2014 +0100
    11.2 +++ b/src/HOL/Tools/BNF/bnf_fp_n2m_tactics.ML	Tue Feb 25 18:14:26 2014 +0100
    11.3 @@ -8,29 +8,35 @@
    11.4  signature BNF_FP_N2M_TACTICS =
    11.5  sig
    11.6    val mk_rel_xtor_co_induct_tactic: BNF_Util.fp_kind -> thm list -> thm list -> thm list ->
    11.7 -    {prems: thm list, context: Proof.context} -> tactic
    11.8 +    thm list -> {prems: thm list, context: Proof.context} -> tactic
    11.9  end;
   11.10  
   11.11  structure BNF_FP_N2M_Tactics : BNF_FP_N2M_TACTICS =
   11.12  struct
   11.13  
   11.14  open BNF_Util
   11.15 +open BNF_Tactics
   11.16  open BNF_FP_Util
   11.17  
   11.18 -fun mk_rel_xtor_co_induct_tactic fp co_inducts rel_defs rel_monos
   11.19 +val vimage2p_unfolds = o_apply :: @{thms vimage2p_def};
   11.20 +
   11.21 +fun mk_rel_xtor_co_induct_tactic fp abs_inverses co_inducts0 rel_defs rel_monos
   11.22    {context = ctxt, prems = raw_C_IHs} =
   11.23    let
   11.24 -    val unfolds = map (fn def => unfold_thms ctxt (id_apply :: no_reflexive [def])) rel_defs;
   11.25 +    val co_inducts = map (unfold_thms ctxt vimage2p_unfolds) co_inducts0;
   11.26 +    val unfolds = map (fn def =>
   11.27 +      unfold_thms ctxt (id_apply :: vimage2p_unfolds @ abs_inverses @ no_reflexive [def])) rel_defs;
   11.28      val folded_C_IHs = map (fn thm => thm RS @{thm spec2} RS mp) raw_C_IHs;
   11.29      val C_IHs = map2 (curry op |>) folded_C_IHs unfolds;
   11.30      val C_IH_monos =
   11.31        map3 (fn C_IH => fn mono => fn unfold =>
   11.32 -        (mono RSN (2, @{thm rev_predicate2D}), C_IH)
   11.33 +        (mono RSN (2, @{thm vimage2p_mono}), C_IH)
   11.34          |> fp = Greatest_FP ? swap
   11.35          |> op RS
   11.36          |> unfold)
   11.37        folded_C_IHs rel_monos unfolds;
   11.38    in
   11.39 +    unfold_thms_tac ctxt vimage2p_unfolds THEN
   11.40      HEADGOAL (CONJ_WRAP_GEN' (rtac @{thm context_conjI})
   11.41        (fn thm => rtac thm THEN_ALL_NEW (rotate_tac ~1 THEN'
   11.42           REPEAT_ALL_NEW (FIRST' [eresolve_tac C_IHs, eresolve_tac C_IH_monos,
    12.1 --- a/src/HOL/Tools/BNF/bnf_fp_util.ML	Fri Feb 28 12:04:40 2014 +0100
    12.2 +++ b/src/HOL/Tools/BNF/bnf_fp_util.ML	Tue Feb 25 18:14:26 2014 +0100
    12.3 @@ -142,16 +142,15 @@
    12.4  
    12.5    val mk_case_sum: term * term -> term
    12.6    val mk_case_sumN: term list -> term
    12.7 -  val mk_case_sumN_balanced: term list -> term
    12.8 +  val mk_case_absumprod: typ -> term -> term list -> term list -> term list list -> term
    12.9 +
   12.10    val mk_Inl: typ -> term -> term
   12.11    val mk_Inr: typ -> term -> term
   12.12    val mk_InN: typ list -> term -> int -> term
   12.13 -  val mk_InN_balanced: typ -> int -> term -> int -> term
   12.14 +  val mk_absumprod: typ -> term -> int -> int -> term list -> term
   12.15  
   12.16    val dest_sumT: typ -> typ * typ
   12.17 -  val dest_sumTN: int -> typ -> typ list
   12.18 -  val dest_sumTN_balanced: int -> typ -> typ list
   12.19 -  val dest_tupleT: int -> typ -> typ list
   12.20 +  val dest_absumprodT: typ -> typ -> int -> int list -> typ -> typ list list
   12.21  
   12.22    val If_const: typ -> term
   12.23  
   12.24 @@ -160,8 +159,8 @@
   12.25    val mk_union: term * term -> term
   12.26  
   12.27    val mk_sumEN: int -> thm
   12.28 -  val mk_sumEN_balanced: int -> thm
   12.29 -  val mk_sumEN_tupled_balanced: int list -> thm
   12.30 +  val mk_absumprodE: thm -> int list -> thm
   12.31 +
   12.32    val mk_sum_caseN: int -> int -> thm
   12.33    val mk_sum_caseN_balanced: int -> int -> thm
   12.34  
   12.35 @@ -176,12 +175,12 @@
   12.36    val mk_xtor_un_fold_o_map_thms: BNF_Util.fp_kind -> bool -> int -> thm -> thm list -> thm list ->
   12.37      thm list -> thm list -> thm list
   12.38  
   12.39 -  val mk_strong_coinduct_thm: thm -> thm list -> thm list -> Proof.context -> thm
   12.40 +  val mk_strong_coinduct_thm: thm -> thm list -> thm list -> (thm -> thm) -> Proof.context -> thm
   12.41  
   12.42    val fp_bnf: (binding list -> (string * sort) list -> typ list * typ list list ->
   12.43 -      BNF_Def.bnf list -> local_theory -> 'a) ->
   12.44 +      BNF_Def.bnf list -> BNF_Comp.absT_info list -> local_theory -> 'a) ->
   12.45      binding list -> (string * sort) list -> (string * sort) list -> ((string * sort) * typ) list ->
   12.46 -    local_theory -> BNF_Def.bnf list * 'a
   12.47 +    local_theory -> (BNF_Def.bnf list * BNF_Comp.absT_info list) * 'a
   12.48  end;
   12.49  
   12.50  structure BNF_FP_Util : BNF_FP_UTIL =
   12.51 @@ -347,9 +346,6 @@
   12.52  
   12.53  fun dest_sumT (Type (@{type_name sum}, [T, T'])) = (T, T');
   12.54  
   12.55 -fun dest_sumTN 1 T = [T]
   12.56 -  | dest_sumTN n (Type (@{type_name sum}, [T, T'])) = T :: dest_sumTN (n - 1) T';
   12.57 -
   12.58  val dest_sumTN_balanced = Balanced_Tree.dest dest_sumT;
   12.59  
   12.60  (* TODO: move something like this to "HOLogic"? *)
   12.61 @@ -357,6 +353,8 @@
   12.62    | dest_tupleT 1 T = [T]
   12.63    | dest_tupleT n (Type (@{type_name prod}, [T, T'])) = T :: dest_tupleT (n - 1) T';
   12.64  
   12.65 +fun dest_absumprodT absT repT n ms = map2 dest_tupleT ms o dest_sumTN_balanced n o mk_repT absT repT;
   12.66 +
   12.67  val mk_sumTN = Library.foldr1 mk_sumT;
   12.68  val mk_sumTN_balanced = Balanced_Tree.make mk_sumT;
   12.69  
   12.70 @@ -397,6 +395,10 @@
   12.71      |> repair_types sum_T
   12.72    end;
   12.73  
   12.74 +fun mk_absumprod absT abs0 n k ts =
   12.75 +  let val abs = mk_abs absT abs0;
   12.76 +  in abs $ mk_InN_balanced (domain_type (fastype_of abs)) n (HOLogic.mk_tuple ts) k end;
   12.77 +
   12.78  fun mk_case_sum (f, g) =
   12.79    let
   12.80      val fT = fastype_of f;
   12.81 @@ -409,6 +411,12 @@
   12.82  val mk_case_sumN = Library.foldr1 mk_case_sum;
   12.83  val mk_case_sumN_balanced = Balanced_Tree.make mk_case_sum;
   12.84  
   12.85 +fun mk_tupled_fun f x xs =
   12.86 +  if xs = [x] then f else HOLogic.tupled_lambda x (Term.list_comb (f, xs));
   12.87 +
   12.88 +fun mk_case_absumprod absT rep fs xs xss =
   12.89 +  HOLogic.mk_comp (mk_case_sumN_balanced (map3 mk_tupled_fun fs xs xss), mk_rep absT rep);
   12.90 +
   12.91  fun If_const T = Const (@{const_name If}, HOLogic.boolT --> T --> T --> T);
   12.92  fun mk_If p t f = let val T = fastype_of t in If_const T $ p $ t $ f end;
   12.93  
   12.94 @@ -441,21 +449,15 @@
   12.95    Balanced_Tree.make (fn (thm1, thm2) => thm1 RSN (1, thm2 RSN (2, @{thm obj_sumE_f})))
   12.96      (replicate n asm_rl);
   12.97  
   12.98 -fun mk_sumEN_balanced' n all_impIs = mk_obj_sumEN_balanced n OF all_impIs RS @{thm obj_one_pointE};
   12.99 -
  12.100 -fun mk_sumEN_balanced 1 = @{thm one_pointE} (*optimization*)
  12.101 -  | mk_sumEN_balanced 2 = @{thm sumE} (*optimization*)
  12.102 -  | mk_sumEN_balanced n = mk_sumEN_balanced' n (replicate n (impI RS allI));
  12.103 -
  12.104  fun mk_tupled_allIN 0 = @{thm unit_all_impI}
  12.105    | mk_tupled_allIN 1 = @{thm impI[THEN allI]}
  12.106    | mk_tupled_allIN 2 = @{thm prod_all_impI} (*optimization*)
  12.107    | mk_tupled_allIN n = mk_tupled_allIN (n - 1) RS @{thm prod_all_impI_step};
  12.108  
  12.109 -fun mk_sumEN_tupled_balanced ms =
  12.110 +fun mk_absumprodE type_definition ms =
  12.111    let val n = length ms in
  12.112 -    if forall (curry op = 1) ms then mk_sumEN_balanced n
  12.113 -    else mk_sumEN_balanced' n (map mk_tupled_allIN ms)
  12.114 +    mk_obj_sumEN_balanced n OF map mk_tupled_allIN ms RS
  12.115 +      (type_definition RS @{thm type_copy_obj_one_point_absE})
  12.116    end;
  12.117  
  12.118  fun mk_sum_caseN 1 1 = refl
  12.119 @@ -543,7 +545,7 @@
  12.120      split_conj_thm (un_fold_unique OF map (fp_case fp I mk_sym) unique_prems)
  12.121    end;
  12.122  
  12.123 -fun mk_strong_coinduct_thm coind rel_eqs rel_monos ctxt =
  12.124 +fun mk_strong_coinduct_thm coind rel_eqs rel_monos mk_vimage2p ctxt =
  12.125    let
  12.126      val n = Thm.nprems_of coind;
  12.127      val m = Thm.nprems_of (hd rel_monos) - n;
  12.128 @@ -554,7 +556,7 @@
  12.129        let
  12.130          val eq = iffD2 OF [rel_eq RS @{thm predicate2_eqD}, refl];
  12.131          val mono = rel_mono OF (replicate m @{thm order_refl} @ replicate n @{thm eq_subset});
  12.132 -      in eq RS (mono RS @{thm predicate2D}) RS @{thm eqTrueI} end;
  12.133 +      in mk_vimage2p (eq RS (mono RS @{thm predicate2D})) RS @{thm eqTrueI} end;
  12.134      val unfolds = map2 mk_unfold rel_eqs rel_monos @ @{thms sup_fun_def sup_bool_def
  12.135        imp_disjL all_conj_distrib subst_eq_imp simp_thms(18,21,35)};
  12.136    in
  12.137 @@ -603,18 +605,19 @@
  12.138      fun pre_qualify b = Binding.qualify false (Binding.name_of b)
  12.139        #> Config.get lthy' bnf_note_all = false ? Binding.conceal;
  12.140  
  12.141 -    val ((pre_bnfs, deadss), lthy'') =
  12.142 +    val ((pre_bnfs, (deadss, absT_infos)), lthy'') =
  12.143        fold_map3 (fn b => seal_bnf (pre_qualify b) unfold_set' (Binding.prefix_name preN b))
  12.144          bs Dss bnfs' lthy'
  12.145 -      |>> split_list;
  12.146 +      |>> split_list
  12.147 +      |>> apsnd split_list;
  12.148  
  12.149      val timer = time (timer "Normalization & sealing of BNFs");
  12.150  
  12.151 -    val res = construct_fp bs resBs (map TFree resDs, deadss) pre_bnfs lthy'';
  12.152 +    val res = construct_fp bs resBs (map TFree resDs, deadss) pre_bnfs absT_infos lthy'';
  12.153  
  12.154      val timer = time (timer "FP construction in total");
  12.155    in
  12.156 -    timer; (pre_bnfs, res)
  12.157 +    timer; ((pre_bnfs, absT_infos), res)
  12.158    end;
  12.159  
  12.160  end;
    13.1 --- a/src/HOL/Tools/BNF/bnf_gfp.ML	Fri Feb 28 12:04:40 2014 +0100
    13.2 +++ b/src/HOL/Tools/BNF/bnf_gfp.ML	Tue Feb 25 18:14:26 2014 +0100
    13.3 @@ -11,7 +11,7 @@
    13.4  sig
    13.5    val construct_gfp: mixfix list -> binding list -> binding list -> binding list list ->
    13.6      binding list -> (string * sort) list -> typ list * typ list list -> BNF_Def.bnf list ->
    13.7 -    local_theory -> BNF_FP_Util.fp_result * local_theory
    13.8 +    BNF_Comp.absT_info list -> local_theory -> BNF_FP_Util.fp_result * local_theory
    13.9  end;
   13.10  
   13.11  structure BNF_GFP : BNF_GFP =
   13.12 @@ -55,7 +55,7 @@
   13.13       ((i, I), nth (nth lwitss i) nwit) :: maps (tree_to_coind_wits lwitss) subtrees;
   13.14  
   13.15  (*all BNFs have the same lives*)
   13.16 -fun construct_gfp mixfixes map_bs rel_bs set_bss0 bs resBs (resDs, Dss) bnfs lthy =
   13.17 +fun construct_gfp mixfixes map_bs rel_bs set_bss0 bs resBs (resDs, Dss) bnfs _ lthy =
   13.18    let
   13.19      val time = time lthy;
   13.20      val timer = time (Timer.startRealTimer ());
    14.1 --- a/src/HOL/Tools/BNF/bnf_lfp.ML	Fri Feb 28 12:04:40 2014 +0100
    14.2 +++ b/src/HOL/Tools/BNF/bnf_lfp.ML	Tue Feb 25 18:14:26 2014 +0100
    14.3 @@ -10,7 +10,7 @@
    14.4  sig
    14.5    val construct_lfp: mixfix list -> binding list -> binding list -> binding list list ->
    14.6      binding list -> (string * sort) list -> typ list * typ list list -> BNF_Def.bnf list ->
    14.7 -    local_theory -> BNF_FP_Util.fp_result * local_theory
    14.8 +    BNF_Comp.absT_info list -> local_theory -> BNF_FP_Util.fp_result * local_theory
    14.9  end;
   14.10  
   14.11  structure BNF_LFP : BNF_LFP =
   14.12 @@ -26,7 +26,7 @@
   14.13  open BNF_LFP_Tactics
   14.14  
   14.15  (*all BNFs have the same lives*)
   14.16 -fun construct_lfp mixfixes map_bs rel_bs set_bss0 bs resBs (resDs, Dss) bnfs lthy =
   14.17 +fun construct_lfp mixfixes map_bs rel_bs set_bss0 bs resBs (resDs, Dss) bnfs _ lthy =
   14.18    let
   14.19      val time = time lthy;
   14.20      val timer = time (Timer.startRealTimer ());
   14.21 @@ -701,6 +701,7 @@
   14.22        let
   14.23          val i_field = HOLogic.mk_mem (idx, field_suc_bd);
   14.24          val min_algs = mk_min_algs ss;
   14.25 +
   14.26          val min_algss = map (fn k => mk_nthN n (min_algs $ idx) k) ks;
   14.27  
   14.28          val concl = HOLogic.mk_Trueprop
    15.1 --- a/src/HOL/Tools/BNF/bnf_util.ML	Fri Feb 28 12:04:40 2014 +0100
    15.2 +++ b/src/HOL/Tools/BNF/bnf_util.ML	Tue Feb 25 18:14:26 2014 +0100
    15.3 @@ -43,6 +43,11 @@
    15.4        'o -> 'p -> 'q) ->
    15.5      'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
    15.6      'i list -> 'j list -> 'k list -> 'l list -> 'm list -> 'n list -> 'o list -> 'p list -> 'q list
    15.7 +  val map17: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l -> 'm -> 'n ->
    15.8 +      'o -> 'p -> 'q -> 'r) ->
    15.9 +    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
   15.10 +    'i list -> 'j list -> 'k list -> 'l list -> 'm list -> 'n list -> 'o list -> 'p list ->
   15.11 +    'q list -> 'r list
   15.12    val fold_map4: ('a -> 'b -> 'c -> 'd -> 'e -> 'f * 'e) ->
   15.13      'a list -> 'b list -> 'c list -> 'd list -> 'e -> 'f list * 'e
   15.14    val fold_map5: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g * 'f) ->
   15.15 @@ -109,6 +114,7 @@
   15.16    val mk_ordLeq: term -> term -> term
   15.17    val mk_rel_comp: term * term -> term
   15.18    val mk_rel_compp: term * term -> term
   15.19 +  val mk_vimage2p: term -> term -> term
   15.20  
   15.21    (*parameterized terms*)
   15.22    val mk_nthN: int -> term -> int -> term
   15.23 @@ -225,6 +231,14 @@
   15.24        map16 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s x12s x13s x14s x15s x16s
   15.25    | map16 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   15.26  
   15.27 +fun map17 _ [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] = []
   15.28 +  | map17 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s)
   15.29 +      (x9::x9s) (x10::x10s) (x11::x11s) (x12::x12s) (x13::x13s) (x14::x14s) (x15::x15s)
   15.30 +      (x16::x16s) (x17::x17s) =
   15.31 +    f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 ::
   15.32 +      map17 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s x12s x13s x14s x15s x16s x17s
   15.33 +  | map17 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   15.34 +
   15.35  fun fold_map4 _ [] [] [] [] acc = ([], acc)
   15.36    | fold_map4 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) acc =
   15.37      let
   15.38 @@ -498,6 +512,15 @@
   15.39    let val T = (case xs of [] => defT | (x::_) => fastype_of x);
   15.40    in Const (@{const_name collect}, HOLogic.mk_setT T --> T) $ (HOLogic.mk_set T xs) end;
   15.41  
   15.42 +fun mk_vimage2p f g =
   15.43 +  let
   15.44 +    val (T1, T2) = dest_funT (fastype_of f);
   15.45 +    val (U1, U2) = dest_funT (fastype_of g);
   15.46 +  in
   15.47 +    Const (@{const_name vimage2p},
   15.48 +      (T1 --> T2) --> (U1 --> U2) --> mk_pred2T T2 U2 --> mk_pred2T T1 U1) $ f $ g
   15.49 +  end;
   15.50 +
   15.51  fun mk_trans thm1 thm2 = trans OF [thm1, thm2];
   15.52  fun mk_sym thm = thm RS sym;
   15.53