src/HOL/BNF/Examples/Koenig.thy
author blanchet
Tue, 19 Nov 2013 14:11:26 +0100
changeset 55863 930409d43211
parent 55164 e5853a648b59
permissions -rw-r--r--
use suffix '_list' etc. instead of prefix 'list_' for constants not present in the old package
wenzelm@51545
     1
(*  Title:      HOL/BNF/Examples/Koenig.thy
traytel@51532
     2
    Author:     Dmitriy Traytel, TU Muenchen
traytel@51532
     3
    Author:     Andrei Popescu, TU Muenchen
traytel@51532
     4
    Copyright   2012
traytel@51532
     5
traytel@51532
     6
Koenig's lemma.
traytel@51532
     7
*)
traytel@51532
     8
traytel@51532
     9
header {* Koenig's lemma *}
traytel@51532
    10
traytel@51532
    11
theory Koenig
traytel@51533
    12
imports TreeFI Stream
traytel@51532
    13
begin
traytel@51532
    14
traytel@51532
    15
(* infinite trees: *)
traytel@51532
    16
coinductive infiniteTr where
blanchet@55863
    17
"\<lbrakk>tr' \<in> set_listF (sub tr); infiniteTr tr'\<rbrakk> \<Longrightarrow> infiniteTr tr"
traytel@51532
    18
traytel@51532
    19
lemma infiniteTr_strong_coind[consumes 1, case_names sub]:
traytel@51532
    20
assumes *: "phi tr" and
blanchet@55863
    21
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> set_listF (sub tr). phi tr' \<or> infiniteTr tr'"
traytel@51532
    22
shows "infiniteTr tr"
traytel@51532
    23
using assms by (elim infiniteTr.coinduct) blast
traytel@51532
    24
traytel@51532
    25
lemma infiniteTr_coind[consumes 1, case_names sub, induct pred: infiniteTr]:
traytel@51532
    26
assumes *: "phi tr" and
blanchet@55863
    27
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> set_listF (sub tr). phi tr'"
traytel@51532
    28
shows "infiniteTr tr"
traytel@51532
    29
using assms by (elim infiniteTr.coinduct) blast
traytel@51532
    30
traytel@51532
    31
lemma infiniteTr_sub[simp]:
blanchet@55863
    32
"infiniteTr tr \<Longrightarrow> (\<exists> tr' \<in> set_listF (sub tr). infiniteTr tr')"
traytel@51532
    33
by (erule infiniteTr.cases) blast
traytel@51532
    34
traytel@55164
    35
primcorec konigPath where
traytel@55164
    36
  "shd (konigPath t) = lab t"
blanchet@55863
    37
| "stl (konigPath t) = konigPath (SOME tr. tr \<in> set_listF (sub t) \<and> infiniteTr tr)"
traytel@51532
    38
traytel@51532
    39
(* proper paths in trees: *)
traytel@51532
    40
coinductive properPath where
blanchet@55863
    41
"\<lbrakk>shd as = lab tr; tr' \<in> set_listF (sub tr); properPath (stl as) tr'\<rbrakk> \<Longrightarrow>
traytel@51532
    42
 properPath as tr"
traytel@51532
    43
traytel@51532
    44
lemma properPath_strong_coind[consumes 1, case_names shd_lab sub]:
traytel@51532
    45
assumes *: "phi as tr" and
traytel@51532
    46
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
traytel@51532
    47
***: "\<And> as tr.
traytel@51532
    48
         phi as tr \<Longrightarrow>
blanchet@55863
    49
         \<exists> tr' \<in> set_listF (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
traytel@51532
    50
shows "properPath as tr"
traytel@51532
    51
using assms by (elim properPath.coinduct) blast
traytel@51532
    52
traytel@51532
    53
lemma properPath_coind[consumes 1, case_names shd_lab sub, induct pred: properPath]:
traytel@51532
    54
assumes *: "phi as tr" and
traytel@51532
    55
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
traytel@51532
    56
***: "\<And> as tr.
traytel@51532
    57
         phi as tr \<Longrightarrow>
blanchet@55863
    58
         \<exists> tr' \<in> set_listF (sub tr). phi (stl as) tr'"
traytel@51532
    59
shows "properPath as tr"
traytel@51532
    60
using properPath_strong_coind[of phi, OF * **] *** by blast
traytel@51532
    61
traytel@51532
    62
lemma properPath_shd_lab:
traytel@51532
    63
"properPath as tr \<Longrightarrow> shd as = lab tr"
traytel@51532
    64
by (erule properPath.cases) blast
traytel@51532
    65
traytel@51532
    66
lemma properPath_sub:
traytel@51532
    67
"properPath as tr \<Longrightarrow>
blanchet@55863
    68
 \<exists> tr' \<in> set_listF (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
traytel@51532
    69
by (erule properPath.cases) blast
traytel@51532
    70
traytel@51532
    71
(* prove the following by coinduction *)
traytel@51532
    72
theorem Konig:
traytel@51532
    73
  assumes "infiniteTr tr"
traytel@51532
    74
  shows "properPath (konigPath tr) tr"
traytel@51532
    75
proof-
traytel@51532
    76
  {fix as
traytel@51532
    77
   assume "infiniteTr tr \<and> as = konigPath tr" hence "properPath as tr"
traytel@55164
    78
   proof (coinduction arbitrary: tr as rule: properPath_coind)
traytel@55164
    79
     case (sub tr as)
blanchet@55863
    80
     let ?t = "SOME t'. t' \<in> set_listF (sub tr) \<and> infiniteTr t'"
blanchet@55863
    81
     from sub have "\<exists>t' \<in> set_listF (sub tr). infiniteTr t'" by simp
blanchet@55863
    82
     then have "\<exists>t'. t' \<in> set_listF (sub tr) \<and> infiniteTr t'" by blast
blanchet@55863
    83
     then have "?t \<in> set_listF (sub tr) \<and> infiniteTr ?t" by (rule someI_ex)
traytel@55164
    84
     moreover have "stl (konigPath tr) = konigPath ?t" by simp
traytel@55164
    85
     ultimately show ?case using sub by blast
traytel@51532
    86
   qed simp
traytel@51532
    87
  }
traytel@51532
    88
  thus ?thesis using assms by blast
traytel@51532
    89
qed
traytel@51532
    90
traytel@51532
    91
(* some more stream theorems *)
traytel@51532
    92
traytel@55164
    93
primcorec plus :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" (infixr "\<oplus>" 66) where
traytel@55164
    94
  "shd (plus xs ys) = shd xs + shd ys"
traytel@55164
    95
| "stl (plus xs ys) = plus (stl xs) (stl ys)"
traytel@51532
    96
traytel@51532
    97
definition scalar :: "nat \<Rightarrow> nat stream \<Rightarrow> nat stream" (infixr "\<cdot>" 68) where
traytel@52909
    98
  [simp]: "scalar n = smap (\<lambda>x. n * x)"
traytel@51532
    99
traytel@55164
   100
primcorec ones :: "nat stream" where "ones = 1 ## ones"
traytel@55164
   101
primcorec twos :: "nat stream" where "twos = 2 ## twos"
traytel@51532
   102
definition ns :: "nat \<Rightarrow> nat stream" where [simp]: "ns n = scalar n ones"
traytel@51532
   103
traytel@51532
   104
lemma "ones \<oplus> ones = twos"
traytel@55164
   105
  by coinduction simp
traytel@51532
   106
traytel@51532
   107
lemma "n \<cdot> twos = ns (2 * n)"
traytel@55164
   108
  by coinduction simp
traytel@51532
   109
traytel@51532
   110
lemma prod_scalar: "(n * m) \<cdot> xs = n \<cdot> m \<cdot> xs"
traytel@55164
   111
  by (coinduction arbitrary: xs) auto
traytel@51532
   112
traytel@51532
   113
lemma scalar_plus: "n \<cdot> (xs \<oplus> ys) = n \<cdot> xs \<oplus> n \<cdot> ys"
traytel@55164
   114
  by (coinduction arbitrary: xs ys) (auto simp: add_mult_distrib2)
traytel@51532
   115
traytel@51532
   116
lemma plus_comm: "xs \<oplus> ys = ys \<oplus> xs"
traytel@55164
   117
  by (coinduction arbitrary: xs ys) auto
traytel@51532
   118
traytel@51532
   119
lemma plus_assoc: "(xs \<oplus> ys) \<oplus> zs = xs \<oplus> ys \<oplus> zs"
traytel@55164
   120
  by (coinduction arbitrary: xs ys zs) auto
traytel@51532
   121
traytel@51532
   122
end