src/HOL/Datatype.thy
author paulson
Fri Sep 26 10:34:57 2003 +0200 (2003-09-26)
changeset 14208 144f45277d5a
parent 14187 26dfcd0ac436
child 14274 0cb8a9a144d2
permissions -rw-r--r--
misc tidying
     1 (*  Title:      HOL/Datatype.thy
     2     ID:         $Id$
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 *)
     6 
     7 header {* Datatypes *}
     8 
     9 theory Datatype = Datatype_Universe:
    10 
    11 subsection {* Representing primitive types *}
    12 
    13 rep_datatype bool
    14   distinct True_not_False False_not_True
    15   induction bool_induct
    16 
    17 declare case_split [cases type: bool]
    18   -- "prefer plain propositional version"
    19 
    20 rep_datatype unit
    21   induction unit_induct
    22 
    23 rep_datatype prod
    24   inject Pair_eq
    25   induction prod_induct
    26 
    27 rep_datatype sum
    28   distinct Inl_not_Inr Inr_not_Inl
    29   inject Inl_eq Inr_eq
    30   induction sum_induct
    31 
    32 ML {*
    33   val [sum_case_Inl, sum_case_Inr] = thms "sum.cases";
    34   bind_thm ("sum_case_Inl", sum_case_Inl);
    35   bind_thm ("sum_case_Inr", sum_case_Inr);
    36 *} -- {* compatibility *}
    37 
    38 lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)"
    39   apply (rule_tac s = s in sumE)
    40    apply (erule ssubst)
    41    apply (rule sum_case_Inl)
    42   apply (erule ssubst)
    43   apply (rule sum_case_Inr)
    44   done
    45 
    46 lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t"
    47   -- {* Prevents simplification of @{text f} and @{text g}: much faster. *}
    48   by (erule arg_cong)
    49 
    50 lemma sum_case_inject:
    51   "sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P"
    52 proof -
    53   assume a: "sum_case f1 f2 = sum_case g1 g2"
    54   assume r: "f1 = g1 ==> f2 = g2 ==> P"
    55   show P
    56     apply (rule r)
    57      apply (rule ext)
    58      apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp)
    59     apply (rule ext)
    60     apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp)
    61     done
    62 qed
    63 
    64 constdefs
    65   Suml :: "('a => 'c) => 'a + 'b => 'c"
    66   "Suml == (%f. sum_case f arbitrary)"
    67 
    68   Sumr :: "('b => 'c) => 'a + 'b => 'c"
    69   "Sumr == sum_case arbitrary"
    70 
    71 lemma Suml_inject: "Suml f = Suml g ==> f = g"
    72   by (unfold Suml_def) (erule sum_case_inject)
    73 
    74 lemma Sumr_inject: "Sumr f = Sumr g ==> f = g"
    75   by (unfold Sumr_def) (erule sum_case_inject)
    76 
    77 
    78 subsection {* Finishing the datatype package setup *}
    79 
    80 text {* Belongs to theory @{text Datatype_Universe}; hides popular names. *}
    81 hide const Node Atom Leaf Numb Lim Split Case Suml Sumr
    82 hide type node item
    83 
    84 
    85 subsection {* Further cases/induct rules for tuples *}
    86 
    87 lemma prod_cases3 [case_names fields, cases type]:
    88     "(!!a b c. y = (a, b, c) ==> P) ==> P"
    89   apply (cases y)
    90   apply (case_tac b, blast)
    91   done
    92 
    93 lemma prod_induct3 [case_names fields, induct type]:
    94     "(!!a b c. P (a, b, c)) ==> P x"
    95   by (cases x) blast
    96 
    97 lemma prod_cases4 [case_names fields, cases type]:
    98     "(!!a b c d. y = (a, b, c, d) ==> P) ==> P"
    99   apply (cases y)
   100   apply (case_tac c, blast)
   101   done
   102 
   103 lemma prod_induct4 [case_names fields, induct type]:
   104     "(!!a b c d. P (a, b, c, d)) ==> P x"
   105   by (cases x) blast
   106 
   107 lemma prod_cases5 [case_names fields, cases type]:
   108     "(!!a b c d e. y = (a, b, c, d, e) ==> P) ==> P"
   109   apply (cases y)
   110   apply (case_tac d, blast)
   111   done
   112 
   113 lemma prod_induct5 [case_names fields, induct type]:
   114     "(!!a b c d e. P (a, b, c, d, e)) ==> P x"
   115   by (cases x) blast
   116 
   117 lemma prod_cases6 [case_names fields, cases type]:
   118     "(!!a b c d e f. y = (a, b, c, d, e, f) ==> P) ==> P"
   119   apply (cases y)
   120   apply (case_tac e, blast)
   121   done
   122 
   123 lemma prod_induct6 [case_names fields, induct type]:
   124     "(!!a b c d e f. P (a, b, c, d, e, f)) ==> P x"
   125   by (cases x) blast
   126 
   127 lemma prod_cases7 [case_names fields, cases type]:
   128     "(!!a b c d e f g. y = (a, b, c, d, e, f, g) ==> P) ==> P"
   129   apply (cases y)
   130   apply (case_tac f, blast)
   131   done
   132 
   133 lemma prod_induct7 [case_names fields, induct type]:
   134     "(!!a b c d e f g. P (a, b, c, d, e, f, g)) ==> P x"
   135   by (cases x) blast
   136 
   137 
   138 subsection {* The option type *}
   139 
   140 datatype 'a option = None | Some 'a
   141 
   142 lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
   143   by (induct x) auto
   144 
   145 lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
   146   by (induct x) auto
   147 
   148 lemma option_caseE:
   149   "(case x of None => P | Some y => Q y) ==>
   150     (x = None ==> P ==> R) ==>
   151     (!!y. x = Some y ==> Q y ==> R) ==> R"
   152   by (cases x) simp_all
   153 
   154 
   155 subsubsection {* Operations *}
   156 
   157 consts
   158   the :: "'a option => 'a"
   159 primrec
   160   "the (Some x) = x"
   161 
   162 consts
   163   o2s :: "'a option => 'a set"
   164 primrec
   165   "o2s None = {}"
   166   "o2s (Some x) = {x}"
   167 
   168 lemma ospec [dest]: "(ALL x:o2s A. P x) ==> A = Some x ==> P x"
   169   by simp
   170 
   171 ML_setup {* claset_ref() := claset() addSD2 ("ospec", thm "ospec") *}
   172 
   173 lemma elem_o2s [iff]: "(x : o2s xo) = (xo = Some x)"
   174   by (cases xo) auto
   175 
   176 lemma o2s_empty_eq [simp]: "(o2s xo = {}) = (xo = None)"
   177   by (cases xo) auto
   178 
   179 
   180 constdefs
   181   option_map :: "('a => 'b) => ('a option => 'b option)"
   182   "option_map == %f y. case y of None => None | Some x => Some (f x)"
   183 
   184 lemma option_map_None [simp]: "option_map f None = None"
   185   by (simp add: option_map_def)
   186 
   187 lemma option_map_Some [simp]: "option_map f (Some x) = Some (f x)"
   188   by (simp add: option_map_def)
   189 
   190 lemma option_map_is_None[iff]:
   191  "(option_map f opt = None) = (opt = None)"
   192 by (simp add: option_map_def split add: option.split)
   193 
   194 lemma option_map_eq_Some [iff]:
   195     "(option_map f xo = Some y) = (EX z. xo = Some z & f z = y)"
   196 by (simp add: option_map_def split add: option.split)
   197 
   198 lemma option_map_comp:
   199  "option_map f (option_map g opt) = option_map (f o g) opt"
   200 by (simp add: option_map_def split add: option.split)
   201 
   202 lemma option_map_o_sum_case [simp]:
   203     "option_map f o sum_case g h = sum_case (option_map f o g) (option_map f o h)"
   204   apply (rule ext)
   205   apply (simp split add: sum.split)
   206   done
   207 
   208 end