Commit 6c74ca7b authored by Martin Spoto's avatar Martin Spoto
Browse files

Fix occurence check

parent c9d53d9e
......@@ -2,34 +2,33 @@ package ch.heiafr.prolograal.runtime;
import ch.heiafr.prolograal.builtins.ProloGraalBuiltinAtoms;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
public class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
protected ProloGraalAtom functor;
protected final List<ProloGraalTerm<?>> subterms;
/**
* Variables contained in this structure only (not in context)
*/
protected final Map<ProloGraalVariable, ProloGraalVariable> subVariables;
protected int arity;
public ProloGraalStructure(Map<ProloGraalVariable, ProloGraalVariable> variables) {
super(variables);
this.subterms = new ArrayList<>();
}
// private copy constructor
private ProloGraalStructure(Map<ProloGraalVariable, ProloGraalVariable> variables, ProloGraalAtom functor, List<ProloGraalTerm<?>> subterms) {
super(variables);
this.functor = functor;
this.subterms = subterms;
this.arity = subterms.size();
this.subVariables = new HashMap<>();
}
public void addSubterm(ProloGraalTerm<?> subterm) {
this.subterms.add(subterm);
if (subterm instanceof ProloGraalVariable) {
this.subVariables.put((ProloGraalVariable) subterm, (ProloGraalVariable) subterm);
this.variables.put((ProloGraalVariable) subterm, (ProloGraalVariable) subterm);
}
if(subterm instanceof ProloGraalStructure) {
this.subVariables.putAll(((ProloGraalStructure) subterm).subVariables);
}
arity++;
}
......@@ -37,6 +36,10 @@ public class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
return this.variables;
}
public Map<ProloGraalVariable, ProloGraalVariable> getSubVariables() {
return this.subVariables;
}
public void setFunctor(ProloGraalAtom functor) {
this.functor = functor;
}
......@@ -119,8 +122,9 @@ public class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
@Override
public ProloGraalStructure copy(Map<ProloGraalVariable, ProloGraalVariable> variables) {
List<ProloGraalTerm<?>> subterms = new ArrayList<>(this.subterms.size());
this.subterms.forEach(x -> subterms.add(x.copy(variables)));
return new ProloGraalStructure(variables, this.functor.copy(variables), subterms);
ProloGraalStructure struct = new ProloGraalStructure(variables);
struct.setFunctor(this.functor.copy(variables));
this.subterms.forEach(x -> struct.addSubterm(x.copy(variables)));
return struct;
}
}
\ No newline at end of file
......@@ -99,20 +99,22 @@ public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable>
return true;
}
// occurs check
if (!(other instanceof ProloGraalVariable) &&
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) {
ProloGraalTerm<?> rootValue = other.getRootValue();
if(rootValue instanceof ProloGraalStructure) {
Map<ProloGraalVariable, ProloGraalVariable> subVariables = ((ProloGraalStructure) rootValue).getSubVariables();
if(subVariables.get(this) == this) { // contains directly this
return false;
}
for(ProloGraalVariable var : subVariables.values()) {
if(var.getRootValue() == this) // or contains indirectly
return false;
}
}
}
if (this.isBound) {
return this.boundValue.getRootValue().isUnifiable(other.getRootValue());
return this.boundValue.getRootValue().isUnifiable(rootValue);
} else {
this.bind(other.getRootValue());
this.bind(rootValue);
return true;
}
}
......
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