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