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