Commit 8e4922ee authored by Martin Spoto's avatar Martin Spoto
Browse files

make list display more beautiful with variables

parent c4d74998
package ch.heiafr.prolograal.nodes;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.stream.Collectors;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.exceptions.ProloGraalExistenceError;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
import java.util.List;
import java.util.Map;
public final class ProloGraalResolverNode extends RootNode {
......@@ -30,12 +27,18 @@ public final class ProloGraalResolverNode extends RootNode {
if (ProloGraalLanguage.DEBUG_MODE) {
System.out.println("Executing : " + goalRuntime.getFirstClause());
}
ProloGraalBoolean r = resolve(goalRuntime.getFirstClause().getHead());
makePretty(goalRuntime.getFirstClause().getHead());
ProloGraalTerm<?> head = goalRuntime.getFirstClause().getHead();
System.out.println(goalRuntime.getFirstClause().getHead());
ProloGraalBoolean r = resolve(head);
makePretty(head);
if(head instanceof ProloGraalStructure) {
System.out.println(((ProloGraalStructure) head).toRootString());
} else {
System.out.println(head.toString());
}
return r;
}
......
package ch.heiafr.prolograal.runtime;
import ch.heiafr.prolograal.builtins.ProloGraalBuiltinAtoms;
import java.util.Map;
public final class ProloGraalAtom extends ProloGraalTerm<ProloGraalAtom> {
......
......@@ -7,33 +7,39 @@ import java.util.stream.Collectors;
import ch.heiafr.prolograal.builtins.ProloGraalBuiltinAtoms;
public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
public class ProloGraalList extends ProloGraalStructure {
protected List<ProloGraalTerm<?>> items;
protected ProloGraalTerm<?> tail = ProloGraalBuiltinAtoms.EMPTY_LIST;
protected ProloGraalStructure internal;
public ProloGraalList(Map<ProloGraalVariable, ProloGraalVariable> variables) {
super(variables);
items = new ArrayList<>();
}
public static ProloGraalList fromInternal(Map<ProloGraalVariable, ProloGraalVariable> variables, ProloGraalStructure internal) {
ProloGraalList r = new ProloGraalList(variables);
public List<ProloGraalTerm<?>> getItems() {
return items;
}
public ProloGraalTerm<?> getTail() {
return tail;
}
public static ProloGraalList fromInternal(ProloGraalStructure internal) {
ProloGraalList r = new ProloGraalList(null);
while(true) {
if(internal.getFunctor() != ProloGraalBuiltinAtoms.DOT_OPERATOR || internal.getArity() != 2) {
if(!internal.getFunctor().equals(ProloGraalBuiltinAtoms.DOT_OPERATOR) || internal.getArity() != 2) {
throw new RuntimeException("Cannot build list from internal representation");
}
ProloGraalTerm<?> head = internal.getArguments().get(0);
ProloGraalTerm<?> tail = internal.getArguments().get(1);
if(head instanceof ProloGraalStructure && ((ProloGraalStructure)head).getFunctor() == ProloGraalBuiltinAtoms.DOT_OPERATOR) {
r.items.add(ProloGraalList.fromInternal(variables, (ProloGraalStructure) head));
ProloGraalTerm<?> head = internal.getArguments().get(0).getRootValue();
ProloGraalTerm<?> tail = internal.getArguments().get(1).getRootValue();
if(head instanceof ProloGraalStructure && ((ProloGraalStructure)head).getFunctor().equals(ProloGraalBuiltinAtoms.DOT_OPERATOR)) {
r.items.add(ProloGraalList.fromInternal((ProloGraalStructure) head));
} else {
r.items.add(head);
}
if(!(tail instanceof ProloGraalStructure) || ((ProloGraalStructure)tail).getFunctor() != ProloGraalBuiltinAtoms.DOT_OPERATOR) {
if(!(tail instanceof ProloGraalStructure) || !((ProloGraalStructure)tail).getFunctor().equals(ProloGraalBuiltinAtoms.DOT_OPERATOR)) {
r.tail = tail;
break;
} else {
......@@ -58,59 +64,20 @@ public class ProloGraalList extends ProloGraalTerm<ProloGraalList> {
public void buildInteralRepresentation() {
assert items != null && !items.isEmpty();
internal = new ProloGraalStructure(variables);
ProloGraalStructure internal = this;
ProloGraalStructure struct = internal;
internal.setFunctor(ProloGraalBuiltinAtoms.DOT_OPERATOR);
for(int i = 0; i < items.size()-1; i++) {
ProloGraalTerm<?> item = items.get(i);
if(item instanceof ProloGraalList) {
struct.addSubterm(((ProloGraalList)item).internal);
} else {
struct.addSubterm(item);
}
struct.addSubterm(item);
ProloGraalStructure next = new ProloGraalStructure(variables);
next.setFunctor(ProloGraalBuiltinAtoms.DOT_OPERATOR);
struct.addSubterm(next);
struct = next;
}
ProloGraalTerm<?> item = items.get(items.size()-1);
if(item instanceof ProloGraalList) {
struct.addSubterm(((ProloGraalList)item).internal);
} else {
struct.addSubterm(item);
}
if(tail instanceof ProloGraalList) {
struct.addSubterm(((ProloGraalList)this.tail).internal);
} else {
struct.addSubterm(this.tail);
}
}
@Override
public String toString() {
String end = "";
if(tail != ProloGraalBuiltinAtoms.EMPTY_LIST) {
end = " | " + tail.toString();
}
return "[" + items.stream().map(Object::toString).collect(Collectors.joining(", ")) + end + "]";
}
@Override
public boolean equals(Object obj) {
if(obj == null) return false;
if(!(obj instanceof ProloGraalList)) return false;
return this.internal.equals(((ProloGraalList)obj).internal);
}
@Override
public boolean isUnifiable(ProloGraalTerm<?> other) {
if(other instanceof ProloGraalList) {
return this.internal.isUnifiable(((ProloGraalList)other).internal);
} else if(other instanceof ProloGraalVariable) {
return other.isUnifiable(this);
}
return this.internal.isUnifiable(other);
struct.addSubterm(item);
struct.addSubterm(this.tail);
}
@Override
......
......@@ -5,8 +5,9 @@ import ch.heiafr.prolograal.builtins.ProloGraalBuiltinAtoms;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
public class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
protected ProloGraalAtom functor;
protected final List<ProloGraalTerm<?>> subterms;
protected int arity;
......@@ -26,8 +27,8 @@ public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructur
public void addSubterm(ProloGraalTerm<?> subterm) {
this.subterms.add(subterm);
if(subterm instanceof ProloGraalVariable) {
this.variables.put((ProloGraalVariable)subterm, (ProloGraalVariable)subterm);
if (subterm instanceof ProloGraalVariable) {
this.variables.put((ProloGraalVariable) subterm, (ProloGraalVariable) subterm);
}
arity++;
}
......@@ -52,23 +53,47 @@ public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructur
return this.arity;
}
public String toRootString() {
if (functor == null)
return "(Empty structure)";
else if (functor.equals(ProloGraalBuiltinAtoms.DOT_OPERATOR) && arity == 2) {
ProloGraalList list = ProloGraalList.fromInternal(this);
String tail = !list.getTail().getRootValue().equals(ProloGraalBuiltinAtoms.EMPTY_LIST) ? " | " + list.getTail().getRootValue().toString() : "";
return "[" + list.getItems().stream().map(ProloGraalTerm::getRootValue).map(x -> {
if (x instanceof ProloGraalStructure)
return ((ProloGraalStructure) x).toRootString();
else
return x.toString();
}).collect(Collectors.joining(", ")) + tail + "]";
}
String subtermsString = subterms.stream().map(ProloGraalTerm::getRootValue).map(x -> {
if (x instanceof ProloGraalStructure)
return ((ProloGraalStructure) x).toRootString();
else
return x.toString();
}).collect(Collectors.joining(", "));
return functor.getName() + "/" + arity + "(" + subtermsString + ")";
}
@Override
public String toString() {
if(functor == null)
if (functor == null)
return "(Empty structure)";
else if(functor == ProloGraalBuiltinAtoms.DOT_OPERATOR) {
return ProloGraalList.fromInternal(null, this).toString();
else if (functor.equals(ProloGraalBuiltinAtoms.DOT_OPERATOR) && arity == 2) {
ProloGraalList list = ProloGraalList.fromInternal(this);
String tail = !list.getTail().equals(ProloGraalBuiltinAtoms.EMPTY_LIST) ? " | " + list.getTail().toString() : "";
return "[" + list.getItems().stream().map(Object::toString).collect(Collectors.joining(", ")) + tail + "]";
}
String subtermsString = subterms.toString();
return functor.getName() + "/" + arity + "(" + subtermsString.substring(1, subtermsString.length()-1) + ")";
return functor.getName() + "/" + arity + "(" + subtermsString.substring(1, subtermsString.length() - 1) + ")";
}
@Override
public boolean equals(Object obj) {
if(obj == null) return false;
if(!(obj instanceof ProloGraalStructure)) return false;
ProloGraalStructure other = (ProloGraalStructure)obj;
if (obj == null) return false;
if (!(obj instanceof ProloGraalStructure)) return false;
ProloGraalStructure other = (ProloGraalStructure) obj;
return this.functor.equals(other.functor) && this.arity == other.arity;
}
......@@ -79,16 +104,14 @@ 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))) {
if (this.equals(other)) {
for (int i = 0; i < this.arity; i++) {
if (!this.subterms.get(i).isUnifiable(((ProloGraalStructure) other).subterms.get(i))) {
return false;
}
}
return true;
} else if(other instanceof ProloGraalVariable) {
} else if (other instanceof ProloGraalVariable) {
return other.isUnifiable(this);
}
return false;
......
......@@ -86,7 +86,11 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
@Override
public String toString() {
return this.name + (this.isBound ? " = " + this.getRootValue().toString() : "");
if(ProloGraalLanguage.DEBUG_MODE) {
return this.name + (this.isBound ? " = " + this.getRootValue().toString() : "");
} else {
return this.getRootValue().toString();
}
}
@Override
......@@ -96,13 +100,17 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
}
// occurs check
if (!(other instanceof ProloGraalVariable) &&
other.getVariables() != null &&
other.getVariables().containsKey(this) &&
other.getVariables().get(this) == this) {
return false;
other.getVariables() != null) {
ProloGraalTerm<?> rootVal = this.getRootValue();
if(rootVal instanceof ProloGraalVariable) {
ProloGraalVariable rootVar = (ProloGraalVariable)rootVal;
if(other.variables.containsKey(rootVar) && other.variables.get(rootVar) == rootVar) {
return false;
}
}
}
if (this.isBound) {
return this.boundValue.isUnifiable(other.getRootValue());
return this.boundValue.getRootValue().isUnifiable(other.getRootValue());
} else {
this.bind(other.getRootValue());
return true;
......
......@@ -10,6 +10,10 @@ test2(_, R) :-
test3(a, 4).
test3(b, 5).
concatDL('-'(Xs,Ys), '-'(Ys, DYs), '-'(Xs,DYs)).
% concatDL('-'([a, b, c | Xs], Xs), '-'([d, e, f | Ys], Ys), R).
fact([a, b, c, d]).
nested([a, [b, c, d], e, f]).
ending([a, b | [c, d, e | [f, g | h]]]).
......@@ -77,4 +81,4 @@ unifyMany(A, A).
unifyMany3([], []).
unifyMany3([X | Xs], [X | Ys]) :-
unifyMany3(Xs, Ys).
\ No newline at end of file
unifyMany3(Xs, Ys).
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