src/HOL/Product_Type.thy
author haftmann
Tue Sep 19 15:21:42 2006 +0200 (2006-09-19)
changeset 20588 c847c56edf0c
parent 20415 e3d2d7b01279
child 21046 fe1db2f991a7
permissions -rw-r--r--
added operational equality
     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
    10 imports Fun
    11 uses ("Tools/split_rule.ML")
    12 begin
    13 
    14 subsection {* Unit *}
    15 
    16 typedef unit = "{True}"
    17 proof
    18   show "True : ?unit" ..
    19 qed
    20 
    21 constdefs
    22   Unity :: unit    ("'(')")
    23   "() == Abs_unit True"
    24 
    25 lemma unit_eq: "u = ()"
    26   by (induct u) (simp add: unit_def Unity_def)
    27 
    28 text {*
    29   Simplification procedure for @{thm [source] unit_eq}.  Cannot use
    30   this rule directly --- it loops!
    31 *}
    32 
    33 ML_setup {*
    34   val unit_eq_proc =
    35     let val unit_meta_eq = mk_meta_eq (thm "unit_eq") in
    36       Simplifier.simproc (Theory.sign_of (the_context ())) "unit_eq" ["x::unit"]
    37       (fn _ => fn _ => fn t => if HOLogic.is_unit t then NONE else SOME unit_meta_eq)
    38     end;
    39 
    40   Addsimprocs [unit_eq_proc];
    41 *}
    42 
    43 lemma unit_all_eq1: "(!!x::unit. PROP P x) == PROP P ()"
    44   by simp
    45 
    46 lemma unit_all_eq2: "(!!x::unit. PROP P) == PROP P"
    47   by (rule triv_forall_equality)
    48 
    49 lemma unit_induct [induct type: unit]: "P () ==> P x"
    50   by simp
    51 
    52 text {*
    53   This rewrite counters the effect of @{text unit_eq_proc} on @{term
    54   [source] "%u::unit. f u"}, replacing it by @{term [source]
    55   f} rather than by @{term [source] "%u. f ()"}.
    56 *}
    57 
    58 lemma unit_abs_eta_conv [simp]: "(%u::unit. f ()) = f"
    59   by (rule ext) simp
    60 
    61 
    62 subsection {* Pairs *}
    63 
    64 subsubsection {* Type definition *}
    65 
    66 constdefs
    67   Pair_Rep :: "['a, 'b] => ['a, 'b] => bool"
    68   "Pair_Rep == (%a b. %x y. x=a & y=b)"
    69 
    70 global
    71 
    72 typedef (Prod)
    73   ('a, 'b) "*"    (infixr 20)
    74     = "{f. EX a b. f = Pair_Rep (a::'a) (b::'b)}"
    75 proof
    76   fix a b show "Pair_Rep a b : ?Prod"
    77     by blast
    78 qed
    79 
    80 syntax (xsymbols)
    81   "*"      :: "[type, type] => type"         ("(_ \<times>/ _)" [21, 20] 20)
    82 syntax (HTML output)
    83   "*"      :: "[type, type] => type"         ("(_ \<times>/ _)" [21, 20] 20)
    84 
    85 local
    86 
    87 
    88 subsubsection {* Definitions *}
    89 
    90 global
    91 
    92 consts
    93   fst      :: "'a * 'b => 'a"
    94   snd      :: "'a * 'b => 'b"
    95   split    :: "[['a, 'b] => 'c, 'a * 'b] => 'c"
    96   curry    :: "['a * 'b => 'c, 'a, 'b] => 'c"
    97   prod_fun :: "['a => 'b, 'c => 'd, 'a * 'c] => 'b * 'd"
    98   Pair     :: "['a, 'b] => 'a * 'b"
    99   Sigma    :: "['a set, 'a => 'b set] => ('a * 'b) set"
   100 
   101 local
   102 
   103 defs
   104   Pair_def:     "Pair a b == Abs_Prod (Pair_Rep a b)"
   105   fst_def:      "fst p == THE a. EX b. p = Pair a b"
   106   snd_def:      "snd p == THE b. EX a. p = Pair a b"
   107   split_def:    "split == (%c p. c (fst p) (snd p))"
   108   curry_def:    "curry == (%c x y. c (Pair x y))"
   109   prod_fun_def: "prod_fun f g == split (%x y. Pair (f x) (g y))"
   110   Sigma_def:    "Sigma A B == UN x:A. UN y:B x. {Pair x y}"
   111 
   112 abbreviation
   113   Times :: "['a set, 'b set] => ('a * 'b) set"  (infixr "<*>" 80)
   114   "A <*> B == Sigma A (%_. B)"
   115 
   116 const_syntax (xsymbols)
   117   Times  (infixr "\<times>" 80)
   118 
   119 const_syntax (HTML output)
   120   Times  (infixr "\<times>" 80)
   121 
   122 
   123 subsubsection {* Concrete syntax *}
   124 
   125 text {*
   126   Patterns -- extends pre-defined type @{typ pttrn} used in
   127   abstractions.
   128 *}
   129 
   130 nonterminals
   131   tuple_args patterns
   132 
   133 syntax
   134   "_tuple"      :: "'a => tuple_args => 'a * 'b"        ("(1'(_,/ _'))")
   135   "_tuple_arg"  :: "'a => tuple_args"                   ("_")
   136   "_tuple_args" :: "'a => tuple_args => tuple_args"     ("_,/ _")
   137   "_pattern"    :: "[pttrn, patterns] => pttrn"         ("'(_,/ _')")
   138   ""            :: "pttrn => patterns"                  ("_")
   139   "_patterns"   :: "[pttrn, patterns] => patterns"      ("_,/ _")
   140   "@Sigma" ::"[pttrn, 'a set, 'b set] => ('a * 'b) set" ("(3SIGMA _:_./ _)" 10)
   141 
   142 translations
   143   "(x, y)"       == "Pair x y"
   144   "_tuple x (_tuple_args y z)" == "_tuple x (_tuple_arg (_tuple y z))"
   145   "%(x,y,zs).b"  == "split(%x (y,zs).b)"
   146   "%(x,y).b"     == "split(%x y. b)"
   147   "_abs (Pair x y) t" => "%(x,y).t"
   148   (* The last rule accommodates tuples in `case C ... (x,y) ... => ...'
   149      The (x,y) is parsed as `Pair x y' because it is logic, not pttrn *)
   150   "SIGMA x:A. B" == "Sigma A (%x. B)"
   151 
   152 (* reconstructs pattern from (nested) splits, avoiding eta-contraction of body*)
   153 (* works best with enclosing "let", if "let" does not avoid eta-contraction   *)
   154 print_translation {*
   155 let fun split_tr' [Abs (x,T,t as (Abs abs))] =
   156       (* split (%x y. t) => %(x,y) t *)
   157       let val (y,t') = atomic_abs_tr' abs;
   158           val (x',t'') = atomic_abs_tr' (x,T,t');
   159     
   160       in Syntax.const "_abs" $ (Syntax.const "_pattern" $x'$y) $ t'' end
   161     | split_tr' [Abs (x,T,(s as Const ("split",_)$t))] =
   162        (* split (%x. (split (%y z. t))) => %(x,y,z). t *)
   163        let val (Const ("_abs",_)$(Const ("_pattern",_)$y$z)$t') = split_tr' [t];
   164            val (x',t'') = atomic_abs_tr' (x,T,t');
   165        in Syntax.const "_abs"$ 
   166            (Syntax.const "_pattern"$x'$(Syntax.const "_patterns"$y$z))$t'' end
   167     | split_tr' [Const ("split",_)$t] =
   168        (* split (split (%x y z. t)) => %((x,y),z). t *)   
   169        split_tr' [(split_tr' [t])] (* inner split_tr' creates next pattern *)
   170     | split_tr' [Const ("_abs",_)$x_y$(Abs abs)] =
   171        (* split (%pttrn z. t) => %(pttrn,z). t *)
   172        let val (z,t) = atomic_abs_tr' abs;
   173        in Syntax.const "_abs" $ (Syntax.const "_pattern" $x_y$z) $ t end
   174     | split_tr' _ =  raise Match;
   175 in [("split", split_tr')]
   176 end
   177 *}
   178 
   179 (* print "split f" as "\<lambda>(x,y). f x y" and "split (\<lambda>x. f x)" as "\<lambda>(x,y). f x y" *) 
   180 typed_print_translation {*
   181 let
   182   fun split_guess_names_tr' _ T [Abs (x,_,Abs _)] = raise Match
   183     | split_guess_names_tr' _ T  [Abs (x,xT,t)] =
   184         (case (head_of t) of
   185            Const ("split",_) => raise Match
   186          | _ => let 
   187                   val (_::yT::_) = binder_types (domain_type T) handle Bind => raise Match;
   188                   val (y,t') = atomic_abs_tr' ("y",yT,(incr_boundvars 1 t)$Bound 0); 
   189                   val (x',t'') = atomic_abs_tr' (x,xT,t');
   190                 in Syntax.const "_abs" $ (Syntax.const "_pattern" $x'$y) $ t'' end)
   191     | split_guess_names_tr' _ T [t] =
   192        (case (head_of t) of
   193            Const ("split",_) => raise Match 
   194          | _ => let 
   195                   val (xT::yT::_) = binder_types (domain_type T) handle Bind => raise Match;
   196                   val (y,t') = 
   197                         atomic_abs_tr' ("y",yT,(incr_boundvars 2 t)$Bound 1$Bound 0); 
   198                   val (x',t'') = atomic_abs_tr' ("x",xT,t');
   199                 in Syntax.const "_abs" $ (Syntax.const "_pattern" $x'$y) $ t'' end)
   200     | split_guess_names_tr' _ _ _ = raise Match;
   201 in [("split", split_guess_names_tr')]
   202 end 
   203 *}
   204 
   205 
   206 subsubsection {* Lemmas and proof tool setup *}
   207 
   208 lemma ProdI: "Pair_Rep a b : Prod"
   209   unfolding Prod_def by blast
   210 
   211 lemma Pair_Rep_inject: "Pair_Rep a b = Pair_Rep a' b' ==> a = a' & b = b'"
   212   apply (unfold Pair_Rep_def)
   213   apply (drule fun_cong [THEN fun_cong], blast)
   214   done
   215 
   216 lemma inj_on_Abs_Prod: "inj_on Abs_Prod Prod"
   217   apply (rule inj_on_inverseI)
   218   apply (erule Abs_Prod_inverse)
   219   done
   220 
   221 lemma Pair_inject:
   222   assumes "(a, b) = (a', b')"
   223     and "a = a' ==> b = b' ==> R"
   224   shows R
   225   apply (insert prems [unfolded Pair_def])
   226   apply (rule inj_on_Abs_Prod [THEN inj_onD, THEN Pair_Rep_inject, THEN conjE])
   227   apply (assumption | rule ProdI)+
   228   done
   229 
   230 lemma Pair_eq [iff]: "((a, b) = (a', b')) = (a = a' & b = b')"
   231   by (blast elim!: Pair_inject)
   232 
   233 lemma fst_conv [simp]: "fst (a, b) = a"
   234   unfolding fst_def by blast
   235 
   236 lemma snd_conv [simp]: "snd (a, b) = b"
   237   unfolding snd_def by blast
   238 
   239 lemma fst_eqD: "fst (x, y) = a ==> x = a"
   240   by simp
   241 
   242 lemma snd_eqD: "snd (x, y) = a ==> y = a"
   243   by simp
   244 
   245 lemma PairE_lemma: "EX x y. p = (x, y)"
   246   apply (unfold Pair_def)
   247   apply (rule Rep_Prod [unfolded Prod_def, THEN CollectE])
   248   apply (erule exE, erule exE, rule exI, rule exI)
   249   apply (rule Rep_Prod_inverse [symmetric, THEN trans])
   250   apply (erule arg_cong)
   251   done
   252 
   253 lemma PairE [cases type: *]: "(!!x y. p = (x, y) ==> Q) ==> Q"
   254   using PairE_lemma [of p] by blast
   255 
   256 ML {*
   257   local val PairE = thm "PairE" in
   258     fun pair_tac s =
   259       EVERY' [res_inst_tac [("p", s)] PairE, hyp_subst_tac, K prune_params_tac];
   260   end;
   261 *}
   262 
   263 lemma surjective_pairing: "p = (fst p, snd p)"
   264   -- {* Do not add as rewrite rule: invalidates some proofs in IMP *}
   265   by (cases p) simp
   266 
   267 lemmas pair_collapse = surjective_pairing [symmetric]
   268 declare pair_collapse [simp]
   269 
   270 lemma surj_pair [simp]: "EX x y. z = (x, y)"
   271   apply (rule exI)
   272   apply (rule exI)
   273   apply (rule surjective_pairing)
   274   done
   275 
   276 lemma split_paired_all: "(!!x. PROP P x) == (!!a b. PROP P (a, b))"
   277 proof
   278   fix a b
   279   assume "!!x. PROP P x"
   280   then show "PROP P (a, b)" .
   281 next
   282   fix x
   283   assume "!!a b. PROP P (a, b)"
   284   from `PROP P (fst x, snd x)` show "PROP P x" by simp
   285 qed
   286 
   287 lemmas split_tupled_all = split_paired_all unit_all_eq2
   288 
   289 text {*
   290   The rule @{thm [source] split_paired_all} does not work with the
   291   Simplifier because it also affects premises in congrence rules,
   292   where this can lead to premises of the form @{text "!!a b. ... =
   293   ?P(a, b)"} which cannot be solved by reflexivity.
   294 *}
   295 
   296 ML_setup {*
   297   (* replace parameters of product type by individual component parameters *)
   298   val safe_full_simp_tac = generic_simp_tac true (true, false, false);
   299   local (* filtering with exists_paired_all is an essential optimization *)
   300     fun exists_paired_all (Const ("all", _) $ Abs (_, T, t)) =
   301           can HOLogic.dest_prodT T orelse exists_paired_all t
   302       | exists_paired_all (t $ u) = exists_paired_all t orelse exists_paired_all u
   303       | exists_paired_all (Abs (_, _, t)) = exists_paired_all t
   304       | exists_paired_all _ = false;
   305     val ss = HOL_basic_ss
   306       addsimps [thm "split_paired_all", thm "unit_all_eq2", thm "unit_abs_eta_conv"]
   307       addsimprocs [unit_eq_proc];
   308   in
   309     val split_all_tac = SUBGOAL (fn (t, i) =>
   310       if exists_paired_all t then safe_full_simp_tac ss i else no_tac);
   311     val unsafe_split_all_tac = SUBGOAL (fn (t, i) =>
   312       if exists_paired_all t then full_simp_tac ss i else no_tac);
   313     fun split_all th =
   314    if exists_paired_all (#prop (Thm.rep_thm th)) then full_simplify ss th else th;
   315   end;
   316 
   317 change_claset (fn cs => cs addSbefore ("split_all_tac", split_all_tac));
   318 *}
   319 
   320 lemma split_paired_All [simp]: "(ALL x. P x) = (ALL a b. P (a, b))"
   321   -- {* @{text "[iff]"} is not a good idea because it makes @{text blast} loop *}
   322   by fast
   323 
   324 lemma curry_split [simp]: "curry (split f) = f"
   325   by (simp add: curry_def split_def)
   326 
   327 lemma split_curry [simp]: "split (curry f) = f"
   328   by (simp add: curry_def split_def)
   329 
   330 lemma curryI [intro!]: "f (a,b) ==> curry f a b"
   331   by (simp add: curry_def)
   332 
   333 lemma curryD [dest!]: "curry f a b ==> f (a,b)"
   334   by (simp add: curry_def)
   335 
   336 lemma curryE: "[| curry f a b ; f (a,b) ==> Q |] ==> Q"
   337   by (simp add: curry_def)
   338 
   339 lemma curry_conv [simp]: "curry f a b = f (a,b)"
   340   by (simp add: curry_def)
   341 
   342 lemma prod_induct [induct type: *]: "!!x. (!!a b. P (a, b)) ==> P x"
   343   by fast
   344 
   345 lemma split_paired_Ex [simp]: "(EX x. P x) = (EX a b. P (a, b))"
   346   by fast
   347 
   348 lemma split_conv [simp]: "split c (a, b) = c a b"
   349   by (simp add: split_def)
   350 
   351 lemmas split = split_conv  -- {* for backwards compatibility *}
   352 
   353 lemmas splitI = split_conv [THEN iffD2, standard]
   354 lemmas splitD = split_conv [THEN iffD1, standard]
   355 
   356 lemma split_Pair_apply: "split (%x y. f (x, y)) = f"
   357   -- {* Subsumes the old @{text split_Pair} when @{term f} is the identity function. *}
   358   apply (rule ext)
   359   apply (tactic {* pair_tac "x" 1 *}, simp)
   360   done
   361 
   362 lemma split_paired_The: "(THE x. P x) = (THE (a, b). P (a, b))"
   363   -- {* Can't be added to simpset: loops! *}
   364   by (simp add: split_Pair_apply)
   365 
   366 lemma The_split: "The (split P) = (THE xy. P (fst xy) (snd xy))"
   367   by (simp add: split_def)
   368 
   369 lemma Pair_fst_snd_eq: "!!s t. (s = t) = (fst s = fst t & snd s = snd t)"
   370 by (simp only: split_tupled_all, simp)
   371 
   372 lemma prod_eqI [intro?]: "fst p = fst q ==> snd p = snd q ==> p = q"
   373   by (simp add: Pair_fst_snd_eq)
   374 
   375 lemma split_weak_cong: "p = q ==> split c p = split c q"
   376   -- {* Prevents simplification of @{term c}: much faster *}
   377   by (erule arg_cong)
   378 
   379 lemma split_eta: "(%(x, y). f (x, y)) = f"
   380   apply (rule ext)
   381   apply (simp only: split_tupled_all)
   382   apply (rule split_conv)
   383   done
   384 
   385 lemma cond_split_eta: "(!!x y. f x y = g (x, y)) ==> (%(x, y). f x y) = g"
   386   by (simp add: split_eta)
   387 
   388 text {*
   389   Simplification procedure for @{thm [source] cond_split_eta}.  Using
   390   @{thm [source] split_eta} as a rewrite rule is not general enough,
   391   and using @{thm [source] cond_split_eta} directly would render some
   392   existing proofs very inefficient; similarly for @{text
   393   split_beta}. *}
   394 
   395 ML_setup {*
   396 
   397 local
   398   val cond_split_eta_ss = HOL_basic_ss addsimps [thm "cond_split_eta"]
   399   fun  Pair_pat k 0 (Bound m) = (m = k)
   400   |    Pair_pat k i (Const ("Pair",  _) $ Bound m $ t) = i > 0 andalso
   401                         m = k+i andalso Pair_pat k (i-1) t
   402   |    Pair_pat _ _ _ = false;
   403   fun no_args k i (Abs (_, _, t)) = no_args (k+1) i t
   404   |   no_args k i (t $ u) = no_args k i t andalso no_args k i u
   405   |   no_args k i (Bound m) = m < k orelse m > k+i
   406   |   no_args _ _ _ = true;
   407   fun split_pat tp i (Abs  (_,_,t)) = if tp 0 i t then SOME (i,t) else NONE
   408   |   split_pat tp i (Const ("split", _) $ Abs (_, _, t)) = split_pat tp (i+1) t
   409   |   split_pat tp i _ = NONE;
   410   fun metaeq ss lhs rhs = mk_meta_eq (Goal.prove (Simplifier.the_context ss) [] []
   411         (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs,rhs)))
   412         (K (simp_tac (Simplifier.inherit_context ss cond_split_eta_ss) 1)));
   413 
   414   fun beta_term_pat k i (Abs (_, _, t)) = beta_term_pat (k+1) i t
   415   |   beta_term_pat k i (t $ u) = Pair_pat k i (t $ u) orelse
   416                         (beta_term_pat k i t andalso beta_term_pat k i u)
   417   |   beta_term_pat k i t = no_args k i t;
   418   fun  eta_term_pat k i (f $ arg) = no_args k i f andalso Pair_pat k i arg
   419   |    eta_term_pat _ _ _ = false;
   420   fun subst arg k i (Abs (x, T, t)) = Abs (x, T, subst arg (k+1) i t)
   421   |   subst arg k i (t $ u) = if Pair_pat k i (t $ u) then incr_boundvars k arg
   422                               else (subst arg k i t $ subst arg k i u)
   423   |   subst arg k i t = t;
   424   fun beta_proc ss (s as Const ("split", _) $ Abs (_, _, t) $ arg) =
   425         (case split_pat beta_term_pat 1 t of
   426         SOME (i,f) => SOME (metaeq ss s (subst arg 0 i f))
   427         | NONE => NONE)
   428   |   beta_proc _ _ = NONE;
   429   fun eta_proc ss (s as Const ("split", _) $ Abs (_, _, t)) =
   430         (case split_pat eta_term_pat 1 t of
   431           SOME (_,ft) => SOME (metaeq ss s (let val (f $ arg) = ft in f end))
   432         | NONE => NONE)
   433   |   eta_proc _ _ = NONE;
   434 in
   435   val split_beta_proc = Simplifier.simproc (Theory.sign_of (the_context ()))
   436     "split_beta" ["split f z"] (K beta_proc);
   437   val split_eta_proc = Simplifier.simproc (Theory.sign_of (the_context ()))
   438     "split_eta" ["split f"] (K eta_proc);
   439 end;
   440 
   441 Addsimprocs [split_beta_proc, split_eta_proc];
   442 *}
   443 
   444 lemma split_beta: "(%(x, y). P x y) z = P (fst z) (snd z)"
   445   by (subst surjective_pairing, rule split_conv)
   446 
   447 lemma split_split: "R (split c p) = (ALL x y. p = (x, y) --> R (c x y))"
   448   -- {* For use with @{text split} and the Simplifier. *}
   449   by (insert surj_pair [of p], clarify, simp)
   450 
   451 text {*
   452   @{thm [source] split_split} could be declared as @{text "[split]"}
   453   done after the Splitter has been speeded up significantly;
   454   precompute the constants involved and don't do anything unless the
   455   current goal contains one of those constants.
   456 *}
   457 
   458 lemma split_split_asm: "R (split c p) = (~(EX x y. p = (x, y) & (~R (c x y))))"
   459 by (subst split_split, simp)
   460 
   461 
   462 text {*
   463   \medskip @{term split} used as a logical connective or set former.
   464 
   465   \medskip These rules are for use with @{text blast}; could instead
   466   call @{text simp} using @{thm [source] split} as rewrite. *}
   467 
   468 lemma splitI2: "!!p. [| !!a b. p = (a, b) ==> c a b |] ==> split c p"
   469   apply (simp only: split_tupled_all)
   470   apply (simp (no_asm_simp))
   471   done
   472 
   473 lemma splitI2': "!!p. [| !!a b. (a, b) = p ==> c a b x |] ==> split c p x"
   474   apply (simp only: split_tupled_all)
   475   apply (simp (no_asm_simp))
   476   done
   477 
   478 lemma splitE: "split c p ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q"
   479   by (induct p) (auto simp add: split_def)
   480 
   481 lemma splitE': "split c p z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q"
   482   by (induct p) (auto simp add: split_def)
   483 
   484 lemma splitE2:
   485   "[| Q (split P z);  !!x y. [|z = (x, y); Q (P x y)|] ==> R |] ==> R"
   486 proof -
   487   assume q: "Q (split P z)"
   488   assume r: "!!x y. [|z = (x, y); Q (P x y)|] ==> R"
   489   show R
   490     apply (rule r surjective_pairing)+
   491     apply (rule split_beta [THEN subst], rule q)
   492     done
   493 qed
   494 
   495 lemma splitD': "split R (a,b) c ==> R a b c"
   496   by simp
   497 
   498 lemma mem_splitI: "z: c a b ==> z: split c (a, b)"
   499   by simp
   500 
   501 lemma mem_splitI2: "!!p. [| !!a b. p = (a, b) ==> z: c a b |] ==> z: split c p"
   502 by (simp only: split_tupled_all, simp)
   503 
   504 lemma mem_splitE:
   505   assumes major: "z: split c p"
   506     and cases: "!!x y. [| p = (x,y); z: c x y |] ==> Q"
   507   shows Q
   508   by (rule major [unfolded split_def] cases surjective_pairing)+
   509 
   510 declare mem_splitI2 [intro!] mem_splitI [intro!] splitI2' [intro!] splitI2 [intro!] splitI [intro!]
   511 declare mem_splitE [elim!] splitE' [elim!] splitE [elim!]
   512 
   513 ML_setup {*
   514 local (* filtering with exists_p_split is an essential optimization *)
   515   fun exists_p_split (Const ("split",_) $ _ $ (Const ("Pair",_)$_$_)) = true
   516     | exists_p_split (t $ u) = exists_p_split t orelse exists_p_split u
   517     | exists_p_split (Abs (_, _, t)) = exists_p_split t
   518     | exists_p_split _ = false;
   519   val ss = HOL_basic_ss addsimps [thm "split_conv"];
   520 in
   521 val split_conv_tac = SUBGOAL (fn (t, i) =>
   522     if exists_p_split t then safe_full_simp_tac ss i else no_tac);
   523 end;
   524 (* This prevents applications of splitE for already splitted arguments leading
   525    to quite time-consuming computations (in particular for nested tuples) *)
   526 change_claset (fn cs => cs addSbefore ("split_conv_tac", split_conv_tac));
   527 *}
   528 
   529 lemma split_eta_SetCompr [simp]: "(%u. EX x y. u = (x, y) & P (x, y)) = P"
   530   by (rule ext) fast
   531 
   532 lemma split_eta_SetCompr2 [simp]: "(%u. EX x y. u = (x, y) & P x y) = split P"
   533   by (rule ext) fast
   534 
   535 lemma split_part [simp]: "(%(a,b). P & Q a b) = (%ab. P & split Q ab)"
   536   -- {* Allows simplifications of nested splits in case of independent predicates. *}
   537   by (rule ext) blast
   538 
   539 (* Do NOT make this a simp rule as it
   540    a) only helps in special situations
   541    b) can lead to nontermination in the presence of split_def
   542 *)
   543 lemma split_comp_eq: 
   544   fixes f :: "'a => 'b => 'c" and g :: "'d => 'a"
   545   shows "(%u. f (g (fst u)) (snd u)) = (split (%x. f (g x)))"
   546   by (rule ext) auto
   547 
   548 lemma The_split_eq [simp]: "(THE (x',y'). x = x' & y = y') = (x, y)"
   549   by blast
   550 
   551 (*
   552 the following  would be slightly more general,
   553 but cannot be used as rewrite rule:
   554 ### Cannot add premise as rewrite rule because it contains (type) unknowns:
   555 ### ?y = .x
   556 Goal "[| P y; !!x. P x ==> x = y |] ==> (@(x',y). x = x' & P y) = (x,y)"
   557 by (rtac some_equality 1)
   558 by ( Simp_tac 1)
   559 by (split_all_tac 1)
   560 by (Asm_full_simp_tac 1)
   561 qed "The_split_eq";
   562 *)
   563 
   564 lemma injective_fst_snd: "!!x y. [|fst x = fst y; snd x = snd y|] ==> x = y"
   565   by auto
   566 
   567 
   568 text {*
   569   \bigskip @{term prod_fun} --- action of the product functor upon
   570   functions.
   571 *}
   572 
   573 lemma prod_fun [simp]: "prod_fun f g (a, b) = (f a, g b)"
   574   by (simp add: prod_fun_def)
   575 
   576 lemma prod_fun_compose: "prod_fun (f1 o f2) (g1 o g2) = (prod_fun f1 g1 o prod_fun f2 g2)"
   577   apply (rule ext)
   578   apply (tactic {* pair_tac "x" 1 *}, simp)
   579   done
   580 
   581 lemma prod_fun_ident [simp]: "prod_fun (%x. x) (%y. y) = (%z. z)"
   582   apply (rule ext)
   583   apply (tactic {* pair_tac "z" 1 *}, simp)
   584   done
   585 
   586 lemma prod_fun_imageI [intro]: "(a, b) : r ==> (f a, g b) : prod_fun f g ` r"
   587   apply (rule image_eqI)
   588   apply (rule prod_fun [symmetric], assumption)
   589   done
   590 
   591 lemma prod_fun_imageE [elim!]:
   592   assumes major: "c: (prod_fun f g)`r"
   593     and cases: "!!x y. [| c=(f(x),g(y));  (x,y):r |] ==> P"
   594   shows P
   595   apply (rule major [THEN imageE])
   596   apply (rule_tac p = x in PairE)
   597   apply (rule cases)
   598    apply (blast intro: prod_fun)
   599   apply blast
   600   done
   601 
   602 
   603 constdefs
   604   upd_fst :: "('a => 'c) => 'a * 'b => 'c * 'b"
   605  "upd_fst f == prod_fun f id"
   606 
   607   upd_snd :: "('b => 'c) => 'a * 'b => 'a * 'c"
   608  "upd_snd f == prod_fun id f"
   609 
   610 lemma upd_fst_conv [simp]: "upd_fst f (x,y) = (f x,y)" 
   611   by (simp add: upd_fst_def)
   612 
   613 lemma upd_snd_conv [simp]: "upd_snd f (x,y) = (x,f y)" 
   614   by (simp add: upd_snd_def)
   615 
   616 text {*
   617   \bigskip Disjoint union of a family of sets -- Sigma.
   618 *}
   619 
   620 lemma SigmaI [intro!]: "[| a:A;  b:B(a) |] ==> (a,b) : Sigma A B"
   621   by (unfold Sigma_def) blast
   622 
   623 lemma SigmaE [elim!]:
   624     "[| c: Sigma A B;
   625         !!x y.[| x:A;  y:B(x);  c=(x,y) |] ==> P
   626      |] ==> P"
   627   -- {* The general elimination rule. *}
   628   by (unfold Sigma_def) blast
   629 
   630 text {*
   631   Elimination of @{term "(a, b) : A \<times> B"} -- introduces no
   632   eigenvariables.
   633 *}
   634 
   635 lemma SigmaD1: "(a, b) : Sigma A B ==> a : A"
   636   by blast
   637 
   638 lemma SigmaD2: "(a, b) : Sigma A B ==> b : B a"
   639   by blast
   640 
   641 lemma SigmaE2:
   642     "[| (a, b) : Sigma A B;
   643         [| a:A;  b:B(a) |] ==> P
   644      |] ==> P"
   645   by blast
   646 
   647 lemma Sigma_cong:
   648      "\<lbrakk>A = B; !!x. x \<in> B \<Longrightarrow> C x = D x\<rbrakk>
   649       \<Longrightarrow> (SIGMA x: A. C x) = (SIGMA x: B. D x)"
   650   by auto
   651 
   652 lemma Sigma_mono: "[| A <= C; !!x. x:A ==> B x <= D x |] ==> Sigma A B <= Sigma C D"
   653   by blast
   654 
   655 lemma Sigma_empty1 [simp]: "Sigma {} B = {}"
   656   by blast
   657 
   658 lemma Sigma_empty2 [simp]: "A <*> {} = {}"
   659   by blast
   660 
   661 lemma UNIV_Times_UNIV [simp]: "UNIV <*> UNIV = UNIV"
   662   by auto
   663 
   664 lemma Compl_Times_UNIV1 [simp]: "- (UNIV <*> A) = UNIV <*> (-A)"
   665   by auto
   666 
   667 lemma Compl_Times_UNIV2 [simp]: "- (A <*> UNIV) = (-A) <*> UNIV"
   668   by auto
   669 
   670 lemma mem_Sigma_iff [iff]: "((a,b): Sigma A B) = (a:A & b:B(a))"
   671   by blast
   672 
   673 lemma Times_subset_cancel2: "x:C ==> (A <*> C <= B <*> C) = (A <= B)"
   674   by blast
   675 
   676 lemma Times_eq_cancel2: "x:C ==> (A <*> C = B <*> C) = (A = B)"
   677   by (blast elim: equalityE)
   678 
   679 lemma SetCompr_Sigma_eq:
   680     "Collect (split (%x y. P x & Q x y)) = (SIGMA x:Collect P. Collect (Q x))"
   681   by blast
   682 
   683 text {*
   684   \bigskip Complex rules for Sigma.
   685 *}
   686 
   687 lemma Collect_split [simp]: "{(a,b). P a & Q b} = Collect P <*> Collect Q"
   688   by blast
   689 
   690 lemma UN_Times_distrib:
   691   "(UN (a,b):(A <*> B). E a <*> F b) = (UNION A E) <*> (UNION B F)"
   692   -- {* Suggested by Pierre Chartier *}
   693   by blast
   694 
   695 lemma split_paired_Ball_Sigma [simp]:
   696     "(ALL z: Sigma A B. P z) = (ALL x:A. ALL y: B x. P(x,y))"
   697   by blast
   698 
   699 lemma split_paired_Bex_Sigma [simp]:
   700     "(EX z: Sigma A B. P z) = (EX x:A. EX y: B x. P(x,y))"
   701   by blast
   702 
   703 lemma Sigma_Un_distrib1: "(SIGMA i:I Un J. C(i)) = (SIGMA i:I. C(i)) Un (SIGMA j:J. C(j))"
   704   by blast
   705 
   706 lemma Sigma_Un_distrib2: "(SIGMA i:I. A(i) Un B(i)) = (SIGMA i:I. A(i)) Un (SIGMA i:I. B(i))"
   707   by blast
   708 
   709 lemma Sigma_Int_distrib1: "(SIGMA i:I Int J. C(i)) = (SIGMA i:I. C(i)) Int (SIGMA j:J. C(j))"
   710   by blast
   711 
   712 lemma Sigma_Int_distrib2: "(SIGMA i:I. A(i) Int B(i)) = (SIGMA i:I. A(i)) Int (SIGMA i:I. B(i))"
   713   by blast
   714 
   715 lemma Sigma_Diff_distrib1: "(SIGMA i:I - J. C(i)) = (SIGMA i:I. C(i)) - (SIGMA j:J. C(j))"
   716   by blast
   717 
   718 lemma Sigma_Diff_distrib2: "(SIGMA i:I. A(i) - B(i)) = (SIGMA i:I. A(i)) - (SIGMA i:I. B(i))"
   719   by blast
   720 
   721 lemma Sigma_Union: "Sigma (Union X) B = (UN A:X. Sigma A B)"
   722   by blast
   723 
   724 text {*
   725   Non-dependent versions are needed to avoid the need for higher-order
   726   matching, especially when the rules are re-oriented.
   727 *}
   728 
   729 lemma Times_Un_distrib1: "(A Un B) <*> C = (A <*> C) Un (B <*> C)"
   730   by blast
   731 
   732 lemma Times_Int_distrib1: "(A Int B) <*> C = (A <*> C) Int (B <*> C)"
   733   by blast
   734 
   735 lemma Times_Diff_distrib1: "(A - B) <*> C = (A <*> C) - (B <*> C)"
   736   by blast
   737 
   738 
   739 lemma pair_imageI [intro]: "(a, b) : A ==> f a b : (%(a, b). f a b) ` A"
   740   apply (rule_tac x = "(a, b)" in image_eqI)
   741    apply auto
   742   done
   743 
   744 
   745 text {*
   746   Setup of internal @{text split_rule}.
   747 *}
   748 
   749 constdefs
   750   internal_split :: "('a => 'b => 'c) => 'a * 'b => 'c"
   751   "internal_split == split"
   752 
   753 lemma internal_split_conv: "internal_split c (a, b) = c a b"
   754   by (simp only: internal_split_def split_conv)
   755 
   756 hide const internal_split
   757 
   758 use "Tools/split_rule.ML"
   759 setup SplitRule.setup
   760 
   761 
   762 subsection {* Code generator setup *}
   763 
   764 instance unit :: eq ..
   765 
   766 lemma [code func]:
   767   "OperationalEquality.eq (u\<Colon>unit) v = True" unfolding eq_def unit_eq [of u] unit_eq [of v] by rule+
   768 
   769 code_instance unit :: eq
   770   (Haskell -)
   771 
   772 instance * :: (eq, eq) eq ..
   773 
   774 lemma [code func]:
   775   "OperationalEquality.eq (x1, y1) (x2, y2) = (OperationalEquality.eq x1 x2 \<and> OperationalEquality.eq y1 y2)"
   776   unfolding eq_def by auto
   777 
   778 code_instance * :: eq
   779   (Haskell -)
   780 
   781 code_const "OperationalEquality.eq \<Colon> unit \<Rightarrow> unit \<Rightarrow> bool"
   782   (Haskell infixl 4 "==")
   783 
   784 code_const "OperationalEquality.eq \<Colon> 'a\<Colon>eq \<times> 'b\<Colon>eq \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool"
   785   (Haskell infixl 4 "==")
   786 
   787 types_code
   788   "*"     ("(_ */ _)")
   789 attach (term_of) {*
   790 fun term_of_id_42 f T g U (x, y) = HOLogic.pair_const T U $ f x $ g y;
   791 *}
   792 attach (test) {*
   793 fun gen_id_42 aG bG i = (aG i, bG i);
   794 *}
   795 
   796 consts_code
   797   "Pair"    ("(_,/ _)")
   798 
   799 ML {*
   800 
   801 fun strip_abs_split 0 t = ([], t)
   802   | strip_abs_split i (Abs (s, T, t)) =
   803       let
   804         val s' = Codegen.new_name t s;
   805         val v = Free (s', T)
   806       in apfst (cons v) (strip_abs_split (i-1) (subst_bound (v, t))) end
   807   | strip_abs_split i (u as Const ("split", _) $ t) = (case strip_abs_split (i+1) t of
   808         (v :: v' :: vs, u) => (HOLogic.mk_prod (v, v') :: vs, u)
   809       | _ => ([], u))
   810   | strip_abs_split i t = ([], t);
   811 
   812 fun let_codegen thy defs gr dep thyname brack t = (case strip_comb t of
   813     (t1 as Const ("Let", _), t2 :: t3 :: ts) =>
   814     let
   815       fun dest_let (l as Const ("Let", _) $ t $ u) =
   816           (case strip_abs_split 1 u of
   817              ([p], u') => apfst (cons (p, t)) (dest_let u')
   818            | _ => ([], l))
   819         | dest_let t = ([], t);
   820       fun mk_code (gr, (l, r)) =
   821         let
   822           val (gr1, pl) = Codegen.invoke_codegen thy defs dep thyname false (gr, l);
   823           val (gr2, pr) = Codegen.invoke_codegen thy defs dep thyname false (gr1, r);
   824         in (gr2, (pl, pr)) end
   825     in case dest_let (t1 $ t2 $ t3) of
   826         ([], _) => NONE
   827       | (ps, u) =>
   828           let
   829             val (gr1, qs) = foldl_map mk_code (gr, ps);
   830             val (gr2, pu) = Codegen.invoke_codegen thy defs dep thyname false (gr1, u);
   831             val (gr3, pargs) = foldl_map
   832               (Codegen.invoke_codegen thy defs dep thyname true) (gr2, ts)
   833           in
   834             SOME (gr3, Codegen.mk_app brack
   835               (Pretty.blk (0, [Pretty.str "let ", Pretty.blk (0, List.concat
   836                   (separate [Pretty.str ";", Pretty.brk 1] (map (fn (pl, pr) =>
   837                     [Pretty.block [Pretty.str "val ", pl, Pretty.str " =",
   838                        Pretty.brk 1, pr]]) qs))),
   839                 Pretty.brk 1, Pretty.str "in ", pu,
   840                 Pretty.brk 1, Pretty.str "end"])) pargs)
   841           end
   842     end
   843   | _ => NONE);
   844 
   845 fun split_codegen thy defs gr dep thyname brack t = (case strip_comb t of
   846     (t1 as Const ("split", _), t2 :: ts) =>
   847       (case strip_abs_split 1 (t1 $ t2) of
   848          ([p], u) =>
   849            let
   850              val (gr1, q) = Codegen.invoke_codegen thy defs dep thyname false (gr, p);
   851              val (gr2, pu) = Codegen.invoke_codegen thy defs dep thyname false (gr1, u);
   852              val (gr3, pargs) = foldl_map
   853                (Codegen.invoke_codegen thy defs dep thyname true) (gr2, ts)
   854            in
   855              SOME (gr2, Codegen.mk_app brack
   856                (Pretty.block [Pretty.str "(fn ", q, Pretty.str " =>",
   857                  Pretty.brk 1, pu, Pretty.str ")"]) pargs)
   858            end
   859        | _ => NONE)
   860   | _ => NONE);
   861 
   862 val prod_codegen_setup =
   863   Codegen.add_codegen "let_codegen" let_codegen
   864   #> Codegen.add_codegen "split_codegen" split_codegen
   865   #> CodegenPackage.add_appconst
   866        ("Let", CodegenPackage.appgen_let)
   867 
   868 *}
   869 
   870 setup prod_codegen_setup
   871 
   872 ML
   873 {*
   874 val Collect_split = thm "Collect_split";
   875 val Compl_Times_UNIV1 = thm "Compl_Times_UNIV1";
   876 val Compl_Times_UNIV2 = thm "Compl_Times_UNIV2";
   877 val PairE = thm "PairE";
   878 val PairE_lemma = thm "PairE_lemma";
   879 val Pair_Rep_inject = thm "Pair_Rep_inject";
   880 val Pair_def = thm "Pair_def";
   881 val Pair_eq = thm "Pair_eq";
   882 val Pair_fst_snd_eq = thm "Pair_fst_snd_eq";
   883 val Pair_inject = thm "Pair_inject";
   884 val ProdI = thm "ProdI";
   885 val SetCompr_Sigma_eq = thm "SetCompr_Sigma_eq";
   886 val SigmaD1 = thm "SigmaD1";
   887 val SigmaD2 = thm "SigmaD2";
   888 val SigmaE = thm "SigmaE";
   889 val SigmaE2 = thm "SigmaE2";
   890 val SigmaI = thm "SigmaI";
   891 val Sigma_Diff_distrib1 = thm "Sigma_Diff_distrib1";
   892 val Sigma_Diff_distrib2 = thm "Sigma_Diff_distrib2";
   893 val Sigma_Int_distrib1 = thm "Sigma_Int_distrib1";
   894 val Sigma_Int_distrib2 = thm "Sigma_Int_distrib2";
   895 val Sigma_Un_distrib1 = thm "Sigma_Un_distrib1";
   896 val Sigma_Un_distrib2 = thm "Sigma_Un_distrib2";
   897 val Sigma_Union = thm "Sigma_Union";
   898 val Sigma_def = thm "Sigma_def";
   899 val Sigma_empty1 = thm "Sigma_empty1";
   900 val Sigma_empty2 = thm "Sigma_empty2";
   901 val Sigma_mono = thm "Sigma_mono";
   902 val The_split = thm "The_split";
   903 val The_split_eq = thm "The_split_eq";
   904 val The_split_eq = thm "The_split_eq";
   905 val Times_Diff_distrib1 = thm "Times_Diff_distrib1";
   906 val Times_Int_distrib1 = thm "Times_Int_distrib1";
   907 val Times_Un_distrib1 = thm "Times_Un_distrib1";
   908 val Times_eq_cancel2 = thm "Times_eq_cancel2";
   909 val Times_subset_cancel2 = thm "Times_subset_cancel2";
   910 val UNIV_Times_UNIV = thm "UNIV_Times_UNIV";
   911 val UN_Times_distrib = thm "UN_Times_distrib";
   912 val Unity_def = thm "Unity_def";
   913 val cond_split_eta = thm "cond_split_eta";
   914 val fst_conv = thm "fst_conv";
   915 val fst_def = thm "fst_def";
   916 val fst_eqD = thm "fst_eqD";
   917 val inj_on_Abs_Prod = thm "inj_on_Abs_Prod";
   918 val injective_fst_snd = thm "injective_fst_snd";
   919 val mem_Sigma_iff = thm "mem_Sigma_iff";
   920 val mem_splitE = thm "mem_splitE";
   921 val mem_splitI = thm "mem_splitI";
   922 val mem_splitI2 = thm "mem_splitI2";
   923 val prod_eqI = thm "prod_eqI";
   924 val prod_fun = thm "prod_fun";
   925 val prod_fun_compose = thm "prod_fun_compose";
   926 val prod_fun_def = thm "prod_fun_def";
   927 val prod_fun_ident = thm "prod_fun_ident";
   928 val prod_fun_imageE = thm "prod_fun_imageE";
   929 val prod_fun_imageI = thm "prod_fun_imageI";
   930 val prod_induct = thm "prod_induct";
   931 val snd_conv = thm "snd_conv";
   932 val snd_def = thm "snd_def";
   933 val snd_eqD = thm "snd_eqD";
   934 val split = thm "split";
   935 val splitD = thm "splitD";
   936 val splitD' = thm "splitD'";
   937 val splitE = thm "splitE";
   938 val splitE' = thm "splitE'";
   939 val splitE2 = thm "splitE2";
   940 val splitI = thm "splitI";
   941 val splitI2 = thm "splitI2";
   942 val splitI2' = thm "splitI2'";
   943 val split_Pair_apply = thm "split_Pair_apply";
   944 val split_beta = thm "split_beta";
   945 val split_conv = thm "split_conv";
   946 val split_def = thm "split_def";
   947 val split_eta = thm "split_eta";
   948 val split_eta_SetCompr = thm "split_eta_SetCompr";
   949 val split_eta_SetCompr2 = thm "split_eta_SetCompr2";
   950 val split_paired_All = thm "split_paired_All";
   951 val split_paired_Ball_Sigma = thm "split_paired_Ball_Sigma";
   952 val split_paired_Bex_Sigma = thm "split_paired_Bex_Sigma";
   953 val split_paired_Ex = thm "split_paired_Ex";
   954 val split_paired_The = thm "split_paired_The";
   955 val split_paired_all = thm "split_paired_all";
   956 val split_part = thm "split_part";
   957 val split_split = thm "split_split";
   958 val split_split_asm = thm "split_split_asm";
   959 val split_tupled_all = thms "split_tupled_all";
   960 val split_weak_cong = thm "split_weak_cong";
   961 val surj_pair = thm "surj_pair";
   962 val surjective_pairing = thm "surjective_pairing";
   963 val unit_abs_eta_conv = thm "unit_abs_eta_conv";
   964 val unit_all_eq1 = thm "unit_all_eq1";
   965 val unit_all_eq2 = thm "unit_all_eq2";
   966 val unit_eq = thm "unit_eq";
   967 val unit_induct = thm "unit_induct";
   968 *}
   969 
   970 end