src/HOL/Datatype.thy
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 24845 abcd15369ffa
child 25511 54db9b5080b8
permissions -rw-r--r--
Name.uu, Name.aT;
     1 (*  Title:      HOL/Datatype.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     5 
     6 Could <*> be generalized to a general summation (Sigma)?
     7 *)
     8 
     9 header {* Analogues of the Cartesian Product and Disjoint Sum for Datatypes *}
    10 
    11 theory Datatype
    12 imports Finite_Set
    13 uses "Tools/datatype_codegen.ML"
    14 begin
    15 
    16 typedef (Node)
    17   ('a,'b) node = "{p. EX f x k. p = (f::nat=>'b+nat, x::'a+nat) & f k = Inr 0}"
    18     --{*it is a subtype of @{text "(nat=>'b+nat) * ('a+nat)"}*}
    19   by auto
    20 
    21 text{*Datatypes will be represented by sets of type @{text node}*}
    22 
    23 types 'a item        = "('a, unit) node set"
    24       ('a, 'b) dtree = "('a, 'b) node set"
    25 
    26 consts
    27   apfst     :: "['a=>'c, 'a*'b] => 'c*'b"
    28   Push      :: "[('b + nat), nat => ('b + nat)] => (nat => ('b + nat))"
    29 
    30   Push_Node :: "[('b + nat), ('a, 'b) node] => ('a, 'b) node"
    31   ndepth    :: "('a, 'b) node => nat"
    32 
    33   Atom      :: "('a + nat) => ('a, 'b) dtree"
    34   Leaf      :: "'a => ('a, 'b) dtree"
    35   Numb      :: "nat => ('a, 'b) dtree"
    36   Scons     :: "[('a, 'b) dtree, ('a, 'b) dtree] => ('a, 'b) dtree"
    37   In0       :: "('a, 'b) dtree => ('a, 'b) dtree"
    38   In1       :: "('a, 'b) dtree => ('a, 'b) dtree"
    39   Lim       :: "('b => ('a, 'b) dtree) => ('a, 'b) dtree"
    40 
    41   ntrunc    :: "[nat, ('a, 'b) dtree] => ('a, 'b) dtree"
    42 
    43   uprod     :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    44   usum      :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    45 
    46   Split     :: "[[('a, 'b) dtree, ('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    47   Case      :: "[[('a, 'b) dtree]=>'c, [('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    48 
    49   dprod     :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    50                 => (('a, 'b) dtree * ('a, 'b) dtree)set"
    51   dsum      :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    52                 => (('a, 'b) dtree * ('a, 'b) dtree)set"
    53 
    54 
    55 defs
    56 
    57   Push_Node_def:  "Push_Node == (%n x. Abs_Node (apfst (Push n) (Rep_Node x)))"
    58 
    59   (*crude "lists" of nats -- needed for the constructions*)
    60   apfst_def:  "apfst == (%f (x,y). (f(x),y))"
    61   Push_def:   "Push == (%b h. nat_case b h)"
    62 
    63   (** operations on S-expressions -- sets of nodes **)
    64 
    65   (*S-expression constructors*)
    66   Atom_def:   "Atom == (%x. {Abs_Node((%k. Inr 0, x))})"
    67   Scons_def:  "Scons M N == (Push_Node (Inr 1) ` M) Un (Push_Node (Inr (Suc 1)) ` N)"
    68 
    69   (*Leaf nodes, with arbitrary or nat labels*)
    70   Leaf_def:   "Leaf == Atom o Inl"
    71   Numb_def:   "Numb == Atom o Inr"
    72 
    73   (*Injections of the "disjoint sum"*)
    74   In0_def:    "In0(M) == Scons (Numb 0) M"
    75   In1_def:    "In1(M) == Scons (Numb 1) M"
    76 
    77   (*Function spaces*)
    78   Lim_def: "Lim f == Union {z. ? x. z = Push_Node (Inl x) ` (f x)}"
    79 
    80   (*the set of nodes with depth less than k*)
    81   ndepth_def: "ndepth(n) == (%(f,x). LEAST k. f k = Inr 0) (Rep_Node n)"
    82   ntrunc_def: "ntrunc k N == {n. n:N & ndepth(n)<k}"
    83 
    84   (*products and sums for the "universe"*)
    85   uprod_def:  "uprod A B == UN x:A. UN y:B. { Scons x y }"
    86   usum_def:   "usum A B == In0`A Un In1`B"
    87 
    88   (*the corresponding eliminators*)
    89   Split_def:  "Split c M == THE u. EX x y. M = Scons x y & u = c x y"
    90 
    91   Case_def:   "Case c d M == THE u.  (EX x . M = In0(x) & u = c(x))
    92                                   | (EX y . M = In1(y) & u = d(y))"
    93 
    94 
    95   (** equality for the "universe" **)
    96 
    97   dprod_def:  "dprod r s == UN (x,x'):r. UN (y,y'):s. {(Scons x y, Scons x' y')}"
    98 
    99   dsum_def:   "dsum r s == (UN (x,x'):r. {(In0(x),In0(x'))}) Un
   100                           (UN (y,y'):s. {(In1(y),In1(y'))})"
   101 
   102 
   103 
   104 (** apfst -- can be used in similar type definitions **)
   105 
   106 lemma apfst_conv [simp, code]: "apfst f (a, b) = (f a, b)"
   107 by (simp add: apfst_def)
   108 
   109 
   110 lemma apfst_convE: 
   111     "[| q = apfst f p;  !!x y. [| p = (x,y);  q = (f(x),y) |] ==> R  
   112      |] ==> R"
   113 by (force simp add: apfst_def)
   114 
   115 (** Push -- an injection, analogous to Cons on lists **)
   116 
   117 lemma Push_inject1: "Push i f = Push j g  ==> i=j"
   118 apply (simp add: Push_def expand_fun_eq) 
   119 apply (drule_tac x=0 in spec, simp) 
   120 done
   121 
   122 lemma Push_inject2: "Push i f = Push j g  ==> f=g"
   123 apply (auto simp add: Push_def expand_fun_eq) 
   124 apply (drule_tac x="Suc x" in spec, simp) 
   125 done
   126 
   127 lemma Push_inject:
   128     "[| Push i f =Push j g;  [| i=j;  f=g |] ==> P |] ==> P"
   129 by (blast dest: Push_inject1 Push_inject2) 
   130 
   131 lemma Push_neq_K0: "Push (Inr (Suc k)) f = (%z. Inr 0) ==> P"
   132 by (auto simp add: Push_def expand_fun_eq split: nat.split_asm)
   133 
   134 lemmas Abs_Node_inj = Abs_Node_inject [THEN [2] rev_iffD1, standard]
   135 
   136 
   137 (*** Introduction rules for Node ***)
   138 
   139 lemma Node_K0_I: "(%k. Inr 0, a) : Node"
   140 by (simp add: Node_def)
   141 
   142 lemma Node_Push_I: "p: Node ==> apfst (Push i) p : Node"
   143 apply (simp add: Node_def Push_def) 
   144 apply (fast intro!: apfst_conv nat_case_Suc [THEN trans])
   145 done
   146 
   147 
   148 subsection{*Freeness: Distinctness of Constructors*}
   149 
   150 (** Scons vs Atom **)
   151 
   152 lemma Scons_not_Atom [iff]: "Scons M N \<noteq> Atom(a)"
   153 apply (simp add: Atom_def Scons_def Push_Node_def One_nat_def)
   154 apply (blast intro: Node_K0_I Rep_Node [THEN Node_Push_I] 
   155          dest!: Abs_Node_inj 
   156          elim!: apfst_convE sym [THEN Push_neq_K0])  
   157 done
   158 
   159 lemmas Atom_not_Scons [iff] = Scons_not_Atom [THEN not_sym, standard]
   160 
   161 
   162 (*** Injectiveness ***)
   163 
   164 (** Atomic nodes **)
   165 
   166 lemma inj_Atom: "inj(Atom)"
   167 apply (simp add: Atom_def)
   168 apply (blast intro!: inj_onI Node_K0_I dest!: Abs_Node_inj)
   169 done
   170 lemmas Atom_inject = inj_Atom [THEN injD, standard]
   171 
   172 lemma Atom_Atom_eq [iff]: "(Atom(a)=Atom(b)) = (a=b)"
   173 by (blast dest!: Atom_inject)
   174 
   175 lemma inj_Leaf: "inj(Leaf)"
   176 apply (simp add: Leaf_def o_def)
   177 apply (rule inj_onI)
   178 apply (erule Atom_inject [THEN Inl_inject])
   179 done
   180 
   181 lemmas Leaf_inject [dest!] = inj_Leaf [THEN injD, standard]
   182 
   183 lemma inj_Numb: "inj(Numb)"
   184 apply (simp add: Numb_def o_def)
   185 apply (rule inj_onI)
   186 apply (erule Atom_inject [THEN Inr_inject])
   187 done
   188 
   189 lemmas Numb_inject [dest!] = inj_Numb [THEN injD, standard]
   190 
   191 
   192 (** Injectiveness of Push_Node **)
   193 
   194 lemma Push_Node_inject:
   195     "[| Push_Node i m =Push_Node j n;  [| i=j;  m=n |] ==> P  
   196      |] ==> P"
   197 apply (simp add: Push_Node_def)
   198 apply (erule Abs_Node_inj [THEN apfst_convE])
   199 apply (rule Rep_Node [THEN Node_Push_I])+
   200 apply (erule sym [THEN apfst_convE]) 
   201 apply (blast intro: Rep_Node_inject [THEN iffD1] trans sym elim!: Push_inject)
   202 done
   203 
   204 
   205 (** Injectiveness of Scons **)
   206 
   207 lemma Scons_inject_lemma1: "Scons M N <= Scons M' N' ==> M<=M'"
   208 apply (simp add: Scons_def One_nat_def)
   209 apply (blast dest!: Push_Node_inject)
   210 done
   211 
   212 lemma Scons_inject_lemma2: "Scons M N <= Scons M' N' ==> N<=N'"
   213 apply (simp add: Scons_def One_nat_def)
   214 apply (blast dest!: Push_Node_inject)
   215 done
   216 
   217 lemma Scons_inject1: "Scons M N = Scons M' N' ==> M=M'"
   218 apply (erule equalityE)
   219 apply (iprover intro: equalityI Scons_inject_lemma1)
   220 done
   221 
   222 lemma Scons_inject2: "Scons M N = Scons M' N' ==> N=N'"
   223 apply (erule equalityE)
   224 apply (iprover intro: equalityI Scons_inject_lemma2)
   225 done
   226 
   227 lemma Scons_inject:
   228     "[| Scons M N = Scons M' N';  [| M=M';  N=N' |] ==> P |] ==> P"
   229 by (iprover dest: Scons_inject1 Scons_inject2)
   230 
   231 lemma Scons_Scons_eq [iff]: "(Scons M N = Scons M' N') = (M=M' & N=N')"
   232 by (blast elim!: Scons_inject)
   233 
   234 (*** Distinctness involving Leaf and Numb ***)
   235 
   236 (** Scons vs Leaf **)
   237 
   238 lemma Scons_not_Leaf [iff]: "Scons M N \<noteq> Leaf(a)"
   239 by (simp add: Leaf_def o_def Scons_not_Atom)
   240 
   241 lemmas Leaf_not_Scons  [iff] = Scons_not_Leaf [THEN not_sym, standard]
   242 
   243 (** Scons vs Numb **)
   244 
   245 lemma Scons_not_Numb [iff]: "Scons M N \<noteq> Numb(k)"
   246 by (simp add: Numb_def o_def Scons_not_Atom)
   247 
   248 lemmas Numb_not_Scons [iff] = Scons_not_Numb [THEN not_sym, standard]
   249 
   250 
   251 (** Leaf vs Numb **)
   252 
   253 lemma Leaf_not_Numb [iff]: "Leaf(a) \<noteq> Numb(k)"
   254 by (simp add: Leaf_def Numb_def)
   255 
   256 lemmas Numb_not_Leaf [iff] = Leaf_not_Numb [THEN not_sym, standard]
   257 
   258 
   259 (*** ndepth -- the depth of a node ***)
   260 
   261 lemma ndepth_K0: "ndepth (Abs_Node(%k. Inr 0, x)) = 0"
   262 by (simp add: ndepth_def  Node_K0_I [THEN Abs_Node_inverse] Least_equality)
   263 
   264 lemma ndepth_Push_Node_aux:
   265      "nat_case (Inr (Suc i)) f k = Inr 0 --> Suc(LEAST x. f x = Inr 0) <= k"
   266 apply (induct_tac "k", auto)
   267 apply (erule Least_le)
   268 done
   269 
   270 lemma ndepth_Push_Node: 
   271     "ndepth (Push_Node (Inr (Suc i)) n) = Suc(ndepth(n))"
   272 apply (insert Rep_Node [of n, unfolded Node_def])
   273 apply (auto simp add: ndepth_def Push_Node_def
   274                  Rep_Node [THEN Node_Push_I, THEN Abs_Node_inverse])
   275 apply (rule Least_equality)
   276 apply (auto simp add: Push_def ndepth_Push_Node_aux)
   277 apply (erule LeastI)
   278 done
   279 
   280 
   281 (*** ntrunc applied to the various node sets ***)
   282 
   283 lemma ntrunc_0 [simp]: "ntrunc 0 M = {}"
   284 by (simp add: ntrunc_def)
   285 
   286 lemma ntrunc_Atom [simp]: "ntrunc (Suc k) (Atom a) = Atom(a)"
   287 by (auto simp add: Atom_def ntrunc_def ndepth_K0)
   288 
   289 lemma ntrunc_Leaf [simp]: "ntrunc (Suc k) (Leaf a) = Leaf(a)"
   290 by (simp add: Leaf_def o_def ntrunc_Atom)
   291 
   292 lemma ntrunc_Numb [simp]: "ntrunc (Suc k) (Numb i) = Numb(i)"
   293 by (simp add: Numb_def o_def ntrunc_Atom)
   294 
   295 lemma ntrunc_Scons [simp]: 
   296     "ntrunc (Suc k) (Scons M N) = Scons (ntrunc k M) (ntrunc k N)"
   297 by (auto simp add: Scons_def ntrunc_def One_nat_def ndepth_Push_Node) 
   298 
   299 
   300 
   301 (** Injection nodes **)
   302 
   303 lemma ntrunc_one_In0 [simp]: "ntrunc (Suc 0) (In0 M) = {}"
   304 apply (simp add: In0_def)
   305 apply (simp add: Scons_def)
   306 done
   307 
   308 lemma ntrunc_In0 [simp]: "ntrunc (Suc(Suc k)) (In0 M) = In0 (ntrunc (Suc k) M)"
   309 by (simp add: In0_def)
   310 
   311 lemma ntrunc_one_In1 [simp]: "ntrunc (Suc 0) (In1 M) = {}"
   312 apply (simp add: In1_def)
   313 apply (simp add: Scons_def)
   314 done
   315 
   316 lemma ntrunc_In1 [simp]: "ntrunc (Suc(Suc k)) (In1 M) = In1 (ntrunc (Suc k) M)"
   317 by (simp add: In1_def)
   318 
   319 
   320 subsection{*Set Constructions*}
   321 
   322 
   323 (*** Cartesian Product ***)
   324 
   325 lemma uprodI [intro!]: "[| M:A;  N:B |] ==> Scons M N : uprod A B"
   326 by (simp add: uprod_def)
   327 
   328 (*The general elimination rule*)
   329 lemma uprodE [elim!]:
   330     "[| c : uprod A B;   
   331         !!x y. [| x:A;  y:B;  c = Scons x y |] ==> P  
   332      |] ==> P"
   333 by (auto simp add: uprod_def) 
   334 
   335 
   336 (*Elimination of a pair -- introduces no eigenvariables*)
   337 lemma uprodE2: "[| Scons M N : uprod A B;  [| M:A;  N:B |] ==> P |] ==> P"
   338 by (auto simp add: uprod_def)
   339 
   340 
   341 (*** Disjoint Sum ***)
   342 
   343 lemma usum_In0I [intro]: "M:A ==> In0(M) : usum A B"
   344 by (simp add: usum_def)
   345 
   346 lemma usum_In1I [intro]: "N:B ==> In1(N) : usum A B"
   347 by (simp add: usum_def)
   348 
   349 lemma usumE [elim!]: 
   350     "[| u : usum A B;   
   351         !!x. [| x:A;  u=In0(x) |] ==> P;  
   352         !!y. [| y:B;  u=In1(y) |] ==> P  
   353      |] ==> P"
   354 by (auto simp add: usum_def)
   355 
   356 
   357 (** Injection **)
   358 
   359 lemma In0_not_In1 [iff]: "In0(M) \<noteq> In1(N)"
   360 by (auto simp add: In0_def In1_def One_nat_def)
   361 
   362 lemmas In1_not_In0 [iff] = In0_not_In1 [THEN not_sym, standard]
   363 
   364 lemma In0_inject: "In0(M) = In0(N) ==>  M=N"
   365 by (simp add: In0_def)
   366 
   367 lemma In1_inject: "In1(M) = In1(N) ==>  M=N"
   368 by (simp add: In1_def)
   369 
   370 lemma In0_eq [iff]: "(In0 M = In0 N) = (M=N)"
   371 by (blast dest!: In0_inject)
   372 
   373 lemma In1_eq [iff]: "(In1 M = In1 N) = (M=N)"
   374 by (blast dest!: In1_inject)
   375 
   376 lemma inj_In0: "inj In0"
   377 by (blast intro!: inj_onI)
   378 
   379 lemma inj_In1: "inj In1"
   380 by (blast intro!: inj_onI)
   381 
   382 
   383 (*** Function spaces ***)
   384 
   385 lemma Lim_inject: "Lim f = Lim g ==> f = g"
   386 apply (simp add: Lim_def)
   387 apply (rule ext)
   388 apply (blast elim!: Push_Node_inject)
   389 done
   390 
   391 
   392 (*** proving equality of sets and functions using ntrunc ***)
   393 
   394 lemma ntrunc_subsetI: "ntrunc k M <= M"
   395 by (auto simp add: ntrunc_def)
   396 
   397 lemma ntrunc_subsetD: "(!!k. ntrunc k M <= N) ==> M<=N"
   398 by (auto simp add: ntrunc_def)
   399 
   400 (*A generalized form of the take-lemma*)
   401 lemma ntrunc_equality: "(!!k. ntrunc k M = ntrunc k N) ==> M=N"
   402 apply (rule equalityI)
   403 apply (rule_tac [!] ntrunc_subsetD)
   404 apply (rule_tac [!] ntrunc_subsetI [THEN [2] subset_trans], auto) 
   405 done
   406 
   407 lemma ntrunc_o_equality: 
   408     "[| !!k. (ntrunc(k) o h1) = (ntrunc(k) o h2) |] ==> h1=h2"
   409 apply (rule ntrunc_equality [THEN ext])
   410 apply (simp add: expand_fun_eq) 
   411 done
   412 
   413 
   414 (*** Monotonicity ***)
   415 
   416 lemma uprod_mono: "[| A<=A';  B<=B' |] ==> uprod A B <= uprod A' B'"
   417 by (simp add: uprod_def, blast)
   418 
   419 lemma usum_mono: "[| A<=A';  B<=B' |] ==> usum A B <= usum A' B'"
   420 by (simp add: usum_def, blast)
   421 
   422 lemma Scons_mono: "[| M<=M';  N<=N' |] ==> Scons M N <= Scons M' N'"
   423 by (simp add: Scons_def, blast)
   424 
   425 lemma In0_mono: "M<=N ==> In0(M) <= In0(N)"
   426 by (simp add: In0_def subset_refl Scons_mono)
   427 
   428 lemma In1_mono: "M<=N ==> In1(M) <= In1(N)"
   429 by (simp add: In1_def subset_refl Scons_mono)
   430 
   431 
   432 (*** Split and Case ***)
   433 
   434 lemma Split [simp]: "Split c (Scons M N) = c M N"
   435 by (simp add: Split_def)
   436 
   437 lemma Case_In0 [simp]: "Case c d (In0 M) = c(M)"
   438 by (simp add: Case_def)
   439 
   440 lemma Case_In1 [simp]: "Case c d (In1 N) = d(N)"
   441 by (simp add: Case_def)
   442 
   443 
   444 
   445 (**** UN x. B(x) rules ****)
   446 
   447 lemma ntrunc_UN1: "ntrunc k (UN x. f(x)) = (UN x. ntrunc k (f x))"
   448 by (simp add: ntrunc_def, blast)
   449 
   450 lemma Scons_UN1_x: "Scons (UN x. f x) M = (UN x. Scons (f x) M)"
   451 by (simp add: Scons_def, blast)
   452 
   453 lemma Scons_UN1_y: "Scons M (UN x. f x) = (UN x. Scons M (f x))"
   454 by (simp add: Scons_def, blast)
   455 
   456 lemma In0_UN1: "In0(UN x. f(x)) = (UN x. In0(f(x)))"
   457 by (simp add: In0_def Scons_UN1_y)
   458 
   459 lemma In1_UN1: "In1(UN x. f(x)) = (UN x. In1(f(x)))"
   460 by (simp add: In1_def Scons_UN1_y)
   461 
   462 
   463 (*** Equality for Cartesian Product ***)
   464 
   465 lemma dprodI [intro!]: 
   466     "[| (M,M'):r;  (N,N'):s |] ==> (Scons M N, Scons M' N') : dprod r s"
   467 by (auto simp add: dprod_def)
   468 
   469 (*The general elimination rule*)
   470 lemma dprodE [elim!]: 
   471     "[| c : dprod r s;   
   472         !!x y x' y'. [| (x,x') : r;  (y,y') : s;  
   473                         c = (Scons x y, Scons x' y') |] ==> P  
   474      |] ==> P"
   475 by (auto simp add: dprod_def)
   476 
   477 
   478 (*** Equality for Disjoint Sum ***)
   479 
   480 lemma dsum_In0I [intro]: "(M,M'):r ==> (In0(M), In0(M')) : dsum r s"
   481 by (auto simp add: dsum_def)
   482 
   483 lemma dsum_In1I [intro]: "(N,N'):s ==> (In1(N), In1(N')) : dsum r s"
   484 by (auto simp add: dsum_def)
   485 
   486 lemma dsumE [elim!]: 
   487     "[| w : dsum r s;   
   488         !!x x'. [| (x,x') : r;  w = (In0(x), In0(x')) |] ==> P;  
   489         !!y y'. [| (y,y') : s;  w = (In1(y), In1(y')) |] ==> P  
   490      |] ==> P"
   491 by (auto simp add: dsum_def)
   492 
   493 
   494 (*** Monotonicity ***)
   495 
   496 lemma dprod_mono: "[| r<=r';  s<=s' |] ==> dprod r s <= dprod r' s'"
   497 by blast
   498 
   499 lemma dsum_mono: "[| r<=r';  s<=s' |] ==> dsum r s <= dsum r' s'"
   500 by blast
   501 
   502 
   503 (*** Bounding theorems ***)
   504 
   505 lemma dprod_Sigma: "(dprod (A <*> B) (C <*> D)) <= (uprod A C) <*> (uprod B D)"
   506 by blast
   507 
   508 lemmas dprod_subset_Sigma = subset_trans [OF dprod_mono dprod_Sigma, standard]
   509 
   510 (*Dependent version*)
   511 lemma dprod_subset_Sigma2:
   512      "(dprod (Sigma A B) (Sigma C D)) <= 
   513       Sigma (uprod A C) (Split (%x y. uprod (B x) (D y)))"
   514 by auto
   515 
   516 lemma dsum_Sigma: "(dsum (A <*> B) (C <*> D)) <= (usum A C) <*> (usum B D)"
   517 by blast
   518 
   519 lemmas dsum_subset_Sigma = subset_trans [OF dsum_mono dsum_Sigma, standard]
   520 
   521 
   522 (*** Domain ***)
   523 
   524 lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
   525 by auto
   526 
   527 lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
   528 by auto
   529 
   530 
   531 text {* hides popular names *}
   532 hide (open) type node item
   533 hide (open) const Push Node Atom Leaf Numb Lim Split Case
   534 
   535 
   536 section {* Datatypes *}
   537 
   538 subsection {* Representing sums *}
   539 
   540 rep_datatype sum
   541   distinct Inl_not_Inr Inr_not_Inl
   542   inject Inl_eq Inr_eq
   543   induction sum_induct
   544 
   545 lemma size_sum [code func]:
   546   "size (x \<Colon> 'a + 'b) = 0" by (cases x) auto
   547 
   548 lemma sum_case_KK[simp]: "sum_case (%x. a) (%x. a) = (%x. a)"
   549   by (rule ext) (simp split: sum.split)
   550 
   551 lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)"
   552   apply (rule_tac s = s in sumE)
   553    apply (erule ssubst)
   554    apply (rule sum.cases(1))
   555   apply (erule ssubst)
   556   apply (rule sum.cases(2))
   557   done
   558 
   559 lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t"
   560   -- {* Prevents simplification of @{text f} and @{text g}: much faster. *}
   561   by simp
   562 
   563 lemma sum_case_inject:
   564   "sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P"
   565 proof -
   566   assume a: "sum_case f1 f2 = sum_case g1 g2"
   567   assume r: "f1 = g1 ==> f2 = g2 ==> P"
   568   show P
   569     apply (rule r)
   570      apply (rule ext)
   571      apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp)
   572     apply (rule ext)
   573     apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp)
   574     done
   575 qed
   576 
   577 constdefs
   578   Suml :: "('a => 'c) => 'a + 'b => 'c"
   579   "Suml == (%f. sum_case f arbitrary)"
   580 
   581   Sumr :: "('b => 'c) => 'a + 'b => 'c"
   582   "Sumr == sum_case arbitrary"
   583 
   584 lemma Suml_inject: "Suml f = Suml g ==> f = g"
   585   by (unfold Suml_def) (erule sum_case_inject)
   586 
   587 lemma Sumr_inject: "Sumr f = Sumr g ==> f = g"
   588   by (unfold Sumr_def) (erule sum_case_inject)
   589 
   590 hide (open) const Suml Sumr
   591 
   592 
   593 subsection {* The option datatype *}
   594 
   595 datatype 'a option = None | Some 'a
   596 
   597 lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
   598   by (induct x) auto
   599 
   600 lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
   601   by (induct x) auto
   602 
   603 text{*Although it may appear that both of these equalities are helpful
   604 only when applied to assumptions, in practice it seems better to give
   605 them the uniform iff attribute. *}
   606 
   607 lemma option_caseE:
   608   assumes c: "(case x of None => P | Some y => Q y)"
   609   obtains
   610     (None) "x = None" and P
   611   | (Some) y where "x = Some y" and "Q y"
   612   using c by (cases x) simp_all
   613 
   614 lemma insert_None_conv_UNIV: "insert None (range Some) = UNIV"
   615   by (rule set_ext, case_tac x) auto
   616 
   617 instance option :: (finite) finite
   618 proof
   619   have "finite (UNIV :: 'a set)" by (rule finite)
   620   hence "finite (insert None (Some ` (UNIV :: 'a set)))" by simp
   621   also have "insert None (Some ` (UNIV :: 'a set)) = UNIV"
   622     by (rule insert_None_conv_UNIV)
   623   finally show "finite (UNIV :: 'a option set)" .
   624 qed
   625 
   626 lemma univ_option [noatp, code func]:
   627   "UNIV = insert (None \<Colon> 'a\<Colon>finite option) (image Some UNIV)"
   628   unfolding insert_None_conv_UNIV ..
   629 
   630 
   631 subsubsection {* Operations *}
   632 
   633 consts
   634   the :: "'a option => 'a"
   635 primrec
   636   "the (Some x) = x"
   637 
   638 consts
   639   o2s :: "'a option => 'a set"
   640 primrec
   641   "o2s None = {}"
   642   "o2s (Some x) = {x}"
   643 
   644 lemma ospec [dest]: "(ALL x:o2s A. P x) ==> A = Some x ==> P x"
   645   by simp
   646 
   647 ML_setup {* change_claset (fn cs => cs addSD2 ("ospec", thm "ospec")) *}
   648 
   649 lemma elem_o2s [iff]: "(x : o2s xo) = (xo = Some x)"
   650   by (cases xo) auto
   651 
   652 lemma o2s_empty_eq [simp]: "(o2s xo = {}) = (xo = None)"
   653   by (cases xo) auto
   654 
   655 constdefs
   656   option_map :: "('a => 'b) => ('a option => 'b option)"
   657   "option_map == %f y. case y of None => None | Some x => Some (f x)"
   658 
   659 lemmas [code func del] = option_map_def
   660 
   661 lemma option_map_None [simp, code]: "option_map f None = None"
   662   by (simp add: option_map_def)
   663 
   664 lemma option_map_Some [simp, code]: "option_map f (Some x) = Some (f x)"
   665   by (simp add: option_map_def)
   666 
   667 lemma option_map_is_None [iff]:
   668     "(option_map f opt = None) = (opt = None)"
   669   by (simp add: option_map_def split add: option.split)
   670 
   671 lemma option_map_eq_Some [iff]:
   672     "(option_map f xo = Some y) = (EX z. xo = Some z & f z = y)"
   673   by (simp add: option_map_def split add: option.split)
   674 
   675 lemma option_map_comp:
   676     "option_map f (option_map g opt) = option_map (f o g) opt"
   677   by (simp add: option_map_def split add: option.split)
   678 
   679 lemma option_map_o_sum_case [simp]:
   680     "option_map f o sum_case g h = sum_case (option_map f o g) (option_map f o h)"
   681   by (rule ext) (simp split: sum.split)
   682 
   683 
   684 subsubsection {* Code generator setup *}
   685 
   686 setup DatatypeCodegen.setup
   687 
   688 definition
   689   is_none :: "'a option \<Rightarrow> bool" where
   690   is_none_none [code post, symmetric, code inline]: "is_none x \<longleftrightarrow> x = None"
   691 
   692 lemma is_none_code [code]:
   693   shows "is_none None \<longleftrightarrow> True"
   694     and "is_none (Some x) \<longleftrightarrow> False"
   695   unfolding is_none_none [symmetric] by simp_all
   696 
   697 hide (open) const is_none
   698 
   699 code_type option
   700   (SML "_ option")
   701   (OCaml "_ option")
   702   (Haskell "Maybe _")
   703 
   704 code_const None and Some
   705   (SML "NONE" and "SOME")
   706   (OCaml "None" and "Some _")
   707   (Haskell "Nothing" and "Just")
   708 
   709 code_instance option :: eq
   710   (Haskell -)
   711 
   712 code_const "op = \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
   713   (Haskell infixl 4 "==")
   714 
   715 code_reserved SML
   716   option NONE SOME
   717 
   718 code_reserved OCaml
   719   option None Some
   720 
   721 code_modulename SML
   722   Datatype Nat
   723 
   724 code_modulename OCaml
   725   Datatype Nat
   726 
   727 code_modulename Haskell
   728   Datatype Nat
   729 
   730 end