merged with theory Datatype_Universe;
authorwenzelm
Sun Oct 01 22:19:21 2006 +0200 (2006-10-01)
changeset 20819cb6ae81dd0be
parent 20818 cb7ec413f95d
child 20820 58693343905f
merged with theory Datatype_Universe;
src/HOL/Datatype.thy
     1.1 --- a/src/HOL/Datatype.thy	Sun Oct 01 18:30:04 2006 +0200
     1.2 +++ b/src/HOL/Datatype.thy	Sun Oct 01 22:19:21 2006 +0200
     1.3 @@ -1,14 +1,548 @@
     1.4  (*  Title:      HOL/Datatype.thy
     1.5      ID:         $Id$
     1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7      Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     1.8 +
     1.9 +Could <*> be generalized to a general summation (Sigma)?
    1.10  *)
    1.11  
    1.12 -header {* Datatypes *}
    1.13 +header{*Analogues of the Cartesian Product and Disjoint Sum for Datatypes*}
    1.14  
    1.15  theory Datatype
    1.16 -imports Datatype_Universe
    1.17 +imports NatArith Sum_Type
    1.18  begin
    1.19  
    1.20 +
    1.21 +typedef (Node)
    1.22 +  ('a,'b) node = "{p. EX f x k. p = (f::nat=>'b+nat, x::'a+nat) & f k = Inr 0}"
    1.23 +    --{*it is a subtype of @{text "(nat=>'b+nat) * ('a+nat)"}*}
    1.24 +  by auto
    1.25 +
    1.26 +text{*Datatypes will be represented by sets of type @{text node}*}
    1.27 +
    1.28 +types 'a item        = "('a, unit) node set"
    1.29 +      ('a, 'b) dtree = "('a, 'b) node set"
    1.30 +
    1.31 +consts
    1.32 +  apfst     :: "['a=>'c, 'a*'b] => 'c*'b"
    1.33 +  Push      :: "[('b + nat), nat => ('b + nat)] => (nat => ('b + nat))"
    1.34 +
    1.35 +  Push_Node :: "[('b + nat), ('a, 'b) node] => ('a, 'b) node"
    1.36 +  ndepth    :: "('a, 'b) node => nat"
    1.37 +
    1.38 +  Atom      :: "('a + nat) => ('a, 'b) dtree"
    1.39 +  Leaf      :: "'a => ('a, 'b) dtree"
    1.40 +  Numb      :: "nat => ('a, 'b) dtree"
    1.41 +  Scons     :: "[('a, 'b) dtree, ('a, 'b) dtree] => ('a, 'b) dtree"
    1.42 +  In0       :: "('a, 'b) dtree => ('a, 'b) dtree"
    1.43 +  In1       :: "('a, 'b) dtree => ('a, 'b) dtree"
    1.44 +  Lim       :: "('b => ('a, 'b) dtree) => ('a, 'b) dtree"
    1.45 +
    1.46 +  ntrunc    :: "[nat, ('a, 'b) dtree] => ('a, 'b) dtree"
    1.47 +
    1.48 +  uprod     :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    1.49 +  usum      :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    1.50 +
    1.51 +  Split     :: "[[('a, 'b) dtree, ('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    1.52 +  Case      :: "[[('a, 'b) dtree]=>'c, [('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    1.53 +
    1.54 +  dprod     :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    1.55 +                => (('a, 'b) dtree * ('a, 'b) dtree)set"
    1.56 +  dsum      :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    1.57 +                => (('a, 'b) dtree * ('a, 'b) dtree)set"
    1.58 +
    1.59 +
    1.60 +defs
    1.61 +
    1.62 +  Push_Node_def:  "Push_Node == (%n x. Abs_Node (apfst (Push n) (Rep_Node x)))"
    1.63 +
    1.64 +  (*crude "lists" of nats -- needed for the constructions*)
    1.65 +  apfst_def:  "apfst == (%f (x,y). (f(x),y))"
    1.66 +  Push_def:   "Push == (%b h. nat_case b h)"
    1.67 +
    1.68 +  (** operations on S-expressions -- sets of nodes **)
    1.69 +
    1.70 +  (*S-expression constructors*)
    1.71 +  Atom_def:   "Atom == (%x. {Abs_Node((%k. Inr 0, x))})"
    1.72 +  Scons_def:  "Scons M N == (Push_Node (Inr 1) ` M) Un (Push_Node (Inr (Suc 1)) ` N)"
    1.73 +
    1.74 +  (*Leaf nodes, with arbitrary or nat labels*)
    1.75 +  Leaf_def:   "Leaf == Atom o Inl"
    1.76 +  Numb_def:   "Numb == Atom o Inr"
    1.77 +
    1.78 +  (*Injections of the "disjoint sum"*)
    1.79 +  In0_def:    "In0(M) == Scons (Numb 0) M"
    1.80 +  In1_def:    "In1(M) == Scons (Numb 1) M"
    1.81 +
    1.82 +  (*Function spaces*)
    1.83 +  Lim_def: "Lim f == Union {z. ? x. z = Push_Node (Inl x) ` (f x)}"
    1.84 +
    1.85 +  (*the set of nodes with depth less than k*)
    1.86 +  ndepth_def: "ndepth(n) == (%(f,x). LEAST k. f k = Inr 0) (Rep_Node n)"
    1.87 +  ntrunc_def: "ntrunc k N == {n. n:N & ndepth(n)<k}"
    1.88 +
    1.89 +  (*products and sums for the "universe"*)
    1.90 +  uprod_def:  "uprod A B == UN x:A. UN y:B. { Scons x y }"
    1.91 +  usum_def:   "usum A B == In0`A Un In1`B"
    1.92 +
    1.93 +  (*the corresponding eliminators*)
    1.94 +  Split_def:  "Split c M == THE u. EX x y. M = Scons x y & u = c x y"
    1.95 +
    1.96 +  Case_def:   "Case c d M == THE u.  (EX x . M = In0(x) & u = c(x))
    1.97 +                                  | (EX y . M = In1(y) & u = d(y))"
    1.98 +
    1.99 +
   1.100 +  (** equality for the "universe" **)
   1.101 +
   1.102 +  dprod_def:  "dprod r s == UN (x,x'):r. UN (y,y'):s. {(Scons x y, Scons x' y')}"
   1.103 +
   1.104 +  dsum_def:   "dsum r s == (UN (x,x'):r. {(In0(x),In0(x'))}) Un
   1.105 +                          (UN (y,y'):s. {(In1(y),In1(y'))})"
   1.106 +
   1.107 +
   1.108 +
   1.109 +(** apfst -- can be used in similar type definitions **)
   1.110 +
   1.111 +lemma apfst_conv [simp]: "apfst f (a,b) = (f(a),b)"
   1.112 +by (simp add: apfst_def)
   1.113 +
   1.114 +
   1.115 +lemma apfst_convE: 
   1.116 +    "[| q = apfst f p;  !!x y. [| p = (x,y);  q = (f(x),y) |] ==> R  
   1.117 +     |] ==> R"
   1.118 +by (force simp add: apfst_def)
   1.119 +
   1.120 +(** Push -- an injection, analogous to Cons on lists **)
   1.121 +
   1.122 +lemma Push_inject1: "Push i f = Push j g  ==> i=j"
   1.123 +apply (simp add: Push_def expand_fun_eq) 
   1.124 +apply (drule_tac x=0 in spec, simp) 
   1.125 +done
   1.126 +
   1.127 +lemma Push_inject2: "Push i f = Push j g  ==> f=g"
   1.128 +apply (auto simp add: Push_def expand_fun_eq) 
   1.129 +apply (drule_tac x="Suc x" in spec, simp) 
   1.130 +done
   1.131 +
   1.132 +lemma Push_inject:
   1.133 +    "[| Push i f =Push j g;  [| i=j;  f=g |] ==> P |] ==> P"
   1.134 +by (blast dest: Push_inject1 Push_inject2) 
   1.135 +
   1.136 +lemma Push_neq_K0: "Push (Inr (Suc k)) f = (%z. Inr 0) ==> P"
   1.137 +by (auto simp add: Push_def expand_fun_eq split: nat.split_asm)
   1.138 +
   1.139 +lemmas Abs_Node_inj = Abs_Node_inject [THEN [2] rev_iffD1, standard]
   1.140 +
   1.141 +
   1.142 +(*** Introduction rules for Node ***)
   1.143 +
   1.144 +lemma Node_K0_I: "(%k. Inr 0, a) : Node"
   1.145 +by (simp add: Node_def)
   1.146 +
   1.147 +lemma Node_Push_I: "p: Node ==> apfst (Push i) p : Node"
   1.148 +apply (simp add: Node_def Push_def) 
   1.149 +apply (fast intro!: apfst_conv nat_case_Suc [THEN trans])
   1.150 +done
   1.151 +
   1.152 +
   1.153 +subsection{*Freeness: Distinctness of Constructors*}
   1.154 +
   1.155 +(** Scons vs Atom **)
   1.156 +
   1.157 +lemma Scons_not_Atom [iff]: "Scons M N \<noteq> Atom(a)"
   1.158 +apply (simp add: Atom_def Scons_def Push_Node_def One_nat_def)
   1.159 +apply (blast intro: Node_K0_I Rep_Node [THEN Node_Push_I] 
   1.160 +         dest!: Abs_Node_inj 
   1.161 +         elim!: apfst_convE sym [THEN Push_neq_K0])  
   1.162 +done
   1.163 +
   1.164 +lemmas Atom_not_Scons = Scons_not_Atom [THEN not_sym, standard]
   1.165 +declare Atom_not_Scons [iff]
   1.166 +
   1.167 +(*** Injectiveness ***)
   1.168 +
   1.169 +(** Atomic nodes **)
   1.170 +
   1.171 +lemma inj_Atom: "inj(Atom)"
   1.172 +apply (simp add: Atom_def)
   1.173 +apply (blast intro!: inj_onI Node_K0_I dest!: Abs_Node_inj)
   1.174 +done
   1.175 +lemmas Atom_inject = inj_Atom [THEN injD, standard]
   1.176 +
   1.177 +lemma Atom_Atom_eq [iff]: "(Atom(a)=Atom(b)) = (a=b)"
   1.178 +by (blast dest!: Atom_inject)
   1.179 +
   1.180 +lemma inj_Leaf: "inj(Leaf)"
   1.181 +apply (simp add: Leaf_def o_def)
   1.182 +apply (rule inj_onI)
   1.183 +apply (erule Atom_inject [THEN Inl_inject])
   1.184 +done
   1.185 +
   1.186 +lemmas Leaf_inject = inj_Leaf [THEN injD, standard]
   1.187 +declare Leaf_inject [dest!]
   1.188 +
   1.189 +lemma inj_Numb: "inj(Numb)"
   1.190 +apply (simp add: Numb_def o_def)
   1.191 +apply (rule inj_onI)
   1.192 +apply (erule Atom_inject [THEN Inr_inject])
   1.193 +done
   1.194 +
   1.195 +lemmas Numb_inject = inj_Numb [THEN injD, standard]
   1.196 +declare Numb_inject [dest!]
   1.197 +
   1.198 +
   1.199 +(** Injectiveness of Push_Node **)
   1.200 +
   1.201 +lemma Push_Node_inject:
   1.202 +    "[| Push_Node i m =Push_Node j n;  [| i=j;  m=n |] ==> P  
   1.203 +     |] ==> P"
   1.204 +apply (simp add: Push_Node_def)
   1.205 +apply (erule Abs_Node_inj [THEN apfst_convE])
   1.206 +apply (rule Rep_Node [THEN Node_Push_I])+
   1.207 +apply (erule sym [THEN apfst_convE]) 
   1.208 +apply (blast intro: Rep_Node_inject [THEN iffD1] trans sym elim!: Push_inject)
   1.209 +done
   1.210 +
   1.211 +
   1.212 +(** Injectiveness of Scons **)
   1.213 +
   1.214 +lemma Scons_inject_lemma1: "Scons M N <= Scons M' N' ==> M<=M'"
   1.215 +apply (simp add: Scons_def One_nat_def)
   1.216 +apply (blast dest!: Push_Node_inject)
   1.217 +done
   1.218 +
   1.219 +lemma Scons_inject_lemma2: "Scons M N <= Scons M' N' ==> N<=N'"
   1.220 +apply (simp add: Scons_def One_nat_def)
   1.221 +apply (blast dest!: Push_Node_inject)
   1.222 +done
   1.223 +
   1.224 +lemma Scons_inject1: "Scons M N = Scons M' N' ==> M=M'"
   1.225 +apply (erule equalityE)
   1.226 +apply (iprover intro: equalityI Scons_inject_lemma1)
   1.227 +done
   1.228 +
   1.229 +lemma Scons_inject2: "Scons M N = Scons M' N' ==> N=N'"
   1.230 +apply (erule equalityE)
   1.231 +apply (iprover intro: equalityI Scons_inject_lemma2)
   1.232 +done
   1.233 +
   1.234 +lemma Scons_inject:
   1.235 +    "[| Scons M N = Scons M' N';  [| M=M';  N=N' |] ==> P |] ==> P"
   1.236 +by (iprover dest: Scons_inject1 Scons_inject2)
   1.237 +
   1.238 +lemma Scons_Scons_eq [iff]: "(Scons M N = Scons M' N') = (M=M' & N=N')"
   1.239 +by (blast elim!: Scons_inject)
   1.240 +
   1.241 +(*** Distinctness involving Leaf and Numb ***)
   1.242 +
   1.243 +(** Scons vs Leaf **)
   1.244 +
   1.245 +lemma Scons_not_Leaf [iff]: "Scons M N \<noteq> Leaf(a)"
   1.246 +by (simp add: Leaf_def o_def Scons_not_Atom)
   1.247 +
   1.248 +lemmas Leaf_not_Scons = Scons_not_Leaf [THEN not_sym, standard]
   1.249 +declare Leaf_not_Scons [iff]
   1.250 +
   1.251 +(** Scons vs Numb **)
   1.252 +
   1.253 +lemma Scons_not_Numb [iff]: "Scons M N \<noteq> Numb(k)"
   1.254 +by (simp add: Numb_def o_def Scons_not_Atom)
   1.255 +
   1.256 +lemmas Numb_not_Scons = Scons_not_Numb [THEN not_sym, standard]
   1.257 +declare Numb_not_Scons [iff]
   1.258 +
   1.259 +
   1.260 +(** Leaf vs Numb **)
   1.261 +
   1.262 +lemma Leaf_not_Numb [iff]: "Leaf(a) \<noteq> Numb(k)"
   1.263 +by (simp add: Leaf_def Numb_def)
   1.264 +
   1.265 +lemmas Numb_not_Leaf = Leaf_not_Numb [THEN not_sym, standard]
   1.266 +declare Numb_not_Leaf [iff]
   1.267 +
   1.268 +
   1.269 +(*** ndepth -- the depth of a node ***)
   1.270 +
   1.271 +lemma ndepth_K0: "ndepth (Abs_Node(%k. Inr 0, x)) = 0"
   1.272 +by (simp add: ndepth_def  Node_K0_I [THEN Abs_Node_inverse] Least_equality)
   1.273 +
   1.274 +lemma ndepth_Push_Node_aux:
   1.275 +     "nat_case (Inr (Suc i)) f k = Inr 0 --> Suc(LEAST x. f x = Inr 0) <= k"
   1.276 +apply (induct_tac "k", auto)
   1.277 +apply (erule Least_le)
   1.278 +done
   1.279 +
   1.280 +lemma ndepth_Push_Node: 
   1.281 +    "ndepth (Push_Node (Inr (Suc i)) n) = Suc(ndepth(n))"
   1.282 +apply (insert Rep_Node [of n, unfolded Node_def])
   1.283 +apply (auto simp add: ndepth_def Push_Node_def
   1.284 +                 Rep_Node [THEN Node_Push_I, THEN Abs_Node_inverse])
   1.285 +apply (rule Least_equality)
   1.286 +apply (auto simp add: Push_def ndepth_Push_Node_aux)
   1.287 +apply (erule LeastI)
   1.288 +done
   1.289 +
   1.290 +
   1.291 +(*** ntrunc applied to the various node sets ***)
   1.292 +
   1.293 +lemma ntrunc_0 [simp]: "ntrunc 0 M = {}"
   1.294 +by (simp add: ntrunc_def)
   1.295 +
   1.296 +lemma ntrunc_Atom [simp]: "ntrunc (Suc k) (Atom a) = Atom(a)"
   1.297 +by (auto simp add: Atom_def ntrunc_def ndepth_K0)
   1.298 +
   1.299 +lemma ntrunc_Leaf [simp]: "ntrunc (Suc k) (Leaf a) = Leaf(a)"
   1.300 +by (simp add: Leaf_def o_def ntrunc_Atom)
   1.301 +
   1.302 +lemma ntrunc_Numb [simp]: "ntrunc (Suc k) (Numb i) = Numb(i)"
   1.303 +by (simp add: Numb_def o_def ntrunc_Atom)
   1.304 +
   1.305 +lemma ntrunc_Scons [simp]: 
   1.306 +    "ntrunc (Suc k) (Scons M N) = Scons (ntrunc k M) (ntrunc k N)"
   1.307 +by (auto simp add: Scons_def ntrunc_def One_nat_def ndepth_Push_Node) 
   1.308 +
   1.309 +
   1.310 +
   1.311 +(** Injection nodes **)
   1.312 +
   1.313 +lemma ntrunc_one_In0 [simp]: "ntrunc (Suc 0) (In0 M) = {}"
   1.314 +apply (simp add: In0_def)
   1.315 +apply (simp add: Scons_def)
   1.316 +done
   1.317 +
   1.318 +lemma ntrunc_In0 [simp]: "ntrunc (Suc(Suc k)) (In0 M) = In0 (ntrunc (Suc k) M)"
   1.319 +by (simp add: In0_def)
   1.320 +
   1.321 +lemma ntrunc_one_In1 [simp]: "ntrunc (Suc 0) (In1 M) = {}"
   1.322 +apply (simp add: In1_def)
   1.323 +apply (simp add: Scons_def)
   1.324 +done
   1.325 +
   1.326 +lemma ntrunc_In1 [simp]: "ntrunc (Suc(Suc k)) (In1 M) = In1 (ntrunc (Suc k) M)"
   1.327 +by (simp add: In1_def)
   1.328 +
   1.329 +
   1.330 +subsection{*Set Constructions*}
   1.331 +
   1.332 +
   1.333 +(*** Cartesian Product ***)
   1.334 +
   1.335 +lemma uprodI [intro!]: "[| M:A;  N:B |] ==> Scons M N : uprod A B"
   1.336 +by (simp add: uprod_def)
   1.337 +
   1.338 +(*The general elimination rule*)
   1.339 +lemma uprodE [elim!]:
   1.340 +    "[| c : uprod A B;   
   1.341 +        !!x y. [| x:A;  y:B;  c = Scons x y |] ==> P  
   1.342 +     |] ==> P"
   1.343 +by (auto simp add: uprod_def) 
   1.344 +
   1.345 +
   1.346 +(*Elimination of a pair -- introduces no eigenvariables*)
   1.347 +lemma uprodE2: "[| Scons M N : uprod A B;  [| M:A;  N:B |] ==> P |] ==> P"
   1.348 +by (auto simp add: uprod_def)
   1.349 +
   1.350 +
   1.351 +(*** Disjoint Sum ***)
   1.352 +
   1.353 +lemma usum_In0I [intro]: "M:A ==> In0(M) : usum A B"
   1.354 +by (simp add: usum_def)
   1.355 +
   1.356 +lemma usum_In1I [intro]: "N:B ==> In1(N) : usum A B"
   1.357 +by (simp add: usum_def)
   1.358 +
   1.359 +lemma usumE [elim!]: 
   1.360 +    "[| u : usum A B;   
   1.361 +        !!x. [| x:A;  u=In0(x) |] ==> P;  
   1.362 +        !!y. [| y:B;  u=In1(y) |] ==> P  
   1.363 +     |] ==> P"
   1.364 +by (auto simp add: usum_def)
   1.365 +
   1.366 +
   1.367 +(** Injection **)
   1.368 +
   1.369 +lemma In0_not_In1 [iff]: "In0(M) \<noteq> In1(N)"
   1.370 +by (auto simp add: In0_def In1_def One_nat_def)
   1.371 +
   1.372 +lemmas In1_not_In0 = In0_not_In1 [THEN not_sym, standard]
   1.373 +declare In1_not_In0 [iff]
   1.374 +
   1.375 +lemma In0_inject: "In0(M) = In0(N) ==>  M=N"
   1.376 +by (simp add: In0_def)
   1.377 +
   1.378 +lemma In1_inject: "In1(M) = In1(N) ==>  M=N"
   1.379 +by (simp add: In1_def)
   1.380 +
   1.381 +lemma In0_eq [iff]: "(In0 M = In0 N) = (M=N)"
   1.382 +by (blast dest!: In0_inject)
   1.383 +
   1.384 +lemma In1_eq [iff]: "(In1 M = In1 N) = (M=N)"
   1.385 +by (blast dest!: In1_inject)
   1.386 +
   1.387 +lemma inj_In0: "inj In0"
   1.388 +by (blast intro!: inj_onI)
   1.389 +
   1.390 +lemma inj_In1: "inj In1"
   1.391 +by (blast intro!: inj_onI)
   1.392 +
   1.393 +
   1.394 +(*** Function spaces ***)
   1.395 +
   1.396 +lemma Lim_inject: "Lim f = Lim g ==> f = g"
   1.397 +apply (simp add: Lim_def)
   1.398 +apply (rule ext)
   1.399 +apply (blast elim!: Push_Node_inject)
   1.400 +done
   1.401 +
   1.402 +
   1.403 +(*** proving equality of sets and functions using ntrunc ***)
   1.404 +
   1.405 +lemma ntrunc_subsetI: "ntrunc k M <= M"
   1.406 +by (auto simp add: ntrunc_def)
   1.407 +
   1.408 +lemma ntrunc_subsetD: "(!!k. ntrunc k M <= N) ==> M<=N"
   1.409 +by (auto simp add: ntrunc_def)
   1.410 +
   1.411 +(*A generalized form of the take-lemma*)
   1.412 +lemma ntrunc_equality: "(!!k. ntrunc k M = ntrunc k N) ==> M=N"
   1.413 +apply (rule equalityI)
   1.414 +apply (rule_tac [!] ntrunc_subsetD)
   1.415 +apply (rule_tac [!] ntrunc_subsetI [THEN [2] subset_trans], auto) 
   1.416 +done
   1.417 +
   1.418 +lemma ntrunc_o_equality: 
   1.419 +    "[| !!k. (ntrunc(k) o h1) = (ntrunc(k) o h2) |] ==> h1=h2"
   1.420 +apply (rule ntrunc_equality [THEN ext])
   1.421 +apply (simp add: expand_fun_eq) 
   1.422 +done
   1.423 +
   1.424 +
   1.425 +(*** Monotonicity ***)
   1.426 +
   1.427 +lemma uprod_mono: "[| A<=A';  B<=B' |] ==> uprod A B <= uprod A' B'"
   1.428 +by (simp add: uprod_def, blast)
   1.429 +
   1.430 +lemma usum_mono: "[| A<=A';  B<=B' |] ==> usum A B <= usum A' B'"
   1.431 +by (simp add: usum_def, blast)
   1.432 +
   1.433 +lemma Scons_mono: "[| M<=M';  N<=N' |] ==> Scons M N <= Scons M' N'"
   1.434 +by (simp add: Scons_def, blast)
   1.435 +
   1.436 +lemma In0_mono: "M<=N ==> In0(M) <= In0(N)"
   1.437 +by (simp add: In0_def subset_refl Scons_mono)
   1.438 +
   1.439 +lemma In1_mono: "M<=N ==> In1(M) <= In1(N)"
   1.440 +by (simp add: In1_def subset_refl Scons_mono)
   1.441 +
   1.442 +
   1.443 +(*** Split and Case ***)
   1.444 +
   1.445 +lemma Split [simp]: "Split c (Scons M N) = c M N"
   1.446 +by (simp add: Split_def)
   1.447 +
   1.448 +lemma Case_In0 [simp]: "Case c d (In0 M) = c(M)"
   1.449 +by (simp add: Case_def)
   1.450 +
   1.451 +lemma Case_In1 [simp]: "Case c d (In1 N) = d(N)"
   1.452 +by (simp add: Case_def)
   1.453 +
   1.454 +
   1.455 +
   1.456 +(**** UN x. B(x) rules ****)
   1.457 +
   1.458 +lemma ntrunc_UN1: "ntrunc k (UN x. f(x)) = (UN x. ntrunc k (f x))"
   1.459 +by (simp add: ntrunc_def, blast)
   1.460 +
   1.461 +lemma Scons_UN1_x: "Scons (UN x. f x) M = (UN x. Scons (f x) M)"
   1.462 +by (simp add: Scons_def, blast)
   1.463 +
   1.464 +lemma Scons_UN1_y: "Scons M (UN x. f x) = (UN x. Scons M (f x))"
   1.465 +by (simp add: Scons_def, blast)
   1.466 +
   1.467 +lemma In0_UN1: "In0(UN x. f(x)) = (UN x. In0(f(x)))"
   1.468 +by (simp add: In0_def Scons_UN1_y)
   1.469 +
   1.470 +lemma In1_UN1: "In1(UN x. f(x)) = (UN x. In1(f(x)))"
   1.471 +by (simp add: In1_def Scons_UN1_y)
   1.472 +
   1.473 +
   1.474 +(*** Equality for Cartesian Product ***)
   1.475 +
   1.476 +lemma dprodI [intro!]: 
   1.477 +    "[| (M,M'):r;  (N,N'):s |] ==> (Scons M N, Scons M' N') : dprod r s"
   1.478 +by (auto simp add: dprod_def)
   1.479 +
   1.480 +(*The general elimination rule*)
   1.481 +lemma dprodE [elim!]: 
   1.482 +    "[| c : dprod r s;   
   1.483 +        !!x y x' y'. [| (x,x') : r;  (y,y') : s;  
   1.484 +                        c = (Scons x y, Scons x' y') |] ==> P  
   1.485 +     |] ==> P"
   1.486 +by (auto simp add: dprod_def)
   1.487 +
   1.488 +
   1.489 +(*** Equality for Disjoint Sum ***)
   1.490 +
   1.491 +lemma dsum_In0I [intro]: "(M,M'):r ==> (In0(M), In0(M')) : dsum r s"
   1.492 +by (auto simp add: dsum_def)
   1.493 +
   1.494 +lemma dsum_In1I [intro]: "(N,N'):s ==> (In1(N), In1(N')) : dsum r s"
   1.495 +by (auto simp add: dsum_def)
   1.496 +
   1.497 +lemma dsumE [elim!]: 
   1.498 +    "[| w : dsum r s;   
   1.499 +        !!x x'. [| (x,x') : r;  w = (In0(x), In0(x')) |] ==> P;  
   1.500 +        !!y y'. [| (y,y') : s;  w = (In1(y), In1(y')) |] ==> P  
   1.501 +     |] ==> P"
   1.502 +by (auto simp add: dsum_def)
   1.503 +
   1.504 +
   1.505 +(*** Monotonicity ***)
   1.506 +
   1.507 +lemma dprod_mono: "[| r<=r';  s<=s' |] ==> dprod r s <= dprod r' s'"
   1.508 +by blast
   1.509 +
   1.510 +lemma dsum_mono: "[| r<=r';  s<=s' |] ==> dsum r s <= dsum r' s'"
   1.511 +by blast
   1.512 +
   1.513 +
   1.514 +(*** Bounding theorems ***)
   1.515 +
   1.516 +lemma dprod_Sigma: "(dprod (A <*> B) (C <*> D)) <= (uprod A C) <*> (uprod B D)"
   1.517 +by blast
   1.518 +
   1.519 +lemmas dprod_subset_Sigma = subset_trans [OF dprod_mono dprod_Sigma, standard]
   1.520 +
   1.521 +(*Dependent version*)
   1.522 +lemma dprod_subset_Sigma2:
   1.523 +     "(dprod (Sigma A B) (Sigma C D)) <= 
   1.524 +      Sigma (uprod A C) (Split (%x y. uprod (B x) (D y)))"
   1.525 +by auto
   1.526 +
   1.527 +lemma dsum_Sigma: "(dsum (A <*> B) (C <*> D)) <= (usum A C) <*> (usum B D)"
   1.528 +by blast
   1.529 +
   1.530 +lemmas dsum_subset_Sigma = subset_trans [OF dsum_mono dsum_Sigma, standard]
   1.531 +
   1.532 +
   1.533 +(*** Domain ***)
   1.534 +
   1.535 +lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
   1.536 +by auto
   1.537 +
   1.538 +lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
   1.539 +by auto
   1.540 +
   1.541 +
   1.542 +subsection {* Finishing the datatype package setup *}
   1.543 +
   1.544 +text {* Belongs to theory @{text Datatype_Universe}; hides popular names. *}
   1.545 +hide (open) const Push Node Atom Leaf Numb Lim Split Case
   1.546 +hide (open) type node item
   1.547 +
   1.548 +
   1.549 +section {* Datatypes *}
   1.550 +
   1.551  setup "DatatypeCodegen.setup2"
   1.552  
   1.553  subsection {* Representing primitive types *}
   1.554 @@ -275,4 +809,95 @@
   1.555  code_const "OperationalEquality.eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
   1.556    (Haskell infixl 4 "==")
   1.557  
   1.558 +ML
   1.559 +{*
   1.560 +val apfst_conv = thm "apfst_conv";
   1.561 +val apfst_convE = thm "apfst_convE";
   1.562 +val Push_inject1 = thm "Push_inject1";
   1.563 +val Push_inject2 = thm "Push_inject2";
   1.564 +val Push_inject = thm "Push_inject";
   1.565 +val Push_neq_K0 = thm "Push_neq_K0";
   1.566 +val Abs_Node_inj = thm "Abs_Node_inj";
   1.567 +val Node_K0_I = thm "Node_K0_I";
   1.568 +val Node_Push_I = thm "Node_Push_I";
   1.569 +val Scons_not_Atom = thm "Scons_not_Atom";
   1.570 +val Atom_not_Scons = thm "Atom_not_Scons";
   1.571 +val inj_Atom = thm "inj_Atom";
   1.572 +val Atom_inject = thm "Atom_inject";
   1.573 +val Atom_Atom_eq = thm "Atom_Atom_eq";
   1.574 +val inj_Leaf = thm "inj_Leaf";
   1.575 +val Leaf_inject = thm "Leaf_inject";
   1.576 +val inj_Numb = thm "inj_Numb";
   1.577 +val Numb_inject = thm "Numb_inject";
   1.578 +val Push_Node_inject = thm "Push_Node_inject";
   1.579 +val Scons_inject1 = thm "Scons_inject1";
   1.580 +val Scons_inject2 = thm "Scons_inject2";
   1.581 +val Scons_inject = thm "Scons_inject";
   1.582 +val Scons_Scons_eq = thm "Scons_Scons_eq";
   1.583 +val Scons_not_Leaf = thm "Scons_not_Leaf";
   1.584 +val Leaf_not_Scons = thm "Leaf_not_Scons";
   1.585 +val Scons_not_Numb = thm "Scons_not_Numb";
   1.586 +val Numb_not_Scons = thm "Numb_not_Scons";
   1.587 +val Leaf_not_Numb = thm "Leaf_not_Numb";
   1.588 +val Numb_not_Leaf = thm "Numb_not_Leaf";
   1.589 +val ndepth_K0 = thm "ndepth_K0";
   1.590 +val ndepth_Push_Node_aux = thm "ndepth_Push_Node_aux";
   1.591 +val ndepth_Push_Node = thm "ndepth_Push_Node";
   1.592 +val ntrunc_0 = thm "ntrunc_0";
   1.593 +val ntrunc_Atom = thm "ntrunc_Atom";
   1.594 +val ntrunc_Leaf = thm "ntrunc_Leaf";
   1.595 +val ntrunc_Numb = thm "ntrunc_Numb";
   1.596 +val ntrunc_Scons = thm "ntrunc_Scons";
   1.597 +val ntrunc_one_In0 = thm "ntrunc_one_In0";
   1.598 +val ntrunc_In0 = thm "ntrunc_In0";
   1.599 +val ntrunc_one_In1 = thm "ntrunc_one_In1";
   1.600 +val ntrunc_In1 = thm "ntrunc_In1";
   1.601 +val uprodI = thm "uprodI";
   1.602 +val uprodE = thm "uprodE";
   1.603 +val uprodE2 = thm "uprodE2";
   1.604 +val usum_In0I = thm "usum_In0I";
   1.605 +val usum_In1I = thm "usum_In1I";
   1.606 +val usumE = thm "usumE";
   1.607 +val In0_not_In1 = thm "In0_not_In1";
   1.608 +val In1_not_In0 = thm "In1_not_In0";
   1.609 +val In0_inject = thm "In0_inject";
   1.610 +val In1_inject = thm "In1_inject";
   1.611 +val In0_eq = thm "In0_eq";
   1.612 +val In1_eq = thm "In1_eq";
   1.613 +val inj_In0 = thm "inj_In0";
   1.614 +val inj_In1 = thm "inj_In1";
   1.615 +val Lim_inject = thm "Lim_inject";
   1.616 +val ntrunc_subsetI = thm "ntrunc_subsetI";
   1.617 +val ntrunc_subsetD = thm "ntrunc_subsetD";
   1.618 +val ntrunc_equality = thm "ntrunc_equality";
   1.619 +val ntrunc_o_equality = thm "ntrunc_o_equality";
   1.620 +val uprod_mono = thm "uprod_mono";
   1.621 +val usum_mono = thm "usum_mono";
   1.622 +val Scons_mono = thm "Scons_mono";
   1.623 +val In0_mono = thm "In0_mono";
   1.624 +val In1_mono = thm "In1_mono";
   1.625 +val Split = thm "Split";
   1.626 +val Case_In0 = thm "Case_In0";
   1.627 +val Case_In1 = thm "Case_In1";
   1.628 +val ntrunc_UN1 = thm "ntrunc_UN1";
   1.629 +val Scons_UN1_x = thm "Scons_UN1_x";
   1.630 +val Scons_UN1_y = thm "Scons_UN1_y";
   1.631 +val In0_UN1 = thm "In0_UN1";
   1.632 +val In1_UN1 = thm "In1_UN1";
   1.633 +val dprodI = thm "dprodI";
   1.634 +val dprodE = thm "dprodE";
   1.635 +val dsum_In0I = thm "dsum_In0I";
   1.636 +val dsum_In1I = thm "dsum_In1I";
   1.637 +val dsumE = thm "dsumE";
   1.638 +val dprod_mono = thm "dprod_mono";
   1.639 +val dsum_mono = thm "dsum_mono";
   1.640 +val dprod_Sigma = thm "dprod_Sigma";
   1.641 +val dprod_subset_Sigma = thm "dprod_subset_Sigma";
   1.642 +val dprod_subset_Sigma2 = thm "dprod_subset_Sigma2";
   1.643 +val dsum_Sigma = thm "dsum_Sigma";
   1.644 +val dsum_subset_Sigma = thm "dsum_subset_Sigma";
   1.645 +val Domain_dprod = thm "Domain_dprod";
   1.646 +val Domain_dsum = thm "Domain_dsum";
   1.647 +*}
   1.648 +
   1.649  end