In the '80s, Alain Comerauer, Joxan Jaffar and others published--- Synchronet 3.20a-Linux NewsLink 1.114
algorithms for unification without occurs check that could work
with infinite (cyclic) terms with termination guaranteed.
Until yesterday, I always believed that the standard total order for
finite Prolog terms readily extended to include infinite terms as
well. However, the following example shows two terms that can't be
ordered.
Consider the terms A and B defined by the equations
[1] A = s(B,0).
[2] B = s(A,1).
Clearly, A and B are not identical, so is A @< B or A @> B?
Assume A @< B. But then s(A,1) @< s(B,0) i.e. B @< A. Contradiction.
Assume A @> B. But then s(A,1) @> s(B,0) i.e. B @> A. Contradiction.
So the standard order of Prolog terms cannot include (some) infinite
terms. On reflection, the same is true for the integers---infinity
minus infinity is not defined.
Perhaps this is all obvious, but I was a bit jarred at first.
Mats Carlsson, PhD
Computing Science Department tel: +46 18 187691
Uppsala University fax: +46 18 511925
P.O. Box 311 Email: m...@csd.uu.se
S-751 05 Uppsala, Sweden
This never gets old, especially since even seasoned Prologers--- Synchronet 3.20a-Linux NewsLink 1.114
might not be aware of it. But it made it even into Prolog system documentation, like here:
"Please note : the standard order is only well-defined for finite (acyclic) terms.
There are infinite (cyclic) terms for which no order relation holds." https://sicstus.sics.se/sicstus/docs/3.12.11/html/sicstus/Term-Compare.html
And here is a funny disagreement between two Prolog systems,
an old lady (SWI-Prolog) and a youngster (Scryer Prolog):
/* SWI-Prolog 9.1.7 */
?- X=f(X,a(X)), Y=f(Y,b(Y)), Z=f(Y,c(Y)),
compare(A,X,Y), compare(B,Y,Z), compare(C,X,Z).
A = B, B = (<),
C = (>).
/* Scryer Prolog 0.9.1-194 */
?- X=f(X,a(X)), Y=f(Y,b(Y)), Z=f(Y,c(Y)),
compare(A,X,Y), compare(B,Y,Z), compare(C,X,Z).
A = (<), B = (<), C = (<).
Mats Carlsson schrieb am Dienstag, 16. Juli 1996 um 09:00:00 UTC+2:
In the '80s, Alain Comerauer, Joxan Jaffar and others published
algorithms for unification without occurs check that could work
with infinite (cyclic) terms with termination guaranteed.
Until yesterday, I always believed that the standard total order for finite Prolog terms readily extended to include infinite terms as
well. However, the following example shows two terms that can't be ordered.
Consider the terms A and B defined by the equations
[1] A = s(B,0).
[2] B = s(A,1).
Clearly, A and B are not identical, so is A @< B or A @> B?
Assume A @< B. But then s(A,1) @< s(B,0) i.e. B @< A. Contradiction. Assume A @> B. But then s(A,1) @> s(B,0) i.e. B @> A. Contradiction.
So the standard order of Prolog terms cannot include (some) infinite terms. On reflection, the same is true for the integers---infinity
minus infinity is not defined.
Perhaps this is all obvious, but I was a bit jarred at first.
Mats Carlsson, PhD
Computing Science Department tel: +46 18 187691
Uppsala University fax: +46 18 511925
P.O. Box 311 Email: m...@csd.uu.se
S-751 05 Uppsala, Sweden
Here is a formal proof of Matt Carlsons counter example.--- Synchronet 3.20a-Linux NewsLink 1.114
Wiithout loss of generality its enough to handle the case
Lab, although Matt Carlson mentioned both proofs:
/* Lexical Ordering */
∀x∀y∀z∀t(Ls(x,y)s(z,t) ↔ (Lxz ∨ (x=z ∧ Lyt))),
/* Example A,B satisfies A @< B and ~(B @< A) */
Lab, ¬Lba,
/* Example A,B is A=s(B,c) and B=s(A,d), proof works for any c,d */ a=s(b,c), b=s(a,d)
/* We can derive a Contradiction */
entails p∧¬p. https://www.umsu.de/trees/#~6x~6y~6z~6t(Ls(x,y)s(z,t)~4Lxz~2(x=z~1Lyt)),Lab,~3Lba,a=s(b,c),b=s(a,d)
The consistency means unless hell freezes over,
we can never find a less than relationship L.
Corr.:--- Synchronet 3.20a-Linux NewsLink 1.114
The inconsistency means unless hell freezes over,
we can never find a less than relationship L.
Mostowski Collapse schrieb am Freitag, 31. März 2023 um 00:32:28 UTC+2:
Here is a formal proof of Matt Carlsons counter example.
Wiithout loss of generality its enough to handle the case
Lab, although Matt Carlson mentioned both proofs:
/* Lexical Ordering */
∀x∀y∀z∀t(Ls(x,y)s(z,t) ↔ (Lxz ∨ (x=z ∧ Lyt))),
/* Example A,B satisfies A @< B and ~(B @< A) */
Lab, ¬Lba,
/* Example A,B is A=s(B,c) and B=s(A,d), proof works for any c,d */ a=s(b,c), b=s(a,d)
/* We can derive a Contradiction */
entails p∧¬p. https://www.umsu.de/trees/#~6x~6y~6z~6t(Ls(x,y)s(z,t)~4Lxz~2(x=z~1Lyt)),Lab,~3Lba,a=s(b,c),b=s(a,d)
The consistency means unless hell freezes over,
we can never find a less than relationship L.
Here is a relatively cheap compare for cyclic terms that satisfies the--- Synchronet 3.20a-Linux NewsLink 1.114
laws, except lexical order. It is based on the idea to define:
S @<' T :<=> rep(S) @< rep(T)
The only requirement to the function rep is that it is injective,
then the laws, except for lexical order, are automatically satisfied.
The proof is not so difficult. Here is an example proof, showing
irreflexivity of @<’ based on irreflexivity of @<:
/* I am using r for the function rep,
R for the relation @<' and L for the relation @< */
/* Injectivity */
∀x∀y(r(x)=r(y) → x=y),
/* Bootstrapping */
∀x∀y(Rxy ↔ Lr(x)r(y)),
/* Irreflexivity */
∀x¬Lxx
/* Irreflexivity */
entails ∀x¬Rxx.
https://www.umsu.de/trees/#~6x~6y(r(x)=r(y)~5x=y),~6x~6y(Rxy~4Lr(x)r(y)),~6x(~3Lxx)%7C=~6x(~3Rxx)
Here is a relatively cheap compare for cyclic terms that satisfies the--- Synchronet 3.20a-Linux NewsLink 1.114
laws, except lexical order. It is based on the idea to define:
S @<' T :<=> rep(S) @< rep(T)
The only requirement to the function rep is that it is injective,
then the laws, except for lexical order, are automatically satisfied.
The proof is not so difficult. Here is an example proof, showing
irreflexivity of @<’ based on irreflexivity of @<:
/* I am using r for the function rep,
R for the relation @<' and L for the relation @< */
/* Injectivity */
∀x∀y(r(x)=r(y) → x=y),
/* Bootstrapping */
∀x∀y(Rxy ↔ Lr(x)r(y)),
/* Irreflexivity */
∀x¬Lxx
/* Irreflexivity */
entails ∀x¬Rxx.
https://www.umsu.de/trees/#~6x~6y(r(x)=r(y)~5x=y),~6x~6y(Rxy~4Lr(x)r(y)),~6x(~3Lxx)%7C=~6x(~3Rxx)
Here is a sort example, the Matt Carlsson pairs can be easily sorted:--- Synchronet 3.20a-Linux NewsLink 1.114
?- A=(B,0), B=(A,1), predsort(rep_compare, [A,B], X),
term_factorized(X, P, Q).
P = [_A, _B],
Q = [_B=(_A, 0), _A=(_B, 1)].
?- A=(B,0), B=((B,0),1), predsort(rep_compare, [B,A], X),
term_factorized(X, P, Q).
P = [_A, _B],
Q = [_B=(_A, 0), _A=(_B, 1)].
And the tripple X,Y,Z that has a bug in native compare/3, the bug is gone:
?- X=f(X,a(X)), Y=f(Y,b(Y)), Z=f(Y,c(Y)),
rep_compare(A,X,Y), rep_compare(B,Y,Z), rep_compare(C,X,Z).
A = B, B = C, C = (<)
Mostowski Collapse schrieb am Freitag, 31. März 2023 um 23:08:55 UTC+2:
Here is a relatively cheap compare for cyclic terms that satisfies the laws, except lexical order. It is based on the idea to define:
S @<' T :<=> rep(S) @< rep(T)
The only requirement to the function rep is that it is injective,
then the laws, except for lexical order, are automatically satisfied.
The proof is not so difficult. Here is an example proof, showing
irreflexivity of @<’ based on irreflexivity of @<:
/* I am using r for the function rep,
R for the relation @<' and L for the relation @< */
/* Injectivity */
∀x∀y(r(x)=r(y) → x=y),
/* Bootstrapping */
∀x∀y(Rxy ↔ Lr(x)r(y)),
/* Irreflexivity */
∀x¬Lxx
/* Irreflexivity */
entails ∀x¬Rxx.
https://www.umsu.de/trees/#~6x~6y(r(x)=r(y)~5x=y),~6x~6y(Rxy~4Lr(x)r(y)),~6x(~3Lxx)%7C=~6x(~3Rxx)
Instead of using the verification path, and making--- Synchronet 3.20a-Linux NewsLink 1.114
mathematical proofs, one can also use the validation path.
What helps is this fuzzer:
% random_cyclic(-Term)
random_cyclic(T) :-
random_cyclic([T], T).
% random_cyclic(+List, -Term)
random_cyclic(L, T) :-
length(L, M),
random(R),
N is truncate(R*(M+3)),
(N = 0 -> T = 0;
N = 1 -> T = 1;
N = 2 -> T = s(P,Q), random_cyclic([P|L], P), random_cyclic([Q|L], Q);
K is N-3, nth0(K, L, S), S=T).
Now I found that term_factorized/3 might run into
a stack overflow. The reason could be the buggy
compare/3, in case it is used in rbtrees:
?- aggregate_all(count, (between(1,1000000,_), random_cyclic(A), random_cyclic(B), random_cyclic(C), rep_less(A,B),
rep_less(B,C), \+ rep_less(A,C)), F).
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 0.7Gb, global: 0.2Gb, trail: 1Kb
ERROR: Stack depth: 12,882,088, last-call: 50%, Choice points: 6
ERROR: In:
ERROR: [12,882,088] rbtrees:lookup(<compound s/2>,
_64416492, <compound black/4>)
ERROR: [12,882,086] terms:insert_vars(<compound s/2>,
_64416526, <compound t/2>)
There is a new surprise. I get the same stack--- Synchronet 3.20a-Linux NewsLink 1.114
overflow when I use a ground term:
/* SWI-Prolog 9.1.7 */
?- T = _S1, % where
_S1 = s(s(_S1, _S2), 0),
_S2 = s(_S2, _S1), term_factorized(T, Y, L).
ERROR: Stack limit (1.0Gb) exceeded
The stack overflow glitch is not only present in non-ground
terms, it sadly happens for ground terms as well.
Mostowski Collapse schrieb am Samstag, 1. April 2023 um 20:23:06 UTC+2:
Instead of using the verification path, and making
mathematical proofs, one can also use the validation path.
What helps is this fuzzer:
% random_cyclic(-Term)
random_cyclic(T) :-
random_cyclic([T], T).
% random_cyclic(+List, -Term)
random_cyclic(L, T) :-
length(L, M),
random(R),
N is truncate(R*(M+3)),
(N = 0 -> T = 0;
N = 1 -> T = 1;
N = 2 -> T = s(P,Q), random_cyclic([P|L], P), random_cyclic([Q|L], Q);
K is N-3, nth0(K, L, S), S=T).
Now I found that term_factorized/3 might run into
a stack overflow. The reason could be the buggy
compare/3, in case it is used in rbtrees:
?- aggregate_all(count, (between(1,1000000,_), random_cyclic(A), random_cyclic(B), random_cyclic(C), rep_less(A,B),
rep_less(B,C), \+ rep_less(A,C)), F).
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 0.7Gb, global: 0.2Gb, trail: 1Kb
ERROR: Stack depth: 12,882,088, last-call: 50%, Choice points: 6
ERROR: In:
ERROR: [12,882,088] rbtrees:lookup(<compound s/2>,
_64416492, <compound black/4>)
ERROR: [12,882,086] terms:insert_vars(<compound s/2>,
_64416526, <compound t/2>)
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 915 |
Nodes: | 10 (2 / 8) |
Uptime: | 35:47:55 |
Calls: | 12,170 |
Calls today: | 2 |
Files: | 186,521 |
Messages: | 2,234,364 |