src/HOL/Datatype_Examples/Koenig.thy
author hoelzl
Tue, 07 Oct 2014 10:48:29 +0200
changeset 58607 1f90ea1b4010
parent 58309 a09ec6daaa19
child 58889 5b7a9633cfa8
permissions -rw-r--r--
move Stream theory from Datatype_Examples to Library
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58309
a09ec6daaa19 renamed 'BNF_Examples' to 'Datatype_Examples' (cf. 'datatypes.pdf')
blanchet
parents: 57634
diff changeset
     1
(*  Title:      HOL/Datatype_Examples/Koenig.thy
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     2
    Author:     Dmitriy Traytel, TU Muenchen
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     3
    Author:     Andrei Popescu, TU Muenchen
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     4
    Copyright   2012
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     5
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     6
Koenig's lemma.
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     7
*)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     8
57634
blanchet
parents: 57207
diff changeset
     9
header {* Koenig's Lemma *}
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    10
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    11
theory Koenig
58607
1f90ea1b4010 move Stream theory from Datatype_Examples to Library
hoelzl
parents: 58309
diff changeset
    12
imports TreeFI "~~/src/HOL/Library/Stream"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    13
begin
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    14
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    15
(* infinite trees: *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    16
coinductive infiniteTr where
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    17
"\<lbrakk>tr' \<in> set (sub tr); infiniteTr tr'\<rbrakk> \<Longrightarrow> infiniteTr tr"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    18
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    19
lemma infiniteTr_strong_coind[consumes 1, case_names sub]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    20
assumes *: "phi tr" and
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    21
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> set (sub tr). phi tr' \<or> infiniteTr tr'"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    22
shows "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    23
using assms by (elim infiniteTr.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    24
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    25
lemma infiniteTr_coind[consumes 1, case_names sub, induct pred: infiniteTr]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    26
assumes *: "phi tr" and
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    27
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> set (sub tr). phi tr'"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    28
shows "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    29
using assms by (elim infiniteTr.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    30
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    31
lemma infiniteTr_sub[simp]:
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    32
"infiniteTr tr \<Longrightarrow> (\<exists> tr' \<in> set (sub tr). infiniteTr tr')"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    33
by (erule infiniteTr.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    34
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    35
primcorec konigPath where
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    36
  "shd (konigPath t) = lab t"
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    37
| "stl (konigPath t) = konigPath (SOME tr. tr \<in> set (sub t) \<and> infiniteTr tr)"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    38
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    39
(* proper paths in trees: *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    40
coinductive properPath where
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    41
"\<lbrakk>shd as = lab tr; tr' \<in> set (sub tr); properPath (stl as) tr'\<rbrakk> \<Longrightarrow>
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    42
 properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    43
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    44
lemma properPath_strong_coind[consumes 1, case_names shd_lab sub]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    45
assumes *: "phi as tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    46
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    47
***: "\<And> as tr.
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    48
         phi as tr \<Longrightarrow>
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    49
         \<exists> tr' \<in> set (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    50
shows "properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    51
using assms by (elim properPath.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    52
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    53
lemma properPath_coind[consumes 1, case_names shd_lab sub, induct pred: properPath]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    54
assumes *: "phi as tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    55
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    56
***: "\<And> as tr.
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    57
         phi as tr \<Longrightarrow>
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    58
         \<exists> tr' \<in> set (sub tr). phi (stl as) tr'"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    59
shows "properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    60
using properPath_strong_coind[of phi, OF * **] *** by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    61
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    62
lemma properPath_shd_lab:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    63
"properPath as tr \<Longrightarrow> shd as = lab tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    64
by (erule properPath.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    65
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    66
lemma properPath_sub:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    67
"properPath as tr \<Longrightarrow>
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    68
 \<exists> tr' \<in> set (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    69
by (erule properPath.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    70
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    71
(* prove the following by coinduction *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    72
theorem Konig:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    73
  assumes "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    74
  shows "properPath (konigPath tr) tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    75
proof-
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    76
  {fix as
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    77
   assume "infiniteTr tr \<and> as = konigPath tr" hence "properPath as tr"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    78
   proof (coinduction arbitrary: tr as rule: properPath_coind)
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    79
     case (sub tr as)
57207
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    80
     let ?t = "SOME t'. t' \<in> set (sub tr) \<and> infiniteTr t'"
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    81
     from sub have "\<exists>t' \<in> set (sub tr). infiniteTr t'" by simp
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    82
     then have "\<exists>t'. t' \<in> set (sub tr) \<and> infiniteTr t'" by blast
df0f8ad7cc30 got rid of 'listF' example, which is now subsumed by the real 'list' type
blanchet
parents: 55075
diff changeset
    83
     then have "?t \<in> set (sub tr) \<and> infiniteTr ?t" by (rule someI_ex)
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    84
     moreover have "stl (konigPath tr) = konigPath ?t" by simp
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    85
     ultimately show ?case using sub by blast
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    86
   qed simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    87
  }
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    88
  thus ?thesis using assms by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    89
qed
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    90
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    91
(* some more stream theorems *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    92
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    93
primcorec plus :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" (infixr "\<oplus>" 66) where
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    94
  "shd (plus xs ys) = shd xs + shd ys"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    95
| "stl (plus xs ys) = plus (stl xs) (stl ys)"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    96
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    97
definition scalar :: "nat \<Rightarrow> nat stream \<Rightarrow> nat stream" (infixr "\<cdot>" 68) where
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 50530
diff changeset
    98
  [simp]: "scalar n = smap (\<lambda>x. n * x)"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    99
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   100
primcorec ones :: "nat stream" where "ones = 1 ## ones"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   101
primcorec twos :: "nat stream" where "twos = 2 ## twos"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   102
definition ns :: "nat \<Rightarrow> nat stream" where [simp]: "ns n = scalar n ones"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   103
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   104
lemma "ones \<oplus> ones = twos"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   105
  by coinduction simp
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   106
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   107
lemma "n \<cdot> twos = ns (2 * n)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   108
  by coinduction simp
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   109
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   110
lemma prod_scalar: "(n * m) \<cdot> xs = n \<cdot> m \<cdot> xs"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   111
  by (coinduction arbitrary: xs) auto
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   112
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   113
lemma scalar_plus: "n \<cdot> (xs \<oplus> ys) = n \<cdot> xs \<oplus> n \<cdot> ys"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   114
  by (coinduction arbitrary: xs ys) (auto simp: add_mult_distrib2)
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   115
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   116
lemma plus_comm: "xs \<oplus> ys = ys \<oplus> xs"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   117
  by (coinduction arbitrary: xs ys) auto
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   118
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   119
lemma plus_assoc: "(xs \<oplus> ys) \<oplus> zs = xs \<oplus> ys \<oplus> zs"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   120
  by (coinduction arbitrary: xs ys zs) auto
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   121
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   122
end