src/HOL/Product_Type.thy
author Andreas Lochbihler
Tue Jun 10 18:24:53 2014 +0200 (2014-06-10)
changeset 57201 697e0fad9337
parent 57091 1fa9c19ba2c9
child 57233 8fcbfce2a2a9
permissions -rw-r--r--
add type class instances for unit
     1 (*  Title:      HOL/Product_Type.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1992  University of Cambridge
     4 *)
     5 
     6 header {* Cartesian products *}
     7 
     8 theory Product_Type
     9 imports Typedef Inductive Fun
    10 keywords "inductive_set" "coinductive_set" :: thy_decl
    11 begin
    12 
    13 subsection {* @{typ bool} is a datatype *}
    14 
    15 free_constructors case_bool for True | False
    16 by auto
    17 
    18 text {* Avoid name clashes by prefixing the output of @{text rep_datatype} with @{text old}. *}
    19 
    20 setup {* Sign.mandatory_path "old" *}
    21 
    22 rep_datatype True False by (auto intro: bool_induct)
    23 
    24 setup {* Sign.parent_path *}
    25 
    26 text {* But erase the prefix for properties that are not generated by @{text free_constructors}. *}
    27 
    28 setup {* Sign.mandatory_path "bool" *}
    29 
    30 lemmas induct = old.bool.induct
    31 lemmas inducts = old.bool.inducts
    32 lemmas rec = old.bool.rec
    33 lemmas simps = bool.distinct bool.case bool.rec
    34 
    35 setup {* Sign.parent_path *}
    36 
    37 declare case_split [cases type: bool]
    38   -- "prefer plain propositional version"
    39 
    40 lemma
    41   shows [code]: "HOL.equal False P \<longleftrightarrow> \<not> P"
    42     and [code]: "HOL.equal True P \<longleftrightarrow> P" 
    43     and [code]: "HOL.equal P False \<longleftrightarrow> \<not> P"
    44     and [code]: "HOL.equal P True \<longleftrightarrow> P"
    45     and [code nbe]: "HOL.equal P P \<longleftrightarrow> True"
    46   by (simp_all add: equal)
    47 
    48 lemma If_case_cert:
    49   assumes "CASE \<equiv> (\<lambda>b. If b f g)"
    50   shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
    51   using assms by simp_all
    52 
    53 setup {*
    54   Code.add_case @{thm If_case_cert}
    55 *}
    56 
    57 code_printing
    58   constant "HOL.equal :: bool \<Rightarrow> bool \<Rightarrow> bool" \<rightharpoonup> (Haskell) infix 4 "=="
    59 | class_instance "bool" :: "equal" \<rightharpoonup> (Haskell) -
    60 
    61 
    62 subsection {* The @{text unit} type *}
    63 
    64 typedef unit = "{True}"
    65   by auto
    66 
    67 definition Unity :: unit  ("'(')")
    68   where "() = Abs_unit True"
    69 
    70 lemma unit_eq [no_atp]: "u = ()"
    71   by (induct u) (simp add: Unity_def)
    72 
    73 text {*
    74   Simplification procedure for @{thm [source] unit_eq}.  Cannot use
    75   this rule directly --- it loops!
    76 *}
    77 
    78 simproc_setup unit_eq ("x::unit") = {*
    79   fn _ => fn _ => fn ct =>
    80     if HOLogic.is_unit (term_of ct) then NONE
    81     else SOME (mk_meta_eq @{thm unit_eq})
    82 *}
    83 
    84 free_constructors case_unit for "()"
    85 by auto
    86 
    87 text {* Avoid name clashes by prefixing the output of @{text rep_datatype} with @{text old}. *}
    88 
    89 setup {* Sign.mandatory_path "old" *}
    90 
    91 rep_datatype "()" by simp
    92 
    93 setup {* Sign.parent_path *}
    94 
    95 text {* But erase the prefix for properties that are not generated by @{text free_constructors}. *}
    96 
    97 setup {* Sign.mandatory_path "unit" *}
    98 
    99 lemmas induct = old.unit.induct
   100 lemmas inducts = old.unit.inducts
   101 lemmas rec = old.unit.rec
   102 lemmas simps = unit.case unit.rec
   103 
   104 setup {* Sign.parent_path *}
   105 
   106 lemma unit_all_eq1: "(!!x::unit. PROP P x) == PROP P ()"
   107   by simp
   108 
   109 lemma unit_all_eq2: "(!!x::unit. PROP P) == PROP P"
   110   by (rule triv_forall_equality)
   111 
   112 text {*
   113   This rewrite counters the effect of simproc @{text unit_eq} on @{term
   114   [source] "%u::unit. f u"}, replacing it by @{term [source]
   115   f} rather than by @{term [source] "%u. f ()"}.
   116 *}
   117 
   118 lemma unit_abs_eta_conv [simp]: "(%u::unit. f ()) = f"
   119   by (rule ext) simp
   120 
   121 lemma UNIV_unit:
   122   "UNIV = {()}" by auto
   123 
   124 instantiation unit :: default
   125 begin
   126 
   127 definition "default = ()"
   128 
   129 instance ..
   130 
   131 end
   132 
   133 instantiation unit :: linorder
   134 begin
   135 
   136 definition less_eq_unit :: "unit \<Rightarrow> unit \<Rightarrow> bool" where
   137 "less_eq_unit _ _ = True"
   138 
   139 definition less_unit :: "unit \<Rightarrow> unit \<Rightarrow> bool" where
   140 "less_unit _ _ = False"
   141 
   142 declare less_eq_unit_def [simp, abs_def, code_unfold] less_unit_def [simp, abs_def, code_unfold]
   143 
   144 instance
   145 proof qed auto
   146 
   147 end
   148 
   149 instantiation unit :: complete_boolean_algebra begin
   150 
   151 definition "top = ()"
   152 definition "bot = ()"
   153 definition [code_unfold]: "sup _ _ = ()"
   154 definition [code_unfold]: "inf _ _ = ()"
   155 definition "Sup _ = ()"
   156 definition "Inf _ = ()"
   157 definition [simp, code_unfold]: "uminus = (\<lambda>_ :: unit. ())"
   158 
   159 instance by intro_classes auto
   160 
   161 end
   162 
   163 instance unit :: "{complete_linorder, wellorder}"
   164   by intro_classes auto
   165 
   166 lemma [code]:
   167   "HOL.equal (u\<Colon>unit) v \<longleftrightarrow> True" unfolding equal unit_eq [of u] unit_eq [of v] by rule+
   168 
   169 code_printing
   170   type_constructor unit \<rightharpoonup>
   171     (SML) "unit"
   172     and (OCaml) "unit"
   173     and (Haskell) "()"
   174     and (Scala) "Unit"
   175 | constant Unity \<rightharpoonup>
   176     (SML) "()"
   177     and (OCaml) "()"
   178     and (Haskell) "()"
   179     and (Scala) "()"
   180 | class_instance unit :: equal \<rightharpoonup>
   181     (Haskell) -
   182 | constant "HOL.equal :: unit \<Rightarrow> unit \<Rightarrow> bool" \<rightharpoonup>
   183     (Haskell) infix 4 "=="
   184 
   185 code_reserved SML
   186   unit
   187 
   188 code_reserved OCaml
   189   unit
   190 
   191 code_reserved Scala
   192   Unit
   193 
   194 
   195 subsection {* The product type *}
   196 
   197 subsubsection {* Type definition *}
   198 
   199 definition Pair_Rep :: "'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool" where
   200   "Pair_Rep a b = (\<lambda>x y. x = a \<and> y = b)"
   201 
   202 definition "prod = {f. \<exists>a b. f = Pair_Rep (a\<Colon>'a) (b\<Colon>'b)}"
   203 
   204 typedef ('a, 'b) prod (infixr "*" 20) = "prod :: ('a \<Rightarrow> 'b \<Rightarrow> bool) set"
   205   unfolding prod_def by auto
   206 
   207 type_notation (xsymbols)
   208   "prod"  ("(_ \<times>/ _)" [21, 20] 20)
   209 type_notation (HTML output)
   210   "prod"  ("(_ \<times>/ _)" [21, 20] 20)
   211 
   212 definition Pair :: "'a \<Rightarrow> 'b \<Rightarrow> 'a \<times> 'b" where
   213   "Pair a b = Abs_prod (Pair_Rep a b)"
   214 
   215 lemma prod_cases: "(\<And>a b. P (Pair a b)) \<Longrightarrow> P p"
   216   by (cases p) (auto simp add: prod_def Pair_def Pair_Rep_def)
   217 
   218 free_constructors case_prod for Pair fst snd
   219 proof -
   220   fix P :: bool and p :: "'a \<times> 'b"
   221   show "(\<And>x1 x2. p = Pair x1 x2 \<Longrightarrow> P) \<Longrightarrow> P"
   222     by (cases p) (auto simp add: prod_def Pair_def Pair_Rep_def)
   223 next
   224   fix a c :: 'a and b d :: 'b
   225   have "Pair_Rep a b = Pair_Rep c d \<longleftrightarrow> a = c \<and> b = d"
   226     by (auto simp add: Pair_Rep_def fun_eq_iff)
   227   moreover have "Pair_Rep a b \<in> prod" and "Pair_Rep c d \<in> prod"
   228     by (auto simp add: prod_def)
   229   ultimately show "Pair a b = Pair c d \<longleftrightarrow> a = c \<and> b = d"
   230     by (simp add: Pair_def Abs_prod_inject)
   231 qed
   232 
   233 text {* Avoid name clashes by prefixing the output of @{text rep_datatype} with @{text old}. *}
   234 
   235 setup {* Sign.mandatory_path "old" *}
   236 
   237 rep_datatype Pair
   238 by (erule prod_cases) (rule prod.inject)
   239 
   240 setup {* Sign.parent_path *}
   241 
   242 text {* But erase the prefix for properties that are not generated by @{text free_constructors}. *}
   243 
   244 setup {* Sign.mandatory_path "prod" *}
   245 
   246 declare
   247   old.prod.inject[iff del]
   248 
   249 lemmas induct = old.prod.induct
   250 lemmas inducts = old.prod.inducts
   251 lemmas rec = old.prod.rec
   252 lemmas simps = prod.inject prod.case prod.rec
   253 
   254 setup {* Sign.parent_path *}
   255 
   256 declare prod.case [nitpick_simp del]
   257 declare prod.weak_case_cong [cong del]
   258 
   259 
   260 subsubsection {* Tuple syntax *}
   261 
   262 abbreviation (input) split :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c" where
   263   "split \<equiv> case_prod"
   264 
   265 text {*
   266   Patterns -- extends pre-defined type @{typ pttrn} used in
   267   abstractions.
   268 *}
   269 
   270 nonterminal tuple_args and patterns
   271 
   272 syntax
   273   "_tuple"      :: "'a => tuple_args => 'a * 'b"        ("(1'(_,/ _'))")
   274   "_tuple_arg"  :: "'a => tuple_args"                   ("_")
   275   "_tuple_args" :: "'a => tuple_args => tuple_args"     ("_,/ _")
   276   "_pattern"    :: "[pttrn, patterns] => pttrn"         ("'(_,/ _')")
   277   ""            :: "pttrn => patterns"                  ("_")
   278   "_patterns"   :: "[pttrn, patterns] => patterns"      ("_,/ _")
   279 
   280 translations
   281   "(x, y)" == "CONST Pair x y"
   282   "_pattern x y" => "CONST Pair x y"
   283   "_patterns x y" => "CONST Pair x y"
   284   "_tuple x (_tuple_args y z)" == "_tuple x (_tuple_arg (_tuple y z))"
   285   "%(x, y, zs). b" == "CONST case_prod (%x (y, zs). b)"
   286   "%(x, y). b" == "CONST case_prod (%x y. b)"
   287   "_abs (CONST Pair x y) t" => "%(x, y). t"
   288   -- {* The last rule accommodates tuples in `case C ... (x,y) ... => ...'
   289      The (x,y) is parsed as `Pair x y' because it is logic, not pttrn *}
   290 
   291 (*reconstruct pattern from (nested) splits, avoiding eta-contraction of body;
   292   works best with enclosing "let", if "let" does not avoid eta-contraction*)
   293 print_translation {*
   294   let
   295     fun split_tr' [Abs (x, T, t as (Abs abs))] =
   296           (* split (%x y. t) => %(x,y) t *)
   297           let
   298             val (y, t') = Syntax_Trans.atomic_abs_tr' abs;
   299             val (x', t'') = Syntax_Trans.atomic_abs_tr' (x, T, t');
   300           in
   301             Syntax.const @{syntax_const "_abs"} $
   302               (Syntax.const @{syntax_const "_pattern"} $ x' $ y) $ t''
   303           end
   304       | split_tr' [Abs (x, T, (s as Const (@{const_syntax case_prod}, _) $ t))] =
   305           (* split (%x. (split (%y z. t))) => %(x,y,z). t *)
   306           let
   307             val Const (@{syntax_const "_abs"}, _) $
   308               (Const (@{syntax_const "_pattern"}, _) $ y $ z) $ t' = split_tr' [t];
   309             val (x', t'') = Syntax_Trans.atomic_abs_tr' (x, T, t');
   310           in
   311             Syntax.const @{syntax_const "_abs"} $
   312               (Syntax.const @{syntax_const "_pattern"} $ x' $
   313                 (Syntax.const @{syntax_const "_patterns"} $ y $ z)) $ t''
   314           end
   315       | split_tr' [Const (@{const_syntax case_prod}, _) $ t] =
   316           (* split (split (%x y z. t)) => %((x, y), z). t *)
   317           split_tr' [(split_tr' [t])] (* inner split_tr' creates next pattern *)
   318       | split_tr' [Const (@{syntax_const "_abs"}, _) $ x_y $ Abs abs] =
   319           (* split (%pttrn z. t) => %(pttrn,z). t *)
   320           let val (z, t) = Syntax_Trans.atomic_abs_tr' abs in
   321             Syntax.const @{syntax_const "_abs"} $
   322               (Syntax.const @{syntax_const "_pattern"} $ x_y $ z) $ t
   323           end
   324       | split_tr' _ = raise Match;
   325   in [(@{const_syntax case_prod}, K split_tr')] end
   326 *}
   327 
   328 (* print "split f" as "\<lambda>(x,y). f x y" and "split (\<lambda>x. f x)" as "\<lambda>(x,y). f x y" *) 
   329 typed_print_translation {*
   330   let
   331     fun split_guess_names_tr' T [Abs (x, _, Abs _)] = raise Match
   332       | split_guess_names_tr' T [Abs (x, xT, t)] =
   333           (case (head_of t) of
   334             Const (@{const_syntax case_prod}, _) => raise Match
   335           | _ =>
   336             let 
   337               val (_ :: yT :: _) = binder_types (domain_type T) handle Bind => raise Match;
   338               val (y, t') = Syntax_Trans.atomic_abs_tr' ("y", yT, incr_boundvars 1 t $ Bound 0);
   339               val (x', t'') = Syntax_Trans.atomic_abs_tr' (x, xT, t');
   340             in
   341               Syntax.const @{syntax_const "_abs"} $
   342                 (Syntax.const @{syntax_const "_pattern"} $ x' $ y) $ t''
   343             end)
   344       | split_guess_names_tr' T [t] =
   345           (case head_of t of
   346             Const (@{const_syntax case_prod}, _) => raise Match
   347           | _ =>
   348             let
   349               val (xT :: yT :: _) = binder_types (domain_type T) handle Bind => raise Match;
   350               val (y, t') =
   351                 Syntax_Trans.atomic_abs_tr' ("y", yT, incr_boundvars 2 t $ Bound 1 $ Bound 0);
   352               val (x', t'') = Syntax_Trans.atomic_abs_tr' ("x", xT, t');
   353             in
   354               Syntax.const @{syntax_const "_abs"} $
   355                 (Syntax.const @{syntax_const "_pattern"} $ x' $ y) $ t''
   356             end)
   357       | split_guess_names_tr' _ _ = raise Match;
   358   in [(@{const_syntax case_prod}, K split_guess_names_tr')] end
   359 *}
   360 
   361 (* Force eta-contraction for terms of the form "Q A (%p. case_prod P p)"
   362    where Q is some bounded quantifier or set operator.
   363    Reason: the above prints as "Q p : A. case p of (x,y) \<Rightarrow> P x y"
   364    whereas we want "Q (x,y):A. P x y".
   365    Otherwise prevent eta-contraction.
   366 *)
   367 print_translation {*
   368   let
   369     fun contract Q tr ctxt ts =
   370       (case ts of
   371         [A, Abs (_, _, (s as Const (@{const_syntax case_prod},_) $ t) $ Bound 0)] =>
   372           if Term.is_dependent t then tr ctxt ts
   373           else Syntax.const Q $ A $ s
   374       | _ => tr ctxt ts);
   375   in
   376     [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},
   377      Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"},
   378      Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax INFIMUM} @{syntax_const "_INF"},
   379      Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax SUPREMUM} @{syntax_const "_SUP"}]
   380     |> map (fn (Q, tr) => (Q, contract Q tr))
   381   end
   382 *}
   383 
   384 subsubsection {* Code generator setup *}
   385 
   386 code_printing
   387   type_constructor prod \<rightharpoonup>
   388     (SML) infix 2 "*"
   389     and (OCaml) infix 2 "*"
   390     and (Haskell) "!((_),/ (_))"
   391     and (Scala) "((_),/ (_))"
   392 | constant Pair \<rightharpoonup>
   393     (SML) "!((_),/ (_))"
   394     and (OCaml) "!((_),/ (_))"
   395     and (Haskell) "!((_),/ (_))"
   396     and (Scala) "!((_),/ (_))"
   397 | class_instance  prod :: equal \<rightharpoonup>
   398     (Haskell) -
   399 | constant "HOL.equal :: 'a \<times> 'b \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool" \<rightharpoonup>
   400     (Haskell) infix 4 "=="
   401 
   402 
   403 subsubsection {* Fundamental operations and properties *}
   404 
   405 lemma Pair_inject:
   406   assumes "(a, b) = (a', b')"
   407     and "a = a' ==> b = b' ==> R"
   408   shows R
   409   using assms by simp
   410 
   411 lemma surj_pair [simp]: "EX x y. p = (x, y)"
   412   by (cases p) simp
   413 
   414 code_printing
   415   constant fst \<rightharpoonup> (Haskell) "fst"
   416 | constant snd \<rightharpoonup> (Haskell) "snd"
   417 
   418 lemma case_prod_unfold [nitpick_unfold]: "case_prod = (%c p. c (fst p) (snd p))"
   419   by (simp add: fun_eq_iff split: prod.split)
   420 
   421 lemma fst_eqD: "fst (x, y) = a ==> x = a"
   422   by simp
   423 
   424 lemma snd_eqD: "snd (x, y) = a ==> y = a"
   425   by simp
   426 
   427 lemmas surjective_pairing = prod.collapse [symmetric]
   428 
   429 lemma prod_eq_iff: "s = t \<longleftrightarrow> fst s = fst t \<and> snd s = snd t"
   430   by (cases s, cases t) simp
   431 
   432 lemma prod_eqI [intro?]: "fst p = fst q \<Longrightarrow> snd p = snd q \<Longrightarrow> p = q"
   433   by (simp add: prod_eq_iff)
   434 
   435 lemma split_conv [simp, code]: "split f (a, b) = f a b"
   436   by (fact prod.case)
   437 
   438 lemma splitI: "f a b \<Longrightarrow> split f (a, b)"
   439   by (rule split_conv [THEN iffD2])
   440 
   441 lemma splitD: "split f (a, b) \<Longrightarrow> f a b"
   442   by (rule split_conv [THEN iffD1])
   443 
   444 lemma split_Pair [simp]: "(\<lambda>(x, y). (x, y)) = id"
   445   by (simp add: fun_eq_iff split: prod.split)
   446 
   447 lemma split_eta: "(\<lambda>(x, y). f (x, y)) = f"
   448   -- {* Subsumes the old @{text split_Pair} when @{term f} is the identity function. *}
   449   by (simp add: fun_eq_iff split: prod.split)
   450 
   451 lemma split_comp: "split (f \<circ> g) x = f (g (fst x)) (snd x)"
   452   by (cases x) simp
   453 
   454 lemma split_twice: "split f (split g p) = split (\<lambda>x y. split f (g x y)) p"
   455   by (cases p) simp
   456 
   457 lemma The_split: "The (split P) = (THE xy. P (fst xy) (snd xy))"
   458   by (simp add: case_prod_unfold)
   459 
   460 lemma split_weak_cong: "p = q \<Longrightarrow> split c p = split c q"
   461   -- {* Prevents simplification of @{term c}: much faster *}
   462   by (fact prod.weak_case_cong)
   463 
   464 lemma cond_split_eta: "(!!x y. f x y = g (x, y)) ==> (%(x, y). f x y) = g"
   465   by (simp add: split_eta)
   466 
   467 lemma split_paired_all [no_atp]: "(!!x. PROP P x) == (!!a b. PROP P (a, b))"
   468 proof
   469   fix a b
   470   assume "!!x. PROP P x"
   471   then show "PROP P (a, b)" .
   472 next
   473   fix x
   474   assume "!!a b. PROP P (a, b)"
   475   from `PROP P (fst x, snd x)` show "PROP P x" by simp
   476 qed
   477 
   478 lemma case_prod_distrib: "f (case x of (x, y) \<Rightarrow> g x y) = (case x of (x, y) \<Rightarrow> f (g x y))"
   479   by (cases x) simp
   480 
   481 text {*
   482   The rule @{thm [source] split_paired_all} does not work with the
   483   Simplifier because it also affects premises in congrence rules,
   484   where this can lead to premises of the form @{text "!!a b. ... =
   485   ?P(a, b)"} which cannot be solved by reflexivity.
   486 *}
   487 
   488 lemmas split_tupled_all = split_paired_all unit_all_eq2
   489 
   490 ML {*
   491   (* replace parameters of product type by individual component parameters *)
   492   local (* filtering with exists_paired_all is an essential optimization *)
   493     fun exists_paired_all (Const (@{const_name Pure.all}, _) $ Abs (_, T, t)) =
   494           can HOLogic.dest_prodT T orelse exists_paired_all t
   495       | exists_paired_all (t $ u) = exists_paired_all t orelse exists_paired_all u
   496       | exists_paired_all (Abs (_, _, t)) = exists_paired_all t
   497       | exists_paired_all _ = false;
   498     val ss =
   499       simpset_of
   500        (put_simpset HOL_basic_ss @{context}
   501         addsimps [@{thm split_paired_all}, @{thm unit_all_eq2}, @{thm unit_abs_eta_conv}]
   502         addsimprocs [@{simproc unit_eq}]);
   503   in
   504     fun split_all_tac ctxt = SUBGOAL (fn (t, i) =>
   505       if exists_paired_all t then safe_full_simp_tac (put_simpset ss ctxt) i else no_tac);
   506 
   507     fun unsafe_split_all_tac ctxt = SUBGOAL (fn (t, i) =>
   508       if exists_paired_all t then full_simp_tac (put_simpset ss ctxt) i else no_tac);
   509 
   510     fun split_all ctxt th =
   511       if exists_paired_all (Thm.prop_of th)
   512       then full_simplify (put_simpset ss ctxt) th else th;
   513   end;
   514 *}
   515 
   516 setup {* map_theory_claset (fn ctxt => ctxt addSbefore ("split_all_tac", split_all_tac)) *}
   517 
   518 lemma split_paired_All [simp, no_atp]: "(ALL x. P x) = (ALL a b. P (a, b))"
   519   -- {* @{text "[iff]"} is not a good idea because it makes @{text blast} loop *}
   520   by fast
   521 
   522 lemma split_paired_Ex [simp, no_atp]: "(EX x. P x) = (EX a b. P (a, b))"
   523   by fast
   524 
   525 lemma split_paired_The [no_atp]: "(THE x. P x) = (THE (a, b). P (a, b))"
   526   -- {* Can't be added to simpset: loops! *}
   527   by (simp add: split_eta)
   528 
   529 text {*
   530   Simplification procedure for @{thm [source] cond_split_eta}.  Using
   531   @{thm [source] split_eta} as a rewrite rule is not general enough,
   532   and using @{thm [source] cond_split_eta} directly would render some
   533   existing proofs very inefficient; similarly for @{text
   534   split_beta}.
   535 *}
   536 
   537 ML {*
   538 local
   539   val cond_split_eta_ss =
   540     simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms cond_split_eta});
   541   fun Pair_pat k 0 (Bound m) = (m = k)
   542     | Pair_pat k i (Const (@{const_name Pair},  _) $ Bound m $ t) =
   543         i > 0 andalso m = k + i andalso Pair_pat k (i - 1) t
   544     | Pair_pat _ _ _ = false;
   545   fun no_args k i (Abs (_, _, t)) = no_args (k + 1) i t
   546     | no_args k i (t $ u) = no_args k i t andalso no_args k i u
   547     | no_args k i (Bound m) = m < k orelse m > k + i
   548     | no_args _ _ _ = true;
   549   fun split_pat tp i (Abs  (_, _, t)) = if tp 0 i t then SOME (i, t) else NONE
   550     | split_pat tp i (Const (@{const_name case_prod}, _) $ Abs (_, _, t)) = split_pat tp (i + 1) t
   551     | split_pat tp i _ = NONE;
   552   fun metaeq ctxt lhs rhs = mk_meta_eq (Goal.prove ctxt [] []
   553         (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)))
   554         (K (simp_tac (put_simpset cond_split_eta_ss ctxt) 1)));
   555 
   556   fun beta_term_pat k i (Abs (_, _, t)) = beta_term_pat (k + 1) i t
   557     | beta_term_pat k i (t $ u) =
   558         Pair_pat k i (t $ u) orelse (beta_term_pat k i t andalso beta_term_pat k i u)
   559     | beta_term_pat k i t = no_args k i t;
   560   fun eta_term_pat k i (f $ arg) = no_args k i f andalso Pair_pat k i arg
   561     | eta_term_pat _ _ _ = false;
   562   fun subst arg k i (Abs (x, T, t)) = Abs (x, T, subst arg (k+1) i t)
   563     | subst arg k i (t $ u) =
   564         if Pair_pat k i (t $ u) then incr_boundvars k arg
   565         else (subst arg k i t $ subst arg k i u)
   566     | subst arg k i t = t;
   567 in
   568   fun beta_proc ctxt (s as Const (@{const_name case_prod}, _) $ Abs (_, _, t) $ arg) =
   569         (case split_pat beta_term_pat 1 t of
   570           SOME (i, f) => SOME (metaeq ctxt s (subst arg 0 i f))
   571         | NONE => NONE)
   572     | beta_proc _ _ = NONE;
   573   fun eta_proc ctxt (s as Const (@{const_name case_prod}, _) $ Abs (_, _, t)) =
   574         (case split_pat eta_term_pat 1 t of
   575           SOME (_, ft) => SOME (metaeq ctxt s (let val (f $ arg) = ft in f end))
   576         | NONE => NONE)
   577     | eta_proc _ _ = NONE;
   578 end;
   579 *}
   580 simproc_setup split_beta ("split f z") = {* fn _ => fn ctxt => fn ct => beta_proc ctxt (term_of ct) *}
   581 simproc_setup split_eta ("split f") = {* fn _ => fn ctxt => fn ct => eta_proc ctxt (term_of ct) *}
   582 
   583 lemma split_beta [mono]: "(%(x, y). P x y) z = P (fst z) (snd z)"
   584   by (subst surjective_pairing, rule split_conv)
   585 
   586 lemma split_beta': "(\<lambda>(x,y). f x y) = (\<lambda>x. f (fst x) (snd x))"
   587   by (auto simp: fun_eq_iff)
   588 
   589 
   590 lemma split_split [no_atp]: "R(split c p) = (ALL x y. p = (x, y) --> R(c x y))"
   591   -- {* For use with @{text split} and the Simplifier. *}
   592   by (insert surj_pair [of p], clarify, simp)
   593 
   594 text {*
   595   @{thm [source] split_split} could be declared as @{text "[split]"}
   596   done after the Splitter has been speeded up significantly;
   597   precompute the constants involved and don't do anything unless the
   598   current goal contains one of those constants.
   599 *}
   600 
   601 lemma split_split_asm [no_atp]: "R (split c p) = (~(EX x y. p = (x, y) & (~R (c x y))))"
   602 by (subst split_split, simp)
   603 
   604 text {*
   605   \medskip @{term split} used as a logical connective or set former.
   606 
   607   \medskip These rules are for use with @{text blast}; could instead
   608   call @{text simp} using @{thm [source] prod.split} as rewrite. *}
   609 
   610 lemma splitI2: "!!p. [| !!a b. p = (a, b) ==> c a b |] ==> split c p"
   611   apply (simp only: split_tupled_all)
   612   apply (simp (no_asm_simp))
   613   done
   614 
   615 lemma splitI2': "!!p. [| !!a b. (a, b) = p ==> c a b x |] ==> split c p x"
   616   apply (simp only: split_tupled_all)
   617   apply (simp (no_asm_simp))
   618   done
   619 
   620 lemma splitE: "split c p ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q"
   621   by (induct p) auto
   622 
   623 lemma splitE': "split c p z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q"
   624   by (induct p) auto
   625 
   626 lemma splitE2:
   627   "[| Q (split P z);  !!x y. [|z = (x, y); Q (P x y)|] ==> R |] ==> R"
   628 proof -
   629   assume q: "Q (split P z)"
   630   assume r: "!!x y. [|z = (x, y); Q (P x y)|] ==> R"
   631   show R
   632     apply (rule r surjective_pairing)+
   633     apply (rule split_beta [THEN subst], rule q)
   634     done
   635 qed
   636 
   637 lemma splitD': "split R (a,b) c ==> R a b c"
   638   by simp
   639 
   640 lemma mem_splitI: "z: c a b ==> z: split c (a, b)"
   641   by simp
   642 
   643 lemma mem_splitI2: "!!p. [| !!a b. p = (a, b) ==> z: c a b |] ==> z: split c p"
   644 by (simp only: split_tupled_all, simp)
   645 
   646 lemma mem_splitE:
   647   assumes major: "z \<in> split c p"
   648     and cases: "\<And>x y. p = (x, y) \<Longrightarrow> z \<in> c x y \<Longrightarrow> Q"
   649   shows Q
   650   by (rule major [unfolded case_prod_unfold] cases surjective_pairing)+
   651 
   652 declare mem_splitI2 [intro!] mem_splitI [intro!] splitI2' [intro!] splitI2 [intro!] splitI [intro!]
   653 declare mem_splitE [elim!] splitE' [elim!] splitE [elim!]
   654 
   655 ML {*
   656 local (* filtering with exists_p_split is an essential optimization *)
   657   fun exists_p_split (Const (@{const_name case_prod},_) $ _ $ (Const (@{const_name Pair},_)$_$_)) = true
   658     | exists_p_split (t $ u) = exists_p_split t orelse exists_p_split u
   659     | exists_p_split (Abs (_, _, t)) = exists_p_split t
   660     | exists_p_split _ = false;
   661 in
   662 fun split_conv_tac ctxt = SUBGOAL (fn (t, i) =>
   663   if exists_p_split t
   664   then safe_full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps @{thms split_conv}) i
   665   else no_tac);
   666 end;
   667 *}
   668 
   669 (* This prevents applications of splitE for already splitted arguments leading
   670    to quite time-consuming computations (in particular for nested tuples) *)
   671 setup {* map_theory_claset (fn ctxt => ctxt addSbefore ("split_conv_tac", split_conv_tac)) *}
   672 
   673 lemma split_eta_SetCompr [simp, no_atp]: "(%u. EX x y. u = (x, y) & P (x, y)) = P"
   674   by (rule ext) fast
   675 
   676 lemma split_eta_SetCompr2 [simp, no_atp]: "(%u. EX x y. u = (x, y) & P x y) = split P"
   677   by (rule ext) fast
   678 
   679 lemma split_part [simp]: "(%(a,b). P & Q a b) = (%ab. P & split Q ab)"
   680   -- {* Allows simplifications of nested splits in case of independent predicates. *}
   681   by (rule ext) blast
   682 
   683 (* Do NOT make this a simp rule as it
   684    a) only helps in special situations
   685    b) can lead to nontermination in the presence of split_def
   686 *)
   687 lemma split_comp_eq: 
   688   fixes f :: "'a => 'b => 'c" and g :: "'d => 'a"
   689   shows "(%u. f (g (fst u)) (snd u)) = (split (%x. f (g x)))"
   690   by (rule ext) auto
   691 
   692 lemma pair_imageI [intro]: "(a, b) : A ==> f a b : (%(a, b). f a b) ` A"
   693   apply (rule_tac x = "(a, b)" in image_eqI)
   694    apply auto
   695   done
   696 
   697 lemma The_split_eq [simp]: "(THE (x',y'). x = x' & y = y') = (x, y)"
   698   by blast
   699 
   700 (*
   701 the following  would be slightly more general,
   702 but cannot be used as rewrite rule:
   703 ### Cannot add premise as rewrite rule because it contains (type) unknowns:
   704 ### ?y = .x
   705 Goal "[| P y; !!x. P x ==> x = y |] ==> (@(x',y). x = x' & P y) = (x,y)"
   706 by (rtac some_equality 1)
   707 by ( Simp_tac 1)
   708 by (split_all_tac 1)
   709 by (Asm_full_simp_tac 1)
   710 qed "The_split_eq";
   711 *)
   712 
   713 text {*
   714   Setup of internal @{text split_rule}.
   715 *}
   716 
   717 lemmas case_prodI = prod.case [THEN iffD2]
   718 
   719 lemma case_prodI2: "!!p. [| !!a b. p = (a, b) ==> c a b |] ==> case_prod c p"
   720   by (fact splitI2)
   721 
   722 lemma case_prodI2': "!!p. [| !!a b. (a, b) = p ==> c a b x |] ==> case_prod c p x"
   723   by (fact splitI2')
   724 
   725 lemma case_prodE: "case_prod c p ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q"
   726   by (fact splitE)
   727 
   728 lemma case_prodE': "case_prod c p z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q"
   729   by (fact splitE')
   730 
   731 declare case_prodI [intro!]
   732 
   733 lemma case_prod_beta:
   734   "case_prod f p = f (fst p) (snd p)"
   735   by (fact split_beta)
   736 
   737 lemma prod_cases3 [cases type]:
   738   obtains (fields) a b c where "y = (a, b, c)"
   739   by (cases y, case_tac b) blast
   740 
   741 lemma prod_induct3 [case_names fields, induct type]:
   742     "(!!a b c. P (a, b, c)) ==> P x"
   743   by (cases x) blast
   744 
   745 lemma prod_cases4 [cases type]:
   746   obtains (fields) a b c d where "y = (a, b, c, d)"
   747   by (cases y, case_tac c) blast
   748 
   749 lemma prod_induct4 [case_names fields, induct type]:
   750     "(!!a b c d. P (a, b, c, d)) ==> P x"
   751   by (cases x) blast
   752 
   753 lemma prod_cases5 [cases type]:
   754   obtains (fields) a b c d e where "y = (a, b, c, d, e)"
   755   by (cases y, case_tac d) blast
   756 
   757 lemma prod_induct5 [case_names fields, induct type]:
   758     "(!!a b c d e. P (a, b, c, d, e)) ==> P x"
   759   by (cases x) blast
   760 
   761 lemma prod_cases6 [cases type]:
   762   obtains (fields) a b c d e f where "y = (a, b, c, d, e, f)"
   763   by (cases y, case_tac e) blast
   764 
   765 lemma prod_induct6 [case_names fields, induct type]:
   766     "(!!a b c d e f. P (a, b, c, d, e, f)) ==> P x"
   767   by (cases x) blast
   768 
   769 lemma prod_cases7 [cases type]:
   770   obtains (fields) a b c d e f g where "y = (a, b, c, d, e, f, g)"
   771   by (cases y, case_tac f) blast
   772 
   773 lemma prod_induct7 [case_names fields, induct type]:
   774     "(!!a b c d e f g. P (a, b, c, d, e, f, g)) ==> P x"
   775   by (cases x) blast
   776 
   777 lemma split_def:
   778   "split = (\<lambda>c p. c (fst p) (snd p))"
   779   by (fact case_prod_unfold)
   780 
   781 definition internal_split :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c" where
   782   "internal_split == split"
   783 
   784 lemma internal_split_conv: "internal_split c (a, b) = c a b"
   785   by (simp only: internal_split_def split_conv)
   786 
   787 ML_file "Tools/split_rule.ML"
   788 setup Split_Rule.setup
   789 
   790 hide_const internal_split
   791 
   792 
   793 subsubsection {* Derived operations *}
   794 
   795 definition curry    :: "('a \<times> 'b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'c" where
   796   "curry = (\<lambda>c x y. c (x, y))"
   797 
   798 lemma curry_conv [simp, code]: "curry f a b = f (a, b)"
   799   by (simp add: curry_def)
   800 
   801 lemma curryI [intro!]: "f (a, b) \<Longrightarrow> curry f a b"
   802   by (simp add: curry_def)
   803 
   804 lemma curryD [dest!]: "curry f a b \<Longrightarrow> f (a, b)"
   805   by (simp add: curry_def)
   806 
   807 lemma curryE: "curry f a b \<Longrightarrow> (f (a, b) \<Longrightarrow> Q) \<Longrightarrow> Q"
   808   by (simp add: curry_def)
   809 
   810 lemma curry_split [simp]: "curry (split f) = f"
   811   by (simp add: curry_def split_def)
   812 
   813 lemma split_curry [simp]: "split (curry f) = f"
   814   by (simp add: curry_def split_def)
   815 
   816 lemma curry_K: "curry (\<lambda>x. c) = (\<lambda>x y. c)"
   817 by(simp add: fun_eq_iff)
   818 
   819 text {*
   820   The composition-uncurry combinator.
   821 *}
   822 
   823 notation fcomp (infixl "\<circ>>" 60)
   824 
   825 definition scomp :: "('a \<Rightarrow> 'b \<times> 'c) \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> 'd) \<Rightarrow> 'a \<Rightarrow> 'd" (infixl "\<circ>\<rightarrow>" 60) where
   826   "f \<circ>\<rightarrow> g = (\<lambda>x. case_prod g (f x))"
   827 
   828 lemma scomp_unfold: "scomp = (\<lambda>f g x. g (fst (f x)) (snd (f x)))"
   829   by (simp add: fun_eq_iff scomp_def case_prod_unfold)
   830 
   831 lemma scomp_apply [simp]: "(f \<circ>\<rightarrow> g) x = case_prod g (f x)"
   832   by (simp add: scomp_unfold case_prod_unfold)
   833 
   834 lemma Pair_scomp: "Pair x \<circ>\<rightarrow> f = f x"
   835   by (simp add: fun_eq_iff)
   836 
   837 lemma scomp_Pair: "x \<circ>\<rightarrow> Pair = x"
   838   by (simp add: fun_eq_iff)
   839 
   840 lemma scomp_scomp: "(f \<circ>\<rightarrow> g) \<circ>\<rightarrow> h = f \<circ>\<rightarrow> (\<lambda>x. g x \<circ>\<rightarrow> h)"
   841   by (simp add: fun_eq_iff scomp_unfold)
   842 
   843 lemma scomp_fcomp: "(f \<circ>\<rightarrow> g) \<circ>> h = f \<circ>\<rightarrow> (\<lambda>x. g x \<circ>> h)"
   844   by (simp add: fun_eq_iff scomp_unfold fcomp_def)
   845 
   846 lemma fcomp_scomp: "(f \<circ>> g) \<circ>\<rightarrow> h = f \<circ>> (g \<circ>\<rightarrow> h)"
   847   by (simp add: fun_eq_iff scomp_unfold)
   848 
   849 code_printing
   850   constant scomp \<rightharpoonup> (Eval) infixl 3 "#->"
   851 
   852 no_notation fcomp (infixl "\<circ>>" 60)
   853 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
   854 
   855 text {*
   856   @{term map_prod} --- action of the product functor upon
   857   functions.
   858 *}
   859 
   860 definition map_prod :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c \<times> 'd" where
   861   "map_prod f g = (\<lambda>(x, y). (f x, g y))"
   862 
   863 lemma map_prod_simp [simp, code]:
   864   "map_prod f g (a, b) = (f a, g b)"
   865   by (simp add: map_prod_def)
   866 
   867 functor map_prod: map_prod
   868   by (auto simp add: split_paired_all)
   869 
   870 lemma fst_map_prod [simp]:
   871   "fst (map_prod f g x) = f (fst x)"
   872   by (cases x) simp_all
   873 
   874 lemma snd_prod_fun [simp]:
   875   "snd (map_prod f g x) = g (snd x)"
   876   by (cases x) simp_all
   877 
   878 lemma fst_comp_map_prod [simp]:
   879   "fst \<circ> map_prod f g = f \<circ> fst"
   880   by (rule ext) simp_all
   881 
   882 lemma snd_comp_map_prod [simp]:
   883   "snd \<circ> map_prod f g = g \<circ> snd"
   884   by (rule ext) simp_all
   885 
   886 lemma map_prod_compose:
   887   "map_prod (f1 o f2) (g1 o g2) = (map_prod f1 g1 o map_prod f2 g2)"
   888   by (rule ext) (simp add: map_prod.compositionality comp_def)
   889 
   890 lemma map_prod_ident [simp]:
   891   "map_prod (%x. x) (%y. y) = (%z. z)"
   892   by (rule ext) (simp add: map_prod.identity)
   893 
   894 lemma map_prod_imageI [intro]:
   895   "(a, b) \<in> R \<Longrightarrow> (f a, g b) \<in> map_prod f g ` R"
   896   by (rule image_eqI) simp_all
   897 
   898 lemma prod_fun_imageE [elim!]:
   899   assumes major: "c \<in> map_prod f g ` R"
   900     and cases: "\<And>x y. c = (f x, g y) \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> P"
   901   shows P
   902   apply (rule major [THEN imageE])
   903   apply (case_tac x)
   904   apply (rule cases)
   905   apply simp_all
   906   done
   907 
   908 definition apfst :: "('a \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c \<times> 'b" where
   909   "apfst f = map_prod f id"
   910 
   911 definition apsnd :: "('b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'a \<times> 'c" where
   912   "apsnd f = map_prod id f"
   913 
   914 lemma apfst_conv [simp, code]:
   915   "apfst f (x, y) = (f x, y)" 
   916   by (simp add: apfst_def)
   917 
   918 lemma apsnd_conv [simp, code]:
   919   "apsnd f (x, y) = (x, f y)" 
   920   by (simp add: apsnd_def)
   921 
   922 lemma fst_apfst [simp]:
   923   "fst (apfst f x) = f (fst x)"
   924   by (cases x) simp
   925 
   926 lemma fst_comp_apfst [simp]:
   927   "fst \<circ> apfst f = f \<circ> fst"
   928   by (simp add: fun_eq_iff)
   929 
   930 lemma fst_apsnd [simp]:
   931   "fst (apsnd f x) = fst x"
   932   by (cases x) simp
   933 
   934 lemma fst_comp_apsnd [simp]:
   935   "fst \<circ> apsnd f = fst"
   936   by (simp add: fun_eq_iff)
   937 
   938 lemma snd_apfst [simp]:
   939   "snd (apfst f x) = snd x"
   940   by (cases x) simp
   941 
   942 lemma snd_comp_apfst [simp]:
   943   "snd \<circ> apfst f = snd"
   944   by (simp add: fun_eq_iff)
   945 
   946 lemma snd_apsnd [simp]:
   947   "snd (apsnd f x) = f (snd x)"
   948   by (cases x) simp
   949 
   950 lemma snd_comp_apsnd [simp]:
   951   "snd \<circ> apsnd f = f \<circ> snd"
   952   by (simp add: fun_eq_iff)
   953 
   954 lemma apfst_compose:
   955   "apfst f (apfst g x) = apfst (f \<circ> g) x"
   956   by (cases x) simp
   957 
   958 lemma apsnd_compose:
   959   "apsnd f (apsnd g x) = apsnd (f \<circ> g) x"
   960   by (cases x) simp
   961 
   962 lemma apfst_apsnd [simp]:
   963   "apfst f (apsnd g x) = (f (fst x), g (snd x))"
   964   by (cases x) simp
   965 
   966 lemma apsnd_apfst [simp]:
   967   "apsnd f (apfst g x) = (g (fst x), f (snd x))"
   968   by (cases x) simp
   969 
   970 lemma apfst_id [simp] :
   971   "apfst id = id"
   972   by (simp add: fun_eq_iff)
   973 
   974 lemma apsnd_id [simp] :
   975   "apsnd id = id"
   976   by (simp add: fun_eq_iff)
   977 
   978 lemma apfst_eq_conv [simp]:
   979   "apfst f x = apfst g x \<longleftrightarrow> f (fst x) = g (fst x)"
   980   by (cases x) simp
   981 
   982 lemma apsnd_eq_conv [simp]:
   983   "apsnd f x = apsnd g x \<longleftrightarrow> f (snd x) = g (snd x)"
   984   by (cases x) simp
   985 
   986 lemma apsnd_apfst_commute:
   987   "apsnd f (apfst g p) = apfst g (apsnd f p)"
   988   by simp
   989 
   990 context
   991 begin
   992 
   993 local_setup {* Local_Theory.map_naming (Name_Space.mandatory_path "prod") *}
   994 
   995 definition swap :: "'a \<times> 'b \<Rightarrow> 'b \<times> 'a"
   996 where
   997   "swap p = (snd p, fst p)"
   998 
   999 end
  1000 
  1001 lemma swap_simp [simp]:
  1002   "prod.swap (x, y) = (y, x)"
  1003   by (simp add: prod.swap_def)
  1004 
  1005 lemma pair_in_swap_image [simp]:
  1006   "(y, x) \<in> prod.swap ` A \<longleftrightarrow> (x, y) \<in> A"
  1007   by (auto intro!: image_eqI)
  1008 
  1009 lemma inj_swap [simp]:
  1010   "inj_on prod.swap A"
  1011   by (rule inj_onI) auto
  1012 
  1013 lemma swap_inj_on:
  1014   "inj_on (\<lambda>(i, j). (j, i)) A"
  1015   by (rule inj_onI) auto
  1016 
  1017 lemma case_swap [simp]:
  1018   "(case prod.swap p of (y, x) \<Rightarrow> f x y) = (case p of (x, y) \<Rightarrow> f x y)"
  1019   by (cases p) simp
  1020 
  1021 text {*
  1022   Disjoint union of a family of sets -- Sigma.
  1023 *}
  1024 
  1025 definition Sigma :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> ('a \<times> 'b) set" where
  1026   Sigma_def: "Sigma A B == UN x:A. UN y:B x. {Pair x y}"
  1027 
  1028 abbreviation
  1029   Times :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<times> 'b) set"
  1030     (infixr "<*>" 80) where
  1031   "A <*> B == Sigma A (%_. B)"
  1032 
  1033 notation (xsymbols)
  1034   Times  (infixr "\<times>" 80)
  1035 
  1036 notation (HTML output)
  1037   Times  (infixr "\<times>" 80)
  1038 
  1039 hide_const (open) Times
  1040 
  1041 syntax
  1042   "_Sigma" :: "[pttrn, 'a set, 'b set] => ('a * 'b) set"  ("(3SIGMA _:_./ _)" [0, 0, 10] 10)
  1043 translations
  1044   "SIGMA x:A. B" == "CONST Sigma A (%x. B)"
  1045 
  1046 lemma SigmaI [intro!]: "[| a:A;  b:B(a) |] ==> (a,b) : Sigma A B"
  1047   by (unfold Sigma_def) blast
  1048 
  1049 lemma SigmaE [elim!]:
  1050     "[| c: Sigma A B;
  1051         !!x y.[| x:A;  y:B(x);  c=(x,y) |] ==> P
  1052      |] ==> P"
  1053   -- {* The general elimination rule. *}
  1054   by (unfold Sigma_def) blast
  1055 
  1056 text {*
  1057   Elimination of @{term "(a, b) : A \<times> B"} -- introduces no
  1058   eigenvariables.
  1059 *}
  1060 
  1061 lemma SigmaD1: "(a, b) : Sigma A B ==> a : A"
  1062   by blast
  1063 
  1064 lemma SigmaD2: "(a, b) : Sigma A B ==> b : B a"
  1065   by blast
  1066 
  1067 lemma SigmaE2:
  1068     "[| (a, b) : Sigma A B;
  1069         [| a:A;  b:B(a) |] ==> P
  1070      |] ==> P"
  1071   by blast
  1072 
  1073 lemma Sigma_cong:
  1074      "\<lbrakk>A = B; !!x. x \<in> B \<Longrightarrow> C x = D x\<rbrakk>
  1075       \<Longrightarrow> (SIGMA x: A. C x) = (SIGMA x: B. D x)"
  1076   by auto
  1077 
  1078 lemma Sigma_mono: "[| A <= C; !!x. x:A ==> B x <= D x |] ==> Sigma A B <= Sigma C D"
  1079   by blast
  1080 
  1081 lemma Sigma_empty1 [simp]: "Sigma {} B = {}"
  1082   by blast
  1083 
  1084 lemma Sigma_empty2 [simp]: "A <*> {} = {}"
  1085   by blast
  1086 
  1087 lemma UNIV_Times_UNIV [simp]: "UNIV <*> UNIV = UNIV"
  1088   by auto
  1089 
  1090 lemma Compl_Times_UNIV1 [simp]: "- (UNIV <*> A) = UNIV <*> (-A)"
  1091   by auto
  1092 
  1093 lemma Compl_Times_UNIV2 [simp]: "- (A <*> UNIV) = (-A) <*> UNIV"
  1094   by auto
  1095 
  1096 lemma mem_Sigma_iff [iff]: "((a,b): Sigma A B) = (a:A & b:B(a))"
  1097   by blast
  1098 
  1099 lemma Times_subset_cancel2: "x:C ==> (A <*> C <= B <*> C) = (A <= B)"
  1100   by blast
  1101 
  1102 lemma Times_eq_cancel2: "x:C ==> (A <*> C = B <*> C) = (A = B)"
  1103   by (blast elim: equalityE)
  1104 
  1105 lemma SetCompr_Sigma_eq:
  1106     "Collect (split (%x y. P x & Q x y)) = (SIGMA x:Collect P. Collect (Q x))"
  1107   by blast
  1108 
  1109 lemma Collect_split [simp]: "{(a,b). P a & Q b} = Collect P <*> Collect Q"
  1110   by blast
  1111 
  1112 lemma UN_Times_distrib:
  1113   "(UN (a,b):(A <*> B). E a <*> F b) = (UNION A E) <*> (UNION B F)"
  1114   -- {* Suggested by Pierre Chartier *}
  1115   by blast
  1116 
  1117 lemma split_paired_Ball_Sigma [simp, no_atp]:
  1118     "(ALL z: Sigma A B. P z) = (ALL x:A. ALL y: B x. P(x,y))"
  1119   by blast
  1120 
  1121 lemma split_paired_Bex_Sigma [simp, no_atp]:
  1122     "(EX z: Sigma A B. P z) = (EX x:A. EX y: B x. P(x,y))"
  1123   by blast
  1124 
  1125 lemma Sigma_Un_distrib1: "(SIGMA i:I Un J. C(i)) = (SIGMA i:I. C(i)) Un (SIGMA j:J. C(j))"
  1126   by blast
  1127 
  1128 lemma Sigma_Un_distrib2: "(SIGMA i:I. A(i) Un B(i)) = (SIGMA i:I. A(i)) Un (SIGMA i:I. B(i))"
  1129   by blast
  1130 
  1131 lemma Sigma_Int_distrib1: "(SIGMA i:I Int J. C(i)) = (SIGMA i:I. C(i)) Int (SIGMA j:J. C(j))"
  1132   by blast
  1133 
  1134 lemma Sigma_Int_distrib2: "(SIGMA i:I. A(i) Int B(i)) = (SIGMA i:I. A(i)) Int (SIGMA i:I. B(i))"
  1135   by blast
  1136 
  1137 lemma Sigma_Diff_distrib1: "(SIGMA i:I - J. C(i)) = (SIGMA i:I. C(i)) - (SIGMA j:J. C(j))"
  1138   by blast
  1139 
  1140 lemma Sigma_Diff_distrib2: "(SIGMA i:I. A(i) - B(i)) = (SIGMA i:I. A(i)) - (SIGMA i:I. B(i))"
  1141   by blast
  1142 
  1143 lemma Sigma_Union: "Sigma (Union X) B = (UN A:X. Sigma A B)"
  1144   by blast
  1145 
  1146 text {*
  1147   Non-dependent versions are needed to avoid the need for higher-order
  1148   matching, especially when the rules are re-oriented.
  1149 *}
  1150 
  1151 lemma Times_Un_distrib1: "(A Un B) <*> C = (A <*> C) Un (B <*> C)"
  1152   by (fact Sigma_Un_distrib1)
  1153 
  1154 lemma Times_Int_distrib1: "(A Int B) <*> C = (A <*> C) Int (B <*> C)"
  1155   by (fact Sigma_Int_distrib1)
  1156 
  1157 lemma Times_Diff_distrib1: "(A - B) <*> C = (A <*> C) - (B <*> C)"
  1158   by (fact Sigma_Diff_distrib1)
  1159 
  1160 lemma Times_empty[simp]: "A \<times> B = {} \<longleftrightarrow> A = {} \<or> B = {}"
  1161   by auto
  1162 
  1163 lemma times_eq_iff: "A \<times> B = C \<times> D \<longleftrightarrow> A = C \<and> B = D \<or> ((A = {} \<or> B = {}) \<and> (C = {} \<or> D = {}))"
  1164   by auto
  1165 
  1166 lemma fst_image_times[simp]: "fst ` (A \<times> B) = (if B = {} then {} else A)"
  1167   by force
  1168 
  1169 lemma snd_image_times[simp]: "snd ` (A \<times> B) = (if A = {} then {} else B)"
  1170   by force
  1171 
  1172 lemma vimage_fst:
  1173   "fst -` A = A \<times> UNIV"
  1174   by auto
  1175 
  1176 lemma vimage_snd:
  1177   "snd -` A = UNIV \<times> A"
  1178   by auto
  1179 
  1180 lemma insert_times_insert[simp]:
  1181   "insert a A \<times> insert b B =
  1182    insert (a,b) (A \<times> insert b B \<union> insert a A \<times> B)"
  1183 by blast
  1184 
  1185 lemma vimage_Times: "f -` (A \<times> B) = ((fst \<circ> f) -` A) \<inter> ((snd \<circ> f) -` B)"
  1186   apply auto
  1187   apply (case_tac "f x")
  1188   apply auto
  1189   done
  1190 
  1191 lemma times_Int_times: "A \<times> B \<inter> C \<times> D = (A \<inter> C) \<times> (B \<inter> D)"
  1192   by auto
  1193 
  1194 lemma product_swap:
  1195   "prod.swap ` (A \<times> B) = B \<times> A"
  1196   by (auto simp add: set_eq_iff)
  1197 
  1198 lemma swap_product:
  1199   "(\<lambda>(i, j). (j, i)) ` (A \<times> B) = B \<times> A"
  1200   by (auto simp add: set_eq_iff)
  1201 
  1202 lemma image_split_eq_Sigma:
  1203   "(\<lambda>x. (f x, g x)) ` A = Sigma (f ` A) (\<lambda>x. g ` (f -` {x} \<inter> A))"
  1204 proof (safe intro!: imageI)
  1205   fix a b assume *: "a \<in> A" "b \<in> A" and eq: "f a = f b"
  1206   show "(f b, g a) \<in> (\<lambda>x. (f x, g x)) ` A"
  1207     using * eq[symmetric] by auto
  1208 qed simp_all
  1209 
  1210 definition product :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<times> 'b) set" where
  1211   [code_abbrev]: "product A B = A \<times> B"
  1212 
  1213 hide_const (open) product
  1214 
  1215 lemma member_product:
  1216   "x \<in> Product_Type.product A B \<longleftrightarrow> x \<in> A \<times> B"
  1217   by (simp add: product_def)
  1218 
  1219 text {* The following @{const map_prod} lemmas are due to Joachim Breitner: *}
  1220 
  1221 lemma map_prod_inj_on:
  1222   assumes "inj_on f A" and "inj_on g B"
  1223   shows "inj_on (map_prod f g) (A \<times> B)"
  1224 proof (rule inj_onI)
  1225   fix x :: "'a \<times> 'c" and y :: "'a \<times> 'c"
  1226   assume "x \<in> A \<times> B" hence "fst x \<in> A" and "snd x \<in> B" by auto
  1227   assume "y \<in> A \<times> B" hence "fst y \<in> A" and "snd y \<in> B" by auto
  1228   assume "map_prod f g x = map_prod f g y"
  1229   hence "fst (map_prod f g x) = fst (map_prod f g y)" by (auto)
  1230   hence "f (fst x) = f (fst y)" by (cases x,cases y,auto)
  1231   with `inj_on f A` and `fst x \<in> A` and `fst y \<in> A`
  1232   have "fst x = fst y" by (auto dest:dest:inj_onD)
  1233   moreover from `map_prod f g x = map_prod f g y`
  1234   have "snd (map_prod f g x) = snd (map_prod f g y)" by (auto)
  1235   hence "g (snd x) = g (snd y)" by (cases x,cases y,auto)
  1236   with `inj_on g B` and `snd x \<in> B` and `snd y \<in> B`
  1237   have "snd x = snd y" by (auto dest:dest:inj_onD)
  1238   ultimately show "x = y" by(rule prod_eqI)
  1239 qed
  1240 
  1241 lemma map_prod_surj:
  1242   fixes f :: "'a \<Rightarrow> 'b" and g :: "'c \<Rightarrow> 'd"
  1243   assumes "surj f" and "surj g"
  1244   shows "surj (map_prod f g)"
  1245 unfolding surj_def
  1246 proof
  1247   fix y :: "'b \<times> 'd"
  1248   from `surj f` obtain a where "fst y = f a" by (auto elim:surjE)
  1249   moreover
  1250   from `surj g` obtain b where "snd y = g b" by (auto elim:surjE)
  1251   ultimately have "(fst y, snd y) = map_prod f g (a,b)" by auto
  1252   thus "\<exists>x. y = map_prod f g x" by auto
  1253 qed
  1254 
  1255 lemma map_prod_surj_on:
  1256   assumes "f ` A = A'" and "g ` B = B'"
  1257   shows "map_prod f g ` (A \<times> B) = A' \<times> B'"
  1258 unfolding image_def
  1259 proof(rule set_eqI,rule iffI)
  1260   fix x :: "'a \<times> 'c"
  1261   assume "x \<in> {y\<Colon>'a \<times> 'c. \<exists>x\<Colon>'b \<times> 'd\<in>A \<times> B. y = map_prod f g x}"
  1262   then obtain y where "y \<in> A \<times> B" and "x = map_prod f g y" by blast
  1263   from `image f A = A'` and `y \<in> A \<times> B` have "f (fst y) \<in> A'" by auto
  1264   moreover from `image g B = B'` and `y \<in> A \<times> B` have "g (snd y) \<in> B'" by auto
  1265   ultimately have "(f (fst y), g (snd y)) \<in> (A' \<times> B')" by auto
  1266   with `x = map_prod f g y` show "x \<in> A' \<times> B'" by (cases y, auto)
  1267 next
  1268   fix x :: "'a \<times> 'c"
  1269   assume "x \<in> A' \<times> B'" hence "fst x \<in> A'" and "snd x \<in> B'" by auto
  1270   from `image f A = A'` and `fst x \<in> A'` have "fst x \<in> image f A" by auto
  1271   then obtain a where "a \<in> A" and "fst x = f a" by (rule imageE)
  1272   moreover from `image g B = B'` and `snd x \<in> B'`
  1273   obtain b where "b \<in> B" and "snd x = g b" by auto
  1274   ultimately have "(fst x, snd x) = map_prod f g (a,b)" by auto
  1275   moreover from `a \<in> A` and  `b \<in> B` have "(a , b) \<in> A \<times> B" by auto
  1276   ultimately have "\<exists>y \<in> A \<times> B. x = map_prod f g y" by auto
  1277   thus "x \<in> {x. \<exists>y \<in> A \<times> B. x = map_prod f g y}" by auto
  1278 qed
  1279 
  1280 
  1281 subsection {* Simproc for rewriting a set comprehension into a pointfree expression *}
  1282 
  1283 ML_file "Tools/set_comprehension_pointfree.ML"
  1284 
  1285 setup {*
  1286   Code_Preproc.map_pre (fn ctxt => ctxt addsimprocs
  1287     [Raw_Simplifier.make_simproc {name = "set comprehension", lhss = [@{cpat "Collect ?P"}],
  1288     proc = K Set_Comprehension_Pointfree.code_simproc, identifier = []}])
  1289 *}
  1290 
  1291 
  1292 subsection {* Inductively defined sets *}
  1293 
  1294 (* simplify {(x1, ..., xn). (x1, ..., xn) : S} to S *)
  1295 simproc_setup Collect_mem ("Collect t") = {*
  1296   fn _ => fn ctxt => fn ct =>
  1297     (case term_of ct of
  1298       S as Const (@{const_name Collect}, Type (@{type_name fun}, [_, T])) $ t =>
  1299         let val (u, _, ps) = HOLogic.strip_psplits t in
  1300           (case u of
  1301             (c as Const (@{const_name Set.member}, _)) $ q $ S' =>
  1302               (case try (HOLogic.strip_ptuple ps) q of
  1303                 NONE => NONE
  1304               | SOME ts =>
  1305                   if not (Term.is_open S') andalso
  1306                     ts = map Bound (length ps downto 0)
  1307                   then
  1308                     let val simp =
  1309                       full_simp_tac (put_simpset HOL_basic_ss ctxt
  1310                         addsimps [@{thm split_paired_all}, @{thm split_conv}]) 1
  1311                     in
  1312                       SOME (Goal.prove ctxt [] []
  1313                         (Const (@{const_name Pure.eq}, T --> T --> propT) $ S $ S')
  1314                         (K (EVERY
  1315                           [rtac eq_reflection 1, rtac @{thm subset_antisym} 1,
  1316                            rtac subsetI 1, dtac CollectD 1, simp,
  1317                            rtac subsetI 1, rtac CollectI 1, simp])))
  1318                     end
  1319                   else NONE)
  1320           | _ => NONE)
  1321         end
  1322     | _ => NONE)
  1323 *}
  1324 ML_file "Tools/inductive_set.ML"
  1325 
  1326 
  1327 subsection {* Legacy theorem bindings and duplicates *}
  1328 
  1329 lemma PairE:
  1330   obtains x y where "p = (x, y)"
  1331   by (fact prod.exhaust)
  1332 
  1333 lemmas Pair_eq = prod.inject
  1334 lemmas fst_conv = prod.sel(1)
  1335 lemmas snd_conv = prod.sel(2)
  1336 lemmas pair_collapse = prod.collapse
  1337 lemmas split = split_conv
  1338 lemmas Pair_fst_snd_eq = prod_eq_iff
  1339 
  1340 hide_const (open) prod
  1341 
  1342 end