src/HOL/SizeChange/Graphs.thy
changeset 25764 878c37886eed
parent 25502 9200b36280c0
child 27682 25aceefd4786
equal deleted inserted replaced
25763:474f8ba9dfa9 25764:878c37886eed
    12 subsection {* Basic types, Size Change Graphs *}
    12 subsection {* Basic types, Size Change Graphs *}
    13 
    13 
    14 datatype ('a, 'b) graph = 
    14 datatype ('a, 'b) graph = 
    15   Graph "('a \<times> 'b \<times> 'a) set"
    15   Graph "('a \<times> 'b \<times> 'a) set"
    16 
    16 
    17 fun dest_graph :: "('a, 'b) graph \<Rightarrow> ('a \<times> 'b \<times> 'a) set"
    17 primrec dest_graph :: "('a, 'b) graph \<Rightarrow> ('a \<times> 'b \<times> 'a) set"
    18   where "dest_graph (Graph G) = G"
    18   where "dest_graph (Graph G) = G"
    19 
    19 
    20 lemma graph_dest_graph[simp]:
    20 lemma graph_dest_graph[simp]:
    21   "Graph (dest_graph G) = G"
    21   "Graph (dest_graph G) = G"
    22   by (cases G) simp
    22   by (cases G) simp
    58   shows "G = H"
    58   shows "G = H"
    59   using assms
    59   using assms
    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, type) "{comm_monoid_add}"
    63 instantiation graph :: (type, type) comm_monoid_add
    64   graph_zero_def: "0 == Graph {}" 
    64 begin
    65   graph_plus_def: "G + H == Graph (dest_graph G \<union> dest_graph H)"
    65 
    66 proof
    66 definition
       
    67   graph_zero_def: "0 = Graph {}" 
       
    68 
       
    69 definition
       
    70   graph_plus_def [code func del]: "G + H = Graph (dest_graph G \<union> dest_graph H)"
       
    71 
       
    72 instance proof
    67   fix x y z :: "('a,'b) graph"
    73   fix x y z :: "('a,'b) graph"
    68 
       
    69   show "x + y + z = x + (y + z)" 
    74   show "x + y + z = x + (y + z)" 
    70    and "x + y = y + x" 
    75    and "x + y = y + x" 
    71    and "0 + x = x"
    76    and "0 + x = x"
    72   unfolding graph_plus_def graph_zero_def 
    77   unfolding graph_plus_def graph_zero_def by auto
    73   by auto
    78 qed
    74 qed
    79 
    75 
    80 end
    76 lemmas [code func del] = graph_plus_def
    81 
    77 
    82 instantiation graph :: (type, type) "{distrib_lattice, complete_lattice}"
    78 instance graph :: (type, type) "{distrib_lattice, complete_lattice}"
    83 begin
    79   graph_leq_def: "G \<le> H \<equiv> dest_graph G \<subseteq> dest_graph H"
    84 
    80   graph_less_def: "G < H \<equiv> dest_graph G \<subset> dest_graph H"
    85 definition
    81   "inf G H \<equiv> Graph (dest_graph G \<inter> dest_graph H)"
    86   graph_leq_def [code func del]: "G \<le> H \<longleftrightarrow> dest_graph G \<subseteq> dest_graph H"
    82   "sup (G \<Colon> ('a, 'b) graph)  H \<equiv> G + H"
    87 
    83   Inf_graph_def: "Inf \<equiv> \<lambda>Gs. Graph (\<Inter>(dest_graph ` Gs))"
    88 definition
    84   Sup_graph_def: "Sup \<equiv> \<lambda>Gs. Graph (\<Union>(dest_graph ` Gs))"
    89   graph_less_def [code func del]: "G < H \<longleftrightarrow> dest_graph G \<subset> dest_graph H"
    85 proof
    90 
       
    91 definition
       
    92   [code func del]: "inf G H = Graph (dest_graph G \<inter> dest_graph H)"
       
    93 
       
    94 definition
       
    95   [code func del]: "sup (G \<Colon> ('a, 'b) graph) H = G + H"
       
    96 
       
    97 definition
       
    98   Inf_graph_def [code func del]: "Inf = (\<lambda>Gs. Graph (\<Inter>(dest_graph ` Gs)))"
       
    99 
       
   100 definition
       
   101   Sup_graph_def [code func del]: "Sup = (\<lambda>Gs. Graph (\<Union>(dest_graph ` Gs)))"
       
   102 
       
   103 instance proof
    86   fix x y z :: "('a,'b) graph"
   104   fix x y z :: "('a,'b) graph"
    87   fix A :: "('a, 'b) graph set"
   105   fix A :: "('a, 'b) graph set"
    88 
   106 
    89   show "(x < y) = (x \<le> y \<and> x \<noteq> y)"
   107   show "(x < y) = (x \<le> y \<and> x \<noteq> y)"
    90     unfolding graph_leq_def graph_less_def
   108     unfolding graph_leq_def graph_less_def
   128 
   146 
   129   { assume "\<And>x. x \<in> A \<Longrightarrow> x \<le> z" thus "Sup A \<le> z"
   147   { assume "\<And>x. x \<in> A \<Longrightarrow> x \<le> z" thus "Sup A \<le> z"
   130     unfolding Sup_graph_def graph_leq_def by auto }
   148     unfolding Sup_graph_def graph_leq_def by auto }
   131 qed
   149 qed
   132 
   150 
   133 lemmas [code func del] = graph_leq_def graph_less_def
   151 end
   134   inf_graph_def sup_graph_def Inf_graph_def Sup_graph_def
       
   135 
   152 
   136 lemma in_grplus:
   153 lemma in_grplus:
   137   "has_edge (G + H) p b q = (has_edge G p b q \<or> has_edge H p b q)"
   154   "has_edge (G + H) p b q = (has_edge G p b q \<or> has_edge H p b q)"
   138   by (cases G, cases H, auto simp:has_edge_def graph_plus_def)
   155   by (cases G, cases H, auto simp:has_edge_def graph_plus_def)
   139 
   156 
   142   by (simp add:graph_zero_def has_edge_def)
   159   by (simp add:graph_zero_def has_edge_def)
   143 
   160 
   144 
   161 
   145 subsubsection {* Multiplicative Structure *}
   162 subsubsection {* Multiplicative Structure *}
   146 
   163 
   147 instance graph :: (type, times) mult_zero
   164 instantiation graph :: (type, times) mult_zero
   148   graph_mult_def: "G * H == grcomp G H" 
   165 begin
   149 proof
   166 
       
   167 definition
       
   168   graph_mult_def [code func del]: "G * H = grcomp G H" 
       
   169 
       
   170 instance proof
   150   fix a :: "('a, 'b) graph"
   171   fix a :: "('a, 'b) graph"
   151 
   172 
   152   show "0 * a = 0" 
   173   show "0 * a = 0" 
   153     unfolding graph_mult_def graph_zero_def
   174     unfolding graph_mult_def graph_zero_def
   154     by (cases a) (simp add:grcomp.simps)
   175     by (cases a) (simp add:grcomp.simps)
   155   show "a * 0 = 0" 
   176   show "a * 0 = 0" 
   156     unfolding graph_mult_def graph_zero_def
   177     unfolding graph_mult_def graph_zero_def
   157     by (cases a) (simp add:grcomp.simps)
   178     by (cases a) (simp add:grcomp.simps)
   158 qed
   179 qed
   159 
   180 
   160 lemmas [code func del] = graph_mult_def
   181 end
   161 
   182 
   162 instance graph :: (type, one) one 
   183 instantiation graph :: (type, one) one 
   163   graph_one_def: "1 == Graph { (x, 1, x) |x. True}" ..
   184 begin
       
   185 
       
   186 definition
       
   187   graph_one_def: "1 = Graph { (x, 1, x) |x. True}"
       
   188 
       
   189 instance ..
       
   190 
       
   191 end
   164 
   192 
   165 lemma in_grcomp:
   193 lemma in_grcomp:
   166   "has_edge (G * H) p b q
   194   "has_edge (G * H) p b q
   167   = (\<exists>k e e'. has_edge G p e k \<and> has_edge H k e' q \<and> b = e * e')"
   195   = (\<exists>k e e'. has_edge G p e k \<and> has_edge H k e' q \<and> b = e * e')"
   168   by (cases G, cases H) (auto simp:graph_mult_def has_edge_def image_def)
   196   by (cases G, cases H) (auto simp:graph_mult_def has_edge_def image_def)
   188     show "\<dots> = has_edge (G1 * (G2 * G3)) p J q"
   216     show "\<dots> = has_edge (G1 * (G2 * G3)) p J q"
   189       by (simp only:in_grcomp mult_assoc) blast
   217       by (simp only:in_grcomp mult_assoc) blast
   190   qed
   218   qed
   191 qed
   219 qed
   192 
   220 
   193 fun grpow :: "nat \<Rightarrow> ('a::type, 'b::monoid_mult) graph \<Rightarrow> ('a, 'b) graph"
   221 instantiation graph :: (type, monoid_mult) "{semiring_1, idem_add, recpower, star}"
   194 where
   222 begin
   195   "grpow 0 A = 1"
   223 
   196 | "grpow (Suc n) A = A * (grpow n A)"
   224 primrec power_graph :: "('a\<Colon>type, 'b\<Colon>monoid_mult) graph \<Rightarrow> nat => ('a, 'b) graph"
   197 
   225 where
   198 instance graph :: (type, monoid_mult)
   226   "(A \<Colon> ('a, 'b) graph) ^ 0 = 1"
   199   "{semiring_1,idem_add,recpower,star}"
   227 | "(A \<Colon> ('a, 'b) graph) ^ Suc n = A * (A ^ n)"
   200   graph_pow_def: "A ^ n == grpow n A"
   228 
   201   graph_star_def: "star (G \<Colon> ('a, 'b) graph) == (SUP n. G ^ n)" 
   229 definition
   202 proof
   230   graph_star_def: "star (G \<Colon> ('a, 'b) graph) = (SUP n. G ^ n)" 
       
   231 
       
   232 instance proof
   203   fix a b c :: "('a, 'b) graph"
   233   fix a b c :: "('a, 'b) graph"
   204   
   234   
   205   show "1 * a = a" 
   235   show "1 * a = a" 
   206     by (rule graph_ext) (auto simp:in_grcomp in_grunit)
   236     by (rule graph_ext) (auto simp:in_grcomp in_grunit)
   207   show "a * 1 = a"
   237   show "a * 1 = a"
   217     by simp
   247     by simp
   218 
   248 
   219   show "a + a = a" unfolding graph_plus_def by simp
   249   show "a + a = a" unfolding graph_plus_def by simp
   220   
   250   
   221   show "a ^ 0 = 1" "\<And>n. a ^ (Suc n) = a * a ^ n"
   251   show "a ^ 0 = 1" "\<And>n. a ^ (Suc n) = a * a ^ n"
   222     unfolding graph_pow_def by simp_all
   252     by simp_all
   223 qed
   253 qed
       
   254 
       
   255 end
   224 
   256 
   225 lemma graph_leqI:
   257 lemma graph_leqI:
   226   assumes "\<And>n e n'. has_edge G n e n' \<Longrightarrow> has_edge H n e n'"
   258   assumes "\<And>n e n'. has_edge G n e n' \<Longrightarrow> has_edge H n e n'"
   227   shows "G \<le> H"
   259   shows "G \<le> H"
   228   using assms
   260   using assms
   307   by (simp add:power_Suc power_commutes)
   339   by (simp add:power_Suc power_commutes)
   308 
   340 
   309 
   341 
   310 lemma in_tcl: 
   342 lemma in_tcl: 
   311   "has_edge (tcl G) a x b = (\<exists>n>0. has_edge (G ^ n) a x b)"
   343   "has_edge (tcl G) a x b = (\<exists>n>0. has_edge (G ^ n) a x b)"
   312   apply (auto simp: tcl_is_SUP in_SUP)
   344   apply (auto simp: tcl_is_SUP in_SUP simp del: power_graph.simps)
   313   apply (rule_tac x = "n - 1" in exI, auto)
   345   apply (rule_tac x = "n - 1" in exI, auto)
   314   done
   346   done
   315 
   347 
   316 
   348 
   317 subsection {* Infinite Paths *}
   349 subsection {* Infinite Paths *}