src/HOL/Datatype.thy
author haftmann
Tue Sep 19 15:21:42 2006 +0200 (2006-09-19)
changeset 20588 c847c56edf0c
parent 20523 36a59e5d0039
child 20798 3275b03e2fff
permissions -rw-r--r--
added operational equality
berghofe@5181
     1
(*  Title:      HOL/Datatype.thy
berghofe@5181
     2
    ID:         $Id$
wenzelm@11954
     3
    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
berghofe@5181
     4
*)
berghofe@5181
     5
wenzelm@12918
     6
header {* Datatypes *}
wenzelm@11954
     7
nipkow@15131
     8
theory Datatype
krauss@19770
     9
imports Datatype_Universe
nipkow@15131
    10
begin
wenzelm@11954
    11
haftmann@20588
    12
setup "DatatypeCodegen.setup2"
haftmann@20588
    13
wenzelm@11954
    14
subsection {* Representing primitive types *}
berghofe@5181
    15
berghofe@5759
    16
rep_datatype bool
wenzelm@11954
    17
  distinct True_not_False False_not_True
wenzelm@11954
    18
  induction bool_induct
wenzelm@11954
    19
wenzelm@11954
    20
declare case_split [cases type: bool]
wenzelm@11954
    21
  -- "prefer plain propositional version"
wenzelm@11954
    22
wenzelm@11954
    23
rep_datatype unit
wenzelm@11954
    24
  induction unit_induct
berghofe@5181
    25
berghofe@5181
    26
rep_datatype prod
wenzelm@11954
    27
  inject Pair_eq
wenzelm@11954
    28
  induction prod_induct
wenzelm@11954
    29
wenzelm@12918
    30
rep_datatype sum
wenzelm@12918
    31
  distinct Inl_not_Inr Inr_not_Inl
wenzelm@12918
    32
  inject Inl_eq Inr_eq
wenzelm@12918
    33
  induction sum_induct
wenzelm@12918
    34
wenzelm@12918
    35
ML {*
wenzelm@12918
    36
  val [sum_case_Inl, sum_case_Inr] = thms "sum.cases";
wenzelm@12918
    37
  bind_thm ("sum_case_Inl", sum_case_Inl);
wenzelm@12918
    38
  bind_thm ("sum_case_Inr", sum_case_Inr);
wenzelm@12918
    39
*} -- {* compatibility *}
wenzelm@12918
    40
wenzelm@12918
    41
lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)"
wenzelm@12918
    42
  apply (rule_tac s = s in sumE)
wenzelm@12918
    43
   apply (erule ssubst)
wenzelm@12918
    44
   apply (rule sum_case_Inl)
wenzelm@12918
    45
  apply (erule ssubst)
wenzelm@12918
    46
  apply (rule sum_case_Inr)
wenzelm@12918
    47
  done
wenzelm@12918
    48
wenzelm@12918
    49
lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t"
wenzelm@12918
    50
  -- {* Prevents simplification of @{text f} and @{text g}: much faster. *}
wenzelm@12918
    51
  by (erule arg_cong)
wenzelm@12918
    52
wenzelm@12918
    53
lemma sum_case_inject:
wenzelm@12918
    54
  "sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P"
wenzelm@12918
    55
proof -
wenzelm@12918
    56
  assume a: "sum_case f1 f2 = sum_case g1 g2"
wenzelm@12918
    57
  assume r: "f1 = g1 ==> f2 = g2 ==> P"
wenzelm@12918
    58
  show P
wenzelm@12918
    59
    apply (rule r)
wenzelm@12918
    60
     apply (rule ext)
paulson@14208
    61
     apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp)
wenzelm@12918
    62
    apply (rule ext)
paulson@14208
    63
    apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp)
wenzelm@12918
    64
    done
wenzelm@12918
    65
qed
wenzelm@12918
    66
berghofe@13635
    67
constdefs
berghofe@13635
    68
  Suml :: "('a => 'c) => 'a + 'b => 'c"
berghofe@13635
    69
  "Suml == (%f. sum_case f arbitrary)"
berghofe@13635
    70
berghofe@13635
    71
  Sumr :: "('b => 'c) => 'a + 'b => 'c"
berghofe@13635
    72
  "Sumr == sum_case arbitrary"
berghofe@13635
    73
berghofe@13635
    74
lemma Suml_inject: "Suml f = Suml g ==> f = g"
berghofe@13635
    75
  by (unfold Suml_def) (erule sum_case_inject)
berghofe@13635
    76
berghofe@13635
    77
lemma Sumr_inject: "Sumr f = Sumr g ==> f = g"
berghofe@13635
    78
  by (unfold Sumr_def) (erule sum_case_inject)
