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