src/HOL/Datatype.thy
author haftmann
Tue Jan 17 16:36:57 2006 +0100 (2006-01-17)
changeset 18702 7dc7dcd63224
parent 18576 8d98b7711e47
child 18757 f0d901bc0686
permissions -rw-r--r--
substantial improvements in code generator
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
nipkow@15140
     9
imports Datatype_Universe
nipkow@15131
    10
begin
wenzelm@11954
    11
wenzelm@11954
    12
subsection {* Representing primitive types *}
berghofe@5181
    13
berghofe@5759
    14
rep_datatype bool
wenzelm@11954
    15
  distinct True_not_False False_not_True
wenzelm@11954
    16
  induction bool_induct
wenzelm@11954
    17
wenzelm@11954
    18
declare case_split [cases type: bool]
wenzelm@11954
    19
  -- "prefer plain propositional version"
wenzelm@11954
    20
wenzelm@11954
    21
rep_datatype unit
wenzelm@11954
    22
  induction unit_induct
berghofe@5181
    23
berghofe@5181
    24
rep_datatype prod
wenzelm@11954
    25
  inject Pair_eq
wenzelm@11954
    26
  induction prod_induct
wenzelm@11954
    27
wenzelm@12918
    28
rep_datatype sum
wenzelm@12918
    29
  distinct Inl_not_Inr Inr_not_Inl
wenzelm@12918
    30
  inject Inl_eq Inr_eq
wenzelm@12918
    31
  induction sum_induct
wenzelm@12918
    32
wenzelm@12918
    33
ML {*
wenzelm@12918
    34
  val [sum_case_Inl, sum_case_Inr] = thms "sum.cases";
wenzelm@12918
    35
  bind_thm ("sum_case_Inl", sum_case_Inl);
wenzelm@12918
    36
  bind_thm ("sum_case_Inr", sum_case_Inr);
wenzelm@12918
    37
*} -- {* compatibility *}
wenzelm@12918
    38
wenzelm@12918
    39
lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)"
wenzelm@12918
    40
  apply (rule_tac s = s in sumE)
wenzelm@12918
    41
   apply (erule ssubst)
wenzelm@12918
    42
   apply (rule sum_case_Inl)
wenzelm@12918
    43
  apply (erule ssubst)
wenzelm@12918
    44
  apply (rule sum_case_Inr)
wenzelm@12918
    45
  done
wenzelm@12918
    46
wenzelm@12918
    47
lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t"
wenzelm@12918
    48
  -- {* Prevents simplification of @{text f} and @{text g}: much faster. *}
wenzelm@12918
    49
  by (erule arg_cong)
wenzelm@12918
    50
wenzelm@12918
    51
lemma sum_case_inject:
wenzelm@12918
    52
  "sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P"
wenzelm@12918
    53
proof -
wenzelm@12918
    54
  assume a: "sum_case f1 f2 = sum_case g1 g2"
wenzelm@12918
    55
  assume r: "f1 = g1 ==> f2 = g2 ==> P"
wenzelm@12918
    56
  show P
wenzelm@12918
    57
    apply (rule r)
wenzelm@12918
    58
     apply (rule ext)
paulson@14208
    59
     apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp)
wenzelm@12918
    60
    apply (rule ext)
paulson@14208
    61
    apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp)
wenzelm@12918
    62
    done
wenzelm@12918
    63
qed
wenzelm@12918
    64
berghofe@13635
    65
constdefs
berghofe@13635
    66
  Suml :: "('a => 'c) => 'a + 'b => 'c"
berghofe@13635
    67
  "Suml == (%f. sum_case f arbitrary)"
berghofe@13635
    68
berghofe@13635
    69
  Sumr :: "('b => 'c) => 'a + 'b => 'c"
berghofe@13635
    70
  "Sumr == sum_case arbitrary"
berghofe@13635
    71
berghofe@13635
    72
lemma Suml_inject: "Suml f = Suml g ==> f = g"
berghofe@13635
    73
  by (unfold Suml_def) (erule sum_case_inject)
berghofe@13635
    74
berghofe@13635
    75
lemma Sumr_inject: "Sumr f = Sumr g ==> f = g"
berghofe@13635
    76
  by (unfold Sumr_def) (erule sum_case_inject)
berghofe@13635
    77
berghofe@13635
    78
berghofe@13635
    79
subsection {* Finishing the datatype package setup *}
berghofe@13635
    80
berghofe@13635
    81
text {* Belongs to theory @{text Datatype_Universe}; hides popular names. *}
wenzelm@18230
    82
hide (open) const Push Node Atom Leaf Numb Lim Split Case Suml Sumr
wenzelm@18230
    83
hide (open) type node item
berghofe@13635
    84
wenzelm@12918
    85
wenzelm@12918
    86