berghofe@13635
    79
berghofe@13635
    80
berghofe@13635
    81
subsection {* Finishing the datatype package setup *}
berghofe@13635
    82
berghofe@13635
    83
text {* Belongs to theory @{text Datatype_Universe}; hides popular names. *}
wenzelm@18230
    84
hide (open) const Push Node Atom Leaf Numb Lim Split Case Suml Sumr
wenzelm@18230
    85
hide (open) type node item
berghofe@13635
    86
wenzelm@12918
    87
wenzelm@12918
    88
subsection {* Further cases/induct rules for tuples *}
wenzelm@11954
    89
wenzelm@11954
    90
lemma prod_cases3 [case_names fields, cases type]:
wenzelm@11954
    91
    "(!!a b c. y = (a, b, c) ==> P) ==> P"
wenzelm@11954
    92
  apply (cases y)
paulson@14208
    93
  apply (case_tac b, blast)
wenzelm@11954
    94
  done
wenzelm@11954
    95
wenzelm@11954
    96
lemma prod_induct3 [case_names fields, induct type]:
wenzelm@11954
    97
    "(!!a b c. P (a, b, c)) ==> P x"
wenzelm@11954
    98
  by (cases x) blast
wenzelm@11954
    99
wenzelm@11954
   100
lemma prod_cases4 [case_names fields, cases type]:
wenzelm@11954
   101
    "(!!a b c d. y = (a, b, c, d) ==> P) ==> P"
wenzelm@11954
   102
  apply (cases y)
paulson@14208
   103
  apply (case_tac c, blast)
wenzelm@11954
   104
  done
wenzelm@11954
   105
wenzelm@11954
   106
lemma prod_induct4 [case_names fields, induct type]:
wenzelm@11954
   107
    "(!!a b c d. P (a, b, c, d)) ==> P x"
wenzelm@11954
   108
  by (cases x) blast
berghofe@5181
   109
wenzelm@11954
   110
lemma prod_cases5 [case_names fields, cases type]:
wenzelm@11954
   111
    "(!!a b c d e. y = (a, b, c, d, e) ==> P) ==> P"
wenzelm@11954
   112
  apply (cases y)
paulson@14208
   113
  apply (case_tac d, blast)
wenzelm@11954
   114
  done
wenzelm@11954
   115
wenzelm@11954
   116
lemma prod_induct5 [case_names fields, induct type]:
wenzelm@11954
   117
    "(!!a b c d e. P (a, b, c, d, e)) ==> P x"
wenzelm@11954
   118
  by (cases x) blast
wenzelm@11954
   119
wenzelm@11954
   120
lemma prod_cases6 [case_names fields, cases type]:
wenzelm@11954
   121
    "(!!a b c d e f. y = (a, b, c, d, e, f) ==> P) ==> P"
wenzelm@11954
   122
  apply (cases y)
paulson@14208
   123
  apply (case_tac e, blast)
wenzelm@11954
   124
  done
wenzelm@11954
   125
wenzelm@11954
   126
lemma prod_induct6 [case_names fields, induct type]:
wenzelm@11954
   127
    "(!!a b c d e f. P (a, b, c, d, e, f)) ==> P x"
wenzelm@11954
   128
  by (cases x) blast
wenzelm@11954
   129
wenzelm@11954
   130
lemma prod_cases7 [case_names fields, cases type]:
wenzelm@11954
   131
    "(!!a b c d e f g. y = (a, b, c, d, e, f, g) ==> P) ==> P"
wenzelm@11954
   132
  apply (cases y)
paulson@14208
   133
  apply (case_tac f, blast)
wenzelm@11954
   134
  done
wenzelm@11954
   135
wenzelm@11954
   136
lemma prod_induct7 [case_names fields, induct type]:
wenzelm@11954
   137
    "(!!a b c d e f g. P (a, b, c, d, e, f, g)) ==> P x"
wenzelm@11954
   138
  by (cases x) blast
berghofe@5759
   139
wenzelm@12918
   140
wenzelm@12918
   141
subsection {* The option type *}
wenzelm@12918
   142
wenzelm@12918
   143
datatype 'a option = None | Some 'a
wenzelm@12918
   144
nipkow@18576
   145
lemma not_None_eq[iff]: "(x ~= None) = (EX y. x = Some y)"
nipkow@18576
   146
  by (induct x) auto
nipkow@18576
   147
nipkow@18576
   148
lemma not_Some_eq[iff]: "(ALL y. x ~= Some y) = (x = None)"
paulson@18447
   149
  by (induct x) auto
