src/HOL/Library/Graphs.thy
changeset 22626 7e35b6c8ab5b
parent 22452 8a86fd2a1bf0
child 22660 2d1179ad431c
equal deleted inserted replaced
22625:a2967023d674 22626:7e35b6c8ab5b
    58   shows "G = H"
    58   shows "G = H"
    59   using prems
    59   using prems
    60   by (cases G, cases H, auto simp:split_paired_all has_edge_def)
    60   by (cases G, cases H, auto simp:split_paired_all has_edge_def)
    61 
    61 
    62 
    62 
    63 instance graph :: (type, times) times 
    63 instance graph :: (type, type) "{comm_monoid_add}"
    64   graph_mult_def: "G * H == grcomp G H" ..
    64   graph_zero_def: "0 == Graph {}" 
       
    65   graph_plus_def: "G + H == Graph (dest_graph G \<union> dest_graph H)"
       
    66 proof
       
    67   fix x y z :: "('a,'b) graph"
       
    68 
       
    69   show "x + y + z = x + (y + z)" 
       
    70    and "x + y = y + x" 
       
    71    and "0 + x = x"
       
    72   unfolding graph_plus_def graph_zero_def 
       
    73   by auto
       
    74 qed
       
    75 
       
    76 
       
    77 instance graph :: (type, type) "{distrib_lattice, complete_lattice}"
       
    78   graph_leq_def: "G \<le> H \<equiv> dest_graph G \<subseteq> dest_graph H"
       
    79   graph_less_def: "G < H \<equiv> dest_graph G \<subset> dest_graph H"
       
    80   "inf G H \<equiv> Graph (dest_graph G \<inter> dest_graph H)"
       
    81   "sup G H \<equiv> G + H"
       
    82   Inf_graph_def: "Inf == \<lambda>Gs. Graph (\<Inter>(dest_graph ` Gs))"
       
    83 proof
       
    84   fix x y z :: "('a,'b) graph"
       
    85 
       
    86   show "(x < y) = (x \<le> y \<and> x \<noteq> y)"
       
    87     unfolding graph_leq_def graph_less_def
       
    88     by (cases x, cases y) auto
       
    89 
       
    90   show "x \<le> x" unfolding graph_leq_def ..
       
    91 
       
    92   show "\<lbrakk>x \<le> y; y \<le> x\<rbrakk> \<Longrightarrow> x = y" unfolding graph_leq_def 
       
    93     by (cases x, cases y) simp
       
    94 
       
    95   show "inf x y \<le> x" "inf x y \<le> y"
       
    96     "\<lbrakk>x \<le> y; x \<le> z\<rbrakk> \<Longrightarrow> x \<le> inf y z"
       
    97     unfolding inf_graph_def graph_leq_def by auto
       
    98 
       
    99   show "x \<le> sup x y" "y \<le> sup x y"
       
   100     "\<lbrakk>y \<le> x; z \<le> x\<rbrakk> \<Longrightarrow> sup y z \<le> x"
       
   101     unfolding sup_graph_def graph_leq_def graph_plus_def by auto
       
   102   
       
   103   show "sup x (inf y z) = inf (sup x y) (sup x z)"
       
   104     unfolding inf_graph_def sup_graph_def graph_leq_def graph_plus_def by auto
       
   105 
       
   106   fix A :: "('a, 'b) graph set"
       
   107   show "x \<in> A \<Longrightarrow> Inf A \<le> x"
       
   108     and "(\<And>x. x \<in> A \<Longrightarrow> z \<le> x) \<Longrightarrow> z \<le> Inf A"
       
   109     unfolding Inf_graph_def graph_leq_def by auto
       
   110 
       
   111   from order_trans
       
   112   show "\<lbrakk>x \<le> y; y \<le> z\<rbrakk> \<Longrightarrow> x \<le> z" 
       
   113     unfolding graph_leq_def .
       
   114 qed auto
       
   115 
       
   116 lemma in_grplus:
       
   117   "has_edge (G + H) p b q = (has_edge G p b q \<or> has_edge H p b q)"
       
   118   by (cases G, cases H, auto simp:has_edge_def graph_plus_def)
       
   119 
       
   120 lemma in_grzero:
       
   121   "has_edge 0 p b q = False"
       
   122   by (simp add:graph_zero_def has_edge_def)
       
   123 
       
   124 
       
   125 
       
   126 subsection {* Multiplicative Structure *}
       
   127 
       
   128 instance graph :: (type, times) mult_zero
       
   129   graph_mult_def: "G * H == grcomp G H" 
       
   130 proof
       
   131   fix a :: "('a, 'b) graph"
       
   132 
       
   133   show "0 * a = 0" 
       
   134     unfolding graph_mult_def graph_zero_def
       
   135     by (cases a) (simp add:grcomp.simps)
       
   136   show "a * 0 = 0" 
       
   137     unfolding graph_mult_def graph_zero_def
       
   138     by (cases a) (simp add:grcomp.simps)
       
   139 qed
    65 
   140 
    66 instance graph :: (type, one) one 
   141 instance graph :: (type, one) one 
    67   graph_one_def: "1 == Graph { (x, 1, x) |x. True}" ..
   142   graph_one_def: "1 == Graph { (x, 1, x) |x. True}" ..
    68 
       
    69 instance graph :: (type, type) zero 
       
    70   graph_zero_def: "0 == Graph {}" ..
       
    71 
       
    72 instance graph :: (type, type) plus 
       
    73   graph_plus_def: "G + H == Graph (dest_graph G \<union> dest_graph H)" ..
       
    74 
       
    75 
       
    76 subsection {* Simprules for the graph operations *}
       
    77 
   143 
    78 lemma in_grcomp:
   144 lemma in_grcomp:
    79   "has_edge (G * H) p b q
   145   "has_edge (G * H) p b q
    80   = (\<exists>k e e'. has_edge G p e k \<and> has_edge H k e' q \<and> b = e * e')"
   146   = (\<exists>k e e'. has_edge G p e k \<and> has_edge H k e' q \<and> b = e * e')"
    81   by (cases G, cases H) (auto simp:graph_mult_def has_edge_def image_def)
   147   by (cases G, cases H) (auto simp:graph_mult_def has_edge_def image_def)
    82 
   148 
    83 lemma in_grunit:
   149 lemma in_grunit:
    84   "has_edge 1 p b q = (p = q \<and> b = 1)"
   150   "has_edge 1 p b q = (p = q \<and> b = 1)"
    85   by (auto simp:graph_one_def has_edge_def)
   151   by (auto simp:graph_one_def has_edge_def)
    86 
       
    87 lemma in_grplus:
       
    88   "has_edge (G + H) p b q = (has_edge G p b q \<or> has_edge H p b q)"
       
    89   by (cases G, cases H, auto simp:has_edge_def graph_plus_def)
       
    90 
       
    91 lemma in_grzero:
       
    92   "has_edge 0 p b q = False"
       
    93   by (simp add:graph_zero_def has_edge_def)
       
    94 
       
    95 
   152 
    96 instance graph :: (type, semigroup_mult) semigroup_mult
   153 instance graph :: (type, semigroup_mult) semigroup_mult
    97 proof
   154 proof
    98   fix G1 G2 G3 :: "('a,'b) graph"
   155   fix G1 G2 G3 :: "('a,'b) graph"
    99   
   156   
   110     show "\<dots> = has_edge (G1 * (G2 * G3)) p J q"
   167     show "\<dots> = has_edge (G1 * (G2 * G3)) p J q"
   111       by (simp only:in_grcomp mult_assoc) blast
   168       by (simp only:in_grcomp mult_assoc) blast
   112   qed
   169   qed
   113 qed
   170 qed
   114 
   171 
   115 instance graph :: (type, monoid_mult) monoid_mult
       
   116 proof
       
   117   fix G1 G2 G3 :: "('a,'b) graph"
       
   118   
       
   119   show "1 * G1 = G1" 
       
   120     by (rule graph_ext) (auto simp:in_grcomp in_grunit)
       
   121   show "G1 * 1 = G1"
       
   122     by (rule graph_ext) (auto simp:in_grcomp in_grunit)
       
   123 qed
       
   124 
       
   125 
       
   126 lemma grcomp_rdist:
       
   127   fixes G :: "('a::type, 'b::semigroup_mult) graph"
       
   128   shows "G * (H + I) = G * H + G * I"
       
   129   by (rule graph_ext, simp add:in_grcomp in_grplus) blast
       
   130 
       
   131 lemma grcomp_ldist:
       
   132   fixes G :: "('a::type, 'b::semigroup_mult) graph"
       
   133   shows "(G + H) * I = G * I + H * I"
       
   134   by (rule graph_ext, simp add:in_grcomp in_grplus) blast
       
   135 
       
   136 fun grpow :: "nat \<Rightarrow> ('a::type, 'b::monoid_mult) graph \<Rightarrow> ('a, 'b) graph"
   172 fun grpow :: "nat \<Rightarrow> ('a::type, 'b::monoid_mult) graph \<Rightarrow> ('a, 'b) graph"
   137 where
   173 where
   138   "grpow 0 A = 1"
   174   "grpow 0 A = 1"
   139 | "grpow (Suc n) A = A * (grpow n A)"
   175 | "grpow (Suc n) A = A * (grpow n A)"
   140 
   176 
   141 
   177 instance graph :: (type, monoid_mult) 
   142 instance graph :: (type, monoid_mult) recpower 
   178   "{semiring_1,idem_add,recpower,star}"
   143   graph_pow_def: "A ^ n == grpow n A" 
   179   graph_pow_def: "A ^ n == grpow n A"
   144   by default (simp_all add:graph_pow_def)
   180   graph_star_def: "star G == (SUP n. G ^ n)" 
   145 
   181 proof
   146 subsection {* Order on Graphs *}
   182   fix a b c :: "('a, 'b) graph"
   147 
       
   148 instance graph :: (type, type) order
       
   149   graph_leq_def: "G \<le> H \<equiv> dest_graph G \<subseteq> dest_graph H"
       
   150   graph_less_def: "G < H \<equiv> dest_graph G \<subset> dest_graph H"
       
   151 proof
       
   152   fix x y z :: "('a,'b) graph"
       
   153 
       
   154   show "x \<le> x" unfolding graph_leq_def ..
       
   155   
   183   
   156   from order_trans
   184   show "1 * a = a" 
   157   show "\<lbrakk>x \<le> y; y \<le> z\<rbrakk> \<Longrightarrow> x \<le> z" unfolding graph_leq_def .
   185     by (rule graph_ext) (auto simp:in_grcomp in_grunit)
   158 
   186   show "a * 1 = a"
   159   show "\<lbrakk>x \<le> y; y \<le> x\<rbrakk> \<Longrightarrow> x = y" unfolding graph_leq_def 
   187     by (rule graph_ext) (auto simp:in_grcomp in_grunit)
   160     by (cases x, cases y) simp
       
   161 
       
   162   show "(x < y) = (x \<le> y \<and> x \<noteq> y)"
       
   163     unfolding graph_leq_def graph_less_def
       
   164     by (cases x, cases y) auto
       
   165 qed
       
   166 
       
   167 instance graph :: (type, type) distrib_lattice
       
   168   "inf G H \<equiv> Graph (dest_graph G \<inter> dest_graph H)"
       
   169   "sup G H \<equiv> G + H"
       
   170   by default (auto simp add: split_graph_all graph_plus_def inf_graph_def sup_graph_def graph_leq_def graph_less_def)
       
   171 
       
   172 instance graph :: (type, type) complete_lattice
       
   173   Inf_graph_def: "Inf == \<lambda>Gs. Graph (\<Inter>(dest_graph ` Gs))"
       
   174   by default (auto simp: Inf_graph_def graph_leq_def le_fun_def le_bool_def in_grplus has_edge_def)
       
   175 
       
   176 instance graph :: (type, monoid_mult) semiring_1
       
   177 proof
       
   178   fix a b c :: "('a, 'b) graph"
       
   179 
       
   180   show "a + b + c = a + (b + c)" 
       
   181     and "a + b = b + a" unfolding graph_plus_def
       
   182     by auto
       
   183 
       
   184   show "0 + a = a" unfolding graph_zero_def graph_plus_def
       
   185     by simp
       
   186 
       
   187   show "0 * a = 0" "a * 0 = 0" unfolding graph_zero_def graph_mult_def
       
   188     by (cases a, simp)+
       
   189 
   188 
   190   show "(a + b) * c = a * c + b * c"
   189   show "(a + b) * c = a * c + b * c"
   191     by (rule graph_ext, simp add:in_grcomp in_grplus) blast
   190     by (rule graph_ext, simp add:in_grcomp in_grplus) blast
   192 
   191 
   193   show "a * (b + c) = a * b + a * c"
   192   show "a * (b + c) = a * b + a * c"
   194     by (rule graph_ext, simp add:in_grcomp in_grplus) blast
   193     by (rule graph_ext, simp add:in_grcomp in_grplus) blast
   195 
   194 
   196   show "(0::('a,'b) graph) \<noteq> 1" unfolding graph_zero_def graph_one_def
   195   show "(0::('a,'b) graph) \<noteq> 1" unfolding graph_zero_def graph_one_def
   197     by simp
   196     by simp
   198 qed
   197 
   199 
       
   200 instance graph :: (type, monoid_mult) idem_add
       
   201 proof
       
   202   fix a :: "('a, 'b) graph"
       
   203   show "a + a = a" unfolding graph_plus_def by simp
   198   show "a + a = a" unfolding graph_plus_def by simp
   204 qed
   199   
   205 
   200   show "a ^ 0 = 1" "\<And>n. a ^ (Suc n) = a * a ^ n"
   206 
   201     unfolding graph_pow_def by simp_all
   207 (* define star on graphs *)
   202 qed
   208 
       
   209 
       
   210 instance graph :: (type, monoid_mult) star
       
   211   graph_star_def: "star G == (SUP n. G ^ n)" ..
       
   212 
   203 
   213 
   204 
   214 lemma graph_leqI:
   205 lemma graph_leqI:
   215   assumes "\<And>n e n'. has_edge G n e n' \<Longrightarrow> has_edge H n e n'"
   206   assumes "\<And>n e n'. has_edge G n e n' \<Longrightarrow> has_edge H n e n'"
   216   shows "G \<le> H"
   207   shows "G \<le> H"
   225   assumes "has_edge H n e n' \<Longrightarrow> P"
   216   assumes "has_edge H n e n' \<Longrightarrow> P"
   226   shows P
   217   shows P
   227   using prems
   218   using prems
   228   by (auto simp: in_grplus)
   219   by (auto simp: in_grplus)
   229 
   220 
   230 
       
   231 
       
   232 lemma 
   221 lemma 
   233   assumes "x \<in> S k"
   222   assumes "x \<in> S k"
   234   shows "x \<in> (\<Union>k. S k)"
   223   shows "x \<in> (\<Union>k. S k)"
   235   using prems by blast
   224   using prems by blast
   236 
   225 
   240   using prems unfolding graph_leq_def
   229   using prems unfolding graph_leq_def
   241   by auto
   230   by auto
   242 
   231 
   243 lemma Sup_graph_eq:
   232 lemma Sup_graph_eq:
   244   "(SUP n. Graph (G n)) = Graph (\<Union>n. G n)"
   233   "(SUP n. Graph (G n)) = Graph (\<Union>n. G n)"
   245   unfolding SUPR_def
   234 proof (rule order_antisym)
   246   apply (rule order_antisym)
   235   show "(SUP n. Graph (G n)) \<le> Graph (\<Union>n. G n)"
   247   apply (rule Sup_least)
   236     by  (rule SUP_leI) (auto simp add: graph_leq_def)
   248   apply auto
   237 
   249   apply (simp add:graph_leq_def)
   238   show "Graph (\<Union>n. G n) \<le> (SUP n. Graph (G n))"
   250   apply auto
   239   by (rule graph_union_least, rule le_SUPI', rule) 
   251   apply (rule graph_union_least)
   240 qed
   252   apply (rule Sup_upper)
       
   253   by auto
       
   254 
   241 
   255 lemma has_edge_leq: "has_edge G p b q = (Graph {(p,b,q)} \<le> G)"
   242 lemma has_edge_leq: "has_edge G p b q = (Graph {(p,b,q)} \<le> G)"
   256   unfolding has_edge_def graph_leq_def
   243   unfolding has_edge_def graph_leq_def
   257   by (cases G) simp
   244   by (cases G) simp
   258 
   245