src/HOL/BNF/Examples/Koenig.thy
author traytel
Wed, 02 Oct 2013 13:29:04 +0200
changeset 54027 e5853a648b59
parent 52992 abd760a19e22
child 54490 930409d43211
permissions -rw-r--r--
use new coinduction method and primcorec in examples
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50530
6266e44b3396 updated some headers;
wenzelm
parents: 50518
diff changeset
     1
(*  Title:      HOL/BNF/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
8f6c11103820 renamed theory
traytel
parents:
diff changeset
     9
header {* Koenig's lemma *}
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    10
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    11
theory Koenig
50518
d4fdda801e19 short library for streams
traytel
parents: 50517
diff changeset
    12
imports TreeFI 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
(* selectors for streams *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    16
lemma shd_def': "shd as = fst (stream_dtor as)"
52992
abd760a19e22 fixed "sorry"d proofs
blanchet
parents: 51772
diff changeset
    17
apply (case_tac as)
abd760a19e22 fixed "sorry"d proofs
blanchet
parents: 51772
diff changeset
    18
apply (auto simp add: shd_def)
abd760a19e22 fixed "sorry"d proofs
blanchet
parents: 51772
diff changeset
    19
by (simp add: Stream_def stream.dtor_ctor)
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    20
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    21
lemma stl_def': "stl as = snd (stream_dtor as)"
52992
abd760a19e22 fixed "sorry"d proofs
blanchet
parents: 51772
diff changeset
    22
apply (case_tac as)
abd760a19e22 fixed "sorry"d proofs
blanchet
parents: 51772
diff changeset
    23
apply (auto simp add: stl_def)
abd760a19e22 fixed "sorry"d proofs
blanchet
parents: 51772
diff changeset
    24
by (simp add: Stream_def stream.dtor_ctor)
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    25
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    26
(* infinite trees: *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    27
coinductive infiniteTr where
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    28
"\<lbrakk>tr' \<in> listF_set (sub tr); infiniteTr tr'\<rbrakk> \<Longrightarrow> infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    29
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    30
lemma infiniteTr_strong_coind[consumes 1, case_names sub]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    31
assumes *: "phi tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    32
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> listF_set (sub tr). phi tr' \<or> infiniteTr tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    33
shows "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    34
using assms by (elim infiniteTr.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    35
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    36
lemma infiniteTr_coind[consumes 1, case_names sub, induct pred: infiniteTr]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    37
assumes *: "phi tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    38
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> listF_set (sub tr). phi tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    39
shows "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    40
using assms by (elim infiniteTr.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    41
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    42
lemma infiniteTr_sub[simp]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    43
"infiniteTr tr \<Longrightarrow> (\<exists> tr' \<in> listF_set (sub tr). infiniteTr tr')"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    44
by (erule infiniteTr.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    45
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    46
primcorec konigPath where
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    47
  "shd (konigPath t) = lab t"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    48
| "stl (konigPath t) = konigPath (SOME tr. tr \<in> listF_set (sub t) \<and> infiniteTr tr)"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    49
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    50
(* proper paths in trees: *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    51
coinductive properPath where
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    52
"\<lbrakk>shd as = lab tr; tr' \<in> listF_set (sub tr); properPath (stl as) tr'\<rbrakk> \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    53
 properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    54
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    55
lemma properPath_strong_coind[consumes 1, case_names shd_lab sub]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    56
assumes *: "phi as tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    57
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    58
***: "\<And> as tr.
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    59
         phi as tr \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    60
         \<exists> tr' \<in> listF_set (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    61
shows "properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    62
using assms by (elim properPath.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    63
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    64
lemma properPath_coind[consumes 1, case_names shd_lab sub, induct pred: properPath]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    65
assumes *: "phi as tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    66
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    67
***: "\<And> as tr.
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    68
         phi as tr \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    69
         \<exists> tr' \<in> listF_set (sub tr). phi (stl as) tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    70
shows "properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    71
using properPath_strong_coind[of phi, OF * **] *** by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    72
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    73
lemma properPath_shd_lab:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    74
"properPath as tr \<Longrightarrow> shd as = lab tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    75
by (erule properPath.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    76
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    77
lemma properPath_sub:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    78
"properPath as tr \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    79
 \<exists> tr' \<in> listF_set (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    80
by (erule properPath.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    81
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    82
(* prove the following by coinduction *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    83
theorem Konig:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    84
  assumes "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    85
  shows "properPath (konigPath tr) tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    86
proof-
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    87
  {fix as
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    88
   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
    89
   proof (coinduction arbitrary: tr as rule: properPath_coind)
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    90
     case (sub tr as)
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    91
     let ?t = "SOME t'. t' \<in> listF_set (sub tr) \<and> infiniteTr t'"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    92
     from sub have "\<exists>t' \<in> listF_set (sub tr). infiniteTr t'" by simp
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    93
     then have "\<exists>t'. t' \<in> listF_set (sub tr) \<and> infiniteTr t'" by blast
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    94
     then have "?t \<in> listF_set (sub tr) \<and> infiniteTr ?t" by (rule someI_ex)
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    95
     moreover have "stl (konigPath tr) = konigPath ?t" by simp
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
    96
     ultimately show ?case using sub by blast
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    97
   qed simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    98
  }
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    99
  thus ?thesis using assms by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   100
qed
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   101
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   102
(* some more stream theorems *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   103
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   104
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
   105
  "shd (plus xs ys) = shd xs + shd ys"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   106
| "stl (plus xs ys) = plus (stl xs) (stl ys)"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   107
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   108
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
   109
  [simp]: "scalar n = smap (\<lambda>x. n * x)"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   110
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   111
primcorec ones :: "nat stream" where "ones = 1 ## ones"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   112
primcorec twos :: "nat stream" where "twos = 2 ## twos"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   113
definition ns :: "nat \<Rightarrow> nat stream" where [simp]: "ns n = scalar n ones"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   114
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   115
lemma "ones \<oplus> ones = twos"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   116
  by coinduction simp
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   117
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   118
lemma "n \<cdot> twos = ns (2 * n)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   119
  by coinduction simp
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   120
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   121
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
   122
  by (coinduction arbitrary: xs) auto
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   123
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   124
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
   125
  by (coinduction arbitrary: xs ys) (auto simp: add_mult_distrib2)
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   126
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   127
lemma plus_comm: "xs \<oplus> ys = ys \<oplus> xs"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 52992
diff changeset
   128
  by (coinduction arbitrary: xs ys) auto
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   129
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   130
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
   131
  by (coinduction arbitrary: xs ys zs) auto
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   132
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   133
end