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