src/HOL/Library/Old_Datatype.thy
author wenzelm
Wed Mar 08 10:50:59 2017 +0100 (2017-03-08)
changeset 65151 a7394aa4d21c
parent 62145 5b946c81dfbf
child 65513 587433a18053
permissions -rw-r--r--
tuned proofs;
     1 (*  Title:      HOL/Library/Old_Datatype.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4 *)
     5 
     6 section \<open>Old Datatype package: constructing datatypes from Cartesian Products and Disjoint Sums\<close>
     7 
     8 theory Old_Datatype
     9 imports "../Main"
    10 keywords "old_datatype" :: thy_decl
    11 begin
    12 
    13 ML_file "~~/src/HOL/Tools/datatype_realizer.ML"
    14 
    15 
    16 subsection \<open>The datatype universe\<close>
    17 
    18 definition "Node = {p. EX f x k. p = (f :: nat => 'b + nat, x ::'a + nat) & f k = Inr 0}"
    19 
    20 typedef ('a, 'b) node = "Node :: ((nat => 'b + nat) * ('a + nat)) set"
    21   morphisms Rep_Node Abs_Node
    22   unfolding Node_def by auto
    23 
    24 text\<open>Datatypes will be represented by sets of type \<open>node\<close>\<close>
    25 
    26 type_synonym 'a item        = "('a, unit) node set"
    27 type_synonym ('a, 'b) dtree = "('a, 'b) node set"
    28 
    29 definition Push :: "[('b + nat), nat => ('b + nat)] => (nat => ('b + nat))"
    30   (*crude "lists" of nats -- needed for the constructions*)
    31   where "Push == (%b h. case_nat b h)"
    32 
    33 definition Push_Node :: "[('b + nat), ('a, 'b) node] => ('a, 'b) node"
    34   where "Push_Node == (%n x. Abs_Node (apfst (Push n) (Rep_Node x)))"
    35 
    36 
    37 (** operations on S-expressions -- sets of nodes **)
    38 
    39 (*S-expression constructors*)
    40 definition Atom :: "('a + nat) => ('a, 'b) dtree"
    41   where "Atom == (%x. {Abs_Node((%k. Inr 0, x))})"
    42 definition Scons :: "[('a, 'b) dtree, ('a, 'b) dtree] => ('a, 'b) dtree"
    43   where "Scons M N == (Push_Node (Inr 1) ` M) Un (Push_Node (Inr (Suc 1)) ` N)"
    44 
    45 (*Leaf nodes, with arbitrary or nat labels*)
    46 definition Leaf :: "'a => ('a, 'b) dtree"
    47   where "Leaf == Atom o Inl"
    48 definition Numb :: "nat => ('a, 'b) dtree"
    49   where "Numb == Atom o Inr"
    50 
    51 (*Injections of the "disjoint sum"*)
    52 definition In0 :: "('a, 'b) dtree => ('a, 'b) dtree"
    53   where "In0(M) == Scons (Numb 0) M"
    54 definition In1 :: "('a, 'b) dtree => ('a, 'b) dtree"
    55   where "In1(M) == Scons (Numb 1) M"
    56 
    57 (*Function spaces*)
    58 definition Lim :: "('b => ('a, 'b) dtree) => ('a, 'b) dtree"
    59   where "Lim f == \<Union>{z. ? x. z = Push_Node (Inl x) ` (f x)}"
    60 
    61 (*the set of nodes with depth less than k*)
    62 definition ndepth :: "('a, 'b) node => nat"
    63   where "ndepth(n) == (%(f,x). LEAST k. f k = Inr 0) (Rep_Node n)"
    64 definition ntrunc :: "[nat, ('a, 'b) dtree] => ('a, 'b) dtree"
    65   where "ntrunc k N == {n. n:N & ndepth(n)<k}"
    66 
    67 (*products and sums for the "universe"*)
    68 definition uprod :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    69   where "uprod A B == UN x:A. UN y:B. { Scons x y }"
    70 definition usum :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    71   where "usum A B == In0`A Un In1`B"
    72 
    73 (*the corresponding eliminators*)
    74 definition Split :: "[[('a, 'b) dtree, ('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    75   where "Split c M == THE u. EX x y. M = Scons x y & u = c x y"
    76 
    77 definition Case :: "[[('a, 'b) dtree]=>'c, [('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    78   where "Case c d M == THE u. (EX x . M = In0(x) & u = c(x)) | (EX y . M = In1(y) & u = d(y))"
    79 
    80 
    81 (** equality for the "universe" **)
    82 
    83 definition dprod :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    84       => (('a, 'b) dtree * ('a, 'b) dtree)set"
    85   where "dprod r s == UN (x,x'):r. UN (y,y'):s. {(Scons x y, Scons x' y')}"
    86 
    87 definition dsum :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    88       => (('a, 'b) dtree * ('a, 'b) dtree)set"
    89   where "dsum r s == (UN (x,x'):r. {(In0(x),In0(x'))}) Un (UN (y,y'):s. {(In1(y),In1(y'))})"
    90 
    91 
    92 lemma apfst_convE: 
    93     "[| q = apfst f p;  !!x y. [| p = (x,y);  q = (f(x),y) |] ==> R  
    94      |] ==> R"
    95 by (force simp add: apfst_def)
    96 
    97 (** Push -- an injection, analogous to Cons on lists **)
    98 
    99 lemma Push_inject1: "Push i f = Push j g  ==> i=j"
   100 apply (simp add: Push_def fun_eq_iff) 
   101 apply (drule_tac x=0 in spec, simp) 
   102 done
   103 
   104 lemma Push_inject2: "Push i f = Push j g  ==> f=g"
   105 apply (auto simp add: Push_def fun_eq_iff) 
   106 apply (drule_tac x="Suc x" in spec, simp) 
   107 done
   108 
   109 lemma Push_inject:
   110     "[| Push i f =Push j g;  [| i=j;  f=g |] ==> P |] ==> P"
   111 by (blast dest: Push_inject1 Push_inject2) 
   112 
   113 lemma Push_neq_K0: "Push (Inr (Suc k)) f = (%z. Inr 0) ==> P"
   114 by (auto simp add: Push_def fun_eq_iff split: nat.split_asm)
   115 
   116 lemmas Abs_Node_inj = Abs_Node_inject [THEN [2] rev_iffD1]
   117 
   118 
   119 (*** Introduction rules for Node ***)
   120 
   121 lemma Node_K0_I: "(%k. Inr 0, a) : Node"
   122 by (simp add: Node_def)
   123 
   124 lemma Node_Push_I: "p: Node ==> apfst (Push i) p : Node"
   125 apply (simp add: Node_def Push_def) 
   126 apply (fast intro!: apfst_conv nat.case(2)[THEN trans])
   127 done
   128 
   129 
   130 subsection\<open>Freeness: Distinctness of Constructors\<close>
   131 
   132 (** Scons vs Atom **)
   133 
   134 lemma Scons_not_Atom [iff]: "Scons M N \<noteq> Atom(a)"
   135 unfolding Atom_def Scons_def Push_Node_def One_nat_def
   136 by (blast intro: Node_K0_I Rep_Node [THEN Node_Push_I] 
   137          dest!: Abs_Node_inj 
   138          elim!: apfst_convE sym [THEN Push_neq_K0])  
   139 
   140 lemmas Atom_not_Scons [iff] = Scons_not_Atom [THEN not_sym]
   141 
   142 
   143 (*** Injectiveness ***)
   144 
   145 (** Atomic nodes **)
   146 
   147 lemma inj_Atom: "inj(Atom)"
   148 apply (simp add: Atom_def)
   149 apply (blast intro!: inj_onI Node_K0_I dest!: Abs_Node_inj)
   150 done
   151 lemmas Atom_inject = inj_Atom [THEN injD]
   152 
   153 lemma Atom_Atom_eq [iff]: "(Atom(a)=Atom(b)) = (a=b)"
   154 by (blast dest!: Atom_inject)
   155 
   156 lemma inj_Leaf: "inj(Leaf)"
   157 apply (simp add: Leaf_def o_def)
   158 apply (rule inj_onI)
   159 apply (erule Atom_inject [THEN Inl_inject])
   160 done
   161 
   162 lemmas Leaf_inject [dest!] = inj_Leaf [THEN injD]
   163 
   164 lemma inj_Numb: "inj(Numb)"
   165 apply (simp add: Numb_def o_def)
   166 apply (rule inj_onI)
   167 apply (erule Atom_inject [THEN Inr_inject])
   168 done
   169 
   170 lemmas Numb_inject [dest!] = inj_Numb [THEN injD]
   171 
   172 
   173 (** Injectiveness of Push_Node **)
   174 
   175 lemma Push_Node_inject:
   176     "[| Push_Node i m =Push_Node j n;  [| i=j;  m=n |] ==> P  
   177      |] ==> P"
   178 apply (simp add: Push_Node_def)
   179 apply (erule Abs_Node_inj [THEN apfst_convE])
   180 apply (rule Rep_Node [THEN Node_Push_I])+
   181 apply (erule sym [THEN apfst_convE]) 
   182 apply (blast intro: Rep_Node_inject [THEN iffD1] trans sym elim!: Push_inject)
   183 done
   184 
   185 
   186 (** Injectiveness of Scons **)
   187 
   188 lemma Scons_inject_lemma1: "Scons M N <= Scons M' N' ==> M<=M'"
   189 unfolding Scons_def One_nat_def
   190 by (blast dest!: Push_Node_inject)
   191 
   192 lemma Scons_inject_lemma2: "Scons M N <= Scons M' N' ==> N<=N'"
   193 unfolding Scons_def One_nat_def
   194 by (blast dest!: Push_Node_inject)
   195 
   196 lemma Scons_inject1: "Scons M N = Scons M' N' ==> M=M'"
   197 apply (erule equalityE)
   198 apply (iprover intro: equalityI Scons_inject_lemma1)
   199 done
   200 
   201 lemma Scons_inject2: "Scons M N = Scons M' N' ==> N=N'"
   202 apply (erule equalityE)
   203 apply (iprover intro: equalityI Scons_inject_lemma2)
   204 done
   205 
   206 lemma Scons_inject:
   207     "[| Scons M N = Scons M' N';  [| M=M';  N=N' |] ==> P |] ==> P"
   208 by (iprover dest: Scons_inject1 Scons_inject2)
   209 
   210 lemma Scons_Scons_eq [iff]: "(Scons M N = Scons M' N') = (M=M' & N=N')"
   211 by (blast elim!: Scons_inject)
   212 
   213 (*** Distinctness involving Leaf and Numb ***)
   214 
   215 (** Scons vs Leaf **)
   216 
   217 lemma Scons_not_Leaf [iff]: "Scons M N \<noteq> Leaf(a)"
   218 unfolding Leaf_def o_def by (rule Scons_not_Atom)
   219 
   220 lemmas Leaf_not_Scons  [iff] = Scons_not_Leaf [THEN not_sym]
   221 
   222 (** Scons vs Numb **)
   223 
   224 lemma Scons_not_Numb [iff]: "Scons M N \<noteq> Numb(k)"
   225 unfolding Numb_def o_def by (rule Scons_not_Atom)
   226 
   227 lemmas Numb_not_Scons [iff] = Scons_not_Numb [THEN not_sym]
   228 
   229 
   230 (** Leaf vs Numb **)
   231 
   232 lemma Leaf_not_Numb [iff]: "Leaf(a) \<noteq> Numb(k)"
   233 by (simp add: Leaf_def Numb_def)
   234 
   235 lemmas Numb_not_Leaf [iff] = Leaf_not_Numb [THEN not_sym]
   236 
   237 
   238 (*** ndepth -- the depth of a node ***)
   239 
   240 lemma ndepth_K0: "ndepth (Abs_Node(%k. Inr 0, x)) = 0"
   241 by (simp add: ndepth_def  Node_K0_I [THEN Abs_Node_inverse] Least_equality)
   242 
   243 lemma ndepth_Push_Node_aux:
   244      "case_nat (Inr (Suc i)) f k = Inr 0 --> Suc(LEAST x. f x = Inr 0) <= k"
   245 apply (induct_tac "k", auto)
   246 apply (erule Least_le)
   247 done
   248 
   249 lemma ndepth_Push_Node: 
   250     "ndepth (Push_Node (Inr (Suc i)) n) = Suc(ndepth(n))"
   251 apply (insert Rep_Node [of n, unfolded Node_def])
   252 apply (auto simp add: ndepth_def Push_Node_def
   253                  Rep_Node [THEN Node_Push_I, THEN Abs_Node_inverse])
   254 apply (rule Least_equality)
   255 apply (auto simp add: Push_def ndepth_Push_Node_aux)
   256 apply (erule LeastI)
   257 done
   258 
   259 
   260 (*** ntrunc applied to the various node sets ***)
   261 
   262 lemma ntrunc_0 [simp]: "ntrunc 0 M = {}"
   263 by (simp add: ntrunc_def)
   264 
   265 lemma ntrunc_Atom [simp]: "ntrunc (Suc k) (Atom a) = Atom(a)"
   266 by (auto simp add: Atom_def ntrunc_def ndepth_K0)
   267 
   268 lemma ntrunc_Leaf [simp]: "ntrunc (Suc k) (Leaf a) = Leaf(a)"
   269 unfolding Leaf_def o_def by (rule ntrunc_Atom)
   270 
   271 lemma ntrunc_Numb [simp]: "ntrunc (Suc k) (Numb i) = Numb(i)"
   272 unfolding Numb_def o_def by (rule ntrunc_Atom)
   273 
   274 lemma ntrunc_Scons [simp]: 
   275     "ntrunc (Suc k) (Scons M N) = Scons (ntrunc k M) (ntrunc k N)"
   276 unfolding Scons_def ntrunc_def One_nat_def
   277 by (auto simp add: ndepth_Push_Node)
   278 
   279 
   280 
   281 (** Injection nodes **)
   282 
   283 lemma ntrunc_one_In0 [simp]: "ntrunc (Suc 0) (In0 M) = {}"
   284 apply (simp add: In0_def)
   285 apply (simp add: Scons_def)
   286 done
   287 
   288 lemma ntrunc_In0 [simp]: "ntrunc (Suc(Suc k)) (In0 M) = In0 (ntrunc (Suc k) M)"
   289 by (simp add: In0_def)
   290 
   291 lemma ntrunc_one_In1 [simp]: "ntrunc (Suc 0) (In1 M) = {}"
   292 apply (simp add: In1_def)
   293 apply (simp add: Scons_def)
   294 done
   295 
   296 lemma ntrunc_In1 [simp]: "ntrunc (Suc(Suc k)) (In1 M) = In1 (ntrunc (Suc k) M)"
   297 by (simp add: In1_def)
   298 
   299 
   300 subsection\<open>Set Constructions\<close>
   301 
   302 
   303 (*** Cartesian Product ***)
   304 
   305 lemma uprodI [intro!]: "[| M:A;  N:B |] ==> Scons M N : uprod A B"
   306 by (simp add: uprod_def)
   307 
   308 (*The general elimination rule*)
   309 lemma uprodE [elim!]:
   310     "[| c : uprod A B;   
   311         !!x y. [| x:A;  y:B;  c = Scons x y |] ==> P  
   312      |] ==> P"
   313 by (auto simp add: uprod_def) 
   314 
   315 
   316 (*Elimination of a pair -- introduces no eigenvariables*)
   317 lemma uprodE2: "[| Scons M N : uprod A B;  [| M:A;  N:B |] ==> P |] ==> P"
   318 by (auto simp add: uprod_def)
   319 
   320 
   321 (*** Disjoint Sum ***)
   322 
   323 lemma usum_In0I [intro]: "M:A ==> In0(M) : usum A B"
   324 by (simp add: usum_def)
   325 
   326 lemma usum_In1I [intro]: "N:B ==> In1(N) : usum A B"
   327 by (simp add: usum_def)
   328 
   329 lemma usumE [elim!]: 
   330     "[| u : usum A B;   
   331         !!x. [| x:A;  u=In0(x) |] ==> P;  
   332         !!y. [| y:B;  u=In1(y) |] ==> P  
   333      |] ==> P"
   334 by (auto simp add: usum_def)
   335 
   336 
   337 (** Injection **)
   338 
   339 lemma In0_not_In1 [iff]: "In0(M) \<noteq> In1(N)"
   340 unfolding In0_def In1_def One_nat_def by auto
   341 
   342 lemmas In1_not_In0 [iff] = In0_not_In1 [THEN not_sym]
   343 
   344 lemma In0_inject: "In0(M) = In0(N) ==>  M=N"
   345 by (simp add: In0_def)
   346 
   347 lemma In1_inject: "In1(M) = In1(N) ==>  M=N"
   348 by (simp add: In1_def)
   349 
   350 lemma In0_eq [iff]: "(In0 M = In0 N) = (M=N)"
   351 by (blast dest!: In0_inject)
   352 
   353 lemma In1_eq [iff]: "(In1 M = In1 N) = (M=N)"
   354 by (blast dest!: In1_inject)
   355 
   356 lemma inj_In0: "inj In0"
   357 by (blast intro!: inj_onI)
   358 
   359 lemma inj_In1: "inj In1"
   360 by (blast intro!: inj_onI)
   361 
   362 
   363 (*** Function spaces ***)
   364 
   365 lemma Lim_inject: "Lim f = Lim g ==> f = g"
   366 apply (simp add: Lim_def)
   367 apply (rule ext)
   368 apply (blast elim!: Push_Node_inject)
   369 done
   370 
   371 
   372 (*** proving equality of sets and functions using ntrunc ***)
   373 
   374 lemma ntrunc_subsetI: "ntrunc k M <= M"
   375 by (auto simp add: ntrunc_def)
   376 
   377 lemma ntrunc_subsetD: "(!!k. ntrunc k M <= N) ==> M<=N"
   378 by (auto simp add: ntrunc_def)
   379 
   380 (*A generalized form of the take-lemma*)
   381 lemma ntrunc_equality: "(!!k. ntrunc k M = ntrunc k N) ==> M=N"
   382 apply (rule equalityI)
   383 apply (rule_tac [!] ntrunc_subsetD)
   384 apply (rule_tac [!] ntrunc_subsetI [THEN [2] subset_trans], auto) 
   385 done
   386 
   387 lemma ntrunc_o_equality: 
   388     "[| !!k. (ntrunc(k) o h1) = (ntrunc(k) o h2) |] ==> h1=h2"
   389 apply (rule ntrunc_equality [THEN ext])
   390 apply (simp add: fun_eq_iff) 
   391 done
   392 
   393 
   394 (*** Monotonicity ***)
   395 
   396 lemma uprod_mono: "[| A<=A';  B<=B' |] ==> uprod A B <= uprod A' B'"
   397 by (simp add: uprod_def, blast)
   398 
   399 lemma usum_mono: "[| A<=A';  B<=B' |] ==> usum A B <= usum A' B'"
   400 by (simp add: usum_def, blast)
   401 
   402 lemma Scons_mono: "[| M<=M';  N<=N' |] ==> Scons M N <= Scons M' N'"
   403 by (simp add: Scons_def, blast)
   404 
   405 lemma In0_mono: "M<=N ==> In0(M) <= In0(N)"
   406 by (simp add: In0_def Scons_mono)
   407 
   408 lemma In1_mono: "M<=N ==> In1(M) <= In1(N)"
   409 by (simp add: In1_def Scons_mono)
   410 
   411 
   412 (*** Split and Case ***)
   413 
   414 lemma Split [simp]: "Split c (Scons M N) = c M N"
   415 by (simp add: Split_def)
   416 
   417 lemma Case_In0 [simp]: "Case c d (In0 M) = c(M)"
   418 by (simp add: Case_def)
   419 
   420 lemma Case_In1 [simp]: "Case c d (In1 N) = d(N)"
   421 by (simp add: Case_def)
   422 
   423 
   424 
   425 (**** UN x. B(x) rules ****)
   426 
   427 lemma ntrunc_UN1: "ntrunc k (UN x. f(x)) = (UN x. ntrunc k (f x))"
   428 by (simp add: ntrunc_def, blast)
   429 
   430 lemma Scons_UN1_x: "Scons (UN x. f x) M = (UN x. Scons (f x) M)"
   431 by (simp add: Scons_def, blast)
   432 
   433 lemma Scons_UN1_y: "Scons M (UN x. f x) = (UN x. Scons M (f x))"
   434 by (simp add: Scons_def, blast)
   435 
   436 lemma In0_UN1: "In0(UN x. f(x)) = (UN x. In0(f(x)))"
   437 by (simp add: In0_def Scons_UN1_y)
   438 
   439 lemma In1_UN1: "In1(UN x. f(x)) = (UN x. In1(f(x)))"
   440 by (simp add: In1_def Scons_UN1_y)
   441 
   442 
   443 (*** Equality for Cartesian Product ***)
   444 
   445 lemma dprodI [intro!]: 
   446     "[| (M,M'):r;  (N,N'):s |] ==> (Scons M N, Scons M' N') : dprod r s"
   447 by (auto simp add: dprod_def)
   448 
   449 (*The general elimination rule*)
   450 lemma dprodE [elim!]: 
   451     "[| c : dprod r s;   
   452         !!x y x' y'. [| (x,x') : r;  (y,y') : s;  
   453                         c = (Scons x y, Scons x' y') |] ==> P  
   454      |] ==> P"
   455 by (auto simp add: dprod_def)
   456 
   457 
   458 (*** Equality for Disjoint Sum ***)
   459 
   460 lemma dsum_In0I [intro]: "(M,M'):r ==> (In0(M), In0(M')) : dsum r s"
   461 by (auto simp add: dsum_def)
   462 
   463 lemma dsum_In1I [intro]: "(N,N'):s ==> (In1(N), In1(N')) : dsum r s"
   464 by (auto simp add: dsum_def)
   465 
   466 lemma dsumE [elim!]: 
   467     "[| w : dsum r s;   
   468         !!x x'. [| (x,x') : r;  w = (In0(x), In0(x')) |] ==> P;  
   469         !!y y'. [| (y,y') : s;  w = (In1(y), In1(y')) |] ==> P  
   470      |] ==> P"
   471 by (auto simp add: dsum_def)
   472 
   473 
   474 (*** Monotonicity ***)
   475 
   476 lemma dprod_mono: "[| r<=r';  s<=s' |] ==> dprod r s <= dprod r' s'"
   477 by blast
   478 
   479 lemma dsum_mono: "[| r<=r';  s<=s' |] ==> dsum r s <= dsum r' s'"
   480 by blast
   481 
   482 
   483 (*** Bounding theorems ***)
   484 
   485 lemma dprod_Sigma: "(dprod (A \<times> B) (C \<times> D)) <= (uprod A C) \<times> (uprod B D)"
   486 by blast
   487 
   488 lemmas dprod_subset_Sigma = subset_trans [OF dprod_mono dprod_Sigma]
   489 
   490 (*Dependent version*)
   491 lemma dprod_subset_Sigma2:
   492     "(dprod (Sigma A B) (Sigma C D)) <= Sigma (uprod A C) (Split (%x y. uprod (B x) (D y)))"
   493 by auto
   494 
   495 lemma dsum_Sigma: "(dsum (A \<times> B) (C \<times> D)) <= (usum A C) \<times> (usum B D)"
   496 by blast
   497 
   498 lemmas dsum_subset_Sigma = subset_trans [OF dsum_mono dsum_Sigma]
   499 
   500 
   501 (*** Domain theorems ***)
   502 
   503 lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
   504   by auto
   505 
   506 lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
   507   by auto
   508 
   509 
   510 text \<open>hides popular names\<close>
   511 hide_type (open) node item
   512 hide_const (open) Push Node Atom Leaf Numb Lim Split Case
   513 
   514 ML_file "~~/src/HOL/Tools/Old_Datatype/old_datatype.ML"
   515 ML_file "~~/src/HOL/Tools/inductive_realizer.ML"
   516 
   517 end