paulson@18447
   150
nipkow@18576
   151
text{*Although it may appear that both of these equalities are helpful
nipkow@18576
   152
only when applied to assumptions, in practice it seems better to give
nipkow@18576
   153
them the uniform iff attribute. *}
wenzelm@12918
   154
nipkow@18576
   155
(*
paulson@18447
   156
lemmas not_None_eq_D = not_None_eq [THEN iffD1]
paulson@18447
   157
declare not_None_eq_D [dest!]
paulson@18447
   158
paulson@18447
   159
lemmas not_Some_eq_D = not_Some_eq [THEN iffD1]
paulson@18447
   160
declare not_Some_eq_D [dest!]
nipkow@18576
   161
*)
wenzelm@12918
   162
wenzelm@12918
   163
lemma option_caseE:
wenzelm@12918
   164
  "(case x of None => P | Some y => Q y) ==>
wenzelm@12918
   165
    (x = None ==> P ==> R) ==>
wenzelm@12918
   166
    (!!y. x = Some y ==> Q y ==> R) ==> R"
wenzelm@12918
   167
  by (cases x) simp_all
wenzelm@12918
   168
wenzelm@12918
   169
wenzelm@12918
   170
subsubsection {* Operations *}
wenzelm@12918
   171
wenzelm@12918
   172
consts
wenzelm@12918
   173
  the :: "'a option => 'a"
wenzelm@12918
   174
primrec
wenzelm@12918
   175
  "the (Some x) = x"
wenzelm@12918
   176
wenzelm@12918
   177
consts
wenzelm@12918
   178
  o2s :: "'a option => 'a set"
wenzelm@12918
   179
primrec
wenzelm@12918
   180
  "o2s None = {}"
wenzelm@12918
   181
  "o2s (Some x) = {x}"
wenzelm@12918
   182
wenzelm@12918
   183
lemma ospec [dest]: "(ALL x:o2s A. P x) ==> A = Some x ==> P x"
wenzelm@12918
   184
  by simp
wenzelm@12918
   185
wenzelm@17876
   186
ML_setup {* change_claset (fn cs => cs addSD2 ("ospec", thm "ospec")) *}
wenzelm@12918
   187
wenzelm@12918
   188
lemma elem_o2s [iff]: "(x : o2s xo) = (xo = Some x)"
wenzelm@12918
   189
  by (cases xo) auto
wenzelm@12918
   190
wenzelm@12918
   191
lemma o2s_empty_eq [simp]: "(o2s xo = {}) = (xo = None)"
wenzelm@12918
   192
  by (cases xo) auto
wenzelm@12918
   193
wenzelm@12918
   194
wenzelm@12918
   195
constdefs
wenzelm@12918
   196
  option_map :: "('a => 'b) => ('a option => 'b option)"
wenzelm@12918
   197
  "option_map == %f y. case y of None => None | Some x => Some (f x)"
wenzelm@12918
   198
wenzelm@12918
   199
lemma option_map_None [simp]: "option_map f None = None"
wenzelm@12918
   200
  by (simp add: option_map_def)
wenzelm@12918
   201
wenzelm@12918
   202
lemma option_map_Some [simp]: "option_map f (Some x) = Some (f x)"
wenzelm@12918
   203
  by (simp add: option_map_def)
wenzelm@12918
   204
nipkow@14187
   205
lemma option_map_is_None[iff]:
nipkow@14187
   206
 "(option_map f opt = None) = (opt = None)"
nipkow@14187
   207
by (simp add: option_map_def split add: option.split)
nipkow@14187
   208
wenzelm@12918
   209
lemma option_map_eq_Some [iff]:
wenzelm@12918
   210
    "(option_map f xo = Some y) = (EX z. xo = Some z & f z = y)"
nipkow@14187
   211
by (simp add: option_map_def split add: option.split)
nipkow@14187
   212
nipkow@14187
   213
lemma option_map_comp:
nipkow@14187
   214
 "option_map f (option_map g opt) = option_map (f o g) opt"
nipkow@14187
   215
by (simp add: option_map_def split add: option.split)
wenzelm@12918
   216
wenzelm@12918
   217
lemma option_map_o_sum_case [simp]:
wenzelm@12918
   218
    "option_map f o sum_case g h = sum_case (option_map f o g) (option_map f o h)"
wenzelm@12918
   219
  apply (rule ext)
wenzelm@12918
   220
  apply (simp split add: sum.split)
wenzelm@12918
   221
  done
wenzelm@12918
   222
haftmann@19787
   223
haftmann@19817
   224
subsubsection {* Codegenerator setup *}
haftmann@19817
   225
