src/HOL/Library/Graphs.thy
author haftmann
Fri Mar 16 21:32:08 2007 +0100 (2007-03-16)
changeset 22452 8a86fd2a1bf0
parent 22431 28344ccffc35
child 22626 7e35b6c8ab5b
permissions -rw-r--r--
adjusted to new lattice theory developement in Lattices.thy / FixedPoint.thy
krauss@22371
     1
(*  Title:      HOL/Library/Graphs.thy
krauss@22371
     2
    ID:         $Id$
krauss@22371
     3
    Author:     Alexander Krauss, TU Muenchen
krauss@22371
     4
*)
krauss@22371
     5
krauss@22359
     6
theory Graphs
krauss@22359
     7
imports Main SCT_Misc Kleene_Algebras ExecutableSet
krauss@22359
     8
begin
krauss@22359
     9
krauss@22359
    10
krauss@22359
    11
section {* Basic types, Size Change Graphs *}
krauss@22359
    12
krauss@22359
    13
datatype ('a, 'b) graph = 
krauss@22359
    14
  Graph "('a \<times> 'b \<times> 'a) set"
krauss@22359
    15
krauss@22359
    16
fun dest_graph :: "('a, 'b) graph \<Rightarrow> ('a \<times> 'b \<times> 'a) set"
krauss@22359
    17
  where "dest_graph (Graph G) = G"
krauss@22359
    18
krauss@22359
    19
lemma graph_dest_graph[simp]:
krauss@22359
    20
  "Graph (dest_graph G) = G"
krauss@22359
    21
  by (cases G) simp
krauss@22359
    22
haftmann@22452
    23
lemma split_graph_all:
haftmann@22452
    24
  "(\<And>gr. PROP P gr) \<equiv> (\<And>set. PROP P (Graph set))"
haftmann@22452
    25
proof
haftmann@22452
    26
  fix set
haftmann@22452
    27
  assume "\<And>gr. PROP P gr"
haftmann@22452
    28
  then show "PROP P (Graph set)" .
haftmann@22452
    29
next
haftmann@22452
    30
  fix gr
haftmann@22452
    31
  assume "\<And>set. PROP P (Graph set)"
haftmann@22452
    32
  then have "PROP P (Graph (dest_graph gr))" .
haftmann@22452
    33
  then show "PROP P gr" by simp
haftmann@22452
    34
qed
haftmann@22452
    35
krauss@22359
    36
definition 
krauss@22359
    37
  has_edge :: "('n,'e) graph \<Rightarrow> 'n \<Rightarrow> 'e \<Rightarrow> 'n \<Rightarrow> bool"
krauss@22359
    38
("_ \<turnstile> _ \<leadsto>\<^bsup>_\<^esup> _")
krauss@22359
    39
where
krauss@22359
    40
  "has_edge G n e n' = ((n, e, n') \<in> dest_graph G)"
krauss@22359
    41
krauss@22359
    42
krauss@22359
    43
krauss@22359
    44
section {* Graph composition *}
krauss@22359
    45
krauss@22359
    46
fun grcomp :: "('n, 'e::times) graph \<Rightarrow> ('n, 'e) graph  \<Rightarrow> ('n, 'e) graph"
krauss@22359
    47
where
krauss@22359
    48
  "grcomp (Graph G) (Graph H) = 
