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