haftmann@19787
   226
consts
haftmann@19787
   227
  is_none :: "'a option \<Rightarrow> bool"
haftmann@19787
   228
primrec
haftmann@19787
   229
  "is_none None = True"
haftmann@19787
   230
  "is_none (Some x) = False"
haftmann@19787
   231
haftmann@20105
   232
lemma is_none_none [code inline]:
haftmann@19787
   233
  "(x = None) = (is_none x)" 
haftmann@20105
   234
by (cases x) simp_all
haftmann@19787
   235
wenzelm@17458
   236
lemmas [code] = imp_conv_disj
wenzelm@17458
   237
krauss@20523
   238
lemma [code func]:
haftmann@19138
   239
  "(\<not> True) = False" by (rule HOL.simp_thms)
haftmann@19138
   240
krauss@20523
   241
lemma [code func]:
haftmann@19138
   242
  "(\<not> False) = True" by (rule HOL.simp_thms)
haftmann@19138
   243
krauss@20523
   244
lemma [code func]:
nipkow@19179
   245
  shows "(False \<and> x) = False"
nipkow@19179
   246
  and   "(True \<and> x) = x"
nipkow@19179
   247
  and   "(x \<and> False) = False"
nipkow@19179
   248
  and   "(x \<and> True) = x" by simp_all
haftmann@19138
   249
krauss@20523
   250
lemma [code func]:
nipkow@19179
   251
  shows "(False \<or> x) = x"
nipkow@19179
   252
  and   "(True \<or> x) = True"
nipkow@19179
   253
  and   "(x \<or> False) = x"
nipkow@19179
   254
  and   "(x \<or> True) = True" by simp_all
haftmann@19138
   255
haftmann@19138
   256
declare
krauss@20523
   257
  if_True [code func]
krauss@20523
   258
  if_False [code func]
nipkow@19179
   259
  fst_conv [code]
nipkow@19179
   260
  snd_conv [code]
haftmann@19138
   261
haftmann@20105
   262
lemma split_is_prod_case [code inline]:
haftmann@20105
   263
  "split = prod_case"
haftmann@20105
   264
by (simp add: expand_fun_eq split_def prod.cases)
haftmann@20105
   265
haftmann@20453
   266
code_type bool
haftmann@20453
   267
  (SML target_atom "bool")
haftmann@20453
   268
  (Haskell target_atom "Bool")
haftmann@19138
   269
haftmann@20453
   270
code_const True and False and Not and "op &" and "op |" and If
haftmann@20453
   271
  (SML target_atom "true" and target_atom "false" and target_atom "not"
haftmann@20453
   272
    and infixl 1 "andalso" and infixl 0 "orelse"
haftmann@20453
   273
    and target_atom "(if __/ then __/ else __)")
haftmann@20453
   274
  (Haskell target_atom "True" and target_atom "False" and target_atom "not"
haftmann@20453
   275
    and infixl 3 "&&" and infixl 2 "||"
haftmann@20453
   276
    and target_atom "(if __/ then __/ else __)")
haftmann@20453
   277
haftmann@20453
   278
code_type *
haftmann@20453
   279
  (SML infix 2 "*")
haftmann@20453
   280
  (Haskell target_atom "(__,/ __)")
haftmann@19138
   281
haftmann@20453
   282
code_const Pair
haftmann@20453
   283
  (SML target_atom "(__,/ __)")
haftmann@20453
   284
  (Haskell target_atom "(__,/ __)")
haftmann@18702
   285
haftmann@20453
   286
code_type unit
haftmann@20453
   287
  (SML target_atom "unit")
haftmann@20453
   288
  (Haskell target_atom "()")
haftmann@19150
   289
haftmann@20453
   290
code_const Unity
haftmann@20453
   291
  (SML target_atom "()")
haftmann@20453
   292
  (Haskell target_atom "()")
haftmann@19150
   293
haftmann@20453
   294
code_type option
haftmann@20453
   295
  (SML "_ option")
haftmann@20453
   296
  (Haskell "Maybe _")
haftmann@19150
   297
haftmann@20453
   298
code_const None and Some
haftmann@20453
   299
  (SML target_atom "NONE" and target_atom "SOME")
haftmann@20453
   300
  (Haskell target_atom "Nothing" and target_atom "Just")
haftmann@19150
   301
haftmann@20588
   302
code_instance option :: eq
haftmann@20588
   303
  (Haskell -)
haftmann@20588
   304
haftmann@20588
   305
code_const "OperationalEquality.eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
haftmann@20588
   306
  (Haskell infixl 4 "==")
haftmann@20588
   307
berghofe@5181
   308
end