Commit 584ccdfb authored by Martin Spoto's avatar Martin Spoto
Browse files

List unification

parent f860d002
package ch.heiafr.prolograal.nodes;
import java.util.List;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
......@@ -13,8 +11,6 @@ import com.oracle.truffle.api.nodes.RootNode;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.runtime.ProloGraalContext;
import ch.heiafr.prolograal.runtime.ProloGraalRuntime;
import ch.heiafr.prolograal.runtime.ProloGraalTerm;
import ch.heiafr.prolograal.runtime.ProloGraalVariable;
public final class ProloGraalEvalRootNode extends RootNode {
......
......@@ -5,16 +5,12 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.source.Source;
import org.graalvm.collections.Pair;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.exceptions.ProloGraalExistenceError;
import ch.heiafr.prolograal.parser.ProloGraalParseError;
......@@ -23,8 +19,6 @@ import ch.heiafr.prolograal.runtime.ProloGraalBoolean;
import ch.heiafr.prolograal.runtime.ProloGraalContext;
import ch.heiafr.prolograal.runtime.ProloGraalRuntime;
import ch.heiafr.prolograal.runtime.ProloGraalSuccess;
import ch.heiafr.prolograal.runtime.ProloGraalTerm;
import ch.heiafr.prolograal.runtime.ProloGraalVariable;
public class ProloGraalInterpreterNode extends RootNode {
......
......@@ -6,8 +6,6 @@ import java.util.List;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
import org.graalvm.collections.Pair;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.exceptions.ProloGraalExistenceError;
import ch.heiafr.prolograal.runtime.*;
......@@ -44,28 +42,24 @@ public final class ProloGraalResolverNode extends RootNode {
// System.out.println("Current goal : " + goal);
// System.out.println("Current candidates : " + terms);
Pair<ProloGraalTerm<?>, List<ProloGraalVariable>> goalPair = goalRuntime.getFirstPair();
ProloGraalTerm<?> goal = goalPair.getLeft();
List<ProloGraalVariable> goalVariables = goalPair.getRight();
ProloGraalClause goal = goalRuntime.getFirstClause();
List<ProloGraalSuccess> successes = new ArrayList<>();
List<Pair<ProloGraalTerm<?>, List<ProloGraalVariable>>> possibleClauses = runtime.getClauses().get(goal);
List<ProloGraalClause> possibleClauses = runtime.getClauses().get(goal.getHead());
if(possibleClauses == null)
throw new ProloGraalExistenceError();
for(int i = 0; i < possibleClauses.size(); i++) {
Pair<ProloGraalTerm<?>, List<ProloGraalVariable>> clausePair = possibleClauses.get(i);
ProloGraalTerm<?> clause = clausePair.getLeft();
List<ProloGraalVariable> variables = clausePair.getRight();
ProloGraalClause clause = possibleClauses.get(i);
if(clause.isUnifiable(goal)) {
if(ProloGraalLanguage.DEBUG_MODE)
System.out.println("Success with clause : " + clause.toString());
successes.add(new ProloGraalSuccess(clause, goalVariables));
successes.add(new ProloGraalSuccess(goal));
}
variables.forEach(ProloGraalVariable::unbind);
goalVariables.forEach(ProloGraalVariable::unbind);
clause.getVariables().values().forEach(ProloGraalVariable::unbind);
goal.getVariables().values().forEach(ProloGraalVariable::unbind);
}
if(successes.size() > 0) {
......
......@@ -3,6 +3,7 @@ package ch.heiafr.prolograal.parser;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
......@@ -15,6 +16,18 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
private Deque<ProloGraalTerm<?>> elements = new ArrayDeque<>();
private Deque<ProloGraalClause> clauses = new ArrayDeque<>();
public List<ProloGraalClause> getClauses() {
List<ProloGraalClause> r = new ArrayList<>();
Iterator<ProloGraalClause> it = clauses.descendingIterator();
it.forEachRemaining(r::add);
System.out.println(r);
return r;
}
public void debug() {
System.out.println(clauses.stream().map(x -> x.toString() + "\n").collect(Collectors.joining()));
}
......
......@@ -57,6 +57,6 @@ public class ProloGraalParserImpl {
if(ProloGraalLanguage.DEBUG_MODE)
listener.debug();
return new ProloGraalRuntime(null, null);
return new ProloGraalRuntime(listener.getClauses());
}
}
\ No newline at end of file
......@@ -17,6 +17,14 @@ public class ProloGraalClause {
variables = new HashMap<>();
}
public boolean isFact() {
return goals.size() == 0;
}
public ProloGraalTerm<?> getHead() {
return head;
}
public void setHead(ProloGraalTerm<?> head) {
this.head = head;
}
......@@ -34,4 +42,8 @@ public class ProloGraalClause {
return head.toString() + ":-" + goals.stream().map(Object::toString).collect(Collectors.joining(",")) + ".";
}
public boolean isUnifiable(ProloGraalClause goal) {
return this.head.isUnifiable(goal.head);
}
}
\ No newline at end of file
......@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.builtins.ProloGraalBuiltinAtoms;
public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
......@@ -85,12 +84,13 @@ public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
} else {
struct.addSubterm(this.tail);
}
/*
if(ProloGraalLanguage.DEBUG_MODE) {
System.out.println(this.toString());
System.out.println(internal);
System.out.println(fromInternal(internal));
System.out.println("###");
}
}*/
}
@Override
......@@ -104,20 +104,31 @@ public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return false;
if(obj == null) return false;
if(!(obj instanceof ProloGraalList)) return false;
return this.internal.equals(((ProloGraalList)obj).internal);
}
@Override
public boolean isUnifiable(ProloGraalTerm<?> other) {
// TODO Auto-generated method stub
return false;
if(other instanceof ProloGraalList) {
return this.internal.isUnifiable(((ProloGraalList)other).internal);
} else if(other instanceof ProloGraalVariable) {
return ((ProloGraalVariable)other).isUnifiable(this);
}
return this.internal.isUnifiable(other);
}
@Override
public ProloGraalList copy() {
// TODO Auto-generated method stub
return null;
ProloGraalList list = new ProloGraalList();
list.tail = this.tail.copy();
for(ProloGraalTerm<?> item : this.items) {
list.addItem(item.copy());
}
list.buildInteralRepresentation();
return list;
}
}
\ No newline at end of file
......@@ -5,30 +5,25 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.graalvm.collections.Pair;
public final class ProloGraalRuntime {
private final Map<ProloGraalTerm<?>, List<Pair<ProloGraalTerm<?>, List<ProloGraalVariable>>>> clauses;
private final Map<ProloGraalTerm<?>, List<ProloGraalClause>> clauses;
public ProloGraalRuntime(List<ProloGraalTerm<?>> terms, List<List<ProloGraalVariable>> variables) {
public ProloGraalRuntime(List<ProloGraalClause> clauseList) {
clauses = new HashMap<>();
for(int i = 0; i < terms.size(); i++) {
clauses.putIfAbsent(terms.get(i), new ArrayList<>());
List<Pair<ProloGraalTerm<?>, List<ProloGraalVariable>>> terms1 = clauses.get(terms.get(i));
terms1.add(Pair.create(terms.get(i), variables.get(i)));
for (int i = 0; i < clauseList.size(); i++) {
clauses.putIfAbsent(clauseList.get(i).getHead(), new ArrayList<>());
List<ProloGraalClause> clauses1 = clauses.get(clauseList.get(i).getHead());
clauses1.add(clauseList.get(i));
}
}
public final Map<ProloGraalTerm<?>, List<Pair<ProloGraalTerm<?>, List<ProloGraalVariable>>>> getClauses() {
public final Map<ProloGraalTerm<?>, List<ProloGraalClause>> getClauses() {
return clauses;
}
public final Pair<ProloGraalTerm<?>, List<ProloGraalVariable>> getFirstPair() {
public final ProloGraalClause getFirstClause() {
return clauses.values().stream().findFirst().orElseThrow(() -> new RuntimeException()).get(0);
}
public final ProloGraalTerm<?> getFirstClause() {
return getFirstPair().getLeft();
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ import java.util.List;
public class ProloGraalSuccess extends ProloGraalBoolean {
private ProloGraalTerm<?> clause;
private ProloGraalClause clause;
private List<ProloGraalVariable> variables;
private List<ProloGraalSuccess> successes;
......@@ -14,10 +14,10 @@ public class ProloGraalSuccess extends ProloGraalBoolean {
}
public ProloGraalSuccess(ProloGraalTerm<?> clause, List<ProloGraalVariable> variables) {
this.clause = clause.copy();
this.variables = new ArrayList<>(variables.size());
for(ProloGraalVariable variable : variables) {
public ProloGraalSuccess(ProloGraalClause clause) {
this.clause = clause;
this.variables = new ArrayList<>(clause.getVariables().size());
for(ProloGraalVariable variable : clause.getVariables().values()) {
this.variables.add(variable.copy());
}
}
......@@ -34,7 +34,7 @@ public class ProloGraalSuccess extends ProloGraalBoolean {
return this.variables;
}
public ProloGraalTerm<?> getClause() {
public ProloGraalClause getClause() {
return this.clause;
}
......
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