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