Commit 4a5f801d authored by Martin Spoto's avatar Martin Spoto
Browse files

Working for test predicate ?

parent e07e3880
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
......@@ -7,10 +7,11 @@
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="launcher" />
<module name="prolograal" />
<module name="language" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="language" target="1.8" />
<module name="launcher" target="1.8" />
<module name="prolograal" target="1.8" />
<module name="prolograal-parent" target="1.5" />
......
......@@ -6,6 +6,11 @@
<option value="$PROJECT_DIR$/code/pom.xml" />
</list>
</option>
<option name="ignoredFiles">
<set>
<option value="$PROJECT_DIR$/code/language/pom.xml" />
</set>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="GraalVM-19.2.0.1" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
......
......@@ -4,9 +4,7 @@
<modules>
<module fileurl="file://$PROJECT_DIR$/code/language/src/main/language.iml" filepath="$PROJECT_DIR$/code/language/src/main/language.iml" />
<module fileurl="file://$PROJECT_DIR$/code/launcher/launcher.iml" filepath="$PROJECT_DIR$/code/launcher/launcher.iml" />
<module fileurl="file://$PROJECT_DIR$/code/language/prolograal.iml" filepath="$PROJECT_DIR$/code/language/prolograal.iml" />
<module fileurl="file://$PROJECT_DIR$/code/prolograal-parent.iml" filepath="$PROJECT_DIR$/code/prolograal-parent.iml" />
<module fileurl="file://$PROJECT_DIR$/code/language/src/test/test.iml" filepath="$PROJECT_DIR$/code/language/src/test/test.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="ProloGraalMain" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="ch.heiafr.prolograal.launcher.ProloGraalMain" />
<module name="launcher" />
<option name="PROGRAM_PARAMETERS" value="&quot;$ProjectFileDir$\code\language\tests\04_clauses.pl&quot;" />
<option name="VM_PARAMETERS" value="-Dtruffle.class.path.append=&quot;$ProjectFileDir$\code\language\target\prolograal-19.2.0.1-SNAPSHOT.jar;$ProjectFileDir$\code\antlr-4.7.1-complete.jar&quot;" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="ch.heiafr.prolograal.launcher.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
<option name="Maven.BeforeRunTask" enabled="true" file="$PROJECT_DIR$/code/language/pom.xml" goal="package" />
</method>
</configuration>
</component>
\ No newline at end of file
......@@ -83,11 +83,9 @@ public class ProloGraalLanguage extends TruffleLanguage<ProloGraalContext> {
protected boolean isObjectOfLanguage(Object object) {
if (!(object instanceof TruffleObject)) {
return false;
} else if (object instanceof ProloGraalTerm<?> || object instanceof ProloGraalBoolean) {
return true;
} else {
return false;
}
return object instanceof ProloGraalTerm<?> || object instanceof ProloGraalBoolean;
}
@Override
......
......@@ -36,8 +36,6 @@ public final class ProloGraalResolverNode extends RootNode {
return r;
}
private Stack<ProloGraalClause> visitedClauses = new Stack<>();
public ProloGraalBoolean resolve(ProloGraalTerm<?> goal) {
System.out.println("Resolving goal " + goal);
......@@ -48,9 +46,9 @@ public final class ProloGraalResolverNode extends RootNode {
throw new ProloGraalExistenceError();
// for each possible clause
for(ProloGraalClause clauseIt : possibleClauses) {
outer: for(ProloGraalClause clauseIt : possibleClauses) {
ProloGraalClause clause = clauseIt.copy(); // creates a new context for this clause
visitedClauses.push(clause);
System.out.println("Trying " + clause + " to resolve " + goal);
// save current states
goal.save();
......@@ -59,7 +57,6 @@ public final class ProloGraalResolverNode extends RootNode {
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
clause = clauseIt.copy();
goal.isUnifiable(clause.getHead()); // unify again to report variables to parent
System.out.println("Success for goal : " + goal);
return new ProloGraalSuccess();
......@@ -67,24 +64,24 @@ public final class ProloGraalResolverNode extends RootNode {
// 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);
subGoal.save();
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);
continue;
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);
// unify again to report variables to parent
clause = clauseIt.copy();
goal.isUnifiable(clause.getHead());
return new ProloGraalSuccess();
}
} else {
System.out.println("Could not unify " + clause + " for goal " + goal);
// undo all changes that the unification may have done
// ! should be a no-op !
goal.undo();
}
}
......
......@@ -22,7 +22,7 @@ public final class ProloGraalAtom extends ProloGraalTerm<ProloGraalAtom> {
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalAtom && this.name.equals(((ProloGraalAtom)obj).name);
return obj instanceof ProloGraalAtom && this.name.equals(((ProloGraalAtom) obj).name);
}
@Override
......@@ -30,7 +30,7 @@ public final class ProloGraalAtom extends ProloGraalTerm<ProloGraalAtom> {
if(this.equals(other)) {
return true;
} else if(other instanceof ProloGraalVariable) {
return ((ProloGraalVariable)other).isUnifiable(this);
return other.isUnifiable(this);
}
return false;
}
......
......@@ -28,7 +28,7 @@ public final class ProloGraalDoubleNumber extends ProloGraalNumber<ProloGraalDou
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalDoubleNumber && this.value == ((ProloGraalDoubleNumber)obj).value;
return obj instanceof ProloGraalDoubleNumber && this.value == ((ProloGraalDoubleNumber) obj).value;
}
@Override
......
......@@ -28,7 +28,7 @@ public final class ProloGraalIntegerNumber extends ProloGraalNumber<ProloGraalIn
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalIntegerNumber && this.value == ((ProloGraalIntegerNumber)obj).value;
return obj instanceof ProloGraalIntegerNumber && this.value == ((ProloGraalIntegerNumber) obj).value;
}
@Override
......
......@@ -40,8 +40,6 @@ public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
internal = (ProloGraalStructure) tail;
}
}
System.out.println(r);
return r;
}
......@@ -94,7 +92,7 @@ public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
if(tail != ProloGraalBuiltinAtoms.EMPTY_LIST) {
end = " | " + tail.toString();
}
return "[" + items.stream().map(x -> x.toString()).collect(Collectors.joining(", ")) + end + "]";
return "[" + items.stream().map(Object::toString).collect(Collectors.joining(", ")) + end + "]";
}
@Override
......@@ -110,7 +108,7 @@ public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
if(other instanceof ProloGraalList) {
return this.internal.isUnifiable(((ProloGraalList)other).internal);
} else if(other instanceof ProloGraalVariable) {
return ((ProloGraalVariable)other).isUnifiable(this);
return other.isUnifiable(this);
}
return this.internal.isUnifiable(other);
}
......@@ -125,17 +123,4 @@ public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
list.buildInteralRepresentation();
return list;
}
// save current state
@Override
public void save() {
this.internal.save();
}
// go back to previous state
@Override
public void undo() {
this.internal.undo();
}
}
\ No newline at end of file
......@@ -12,13 +12,13 @@ public abstract class ProloGraalNumber<T extends ProloGraalTerm<T>> extends Prol
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalNumber && ((ProloGraalNumber<?>)obj).asDouble() == this.asDouble() && ((ProloGraalNumber<?>)obj).asInt() == this.asInt();
return obj instanceof ProloGraalNumber && ((ProloGraalNumber<?>) obj).asDouble() == this.asDouble() && ((ProloGraalNumber<?>) obj).asInt() == this.asInt();
}
@Override
public boolean isUnifiable(ProloGraalTerm<?> other) {
if(other instanceof ProloGraalVariable)
return ((ProloGraalVariable)other).isUnifiable(this);
return other.isUnifiable(this);
else
return this.equals(other);
}
......
......@@ -23,7 +23,7 @@ public final class ProloGraalRuntime {
}
public final ProloGraalClause getFirstClause() {
return clauses.values().stream().findFirst().orElseThrow(() -> new RuntimeException()).get(0);
return clauses.values().stream().findFirst().orElseThrow(RuntimeException::new).get(0);
}
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
import ch.heiafr.prolograal.builtins.ProloGraalBuiltinAtoms;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
......@@ -54,6 +56,9 @@ public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructur
public String toString() {
if(functor == null)
return "(Empty structure)";
else if(functor == ProloGraalBuiltinAtoms.DOT_OPERATOR) {
return ProloGraalList.fromInternal(null, this).toString();
}
String subtermsString = subterms.toString();
return functor.getName() + "/" + arity + "(" + subtermsString.substring(1, subtermsString.length()-1) + ")";
}
......@@ -82,7 +87,7 @@ public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructur
}
return true;
} else if(other instanceof ProloGraalVariable) {
return ((ProloGraalVariable)other).isUnifiable(this);
return other.isUnifiable(this);
}
return false;
}
......@@ -93,15 +98,4 @@ public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructur
this.subterms.forEach(x -> subterms.add(x.copy(variables)));
return new ProloGraalStructure(variables, this.functor.copy(variables), subterms);
}
// save current state
@Override
public void save() {
this.subterms.forEach(ProloGraalTerm::save);
}
@Override
public void undo() {
this.subterms.forEach(ProloGraalTerm::undo);
}
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
import java.util.Deque;
import java.util.Map;
import com.oracle.truffle.api.interop.TruffleObject;
......@@ -25,13 +26,13 @@ public abstract class ProloGraalTerm<T extends ProloGraalTerm<T>> implements Tru
public abstract boolean isUnifiable(ProloGraalTerm<?> other);
// save current state
public void save() {
public final void save() {
variables.values().forEach(ProloGraalVariable::saveState);
}
// go back to previous state
public void undo() {
public final void undo() {
variables.values().forEach(ProloGraalVariable::revertState);
}
public Map<ProloGraalVariable, ProloGraalVariable> getVariables() {
......
......@@ -14,7 +14,6 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
private final String name;
private boolean isBound = false;
private ProloGraalTerm<?> boundValue;
private ProloGraalVariable parent; // used to indroduce a double link when binding variables
public ProloGraalVariable(Map<ProloGraalVariable, ProloGraalVariable> variables, String name) {
super(variables);
......@@ -23,30 +22,33 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
}
this.name = name;
this.hashCode = name.hashCode();
variables.put(this, this);
variables.putIfAbsent(this, this);
}
public ProloGraalVariable(Map<ProloGraalVariable, ProloGraalVariable> variables, ProloGraalVariable other) {
super(variables);
this.name = other.name;
this.isBound = other.isBound;
if(other.isBound)
if (other.isBound) {
this.boundValue = other.boundValue.copy(variables);
variables.put(this, this);
}
this.hashCode = name.hashCode();
variables.putIfAbsent(this, this);
}
private ProloGraalVariable(ProloGraalVariable other) {
super(null);
this.name = other.name;
this.isBound = other.isBound;
this.boundValue = other.boundValue;
this.hashCode = other.hashCode;
}
public ProloGraalTerm<?> getValue() {
if (isBound)
if (isBound) {
return boundValue;
else
} else {
return this;
}
private void signalParent(ProloGraalTerm<?> other) {
if(this.parent != null) {
System.out.println(this + " signaling to " + this.parent);
parent.bind(other);
parent.signalParent(other);
}
}
......@@ -62,12 +64,8 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
}
this.isBound = true;
this.boundValue = other;
if(other instanceof ProloGraalVariable) {
((ProloGraalVariable)other).parent = this;
}
signalParent(other);
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println(name + " = " + other + "<@" + System.identityHashCode(variables) + ">");
System.out.println(name + " = " + other);
}
}
......@@ -78,7 +76,7 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalVariable && this.name.equals(((ProloGraalVariable) obj).name);
return obj instanceof ProloGraalVariable && this.name.equals(((ProloGraalVariable) obj).name);
}
@Override
......@@ -91,26 +89,29 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
}
}
}*/
return this.name + (this.isBound ? " = " + boundValue.toString() : "") + " <@" + System.identityHashCode(variables) + ">";
return this.name + (this.isBound ? " = " + boundValue.toString() : "");
}
@Override
public boolean isUnifiable(ProloGraalTerm<?> other) {
if (other instanceof ProloGraalVariable && (ProloGraalVariable) other == this) // can always unify to itself
if (other instanceof ProloGraalVariable && other == this) // can always unify to itself
{
return true;
}
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
// loops
} else {
otherVar.bind(this);
return true;
}
} else {
if(ProloGraalLanguage.DEBUG_MODE)
/*if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Already bound :" + this.toString() + ", trying to unify to : " + other.toString());
}*/
return this.boundValue.isUnifiable(other);
}
} else {
......@@ -137,16 +138,22 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
}
// save current state
@Override
public void save() {
states.push(this.copy(variables));
public void saveState() {
//System.out.println("Saving state " + this);
states.push(new ProloGraalVariable(this));
if(this.isBound && this.boundValue instanceof ProloGraalVariable) {
((ProloGraalVariable) this.boundValue).saveState();
}
}
// go back to previous state
@Override
public void undo() {
public void revertState() {
ProloGraalVariable previous = states.pop();
this.isBound = previous.isBound;
this.boundValue = previous.boundValue;
//System.out.println("Restored state " + this);
if(this.isBound && this.boundValue instanceof ProloGraalVariable) {
((ProloGraalVariable) this.boundValue).revertState();
}
}
}
\ No newline at end of file
......@@ -65,5 +65,10 @@
<artifactId>graal-sdk</artifactId>
<version>${graalvm.version}</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7</version>
</dependency>
</dependencies>
</project>
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