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