krauss@22359
    49
  Graph {(p,b,q) | p b q. 
krauss@22359
    50
  (\<exists>k e e'. (p,e,k)\<in>G \<and> (k,e',q)\<in>H \<and> b = e * e')}"
krauss@22359
    51
krauss@22359
    52
krauss@22359
    53
declare grcomp.simps[code del]
krauss@22359
    54
krauss@22359
    55
krauss@22359
    56
lemma graph_ext:
krauss@22359
    57
  assumes "\<And>n e n'. has_edge G n e n' = has_edge H n e n'"
krauss@22359
    58
  shows "G = H"
krauss@22359
    59
  using prems
krauss@22359
    60
  by (cases G, cases H, auto simp:split_paired_all has_edge_def)
krauss@22359
    61
krauss@22359
    62
krauss@22359
    63
instance graph :: (type, times) times 
krauss@22359
    64
  graph_mult_def: "G * H == grcomp G H" ..
krauss@22359
    65
krauss@22359
    66
instance graph :: (type, one) one 
krauss@22359
    67
  graph_one_def: "1 == Graph { (x, 1, x) |x. True}" ..
krauss@22359
    68
krauss@22359
    69
instance graph :: (type, type) zero 
krauss@22359
    70
  graph_zero_def: "0 == Graph {}" ..
krauss@22359
    71
krauss@22359
    72
instance graph :: (type, type) plus 
krauss@22359
    73
  graph_plus_def: "G + H == Graph (dest_graph G \<union> dest_graph H)" ..
krauss@22359
    74
krauss@22359
    75
krauss@22359
    76
subsection {* Simprules for the graph operations *}
krauss@22359
    77
krauss@22359
    78
lemma in_grcomp:
krauss@22359
    79
  "has_edge (G * H) p b q
krauss@22359
    80
  = (\<exists>k e e'. has_edge G p e k \<and> has_edge H k e' q \<and> b = e * e')"
krauss@22359
    81
  by (cases G, cases H) (auto simp:graph_mult_def has_edge_def image_def)
krauss@22359
    82
krauss@22359
    83
lemma in_grunit:
krauss@22359
    84
  "has_edge 1 p b q = (p = q \<and> b = 1)"
krauss@22359
    85
  by (auto simp:graph_one_def has_edge_def)
krauss@22359
    86
krauss@22359
    87
lemma in_grplus:
krauss@22359
    88
  "has_edge (G + H) p b q = (has_edge G p b q \<or> has_edge H p b q)"
krauss@22359
    89
  by (cases G, cases H, auto simp:has_edge_def graph_plus_def)
krauss@22359
    90
krauss@22359
    91
lemma in_grzero:
krauss@22359
    92
  "has_edge 0 p b q = False"
krauss@22359
    93
  by (simp add:graph_zero_def has_edge_def)
krauss@22359
    94
krauss@22359
    95
krauss@22359
    96
instance graph :: (type, semigroup_mult) semigroup_mult
krauss@22359
    97
proof
krauss@22359
    98
  fix G1 G2 G3 :: "('a,'b) graph"
krauss@22359
    99
  
krauss@22359
   100
  show "G1 * G2 * G3 = G1 * (G2 * G3)"
krauss@22359
   101
  proof (rule graph_ext, rule trans)
krauss@22359
   102
    fix p J q
krauss@22359
   103
    show "has_edge ((G1 * G2) * G3) p J q =
krauss@22359
   104
      (\<exists>G i H j I.
krauss@22359
   105
      has_edge G1 p G i
krauss@22359
   106
      \<and> has_edge G2 i H j
krauss@22359
   107
      \<and> has_edge G3 j I q
krauss@22359
   108
      \<and> J = (G * H) * I)"
krauss@22359
   109
      by (simp only:in_grcomp) blast
krauss@22359
   110
    show "\<dots> = has_edge (G1 * (G2 * G3)) p J q"
krauss@22359
   111
      by (simp only:in_grcomp mult_assoc) blast
krauss@22359
   112
  qed
krauss@22359
   113
qed
krauss@22359
   114
krauss@22359
   115
instance graph :: (type, monoid_mult) monoid_mult
krauss@22359
   116
proof
krauss@22359
   117
  fix G1 G2 G3 :: "('a,'b) graph"
krauss@22359
   118
  
krauss@22359
   119
  show "1 * G1 = G1" 
krauss@22359
   120
    by (rule graph_ext) (auto simp:in_grcomp in_grunit)
krauss@22359
   121
  show "G1 * 1 = G1"
krauss@22359
   122
    by (rule graph_ext) (auto simp:in_grcomp in_grunit)
krauss@22359
   123
qed
krauss@22359
   124
krauss@22359
   125
krauss@22359
   126
lemma grcomp_rdist:
krauss@22359
   127
  fixes G :: "('a::type, 'b::semigroup_mult) graph"
krauss@22359
   128
  shows "G * (H + I) = G * H + G * I"
krauss@22359
   129
  by (rule graph_ext, simp add:in_grcomp in_grplus) blast
krauss@22359
   130
krauss@22359
   131
lemma grcomp_ldist:
krauss@22359
   132
  fixes G :: "('a::type, 'b::semigroup_mult) graph"
krauss@22359
   133
  shows "(G + H) * I = G * I + H * I"
krauss@22359
   134
  by (rule graph_ext, simp add:in_grcomp in_grplus) blast
krauss@22359
   135
krauss@22359
   136
fun grpow :: "nat \<Rightarrow> ('a::type, 'b::monoid_mult) graph \<Rightarrow> ('a, 'b) graph"
krauss@22359
   137
where
krauss@22359
   138
  "grpow 0 A = 1"
krauss@22359
   139
| "grpow (Suc n) A = A * (grpow n A)"
krauss@22359
   140
krauss@22359
   141
krauss@22359
   142
instance graph :: (type, monoid_mult) recpower 
krauss@22359
   143
  graph_pow_def: "A ^ n == grpow n A" 
krauss@22359
   144
  by default (simp_all add:graph_pow_def)
krauss@22359
   145
krauss@22359
   146
subsection {* Order on Graphs *}
krauss@22359
   147
krauss@22359
   148
instance graph :: (type, type) order
haftmann@22452
   149
  graph_leq_def: "G \<le> H \<equiv> dest_graph G \<subseteq> dest_graph H"
haftmann@22452
   150
  graph_less_def: "G < H \<equiv> dest_graph G \<subset> dest_graph H"
krauss@22359
   151
proof
krauss@22359
   152
  fix x y z :: "('a,'b) graph"
krauss@22359
   153
krauss@22359
   154
  show "x \<le> x" unfolding graph_leq_def ..
krauss@22359
   155
  
krauss@22359
   156
  from order_trans
krauss@22359
   157
  show "\<lbrakk>x \<le> y; y \<le> z\<rbrakk> \<Longrightarrow> x \<le> z" unfolding graph_leq_def .
krauss@22359
   158
krauss@22359
   159
  show "\<lbrakk>x \<le> y; y \<le> x\<rbrakk> \<Longrightarrow> x = y" unfolding graph_leq_def 
krauss@22359
   160
    by (cases x, cases y) simp
krauss@22359
   161
krauss@22359
   162
  show "(x < y) = (x \<le> y \<and> x \<noteq> y)"
krauss@22359
   163
    unfolding graph_leq_def graph_less_def
krauss@22359
   164
    by (cases x, cases y) auto
krauss@22359
   165
qed
krauss@22359
   166
haftmann@22452
   167
instance graph :: (type, type) distrib_lattice
haftmann@22452
   168
  "inf G H \<equiv> Graph (dest_graph G \<inter> dest_graph H)"
haftmann@22452
   169
  "sup G H \<equiv> G + H"
haftmann@22452
   170
  by default (auto simp add: split_graph_all graph_plus_def inf_graph_def sup_graph_def graph_leq_def graph_less_def)
krauss@22359
   171
haftmann@22452
   172
instance graph :: (type, type) complete_lattice
haftmann@22452
   173
  Inf_graph_def: "Inf == \<lambda>Gs. Graph (\<Inter>(dest_graph ` Gs))"
haftmann@22452
   174
  by default (auto simp: Inf_graph_def graph_leq_def le_fun_def le_bool_def in_grplus has_edge_def)
krauss@22359
   175
krauss@22359
   176
instance graph :: (type, monoid_mult) semiring_1
krauss@22359
   177
proof
krauss@22359
   178
  fix a b c :: "('a, 'b) graph"
krauss@22359
   179
krauss@22359
   180
  show "a + b + c = a + (b + c)" 
krauss@22359
   181
    and "a + b = b + a" unfolding graph_plus_def
krauss@22359
   182
    by auto
krauss@22359
   183
krauss@22359
   184
  show "0 + a = a" unfolding graph_zero_def graph_plus_def
krauss@22359
   185
    by simp
krauss@22359
   186
krauss@22359
   187
  show "0 * a = 0" "a * 0 = 0" unfolding graph_zero_def graph_mult_def
krauss@22359
   188
    by (cases a, simp)+
krauss@22359
   189
krauss@22359
   190
  show "(a + b) * c = a * c + b * c"
krauss@22359
   191
    by (rule graph_ext, simp add:in_grcomp in_grplus) blast
krauss@22359
   192
krauss@22359
   193
  show "a * (b + c) = a * b + a * c"
krauss@22359
   194
    by (rule graph_ext, simp add:in_grcomp in_grplus) blast
krauss@22359
   195
krauss@22359
   196
  show "(0::('a,'b) graph) \<noteq> 1" unfolding graph_zero_def graph_one_def
krauss@22359
   197
    by simp
krauss@22359
   198
qed
krauss@22359
   199
krauss@22359
   200
instance graph :: (type, monoid_mult) idem_add
krauss@22359
   201
proof
krauss@22359
   202
  fix a :: "('a, 'b) graph"
haftmann@22452
   203
  show "a + a = a" unfolding graph_plus_def by simp
krauss@22359
   204
qed
krauss@22359
   205
krauss@22359
   206
krauss@22359
   207
(* define star on graphs *)
krauss@22359
   208
krauss@22359
   209
krauss@22359
   210
instance graph :: (type, monoid_mult) star
krauss@22359
   211
  graph_star_def: "star G == (SUP n. G ^ n)" ..
krauss@22359
   212
krauss@22359
   213
krauss@22359
   214
lemma graph_leqI:
krauss@22359
   215
  assumes "\<And>n e n'. has_edge G n e n' \<Longrightarrow> has_edge H n e n'"
krauss@22359
   216
  shows "G \<le> H"
krauss@22359
   217
  using prems
krauss@22359
   218
  unfolding graph_leq_def has_edge_def
krauss@22359
   219
  by auto
krauss@22359
   220
krauss@22359
   221
krauss@22359
   222
lemma in_graph_plusE:
krauss@22359
   223
  assumes "has_edge (G + H) n e n'"
krauss@22359
   224
  assumes "has_edge G n e n' \<Longrightarrow> P"
krauss@22359
   225
  assumes "has_edge H n e n' \<Longrightarrow> P"
krauss@22359
   226
  shows P
krauss@22359
   227
  using prems
krauss@22359
   228
  by (auto simp: in_grplus)
krauss@22359
   229
krauss@22359
   230
krauss@22359
   231
krauss@22359
   232
lemma 
krauss@22359
   233
  assumes "x \<in> S k"
krauss@22359
   234
  shows "x \<in> (\<Union>k. S k)"
krauss@22359
   235
  using prems by blast
krauss@22359
   236
krauss@22359
   237
lemma graph_union_least:
krauss@22359
   238
  assumes "\<And>n. Graph (G n) \<le> C"
krauss@22359
   239
  shows "Graph (\<Union>n. G n) \<le> C"
krauss@22359
   240
  using prems unfolding graph_leq_def
krauss@22359
   241
  by auto
krauss@22359
   242
krauss@22359
   243
lemma Sup_graph_eq:
krauss@22359
   244
  "(SUP n. Graph (G n)) = Graph (\<Union>n. G n)"
berghofe@22431
   245
  unfolding SUPR_def
krauss@22359
   246
  apply (rule order_antisym)
krauss@22359
   247
  apply (rule Sup_least)
krauss@22359
   248
  apply auto
krauss@22359
   249
  apply (simp add:graph_leq_def)
krauss@22359
   250
  apply auto
krauss@22359
   251
  apply (rule graph_union_least)
krauss@22359
   252
  apply (rule Sup_upper)
krauss@22359
   253
  by auto
krauss@22359
   254
krauss@22359
   255
lemma has_edge_leq: "has_edge G p b q = (Graph {(p,b,q)} \<le> G)"
krauss@22359
   256
  unfolding has_edge_def graph_leq_def
krauss@22359
   257
  by (cases G) simp
krauss@22359
   258
krauss@22359
   259
krauss@22359
   260
lemma Sup_graph_eq2:
krauss@22359
   261
  "(SUP n. G n) = Graph (\<Union>n. dest_graph (G n))"
krauss@22359
   262
  using Sup_graph_eq[of "\<lambda>n. dest_graph (G n)", simplified]
krauss@22359
   263
  by simp
krauss@22359
   264
krauss@22359
   265
lemma in_SUP:
krauss@22359
   266
  "has_edge (SUP x. Gs x) p b q = (\<exists>x. has_edge (Gs x) p b q)"
krauss@22359
   267
  unfolding Sup_graph_eq2 has_edge_leq graph_leq_def
krauss@22359
   268
  by simp
krauss@22359
   269
haftmann@22452
   270
instance graph :: (type, monoid_mult) kleene_by_complete_lattice
krauss@22359
   271
proof
krauss@22359
   272
  fix a b c :: "('a, 'b) graph"
krauss@22359
   273
krauss@22359
   274
  show "a \<le> b \<longleftrightarrow> a + b = b" unfolding graph_leq_def graph_plus_def
krauss@22359
   275
    by (cases a, cases b) auto
krauss@22359
   276
krauss@22359
   277
  from order_less_le show "a < b \<longleftrightarrow> a \<le> b \<and> a \<noteq> b" .
krauss@22359
   278
krauss@22359
   279
  show "a * star b * c = (SUP n. a * b ^ n * c)"
krauss@22359
   280
    unfolding graph_star_def
krauss@22359
   281
    by (rule graph_ext) (force simp:in_SUP in_grcomp)
krauss@22359
   282
qed
krauss@22359
   283
krauss@22359
   284
krauss@22359
   285
lemma in_star: 
krauss@22359
   286
  "has_edge (star G) a x b = (\<exists>n. has_edge (G ^ n) a x b)"
krauss@22359
   287
  by (auto simp:graph_star_def in_SUP)
krauss@22359
   288
krauss@22359
   289
lemma tcl_is_SUP:
krauss@22359
   290
  "tcl (G::('a::type, 'b::monoid_mult) graph) =
krauss@22359
   291
  (SUP n. G ^ (Suc n))"
krauss@22359
   292
  unfolding tcl_def 
krauss@22359
   293
  using star_cont[of 1 G G]
krauss@22359
   294
  by (simp add:power_Suc power_commutes)
krauss@22359
   295
krauss@22359
   296
krauss@22359
   297
lemma in_tcl: 
krauss@22359
   298
  "has_edge (tcl G) a x b = (\<exists>n>0. has_edge (G ^ n) a x b)"
krauss@22359
   299
  apply (auto simp: tcl_is_SUP in_SUP)
krauss@22359
   300
  apply (rule_tac x = "n - 1" in exI, auto)
krauss@22359
   301
  done
krauss@22359
   302
krauss@22359
   303
krauss@22359
   304
krauss@22359
   305
section {* Infinite Paths *}
krauss@22359
   306
krauss@22359
   307
types ('n, 'e) ipath = "('n \<times> 'e) sequence"
krauss@22359
   308
krauss@22359
   309
definition has_ipath :: "('n, 'e) graph \<Rightarrow> ('n, 'e) ipath \<Rightarrow> bool"
krauss@22359
   310
where
krauss@22359
   311
  "has_ipath G p = 
krauss@22359
   312
  (\<forall>i. has_edge G (fst (p i)) (snd (p i)) (fst (p (Suc i))))"
krauss@22359
   313
krauss@22359
   314
krauss@22359
   315
krauss@22359
   316
section {* Finite Paths *}
krauss@22359
   317
krauss@22359
   318
types ('n, 'e) fpath = "('n \<times> ('e \<times> 'n) list)"
krauss@22359
   319
krauss@22359
   320
inductive2  has_fpath :: "('n, 'e) graph \<Rightarrow> ('n, 'e) fpath \<Rightarrow> bool" 
krauss@22359
   321
  for G :: "('n, 'e) graph"
krauss@22359
   322
where
krauss@22359
   323
  has_fpath_empty: "has_fpath G (n, [])"
krauss@22359
   324
| has_fpath_join: "\<lbrakk>G \<turnstile> n \<leadsto>\<^bsup>e\<^esup> n'; has_fpath G (n', es)\<rbrakk> \<Longrightarrow> has_fpath G (n, (e, n')#es)"
krauss@22359
   325
krauss@22359
   326
definition 
krauss@22359
   327
  "end_node p = 
krauss@22359
   328
  (if snd p = [] then fst p else snd (snd p ! (length (snd p) - 1)))"
krauss@22359
   329
krauss@22359
   330
definition path_nth :: "('n, 'e) fpath \<Rightarrow> nat \<Rightarrow> ('n \<times> 'e \<times> 'n)"
krauss@22359
   331
where
krauss@22359
   332
  "path_nth p k = (if k = 0 then fst p else snd (snd p ! (k - 1)), snd p ! k)"
krauss@22359
   333
krauss@22359
   334
lemma endnode_nth:
krauss@22359
   335
  assumes "length (snd p) = Suc k"
krauss@22359
   336
  shows "end_node p = snd (snd (path_nth p k))"
krauss@22359
   337
  using prems unfolding end_node_def path_nth_def
krauss@22359
   338
  by auto
krauss@22359
   339
krauss@22359
   340
lemma path_nth_graph:
krauss@22359
   341
  assumes "k < length (snd p)"
krauss@22359
   342
  assumes "has_fpath G p"
krauss@22359
   343
  shows "(\<lambda>(n,e,n'). has_edge G n e n') (path_nth p k)"
krauss@22359
   344
  using prems
krauss@22359
   345
proof (induct k arbitrary:p)
krauss@22359
   346
  case 0 thus ?case 
krauss@22359
   347
    unfolding path_nth_def by (auto elim:has_fpath.cases)
krauss@22359
   348
next
krauss@22359
   349
  case (Suc k p)
krauss@22359
   350
krauss@22359
   351
  from `has_fpath G p` show ?case 
krauss@22359
   352
  proof (rule has_fpath.cases)
krauss@22359
   353
    case goal1 with Suc show ?case by simp
krauss@22359
   354
  next
krauss@22359
   355
    fix n e n' es
krauss@22359
   356
    assume st: "p = (n, (e, n') # es)"
krauss@22359
   357
       "G \<turnstile> n \<leadsto>\<^bsup>e\<^esup> n'"
krauss@22359
   358
       "has_fpath G (n', es)"
krauss@22359
   359
    with Suc
krauss@22359
   360
    have "(\<lambda>(n, b, a). G \<turnstile> n \<leadsto>\<^bsup>b\<^esup> a) (path_nth (n', es) k)" by simp
krauss@22359
   361
    with st show ?thesis by (cases k, auto simp:path_nth_def)
krauss@22359
   362
  qed
krauss@22359
   363
qed
krauss@22359
   364
krauss@22359
   365
lemma path_nth_connected:
krauss@22359
   366
  assumes "Suc k < length (snd p)"
krauss@22359
   367
  shows "fst (path_nth p (Suc k)) = snd (snd (path_nth p k))"
krauss@22359
   368
  using prems
krauss@22359
   369
  unfolding path_nth_def
krauss@22359
   370
  by auto
krauss@22359
   371
krauss@22359
   372
definition path_loop :: "('n, 'e) fpath \<Rightarrow> ('n, 'e) ipath" ("omega")
krauss@22359
   373
where
krauss@22359
   374
  "omega p \<equiv> (\<lambda>i. (\<lambda>(n,e,n'). (n,e)) (path_nth p (i mod (length (snd p)))))"
krauss@22359
   375
krauss@22359
   376
lemma fst_p0: "fst (path_nth p 0) = fst p"
krauss@22359
   377
  unfolding path_nth_def by simp
krauss@22359
   378
krauss@22359
   379
lemma path_loop_connect:
krauss@22359
   380
  assumes "fst p = end_node p"
krauss@22359
   381
  and "0 < length (snd p)" (is "0 < ?l")
krauss@22359
   382
  shows "fst (path_nth p (Suc i mod (length (snd p))))
krauss@22359
   383
  = snd (snd (path_nth p (i mod length (snd p))))"
krauss@22359
   384
  (is "\<dots> = snd (snd (path_nth p ?k))")
krauss@22359
   385
proof -
krauss@22359
   386
  from `0 < ?l` have "i mod ?l < ?l" (is "?k < ?l")
krauss@22359
   387
    by simp
krauss@22359
   388
krauss@22359
   389
  show ?thesis 
krauss@22359
   390
  proof (cases "Suc ?k < ?l")
krauss@22359
   391
    case True
krauss@22359
   392
    hence "Suc ?k \<noteq> ?l" by simp
krauss@22359
   393
    with path_nth_connected[OF True]
krauss@22359
   394
    show ?thesis
krauss@22359
   395
      by (simp add:mod_Suc)
krauss@22359
   396
  next
krauss@22359
   397
    case False 
krauss@22359
   398
    with `?k < ?l` have wrap: "Suc ?k = ?l" by simp
krauss@22359
   399
krauss@22359
   400
    hence "fst (path_nth p (Suc i mod ?l)) = fst (path_nth p 0)" 
krauss@22359
   401
      by (simp add: mod_Suc)
krauss@22359
   402
    also from fst_p0 have "\<dots> = fst p" .
krauss@22359
   403
    also have "\<dots> = end_node p" .
krauss@22359
   404
    also have "\<dots> = snd (snd (path_nth p ?k))" 
krauss@22359
   405
      by (auto simp:endnode_nth wrap)
krauss@22359
   406
    finally show ?thesis .
krauss@22359
   407
  qed
krauss@22359
   408
qed
krauss@22359
   409
krauss@22359
   410
lemma path_loop_graph:
krauss@22359
   411
  assumes "has_fpath G p"
krauss@22359
   412
  and loop: "fst p = end_node p"
krauss@22359
   413
  and nonempty: "0 < length (snd p)" (is "0 < ?l")
krauss@22359
   414
  shows "has_ipath G (omega p)"
krauss@22359
   415
proof (auto simp:has_ipath_def)
krauss@22359
   416
  fix i 
krauss@22359
   417
  from `0 < ?l` have "i mod ?l < ?l" (is "?k < ?l")
krauss@22359
   418
    by simp
krauss@22359
   419
  with path_nth_graph 
krauss@22359
   420
  have pk_G: "(\<lambda>(n,e,n'). has_edge G n e n') (path_nth p ?k)" .
krauss@22359
   421
krauss@22359
   422
  from path_loop_connect[OF loop nonempty] pk_G
krauss@22359
   423
  show "has_edge G (fst (omega p i)) (snd (omega p i)) (fst (omega p (Suc i)))"
krauss@22359
   424
    unfolding path_loop_def has_edge_def split_def
krauss@22359
   425
    by simp
krauss@22359
   426
qed
krauss@22359
   427
krauss@22359
   428
definition prod :: "('n, 'e::monoid_mult) fpath \<Rightarrow> 'e"
krauss@22359
   429
where
krauss@22359
   430
  "prod p = foldr (op *) (map fst (snd p)) 1"
krauss@22359
   431
krauss@22359
   432
lemma prod_simps[simp]:
krauss@22359
   433
  "prod (n, []) = 1"
krauss@22359
   434
  "prod (n, (e,n')#es) = e * (prod (n',es))"
krauss@22359
   435
unfolding prod_def
krauss@22359
   436
by simp_all
krauss@22359
   437
krauss@22359
   438
lemma power_induces_path:
krauss@22359
   439
  assumes a: "has_edge (A ^ k) n G m"
krauss@22359
   440
  obtains p 
krauss@22359
   441
    where "has_fpath A p"
krauss@22359
   442
      and "n = fst p" "m = end_node p"
krauss@22359
   443
      and "G = prod p"
krauss@22359
   444
      and "k = length (snd p)"
krauss@22359
   445
  using a
krauss@22359
   446
proof (induct k arbitrary:m n G thesis)
krauss@22359
   447
  case (0 m n G)
krauss@22359
   448
  let ?p = "(n, [])"
krauss@22359
   449
  from 0 have "has_fpath A ?p" "m = end_node ?p" "G = prod ?p"
krauss@22359
   450
    by (auto simp:in_grunit end_node_def intro:has_fpath.intros)
krauss@22359
   451
  thus ?case using 0 by (auto simp:end_node_def)
krauss@22359
   452
next
krauss@22359
   453
  case (Suc k m n G)
krauss@22359
   454
  hence "has_edge (A * A ^ k) n G m" 
krauss@22359
   455
    by (simp add:power_Suc power_commutes)
krauss@22359
   456
  then obtain G' H j where 
krauss@22359
   457
    a_A: "has_edge A n G' j"
krauss@22359
   458
    and H_pow: "has_edge (A ^ k) j H m"
krauss@22359
   459
    and [simp]: "G = G' * H"
krauss@22359
   460
    by (auto simp:in_grcomp) 
krauss@22359
   461
krauss@22359
   462
  from H_pow and Suc
krauss@22359
   463
  obtain p
krauss@22359
   464
    where p_path: "has_fpath A p"
krauss@22359
   465
    and [simp]: "j = fst p" "m = end_node p" "H = prod p" 
krauss@22359
   466
    "k = length (snd p)"
krauss@22359
   467
    by blast
krauss@22359
   468
krauss@22359
   469
  let ?p' = "(n, (G', j)#snd p)"
krauss@22359
   470
  from a_A and p_path
krauss@22359
   471
  have "has_fpath A ?p'" "m = end_node ?p'" "G = prod ?p'"
krauss@22359
   472
    by (auto simp:end_node_def nth.simps intro:has_fpath.intros split:nat.split)
krauss@22359
   473
  thus ?case using Suc by auto
krauss@22359
   474
qed
krauss@22359
   475
krauss@22359
   476
krauss@22359
   477
krauss@22359
   478
krauss@22359
   479
krauss@22359
   480
section {* Sub-Paths *}
krauss@22359
   481
krauss@22359
   482
krauss@22359
   483
definition sub_path :: "('n, 'e) ipath \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> ('n, 'e) fpath"
krauss@22359
   484
("(_\<langle>_,_\<rangle>)")
krauss@22359
   485
where
krauss@22359
   486
  "p\<langle>i,j\<rangle> =
krauss@22359
   487
  (fst (p i), map (\<lambda>k. (snd (p k), fst (p (Suc k)))) [i ..< j])"
krauss@22359
   488
krauss@22359
   489
krauss@22359
   490
lemma sub_path_is_path: 
krauss@22359
   491
  assumes ipath: "has_ipath G p"
krauss@22359
   492
  assumes l: "i \<le> j"
krauss@22359
   493
  shows "has_fpath G (p\<langle>i,j\<rangle>)"
krauss@22359
   494
  using l
krauss@22359
   495
proof (induct i rule:inc_induct)
krauss@22359
   496
  case 1 show ?case by (auto simp:sub_path_def intro:has_fpath.intros)
krauss@22359
   497
next
krauss@22359
   498
  case (2 i)
krauss@22359
   499
  with ipath upt_rec[of i j]
krauss@22359
   500
  show ?case
krauss@22359
   501
    by (auto simp:sub_path_def has_ipath_def intro:has_fpath.intros)
krauss@22359
   502
qed
krauss@22359
   503
krauss@22359
   504
krauss@22359
   505
lemma sub_path_start[simp]:
krauss@22359
   506
  "fst (p\<langle>i,j\<rangle>) = fst (p i)"
krauss@22359
   507
  by (simp add:sub_path_def)
krauss@22359
   508
krauss@22359
   509
lemma nth_upto[simp]: "k < j - i \<Longrightarrow> [i ..< j] ! k = i + k"
krauss@22359
   510
  by (induct k) auto
krauss@22359
   511
krauss@22359
   512
lemma sub_path_end[simp]:
krauss@22359
   513
  "i < j \<Longrightarrow> end_node (p\<langle>i,j\<rangle>) = fst (p j)"
krauss@22359
   514
  by (auto simp:sub_path_def end_node_def)
krauss@22359
   515
krauss@22359
   516
lemma foldr_map: "foldr f (map g xs) = foldr (f o g) xs"
krauss@22359
   517
  by (induct xs) auto
krauss@22359
   518
krauss@22359
   519
lemma upto_append[simp]:
krauss@22359
   520
  assumes "i \<le> j" "j \<le> k"
krauss@22359
   521
  shows "[ i ..< j ] @ [j ..< k] = [i ..< k]"
krauss@22359
   522
  using prems and upt_add_eq_append[of i j "k - j"]
krauss@22359
   523
  by simp
krauss@22359
   524
krauss@22359
   525
lemma foldr_monoid: "foldr (op *) xs 1 * foldr (op *) ys 1
krauss@22359
   526
  = foldr (op *) (xs @ ys) (1::'a::monoid_mult)"
krauss@22359
   527
  by (induct xs) (auto simp:mult_assoc)
krauss@22359
   528
krauss@22359
   529
lemma sub_path_prod:
krauss@22359
   530
  assumes "i < j"
krauss@22359
   531
  assumes "j < k"
krauss@22359
   532
  shows "prod (p\<langle>i,k\<rangle>) = prod (p\<langle>i,j\<rangle>) * prod (p\<langle>j,k\<rangle>)"
krauss@22359
   533
  using prems
krauss@22359
   534
  unfolding prod_def sub_path_def
krauss@22359
   535
  by (simp add:map_compose[symmetric] comp_def)
krauss@22359
   536
   (simp only:foldr_monoid map_append[symmetric] upto_append)
krauss@22359
   537
krauss@22359
   538
krauss@22359
   539
lemma path_acgpow_aux:
krauss@22359
   540
  assumes "length es = l"
krauss@22359
   541
  assumes "has_fpath G (n,es)"
krauss@22359
   542
  shows "has_edge (G ^ l) n (prod (n,es)) (end_node (n,es))"
krauss@22359
   543
using prems
krauss@22359
   544
proof (induct l arbitrary:n es)
krauss@22359
   545
  case 0 thus ?case
krauss@22359
   546
    by (simp add:in_grunit end_node_def) 
krauss@22359
   547
next
krauss@22359
   548
  case (Suc l n es)
krauss@22359
   549
  hence "es \<noteq> []" by auto
krauss@22359
   550
  let ?n' = "snd (hd es)"
krauss@22359
   551
  let ?es' = "tl es"
krauss@22359
   552
  let ?e = "fst (hd es)"
krauss@22359
   553
krauss@22359
   554
  from Suc have len: "length ?es' = l" by auto
krauss@22359
   555
krauss@22359
   556
  from Suc
krauss@22359
   557
  have [simp]: "end_node (n, es) = end_node (?n', ?es')"
krauss@22359
   558
    by (cases es) (auto simp:end_node_def nth.simps split:nat.split)
krauss@22359
   559
krauss@22359
   560
  from `has_fpath G (n,es)`
krauss@22359
   561
  have "has_fpath G (?n', ?es')"
krauss@22359
   562
    by (rule has_fpath.cases) (auto intro:has_fpath.intros)
krauss@22359
   563
  with Suc len
krauss@22359
   564
  have "has_edge (G ^ l) ?n' (prod (?n', ?es')) (end_node (?n', ?es'))"
krauss@22359
   565
    by auto
krauss@22359
   566
  moreover
krauss@22359
   567
  from `es \<noteq> []`
krauss@22359
   568
  have "prod (n, es) = ?e * (prod (?n', ?es'))"
krauss@22359
   569
    by (cases es) auto
krauss@22359
   570
  moreover
krauss@22359
   571
  from `has_fpath G (n,es)` have c:"has_edge G n ?e ?n'"
krauss@22359
   572
    by (rule has_fpath.cases) (insert `es \<noteq> []`, auto)
krauss@22359
   573
krauss@22359
   574
  ultimately
krauss@22359
   575
  show ?case
krauss@22359
   576
     unfolding power_Suc 
krauss@22359
   577
     by (auto simp:in_grcomp)
krauss@22359
   578
qed
krauss@22359
   579
krauss@22359
   580
krauss@22359
   581
lemma path_acgpow:
krauss@22359
   582
   "has_fpath G p
krauss@22359
   583
  \<Longrightarrow> has_edge (G ^ length (snd p)) (fst p) (prod p) (end_node p)"
krauss@22359
   584
by (cases p)
krauss@22359
   585
   (rule path_acgpow_aux[of "snd p" "length (snd p)" _ "fst p", simplified])
krauss@22359
   586
krauss@22359
   587
krauss@22359
   588
lemma star_paths:
krauss@22359
   589
  "has_edge (star G) a x b =
krauss@22359
   590
   (\<exists>p. has_fpath G p \<and> a = fst p \<and> b = end_node p \<and> x = prod p)"
krauss@22359
   591
proof
krauss@22359
   592
  assume "has_edge (star G) a x b"
krauss@22359
   593
  then obtain n where pow: "has_edge (G ^ n) a x b"
krauss@22359
   594
    by (auto simp:in_star)
krauss@22359
   595
krauss@22359
   596
  then obtain p where
krauss@22359
   597
    "has_fpath G p" "a = fst p" "b = end_node p" "x = prod p"
krauss@22359
   598
    by (rule power_induces_path)
krauss@22359
   599
krauss@22359
   600
  thus "\<exists>p. has_fpath G p \<and> a = fst p \<and> b = end_node p \<and> x = prod p"
krauss@22359
   601
    by blast
krauss@22359
   602
next
krauss@22359
   603
  assume "\<exists>p. has_fpath G p \<and> a = fst p \<and> b = end_node p \<and> x = prod p"
krauss@22359
   604
  then obtain p where
krauss@22359
   605
    "has_fpath G p" "a = fst p" "b = end_node p" "x = prod p"
krauss@22359
   606
    by blast
krauss@22359
   607
krauss@22359
   608
  hence "has_edge (G ^ length (snd p)) a x b"
krauss@22359
   609
    by (auto intro:path_acgpow)
krauss@22359
   610
krauss@22359
   611
  thus "has_edge (star G) a x b"
krauss@22359
   612
    by (auto simp:in_star)
krauss@22359
   613
qed
krauss@22359
   614
krauss@22359
   615
krauss@22359
   616
lemma plus_paths:
krauss@22359
   617
  "has_edge (tcl G) a x b =
krauss@22359
   618
   (\<exists>p. has_fpath G p \<and> a = fst p \<and> b = end_node p \<and> x = prod p \<and> 0 < length (snd p))"
krauss@22359
   619
proof
krauss@22359
   620
  assume "has_edge (tcl G) a x b"
krauss@22359
   621
  
krauss@22359
   622
  then obtain n where pow: "has_edge (G ^ n) a x b" and "0 < n"
krauss@22359
   623
    by (auto simp:in_tcl)
krauss@22359
   624
krauss@22359
   625
  from pow obtain p where
krauss@22359
   626
    "has_fpath G p" "a = fst p" "b = end_node p" "x = prod p"
krauss@22359
   627
    "n = length (snd p)"
krauss@22359
   628
    by (rule power_induces_path)
krauss@22359
   629
krauss@22359
   630
  with `0 < n`
krauss@22359
   631
  show "\<exists>p. has_fpath G p \<and> a = fst p \<and> b = end_node p \<and> x = prod p \<and> 0 < length (snd p) "
krauss@22359
   632
    by blast
krauss@22359
   633
next
krauss@22359
   634
  assume "\<exists>p. has_fpath G p \<and> a = fst p \<and> b = end_node p \<and> x = prod p
krauss@22359
   635
    \<and> 0 < length (snd p)"
krauss@22359
   636
  then obtain p where
krauss@22359
   637
    "has_fpath G p" "a = fst p" "b = end_node p" "x = prod p"
krauss@22359
   638
    "0 < length (snd p)"
krauss@22359
   639
    by blast
krauss@22359
   640
krauss@22359
   641
  hence "has_edge (G ^ length (snd p)) a x b"
krauss@22359
   642
    by (auto intro:path_acgpow)
krauss@22359
   643
krauss@22359
   644
  with `0 < length (snd p)`
krauss@22359
   645
  show "has_edge (tcl G) a x b"
krauss@22359
   646
    by (auto simp:in_tcl)
krauss@22359
   647
qed
krauss@22359
   648
krauss@22359
   649
krauss@22359
   650
definition
krauss@22359
   651
  "contract s p = 
krauss@22359
   652
  (\<lambda>i. (fst (p (s i)), prod (p\<langle>s i,s (Suc i)\<rangle>)))"
krauss@22359
   653
krauss@22359
   654
lemma ipath_contract:
krauss@22359
   655
  assumes [simp]: "increasing s"
krauss@22359
   656
  assumes ipath: "has_ipath G p"
krauss@22359
   657
  shows "has_ipath (tcl G) (contract s p)"
krauss@22359
   658
  unfolding has_ipath_def 
krauss@22359
   659
proof
krauss@22359
   660
  fix i
krauss@22359
   661
  let ?p = "p\<langle>s i,s (Suc i)\<rangle>"
krauss@22359
   662
krauss@22359
   663
  from increasing_strict 
krauss@22359
   664
	have "fst (p (s (Suc i))) = end_node ?p" by simp
krauss@22359
   665
  moreover
krauss@22359
   666
  from increasing_strict[of s i "Suc i"] have "snd ?p \<noteq> []"
krauss@22359
   667
    by (simp add:sub_path_def)
krauss@22359
   668
  moreover
krauss@22359
   669
  from ipath increasing_weak[of s] have "has_fpath G ?p"
krauss@22359
   670
    by (rule sub_path_is_path) auto
krauss@22359
   671
  ultimately
krauss@22359
   672
  show "has_edge (tcl G) 
krauss@22359
   673
    (fst (contract s p i)) (snd (contract s p i)) (fst (contract s p (Suc i)))"
krauss@22359
   674
    unfolding contract_def plus_paths
krauss@22359
   675
    by (intro exI) auto
krauss@22359
   676
qed
krauss@22359
   677
krauss@22359
   678
lemma prod_unfold:
krauss@22359
   679
  "i < j \<Longrightarrow> prod (p\<langle>i,j\<rangle>) 
krauss@22359
   680
  = snd (p i) * prod (p\<langle>Suc i, j\<rangle>)"
krauss@22359
   681
  unfolding prod_def
krauss@22359
   682
  by (simp add:sub_path_def upt_rec[of "i" j])
krauss@22359
   683
krauss@22359
   684
krauss@22359
   685
lemma sub_path_loop:
krauss@22359
   686
  assumes "0 < k"
krauss@22359
   687
  assumes k:"k = length (snd loop)"
krauss@22359
   688
  assumes loop: "fst loop = end_node loop"
krauss@22359
   689
  shows "(omega loop)\<langle>k * i,k * Suc i\<rangle> = loop" (is "?\<omega> = loop")
krauss@22359
   690
proof (rule prod_eqI)
krauss@22359
   691
  show "fst ?\<omega> = fst loop"
krauss@22359
   692
    by (auto simp:path_loop_def path_nth_def split_def k)
haftmann@22422
   693
krauss@22359
   694
  show "snd ?\<omega> = snd loop"
krauss@22359
   695
  proof (rule nth_equalityI[rule_format])
krauss@22359
   696
    show leneq: "length (snd ?\<omega>) = length (snd loop)"
krauss@22359
   697
      unfolding sub_path_def k by simp
krauss@22359
   698
krauss@22359
   699
    fix j assume "j < length (snd (?\<omega>))"
krauss@22359
   700
    with leneq and k have "j < k" by simp
krauss@22359
   701
krauss@22359
   702
    have a: "\<And>i. fst (path_nth loop (Suc i mod k))
krauss@22359
   703
      = snd (snd (path_nth loop (i mod k)))"
krauss@22359
   704
      unfolding k
krauss@22359
   705
      apply (rule path_loop_connect[OF loop])
krauss@22359
   706
      by (insert prems, auto)
krauss@22359
   707
krauss@22359
   708
    from `j < k` 
krauss@22359
   709
    show "snd ?\<omega> ! j = snd loop ! j"
krauss@22359
   710
      unfolding sub_path_def
krauss@22359
   711
      apply (simp add:path_loop_def split_def add_ac)
krauss@22359
   712
      apply (simp add:a k[symmetric])
krauss@22359
   713
      by (simp add:path_nth_def)
krauss@22359
   714
  qed
krauss@22359
   715
qed
krauss@22359
   716
krauss@22359
   717
end