Commit 12d0f054 authored by Martin Spoto's avatar Martin Spoto
Browse files

Working for listConcat and test, but not for DlistConcat

parent 8e4922ee
......@@ -33,7 +33,7 @@ public class ProloGraalLanguage extends TruffleLanguage<ProloGraalContext> {
public static final String ID = "pl";
public static final String MIME_TYPE = "application/x-prolog";
public static final boolean DEBUG_MODE = true;
public static final boolean DEBUG_MODE = false;
static {
if(DEBUG_MODE) {
......
......@@ -7,6 +7,7 @@ import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
......@@ -16,10 +17,6 @@ import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.exceptions.ProloGraalExistenceError;
import ch.heiafr.prolograal.parser.ProloGraalParseError;
import ch.heiafr.prolograal.parser.ProloGraalParserImpl;
import ch.heiafr.prolograal.runtime.ProloGraalBoolean;
import ch.heiafr.prolograal.runtime.ProloGraalContext;
import ch.heiafr.prolograal.runtime.ProloGraalRuntime;
import ch.heiafr.prolograal.runtime.ProloGraalSuccess;
public class ProloGraalInterpreterNode extends RootNode {
......@@ -42,6 +39,7 @@ public class ProloGraalInterpreterNode extends RootNode {
writer.print("?- ");
writer.flush();
String line = reader.readLine();
writer.println();
if (line == null) {
break;
......@@ -67,16 +65,9 @@ public class ProloGraalInterpreterNode extends RootNode {
ProloGraalBoolean callResult = (ProloGraalBoolean) Truffle.getRuntime().createCallTarget(context.getResolverNode()).call(runtime);
if(callResult.asBoolean()) {
ProloGraalSuccess success = (ProloGraalSuccess)callResult;
if(success.getClause() != null) {
writer.println(success.getClause());
writer.println(runtime.getFirstClause().getVariables().values().stream().map(Object::toString).collect(Collectors.joining("\n")));
} else if(success.getSuccesses() != null && success.getSuccesses().size() > 0) {
for(ProloGraalSuccess succ : success.getSuccesses()) {
succ.getVariables().values().forEach(x -> {
if(x.isBound())
writer.println(x);
});
}
for(ProloGraalVariable variable : success.getVariables().values()) {
if(variable.isBound())
writer.println(variable.getName() + " = " + variable.getRootValue());
}
}
writer.println(callResult);
......
......@@ -32,29 +32,16 @@ public final class ProloGraalResolverNode extends RootNode {
ProloGraalBoolean r = resolve(head);
makePretty(head);
if(head instanceof ProloGraalStructure) {
System.out.println(((ProloGraalStructure) head).toRootString());
} else {
System.out.println(head.toString());
if(r.asBoolean()) {
r = new ProloGraalSuccess(head.getVariables());
}
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);
}
private ProloGraalBoolean resolve(ProloGraalTerm<?> goal) {
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Resolving goal " + goal);
}
}
public ProloGraalBoolean resolve(ProloGraalTerm<?> goal) {
System.out.println("Resolving goal " + goal);
// get all possible predicates
List<ProloGraalClause> possibleClauses = clauses.get(goal);
......@@ -65,39 +52,53 @@ public final class ProloGraalResolverNode extends RootNode {
// for each possible clause
outer: for(ProloGraalClause clauseIt : possibleClauses) {
ProloGraalClause clause = clauseIt.copy(); // creates a new context for this clause
System.out.println("Trying " + clause + " to resolve " + goal);
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Trying " + clause + " to resolve " + goal);
}
// save current states
goal.save();
// if the head of the clause is unifiable with the current goal
if(clause.getHead().isUnifiable(goal)) {
System.out.println("Unified " + goal + " with " + clause.getHead());
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Unified " + goal + " with " + clause.getHead());
}
if(clause.isFact()) {
// if the clause is a fact, we just need to return a success after restoring states
goal.isUnifiable(clause.getHead()); // unify again to report variables to parent
System.out.println("Success for goal : " + goal);
//goal.isUnifiable(clause.getHead()); // unify again to report variables to parent
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Success for goal : " + goal);
}
return new ProloGraalSuccess();
} else {
// if the clause is a complex clause, we need to iterate over each goals
for(int i = 0; i < clause.getGoalCount(); i++) {
ProloGraalTerm<?> subGoal = clause.getGoals().get(i);
System.out.println("Resolving subgoal : " + subGoal + " for goal " + goal);
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Resolving subgoal : " + subGoal + " for goal " + goal);
}
if(!resolve(subGoal).asBoolean()) {
// if one of the goals cannot be unified, then this branch is a failure
System.out.println("Failure for goal : " + goal + " with subgoal : " + subGoal);
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Failure for goal : " + goal + " with subgoal : " + subGoal);
}
goal.undo();
continue outer;
}
}
// if we got to this point, every goal has been successfully unified, we can return a success
System.out.println("Success for goal : " + goal);
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Success for goal : " + goal);
}
// unify again to report variables to parent
goal.isUnifiable(clause.getHead());
//goal.isUnifiable(clause.getHead());
return new ProloGraalSuccess();
}
} else {
System.out.println("Could not unify " + clause + " for goal " + goal);
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Could not unify " + clause + " for goal " + goal);
}
// undo all changes that the unification may have done
goal.undo();
}
......@@ -105,7 +106,9 @@ public final class ProloGraalResolverNode extends RootNode {
// we tried every clauses but none was successful
// no need to restore any state
System.out.println("Failure for goal : " + goal);
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Failure for goal : " + goal);
}
return new ProloGraalFailure();
}
......
......@@ -7,6 +7,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import ch.heiafr.prolograal.ProloGraalLanguage;
import org.antlr.v4.runtime.ParserRuleContext;
import ch.heiafr.prolograal.runtime.*;
......@@ -23,7 +24,9 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
it.forEachRemaining(r::add);
System.out.println(r);
if(ProloGraalLanguage.DEBUG_MODE) {
System.out.println(r);
}
return r;
}
......
......@@ -106,7 +106,7 @@ public class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
public boolean isUnifiable(ProloGraalTerm<?> other) {
if (this.equals(other)) {
for (int i = 0; i < this.arity; i++) {
if (!this.subterms.get(i).isUnifiable(((ProloGraalStructure) other).subterms.get(i))) {
if (!this.subterms.get(i).getRootValue().isUnifiable(((ProloGraalStructure) other).subterms.get(i))) {
return false;
}
}
......
package ch.heiafr.prolograal.runtime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ProloGraalSuccess extends ProloGraalBoolean {
private ProloGraalClause clause;
private Map<ProloGraalVariable, ProloGraalVariable> variables;
private List<ProloGraalSuccess> successes;
public ProloGraalSuccess() {
}
public ProloGraalSuccess(ProloGraalClause clause) {
this.clause = clause;
this.variables = new HashMap<>(clause.getVariables().size());
for(ProloGraalVariable variable : clause.getVariables().values()) {
ProloGraalVariable var2 = variable.copy(variables);
this.variables.put(var2, var2);
}
public ProloGraalSuccess(Map<ProloGraalVariable, ProloGraalVariable> variables) {
this.variables = variables;
}
public ProloGraalSuccess(List<ProloGraalSuccess> successes) {
this.successes = successes;
}
public List<ProloGraalSuccess> getSuccesses() {
return this.successes;
}
public Map<ProloGraalVariable, ProloGraalVariable> getVariables() {
return this.variables;
}
public ProloGraalClause getClause() {
return this.clause;
}
@Override
public String toString() {
return "yes";
......
......@@ -21,7 +21,7 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
name = "_" + anonymousVariableSuffix++;
}
this.name = name;
this.hashCode = name.hashCode();
this.hashCode = name.hashCode() * variables.hashCode();
variables.putIfAbsent(this, this);
}
......@@ -32,7 +32,7 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
if (other.isBound) {
this.boundValue = other.boundValue.copy(variables);
}
this.hashCode = name.hashCode();
this.hashCode = name.hashCode() * variables.hashCode();
variables.putIfAbsent(this, this);
}
......@@ -44,6 +44,10 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
this.hashCode = other.hashCode;
}
public String getName() {
return name;
}
@Override
public ProloGraalTerm<?> getRootValue() {
if(this.isBound) {
......@@ -57,7 +61,7 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
}
}
public void bind(ProloGraalTerm<?> other) {
private void bind(ProloGraalTerm<?> other) {
// recursion check
if (other instanceof ProloGraalVariable) {
ProloGraalVariable otherVar = (ProloGraalVariable) other;
......@@ -86,11 +90,7 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
@Override
public String toString() {
if(ProloGraalLanguage.DEBUG_MODE) {
return this.name + (this.isBound ? " = " + this.getRootValue().toString() : "");
} else {
return this.getRootValue().toString();
}
return this.name + (this.isBound ? " = " + this.getRootValue().toString() : "");
}
@Override
......
test(a, b).
test(A, B).
test(a, B).
test(X, X).
listConcat([a, b], [c,d], [a,b,c,d]).
listConcat([a, b], X, [a,b,c,d]).
listConcat(X, [c,d], [a,b,c,d]).
\ No newline at end of file
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