src/HOL/Product_Type.thy
author paulson
Wed Jun 16 14:56:39 2004 +0200 (2004-06-16)
changeset 14952 47455995693d
parent 14565 c6dc17aab88a
child 15131 c69542757a4d
permissions -rw-r--r--
removal of x-symbol syntax <Sigma> for dependent products
     1 (*  Title:      HOL/Product_Type.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1992  University of Cambridge
     5 *)
     6 
     7 header {* Cartesian products *}
     8 
     9 theory Product_Type = Fun
    10 files ("Tools/split_rule.ML"):
    11 
    12 subsection {* Unit *}
    13 
    14 typedef unit = "{True}"
    15 proof
    16   show "True : ?unit" by blast
    17 qed
    18 
    19 constdefs
    20   Unity :: unit    ("'(')")
    21   "() == Abs_unit True"
    22 
    23 lemma unit_eq: "u = ()"
    24   by (induct u) (simp add: unit_def Unity_def)
    25 
    26 text {*
    27   Simplification procedure for @{thm [source] unit_eq}.  Cannot use
    28   this rule directly --- it loops!
    29 *}
    30 
    31 ML_setup {*
    32   val unit_eq_proc =
    33     let val unit_meta_eq = mk_meta_eq (thm "unit_eq") in
    34       Simplifier.simproc (Theory.sign_of (the_context ())) "unit_eq" ["x::unit"]
    35       (fn _ => fn _ => fn t => if HOLogic.is_unit t then None else Some unit_meta_eq)
    36     end;
    37 
    38   Addsimprocs [unit_eq_proc];
    39 *}
    40 
    41 lemma unit_all_eq1: "(!!x::unit. PROP P x) == PROP P ()"
    42   by simp
    43 
    44 lemma unit_all_eq2: "(!!x::unit. PROP P) == PROP P"
    45   by (rule triv_forall_equality)
    46 
    47 lemma unit_induct [induct type: unit]: "P () ==> P x"
    48   by simp
    49 
    50 text {*
    51   This rewrite counters the effect of @{text unit_eq_proc} on @{term
    52   [source] "%u::unit. f u"}, replacing it by @{term [source]
    53   f} rather than by @{term [source] "%u. f ()"}.
    54 *}
    55 
    56 lemma unit_abs_eta_conv [simp]: "(%u::unit. f ()) = f"
    57   by (rule ext) simp
    58 
    59 
    60 subsection {* Pairs *}
    61 
    62 subsubsection {* Type definition *}
    63 
    64 constdefs
    65   Pair_Rep :: "['a, 'b] => ['a, 'b] => bool"
    66   "Pair_Rep == (%a b. %x y. x=a & y=b)"
    67 
    68 global
    69 
    70 typedef (Prod)
    71   ('a, 'b) "*"    (infixr 20)
    72     = "{f. EX a b. f = Pair_Rep (a::'a) (b::'b)}"
    73 proof
    74   fix a b show "Pair_Rep a b : ?Prod"
    75     by blast
    76 qed
    77 
    78 syntax (xsymbols)
    79   "*"      :: "[type, type] => type"         ("(_ \<times>/ _)" [21, 20] 20)
    80 syntax (HTML output)
    81   "*"      :: "[type, type] => type"         ("(_ \<times>/ _)" [21, 20] 20)
    82 
    83 local
    84 
    85 
    86 subsubsection {* Abstract constants and syntax *}
    87 
    88 global
    89 
    90 consts
    91   fst      :: "'a * 'b => 'a"
    92   snd      :: "'a * 'b => 'b"
    93   split    :: "[['a, 'b] => 'c, 'a * 'b] => 'c"
    94   curry    :: "['a * 'b => 'c, 'a, 'b] => 'c"
    95   prod_fun :: "['a => 'b, 'c => 'd, 'a * 'c] => 'b * 'd"
    96   Pair     :: "['a, 'b] => 'a * 'b"
    97   Sigma    :: "['a set, 'a => 'b set] => ('a * 'b) set"
    98 
    99 local
   100 
   101 text {*
   102   Patterns -- extends pre-defined type @{typ pttrn} used in
   103   abstractions.
   104 *}
   105 
   106 nonterminals
   107   tuple_args patterns
   108 
   109 syntax
   110   "_tuple"      :: "'a => tuple_args => 'a * 'b"        ("(1'(_,/ _'))")
   111   "_tuple_arg"  :: "'a => tuple_args"                   ("_")
   112   "_tuple_args" :: "'a => tuple_args => tuple_args"     ("_,/ _")
   113   "_pattern"    :: "[pttrn, patterns] => pttrn"         ("'(_,/ _')")
   114   ""            :: "pttrn => patterns"                  ("_")
   115   "_patterns"   :: "[pttrn, patterns] => patterns"      ("_,/ _")
   116   "@Sigma" ::"[pttrn, 'a set, 'b set] => ('a * 'b) set" ("(3SIGMA _:_./ _)" 10)
   117   "@Times" ::"['a set,  'a => 'b set] => ('a * 'b) set" (infixr "<*>" 80)
   118 
   119 translations
   120   "(x, y)"       == "Pair x y"
   121   "_tuple x (_tuple_args y z)" == "_tuple x (_tuple_arg (_tuple y z))"
   122   "%(x,y,zs).b"  == "split(%x (y,zs).b)"
   123   "%(x,y).b"     == "split(%x y. b)"
   124   "_abs (Pair x y) t" => "%(x,y).t"
   125   (* The last rule accommodates tuples in `case C ... (x,y) ... => ...'
   126      The (x,y) is parsed as `Pair x y' because it is logic, not pttrn *)
   127 
   128   "SIGMA x:A. B" => "Sigma A (%x. B)"
   129   "A <*> B"      => "Sigma A (_K B)"
   130 
   131 (* reconstructs pattern from (nested) splits, avoiding eta-contraction of body*)
   132 (* works best with enclosing "let", if "let" does not avoid eta-contraction   *)
   133 print_translation {*
   134 let fun split_tr' [Abs (x,T,t as (Abs abs))] =
   135       (* split (%x y. t) => %(x,y) t *)
   136       let val (y,t') = atomic_abs_tr' abs;
   137           val (x',t'') = atomic_abs_tr' (x,T,t');
   138     
   139       in Syntax.const "_abs" $ (Syntax.const "_pattern" $x'$y) $ t'' end
   140     | split_tr' [Abs (x,T,(s as Const ("split",_)$t))] =
   141        (* split (%x. (split (%y z. t))) => %(x,y,z). t *)
   142        let val (Const ("_abs",_)$(Const ("_pattern",_)$y$z)$t') = split_tr' [t];
   143            val (x',t'') = atomic_abs_tr' (x,T,t');
   144        in Syntax.const "_abs"$ 
   145            (Syntax.const "_pattern"$x'$(Syntax.const "_patterns"$y$z))$t'' end
   146     | split_tr' [Const ("split",_)$t] =
   147        (* split (split (%x y z. t)) => %((x,y),z). t *)   
   148        split_tr' [(split_tr' [t])] (* inner split_tr' creates next pattern *)
   149     | split_tr' [Const ("_abs",_)$x_y$(Abs abs)] =
   150        (* split (%pttrn z. t) => %(pttrn,z). t *)
   151        let val (z,t) = atomic_abs_tr' abs;
   152        in Syntax.const "_abs" $ (Syntax.const "_pattern" $x_y$z) $ t end
   153     | split_tr' _ =  raise Match;
   154 in [("split", split_tr')]
   155 end
   156 *}
   157 
   158 text{*Deleted x-symbol and html support using @{text"\<Sigma>"} (Sigma) because of the danger of confusion with Sum.*}
   159 syntax (xsymbols)
   160   "@Times" :: "['a set,  'a => 'b set] => ('a * 'b) set"  ("_ \<times> _" [81, 80] 80)
   161 
   162 syntax (HTML output)
   163   "@Times" :: "['a set,  'a => 'b set] => ('a * 'b) set"  ("_ \<times> _" [81, 80] 80)
   164 
   165 print_translation {* [("Sigma", dependent_tr' ("@Sigma", "@Times"))] *}
   166 
   167 
   168 subsubsection {* Definitions *}
   169 
   170 defs
   171   Pair_def:     "Pair a b == Abs_Prod(Pair_Rep a b)"
   172   fst_def:      "fst p == THE a. EX b. p = (a, b)"
   173   snd_def:      "snd p == THE b. EX a. p = (a, b)"
   174   split_def:    "split == (%c p. c (fst p) (snd p))"
   175   curry_def:    "curry == (%c x y. c (x,y))"
   176   prod_fun_def: "prod_fun f g == split(%x y.(f(x), g(y)))"
   177   Sigma_def:    "Sigma A B == UN x:A. UN y:B(x). {(x, y)}"
   178 
   179 
   180 subsubsection {* Lemmas and proof tool setup *}
   181 
   182 lemma ProdI: "Pair_Rep a b : Prod"
   183   by (unfold Prod_def) blast
   184 
   185 lemma Pair_Rep_inject: "Pair_Rep a b = Pair_Rep a' b' ==> a = a' & b = b'"
   186   apply (unfold Pair_Rep_def)
   187   apply (drule fun_cong [THEN fun_cong], blast)
   188   done
   189 
   190 lemma inj_on_Abs_Prod: "inj_on Abs_Prod Prod"
   191   apply (rule inj_on_inverseI)
   192   apply (erule Abs_Prod_inverse)
   193   done
   194 
   195 lemma Pair_inject:
   196   "(a, b) = (a', b') ==> (a = a' ==> b = b' ==> R) ==> R"
   197 proof -
   198   case rule_context [unfolded Pair_def]
   199   show ?thesis
   200     apply (rule inj_on_Abs_Prod [THEN inj_onD, THEN Pair_Rep_inject, THEN conjE])
   201     apply (rule rule_context ProdI)+
   202     .
   203 qed
   204 
   205 lemma Pair_eq [iff]: "((a, b) = (a', b')) = (a = a' & b = b')"
   206   by (blast elim!: Pair_inject)
   207 
   208 lemma fst_conv [simp]: "fst (a, b) = a"
   209   by (unfold fst_def) blast
   210 
   211 lemma snd_conv [simp]: "snd (a, b) = b"
   212   by (unfold snd_def) blast
   213 
   214 lemma fst_eqD: "fst (x, y) = a ==> x = a"
   215   by simp
   216 
   217 lemma snd_eqD: "snd (x, y) = a ==> y = a"
   218   by simp
   219 
   220 lemma PairE_lemma: "EX x y. p = (x, y)"
   221   apply (unfold Pair_def)
   222   apply (rule Rep_Prod [unfolded Prod_def, THEN CollectE])
   223   apply (erule exE, erule exE, rule exI, rule exI)
   224   apply (rule Rep_Prod_inverse [symmetric, THEN trans])
   225   apply (erule arg_cong)
   226   done
   227 
   228 lemma PairE [cases type: *]: "(!!x y. p = (x, y) ==> Q) ==> Q"
   229   by (insert PairE_lemma [of p]) blast
   230 
   231 ML_setup {*
   232   local val PairE = thm "PairE" in
   233     fun pair_tac s =
   234       EVERY' [res_inst_tac [("p", s)] PairE, hyp_subst_tac, K prune_params_tac];
   235   end;
   236 *}
   237 
   238 lemma surjective_pairing: "p = (fst p, snd p)"
   239   -- {* Do not add as rewrite rule: invalidates some proofs in IMP *}
   240   by (cases p) simp
   241 
   242 declare surjective_pairing [symmetric, simp]
   243 
   244 lemma surj_pair [simp]: "EX x y. z = (x, y)"
   245   apply (rule exI)
   246   apply (rule exI)
   247   apply (rule surjective_pairing)
   248   done
   249 
   250 lemma split_paired_all: "(!!x. PROP P x) == (!!a b. PROP P (a, b))"
   251 proof
   252   fix a b
   253   assume "!!x. PROP P x"
   254   thus "PROP P (a, b)" .
   255 next
   256   fix x
   257   assume "!!a b. PROP P (a, b)"
   258   hence "PROP P (fst x, snd x)" .
   259   thus "PROP P x" by simp
   260 qed
   261 
   262 lemmas split_tupled_all = split_paired_all unit_all_eq2
   263 
   264 text {*
   265   The rule @{thm [source] split_paired_all} does not work with the
   266   Simplifier because it also affects premises in congrence rules,
   267   where this can lead to premises of the form @{text "!!a b. ... =
   268   ?P(a, b)"} which cannot be solved by reflexivity.
   269 *}
   270 
   271 ML_setup "
   272   (* replace parameters of product type by individual component parameters *)
   273   val safe_full_simp_tac = generic_simp_tac true (true, false, false);
   274   local (* filtering with exists_paired_all is an essential optimization *)
   275     fun exists_paired_all (Const (\"all\", _) $ Abs (_, T, t)) =
   276           can HOLogic.dest_prodT T orelse exists_paired_all t
   277       | exists_paired_all (t $ u) = exists_paired_all t orelse exists_paired_all u
   278       | exists_paired_all (Abs (_, _, t)) = exists_paired_all t
   279       | exists_paired_all _ = false;
   280     val ss = HOL_basic_ss
   281       addsimps [thm \"split_paired_all\", thm \"unit_all_eq2\", thm \"unit_abs_eta_conv\"]
   282       addsimprocs [unit_eq_proc];
   283   in
   284     val split_all_tac = SUBGOAL (fn (t, i) =>
   285       if exists_paired_all t then safe_full_simp_tac ss i else no_tac);
   286     val unsafe_split_all_tac = SUBGOAL (fn (t, i) =>
   287       if exists_paired_all t then full_simp_tac ss i else no_tac);
   288     fun split_all th =
   289    if exists_paired_all (#prop (Thm.rep_thm th)) then full_simplify ss th else th;
   290   end;
   291 
   292 claset_ref() := claset() addSbefore (\"split_all_tac\", split_all_tac);
   293 "
   294 
   295 lemma split_paired_All [simp]: "(ALL x. P x) = (ALL a b. P (a, b))"
   296   -- {* @{text "[iff]"} is not a good idea because it makes @{text blast} loop *}
   297   by fast
   298 
   299 lemma curry_split [simp]: "curry (split f) = f"
   300   by (simp add: curry_def split_def)
   301 
   302 lemma split_curry [simp]: "split (curry f) = f"
   303   by (simp add: curry_def split_def)
   304 
   305 lemma curryI [intro!]: "f (a,b) ==> curry f a b"
   306   by (simp add: curry_def)
   307 
   308 lemma curryD [dest!]: "curry f a b ==> f (a,b)"
   309   by (simp add: curry_def)
   310 
   311 lemma curryE: "[| curry f a b ; f (a,b) ==> Q |] ==> Q"
   312   by (simp add: curry_def)
   313 
   314 lemma curry_conv [simp]: "curry f a b = f (a,b)"
   315   by (simp add: curry_def)
   316 
   317 lemma prod_induct [induct type: *]: "!!x. (!!a b. P (a, b)) ==> P x"
   318   by fast
   319 
   320 lemma split_paired_Ex [simp]: "(EX x. P x) = (EX a b. P (a, b))"
   321   by fast
   322 
   323 lemma split_conv [simp]: "split c (a, b) = c a b"
   324   by (simp add: split_def)
   325 
   326 lemmas split = split_conv  -- {* for backwards compatibility *}
   327 
   328 lemmas splitI = split_conv [THEN iffD2, standard]
   329 lemmas splitD = split_conv [THEN iffD1, standard]
   330 
   331 lemma split_Pair_apply: "split (%x y. f (x, y)) = f"
   332   -- {* Subsumes the old @{text split_Pair} when @{term f} is the identity function. *}
   333   apply (rule ext)
   334   apply (tactic {* pair_tac "x" 1 *}, simp)
   335   done
   336 
   337 lemma split_paired_The: "(THE x. P x) = (THE (a, b). P (a, b))"
   338   -- {* Can't be added to simpset: loops! *}
   339   by (simp add: split_Pair_apply)
   340 
   341 lemma The_split: "The (split P) = (THE xy. P (fst xy) (snd xy))"
   342   by (simp add: split_def)
   343 
   344 lemma Pair_fst_snd_eq: "!!s t. (s = t) = (fst s = fst t & snd s = snd t)"
   345 by (simp only: split_tupled_all, simp)
   346 
   347 lemma prod_eqI [intro?]: "fst p = fst q ==> snd p = snd q ==> p = q"
   348   by (simp add: Pair_fst_snd_eq)
   349 
   350 lemma split_weak_cong: "p = q ==> split c p = split c q"
   351   -- {* Prevents simplification of @{term c}: much faster *}
   352   by (erule arg_cong)
   353 
   354 lemma split_eta: "(%(x, y). f (x, y)) = f"
   355   apply (rule ext)
   356   apply (simp only: split_tupled_all)
   357   apply (rule split_conv)
   358   done
   359 
   360 lemma cond_split_eta: "(!!x y. f x y = g (x, y)) ==> (%(x, y). f x y) = g"
   361   by (simp add: split_eta)
   362 
   363 text {*
   364   Simplification procedure for @{thm [source] cond_split_eta}.  Using
   365   @{thm [source] split_eta} as a rewrite rule is not general enough,
   366   and using @{thm [source] cond_split_eta} directly would render some
   367   existing proofs very inefficient; similarly for @{text
   368   split_beta}. *}
   369 
   370 ML_setup {*
   371 
   372 local
   373   val cond_split_eta = thm "cond_split_eta";
   374   fun  Pair_pat k 0 (Bound m) = (m = k)
   375   |    Pair_pat k i (Const ("Pair",  _) $ Bound m $ t) = i > 0 andalso
   376                         m = k+i andalso Pair_pat k (i-1) t
   377   |    Pair_pat _ _ _ = false;
   378   fun no_args k i (Abs (_, _, t)) = no_args (k+1) i t
   379   |   no_args k i (t $ u) = no_args k i t andalso no_args k i u
   380   |   no_args k i (Bound m) = m < k orelse m > k+i
   381   |   no_args _ _ _ = true;
   382   fun split_pat tp i (Abs  (_,_,t)) = if tp 0 i t then Some (i,t) else None
   383   |   split_pat tp i (Const ("split", _) $ Abs (_, _, t)) = split_pat tp (i+1) t
   384   |   split_pat tp i _ = None;
   385   fun metaeq sg lhs rhs = mk_meta_eq (Tactic.prove sg [] []
   386         (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs,rhs)))
   387         (K (simp_tac (HOL_basic_ss addsimps [cond_split_eta]) 1)));
   388 
   389   fun beta_term_pat k i (Abs (_, _, t)) = beta_term_pat (k+1) i t
   390   |   beta_term_pat k i (t $ u) = Pair_pat k i (t $ u) orelse
   391                         (beta_term_pat k i t andalso beta_term_pat k i u)
   392   |   beta_term_pat k i t = no_args k i t;
   393   fun  eta_term_pat k i (f $ arg) = no_args k i f andalso Pair_pat k i arg
   394   |    eta_term_pat _ _ _ = false;
   395   fun subst arg k i (Abs (x, T, t)) = Abs (x, T, subst arg (k+1) i t)
   396   |   subst arg k i (t $ u) = if Pair_pat k i (t $ u) then incr_boundvars k arg
   397                               else (subst arg k i t $ subst arg k i u)
   398   |   subst arg k i t = t;
   399   fun beta_proc sg _ (s as Const ("split", _) $ Abs (_, _, t) $ arg) =
   400         (case split_pat beta_term_pat 1 t of
   401         Some (i,f) => Some (metaeq sg s (subst arg 0 i f))
   402         | None => None)
   403   |   beta_proc _ _ _ = None;
   404   fun eta_proc sg _ (s as Const ("split", _) $ Abs (_, _, t)) =
   405         (case split_pat eta_term_pat 1 t of
   406           Some (_,ft) => Some (metaeq sg s (let val (f $ arg) = ft in f end))
   407         | None => None)
   408   |   eta_proc _ _ _ = None;
   409 in
   410   val split_beta_proc = Simplifier.simproc (Theory.sign_of (the_context ()))
   411     "split_beta" ["split f z"] beta_proc;
   412   val split_eta_proc = Simplifier.simproc (Theory.sign_of (the_context ()))
   413     "split_eta" ["split f"] eta_proc;
   414 end;
   415 
   416 Addsimprocs [split_beta_proc, split_eta_proc];
   417 *}
   418 
   419 lemma split_beta: "(%(x, y). P x y) z = P (fst z) (snd z)"
   420   by (subst surjective_pairing, rule split_conv)
   421 
   422 lemma split_split: "R (split c p) = (ALL x y. p = (x, y) --> R (c x y))"
   423   -- {* For use with @{text split} and the Simplifier. *}
   424   apply (subst surjective_pairing)
   425   apply (subst split_conv, blast)
   426   done
   427 
   428 text {*
   429   @{thm [source] split_split} could be declared as @{text "[split]"}
   430   done after the Splitter has been speeded up significantly;
   431   precompute the constants involved and don't do anything unless the
   432   current goal contains one of those constants.
   433 *}
   434 
   435 lemma split_split_asm: "R (split c p) = (~(EX x y. p = (x, y) & (~R (c x y))))"
   436 by (subst split_split, simp)
   437 
   438 
   439 text {*
   440   \medskip @{term split} used as a logical connective or set former.
   441 
   442   \medskip These rules are for use with @{text blast}; could instead
   443   call @{text simp} using @{thm [source] split} as rewrite. *}
   444 
   445 lemma splitI2: "!!p. [| !!a b. p = (a, b) ==> c a b |] ==> split c p"
   446   apply (simp only: split_tupled_all)
   447   apply (simp (no_asm_simp))
   448   done
   449 
   450 lemma splitI2': "!!p. [| !!a b. (a, b) = p ==> c a b x |] ==> split c p x"
   451   apply (simp only: split_tupled_all)
   452   apply (simp (no_asm_simp))
   453   done
   454 
   455 lemma splitE: "split c p ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q"
   456   by (induct p) (auto simp add: split_def)
   457 
   458 lemma splitE': "split c p z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q"
   459   by (induct p) (auto simp add: split_def)
   460 
   461 lemma splitE2:
   462   "[| Q (split P z);  !!x y. [|z = (x, y); Q (P x y)|] ==> R |] ==> R"
   463 proof -
   464   assume q: "Q (split P z)"
   465   assume r: "!!x y. [|z = (x, y); Q (P x y)|] ==> R"
   466   show R
   467     apply (rule r surjective_pairing)+
   468     apply (rule split_beta [THEN subst], rule q)
   469     done
   470 qed
   471 
   472 lemma splitD': "split R (a,b) c ==> R a b c"
   473   by simp
   474 
   475 lemma mem_splitI: "z: c a b ==> z: split c (a, b)"
   476   by simp
   477 
   478 lemma mem_splitI2: "!!p. [| !!a b. p = (a, b) ==> z: c a b |] ==> z: split c p"
   479 by (simp only: split_tupled_all, simp)
   480 
   481 lemma mem_splitE: "[| z: split c p; !!x y. [| p = (x,y); z: c x y |] ==> Q |] ==> Q"
   482 proof -
   483   case rule_context [unfolded split_def]
   484   show ?thesis by (rule rule_context surjective_pairing)+
   485 qed
   486 
   487 declare mem_splitI2 [intro!] mem_splitI [intro!] splitI2' [intro!] splitI2 [intro!] splitI [intro!]
   488 declare mem_splitE [elim!] splitE' [elim!] splitE [elim!]
   489 
   490 ML_setup "
   491 local (* filtering with exists_p_split is an essential optimization *)
   492   fun exists_p_split (Const (\"split\",_) $ _ $ (Const (\"Pair\",_)$_$_)) = true
   493     | exists_p_split (t $ u) = exists_p_split t orelse exists_p_split u
   494     | exists_p_split (Abs (_, _, t)) = exists_p_split t
   495     | exists_p_split _ = false;
   496   val ss = HOL_basic_ss addsimps [thm \"split_conv\"];
   497 in
   498 val split_conv_tac = SUBGOAL (fn (t, i) =>
   499     if exists_p_split t then safe_full_simp_tac ss i else no_tac);
   500 end;
   501 (* This prevents applications of splitE for already splitted arguments leading
   502    to quite time-consuming computations (in particular for nested tuples) *)
   503 claset_ref() := claset() addSbefore (\"split_conv_tac\", split_conv_tac);
   504 "
   505 
   506 lemma split_eta_SetCompr [simp]: "(%u. EX x y. u = (x, y) & P (x, y)) = P"
   507 by (rule ext, fast)
   508 
   509 lemma split_eta_SetCompr2 [simp]: "(%u. EX x y. u = (x, y) & P x y) = split P"
   510 by (rule ext, fast)
   511 
   512 lemma split_part [simp]: "(%(a,b). P & Q a b) = (%ab. P & split Q ab)"
   513   -- {* Allows simplifications of nested splits in case of independent predicates. *}
   514   apply (rule ext, blast)
   515   done
   516 
   517 (* Do NOT make this a simp rule as it
   518    a) only helps in special situations
   519    b) can lead to nontermination in the presence of split_def
   520 *)
   521 lemma split_comp_eq: 
   522 "(%u. f (g (fst u)) (snd u)) = (split (%x. f (g x)))"
   523 by (rule ext, auto)
   524 
   525 lemma The_split_eq [simp]: "(THE (x',y'). x = x' & y = y') = (x, y)"
   526   by blast
   527 
   528 (*
   529 the following  would be slightly more general,
   530 but cannot be used as rewrite rule:
   531 ### Cannot add premise as rewrite rule because it contains (type) unknowns:
   532 ### ?y = .x
   533 Goal "[| P y; !!x. P x ==> x = y |] ==> (@(x',y). x = x' & P y) = (x,y)"
   534 by (rtac some_equality 1)
   535 by ( Simp_tac 1)
   536 by (split_all_tac 1)
   537 by (Asm_full_simp_tac 1)
   538 qed "The_split_eq";
   539 *)
   540 
   541 lemma injective_fst_snd: "!!x y. [|fst x = fst y; snd x = snd y|] ==> x = y"
   542   by auto
   543 
   544 
   545 text {*
   546   \bigskip @{term prod_fun} --- action of the product functor upon
   547   functions.
   548 *}
   549 
   550 lemma prod_fun [simp]: "prod_fun f g (a, b) = (f a, g b)"
   551   by (simp add: prod_fun_def)
   552 
   553 lemma prod_fun_compose: "prod_fun (f1 o f2) (g1 o g2) = (prod_fun f1 g1 o prod_fun f2 g2)"
   554   apply (rule ext)
   555   apply (tactic {* pair_tac "x" 1 *}, simp)
   556   done
   557 
   558 lemma prod_fun_ident [simp]: "prod_fun (%x. x) (%y. y) = (%z. z)"
   559   apply (rule ext)
   560   apply (tactic {* pair_tac "z" 1 *}, simp)
   561   done
   562 
   563 lemma prod_fun_imageI [intro]: "(a, b) : r ==> (f a, g b) : prod_fun f g ` r"
   564   apply (rule image_eqI)
   565   apply (rule prod_fun [symmetric], assumption)
   566   done
   567 
   568 lemma prod_fun_imageE [elim!]:
   569   "[| c: (prod_fun f g)`r;  !!x y. [| c=(f(x),g(y));  (x,y):r |] ==> P
   570     |] ==> P"
   571 proof -
   572   case rule_context
   573   assume major: "c: (prod_fun f g)`r"
   574   show ?thesis
   575     apply (rule major [THEN imageE])
   576     apply (rule_tac p = x in PairE)
   577     apply (rule rule_context)
   578      prefer 2
   579      apply blast
   580     apply (blast intro: prod_fun)
   581     done
   582 qed
   583 
   584 
   585 constdefs
   586   upd_fst :: "('a => 'c) => 'a * 'b => 'c * 'b"
   587  "upd_fst f == prod_fun f id"
   588 
   589   upd_snd :: "('b => 'c) => 'a * 'b => 'a * 'c"
   590  "upd_snd f == prod_fun id f"
   591 
   592 lemma upd_fst_conv [simp]: "upd_fst f (x,y) = (f x,y)" 
   593 by (simp add: upd_fst_def)
   594 
   595 lemma upd_snd_conv [simp]: "upd_snd f (x,y) = (x,f y)" 
   596 by (simp add: upd_snd_def)
   597 
   598 text {*
   599   \bigskip Disjoint union of a family of sets -- Sigma.
   600 *}
   601 
   602 lemma SigmaI [intro!]: "[| a:A;  b:B(a) |] ==> (a,b) : Sigma A B"
   603   by (unfold Sigma_def) blast
   604 
   605 lemma SigmaE [elim!]:
   606     "[| c: Sigma A B;
   607         !!x y.[| x:A;  y:B(x);  c=(x,y) |] ==> P
   608      |] ==> P"
   609   -- {* The general elimination rule. *}
   610   by (unfold Sigma_def) blast
   611 
   612 text {*
   613   Elimination of @{term "(a, b) : A \<times> B"} -- introduces no
   614   eigenvariables.
   615 *}
   616 
   617 lemma SigmaD1: "(a, b) : Sigma A B ==> a : A"
   618 by blast
   619 
   620 lemma SigmaD2: "(a, b) : Sigma A B ==> b : B a"
   621 by blast
   622 
   623 lemma SigmaE2:
   624     "[| (a, b) : Sigma A B;
   625         [| a:A;  b:B(a) |] ==> P
   626      |] ==> P"
   627   by blast
   628 
   629 lemma Sigma_cong:
   630      "\<lbrakk>A = B; !!x. x \<in> B \<Longrightarrow> C x = D x\<rbrakk>
   631       \<Longrightarrow> (SIGMA x: A. C x) = (SIGMA x: B. D x)"
   632 by auto
   633 
   634 lemma Sigma_mono: "[| A <= C; !!x. x:A ==> B x <= D x |] ==> Sigma A B <= Sigma C D"
   635   by blast
   636 
   637 lemma Sigma_empty1 [simp]: "Sigma {} B = {}"
   638   by blast
   639 
   640 lemma Sigma_empty2 [simp]: "A <*> {} = {}"
   641   by blast
   642 
   643 lemma UNIV_Times_UNIV [simp]: "UNIV <*> UNIV = UNIV"
   644   by auto
   645 
   646 lemma Compl_Times_UNIV1 [simp]: "- (UNIV <*> A) = UNIV <*> (-A)"
   647   by auto
   648 
   649 lemma Compl_Times_UNIV2 [simp]: "- (A <*> UNIV) = (-A) <*> UNIV"
   650   by auto
   651 
   652 lemma mem_Sigma_iff [iff]: "((a,b): Sigma A B) = (a:A & b:B(a))"
   653   by blast
   654 
   655 lemma Times_subset_cancel2: "x:C ==> (A <*> C <= B <*> C) = (A <= B)"
   656   by blast
   657 
   658 lemma Times_eq_cancel2: "x:C ==> (A <*> C = B <*> C) = (A = B)"
   659   by (blast elim: equalityE)
   660 
   661 lemma SetCompr_Sigma_eq:
   662     "Collect (split (%x y. P x & Q x y)) = (SIGMA x:Collect P. Collect (Q x))"
   663   by blast
   664 
   665 text {*
   666   \bigskip Complex rules for Sigma.
   667 *}
   668 
   669 lemma Collect_split [simp]: "{(a,b). P a & Q b} = Collect P <*> Collect Q"
   670   by blast
   671 
   672 lemma UN_Times_distrib:
   673   "(UN (a,b):(A <*> B). E a <*> F b) = (UNION A E) <*> (UNION B F)"
   674   -- {* Suggested by Pierre Chartier *}
   675   by blast
   676 
   677 lemma split_paired_Ball_Sigma [simp]:
   678     "(ALL z: Sigma A B. P z) = (ALL x:A. ALL y: B x. P(x,y))"
   679   by blast
   680 
   681 lemma split_paired_Bex_Sigma [simp]:
   682     "(EX z: Sigma A B. P z) = (EX x:A. EX y: B x. P(x,y))"
   683   by blast
   684 
   685 lemma Sigma_Un_distrib1: "(SIGMA i:I Un J. C(i)) = (SIGMA i:I. C(i)) Un (SIGMA j:J. C(j))"
   686   by blast
   687 
   688 lemma Sigma_Un_distrib2: "(SIGMA i:I. A(i) Un B(i)) = (SIGMA i:I. A(i)) Un (SIGMA i:I. B(i))"
   689   by blast
   690 
   691 lemma Sigma_Int_distrib1: "(SIGMA i:I Int J. C(i)) = (SIGMA i:I. C(i)) Int (SIGMA j:J. C(j))"
   692   by blast
   693 
   694 lemma Sigma_Int_distrib2: "(SIGMA i:I. A(i) Int B(i)) = (SIGMA i:I. A(i)) Int (SIGMA i:I. B(i))"
   695   by blast
   696 
   697 lemma Sigma_Diff_distrib1: "(SIGMA i:I - J. C(i)) = (SIGMA i:I. C(i)) - (SIGMA j:J. C(j))"
   698   by blast
   699 
   700 lemma Sigma_Diff_distrib2: "(SIGMA i:I. A(i) - B(i)) = (SIGMA i:I. A(i)) - (SIGMA i:I. B(i))"
   701   by blast
   702 
   703 lemma Sigma_Union: "Sigma (Union X) B = (UN A:X. Sigma A B)"
   704   by blast
   705 
   706 text {*
   707   Non-dependent versions are needed to avoid the need for higher-order
   708   matching, especially when the rules are re-oriented.
   709 *}
   710 
   711 lemma Times_Un_distrib1: "(A Un B) <*> C = (A <*> C) Un (B <*> C)"
   712   by blast
   713 
   714 lemma Times_Int_distrib1: "(A Int B) <*> C = (A <*> C) Int (B <*> C)"
   715   by blast
   716 
   717 lemma Times_Diff_distrib1: "(A - B) <*> C = (A <*> C) - (B <*> C)"
   718   by blast
   719 
   720 
   721 lemma pair_imageI [intro]: "(a, b) : A ==> f a b : (%(a, b). f a b) ` A"
   722   apply (rule_tac x = "(a, b)" in image_eqI)
   723    apply auto
   724   done
   725 
   726 
   727 text {*
   728   Setup of internal @{text split_rule}.
   729 *}
   730 
   731 constdefs
   732   internal_split :: "('a => 'b => 'c) => 'a * 'b => 'c"
   733   "internal_split == split"
   734 
   735 lemma internal_split_conv: "internal_split c (a, b) = c a b"
   736   by (simp only: internal_split_def split_conv)
   737 
   738 hide const internal_split
   739 
   740 use "Tools/split_rule.ML"
   741 setup SplitRule.setup
   742 
   743 end