author | paulson |
Thu, 22 Apr 2010 20:39:48 +0100 | |
changeset 36278 | 6b330b1fa0c0 |
parent 35281 | 206e2f1759cc |
child 36692 | 54b64d4ad524 |
permissions | -rw-r--r-- |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
1 |
(* |
30190 | 2 |
Title: Transitivity reasoner for transitive closures of relations |
3 |
Author: Oliver Kutter, TU Muenchen |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
4 |
*) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
5 |
|
15098 | 6 |
(* |
7 |
||
8 |
The packages provides tactics trancl_tac and rtrancl_tac that prove |
|
9 |
goals of the form |
|
10 |
||
11 |
(x,y) : r^+ and (x,y) : r^* (rtrancl_tac only) |
|
12 |
||
13 |
from premises of the form |
|
14 |
||
15 |
(x,y) : r, (x,y) : r^+ and (x,y) : r^* (rtrancl_tac only) |
|
16 |
||
17 |
by reflexivity and transitivity. The relation r is determined by inspecting |
|
18 |
the conclusion. |
|
19 |
||
20 |
The package is implemented as an ML functor and thus not limited to |
|
21 |
particular constructs for transitive and reflexive-transitive |
|
22 |
closures, neither need relations be represented as sets of pairs. In |
|
23 |
order to instantiate the package for transitive closure only, supply |
|
24 |
dummy theorems to the additional rules for reflexive-transitive |
|
25 |
closures, and don't use rtrancl_tac! |
|
26 |
||
27 |
*) |
|
28 |
||
32215 | 29 |
signature TRANCL_ARITH = |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
30 |
sig |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
31 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
32 |
(* theorems for transitive closure *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
33 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
34 |
val r_into_trancl : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
35 |
(* (a,b) : r ==> (a,b) : r^+ *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
36 |
val trancl_trans : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
37 |
(* [| (a,b) : r^+ ; (b,c) : r^+ |] ==> (a,c) : r^+ *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
38 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
39 |
(* additional theorems for reflexive-transitive closure *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
40 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
41 |
val rtrancl_refl : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
42 |
(* (a,a): r^* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
43 |
val r_into_rtrancl : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
44 |
(* (a,b) : r ==> (a,b) : r^* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
45 |
val trancl_into_rtrancl : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
46 |
(* (a,b) : r^+ ==> (a,b) : r^* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
47 |
val rtrancl_trancl_trancl : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
48 |
(* [| (a,b) : r^* ; (b,c) : r^+ |] ==> (a,c) : r^+ *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
49 |
val trancl_rtrancl_trancl : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
50 |
(* [| (a,b) : r^+ ; (b,c) : r^* |] ==> (a,c) : r^+ *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
51 |
val rtrancl_trans : thm |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
52 |
(* [| (a,b) : r^* ; (b,c) : r^* |] ==> (a,c) : r^* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
53 |
|
15098 | 54 |
(* decomp: decompose a premise or conclusion |
55 |
||
56 |
Returns one of the following: |
|
57 |
||
15531 | 58 |
NONE if not an instance of a relation, |
59 |
SOME (x, y, r, s) if instance of a relation, where |
|
15098 | 60 |
x: left hand side argument, y: right hand side argument, |
61 |
r: the relation, |
|
62 |
s: the kind of closure, one of |
|
63 |
"r": the relation itself, |
|
64 |
"r^+": transitive closure of the relation, |
|
65 |
"r^*": reflexive-transitive closure of the relation |
|
32215 | 66 |
*) |
15098 | 67 |
|
32215 | 68 |
val decomp: term -> (term * term * term * string) option |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
69 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
70 |
end; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
71 |
|
32215 | 72 |
signature TRANCL_TAC = |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
73 |
sig |
32215 | 74 |
val trancl_tac: Proof.context -> int -> tactic |
75 |
val rtrancl_tac: Proof.context -> int -> tactic |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
76 |
end; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
77 |
|
32215 | 78 |
functor Trancl_Tac(Cls: TRANCL_ARITH): TRANCL_TAC = |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
79 |
struct |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
80 |
|
32215 | 81 |
|
22257
159bfab776e2
"prove" function now instantiates relation variable in order
berghofe
parents:
15574
diff
changeset
|
82 |
datatype proof |
32215 | 83 |
= Asm of int |
84 |
| Thm of proof list * thm; |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
85 |
|
22257
159bfab776e2
"prove" function now instantiates relation variable in order
berghofe
parents:
15574
diff
changeset
|
86 |
exception Cannot; (* internal exception: raised if no proof can be found *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
87 |
|
26834
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
88 |
fun decomp t = Option.map (fn (x, y, rel, r) => |
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
89 |
(Envir.beta_eta_contract x, Envir.beta_eta_contract y, |
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
90 |
Envir.beta_eta_contract rel, r)) (Cls.decomp t); |
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
91 |
|
32215 | 92 |
fun prove thy r asms = |
22257
159bfab776e2
"prove" function now instantiates relation variable in order
berghofe
parents:
15574
diff
changeset
|
93 |
let |
159bfab776e2
"prove" function now instantiates relation variable in order
berghofe
parents:
15574
diff
changeset
|
94 |
fun inst thm = |
26834
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
95 |
let val SOME (_, _, r', _) = decomp (concl_of thm) |
22257
159bfab776e2
"prove" function now instantiates relation variable in order
berghofe
parents:
15574
diff
changeset
|
96 |
in Drule.cterm_instantiate [(cterm_of thy r', cterm_of thy r)] thm end; |
159bfab776e2
"prove" function now instantiates relation variable in order
berghofe
parents:
15574
diff
changeset
|
97 |
fun pr (Asm i) = List.nth (asms, i) |
159bfab776e2
"prove" function now instantiates relation variable in order
berghofe
parents:
15574
diff
changeset
|
98 |
| pr (Thm (prfs, thm)) = map pr prfs MRS inst thm |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
99 |
in pr end; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
100 |
|
32215 | 101 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
102 |
(* Internal datatype for inequalities *) |
32215 | 103 |
datatype rel |
15098 | 104 |
= Trans of term * term * proof (* R^+ *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
105 |
| RTrans of term * term * proof; (* R^* *) |
32215 | 106 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
107 |
(* Misc functions for datatype rel *) |
15098 | 108 |
fun lower (Trans (x, _, _)) = x |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
109 |
| lower (RTrans (x,_,_)) = x; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
110 |
|
15098 | 111 |
fun upper (Trans (_, y, _)) = y |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
112 |
| upper (RTrans (_,y,_)) = y; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
113 |
|
15098 | 114 |
fun getprf (Trans (_, _, p)) = p |
32215 | 115 |
| getprf (RTrans (_,_, p)) = p; |
116 |
||
15098 | 117 |
(* ************************************************************************ *) |
118 |
(* *) |
|
119 |
(* mkasm_trancl Rel (t,n): term -> (term , int) -> rel list *) |
|
120 |
(* *) |
|
121 |
(* Analyse assumption t with index n with respect to relation Rel: *) |
|
122 |
(* If t is of the form "(x, y) : Rel" (or Rel^+), translate to *) |
|
123 |
(* an object (singleton list) of internal datatype rel. *) |
|
124 |
(* Otherwise return empty list. *) |
|
125 |
(* *) |
|
126 |
(* ************************************************************************ *) |
|
127 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
128 |
fun mkasm_trancl Rel (t, n) = |
26834
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
129 |
case decomp t of |
32215 | 130 |
SOME (x, y, rel,r) => if rel aconv Rel then |
131 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
132 |
(case r of |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
133 |
"r" => [Trans (x,y, Thm([Asm n], Cls.r_into_trancl))] |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
134 |
| "r+" => [Trans (x,y, Asm n)] |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
135 |
| "r*" => [] |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
136 |
| _ => error ("trancl_tac: unknown relation symbol")) |
32215 | 137 |
else [] |
15531 | 138 |
| NONE => []; |
32215 | 139 |
|
15098 | 140 |
(* ************************************************************************ *) |
141 |
(* *) |
|
142 |
(* mkasm_rtrancl Rel (t,n): term -> (term , int) -> rel list *) |
|
143 |
(* *) |
|
144 |
(* Analyse assumption t with index n with respect to relation Rel: *) |
|
145 |
(* If t is of the form "(x, y) : Rel" (or Rel^+ or Rel^* ), translate to *) |
|
146 |
(* an object (singleton list) of internal datatype rel. *) |
|
147 |
(* Otherwise return empty list. *) |
|
148 |
(* *) |
|
149 |
(* ************************************************************************ *) |
|
150 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
151 |
fun mkasm_rtrancl Rel (t, n) = |
26834
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
152 |
case decomp t of |
15531 | 153 |
SOME (x, y, rel, r) => if rel aconv Rel then |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
154 |
(case r of |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
155 |
"r" => [ Trans (x,y, Thm([Asm n], Cls.r_into_trancl))] |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
156 |
| "r+" => [ Trans (x,y, Asm n)] |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
157 |
| "r*" => [ RTrans(x,y, Asm n)] |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
158 |
| _ => error ("rtrancl_tac: unknown relation symbol" )) |
32215 | 159 |
else [] |
15531 | 160 |
| NONE => []; |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
161 |
|
15098 | 162 |
(* ************************************************************************ *) |
163 |
(* *) |
|
164 |
(* mkconcl_trancl t: term -> (term, rel, proof) *) |
|
165 |
(* mkconcl_rtrancl t: term -> (term, rel, proof) *) |
|
166 |
(* *) |
|
167 |
(* Analyse conclusion t: *) |
|
168 |
(* - must be of form "(x, y) : r^+ (or r^* for rtrancl) *) |
|
169 |
(* - returns r *) |
|
170 |
(* - conclusion in internal form *) |
|
171 |
(* - proof object *) |
|
172 |
(* *) |
|
173 |
(* ************************************************************************ *) |
|
174 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
175 |
fun mkconcl_trancl t = |
26834
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
176 |
case decomp t of |
15531 | 177 |
SOME (x, y, rel, r) => (case r of |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
178 |
"r+" => (rel, Trans (x,y, Asm ~1), Asm 0) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
179 |
| _ => raise Cannot) |
15531 | 180 |
| NONE => raise Cannot; |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
181 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
182 |
fun mkconcl_rtrancl t = |
26834
87a5b9ec3863
Terms returned by decomp are now eta-contracted.
berghofe
parents:
22257
diff
changeset
|
183 |
case decomp t of |
15531 | 184 |
SOME (x, y, rel,r ) => (case r of |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
185 |
"r+" => (rel, Trans (x,y, Asm ~1), Asm 0) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
186 |
| "r*" => (rel, RTrans (x,y, Asm ~1), Asm 0) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
187 |
| _ => raise Cannot) |
15531 | 188 |
| NONE => raise Cannot; |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
189 |
|
15098 | 190 |
(* ************************************************************************ *) |
191 |
(* *) |
|
192 |
(* makeStep (r1, r2): rel * rel -> rel *) |
|
193 |
(* *) |
|
194 |
(* Apply transitivity to r1 and r2, obtaining a new element of r^+ or r^*, *) |
|
195 |
(* according the following rules: *) |
|
196 |
(* *) |
|
197 |
(* ( (a, b) : r^+ , (b,c) : r^+ ) --> (a,c) : r^+ *) |
|
198 |
(* ( (a, b) : r^* , (b,c) : r^+ ) --> (a,c) : r^+ *) |
|
199 |
(* ( (a, b) : r^+ , (b,c) : r^* ) --> (a,c) : r^+ *) |
|
200 |
(* ( (a, b) : r^* , (b,c) : r^* ) --> (a,c) : r^* *) |
|
201 |
(* *) |
|
202 |
(* ************************************************************************ *) |
|
203 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
204 |
fun makeStep (Trans (a,_,p), Trans(_,c,q)) = Trans (a,c, Thm ([p,q], Cls.trancl_trans)) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
205 |
(* refl. + trans. cls. rules *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
206 |
| makeStep (RTrans (a,_,p), Trans(_,c,q)) = Trans (a,c, Thm ([p,q], Cls.rtrancl_trancl_trancl)) |
32215 | 207 |
| makeStep (Trans (a,_,p), RTrans(_,c,q)) = Trans (a,c, Thm ([p,q], Cls.trancl_rtrancl_trancl)) |
15098 | 208 |
| makeStep (RTrans (a,_,p), RTrans(_,c,q)) = RTrans (a,c, Thm ([p,q], Cls.rtrancl_trans)); |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
209 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
210 |
(* ******************************************************************* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
211 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
212 |
(* transPath (Clslist, Cls): (rel list * rel) -> rel *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
213 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
214 |
(* If a path represented by a list of elements of type rel is found, *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
215 |
(* this needs to be contracted to a single element of type rel. *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
216 |
(* Prior to each transitivity step it is checked whether the step is *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
217 |
(* valid. *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
218 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
219 |
(* ******************************************************************* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
220 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
221 |
fun transPath ([],acc) = acc |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
222 |
| transPath (x::xs,acc) = transPath (xs, makeStep(acc,x)) |
32215 | 223 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
224 |
(* ********************************************************************* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
225 |
(* Graph functions *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
226 |
(* ********************************************************************* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
227 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
228 |
(* *********************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
229 |
(* Functions for constructing graphs *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
230 |
(* *********************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
231 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
232 |
fun addEdge (v,d,[]) = [(v,d)] |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
233 |
| addEdge (v,d,((u,dl)::el)) = if v aconv u then ((v,d@dl)::el) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
234 |
else (u,dl):: (addEdge(v,d,el)); |
32215 | 235 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
236 |
(* ********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
237 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
238 |
(* mkGraph constructs from a list of objects of type rel a graph g *) |
15098 | 239 |
(* and a list of all edges with label r+. *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
240 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
241 |
(* ********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
242 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
243 |
fun mkGraph [] = ([],[]) |
32215 | 244 |
| mkGraph ys = |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
245 |
let |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
246 |
fun buildGraph ([],g,zs) = (g,zs) |
32215 | 247 |
| buildGraph (x::xs, g, zs) = |
248 |
case x of (Trans (_,_,_)) => |
|
249 |
buildGraph (xs, addEdge((upper x), [],(addEdge ((lower x),[((upper x),x)],g))), x::zs) |
|
250 |
| _ => buildGraph (xs, addEdge((upper x), [],(addEdge ((lower x),[((upper x),x)],g))), zs) |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
251 |
in buildGraph (ys, [], []) end; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
252 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
253 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
254 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
255 |
(* adjacent g u : (''a * 'b list ) list -> ''a -> 'b list *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
256 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
257 |
(* List of successors of u in graph g *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
258 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
259 |
(* *********************************************************************** *) |
32215 | 260 |
|
261 |
fun adjacent eq_comp ((v,adj)::el) u = |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
262 |
if eq_comp (u, v) then adj else adjacent eq_comp el u |
32215 | 263 |
| adjacent _ [] _ = [] |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
264 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
265 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
266 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
267 |
(* dfs eq_comp g u v: *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
268 |
(* ('a * 'a -> bool) -> ('a *( 'a * rel) list) list -> *) |
32215 | 269 |
(* 'a -> 'a -> (bool * ('a * rel) list) *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
270 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
271 |
(* Depth first search of v from u. *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
272 |
(* Returns (true, path(u, v)) if successful, otherwise (false, []). *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
273 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
274 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
275 |
|
32215 | 276 |
fun dfs eq_comp g u v = |
277 |
let |
|
32740 | 278 |
val pred = Unsynchronized.ref []; |
279 |
val visited = Unsynchronized.ref []; |
|
32215 | 280 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
281 |
fun been_visited v = exists (fn w => eq_comp (w, v)) (!visited) |
32215 | 282 |
|
283 |
fun dfs_visit u' = |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
284 |
let val _ = visited := u' :: (!visited) |
32215 | 285 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
286 |
fun update (x,l) = let val _ = pred := (x,l) ::(!pred) in () end; |
32215 | 287 |
|
288 |
in if been_visited v then () |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
289 |
else (app (fn (v',l) => if been_visited v' then () else ( |
32215 | 290 |
update (v',l); |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
291 |
dfs_visit v'; ()) )) (adjacent eq_comp g u') |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
292 |
end |
32215 | 293 |
in |
294 |
dfs_visit u; |
|
295 |
if (been_visited v) then (true, (!pred)) else (false , []) |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
296 |
end; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
297 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
298 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
299 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
300 |
(* transpose g: *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
301 |
(* (''a * ''a list) list -> (''a * ''a list) list *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
302 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
303 |
(* Computes transposed graph g' from g *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
304 |
(* by reversing all edges u -> v to v -> u *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
305 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
306 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
307 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
308 |
fun transpose eq_comp g = |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
309 |
let |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
310 |
(* Compute list of reversed edges for each adjacency list *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
311 |
fun flip (u,(v,l)::el) = (v,(u,l)) :: flip (u,el) |
32768 | 312 |
| flip (_,[]) = [] |
32215 | 313 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
314 |
(* Compute adjacency list for node u from the list of edges |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
315 |
and return a likewise reduced list of edges. The list of edges |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
316 |
is searches for edges starting from u, and these edges are removed. *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
317 |
fun gather (u,(v,w)::el) = |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
318 |
let |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
319 |
val (adj,edges) = gather (u,el) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
320 |
in |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
321 |
if eq_comp (u, v) then (w::adj,edges) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
322 |
else (adj,(v,w)::edges) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
323 |
end |
32768 | 324 |
| gather (_,[]) = ([],[]) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
325 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
326 |
(* For every node in the input graph, call gather to find all reachable |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
327 |
nodes in the list of edges *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
328 |
fun assemble ((u,_)::el) edges = |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
329 |
let val (adj,edges) = gather (u,edges) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
330 |
in (u,adj) :: assemble el edges |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
331 |
end |
32768 | 332 |
| assemble [] _ = [] |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
333 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
334 |
(* Compute, for each adjacency list, the list with reversed edges, |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
335 |
and concatenate these lists. *) |
32768 | 336 |
val flipped = maps flip g |
32215 | 337 |
|
338 |
in assemble g flipped end |
|
339 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
340 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
341 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
342 |
(* dfs_reachable eq_comp g u: *) |
32215 | 343 |
(* (int * int list) list -> int -> int list *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
344 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
345 |
(* Computes list of all nodes reachable from u in g. *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
346 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
347 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
348 |
|
32215 | 349 |
fun dfs_reachable eq_comp g u = |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
350 |
let |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
351 |
(* List of vertices which have been visited. *) |
32768 | 352 |
val visited = Unsynchronized.ref []; |
32215 | 353 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
354 |
fun been_visited v = exists (fn w => eq_comp (w, v)) (!visited) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
355 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
356 |
fun dfs_visit g u = |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
357 |
let |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
358 |
val _ = visited := u :: !visited |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
359 |
val descendents = |
30190 | 360 |
List.foldr (fn ((v,l),ds) => if been_visited v then ds |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
361 |
else v :: dfs_visit g v @ ds) |
32768 | 362 |
[] (adjacent eq_comp g u) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
363 |
in descendents end |
32215 | 364 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
365 |
in u :: dfs_visit g u end; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
366 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
367 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
368 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
369 |
(* dfs_term_reachable g u: *) |
32215 | 370 |
(* (term * term list) list -> term -> term list *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
371 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
372 |
(* Computes list of all nodes reachable from u in g. *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
373 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
374 |
(* *********************************************************************** *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
375 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
376 |
fun dfs_term_reachable g u = dfs_reachable (op aconv) g u; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
377 |
|
32215 | 378 |
(* ************************************************************************ *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
379 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
380 |
(* findPath x y g: Term.term -> Term.term -> *) |
32215 | 381 |
(* (Term.term * (Term.term * rel list) list) -> *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
382 |
(* (bool, rel list) *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
383 |
(* *) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
384 |
(* Searches a path from vertex x to vertex y in Graph g, returns true and *) |
15098 | 385 |
(* the list of edges if path is found, otherwise false and nil. *) |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
386 |
(* *) |
32215 | 387 |
(* ************************************************************************ *) |
388 |
||
389 |
fun findPath x y g = |
|
390 |
let |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
391 |
val (found, tmp) = dfs (op aconv) g x y ; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
392 |
val pred = map snd tmp; |
32215 | 393 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
394 |
fun path x y = |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
395 |
let |
32215 | 396 |
(* find predecessor u of node v and the edge u -> v *) |
397 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
398 |
fun lookup v [] = raise Cannot |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
399 |
| lookup v (e::es) = if (upper e) aconv v then e else lookup v es; |
32215 | 400 |
|
401 |
(* traverse path backwards and return list of visited edges *) |
|
402 |
fun rev_path v = |
|
403 |
let val l = lookup v pred |
|
404 |
val u = lower l; |
|
405 |
in |
|
406 |
if u aconv x then [l] else (rev_path u) @ [l] |
|
407 |
end |
|
408 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
409 |
in rev_path y end; |
32215 | 410 |
|
411 |
in |
|
412 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
413 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
414 |
if found then ( (found, (path x y) )) else (found,[]) |
32215 | 415 |
|
416 |
||
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
417 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
418 |
end; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
419 |
|
15098 | 420 |
(* ************************************************************************ *) |
421 |
(* *) |
|
422 |
(* findRtranclProof g tranclEdges subgoal: *) |
|
423 |
(* (Term.term * (Term.term * rel list) list) -> rel -> proof list *) |
|
424 |
(* *) |
|
425 |
(* Searches in graph g a proof for subgoal. *) |
|
426 |
(* *) |
|
427 |
(* ************************************************************************ *) |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
428 |
|
32215 | 429 |
fun findRtranclProof g tranclEdges subgoal = |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
430 |
case subgoal of (RTrans (x,y,_)) => if x aconv y then [Thm ([], Cls.rtrancl_refl)] else ( |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
431 |
let val (found, path) = findPath (lower subgoal) (upper subgoal) g |
32215 | 432 |
in |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
433 |
if found then ( |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
434 |
let val path' = (transPath (tl path, hd path)) |
32215 | 435 |
in |
436 |
||
437 |
case path' of (Trans (_,_,p)) => [Thm ([p], Cls.trancl_into_rtrancl )] |
|
438 |
| _ => [getprf path'] |
|
439 |
||
440 |
end |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
441 |
) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
442 |
else raise Cannot |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
443 |
end |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
444 |
) |
32215 | 445 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
446 |
| (Trans (x,y,_)) => ( |
32215 | 447 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
448 |
let |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
449 |
val Vx = dfs_term_reachable g x; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
450 |
val g' = transpose (op aconv) g; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
451 |
val Vy = dfs_term_reachable g' y; |
32215 | 452 |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
453 |
fun processTranclEdges [] = raise Cannot |
32215 | 454 |
| processTranclEdges (e::es) = |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
455 |
if (upper e) mem Vx andalso (lower e) mem Vx |
32215 | 456 |
andalso (upper e) mem Vy andalso (lower e) mem Vy |
457 |
then ( |
|
458 |
||
459 |
||
460 |
if (lower e) aconv x then ( |
|
461 |
if (upper e) aconv y then ( |
|
462 |
[(getprf e)] |
|
463 |
) |
|
464 |
else ( |
|
465 |
let |
|
466 |
val (found,path) = findPath (upper e) y g |
|
467 |
in |
|
468 |
||
469 |
if found then ( |
|
470 |
[getprf (transPath (path, e))] |
|
471 |
) else processTranclEdges es |
|
472 |
||
473 |
end |
|
474 |
) |
|
475 |
) |
|
476 |
else if (upper e) aconv y then ( |
|
477 |
let val (xufound,xupath) = findPath x (lower e) g |
|
478 |
in |
|
479 |
||
480 |
if xufound then ( |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
481 |
|
32215 | 482 |
let val xuRTranclEdge = transPath (tl xupath, hd xupath) |
483 |
val xyTranclEdge = makeStep(xuRTranclEdge,e) |
|
484 |
||
485 |
in [getprf xyTranclEdge] end |
|
486 |
||
487 |
) else processTranclEdges es |
|
488 |
||
489 |
end |
|
490 |
) |
|
491 |
else ( |
|
492 |
||
493 |
let val (xufound,xupath) = findPath x (lower e) g |
|
494 |
val (vyfound,vypath) = findPath (upper e) y g |
|
495 |
in |
|
496 |
if xufound then ( |
|
497 |
if vyfound then ( |
|
498 |
let val xuRTranclEdge = transPath (tl xupath, hd xupath) |
|
499 |
val vyRTranclEdge = transPath (tl vypath, hd vypath) |
|
500 |
val xyTranclEdge = makeStep (makeStep(xuRTranclEdge,e),vyRTranclEdge) |
|
501 |
||
502 |
in [getprf xyTranclEdge] end |
|
503 |
||
504 |
) else processTranclEdges es |
|
505 |
) |
|
506 |
else processTranclEdges es |
|
507 |
end |
|
508 |
) |
|
509 |
) |
|
510 |
else processTranclEdges es; |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
511 |
in processTranclEdges tranclEdges end ) |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
512 |
| _ => raise Cannot |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
513 |
|
32215 | 514 |
|
515 |
fun solveTrancl (asms, concl) = |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
516 |
let val (g,_) = mkGraph asms |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
517 |
in |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
518 |
let val (_, subgoal, _) = mkconcl_trancl concl |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
519 |
val (found, path) = findPath (lower subgoal) (upper subgoal) g |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
520 |
in |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
521 |
if found then [getprf (transPath (tl path, hd path))] |
32215 | 522 |
else raise Cannot |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
523 |
end |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
524 |
end; |
32215 | 525 |
|
526 |
fun solveRtrancl (asms, concl) = |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
527 |
let val (g,tranclEdges) = mkGraph asms |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
528 |
val (_, subgoal, _) = mkconcl_rtrancl concl |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
529 |
in |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
530 |
findRtranclProof g tranclEdges subgoal |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
531 |
end; |
32215 | 532 |
|
533 |
||
32277
ff1e59a15146
trans_tac: use theory from goal state, not the static context, which seems to be outdated under certain circumstances (why?);
wenzelm
parents:
32215
diff
changeset
|
534 |
fun trancl_tac ctxt = SUBGOAL (fn (A, n) => fn st => |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
535 |
let |
32285
ab9b66c2bbca
trancl_tac etc.: back to static context -- problem was caused by bad solver in AFP/JiveDataStoreModel;
wenzelm
parents:
32283
diff
changeset
|
536 |
val thy = ProofContext.theory_of ctxt; |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
537 |
val Hs = Logic.strip_assums_hyp A; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
538 |
val C = Logic.strip_assums_concl A; |
32215 | 539 |
val (rel, subgoals, prf) = mkconcl_trancl C; |
540 |
||
33063 | 541 |
val prems = flat (map_index (mkasm_trancl rel o swap) Hs); |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
542 |
val prfs = solveTrancl (prems, C); |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
543 |
in |
35281
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
544 |
Subgoal.FOCUS (fn {prems, concl, ...} => |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
545 |
let |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
546 |
val SOME (_, _, rel', _) = decomp (term_of concl); |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
547 |
val thms = map (prove thy rel' prems) prfs |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
548 |
in rtac (prove thy rel' thms prf) 1 end) ctxt n st |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
549 |
end |
32277
ff1e59a15146
trans_tac: use theory from goal state, not the static context, which seems to be outdated under certain circumstances (why?);
wenzelm
parents:
32215
diff
changeset
|
550 |
handle Cannot => Seq.empty); |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
551 |
|
32215 | 552 |
|
553 |
fun rtrancl_tac ctxt = SUBGOAL (fn (A, n) => fn st => |
|
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
554 |
let |
32285
ab9b66c2bbca
trancl_tac etc.: back to static context -- problem was caused by bad solver in AFP/JiveDataStoreModel;
wenzelm
parents:
32283
diff
changeset
|
555 |
val thy = ProofContext.theory_of ctxt; |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
556 |
val Hs = Logic.strip_assums_hyp A; |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
557 |
val C = Logic.strip_assums_concl A; |
32215 | 558 |
val (rel, subgoals, prf) = mkconcl_rtrancl C; |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
559 |
|
33063 | 560 |
val prems = flat (map_index (mkasm_rtrancl rel o swap) Hs); |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
561 |
val prfs = solveRtrancl (prems, C); |
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
562 |
in |
35281
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
563 |
Subgoal.FOCUS (fn {prems, concl, ...} => |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
564 |
let |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
565 |
val SOME (_, _, rel', _) = decomp (term_of concl); |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
566 |
val thms = map (prove thy rel' prems) prfs |
206e2f1759cc
Fixed bug that caused (r)trancl_tac to crash when the term denoting the relation
berghofe
parents:
33063
diff
changeset
|
567 |
in rtac (prove thy rel' thms prf) 1 end) ctxt n st |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
568 |
end |
32215 | 569 |
handle Cannot => Seq.empty | Subscript => Seq.empty); |
15076
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
570 |
|
4b3d280ef06a
New prover for transitive and reflexive-transitive closure of relations.
ballarin
parents:
diff
changeset
|
571 |
end; |