src/HOL/Product_Type.thy
author wenzelm
Thu Feb 11 23:00:22 2010 +0100 (2010-02-11)
changeset 35115 446c5063e4fd
parent 34900 9b12b0824bfe
child 35364 b8c62d60195c
permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
     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 [noatp]: "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,noatp]: "(%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 syntax (xsymbols)
   146   "*"      :: "[type, type] => type"         ("(_ \<times>/ _)" [21, 20] 20)
   147 syntax (HTML output)
   148   "*"      :: "[type, type] => type"         ("(_ \<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 
   452 local
   453   val cond_split_eta_ss = HOL_basic_ss addsimps [thm "cond_split_eta"]
   454   fun  Pair_pat k 0 (Bound m) = (m = k)
   455   |    Pair_pat k i (Const ("Pair",  _) $ Bound m $ t) = i > 0 andalso
   456                         m = k+i andalso Pair_pat k (i-1) t
   457   |    Pair_pat _ _ _ = false;
   458   fun no_args k i (Abs (_, _, t)) = no_args (k+1) i t
   459   |   no_args k i (t $ u) = no_args k i t andalso no_args k i u
   460   |   no_args k i (Bound m) = m < k orelse m > k+i
   461   |   no_args _ _ _ = true;
   462   fun split_pat tp i (Abs  (_,_,t)) = if tp 0 i t then SOME (i,t) else NONE
   463   |   split_pat tp i (Const ("split", _) $ Abs (_, _, t)) = split_pat tp (i+1) t
   464   |   split_pat tp i _ = NONE;
   465   fun metaeq ss lhs rhs = mk_meta_eq (Goal.prove (Simplifier.the_context ss) [] []
   466         (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs,rhs)))
   467         (K (simp_tac (Simplifier.inherit_context ss cond_split_eta_ss) 1)));
   468 
   469   fun beta_term_pat k i (Abs (_, _, t)) = beta_term_pat (k+1) i t
   470   |   beta_term_pat k i (t $ u) = Pair_pat k i (t $ u) orelse
   471                         (beta_term_pat k i t andalso beta_term_pat k i u)
   472   |   beta_term_pat k i t = no_args k i t;
   473   fun  eta_term_pat k i (f $ arg) = no_args k i f andalso Pair_pat k i arg
   474   |    eta_term_pat _ _ _ = false;
   475   fun subst arg k i (Abs (x, T, t)) = Abs (x, T, subst arg (k+1) i t)
   476   |   subst arg k i (t $ u) = 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 ("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 ("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 [noatp]: "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 [noatp]: "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 ("split",_) $ _ $ (Const ("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 [thm "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,noatp]: "(%u. EX x y. u = (x, y) & P (x, y)) = P"
   586   by (rule ext) fast
   587 
   588 lemma split_eta_SetCompr2 [simp,noatp]: "(%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 hide const internal_split
   638 
   639 use "Tools/split_rule.ML"
   640 setup SplitRule.setup
   641 
   642 lemmas prod_caseI = prod.cases [THEN iffD2, standard]
   643 
   644 lemma prod_caseI2: "!!p. [| !!a b. p = (a, b) ==> c a b |] ==> prod_case c p"
   645   by auto
   646 
   647 lemma prod_caseI2': "!!p. [| !!a b. (a, b) = p ==> c a b x |] ==> prod_case c p x"
   648   by (auto simp: split_tupled_all)
   649 
   650 lemma prod_caseE: "prod_case c p ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q"
   651   by (induct p) auto
   652 
   653 lemma prod_caseE': "prod_case c p z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q"
   654   by (induct p) auto
   655 
   656 lemma prod_case_unfold: "prod_case = (%c p. c (fst p) (snd p))"
   657   by (simp add: expand_fun_eq)
   658 
   659 declare prod_caseI2' [intro!] prod_caseI2 [intro!] prod_caseI [intro!]
   660 declare prod_caseE' [elim!] prod_caseE [elim!]
   661 
   662 lemma prod_case_split:
   663   "prod_case = split"
   664   by (auto simp add: expand_fun_eq)
   665 
   666 lemma prod_case_beta:
   667   "prod_case f p = f (fst p) (snd p)"
   668   unfolding prod_case_split split_beta ..
   669 
   670 
   671 subsection {* Further cases/induct rules for tuples *}
   672 
   673 lemma prod_cases3 [cases type]:
   674   obtains (fields) a b c where "y = (a, b, c)"
   675   by (cases y, case_tac b) blast
   676 
   677 lemma prod_induct3 [case_names fields, induct type]:
   678     "(!!a b c. P (a, b, c)) ==> P x"
   679   by (cases x) blast
   680 
   681 lemma prod_cases4 [cases type]:
   682   obtains (fields) a b c d where "y = (a, b, c, d)"
   683   by (cases y, case_tac c) blast
   684 
   685 lemma prod_induct4 [case_names fields, induct type]:
   686     "(!!a b c d. P (a, b, c, d)) ==> P x"
   687   by (cases x) blast
   688 
   689 lemma prod_cases5 [cases type]:
   690   obtains (fields) a b c d e where "y = (a, b, c, d, e)"
   691   by (cases y, case_tac d) blast
   692 
   693 lemma prod_induct5 [case_names fields, induct type]:
   694     "(!!a b c d e. P (a, b, c, d, e)) ==> P x"
   695   by (cases x) blast
   696 
   697 lemma prod_cases6 [cases type]:
   698   obtains (fields) a b c d e f where "y = (a, b, c, d, e, f)"
   699   by (cases y, case_tac e) blast
   700 
   701 lemma prod_induct6 [case_names fields, induct type]:
   702     "(!!a b c d e f. P (a, b, c, d, e, f)) ==> P x"
   703   by (cases x) blast
   704 
   705 lemma prod_cases7 [cases type]:
   706   obtains (fields) a b c d e f g where "y = (a, b, c, d, e, f, g)"
   707   by (cases y, case_tac f) blast
   708 
   709 lemma prod_induct7 [case_names fields, induct type]:
   710     "(!!a b c d e f g. P (a, b, c, d, e, f, g)) ==> P x"
   711   by (cases x) blast
   712 
   713 
   714 subsubsection {* Derived operations *}
   715 
   716 text {*
   717   The composition-uncurry combinator.
   718 *}
   719 
   720 notation fcomp (infixl "o>" 60)
   721 
   722 definition
   723   scomp :: "('a \<Rightarrow> 'b \<times> 'c) \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> 'd) \<Rightarrow> 'a \<Rightarrow> 'd" (infixl "o\<rightarrow>" 60)
   724 where
   725   "f o\<rightarrow> g = (\<lambda>x. split g (f x))"
   726 
   727 lemma scomp_apply:  "(f o\<rightarrow> g) x = split g (f x)"
   728   by (simp add: scomp_def)
   729 
   730 lemma Pair_scomp: "Pair x o\<rightarrow> f = f x"
   731   by (simp add: expand_fun_eq scomp_apply)
   732 
   733 lemma scomp_Pair: "x o\<rightarrow> Pair = x"
   734   by (simp add: expand_fun_eq scomp_apply)
   735 
   736 lemma scomp_scomp: "(f o\<rightarrow> g) o\<rightarrow> h = f o\<rightarrow> (\<lambda>x. g x o\<rightarrow> h)"
   737   by (simp add: expand_fun_eq split_twice scomp_def)
   738 
   739 lemma scomp_fcomp: "(f o\<rightarrow> g) o> h = f o\<rightarrow> (\<lambda>x. g x o> h)"
   740   by (simp add: expand_fun_eq scomp_apply fcomp_def split_def)
   741 
   742 lemma fcomp_scomp: "(f o> g) o\<rightarrow> h = f o> (g o\<rightarrow> h)"
   743   by (simp add: expand_fun_eq scomp_apply fcomp_apply)
   744 
   745 code_const scomp
   746   (Eval infixl 3 "#->")
   747 
   748 no_notation fcomp (infixl "o>" 60)
   749 no_notation scomp (infixl "o\<rightarrow>" 60)
   750 
   751 
   752 text {*
   753   @{term prod_fun} --- action of the product functor upon
   754   Datatypes.
   755 *}
   756 
   757 definition prod_fun :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c \<times> 'd" where
   758   [code del]: "prod_fun f g = (\<lambda>(x, y). (f x, g y))"
   759 
   760 lemma prod_fun [simp, code]: "prod_fun f g (a, b) = (f a, g b)"
   761   by (simp add: prod_fun_def)
   762 
   763 lemma prod_fun_compose: "prod_fun (f1 o f2) (g1 o g2) = (prod_fun f1 g1 o prod_fun f2 g2)"
   764   by (rule ext) auto
   765 
   766 lemma prod_fun_ident [simp]: "prod_fun (%x. x) (%y. y) = (%z. z)"
   767   by (rule ext) auto
   768 
   769 lemma prod_fun_imageI [intro]: "(a, b) : r ==> (f a, g b) : prod_fun f g ` r"
   770   apply (rule image_eqI)
   771   apply (rule prod_fun [symmetric], assumption)
   772   done
   773 
   774 lemma prod_fun_imageE [elim!]:
   775   assumes major: "c: (prod_fun f g)`r"
   776     and cases: "!!x y. [| c=(f(x),g(y));  (x,y):r |] ==> P"
   777   shows P
   778   apply (rule major [THEN imageE])
   779   apply (rule_tac p = x in PairE)
   780   apply (rule cases)
   781    apply (blast intro: prod_fun)
   782   apply blast
   783   done
   784 
   785 definition
   786   apfst :: "('a \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'c \<times> 'b"
   787 where
   788   [code del]: "apfst f = prod_fun f id"
   789 
   790 definition
   791   apsnd :: "('b \<Rightarrow> 'c) \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'a \<times> 'c"
   792 where
   793   [code del]: "apsnd f = prod_fun id f"
   794 
   795 lemma apfst_conv [simp, code]:
   796   "apfst f (x, y) = (f x, y)" 
   797   by (simp add: apfst_def)
   798 
   799 lemma apsnd_conv [simp, code]:
   800   "apsnd f (x, y) = (x, f y)" 
   801   by (simp add: apsnd_def)
   802 
   803 lemma fst_apfst [simp]:
   804   "fst (apfst f x) = f (fst x)"
   805   by (cases x) simp
   806 
   807 lemma fst_apsnd [simp]:
   808   "fst (apsnd f x) = fst x"
   809   by (cases x) simp
   810 
   811 lemma snd_apfst [simp]:
   812   "snd (apfst f x) = snd x"
   813   by (cases x) simp
   814 
   815 lemma snd_apsnd [simp]:
   816   "snd (apsnd f x) = f (snd x)"
   817   by (cases x) simp
   818 
   819 lemma apfst_compose:
   820   "apfst f (apfst g x) = apfst (f \<circ> g) x"
   821   by (cases x) simp
   822 
   823 lemma apsnd_compose:
   824   "apsnd f (apsnd g x) = apsnd (f \<circ> g) x"
   825   by (cases x) simp
   826 
   827 lemma apfst_apsnd [simp]:
   828   "apfst f (apsnd g x) = (f (fst x), g (snd x))"
   829   by (cases x) simp
   830 
   831 lemma apsnd_apfst [simp]:
   832   "apsnd f (apfst g x) = (g (fst x), f (snd x))"
   833   by (cases x) simp
   834 
   835 lemma apfst_id [simp] :
   836   "apfst id = id"
   837   by (simp add: expand_fun_eq)
   838 
   839 lemma apsnd_id [simp] :
   840   "apsnd id = id"
   841   by (simp add: expand_fun_eq)
   842 
   843 lemma apfst_eq_conv [simp]:
   844   "apfst f x = apfst g x \<longleftrightarrow> f (fst x) = g (fst x)"
   845   by (cases x) simp
   846 
   847 lemma apsnd_eq_conv [simp]:
   848   "apsnd f x = apsnd g x \<longleftrightarrow> f (snd x) = g (snd x)"
   849   by (cases x) simp
   850 
   851 lemma apsnd_apfst_commute:
   852   "apsnd f (apfst g p) = apfst g (apsnd f p)"
   853   by simp
   854 
   855 text {*
   856   Disjoint union of a family of sets -- Sigma.
   857 *}
   858 
   859 definition  Sigma :: "['a set, 'a => 'b set] => ('a \<times> 'b) set" where
   860   Sigma_def: "Sigma A B == UN x:A. UN y:B x. {Pair x y}"
   861 
   862 abbreviation
   863   Times :: "['a set, 'b set] => ('a * 'b) set"
   864     (infixr "<*>" 80) where
   865   "A <*> B == Sigma A (%_. B)"
   866 
   867 notation (xsymbols)
   868   Times  (infixr "\<times>" 80)
   869 
   870 notation (HTML output)
   871   Times  (infixr "\<times>" 80)
   872 
   873 syntax
   874   "_Sigma" :: "[pttrn, 'a set, 'b set] => ('a * 'b) set"  ("(3SIGMA _:_./ _)" [0, 0, 10] 10)
   875 translations
   876   "SIGMA x:A. B" == "CONST Sigma A (%x. B)"
   877 
   878 lemma SigmaI [intro!]: "[| a:A;  b:B(a) |] ==> (a,b) : Sigma A B"
   879   by (unfold Sigma_def) blast
   880 
   881 lemma SigmaE [elim!]:
   882     "[| c: Sigma A B;
   883         !!x y.[| x:A;  y:B(x);  c=(x,y) |] ==> P
   884      |] ==> P"
   885   -- {* The general elimination rule. *}
   886   by (unfold Sigma_def) blast
   887 
   888 text {*
   889   Elimination of @{term "(a, b) : A \<times> B"} -- introduces no
   890   eigenvariables.
   891 *}
   892 
   893 lemma SigmaD1: "(a, b) : Sigma A B ==> a : A"
   894   by blast
   895 
   896 lemma SigmaD2: "(a, b) : Sigma A B ==> b : B a"
   897   by blast
   898 
   899 lemma SigmaE2:
   900     "[| (a, b) : Sigma A B;
   901         [| a:A;  b:B(a) |] ==> P
   902      |] ==> P"
   903   by blast
   904 
   905 lemma Sigma_cong:
   906      "\<lbrakk>A = B; !!x. x \<in> B \<Longrightarrow> C x = D x\<rbrakk>
   907       \<Longrightarrow> (SIGMA x: A. C x) = (SIGMA x: B. D x)"
   908   by auto
   909 
   910 lemma Sigma_mono: "[| A <= C; !!x. x:A ==> B x <= D x |] ==> Sigma A B <= Sigma C D"
   911   by blast
   912 
   913 lemma Sigma_empty1 [simp]: "Sigma {} B = {}"
   914   by blast
   915 
   916 lemma Sigma_empty2 [simp]: "A <*> {} = {}"
   917   by blast
   918 
   919 lemma UNIV_Times_UNIV [simp]: "UNIV <*> UNIV = UNIV"
   920   by auto
   921 
   922 lemma Compl_Times_UNIV1 [simp]: "- (UNIV <*> A) = UNIV <*> (-A)"
   923   by auto
   924 
   925 lemma Compl_Times_UNIV2 [simp]: "- (A <*> UNIV) = (-A) <*> UNIV"
   926   by auto
   927 
   928 lemma mem_Sigma_iff [iff]: "((a,b): Sigma A B) = (a:A & b:B(a))"
   929   by blast
   930 
   931 lemma Times_subset_cancel2: "x:C ==> (A <*> C <= B <*> C) = (A <= B)"
   932   by blast
   933 
   934 lemma Times_eq_cancel2: "x:C ==> (A <*> C = B <*> C) = (A = B)"
   935   by (blast elim: equalityE)
   936 
   937 lemma SetCompr_Sigma_eq:
   938     "Collect (split (%x y. P x & Q x y)) = (SIGMA x:Collect P. Collect (Q x))"
   939   by blast
   940 
   941 lemma Collect_split [simp]: "{(a,b). P a & Q b} = Collect P <*> Collect Q"
   942   by blast
   943 
   944 lemma UN_Times_distrib:
   945   "(UN (a,b):(A <*> B). E a <*> F b) = (UNION A E) <*> (UNION B F)"
   946   -- {* Suggested by Pierre Chartier *}
   947   by blast
   948 
   949 lemma split_paired_Ball_Sigma [simp,noatp]:
   950     "(ALL z: Sigma A B. P z) = (ALL x:A. ALL y: B x. P(x,y))"
   951   by blast
   952 
   953 lemma split_paired_Bex_Sigma [simp,noatp]:
   954     "(EX z: Sigma A B. P z) = (EX x:A. EX y: B x. P(x,y))"
   955   by blast
   956 
   957 lemma Sigma_Un_distrib1: "(SIGMA i:I Un J. C(i)) = (SIGMA i:I. C(i)) Un (SIGMA j:J. C(j))"
   958   by blast
   959 
   960 lemma Sigma_Un_distrib2: "(SIGMA i:I. A(i) Un B(i)) = (SIGMA i:I. A(i)) Un (SIGMA i:I. B(i))"
   961   by blast
   962 
   963 lemma Sigma_Int_distrib1: "(SIGMA i:I Int J. C(i)) = (SIGMA i:I. C(i)) Int (SIGMA j:J. C(j))"
   964   by blast
   965 
   966 lemma Sigma_Int_distrib2: "(SIGMA i:I. A(i) Int B(i)) = (SIGMA i:I. A(i)) Int (SIGMA i:I. B(i))"
   967   by blast
   968 
   969 lemma Sigma_Diff_distrib1: "(SIGMA i:I - J. C(i)) = (SIGMA i:I. C(i)) - (SIGMA j:J. C(j))"
   970   by blast
   971 
   972 lemma Sigma_Diff_distrib2: "(SIGMA i:I. A(i) - B(i)) = (SIGMA i:I. A(i)) - (SIGMA i:I. B(i))"
   973   by blast
   974 
   975 lemma Sigma_Union: "Sigma (Union X) B = (UN A:X. Sigma A B)"
   976   by blast
   977 
   978 text {*
   979   Non-dependent versions are needed to avoid the need for higher-order
   980   matching, especially when the rules are re-oriented.
   981 *}
   982 
   983 lemma Times_Un_distrib1: "(A Un B) <*> C = (A <*> C) Un (B <*> C)"
   984 by blast
   985 
   986 lemma Times_Int_distrib1: "(A Int B) <*> C = (A <*> C) Int (B <*> C)"
   987 by blast
   988 
   989 lemma Times_Diff_distrib1: "(A - B) <*> C = (A <*> C) - (B <*> C)"
   990 by blast
   991 
   992 lemma insert_times_insert[simp]:
   993   "insert a A \<times> insert b B =
   994    insert (a,b) (A \<times> insert b B \<union> insert a A \<times> B)"
   995 by blast
   996 
   997 lemma vimage_Times: "f -` (A \<times> B) = ((fst \<circ> f) -` A) \<inter> ((snd \<circ> f) -` B)"
   998   by (auto, rule_tac p = "f x" in PairE, auto)
   999 
  1000 subsubsection {* Code generator setup *}
  1001 
  1002 lemma [code]:
  1003   "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)
  1004 
  1005 lemma split_case_cert:
  1006   assumes "CASE \<equiv> split f"
  1007   shows "CASE (a, b) \<equiv> f a b"
  1008   using assms by simp
  1009 
  1010 setup {*
  1011   Code.add_case @{thm split_case_cert}
  1012 *}
  1013 
  1014 code_type *
  1015   (SML infix 2 "*")
  1016   (OCaml infix 2 "*")
  1017   (Haskell "!((_),/ (_))")
  1018   (Scala "((_),/ (_))")
  1019 
  1020 code_instance * :: eq
  1021   (Haskell -)
  1022 
  1023 code_const "eq_class.eq \<Colon> 'a\<Colon>eq \<times> 'b\<Colon>eq \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool"
  1024   (Haskell infixl 4 "==")
  1025 
  1026 code_const Pair
  1027   (SML "!((_),/ (_))")
  1028   (OCaml "!((_),/ (_))")
  1029   (Haskell "!((_),/ (_))")
  1030   (Scala "!((_),/ (_))")
  1031 
  1032 code_const fst and snd
  1033   (Haskell "fst" and "snd")
  1034 
  1035 types_code
  1036   "*"     ("(_ */ _)")
  1037 attach (term_of) {*
  1038 fun term_of_id_42 aF aT bF bT (x, y) = HOLogic.pair_const aT bT $ aF x $ bF y;
  1039 *}
  1040 attach (test) {*
  1041 fun gen_id_42 aG aT bG bT i =
  1042   let
  1043     val (x, t) = aG i;
  1044     val (y, u) = bG i
  1045   in ((x, y), fn () => HOLogic.pair_const aT bT $ t () $ u ()) end;
  1046 *}
  1047 
  1048 consts_code
  1049   "Pair"    ("(_,/ _)")
  1050 
  1051 setup {*
  1052 
  1053 let
  1054 
  1055 fun strip_abs_split 0 t = ([], t)
  1056   | strip_abs_split i (Abs (s, T, t)) =
  1057       let
  1058         val s' = Codegen.new_name t s;
  1059         val v = Free (s', T)
  1060       in apfst (cons v) (strip_abs_split (i-1) (subst_bound (v, t))) end
  1061   | strip_abs_split i (u as Const ("split", _) $ t) = (case strip_abs_split (i+1) t of
  1062         (v :: v' :: vs, u) => (HOLogic.mk_prod (v, v') :: vs, u)
  1063       | _ => ([], u))
  1064   | strip_abs_split i t =
  1065       strip_abs_split i (Abs ("x", hd (binder_types (fastype_of t)), t $ Bound 0));
  1066 
  1067 fun let_codegen thy defs dep thyname brack t gr = (case strip_comb t of
  1068     (t1 as Const ("Let", _), t2 :: t3 :: ts) =>
  1069     let
  1070       fun dest_let (l as Const ("Let", _) $ t $ u) =
  1071           (case strip_abs_split 1 u of
  1072              ([p], u') => apfst (cons (p, t)) (dest_let u')
  1073            | _ => ([], l))
  1074         | dest_let t = ([], t);
  1075       fun mk_code (l, r) gr =
  1076         let
  1077           val (pl, gr1) = Codegen.invoke_codegen thy defs dep thyname false l gr;
  1078           val (pr, gr2) = Codegen.invoke_codegen thy defs dep thyname false r gr1;
  1079         in ((pl, pr), gr2) end
  1080     in case dest_let (t1 $ t2 $ t3) of
  1081         ([], _) => NONE
  1082       | (ps, u) =>
  1083           let
  1084             val (qs, gr1) = fold_map mk_code ps gr;
  1085             val (pu, gr2) = Codegen.invoke_codegen thy defs dep thyname false u gr1;
  1086             val (pargs, gr3) = fold_map
  1087               (Codegen.invoke_codegen thy defs dep thyname true) ts gr2
  1088           in
  1089             SOME (Codegen.mk_app brack
  1090               (Pretty.blk (0, [Codegen.str "let ", Pretty.blk (0, flat
  1091                   (separate [Codegen.str ";", Pretty.brk 1] (map (fn (pl, pr) =>
  1092                     [Pretty.block [Codegen.str "val ", pl, Codegen.str " =",
  1093                        Pretty.brk 1, pr]]) qs))),
  1094                 Pretty.brk 1, Codegen.str "in ", pu,
  1095                 Pretty.brk 1, Codegen.str "end"])) pargs, gr3)
  1096           end
  1097     end
  1098   | _ => NONE);
  1099 
  1100 fun split_codegen thy defs dep thyname brack t gr = (case strip_comb t of
  1101     (t1 as Const ("split", _), t2 :: ts) =>
  1102       let
  1103         val ([p], u) = strip_abs_split 1 (t1 $ t2);
  1104         val (q, gr1) = Codegen.invoke_codegen thy defs dep thyname false p gr;
  1105         val (pu, gr2) = Codegen.invoke_codegen thy defs dep thyname false u gr1;
  1106         val (pargs, gr3) = fold_map
  1107           (Codegen.invoke_codegen thy defs dep thyname true) ts gr2
  1108       in
  1109         SOME (Codegen.mk_app brack
  1110           (Pretty.block [Codegen.str "(fn ", q, Codegen.str " =>",
  1111             Pretty.brk 1, pu, Codegen.str ")"]) pargs, gr2)
  1112       end
  1113   | _ => NONE);
  1114 
  1115 in
  1116 
  1117   Codegen.add_codegen "let_codegen" let_codegen
  1118   #> Codegen.add_codegen "split_codegen" split_codegen
  1119 
  1120 end
  1121 *}
  1122 
  1123 
  1124 subsection {* Legacy bindings *}
  1125 
  1126 ML {*
  1127 val Collect_split = thm "Collect_split";
  1128 val Compl_Times_UNIV1 = thm "Compl_Times_UNIV1";
  1129 val Compl_Times_UNIV2 = thm "Compl_Times_UNIV2";
  1130 val PairE = thm "PairE";
  1131 val Pair_Rep_inject = thm "Pair_Rep_inject";
  1132 val Pair_def = thm "Pair_def";
  1133 val Pair_eq = @{thm "prod.inject"};
  1134 val Pair_fst_snd_eq = thm "Pair_fst_snd_eq";
  1135 val ProdI = thm "ProdI";
  1136 val SetCompr_Sigma_eq = thm "SetCompr_Sigma_eq";
  1137 val SigmaD1 = thm "SigmaD1";
  1138 val SigmaD2 = thm "SigmaD2";
  1139 val SigmaE = thm "SigmaE";
  1140 val SigmaE2 = thm "SigmaE2";
  1141 val SigmaI = thm "SigmaI";
  1142 val Sigma_Diff_distrib1 = thm "Sigma_Diff_distrib1";
  1143 val Sigma_Diff_distrib2 = thm "Sigma_Diff_distrib2";
  1144 val Sigma_Int_distrib1 = thm "Sigma_Int_distrib1";
  1145 val Sigma_Int_distrib2 = thm "Sigma_Int_distrib2";
  1146 val Sigma_Un_distrib1 = thm "Sigma_Un_distrib1";
  1147 val Sigma_Un_distrib2 = thm "Sigma_Un_distrib2";
  1148 val Sigma_Union = thm "Sigma_Union";
  1149 val Sigma_def = thm "Sigma_def";
  1150 val Sigma_empty1 = thm "Sigma_empty1";
  1151 val Sigma_empty2 = thm "Sigma_empty2";
  1152 val Sigma_mono = thm "Sigma_mono";
  1153 val The_split = thm "The_split";
  1154 val The_split_eq = thm "The_split_eq";
  1155 val The_split_eq = thm "The_split_eq";
  1156 val Times_Diff_distrib1 = thm "Times_Diff_distrib1";
  1157 val Times_Int_distrib1 = thm "Times_Int_distrib1";
  1158 val Times_Un_distrib1 = thm "Times_Un_distrib1";
  1159 val Times_eq_cancel2 = thm "Times_eq_cancel2";
  1160 val Times_subset_cancel2 = thm "Times_subset_cancel2";
  1161 val UNIV_Times_UNIV = thm "UNIV_Times_UNIV";
  1162 val UN_Times_distrib = thm "UN_Times_distrib";
  1163 val Unity_def = thm "Unity_def";
  1164 val cond_split_eta = thm "cond_split_eta";
  1165 val fst_conv = thm "fst_conv";
  1166 val fst_def = thm "fst_def";
  1167 val fst_eqD = thm "fst_eqD";
  1168 val inj_on_Abs_Prod = thm "inj_on_Abs_Prod";
  1169 val mem_Sigma_iff = thm "mem_Sigma_iff";
  1170 val mem_splitE = thm "mem_splitE";
  1171 val mem_splitI = thm "mem_splitI";
  1172 val mem_splitI2 = thm "mem_splitI2";
  1173 val prod_eqI = thm "prod_eqI";
  1174 val prod_fun = thm "prod_fun";
  1175 val prod_fun_compose = thm "prod_fun_compose";
  1176 val prod_fun_def = thm "prod_fun_def";
  1177 val prod_fun_ident = thm "prod_fun_ident";
  1178 val prod_fun_imageE = thm "prod_fun_imageE";
  1179 val prod_fun_imageI = thm "prod_fun_imageI";
  1180 val prod_induct = thm "prod.induct";
  1181 val snd_conv = thm "snd_conv";
  1182 val snd_def = thm "snd_def";
  1183 val snd_eqD = thm "snd_eqD";
  1184 val split = thm "split";
  1185 val splitD = thm "splitD";
  1186 val splitD' = thm "splitD'";
  1187 val splitE = thm "splitE";
  1188 val splitE' = thm "splitE'";
  1189 val splitE2 = thm "splitE2";
  1190 val splitI = thm "splitI";
  1191 val splitI2 = thm "splitI2";
  1192 val splitI2' = thm "splitI2'";
  1193 val split_beta = thm "split_beta";
  1194 val split_conv = thm "split_conv";
  1195 val split_def = thm "split_def";
  1196 val split_eta = thm "split_eta";
  1197 val split_eta_SetCompr = thm "split_eta_SetCompr";
  1198 val split_eta_SetCompr2 = thm "split_eta_SetCompr2";
  1199 val split_paired_All = thm "split_paired_All";
  1200 val split_paired_Ball_Sigma = thm "split_paired_Ball_Sigma";
  1201 val split_paired_Bex_Sigma = thm "split_paired_Bex_Sigma";
  1202 val split_paired_Ex = thm "split_paired_Ex";
  1203 val split_paired_The = thm "split_paired_The";
  1204 val split_paired_all = thm "split_paired_all";
  1205 val split_part = thm "split_part";
  1206 val split_split = thm "split_split";
  1207 val split_split_asm = thm "split_split_asm";
  1208 val split_tupled_all = thms "split_tupled_all";
  1209 val split_weak_cong = thm "split_weak_cong";
  1210 val surj_pair = thm "surj_pair";
  1211 val surjective_pairing = thm "surjective_pairing";
  1212 val unit_abs_eta_conv = thm "unit_abs_eta_conv";
  1213 val unit_all_eq1 = thm "unit_all_eq1";
  1214 val unit_all_eq2 = thm "unit_all_eq2";
  1215 val unit_eq = thm "unit_eq";
  1216 *}
  1217 
  1218 use "Tools/inductive_set.ML"
  1219 setup Inductive_Set.setup
  1220 
  1221 end