src/HOL/Product_Type.thy
author wenzelm
Wed Sep 09 20:57:21 2015 +0200 (2015-09-09)
changeset 61144 5e94dfead1c2
parent 61127 76cd7f1ec257
child 61226 af7bed1360f3
permissions -rw-r--r--
simplified simproc programming interfaces;
     1 (*  Title:      HOL/Product_Type.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1992  University of Cambridge
     4 *)
     5 
     6 section \<open>Cartesian products\<close>
     7 
     8 theory Product_Type
     9 imports Typedef Inductive Fun
    10 keywords "inductive_set" "coinductive_set" :: thy_decl
    11 begin
    12 
    13 subsection \<open>@{typ bool} is a datatype\<close>
    14 
    15 free_constructors case_bool for True | False
    16   by auto
    17 
    18 text \<open>Avoid name clashes by prefixing the output of @{text old_rep_datatype} with @{text old}.\<close>
    19 
    20 setup \<open>Sign.mandatory_path "old"\<close>
    21 
    22 old_rep_datatype True False by (auto intro: bool_induct)
    23 
    24 setup \<open>Sign.parent_path\<close>
    25 
    26 text \<open>But erase the prefix for properties that are not generated by @{text free_constructors}.\<close>
    27 
    28 setup \<open>Sign.mandatory_path "bool"\<close>
    29 
    30 lemmas induct = old.bool.induct
    31 lemmas inducts = old.bool.inducts
    32 lemmas rec = old.bool.rec
    33 lemmas simps = bool.distinct bool.case bool.rec
    34 
    35 setup \<open>Sign.parent_path\<close>
    36 
    37 declare case_split [cases type: bool]
    38   -- "prefer plain propositional version"
    39 
    40 lemma
    41   shows [code]: "HOL.equal False P \<longleftrightarrow> \<not> P"
    42     and [code]: "HOL.equal True P \<longleftrightarrow> P" 
    43     and [code]: "HOL.equal P False \<longleftrightarrow> \<not> P"
    44     and [code]: "HOL.equal P True \<longleftrightarrow> P"
    45     and [code nbe]: "HOL.equal P P \<longleftrightarrow> True"
    46   by (simp_all add: equal)
    47 
    48 lemma If_case_cert:
    49   assumes "CASE \<equiv> (\<lambda>b. If b f g)"
    50   shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
    51   using assms by simp_all
    52 
    53 setup \<open>Code.add_case @{thm If_case_cert}\<close>
    54 
    55 code_printing
    56   constant "HOL.equal :: bool \<Rightarrow> bool \<Rightarrow> bool" \<rightharpoonup> (Haskell) infix 4 "=="
    57 | class_instance "bool" :: "equal" \<rightharpoonup> (Haskell) -
    58 
    59 
    60 subsection \<open>The @{text unit} type\<close>
    61 
    62 typedef unit = "{True}"
    63   by auto
    64 
    65 definition Unity :: unit  ("'(')")
    66   where "() = Abs_unit True"
    67 
    68 lemma unit_eq [no_atp]: "u = ()"
    69   by (induct u) (simp add: Unity_def)
    70 
    71 text \<open>
    72   Simplification procedure for @{thm [source] unit_eq}.  Cannot use
    73   this rule directly --- it loops!
    74 \<close>
    75 
    76 simproc_setup unit_eq ("x::unit") = \<open>
    77   fn _ => fn _ => fn ct =>
    78     if HOLogic.is_unit (Thm.term_of ct) then NONE
    79     else SOME (mk_meta_eq @{thm unit_eq})
    80 \<close>
    81 
    82 free_constructors case_unit for "()"
    83   by auto
    84 
    85 text \<open>Avoid name clashes by prefixing the output of @{text old_rep_datatype} with @{text old}.\<close>
    86 
    87 setup \<open>Sign.mandatory_path "old"\<close>
    88 
    89 old_rep_datatype "()" by simp
    90 
    91 setup \<open>Sign.parent_path\<close>
    92 
    93 text \<open>But erase the prefix for properties that are not generated by @{text free_constructors}.\<close>
    94 
    95 setup \<open>Sign.mandatory_path "unit"\<close>
    96 
    97 lemmas induct = old.unit.induct
    98 lemmas inducts = old.unit.inducts
    99 lemmas rec = old.unit.rec
   100 lemmas simps = unit.case unit.rec
   101 
   102 setup \<open>Sign.parent_path\<close>
   103 
   104 lemma unit_all_eq1: "(!!x::unit. PROP P x) == PROP P ()"
   105   by simp
   106 
   107 lemma unit_all_eq2: "(!!x::unit. PROP P) == PROP P"
   108   by (rule triv_forall_equality)
   109 
   110 text \<open>
   111   This rewrite counters the effect of simproc @{text unit_eq} on @{term
   112   [source] "%u::unit. f u"}, replacing it by @{term [source]
   113   f} rather than by @{term [source] "%u. f ()"}.
   114 \<close>
   115 
   116 lemma unit_abs_eta_conv [simp]: "(%u::unit. f ()) = f"
   117   by (rule ext) simp
   118 
   119 lemma UNIV_unit:
   120   "UNIV = {()}" by auto
   121 
   122 instantiation unit :: default
   123 begin
   124 
   125 definition "default = ()"
   126 
   127 instance ..
   128 
   129 end
   130 
   131 instantiation unit :: "{complete_boolean_algebra, complete_linorder, wellorder}"
   132 begin
   133 
   134 definition less_eq_unit :: "unit \<Rightarrow> unit \<Rightarrow> bool"
   135 where
   136   "(_::unit) \<le> _ \<longleftrightarrow> True"
   137 
   138 lemma less_eq_unit [iff]:
   139   "(u::unit) \<le> v"
   140   by (simp add: less_eq_unit_def)
   141 
   142 definition less_unit :: "unit \<Rightarrow> unit \<Rightarrow> bool"
   143 where
   144   "(_::unit) < _ \<longleftrightarrow> False"
   145 
   146 lemma less_unit [iff]:
   147   "\<not> (u::unit) < v"
   148   by (simp_all add: less_eq_unit_def less_unit_def)
   149 
   150 definition bot_unit :: unit
   151 where
   152   [code_unfold]: "\<bottom> = ()"
   153 
   154 definition top_unit :: unit
   155 where
   156   [code_unfold]: "\<top> = ()"
   157 
   158 definition inf_unit :: "unit \<Rightarrow> unit \<Rightarrow> unit"
   159 where
   160   [simp]: "_ \<sqinter> _ = ()"
   161 
   162 definition sup_unit :: "unit \<Rightarrow> unit \<Rightarrow> unit"
   163 where
   164   [simp]: "_ \<squnion> _ = ()"
   165 
   166 definition Inf_unit :: "unit set \<Rightarrow> unit"
   167 where
   168   [simp]: "\<Sqinter>_ = ()"
   169 
   170 definition Sup_unit :: "unit set \<Rightarrow> unit"
   171 where
   172   [simp]: "\<Squnion>_ = ()"
   173 
   174 definition uminus_unit :: "unit \<Rightarrow> unit"
   175 where
   176   [simp]: "- _ = ()"
   177 
   178 declare less_eq_unit_def [abs_def, code_unfold]
   179   less_unit_def [abs_def, code_unfold]
   180   inf_unit_def [abs_def, code_unfold]
   181   sup_unit_def [abs_def, code_unfold]
   182   Inf_unit_def [abs_def, code_unfold]
   183   Sup_unit_def [abs_def, code_unfold]
   184   uminus_unit_def [abs_def, code_unfold]
   185 
   186 instance
   187   by intro_classes auto
   188 
   189 end
   190 
   191 lemma [code]:
   192   "HOL.equal (u::unit) v \<longleftrightarrow> True" unfolding equal unit_eq [of u] unit_eq [of v] by rule+
   193 
   194 code_printing
   195   type_constructor unit \<rightharpoonup>
   196     (SML) "unit"
   197     and (OCaml) "unit"
   198     and (Haskell) "()"
   199     and (Scala) "Unit"
   200 | constant Unity \<rightharpoonup>
   201     (SML) "()"
   202     and (OCaml) "()"
   203     and (Haskell) "()"
   204     and (Scala) "()"
   205 | class_instance unit :: equal \<rightharpoonup>
   206     (Haskell) -
   207 | constant "HOL.equal :: unit \<Rightarrow> unit \<Rightarrow> bool" \<rightharpoonup>
   208     (Haskell) infix 4 "=="
   209 
   210 code_reserved SML
   211   unit
   212 
   213 code_reserved OCaml
   214   unit
   215 
   216 code_reserved Scala
   217   Unit
   218 
   219 
   220 subsection \<open>The product type\<close>
   221 
   222 subsubsection \<open>Type definition\<close>
   223 
   224 definition Pair_Rep :: "'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool" where
   225   "Pair_Rep a b = (\<lambda>x y. x = a \<and> y = b)"
   226 
   227 definition "prod = {f. \<exists>a b. f = Pair_Rep (a::'a) (b::'b)}"
   228 
   229 typedef ('a, 'b) prod (infixr "*" 20) = "prod :: ('a \<Rightarrow> 'b \<Rightarrow> bool) set"
   230   unfolding prod_def by auto
   231 
   232 type_notation (xsymbols)
   233   "prod"  ("(_ \<times>/ _)" [21, 20] 20)
   234 type_notation (HTML output)
   235   "prod"  ("(_ \<times>/ _)" [21, 20] 20)
   236 
   237 definition Pair :: "'a \<Rightarrow> 'b \<Rightarrow> 'a \<times> 'b" where
   238   "Pair a b = Abs_prod (Pair_Rep a b)"
   239 
   240 lemma prod_cases: "(\<And>a b. P (Pair a b)) \<Longrightarrow> P p"
   241   by (cases p) (auto simp add: prod_def Pair_def Pair_Rep_def)
   242 
   243 free_constructors uncurry for Pair fst snd
   244 proof -
   245   fix P :: bool and p :: "'a \<times> 'b"
   246   show "(\<And>x1 x2. p = Pair x1 x2 \<Longrightarrow> P) \<Longrightarrow> P"
   247     by (cases p) (auto simp add: prod_def Pair_def Pair_Rep_def)
   248 next
   249   fix a c :: 'a and b d :: 'b
   250   have "Pair_Rep a b = Pair_Rep c d \<longleftrightarrow> a = c \<and> b = d"
   251     by (auto simp add: Pair_Rep_def fun_eq_iff)
   252   moreover have "Pair_Rep a b \<in> prod" and "Pair_Rep c d \<in> prod"
   253     by (auto simp add: prod_def)
   254   ultimately show "Pair a b = Pair c d \<longleftrightarrow> a = c \<and> b = d"
   255     by (simp add: Pair_def Abs_prod_inject)
   256 qed
   257 
   258 text \<open>Avoid name clashes by prefixing the output of @{text old_rep_datatype} with @{text old}.\<close>
   259 
   260 setup \<open>Sign.mandatory_path "old"\<close>
   261 
   262 old_rep_datatype Pair
   263 by (erule prod_cases) (rule prod.inject)
   264 
   265 setup \<open>Sign.parent_path\<close>
   266 
   267 text \<open>But erase the prefix for properties that are not generated by @{text free_constructors}.\<close>
   268 
   269 setup \<open>Sign.mandatory_path "prod"\<close>
   270 
   271 declare
   272   old.prod.inject[iff del]
   273 
   274 lemmas induct = old.prod.induct
   275 lemmas inducts = old.prod.inducts
   276 lemmas rec = old.prod.rec
   277 lemmas simps = prod.inject prod.case prod.rec
   278 
   279 setup \<open>Sign.parent_path\<close>
   280 
   281 declare prod.case [nitpick_simp del]
   282 declare prod.case_cong_weak [cong del]
   283 
   284 
   285 subsubsection \<open>Tuple syntax\<close>
   286 
   287 text \<open>
   288   Patterns -- extends pre-defined type @{typ pttrn} used in
   289   abstractions.
   290 \<close>
   291 
   292 nonterminal tuple_args and patterns
   293 
   294 syntax
   295   "_tuple"      :: "'a => tuple_args => 'a * 'b"        ("(1'(_,/ _'))")
   296   "_tuple_arg"  :: "'a => tuple_args"                   ("_")
   297   "_tuple_args" :: "'a => tuple_args => tuple_args"     ("_,/ _")
   298   "_pattern"    :: "[pttrn, patterns] => pttrn"         ("'(_,/ _')")
   299   ""            :: "pttrn => patterns"                  ("_")
   300   "_patterns"   :: "[pttrn, patterns] => patterns"      ("_,/ _")
   301 
   302 translations
   303   "(x, y)" \<rightleftharpoons> "CONST Pair x y"
   304   "_pattern x y" \<rightleftharpoons> "CONST Pair x y"
   305   "_patterns x y" \<rightleftharpoons> "CONST Pair x y"
   306   "_tuple x (_tuple_args y z)" \<rightleftharpoons> "_tuple x (_tuple_arg (_tuple y z))"
   307   "\<lambda>(x, y, zs). b" \<rightleftharpoons> "CONST uncurry (\<lambda>x (y, zs). b)"
   308   "\<lambda>(x, y). b" \<rightleftharpoons> "CONST uncurry (\<lambda>x y. b)"
   309   "_abs (CONST Pair x y) t" \<rightharpoonup> "\<lambda>(x, y). t"
   310   -- \<open>This rule accommodates tuples in @{text "case C \<dots> (x, y) \<dots> \<Rightarrow> \<dots>"}:
   311      The @{text "(x, y)"} is parsed as @{text "Pair x y"} because it is @{text logic},
   312      not @{text pttrn}.\<close>
   313 
   314 
   315 subsubsection \<open>Code generator setup\<close>
   316 
   317 code_printing
   318   type_constructor prod \<rightharpoonup>
   319     (SML) infix 2 "*"
   320     and (OCaml) infix 2 "*"
   321     and (Haskell) "!((_),/ (_))"
   322     and (Scala) "((_),/ (_))"
   323 | constant Pair \<rightharpoonup>
   324     (SML) "!((_),/ (_))"
   325     and (OCaml) "!((_),/ (_))"
   326     and (Haskell) "!((_),/ (_))"
   327     and (Scala) "!((_),/ (_))"
   328 | class_instance  prod :: equal \<rightharpoonup>
   329     (Haskell) -
   330 | constant "HOL.equal :: 'a \<times> 'b \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool" \<rightharpoonup>
   331     (Haskell) infix 4 "=="
   332 
   333 
   334 subsubsection \<open>Fundamental operations and properties\<close>
   335 
   336 lemma Pair_inject:
   337   assumes "(a, b) = (a', b')"
   338     and "a = a' ==> b = b' ==> R"
   339   shows R
   340   using assms by simp
   341 
   342 lemma surj_pair [simp]: "EX x y. p = (x, y)"
   343   by (cases p) simp
   344 
   345 code_printing
   346   constant fst \<rightharpoonup> (Haskell) "fst"
   347 | constant snd \<rightharpoonup> (Haskell) "snd"
   348 
   349 lemma case_prod_unfold [nitpick_unfold]: "uncurry = (%c p. c (fst p) (snd p))"
   350   by (simp add: fun_eq_iff split: prod.split)
   351 
   352 lemma fst_eqD: "fst (x, y) = a ==> x = a"
   353   by simp
   354 
   355 lemma snd_eqD: "snd (x, y) = a ==> y = a"
   356   by simp
   357 
   358 lemmas surjective_pairing = prod.collapse [symmetric]
   359 
   360 lemma prod_eq_iff: "s = t \<longleftrightarrow> fst s = fst t \<and> snd s = snd t"
   361   by (cases s, cases t) simp
   362 
   363 lemma prod_eqI [intro?]: "fst p = fst q \<Longrightarrow> snd p = snd q \<Longrightarrow> p = q"
   364   by (simp add: prod_eq_iff)
   365 
   366 lemma split_conv [simp, code]: "(case (a, b) of (c, d) \<Rightarrow> f c d) = f a b"
   367   by (fact prod.case)
   368 
   369 lemma splitI: "f a b \<Longrightarrow> case (a, b) of (c, d) \<Rightarrow> f c d"
   370   by (rule split_conv [THEN iffD2])
   371 
   372 lemma splitD: "(case (a, b) of (c, d) \<Rightarrow> f c d) \<Longrightarrow> f a b"
   373   by (rule split_conv [THEN iffD1])
   374 
   375 lemma split_Pair [simp]: "uncurry Pair = id"
   376   by (simp add: fun_eq_iff split: prod.split)
   377 
   378 lemma split_eta: "(\<lambda>(x, y). f (x, y)) = f"
   379   -- \<open>Subsumes the old @{text split_Pair} when @{term f} is the identity function.\<close>
   380   by (simp add: fun_eq_iff split: prod.split)
   381 
   382 lemma split_comp: "(case x of (a, b) \<Rightarrow> (f \<circ> g) a b) = f (g (fst x)) (snd x)"
   383   by (cases x) simp
   384 
   385 lemma split_twice: "uncurry f (uncurry g p) = uncurry (\<lambda>x y. uncurry f (g x y)) p"
   386   by (fact prod.case_distrib)
   387 
   388 lemma The_split: "The (uncurry P) = (THE xy. P (fst xy) (snd xy))"
   389   by (simp add: case_prod_unfold)
   390 
   391 lemmas split_weak_cong = prod.case_cong_weak
   392   -- \<open>Prevents simplification of @{term c}: much faster\<close>
   393 
   394 lemma cond_split_eta: "(!!x y. f x y = g (x, y)) ==> (%(x, y). f x y) = g"
   395   by (simp add: split_eta)
   396 
   397 lemma split_paired_all [no_atp]: "(!!x. PROP P x) == (!!a b. PROP P (a, b))"
   398 proof
   399   fix a b
   400   assume "!!x. PROP P x"
   401   then show "PROP P (a, b)" .
   402 next
   403   fix x
   404   assume "!!a b. PROP P (a, b)"
   405   from \<open>PROP P (fst x, snd x)\<close> show "PROP P x" by simp
   406 qed
   407 
   408 lemma uncurry_distrib: "f (case x of (x, y) \<Rightarrow> g x y) = (case x of (x, y) \<Rightarrow> f (g x y))"
   409   by (cases x) simp
   410 
   411 text \<open>
   412   The rule @{thm [source] split_paired_all} does not work with the
   413   Simplifier because it also affects premises in congrence rules,
   414   where this can lead to premises of the form @{text "!!a b. ... =
   415   ?P(a, b)"} which cannot be solved by reflexivity.
   416 \<close>
   417 
   418 lemmas split_tupled_all = split_paired_all unit_all_eq2
   419 
   420 ML \<open>
   421   (* replace parameters of product type by individual component parameters *)
   422   local (* filtering with exists_paired_all is an essential optimization *)
   423     fun exists_paired_all (Const (@{const_name Pure.all}, _) $ Abs (_, T, t)) =
   424           can HOLogic.dest_prodT T orelse exists_paired_all t
   425       | exists_paired_all (t $ u) = exists_paired_all t orelse exists_paired_all u
   426       | exists_paired_all (Abs (_, _, t)) = exists_paired_all t
   427       | exists_paired_all _ = false;
   428     val ss =
   429       simpset_of
   430        (put_simpset HOL_basic_ss @{context}
   431         addsimps [@{thm split_paired_all}, @{thm unit_all_eq2}, @{thm unit_abs_eta_conv}]
   432         addsimprocs [@{simproc unit_eq}]);
   433   in
   434     fun split_all_tac ctxt = SUBGOAL (fn (t, i) =>
   435       if exists_paired_all t then safe_full_simp_tac (put_simpset ss ctxt) i else no_tac);
   436 
   437     fun unsafe_split_all_tac ctxt = SUBGOAL (fn (t, i) =>
   438       if exists_paired_all t then full_simp_tac (put_simpset ss ctxt) i else no_tac);
   439 
   440     fun split_all ctxt th =
   441       if exists_paired_all (Thm.prop_of th)
   442       then full_simplify (put_simpset ss ctxt) th else th;
   443   end;
   444 \<close>
   445 
   446 setup \<open>map_theory_claset (fn ctxt => ctxt addSbefore ("split_all_tac", split_all_tac))\<close>
   447 
   448 lemma split_paired_All [simp, no_atp]: "(ALL x. P x) = (ALL a b. P (a, b))"
   449   -- \<open>@{text "[iff]"} is not a good idea because it makes @{text blast} loop\<close>
   450   by fast
   451 
   452 lemma split_paired_Ex [simp, no_atp]: "(EX x. P x) = (EX a b. P (a, b))"
   453   by fast
   454 
   455 lemma split_paired_The [no_atp]: "(THE x. P x) = (THE (a, b). P (a, b))"
   456   -- \<open>Can't be added to simpset: loops!\<close>
   457   by (simp add: split_eta)
   458 
   459 text \<open>
   460   Simplification procedure for @{thm [source] cond_split_eta}.  Using
   461   @{thm [source] split_eta} as a rewrite rule is not general enough,
   462   and using @{thm [source] cond_split_eta} directly would render some
   463   existing proofs very inefficient; similarly for @{text
   464   split_beta}.
   465 \<close>
   466 
   467 ML \<open>
   468 local
   469   val cond_split_eta_ss =
   470     simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms cond_split_eta});
   471   fun Pair_pat k 0 (Bound m) = (m = k)
   472     | Pair_pat k i (Const (@{const_name Pair},  _) $ Bound m $ t) =
   473         i > 0 andalso m = k + i andalso Pair_pat k (i - 1) t
   474     | Pair_pat _ _ _ = false;
   475   fun no_args k i (Abs (_, _, t)) = no_args (k + 1) i t
   476     | no_args k i (t $ u) = no_args k i t andalso no_args k i u
   477     | no_args k i (Bound m) = m < k orelse m > k + i
   478     | no_args _ _ _ = true;
   479   fun split_pat tp i (Abs  (_, _, t)) = if tp 0 i t then SOME (i, t) else NONE
   480     | split_pat tp i (Const (@{const_name uncurry}, _) $ Abs (_, _, t)) = split_pat tp (i + 1) t
   481     | split_pat tp i _ = NONE;
   482   fun metaeq ctxt lhs rhs = mk_meta_eq (Goal.prove ctxt [] []
   483         (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)))
   484         (K (simp_tac (put_simpset cond_split_eta_ss ctxt) 1)));
   485 
   486   fun beta_term_pat k i (Abs (_, _, t)) = beta_term_pat (k + 1) i t
   487     | beta_term_pat k i (t $ u) =
   488         Pair_pat k i (t $ u) orelse (beta_term_pat k i t andalso beta_term_pat k i u)
   489     | beta_term_pat k i t = no_args k i t;
   490   fun eta_term_pat k i (f $ arg) = no_args k i f andalso Pair_pat k i arg
   491     | eta_term_pat _ _ _ = false;
   492   fun subst arg k i (Abs (x, T, t)) = Abs (x, T, subst arg (k+1) i t)
   493     | subst arg k i (t $ u) =
   494         if Pair_pat k i (t $ u) then incr_boundvars k arg
   495         else (subst arg k i t $ subst arg k i u)
   496     | subst arg k i t = t;
   497 in
   498   fun beta_proc ctxt (s as Const (@{const_name uncurry}, _) $ Abs (_, _, t) $ arg) =
   499         (case split_pat beta_term_pat 1 t of
   500           SOME (i, f) => SOME (metaeq ctxt s (subst arg 0 i f))
   501         | NONE => NONE)
   502     | beta_proc _ _ = NONE;
   503   fun eta_proc ctxt (s as Const (@{const_name uncurry}, _) $ Abs (_, _, t)) =
   504         (case split_pat eta_term_pat 1 t of
   505           SOME (_, ft) => SOME (metaeq ctxt s (let val f $ _ = ft in f end))
   506         | NONE => NONE)
   507     | eta_proc _ _ = NONE;
   508 end;
   509 \<close>
   510 simproc_setup split_beta ("split f z") =
   511   \<open>fn _ => fn ctxt => fn ct => beta_proc ctxt (Thm.term_of ct)\<close>
   512 simproc_setup split_eta ("split f") =
   513   \<open>fn _ => fn ctxt => fn ct => eta_proc ctxt (Thm.term_of ct)\<close>
   514 
   515 lemmas split_beta [mono] = prod.case_eq_if
   516 
   517 lemma split_beta': "(\<lambda>(x,y). f x y) = (\<lambda>x. f (fst x) (snd x))"
   518   by (auto simp: fun_eq_iff)
   519 
   520 lemmas split_split [no_atp] = prod.split
   521   -- \<open>For use with @{text split} and the Simplifier.\<close>
   522 
   523 text \<open>
   524   @{thm [source] split_split} could be declared as @{text "[split]"}
   525   done after the Splitter has been speeded up significantly;
   526   precompute the constants involved and don't do anything unless the
   527   current goal contains one of those constants.
   528 \<close>
   529 
   530 lemmas split_split_asm [no_atp] = prod.split_asm
   531 
   532 text \<open>
   533   \medskip @{const uncurry} used as a logical connective or set former.
   534 
   535   \medskip These rules are for use with @{text blast}; could instead
   536   call @{text simp} using @{thm [source] prod.split} as rewrite.\<close>
   537 
   538 lemma splitI2: "!!p. [| !!a b. p = (a, b) ==> c a b |] ==> case p of (a, b) \<Rightarrow> c a b"
   539   apply (simp only: split_tupled_all)
   540   apply (simp (no_asm_simp))
   541   done
   542 
   543 lemma splitI2': "!!p. [| !!a b. (a, b) = p ==> c a b x |] ==> (case p of (a, b) \<Rightarrow> c a b) x"
   544   apply (simp only: split_tupled_all)
   545   apply (simp (no_asm_simp))
   546   done
   547 
   548 lemma splitE: "(case p of (a, b) \<Rightarrow> c a b) ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q"
   549   by (induct p) auto
   550 
   551 lemma splitE': "(case p of (a, b) \<Rightarrow> c a b) z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q"
   552   by (induct p) auto
   553 
   554 lemma splitE2:
   555   "[| Q (case z of (a, b) \<Rightarrow> P a b);  !!x y. [|z = (x, y); Q (P x y)|] ==> R |] ==> R"
   556 proof -
   557   assume q: "Q (uncurry P z)"
   558   assume r: "!!x y. [|z = (x, y); Q (P x y)|] ==> R"
   559   show R
   560     apply (rule r surjective_pairing)+
   561     apply (rule split_beta [THEN subst], rule q)
   562     done
   563 qed
   564 
   565 lemma splitD':
   566   "(case (a, b) of (c, d) \<Rightarrow> R c d) c \<Longrightarrow> R a b c"
   567   by simp
   568 
   569 lemma mem_splitI:
   570   "z \<in> c a b \<Longrightarrow> z \<in> (case (a, b) of (d, e) \<Rightarrow> c d e)"
   571   by simp
   572 
   573 lemma mem_splitI2:
   574   "\<And>p. (\<And>a b. p = (a, b) \<Longrightarrow> z \<in> c a b) \<Longrightarrow> z \<in> (case p of (a, b) \<Rightarrow> c a b)"
   575   by (simp only: split_tupled_all) simp
   576 
   577 lemma mem_splitE:
   578   assumes "z \<in> uncurry c p"
   579   obtains x y where "p = (x, y)" and "z \<in> c x y"
   580   using assms by (rule splitE2)
   581 
   582 declare mem_splitI2 [intro!] mem_splitI [intro!] splitI2' [intro!] splitI2 [intro!] splitI [intro!]
   583 declare mem_splitE [elim!] splitE' [elim!] splitE [elim!]
   584 
   585 ML \<open>
   586 local (* filtering with exists_p_split is an essential optimization *)
   587   fun exists_p_split (Const (@{const_name uncurry},_) $ _ $ (Const (@{const_name Pair},_)$_$_)) = true
   588     | exists_p_split (t $ u) = exists_p_split t orelse exists_p_split u
   589     | exists_p_split (Abs (_, _, t)) = exists_p_split t
   590     | exists_p_split _ = false;
   591 in
   592 fun split_conv_tac ctxt = SUBGOAL (fn (t, i) =>
   593   if exists_p_split t
   594   then safe_full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps @{thms split_conv}) i
   595   else no_tac);
   596 end;
   597 \<close>
   598 
   599 (* This prevents applications of splitE for already splitted arguments leading
   600    to quite time-consuming computations (in particular for nested tuples) *)
   601 setup \<open>map_theory_claset (fn ctxt => ctxt addSbefore ("split_conv_tac", split_conv_tac))\<close>
   602 
   603 lemma split_eta_SetCompr [simp, no_atp]: "(%u. EX x y. u = (x, y) & P (x, y)) = P"
   604   by (rule ext) fast
   605 
   606 lemma split_eta_SetCompr2 [simp, no_atp]: "(%u. EX x y. u = (x, y) & P x y) = uncurry P"
   607   by (rule ext) fast
   608 
   609 lemma split_part [simp]: "(%(a,b). P & Q a b) = (%ab. P & uncurry Q ab)"
   610   -- \<open>Allows simplifications of nested splits in case of independent predicates.\<close>
   611   by (rule ext) blast
   612 
   613 (* Do NOT make this a simp rule as it
   614    a) only helps in special situations
   615    b) can lead to nontermination in the presence of split_def
   616 *)
   617 lemma split_comp_eq: 
   618   fixes f :: "'a => 'b => 'c" and g :: "'d => 'a"
   619   shows "(%u. f (g (fst u)) (snd u)) = (uncurry (%x. f (g x)))"
   620   by (rule ext) auto
   621 
   622 lemma pair_imageI [intro]: "(a, b) : A ==> f a b : (%(a, b). f a b) ` A"
   623   apply (rule_tac x = "(a, b)" in image_eqI)
   624    apply auto
   625   done
   626 
   627 lemma The_split_eq [simp]: "(THE (x',y'). x = x' & y = y') = (x, y)"
   628   by blast
   629 
   630 (*
   631 the following  would be slightly more general,
   632 but cannot be used as rewrite rule:
   633 ### Cannot add premise as rewrite rule because it contains (type) unknowns:
   634 ### ?y = .x
   635 Goal "[| P y; !!x. P x ==> x = y |] ==> (@(x',y). x = x' & P y) = (x,y)"
   636 by (rtac some_equality 1)
   637 by ( Simp_tac 1)
   638 by (split_all_tac 1)
   639 by (Asm_full_simp_tac 1)
   640 qed "The_split_eq";
   641 *)
   642 
   643 text \<open>
   644   Setup of internal @{text split_rule}.
   645 \<close>
   646 
   647 lemmas case_prodI = prod.case [THEN iffD2]
   648 
   649 lemma case_prodI2: "!!p. [| !!a b. p = (a, b) ==> c a b |] ==> uncurry c p"
   650   by (fact splitI2)
   651 
   652 lemma case_prodI2': "!!p. [| !!a b. (a, b) = p ==> c a b x |] ==> uncurry c p x"
   653   by (fact splitI2')
   654 
   655 lemma case_prodE: "uncurry c p ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q"
   656   by (fact splitE)
   657 
   658 lemma case_prodE': "uncurry c p z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q"
   659   by (fact splitE')
   660 
   661 declare case_prodI [intro!]
   662 
   663 lemma case_prod_beta:
   664   "uncurry f p = f (fst p) (snd p)"
   665   by (fact split_beta)
   666 
   667 lemma prod_cases3 [cases type]:
   668   obtains (fields) a b c where "y = (a, b, c)"
   669   by (cases y, case_tac b) blast
   670 
   671 lemma prod_induct3 [case_names fields, induct type]:
   672     "(!!a b c. P (a, b, c)) ==> P x"
   673   by (cases x) blast
   674 
   675 lemma prod_cases4 [cases type]:
   676   obtains (fields) a b c d where "y = (a, b, c, d)"
   677   by (cases y, case_tac c) blast
   678 
   679 lemma prod_induct4 [case_names fields, induct type]:
   680     "(!!a b c d. P (a, b, c, d)) ==> P x"
   681   by (cases x) blast
   682 
   683 lemma prod_cases5 [cases type]:
   684   obtains (fields) a b c d e where "y = (a, b, c, d, e)"
   685   by (cases y, case_tac d) blast
   686 
   687 lemma prod_induct5 [case_names fields, induct type]:
   688     "(!!a b c d e. P (a, b, c, d, e)) ==> P x"
   689   by (cases x) blast
   690 
   691 lemma prod_cases6 [cases type]:
   692   obtains (fields) a b c d e f where "y = (a, b, c, d, e, f)"
   693   by (cases y, case_tac e) blast
   694 
   695 lemma prod_induct6 [case_names fields, induct type]:
   696     "(!!a b c d e f. P (a, b, c, d, e, f)) ==> P x"
   697   by (cases x) blast
   698 
   699 lemma prod_cases7 [cases type]:
   700   obtains (fields) a b c d e f g where "y = (a, b, c, d, e, f, g)"
   701   by (cases y, case_tac f) blast
   702 
   703 lemma prod_induct7 [case_names fields, induct type]:
   704     "(!!a b c d e f g. P (a, b, c, d, e, f, g)) ==> P x"
   705   by (cases x) blast
   706 
   707 definition internal_split :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c" where
   708   "internal_split == uncurry"
   709 
   710 lemma internal_split_conv: "internal_split c (a, b) = c a b"
   711   by (simp only: internal_split_def split_conv)
   712 
   713 ML_file "Tools/split_rule.ML"
   714 
   715 hide_const internal_split
   716 
   717 
   718 subsubsection \<open>Derived operations\<close>
   719 
   720 definition curry    :: "('a \<times> 'b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'c" where
   721   "curry = (\<lambda>c x y. c (x, y))"
   722 
   723 lemma curry_conv [simp, code]: "curry f a b = f (a, b)"
   724   by (simp add: curry_def)
   725 
   726 lemma curryI [intro!]: "f (a, b) \<Longrightarrow> curry f a b"
   727   by (simp add: curry_def)
   728 
   729 lemma curryD [dest!]: "curry f a b \<Longrightarrow> f (a, b)"
   730   by (simp add: curry_def)
   731 
   732 lemma curryE: "curry f a b \<Longrightarrow> (f (a, b) \<Longrightarrow> Q) \<Longrightarrow> Q"
   733   by (simp add: curry_def)
   734 
   735 lemma curry_split [simp]: "curry (uncurry f) = f"
   736   by (simp add: curry_def case_prod_unfold)
   737 
   738 lemma split_curry [simp]: "uncurry (curry f) = f"
   739   by (simp add: curry_def case_prod_unfold)
   740 
   741 lemma curry_K: "curry (\<lambda>x. c) = (\<lambda>x y. c)"
   742 by(simp add: fun_eq_iff)
   743 
   744 text \<open>
   745   The composition-uncurry combinator.
   746 \<close>
   747 
   748 notation fcomp (infixl "\<circ>>" 60)
   749 
   750 definition scomp :: "('a \<Rightarrow> 'b \<times> 'c) \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> 'd) \<Rightarrow> 'a \<Rightarrow> 'd" (infixl "\<circ>\<rightarrow>" 60) where
   751   "f \<circ>\<rightarrow> g = (\<lambda>x. uncurry g (f x))"
   752 
   753 lemma scomp_unfold: "scomp = (\<lambda>f g x. g (fst (f x)) (snd (f x)))"
   754   by (simp add: fun_eq_iff scomp_def case_prod_unfold)
   755 
   756 lemma scomp_apply [simp]: "(f \<circ>\<rightarrow> g) x = uncurry g (f x)"
   757   by (simp add: scomp_unfold case_prod_unfold)
   758 
   759 lemma Pair_scomp: "Pair x \<circ>\<rightarrow> f = f x"
   760   by (simp add: fun_eq_iff)
   761 
   762 lemma scomp_Pair: "x \<circ>\<rightarrow> Pair = x"
   763   by (simp add: fun_eq_iff)
   764 
   765 lemma scomp_scomp: "(f \<circ>\<rightarrow> g) \<circ>\<rightarrow> h = f \<circ>\<rightarrow> (\<lambda>x. g x \<circ>\<rightarrow> h)"
   766   by (simp add: fun_eq_iff scomp_unfold)
   767 
   768 lemma scomp_fcomp: "(f \<circ>\<rightarrow> g) \<circ>> h = f \<circ>\<rightarrow> (\<lambda>x. g x \<circ>> h)"
   769   by (simp add: fun_eq_iff scomp_unfold fcomp_def)
   770 
   771 lemma fcomp_scomp: "(f \<circ>> g) \<circ>\<rightarrow> h = f \<circ>> (g \<circ>\<rightarrow> h)"
   772   by (simp add: fun_eq_iff scomp_unfold)
   773 
   774 code_printing
   775   constant scomp \<rightharpoonup> (Eval) infixl 3 "#->"
   776 
   777 no_notation fcomp (infixl "\<circ>>" 60)
   778 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
   779 
   780 text \<open>
   781   @{term map_prod} --- action of the product functor upon
   782   functions.
   783 \<close>
   784 
   785 definition map_prod :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c \<times> 'd" where
   786   "map_prod f g = (\<lambda>(x, y). (f x, g y))"
   787 
   788 lemma map_prod_simp [simp, code]:
   789   "map_prod f g (a, b) = (f a, g b)"
   790   by (simp add: map_prod_def)
   791 
   792 functor map_prod: map_prod
   793   by (auto simp add: split_paired_all)
   794 
   795 lemma fst_map_prod [simp]:
   796   "fst (map_prod f g x) = f (fst x)"
   797   by (cases x) simp_all
   798 
   799 lemma snd_map_prod [simp]:
   800   "snd (map_prod f g x) = g (snd x)"
   801   by (cases x) simp_all
   802 
   803 lemma fst_comp_map_prod [simp]:
   804   "fst \<circ> map_prod f g = f \<circ> fst"
   805   by (rule ext) simp_all
   806 
   807 lemma snd_comp_map_prod [simp]:
   808   "snd \<circ> map_prod f g = g \<circ> snd"
   809   by (rule ext) simp_all
   810 
   811 lemma map_prod_compose:
   812   "map_prod (f1 o f2) (g1 o g2) = (map_prod f1 g1 o map_prod f2 g2)"
   813   by (rule ext) (simp add: map_prod.compositionality comp_def)
   814 
   815 lemma map_prod_ident [simp]:
   816   "map_prod (%x. x) (%y. y) = (%z. z)"
   817   by (rule ext) (simp add: map_prod.identity)
   818 
   819 lemma map_prod_imageI [intro]:
   820   "(a, b) \<in> R \<Longrightarrow> (f a, g b) \<in> map_prod f g ` R"
   821   by (rule image_eqI) simp_all
   822 
   823 lemma prod_fun_imageE [elim!]:
   824   assumes major: "c \<in> map_prod f g ` R"
   825     and cases: "\<And>x y. c = (f x, g y) \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> P"
   826   shows P
   827   apply (rule major [THEN imageE])
   828   apply (case_tac x)
   829   apply (rule cases)
   830   apply simp_all
   831   done
   832 
   833 definition apfst :: "('a \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c \<times> 'b" where
   834   "apfst f = map_prod f id"
   835 
   836 definition apsnd :: "('b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'a \<times> 'c" where
   837   "apsnd f = map_prod id f"
   838 
   839 lemma apfst_conv [simp, code]:
   840   "apfst f (x, y) = (f x, y)" 
   841   by (simp add: apfst_def)
   842 
   843 lemma apsnd_conv [simp, code]:
   844   "apsnd f (x, y) = (x, f y)" 
   845   by (simp add: apsnd_def)
   846 
   847 lemma fst_apfst [simp]:
   848   "fst (apfst f x) = f (fst x)"
   849   by (cases x) simp
   850 
   851 lemma fst_comp_apfst [simp]:
   852   "fst \<circ> apfst f = f \<circ> fst"
   853   by (simp add: fun_eq_iff)
   854 
   855 lemma fst_apsnd [simp]:
   856   "fst (apsnd f x) = fst x"
   857   by (cases x) simp
   858 
   859 lemma fst_comp_apsnd [simp]:
   860   "fst \<circ> apsnd f = fst"
   861   by (simp add: fun_eq_iff)
   862 
   863 lemma snd_apfst [simp]:
   864   "snd (apfst f x) = snd x"
   865   by (cases x) simp
   866 
   867 lemma snd_comp_apfst [simp]:
   868   "snd \<circ> apfst f = snd"
   869   by (simp add: fun_eq_iff)
   870 
   871 lemma snd_apsnd [simp]:
   872   "snd (apsnd f x) = f (snd x)"
   873   by (cases x) simp
   874 
   875 lemma snd_comp_apsnd [simp]:
   876   "snd \<circ> apsnd f = f \<circ> snd"
   877   by (simp add: fun_eq_iff)
   878 
   879 lemma apfst_compose:
   880   "apfst f (apfst g x) = apfst (f \<circ> g) x"
   881   by (cases x) simp
   882 
   883 lemma apsnd_compose:
   884   "apsnd f (apsnd g x) = apsnd (f \<circ> g) x"
   885   by (cases x) simp
   886 
   887 lemma apfst_apsnd [simp]:
   888   "apfst f (apsnd g x) = (f (fst x), g (snd x))"
   889   by (cases x) simp
   890 
   891 lemma apsnd_apfst [simp]:
   892   "apsnd f (apfst g x) = (g (fst x), f (snd x))"
   893   by (cases x) simp
   894 
   895 lemma apfst_id [simp] :
   896   "apfst id = id"
   897   by (simp add: fun_eq_iff)
   898 
   899 lemma apsnd_id [simp] :
   900   "apsnd id = id"
   901   by (simp add: fun_eq_iff)
   902 
   903 lemma apfst_eq_conv [simp]:
   904   "apfst f x = apfst g x \<longleftrightarrow> f (fst x) = g (fst x)"
   905   by (cases x) simp
   906 
   907 lemma apsnd_eq_conv [simp]:
   908   "apsnd f x = apsnd g x \<longleftrightarrow> f (snd x) = g (snd x)"
   909   by (cases x) simp
   910 
   911 lemma apsnd_apfst_commute:
   912   "apsnd f (apfst g p) = apfst g (apsnd f p)"
   913   by simp
   914 
   915 context
   916 begin
   917 
   918 local_setup \<open>Local_Theory.map_background_naming (Name_Space.mandatory_path "prod")\<close>
   919 
   920 definition swap :: "'a \<times> 'b \<Rightarrow> 'b \<times> 'a"
   921 where
   922   "swap p = (snd p, fst p)"
   923 
   924 end
   925 
   926 lemma swap_simp [simp]:
   927   "prod.swap (x, y) = (y, x)"
   928   by (simp add: prod.swap_def)
   929 
   930 lemma swap_swap [simp]:
   931   "prod.swap (prod.swap p) = p"
   932   by (cases p) simp
   933 
   934 lemma swap_comp_swap [simp]:
   935   "prod.swap \<circ> prod.swap = id"
   936   by (simp add: fun_eq_iff)
   937 
   938 lemma pair_in_swap_image [simp]:
   939   "(y, x) \<in> prod.swap ` A \<longleftrightarrow> (x, y) \<in> A"
   940   by (auto intro!: image_eqI)
   941 
   942 lemma inj_swap [simp]:
   943   "inj_on prod.swap A"
   944   by (rule inj_onI) auto
   945 
   946 lemma swap_inj_on:
   947   "inj_on (\<lambda>(i, j). (j, i)) A"
   948   by (rule inj_onI) auto
   949 
   950 lemma surj_swap [simp]:
   951   "surj prod.swap"
   952   by (rule surjI [of _ prod.swap]) simp
   953 
   954 lemma bij_swap [simp]:
   955   "bij prod.swap"
   956   by (simp add: bij_def)
   957 
   958 lemma case_swap [simp]:
   959   "(case prod.swap p of (y, x) \<Rightarrow> f x y) = (case p of (x, y) \<Rightarrow> f x y)"
   960   by (cases p) simp
   961 
   962 text \<open>
   963   Disjoint union of a family of sets -- Sigma.
   964 \<close>
   965 
   966 definition Sigma :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> ('a \<times> 'b) set" where
   967   Sigma_def: "Sigma A B == UN x:A. UN y:B x. {Pair x y}"
   968 
   969 abbreviation
   970   Times :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<times> 'b) set"
   971     (infixr "<*>" 80) where
   972   "A <*> B == Sigma A (%_. B)"
   973 
   974 notation (xsymbols)
   975   Times  (infixr "\<times>" 80)
   976 
   977 notation (HTML output)
   978   Times  (infixr "\<times>" 80)
   979 
   980 hide_const (open) Times
   981 
   982 syntax
   983   "_Sigma" :: "[pttrn, 'a set, 'b set] => ('a * 'b) set"  ("(3SIGMA _:_./ _)" [0, 0, 10] 10)
   984 translations
   985   "SIGMA x:A. B" == "CONST Sigma A (%x. B)"
   986 
   987 lemma SigmaI [intro!]: "[| a:A;  b:B(a) |] ==> (a,b) : Sigma A B"
   988   by (unfold Sigma_def) blast
   989 
   990 lemma SigmaE [elim!]:
   991     "[| c: Sigma A B;
   992         !!x y.[| x:A;  y:B(x);  c=(x,y) |] ==> P
   993      |] ==> P"
   994   -- \<open>The general elimination rule.\<close>
   995   by (unfold Sigma_def) blast
   996 
   997 text \<open>
   998   Elimination of @{term "(a, b) : A \<times> B"} -- introduces no
   999   eigenvariables.
  1000 \<close>
  1001 
  1002 lemma SigmaD1: "(a, b) : Sigma A B ==> a : A"
  1003   by blast
  1004 
  1005 lemma SigmaD2: "(a, b) : Sigma A B ==> b : B a"
  1006   by blast
  1007 
  1008 lemma SigmaE2:
  1009     "[| (a, b) : Sigma A B;
  1010         [| a:A;  b:B(a) |] ==> P
  1011      |] ==> P"
  1012   by blast
  1013 
  1014 lemma Sigma_cong:
  1015      "\<lbrakk>A = B; !!x. x \<in> B \<Longrightarrow> C x = D x\<rbrakk>
  1016       \<Longrightarrow> (SIGMA x: A. C x) = (SIGMA x: B. D x)"
  1017   by auto
  1018 
  1019 lemma Sigma_mono: "[| A <= C; !!x. x:A ==> B x <= D x |] ==> Sigma A B <= Sigma C D"
  1020   by blast
  1021 
  1022 lemma Sigma_empty1 [simp]: "Sigma {} B = {}"
  1023   by blast
  1024 
  1025 lemma Sigma_empty2 [simp]: "A <*> {} = {}"
  1026   by blast
  1027 
  1028 lemma UNIV_Times_UNIV [simp]: "UNIV <*> UNIV = UNIV"
  1029   by auto
  1030 
  1031 lemma Compl_Times_UNIV1 [simp]: "- (UNIV <*> A) = UNIV <*> (-A)"
  1032   by auto
  1033 
  1034 lemma Compl_Times_UNIV2 [simp]: "- (A <*> UNIV) = (-A) <*> UNIV"
  1035   by auto
  1036 
  1037 lemma mem_Sigma_iff [iff]: "((a,b): Sigma A B) = (a:A & b:B(a))"
  1038   by blast
  1039 
  1040 lemma Sigma_empty_iff: "(SIGMA i:I. X i) = {} \<longleftrightarrow> (\<forall>i\<in>I. X i = {})"
  1041   by auto
  1042 
  1043 lemma Times_subset_cancel2: "x:C ==> (A <*> C <= B <*> C) = (A <= B)"
  1044   by blast
  1045 
  1046 lemma Times_eq_cancel2: "x:C ==> (A <*> C = B <*> C) = (A = B)"
  1047   by (blast elim: equalityE)
  1048 
  1049 lemma SetCompr_Sigma_eq:
  1050   "{(x, y). P x \<and> Q x y} = (SIGMA x:Collect P. Collect (Q x))"
  1051   by blast
  1052 
  1053 lemma Collect_split [simp]:
  1054   "{(a, b). P a \<and> Q b} = Collect P \<times> Collect Q "
  1055   by (fact SetCompr_Sigma_eq)
  1056 
  1057 lemma UN_Times_distrib:
  1058   "(\<Union>(a, b)\<in>A \<times> B. E a \<times> F b) = UNION A E \<times> UNION B F"
  1059   -- \<open>Suggested by Pierre Chartier\<close>
  1060   by blast
  1061 
  1062 lemma split_paired_Ball_Sigma [simp, no_atp]:
  1063   "(\<forall>z\<in>Sigma A B. P z) \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>B x. P (x, y))"
  1064   by blast
  1065 
  1066 lemma split_paired_Bex_Sigma [simp, no_atp]:
  1067   "(\<exists>z\<in>Sigma A B. P z) \<longleftrightarrow> (\<exists>x\<in>A. \<exists>y\<in>B x. P (x, y))"
  1068   by blast
  1069 
  1070 lemma Sigma_Un_distrib1:
  1071   "Sigma (I \<union> J) C = Sigma I C \<union> Sigma J C"
  1072   by blast
  1073 
  1074 lemma Sigma_Un_distrib2:
  1075   "(SIGMA i:I. A i \<union> B i) = Sigma I A \<union> Sigma I B"
  1076   by blast
  1077 
  1078 lemma Sigma_Int_distrib1:
  1079   "Sigma (I \<inter> J) C = Sigma I C \<inter> Sigma J C"
  1080   by blast
  1081 
  1082 lemma Sigma_Int_distrib2:
  1083   "(SIGMA i:I. A i \<inter> B i) = Sigma I A \<inter> Sigma I B"
  1084   by blast
  1085 
  1086 lemma Sigma_Diff_distrib1:
  1087   "Sigma (I - J) C = Sigma I C - Sigma J C"
  1088   by blast
  1089 
  1090 lemma Sigma_Diff_distrib2:
  1091   "(SIGMA i:I. A i - B i) = Sigma I A - Sigma I B"
  1092   by blast
  1093 
  1094 lemma Sigma_Union:
  1095   "Sigma (\<Union>X) B = (\<Union>A\<in>X. Sigma A B)"
  1096   by blast
  1097 
  1098 text \<open>
  1099   Non-dependent versions are needed to avoid the need for higher-order
  1100   matching, especially when the rules are re-oriented.
  1101 \<close>
  1102 
  1103 lemma Times_Un_distrib1:
  1104   "(A \<union> B) \<times> C = A \<times> C \<union> B \<times> C "
  1105   by (fact Sigma_Un_distrib1)
  1106 
  1107 lemma Times_Int_distrib1:
  1108   "(A \<inter> B) \<times> C = A \<times> C \<inter> B \<times> C "
  1109   by (fact Sigma_Int_distrib1)
  1110 
  1111 lemma Times_Diff_distrib1:
  1112   "(A - B) \<times> C = A \<times> C - B \<times> C "
  1113   by (fact Sigma_Diff_distrib1)
  1114 
  1115 lemma Times_empty [simp]:
  1116   "A \<times> B = {} \<longleftrightarrow> A = {} \<or> B = {}"
  1117   by auto
  1118 
  1119 lemma times_eq_iff:
  1120   "A \<times> B = C \<times> D \<longleftrightarrow> A = C \<and> B = D \<or> (A = {} \<or> B = {}) \<and> (C = {} \<or> D = {})"
  1121   by auto
  1122 
  1123 lemma fst_image_times [simp]:
  1124   "fst ` (A \<times> B) = (if B = {} then {} else A)"
  1125   by force
  1126 
  1127 lemma snd_image_times [simp]:
  1128   "snd ` (A \<times> B) = (if A = {} then {} else B)"
  1129   by force
  1130 
  1131 lemma vimage_fst:
  1132   "fst -` A = A \<times> UNIV"
  1133   by auto
  1134 
  1135 lemma vimage_snd:
  1136   "snd -` A = UNIV \<times> A"
  1137   by auto
  1138 
  1139 lemma insert_times_insert[simp]:
  1140   "insert a A \<times> insert b B =
  1141    insert (a,b) (A \<times> insert b B \<union> insert a A \<times> B)"
  1142   by blast
  1143 
  1144 lemma vimage_Times:
  1145   "f -` (A \<times> B) = (fst \<circ> f) -` A \<inter> (snd \<circ> f) -` B"
  1146 proof (rule set_eqI)
  1147   fix x
  1148   show "x \<in> f -` (A \<times> B) \<longleftrightarrow> x \<in> (fst \<circ> f) -` A \<inter> (snd \<circ> f) -` B"
  1149     by (cases "f x") (auto split: prod.split)
  1150 qed
  1151 
  1152 lemma times_Int_times:
  1153   "A \<times> B \<inter> C \<times> D = (A \<inter> C) \<times> (B \<inter> D)"
  1154   by auto
  1155 
  1156 lemma product_swap:
  1157   "prod.swap ` (A \<times> B) = B \<times> A"
  1158   by (auto simp add: set_eq_iff)
  1159 
  1160 lemma swap_product:
  1161   "(\<lambda>(i, j). (j, i)) ` (A \<times> B) = B \<times> A"
  1162   by (auto simp add: set_eq_iff)
  1163 
  1164 lemma image_split_eq_Sigma:
  1165   "(\<lambda>x. (f x, g x)) ` A = Sigma (f ` A) (\<lambda>x. g ` (f -` {x} \<inter> A))"
  1166 proof (safe intro!: imageI)
  1167   fix a b assume *: "a \<in> A" "b \<in> A" and eq: "f a = f b"
  1168   show "(f b, g a) \<in> (\<lambda>x. (f x, g x)) ` A"
  1169     using * eq[symmetric] by auto
  1170 qed simp_all
  1171 
  1172 lemma inj_on_apfst [simp]: "inj_on (apfst f) (A \<times> UNIV) \<longleftrightarrow> inj_on f A"
  1173 by(auto simp add: inj_on_def)
  1174 
  1175 lemma inj_apfst [simp]: "inj (apfst f) \<longleftrightarrow> inj f"
  1176 using inj_on_apfst[of f UNIV] by simp
  1177 
  1178 lemma inj_on_apsnd [simp]: "inj_on (apsnd f) (UNIV \<times> A) \<longleftrightarrow> inj_on f A"
  1179 by(auto simp add: inj_on_def)
  1180 
  1181 lemma inj_apsnd [simp]: "inj (apsnd f) \<longleftrightarrow> inj f"
  1182 using inj_on_apsnd[of f UNIV] by simp
  1183 
  1184 context
  1185 begin
  1186 
  1187 qualified definition product :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<times> 'b) set" where
  1188   [code_abbrev]: "product A B = A \<times> B"
  1189 
  1190 lemma member_product:
  1191   "x \<in> Product_Type.product A B \<longleftrightarrow> x \<in> A \<times> B"
  1192   by (simp add: Product_Type.product_def)
  1193 
  1194 end
  1195   
  1196 text \<open>The following @{const map_prod} lemmas are due to Joachim Breitner:\<close>
  1197 
  1198 lemma map_prod_inj_on:
  1199   assumes "inj_on f A" and "inj_on g B"
  1200   shows "inj_on (map_prod f g) (A \<times> B)"
  1201 proof (rule inj_onI)
  1202   fix x :: "'a \<times> 'c" and y :: "'a \<times> 'c"
  1203   assume "x \<in> A \<times> B" hence "fst x \<in> A" and "snd x \<in> B" by auto
  1204   assume "y \<in> A \<times> B" hence "fst y \<in> A" and "snd y \<in> B" by auto
  1205   assume "map_prod f g x = map_prod f g y"
  1206   hence "fst (map_prod f g x) = fst (map_prod f g y)" by (auto)
  1207   hence "f (fst x) = f (fst y)" by (cases x,cases y,auto)
  1208   with \<open>inj_on f A\<close> and \<open>fst x \<in> A\<close> and \<open>fst y \<in> A\<close>
  1209   have "fst x = fst y" by (auto dest:dest:inj_onD)
  1210   moreover from \<open>map_prod f g x = map_prod f g y\<close>
  1211   have "snd (map_prod f g x) = snd (map_prod f g y)" by (auto)
  1212   hence "g (snd x) = g (snd y)" by (cases x,cases y,auto)
  1213   with \<open>inj_on g B\<close> and \<open>snd x \<in> B\<close> and \<open>snd y \<in> B\<close>
  1214   have "snd x = snd y" by (auto dest:dest:inj_onD)
  1215   ultimately show "x = y" by(rule prod_eqI)
  1216 qed
  1217 
  1218 lemma map_prod_surj:
  1219   fixes f :: "'a \<Rightarrow> 'b" and g :: "'c \<Rightarrow> 'd"
  1220   assumes "surj f" and "surj g"
  1221   shows "surj (map_prod f g)"
  1222 unfolding surj_def
  1223 proof
  1224   fix y :: "'b \<times> 'd"
  1225   from \<open>surj f\<close> obtain a where "fst y = f a" by (auto elim:surjE)
  1226   moreover
  1227   from \<open>surj g\<close> obtain b where "snd y = g b" by (auto elim:surjE)
  1228   ultimately have "(fst y, snd y) = map_prod f g (a,b)" by auto
  1229   thus "\<exists>x. y = map_prod f g x" by auto
  1230 qed
  1231 
  1232 lemma map_prod_surj_on:
  1233   assumes "f ` A = A'" and "g ` B = B'"
  1234   shows "map_prod f g ` (A \<times> B) = A' \<times> B'"
  1235 unfolding image_def
  1236 proof(rule set_eqI,rule iffI)
  1237   fix x :: "'a \<times> 'c"
  1238   assume "x \<in> {y::'a \<times> 'c. \<exists>x::'b \<times> 'd\<in>A \<times> B. y = map_prod f g x}"
  1239   then obtain y where "y \<in> A \<times> B" and "x = map_prod f g y" by blast
  1240   from \<open>image f A = A'\<close> and \<open>y \<in> A \<times> B\<close> have "f (fst y) \<in> A'" by auto
  1241   moreover from \<open>image g B = B'\<close> and \<open>y \<in> A \<times> B\<close> have "g (snd y) \<in> B'" by auto
  1242   ultimately have "(f (fst y), g (snd y)) \<in> (A' \<times> B')" by auto
  1243   with \<open>x = map_prod f g y\<close> show "x \<in> A' \<times> B'" by (cases y, auto)
  1244 next
  1245   fix x :: "'a \<times> 'c"
  1246   assume "x \<in> A' \<times> B'" hence "fst x \<in> A'" and "snd x \<in> B'" by auto
  1247   from \<open>image f A = A'\<close> and \<open>fst x \<in> A'\<close> have "fst x \<in> image f A" by auto
  1248   then obtain a where "a \<in> A" and "fst x = f a" by (rule imageE)
  1249   moreover from \<open>image g B = B'\<close> and \<open>snd x \<in> B'\<close>
  1250   obtain b where "b \<in> B" and "snd x = g b" by auto
  1251   ultimately have "(fst x, snd x) = map_prod f g (a,b)" by auto
  1252   moreover from \<open>a \<in> A\<close> and  \<open>b \<in> B\<close> have "(a , b) \<in> A \<times> B" by auto
  1253   ultimately have "\<exists>y \<in> A \<times> B. x = map_prod f g y" by auto
  1254   thus "x \<in> {x. \<exists>y \<in> A \<times> B. x = map_prod f g y}" by auto
  1255 qed
  1256 
  1257 
  1258 subsection \<open>Simproc for rewriting a set comprehension into a pointfree expression\<close>
  1259 
  1260 ML_file "Tools/set_comprehension_pointfree.ML"
  1261 
  1262 setup \<open>
  1263   Code_Preproc.map_pre (fn ctxt => ctxt addsimprocs
  1264     [Simplifier.make_simproc @{context} "set comprehension"
  1265       {lhss = [@{term "Collect P"}],
  1266        proc = K Set_Comprehension_Pointfree.code_simproc,
  1267        identifier = []}])
  1268 \<close>
  1269 
  1270 
  1271 subsection \<open>Inductively defined sets\<close>
  1272 
  1273 (* simplify {(x1, ..., xn). (x1, ..., xn) : S} to S *)
  1274 simproc_setup Collect_mem ("Collect t") = \<open>
  1275   fn _ => fn ctxt => fn ct =>
  1276     (case Thm.term_of ct of
  1277       S as Const (@{const_name Collect}, Type (@{type_name fun}, [_, T])) $ t =>
  1278         let val (u, _, ps) = HOLogic.strip_psplits t in
  1279           (case u of
  1280             (c as Const (@{const_name Set.member}, _)) $ q $ S' =>
  1281               (case try (HOLogic.strip_ptuple ps) q of
  1282                 NONE => NONE
  1283               | SOME ts =>
  1284                   if not (Term.is_open S') andalso
  1285                     ts = map Bound (length ps downto 0)
  1286                   then
  1287                     let val simp =
  1288                       full_simp_tac (put_simpset HOL_basic_ss ctxt
  1289                         addsimps [@{thm split_paired_all}, @{thm split_conv}]) 1
  1290                     in
  1291                       SOME (Goal.prove ctxt [] []
  1292                         (Const (@{const_name Pure.eq}, T --> T --> propT) $ S $ S')
  1293                         (K (EVERY
  1294                           [resolve_tac ctxt [eq_reflection] 1,
  1295                            resolve_tac ctxt @{thms subset_antisym} 1,
  1296                            resolve_tac ctxt [subsetI] 1, dresolve_tac ctxt [CollectD] 1, simp,
  1297                            resolve_tac ctxt [subsetI] 1, resolve_tac ctxt [CollectI] 1, simp])))
  1298                     end
  1299                   else NONE)
  1300           | _ => NONE)
  1301         end
  1302     | _ => NONE)
  1303 \<close>
  1304 
  1305 ML_file "Tools/inductive_set.ML"
  1306 
  1307 
  1308 subsection \<open>Legacy theorem bindings and duplicates\<close>
  1309 
  1310 abbreviation (input) case_prod :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c" where
  1311   "case_prod \<equiv> uncurry"
  1312 
  1313 abbreviation (input) split :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c" where
  1314   "split \<equiv> uncurry"
  1315 
  1316 lemmas PairE = prod.exhaust
  1317 lemmas Pair_eq = prod.inject
  1318 lemmas fst_conv = prod.sel(1)
  1319 lemmas snd_conv = prod.sel(2)
  1320 lemmas pair_collapse = prod.collapse
  1321 lemmas split = split_conv
  1322 lemmas Pair_fst_snd_eq = prod_eq_iff
  1323 lemmas split_def = case_prod_unfold
  1324 
  1325 hide_const (open) prod
  1326 
  1327 end