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