Commit c4d74998 authored by Martin Spoto's avatar Martin Spoto
Browse files

Working for list concat

parent 4a5f801d
......@@ -32,10 +32,24 @@ public final class ProloGraalResolverNode extends RootNode {
}
ProloGraalBoolean r = resolve(goalRuntime.getFirstClause().getHead());
makePretty(goalRuntime.getFirstClause().getHead());
System.out.println(goalRuntime.getFirstClause().getHead());
return r;
}
private void makePretty(ProloGraalTerm<?> term) {
if(term instanceof ProloGraalStructure) {
ProloGraalStructure struct = (ProloGraalStructure) term;
for (int i = 0; i < struct.getArguments().size(); i++) {
ProloGraalTerm<?> sub = struct.getArguments().get(i).getRootValue();
makePretty(sub);
struct.getArguments().set(i, sub);
}
}
}
public ProloGraalBoolean resolve(ProloGraalTerm<?> goal) {
System.out.println("Resolving goal " + goal);
......
......@@ -79,6 +79,8 @@ public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructur
@Override
public boolean isUnifiable(ProloGraalTerm<?> other) {
if(other instanceof ProloGraalList)
other = ((ProloGraalList) other).internal; // FIXME maybe make list inherit structure
if(this.equals(other)) {
for(int i = 0; i < this.arity; i++) {
if(!this.subterms.get(i).isUnifiable(((ProloGraalStructure)other).subterms.get(i))) {
......
......@@ -23,6 +23,10 @@ public abstract class ProloGraalTerm<T extends ProloGraalTerm<T>> implements Tru
return this.hashCode;
}
public ProloGraalTerm<?> getRootValue() {
return this;
}
public abstract boolean isUnifiable(ProloGraalTerm<?> other);
// save current state
......
......@@ -44,9 +44,14 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
this.hashCode = other.hashCode;
}
public ProloGraalTerm<?> getValue() {
if (isBound) {
return boundValue;
@Override
public ProloGraalTerm<?> getRootValue() {
if(this.isBound) {
if(this.boundValue instanceof ProloGraalVariable) {
return this.boundValue.getRootValue();
} else {
return this.boundValue;
}
} else {
return this;
}
......@@ -81,41 +86,25 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
@Override
public String toString() {
/*if (this.isBound) {
if (boundValue instanceof ProloGraalVariable) {
ProloGraalVariable otherVar = (ProloGraalVariable) boundValue;
if (otherVar.isBound) {
return this.name + " = " + otherVar.boundValue.toString();
}
}
}*/
return this.name + (this.isBound ? " = " + boundValue.toString() : "");
return this.name + (this.isBound ? " = " + this.getRootValue().toString() : "");
}
@Override
public boolean isUnifiable(ProloGraalTerm<?> other) {
if (other instanceof ProloGraalVariable && other == this) // can always unify to itself
{
if (other instanceof ProloGraalVariable && other == this) {// can always unify to itself
return true;
}
// occurs check
if (!(other instanceof ProloGraalVariable) &&
other.getVariables() != null &&
other.getVariables().containsKey(this) &&
other.getVariables().get(this) == this) {
return false;
}
if (this.isBound) {
if (other instanceof ProloGraalVariable) {
ProloGraalVariable otherVar = (ProloGraalVariable) other;
if (otherVar.isBound) {
return this.boundValue.isUnifiable(otherVar.boundValue); // FIXME maybe this will cause infinite
// loops
} else {
otherVar.bind(this);
return true;
}
} else {
/*if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Already bound :" + this.toString() + ", trying to unify to : " + other.toString());
}*/
return this.boundValue.isUnifiable(other);
}
return this.boundValue.isUnifiable(other.getRootValue());
} else {
this.bind(other);
this.bind(other.getRootValue());
return true;
}
}
......
e(A, A).
test(A, R) :-
test2(A, R),
test3(A, 4).
......
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