src/HOL/BNF/Examples/Koenig.thy
author kleing
Sat, 14 Sep 2013 20:57:22 +1000
changeset 53633 69f1221fc892
parent 52992 abd760a19e22
child 54027 e5853a648b59
permissions -rw-r--r--
print find_thms result in reverse order so best result is on top
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
lemma unfold_pair_fun_shd[simp]: "shd (stream_dtor_unfold (f \<odot> g) t) = f t"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    27
unfolding shd_def' pair_fun_def stream.dtor_unfold by simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    28
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    29
lemma unfold_pair_fun_stl[simp]: "stl (stream_dtor_unfold (f \<odot> g) t) =
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    30
 stream_dtor_unfold (f \<odot> g) (g t)"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    31
unfolding stl_def' pair_fun_def stream.dtor_unfold by simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    32
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    33
(* infinite trees: *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    34
coinductive infiniteTr where
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    35
"\<lbrakk>tr' \<in> listF_set (sub tr); infiniteTr tr'\<rbrakk> \<Longrightarrow> infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    36
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    37
lemma infiniteTr_strong_coind[consumes 1, case_names sub]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    38
assumes *: "phi tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    39
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> listF_set (sub tr). phi tr' \<or> infiniteTr tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    40
shows "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    41
using assms by (elim infiniteTr.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    42
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    43
lemma infiniteTr_coind[consumes 1, case_names sub, induct pred: infiniteTr]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    44
assumes *: "phi tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    45
**: "\<And> tr. phi tr \<Longrightarrow> \<exists> tr' \<in> listF_set (sub tr). phi tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    46
shows "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    47
using assms by (elim infiniteTr.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    48
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    49
lemma infiniteTr_sub[simp]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    50
"infiniteTr tr \<Longrightarrow> (\<exists> tr' \<in> listF_set (sub tr). infiniteTr tr')"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    51
by (erule infiniteTr.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    52
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    53
definition "konigPath \<equiv> stream_dtor_unfold
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    54
  (lab \<odot> (\<lambda>tr. SOME tr'. tr' \<in> listF_set (sub tr) \<and> infiniteTr tr'))"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    55
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    56
lemma konigPath_simps[simp]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    57
"shd (konigPath t) = lab t"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    58
"stl (konigPath t) = konigPath (SOME tr. tr \<in> listF_set (sub t) \<and> infiniteTr tr)"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    59
unfolding konigPath_def by simp+
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    60
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    61
(* proper paths in trees: *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    62
coinductive properPath where
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    63
"\<lbrakk>shd as = lab tr; tr' \<in> listF_set (sub tr); properPath (stl as) tr'\<rbrakk> \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    64
 properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    65
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    66
lemma properPath_strong_coind[consumes 1, case_names shd_lab sub]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    67
assumes *: "phi as tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    68
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    69
***: "\<And> as tr.
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    70
         phi as tr \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    71
         \<exists> tr' \<in> listF_set (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    72
shows "properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    73
using assms by (elim properPath.coinduct) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    74
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    75
lemma properPath_coind[consumes 1, case_names shd_lab sub, induct pred: properPath]:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    76
assumes *: "phi as tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    77
**: "\<And> as tr. phi as tr \<Longrightarrow> shd as = lab tr" and
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    78
***: "\<And> as tr.
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    79
         phi as tr \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    80
         \<exists> tr' \<in> listF_set (sub tr). phi (stl as) tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    81
shows "properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    82
using properPath_strong_coind[of phi, OF * **] *** by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    83
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    84
lemma properPath_shd_lab:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    85
"properPath as tr \<Longrightarrow> shd as = lab tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    86
by (erule properPath.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    87
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    88
lemma properPath_sub:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    89
"properPath as tr \<Longrightarrow>
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    90
 \<exists> tr' \<in> listF_set (sub tr). phi (stl as) tr' \<or> properPath (stl as) tr'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    91
by (erule properPath.cases) blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    92
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    93
(* prove the following by coinduction *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    94
theorem Konig:
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    95
  assumes "infiniteTr tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    96
  shows "properPath (konigPath tr) tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    97
proof-
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    98
  {fix as
8f6c11103820 renamed theory
traytel
parents:
diff changeset
    99
   assume "infiniteTr tr \<and> as = konigPath tr" hence "properPath as tr"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   100
   proof (induct rule: properPath_coind, safe)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   101
     fix t
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   102
     let ?t = "SOME t'. t' \<in> listF_set (sub t) \<and> infiniteTr t'"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   103
     assume "infiniteTr t"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   104
     hence "\<exists>t' \<in> listF_set (sub t). infiniteTr t'" by simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   105
     hence "\<exists>t'. t' \<in> listF_set (sub t) \<and> infiniteTr t'" by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   106
     hence "?t \<in> listF_set (sub t) \<and> infiniteTr ?t" by (elim someI_ex)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   107
     moreover have "stl (konigPath t) = konigPath ?t" by simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   108
     ultimately show "\<exists>t' \<in> listF_set (sub t).
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   109
             infiniteTr t' \<and> stl (konigPath t) = konigPath t'" by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   110
   qed simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   111
  }
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   112
  thus ?thesis using assms by blast
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   113
qed
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   114
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   115
(* some more stream theorems *)
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   116
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 50530
diff changeset
   117
lemma stream_map[simp]: "smap f = stream_dtor_unfold (f o shd \<odot> stl)"
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 50530
diff changeset
   118
unfolding smap_def pair_fun_def shd_def'[abs_def] stl_def'[abs_def]
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   119
  map_pair_def o_def prod_case_beta by simp
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   120
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   121
definition plus :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" (infixr "\<oplus>" 66) where
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   122
  [simp]: "plus xs ys =
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   123
    stream_dtor_unfold ((%(xs, ys). shd xs + shd ys) \<odot> (%(xs, ys). (stl xs, stl ys))) (xs, ys)"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   124
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   125
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
   126
  [simp]: "scalar n = smap (\<lambda>x. n * x)"
50517
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   127
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   128
definition ones :: "nat stream" where [simp]: "ones = stream_dtor_unfold ((%x. 1) \<odot> id) ()"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   129
definition twos :: "nat stream" where [simp]: "twos = stream_dtor_unfold ((%x. 2) \<odot> id) ()"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   130
definition ns :: "nat \<Rightarrow> nat stream" where [simp]: "ns n = scalar n ones"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   131
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   132
lemma "ones \<oplus> ones = twos"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   133
by (rule stream.coinduct[of "%x1 x2. \<exists>x. x1 = ones \<oplus> ones \<and> x2 = twos"]) auto
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   134
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   135
lemma "n \<cdot> twos = ns (2 * n)"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   136
by (rule stream.coinduct[of "%x1 x2. \<exists>n. x1 = n \<cdot> twos \<and> x2 = ns (2 * n)"]) force+
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   137
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   138
lemma prod_scalar: "(n * m) \<cdot> xs = n \<cdot> m \<cdot> xs"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   139
by (rule stream.coinduct[of "%x1 x2. \<exists>n m xs. x1 = (n * m) \<cdot> xs \<and> x2 = n \<cdot> m \<cdot> xs"]) force+
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   140
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   141
lemma scalar_plus: "n \<cdot> (xs \<oplus> ys) = n \<cdot> xs \<oplus> n \<cdot> ys"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   142
by (rule stream.coinduct[of "%x1 x2. \<exists>n xs ys. x1 = n \<cdot> (xs \<oplus> ys) \<and> x2 = n \<cdot> xs \<oplus> n \<cdot> ys"])
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   143
   (force simp: add_mult_distrib2)+
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   144
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   145
lemma plus_comm: "xs \<oplus> ys = ys \<oplus> xs"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   146
by (rule stream.coinduct[of "%x1 x2. \<exists>xs ys. x1 = xs \<oplus> ys \<and> x2 = ys \<oplus> xs"]) force+
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   147
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   148
lemma plus_assoc: "(xs \<oplus> ys) \<oplus> zs = xs \<oplus> ys \<oplus> zs"
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   149
by (rule stream.coinduct[of "%x1 x2. \<exists>xs ys zs. x1 = (xs \<oplus> ys) \<oplus> zs \<and> x2 = xs \<oplus> ys \<oplus> zs"]) force+
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   150
8f6c11103820 renamed theory
traytel
parents:
diff changeset
   151
end