subsection {* Further cases/induct rules for tuples *}
wenzelm@11954
    87
wenzelm@11954
    88
lemma prod_cases3 [case_names fields, cases type]:
wenzelm@11954
    89
    "(!!a b c. y = (a, b, c) ==> P) ==> P"
wenzelm@11954
    90
  apply (cases y)
paulson@14208
    91
  apply (case_tac b, blast)
wenzelm@11954
    92
  done
wenzelm@11954
    93
wenzelm@11954
    94
lemma prod_induct3 [case_names fields, induct type]:
wenzelm@11954
    95
    "(!!a b c. P (a, b, c)) ==> P x"
wenzelm@11954
    96
  by (cases x) blast
wenzelm@11954
    97
wenzelm@11954
    98
lemma prod_cases4 [case_names fields, cases type]:
wenzelm@11954
    99
    "(!!a b c d. y = (a, b, c, d) ==> P) ==> P"
wenzelm@11954
   100
  apply (cases y)
paulson@14208
   101
  apply (case_tac c, blast)
wenzelm@11954
   102
  done
wenzelm@11954
   103
wenzelm@11954
   104
lemma prod_induct4 [case_names fields, induct type]:
wenzelm@11954
   105
    "(!!a b c d. P (a, b, c, d)) ==> P x"
wenzelm@11954
   106
  by (cases x) blast
berghofe@5181
   107
wenzelm@11954
   108
lemma prod_cases5 [case_names fields, cases type]:
wenzelm@11954
   109
    "(!!a b c d e. y = (a, b, c, d, e) ==> P) ==> P"
wenzelm@11954
   110
  apply (cases y)
paulson@14208
   111
  apply (case_tac d, blast)
wenzelm@11954
   112
  done
wenzelm@11954
   113
wenzelm@11954
   114
lemma prod_induct5 [case_names fields, induct type]:
wenzelm@11954
   115
    "(!!a b c d e. P (a, b, c, d, e)) ==> P x"
wenzelm@11954
   116
  by (cases x) blast
wenzelm@11954
   117
wenzelm@11954
   118
lemma prod_cases6 [case_names fields, cases type]:
wenzelm@11954
   119
    "(!!a b c d e f. y = (a, b, c, d, e, f) ==> P) ==> P"
wenzelm@11954
   120
  apply (cases y)
paulson@14208
   121
  apply (case_tac e, blast)
wenzelm@11954
   122
  done
wenzelm@11954
   123
wenzelm@11954
   124
lemma prod_induct6 [case_names fields, induct type]:
wenzelm@11954
   125
    "(!!a b c d e f. P (a, b, c, d, e, f)) ==> P x"
wenzelm@11954
   126
  by (cases x) blast
wenzelm@11954
   127
wenzelm@11954
   128
lemma prod_cases7 [case_names fields, cases type]:
wenzelm@11954
   129
    "(!!a b c d e f g. y = (a, b, c, d, e, f, g) ==> P) ==> P"
wenzelm@11954
   130
  apply (cases y)
paulson@14208
   131
  apply (case_tac f, blast)
wenzelm@11954
   132
  done
wenzelm@11954
   133
wenzelm@11954
   134
lemma prod_induct7 [case_names fields, induct type]:
wenzelm@11954
   135
    "(!!a b c d e f g. P (a, b, c, d, e, f, g)) ==> P x"
wenzelm@11954
   136
  by (cases x) blast
berghofe@5759
   137
wenzelm@12918
   138
wenzelm@12918
   139
subsection {* The option type *}
wenzelm@12918
   140
wenzelm@12918
   141
datatype 'a option = None | Some 'a
wenzelm@12918
   142
nipkow@18576
   143
lemma not_None_eq[iff]: "(x ~= None) = (EX y. x = Some y)"
nipkow@18576
   144
  by (induct x) auto
nipkow@18576
   145
nipkow@18576
   146
lemma not_Some_eq[iff]: "(ALL y. x ~= Some y) = (x = None)"
paulson@18447
   147
  by (induct x) auto
paulson@18447
   148
nipkow@18576
   149
text{*Although it may appear that both of these equalities are helpful
nipkow@18576
   150
only when applied to assumptions, in practice it seems better to give
nipkow@18576
   151
them the uniform iff attribute. *}
wenzelm@12918
   152
nipkow@18576
   153
(*
paulson@18447
   154
lemmas not_None_eq_D = not_None_eq [THEN iffD1]
paulson@18447
   155
declare not_None_eq_D [dest!]
paulson@18447
   156
paulson@18447
   157
lemmas not_Some_eq_D = not_Some_eq [THEN iffD1]
paulson@18447
   158
declare not_Some_eq_D [dest!]
nipkow@18576
   159
*)
wenzelm@12918
   160
wenzelm@12918
   161
