s-webber / projog

Prolog programming for the Java platform.
Apache License 2.0
37 stars 9 forks source link

Cyclic variable chains cause infinite loop #198

Closed s-webber closed 3 years ago

s-webber commented 3 years ago

When there is a cyclic chain of variable assignments (e.g. X assigned to Y, Y assigned to Z, Z assigned to X) then the Variable.getValue() never terminates.

Example of how to observe this bug using Prolog:

?- p(A,B,C,D,E,F)=p(E,A,D,F,C,B), numbervars(A).

JUnit tests that demonstrates this bug:

@Test
public void test() {
  Variable v = new Variable("V");
  Variable w = new Variable("W");
  Variable x = new Variable("X");
  Variable y = new Variable("Y");
  Variable z = new Variable("Z");

  v.unify(w);
  w.unify(x);
  x.unify(y);
  y.unify(z);
  z.unify(x);

  // v.getTerm() will never terminate
  assertSame(z, v.getTerm());
}