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