lemma option_caseE:
wenzelm@12918
   162
  "(case x of None => P | Some y => Q y) ==>
wenzelm@12918
   163
    (x = None ==> P ==> R) ==>
wenzelm@12918
   164
    (!!y. x = Some y ==> Q y ==> R) ==> R"
wenzelm@12918
   165
  by (cases x) simp_all
wenzelm@12918
   166
wenzelm@12918
   167
wenzelm@12918
   168
subsubsection {* Operations *}
wenzelm@12918
   169
wenzelm@12918
   170
consts
wenzelm@12918
   171
  the :: "'a option => 'a"
wenzelm@12918
   172
primrec
wenzelm@12918
   173
  "the (Some x) = x"
wenzelm@12918
   174
wenzelm@12918
   175
consts
wenzelm@12918
   176
  o2s :: "'a option => 'a set"
wenzelm@12918
   177
primrec
wenzelm@12918
   178
  "o2s None = {}"
wenzelm@12918
   179
  "o2s (Some x) = {x}"
wenzelm@12918
   180
wenzelm@12918
   181
lemma ospec [dest]: "(ALL x:o2s A. P x) ==> A = Some x ==> P x"
wenzelm@12918
   182
  by simp
wenzelm@12918
   183
wenzelm@17876
   184
ML_setup {* change_claset (fn cs => cs addSD2 ("ospec", thm "ospec")) *}
wenzelm@12918
   185
wenzelm@12918
   186
lemma elem_o2s [iff]: "(x : o2s xo) = (xo = Some x)"
wenzelm@12918
   187
  by (cases xo) auto
wenzelm@12918
   188
wenzelm@12918
   189
lemma o2s_empty_eq [simp]: "(o2s xo = {}) = (xo = None)"
wenzelm@12918
   190
  by (cases xo) auto
wenzelm@12918
   191
wenzelm@12918
   192
wenzelm@12918
   193
constdefs
wenzelm@12918
   194
  option_map :: "('a => 'b) => ('a option => 'b option)"
wenzelm@12918
   195
  "option_map == %f y. case y of None => None | Some x => Some (f x)"
wenzelm@12918
   196
wenzelm@12918
   197
lemma option_map_None [simp]: "option_map f None = None"
wenzelm@12918
   198
  by (simp add: option_map_def)
wenzelm@12918
   199
wenzelm@12918
   200
lemma option_map_Some [simp]: "option_map f (Some x) = Some (f x)"
wenzelm@12918
   201
  by (simp add: option_map_def)
wenzelm@12918
   202
nipkow@14187
   203
lemma option_map_is_None[iff]:
nipkow@14187
   204
 "(option_map f opt = None) = (opt = None)"
nipkow@14187
   205
by (simp add: option_map_def split add: option.split)
nipkow@14187
   206
wenzelm@12918
   207
lemma option_map_eq_Some [iff]:
wenzelm@12918
   208
    "(option_map f xo = Some y) = (EX z. xo = Some z & f z = y)"
nipkow@14187
   209
by (simp add: option_map_def split add: option.split)
nipkow@14187
   210
nipkow@14187
   211
lemma option_map_comp:
nipkow@14187
   212
 "option_map f (option_map g opt) = option_map (f o g) opt"
nipkow@14187
   213
by (simp add: option_map_def split add: option.split)
wenzelm@12918
   214
wenzelm@12918
   215
lemma option_map_o_sum_case [simp]:
wenzelm@12918
   216
    "option_map f o sum_case g h = sum_case (option_map f o g) (option_map f o h)"
wenzelm@12918
   217
  apply (rule ext)
wenzelm@12918
   218
  apply (simp split add: sum.split)
wenzelm@12918
   219
  done
wenzelm@12918
   220
wenzelm@17458
   221
lemmas [code] = imp_conv_disj
wenzelm@17458
   222
haftmann@18702
   223
subsubsection {* Codegenerator setup *}
haftmann@18702
   224
haftmann@18702
   225
code_syntax_const
haftmann@18702
   226
  True
haftmann@18702
   227
    ml (atom "true")
haftmann@18702
   228
    haskell (atom "True")
haftmann@18702
   229
  False
haftmann@18702
   230
    ml (atom "false")
haftmann@18702
   231
    haskell (atom "False")
haftmann@18702
   232
haftmann@18702
   233
code_syntax_const
haftmann@18702
   234
  Pair
haftmann@18702
   235
    ml (atom "(__,/ __)")
haftmann@18702
   236
    haskell (atom "(__,/ __)")
haftmann@18702
   237
haftmann@18702
   238
code_syntax_const
haftmann@18702
   239
  1 :: "nat"
haftmann@18702
   240
    ml ("{*Suc 0*}")
haftmann@18702
   241
    haskell ("{*Suc 0*}")
haftmann@18702
   242
berghofe@5181
   243
end