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