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

Very buggy but works for test(a,b). -> TODO keep track of variables modified

parent 584ccdfb
......@@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.frame.VirtualFrame;
......@@ -59,7 +60,10 @@ public class ProloGraalInterpreterNode extends RootNode {
ProloGraalBoolean callResult = (ProloGraalBoolean) Truffle.getRuntime().createCallTarget(context.getResolverNode()).call(runtime);
if(callResult.asBoolean()) {
ProloGraalSuccess success = (ProloGraalSuccess)callResult;
if(success.getSuccesses().size() > 0) {
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().forEach(x -> {
if(x.isBound())
......
......@@ -2,6 +2,7 @@ package ch.heiafr.prolograal.nodes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
......@@ -12,13 +13,35 @@ import ch.heiafr.prolograal.runtime.*;
public final class ProloGraalResolverNode extends RootNode {
private static class AugmentedArrayList<T> extends ArrayList<T> {
private static final long serialVersionUID = 1L;
public T pop() {
T obj = this.get(this.size()-1);
this.remove(this.size()-1);
return obj;
}
}
private static class ProloGraalResolveState {
private int index = 0;
public void setIndex(int index) {
this.index = index;
}
public int getIndex() {
return this.index;
}
}
private final ProloGraalContext context;
private final ProloGraalRuntime runtime;
private final Map<ProloGraalTerm<?>, List<ProloGraalClause>> clauses;
protected ProloGraalResolverNode(ProloGraalLanguage language) {
super(language);
this.context = language.getContextReference().get();
this.runtime = context.getRuntime();
this.clauses = context.getRuntime().getClauses();
}
@Override
......@@ -35,38 +58,59 @@ public final class ProloGraalResolverNode extends RootNode {
* if(filteredTerms.size() == 0) throw new ProloGraalExistenceError();
*/
return resolve(goalRuntime);
states = new AugmentedArrayList<>();
return resolve(goalRuntime.getFirstClause().getHead(), 0);
}
public ProloGraalBoolean resolve(ProloGraalRuntime goalRuntime) {
// System.out.println("Current goal : " + goal);
// System.out.println("Current candidates : " + terms);
private List<ProloGraalResolveState> states;
ProloGraalClause goal = goalRuntime.getFirstClause();
List<ProloGraalSuccess> successes = new ArrayList<>();
private void cleanClauseGoals(ProloGraalClause clause) {
clause.getGoals().stream()
.filter(x -> x instanceof ProloGraalStructure)
.flatMap(x -> ((ProloGraalStructure)x).getVariables().values().stream())
.forEach(ProloGraalVariable::unbind);
}
List<ProloGraalClause> possibleClauses = runtime.getClauses().get(goal.getHead());
public ProloGraalBoolean resolve(ProloGraalTerm<?> goal, int depth) {
System.out.println("Resolving : " + goal + " at depth " + depth);
List<ProloGraalClause> possibleClauses = clauses.get(goal);
ProloGraalResolveState state;
if(states.size() > depth) {
state = states.get(depth);
} else {
state = new ProloGraalResolveState();
states.add(state);
}
if(possibleClauses == null)
throw new ProloGraalExistenceError();
for(int i = 0; i < possibleClauses.size(); i++) {
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(goal));
ProloGraalClause clause = possibleClauses.get(i);
if(goal.isUnifiable(clause.getHead())) {
if(clause.isFact()) {
System.out.println("New success with : ");
return new ProloGraalSuccess(clause);
} else {
for(int j = 0; j < clause.getGoalCount(); j++) {
ProloGraalTerm<?> newGoal = clause.getGoals().get(j);
System.out.println("New resolve goal : " + newGoal + " at depth " + (depth+1));
ProloGraalBoolean result = resolve(newGoal, depth+1);
if(!result.asBoolean()) {
cleanClauseGoals(clause);
return new ProloGraalFailure();
}
}
if(depth == 0)
cleanClauseGoals(clause);
return new ProloGraalSuccess(clause);
}
}
state.setIndex(i+1);
clause.getVariables().values().forEach(ProloGraalVariable::unbind);
goal.getVariables().values().forEach(ProloGraalVariable::unbind);
}
if(successes.size() > 0) {
return new ProloGraalSuccess(successes);
}
System.out.println("Failure for " + goal + " at depth " + depth);
return new ProloGraalFailure();
}
......
......@@ -33,6 +33,14 @@ public class ProloGraalClause {
goals.add(goal);
}
public int getGoalCount() {
return goals.size();
}
public List<ProloGraalTerm<?>> getGoals() {
return goals;
}
public Map<ProloGraalVariable, ProloGraalVariable> getVariables() {
return variables;
}
......@@ -41,9 +49,4 @@ public class ProloGraalClause {
public String toString() {
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
......@@ -84,13 +84,6 @@ 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
......
package ch.heiafr.prolograal.runtime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
protected ProloGraalAtom functor;
protected final Map<ProloGraalVariable, ProloGraalVariable> variables;
protected final List<ProloGraalTerm<?>> subterms;
protected int arity;
public ProloGraalStructure() {
this.subterms = new ArrayList<>();
this.variables = new HashMap<>();
}
// private copy constructor
private ProloGraalStructure(ProloGraalAtom functor, List<ProloGraalTerm<?>> subterms) {
private ProloGraalStructure(ProloGraalAtom functor, List<ProloGraalTerm<?>> subterms, Map<ProloGraalVariable, ProloGraalVariable> variables) {
this.functor = functor;
this.subterms = subterms;
this.arity = subterms.size();
this.variables = new HashMap<>();
variables.values().forEach(x -> this.variables.put(x, x).copy());
}
public void addSubterm(ProloGraalTerm<?> subterm) {
this.subterms.add(subterm);
if(subterm instanceof ProloGraalVariable) {
this.variables.put((ProloGraalVariable)subterm, (ProloGraalVariable)subterm);
}
arity++;
}
public Map<ProloGraalVariable, ProloGraalVariable> getVariables() {
return this.variables;
}
public void setFunctor(ProloGraalAtom functor) {
this.functor = functor;
}
......@@ -81,6 +94,6 @@ public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructur
public ProloGraalStructure copy() {
List<ProloGraalTerm<?>> subterms = new ArrayList<>(this.subterms.size());
this.subterms.forEach(x -> subterms.add(x.copy()));
return new ProloGraalStructure(this.functor.copy(), subterms);
return new ProloGraalStructure(this.functor.copy(), subterms, variables);
}
}
\ 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