src/HOL/Library/Transitive_Closure_Table.thy
changeset 33649 854173fcd21c
child 33870 5b0d23d2c08f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Transitive_Closure_Table.thy	Thu Nov 12 20:38:57 2009 +0100
     1.3 @@ -0,0 +1,230 @@
     1.4 +(* Author: Stefan Berghofer, Lukas Bulwahn, TU Muenchen *)
     1.5 +
     1.6 +header {* A tabled implementation of the reflexive transitive closure *}
     1.7 +
     1.8 +theory Transitive_Closure_Table
     1.9 +imports Main
    1.10 +begin
    1.11 +
    1.12 +inductive rtrancl_path :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a \<Rightarrow> bool"
    1.13 +  for r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
    1.14 +where
    1.15 +  base: "rtrancl_path r x [] x"
    1.16 +| step: "r x y \<Longrightarrow> rtrancl_path r y ys z \<Longrightarrow> rtrancl_path r x (y # ys) z"
    1.17 +
    1.18 +lemma rtranclp_eq_rtrancl_path: "r\<^sup>*\<^sup>* x y = (\<exists>xs. rtrancl_path r x xs y)"
    1.19 +proof
    1.20 +  assume "r\<^sup>*\<^sup>* x y"
    1.21 +  then show "\<exists>xs. rtrancl_path r x xs y"
    1.22 +  proof (induct rule: converse_rtranclp_induct)
    1.23 +    case 1
    1.24 +    have "rtrancl_path r y [] y" by (rule rtrancl_path.base)
    1.25 +    then show ?case ..
    1.26 +  next
    1.27 +    case (2 x z)
    1.28 +    from `\<exists>xs. rtrancl_path r z xs y`
    1.29 +    obtain xs where "rtrancl_path r z xs y" ..
    1.30 +    with `r x z` have "rtrancl_path r x (z # xs) y"
    1.31 +      by (rule rtrancl_path.step)
    1.32 +    then show ?case ..
    1.33 +  qed
    1.34 +next
    1.35 +  assume "\<exists>xs. rtrancl_path r x xs y"
    1.36 +  then obtain xs where "rtrancl_path r x xs y" ..
    1.37 +  then show "r\<^sup>*\<^sup>* x y"
    1.38 +  proof induct
    1.39 +    case (base x)
    1.40 +    show ?case by (rule rtranclp.rtrancl_refl)
    1.41 +  next
    1.42 +    case (step x y ys z)
    1.43 +    from `r x y` `r\<^sup>*\<^sup>* y z` show ?case
    1.44 +      by (rule converse_rtranclp_into_rtranclp)
    1.45 +  qed
    1.46 +qed
    1.47 +
    1.48 +lemma rtrancl_path_trans:
    1.49 +  assumes xy: "rtrancl_path r x xs y"
    1.50 +  and yz: "rtrancl_path r y ys z"
    1.51 +  shows "rtrancl_path r x (xs @ ys) z" using xy yz
    1.52 +proof (induct arbitrary: z)
    1.53 +  case (base x)
    1.54 +  then show ?case by simp
    1.55 +next
    1.56 +  case (step x y xs)
    1.57 +  then have "rtrancl_path r y (xs @ ys) z"
    1.58 +    by simp
    1.59 +  with `r x y` have "rtrancl_path r x (y # (xs @ ys)) z"
    1.60 +    by (rule rtrancl_path.step)
    1.61 +  then show ?case by simp
    1.62 +qed
    1.63 +
    1.64 +lemma rtrancl_path_appendE:
    1.65 +  assumes xz: "rtrancl_path r x (xs @ y # ys) z"
    1.66 +  obtains "rtrancl_path r x (xs @ [y]) y" and "rtrancl_path r y ys z" using xz
    1.67 +proof (induct xs arbitrary: x)
    1.68 +  case Nil
    1.69 +  then have "rtrancl_path r x (y # ys) z" by simp
    1.70 +  then obtain xy: "r x y" and yz: "rtrancl_path r y ys z"
    1.71 +    by cases auto
    1.72 +  from xy have "rtrancl_path r x [y] y"
    1.73 +    by (rule rtrancl_path.step [OF _ rtrancl_path.base])
    1.74 +  then have "rtrancl_path r x ([] @ [y]) y" by simp
    1.75 +  then show ?thesis using yz by (rule Nil)
    1.76 +next
    1.77 +  case (Cons a as)
    1.78 +  then have "rtrancl_path r x (a # (as @ y # ys)) z" by simp
    1.79 +  then obtain xa: "r x a" and az: "rtrancl_path r a (as @ y # ys) z"
    1.80 +    by cases auto
    1.81 +  show ?thesis
    1.82 +  proof (rule Cons(1) [OF _ az])
    1.83 +    assume "rtrancl_path r y ys z"
    1.84 +    assume "rtrancl_path r a (as @ [y]) y"
    1.85 +    with xa have "rtrancl_path r x (a # (as @ [y])) y"
    1.86 +      by (rule rtrancl_path.step)
    1.87 +    then have "rtrancl_path r x ((a # as) @ [y]) y"
    1.88 +      by simp
    1.89 +    then show ?thesis using `rtrancl_path r y ys z`
    1.90 +      by (rule Cons(2))
    1.91 +  qed
    1.92 +qed
    1.93 +
    1.94 +lemma rtrancl_path_distinct:
    1.95 +  assumes xy: "rtrancl_path r x xs y"
    1.96 +  obtains xs' where "rtrancl_path r x xs' y" and "distinct (x # xs')" using xy
    1.97 +proof (induct xs rule: measure_induct_rule [of length])
    1.98 +  case (less xs)
    1.99 +  show ?case
   1.100 +  proof (cases "distinct (x # xs)")
   1.101 +    case True
   1.102 +    with `rtrancl_path r x xs y` show ?thesis by (rule less)
   1.103 +  next
   1.104 +    case False
   1.105 +    then have "\<exists>as bs cs a. x # xs = as @ [a] @ bs @ [a] @ cs"
   1.106 +      by (rule not_distinct_decomp)
   1.107 +    then obtain as bs cs a where xxs: "x # xs = as @ [a] @ bs @ [a] @ cs"
   1.108 +      by iprover
   1.109 +    show ?thesis
   1.110 +    proof (cases as)
   1.111 +      case Nil
   1.112 +      with xxs have x: "x = a" and xs: "xs = bs @ a # cs"
   1.113 +	by auto
   1.114 +      from x xs `rtrancl_path r x xs y` have cs: "rtrancl_path r x cs y"
   1.115 +	by (auto elim: rtrancl_path_appendE)
   1.116 +      from xs have "length cs < length xs" by simp
   1.117 +      then show ?thesis
   1.118 +	by (rule less(1)) (iprover intro: cs less(2))+
   1.119 +    next
   1.120 +      case (Cons d ds)
   1.121 +      with xxs have xs: "xs = ds @ a # (bs @ [a] @ cs)"
   1.122 +	by auto
   1.123 +      with `rtrancl_path r x xs y` obtain xa: "rtrancl_path r x (ds @ [a]) a"
   1.124 +        and ay: "rtrancl_path r a (bs @ a # cs) y"
   1.125 +	by (auto elim: rtrancl_path_appendE)
   1.126 +      from ay have "rtrancl_path r a cs y" by (auto elim: rtrancl_path_appendE)
   1.127 +      with xa have xy: "rtrancl_path r x ((ds @ [a]) @ cs) y"
   1.128 +	by (rule rtrancl_path_trans)
   1.129 +      from xs have "length ((ds @ [a]) @ cs) < length xs" by simp
   1.130 +      then show ?thesis
   1.131 +	by (rule less(1)) (iprover intro: xy less(2))+
   1.132 +    qed
   1.133 +  qed
   1.134 +qed
   1.135 +
   1.136 +inductive rtrancl_tab :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
   1.137 +  for r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   1.138 +where
   1.139 +  base: "rtrancl_tab r xs x x"
   1.140 +| step: "x \<notin> set xs \<Longrightarrow> r x y \<Longrightarrow> rtrancl_tab r (x # xs) y z \<Longrightarrow> rtrancl_tab r xs x z"
   1.141 +
   1.142 +lemma rtrancl_path_imp_rtrancl_tab:
   1.143 +  assumes path: "rtrancl_path r x xs y"
   1.144 +  and x: "distinct (x # xs)"
   1.145 +  and ys: "({x} \<union> set xs) \<inter> set ys = {}"
   1.146 +  shows "rtrancl_tab r ys x y" using path x ys
   1.147 +proof (induct arbitrary: ys)
   1.148 +  case base
   1.149 +  show ?case by (rule rtrancl_tab.base)
   1.150 +next
   1.151 +  case (step x y zs z)
   1.152 +  then have "x \<notin> set ys" by auto
   1.153 +  from step have "distinct (y # zs)" by simp
   1.154 +  moreover from step have "({y} \<union> set zs) \<inter> set (x # ys) = {}"
   1.155 +    by auto
   1.156 +  ultimately have "rtrancl_tab r (x # ys) y z"
   1.157 +    by (rule step)
   1.158 +  with `x \<notin> set ys` `r x y`
   1.159 +  show ?case by (rule rtrancl_tab.step)
   1.160 +qed
   1.161 +
   1.162 +lemma rtrancl_tab_imp_rtrancl_path:
   1.163 +  assumes tab: "rtrancl_tab r ys x y"
   1.164 +  obtains xs where "rtrancl_path r x xs y" using tab
   1.165 +proof induct
   1.166 +  case base
   1.167 +  from rtrancl_path.base show ?case by (rule base)
   1.168 +next
   1.169 +  case step show ?case by (iprover intro: step rtrancl_path.step)
   1.170 +qed
   1.171 +
   1.172 +lemma rtranclp_eq_rtrancl_tab_nil: "r\<^sup>*\<^sup>* x y = rtrancl_tab r [] x y"
   1.173 +proof
   1.174 +  assume "r\<^sup>*\<^sup>* x y"
   1.175 +  then obtain xs where "rtrancl_path r x xs y"
   1.176 +    by (auto simp add: rtranclp_eq_rtrancl_path)
   1.177 +  then obtain xs' where xs': "rtrancl_path r x xs' y"
   1.178 +    and distinct: "distinct (x # xs')"
   1.179 +    by (rule rtrancl_path_distinct)
   1.180 +  have "({x} \<union> set xs') \<inter> set [] = {}" by simp
   1.181 +  with xs' distinct show "rtrancl_tab r [] x y"
   1.182 +    by (rule rtrancl_path_imp_rtrancl_tab)
   1.183 +next
   1.184 +  assume "rtrancl_tab r [] x y"
   1.185 +  then obtain xs where "rtrancl_path r x xs y"
   1.186 +    by (rule rtrancl_tab_imp_rtrancl_path)
   1.187 +  then show "r\<^sup>*\<^sup>* x y"
   1.188 +    by (auto simp add: rtranclp_eq_rtrancl_path)
   1.189 +qed
   1.190 +
   1.191 +declare rtranclp_eq_rtrancl_tab_nil [code_unfold]
   1.192 +
   1.193 +declare rtranclp_eq_rtrancl_tab_nil[THEN iffD2, code_pred_intro]
   1.194 +
   1.195 +code_pred rtranclp using rtranclp_eq_rtrancl_tab_nil[THEN iffD1] by fastsimp
   1.196 +
   1.197 +subsection {* A simple example *}
   1.198 +
   1.199 +datatype ty = A | B | C
   1.200 +
   1.201 +inductive test :: "ty \<Rightarrow> ty \<Rightarrow> bool"
   1.202 +where
   1.203 +  "test A B"
   1.204 +| "test B A"
   1.205 +| "test B C"
   1.206 +
   1.207 +subsubsection {* Invoking with the SML code generator *}
   1.208 +
   1.209 +code_module Test
   1.210 +contains
   1.211 +test1 = "test\<^sup>*\<^sup>* A C"
   1.212 +test2 = "test\<^sup>*\<^sup>* C A"
   1.213 +test3 = "test\<^sup>*\<^sup>* A _"
   1.214 +test4 = "test\<^sup>*\<^sup>* _ C"
   1.215 +
   1.216 +ML "Test.test1"
   1.217 +ML "Test.test2"
   1.218 +ML "DSeq.list_of Test.test3"
   1.219 +ML "DSeq.list_of Test.test4"
   1.220 +
   1.221 +subsubsection {* Invoking with the predicate compiler and the generic code generator *}
   1.222 +
   1.223 +code_pred test .
   1.224 +
   1.225 +values "{x. test\<^sup>*\<^sup>* A C}"
   1.226 +values "{x. test\<^sup>*\<^sup>* C A}"
   1.227 +values "{x. test\<^sup>*\<^sup>* A x}"
   1.228 +values "{x. test\<^sup>*\<^sup>* x C}"
   1.229 +
   1.230 +hide const test
   1.231 +
   1.232 +end
   1.233 +