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