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