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