Commit 848b93b4 authored by Martin Spoto's avatar Martin Spoto
Browse files

Add more test cases for occurs check; Fix bug with occurs check

parent 9263c785
package ch.heiafr.prolograal.runtime;
import ch.heiafr.prolograal.ProloGraalLanguage;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import ch.heiafr.prolograal.ProloGraalLanguage;
public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable> {
private Deque<ProloGraalVariable> states = new ArrayDeque<>();
......@@ -93,6 +93,25 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
return this.name + (this.isBound ? " = " + this.getRootValue().toString() : "");
}
private boolean occursCheck(ProloGraalStructure root) {
Map<ProloGraalVariable, ProloGraalVariable> subVariables = root.getSubVariables();
if(subVariables.get(this) == this) { // contains directly this
return true;
}
for(ProloGraalVariable var : subVariables.values()) {
ProloGraalTerm<?> rootValue = var.getRootValue();
if(rootValue == this) // or contains indirectly
return true;
if(rootValue instanceof ProloGraalStructure) {
if(occursCheck((ProloGraalStructure) rootValue)) {
return true;
}
}
}
return false;
}
@Override
public boolean unify(ProloGraalTerm<?> other) {
if (other instanceof ProloGraalVariable && other == this) {// can always unify to itself
......@@ -101,14 +120,9 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
// occurs check
ProloGraalTerm<?> rootValue = other.getRootValue();
if(rootValue instanceof ProloGraalStructure) {
Map<ProloGraalVariable, ProloGraalVariable> subVariables = ((ProloGraalStructure) rootValue).getSubVariables();
if(subVariables.get(this) == this) { // contains directly this
if(occursCheck((ProloGraalStructure) rootValue)) {
return false;
}
for(ProloGraalVariable var : subVariables.values()) {
if(var.getRootValue() == this) // or contains indirectly
return false;
}
}
if (this.isBound) {
......
......@@ -14,4 +14,7 @@ multiple(X, b, a).
nested(X, one(Y, two(three(a)))).
f(B, h(A)).
indirection(A, B, C, x).
occurs(X, X).
\ No newline at end of file
occurs(X, X).
e(f(A,B,C,D,E), f(a(B),a(C),h,a(C),a(D))).
e(f(A,B,C,D,E), f(a(B),a(C),A,a(C),a(D))).
e(f(A,B,C,D,E), f(a(B),a(C),E,a(C),a(D))).
\ No newline at end of file
......@@ -43,5 +43,16 @@ C = x
yes
% occurs(X, X).
no
% e(f(A,B,C,D,E), f(a(B),a(C),h,a(C),a(D))).
A = a/1(a/1(h))
B = a/1(h)
C = h
D = a/1(h)
E = a/1(a/1(h))
yes
% e(f(A,B,C,D,E), f(a(B),a(C),A,a(C),a(D))).
no
% e(f(A,B,C,D,E), f(a(B),a(C),E,a(C),a(D))).
no
% EOF
yes
\ No newline at end of file
......@@ -2,6 +2,8 @@ fact(X).
f(A, B).
e(A, A).
multifacts(a).
multifacts(b).
multifacts(c).
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment