Access Control is now enabled for Gitlab Pages so you can now restrict Pages to authorized users only. If needed, make your website publicly available : Navigate to your project's Settings > General > Visibility and select Everyone in pages section.

Commit 127704e0 authored by Tony Licata's avatar Tony Licata

optimized is/2 by removing printer, separated is/2 operators in another class,...

optimized is/2 by removing printer, separated is/2 operators in another class, cleaned proofTreeNode execute while
parent 6cd9f979
package ch.heiafr.prolograal.builtins;
import ch.heiafr.prolograal.runtime.ProloGraalAtom;
import ch.heiafr.prolograal.runtime.ProloGraalDoubleNumber;
import ch.heiafr.prolograal.runtime.ProloGraalIntegerNumber;
import ch.heiafr.prolograal.runtime.ProloGraalNumber;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* Class containing the different built-in atoms.
* Unlike built-in predicates, these are meant for internal use.
* @author Martin Spoto
* @see ProloGraalAtom
*
* @author Tony Licata
* @see ch.heiafr.prolograal.builtins.predicates.ProloGraalIsBuiltin
*/
public final class ProloGraalBuiltinAtoms {
public static final ProloGraalAtom EMPTY_LIST = new ProloGraalAtom(null, "[]");
public static final ProloGraalAtom DOT_OPERATOR = new ProloGraalAtom(null, ".");
public final class ProloGraalIsOperators {
/* is/2 operators */
public static ProloGraalNumber pi(List<ProloGraalNumber> args) {
if (!args.isEmpty()) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.PI);
}
public static ProloGraalNumber e(List<ProloGraalNumber> args) {
if (!args.isEmpty()) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.E);
}
public static ProloGraalNumber tau(List<ProloGraalNumber> args) {
if (!args.isEmpty()) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.PI * 2);
}
public static ProloGraalNumber add(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if (areAllIntegers(arg0, arg1))
return new ProloGraalIntegerNumber(new HashMap<>(), arg0.asInteger() + arg1.asInteger());
return new ProloGraalDoubleNumber(new HashMap<>(), arg0.asDouble() + arg1.asDouble());
}
public static ProloGraalNumber substract(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if (areAllIntegers(arg0, arg1))
return new ProloGraalIntegerNumber(new HashMap<>(), arg0.asInteger() - arg1.asInteger());
return new ProloGraalDoubleNumber(new HashMap<>(), arg0.asDouble() - arg1.asDouble());
}
public static ProloGraalNumber multiply(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if (areAllIntegers(arg0, arg1))
return new ProloGraalIntegerNumber(new HashMap<>(), arg0.asInteger() * arg1.asInteger());
return new ProloGraalDoubleNumber(new HashMap<>(), arg0.asDouble() * arg1.asDouble());
}
public static ProloGraalNumber divide(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), args.get(0).asDouble() / args.get(1).asDouble());
}
public static ProloGraalNumber pow(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if (areAllIntegers(arg0, arg1) && Math.abs(Math.pow(arg0.asInteger(), arg1.asInteger())) < Integer.MAX_VALUE)
return new ProloGraalIntegerNumber(new HashMap<>(), (int) Math.pow(arg0.asInteger(), arg1.asInteger()));
return new ProloGraalDoubleNumber(new HashMap<>(), Math.pow(arg0.asDouble(), arg1.asDouble()));
}
public static ProloGraalNumber sqrt(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.sqrt(args.get(0).asDouble()));
}
public static ProloGraalNumber rem(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if (areAllIntegers(arg0, arg1))
return new ProloGraalIntegerNumber(new HashMap<>(), arg0.asInteger() % arg1.asInteger());
return new ProloGraalDoubleNumber(new HashMap<>(), arg0.asDouble() % arg1.asDouble());
}
public static ProloGraalNumber mod(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
double ad = arg0.asDouble();
double bd = arg1.asDouble();
double res = ((ad % bd + bd) % bd);
if (areAllIntegers(arg0, arg1))
return new ProloGraalIntegerNumber(new HashMap<>(), (int) res);
return new ProloGraalDoubleNumber(new HashMap<>(), res);
}
public static ProloGraalNumber sign(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
double val = args.get(0).asDouble();
int res = 0;
if (val > 0) res = 1;
if (val < 0) res = -1;
return new ProloGraalIntegerNumber(new HashMap<>(), res);
}
public static ProloGraalNumber exp(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.exp(args.get(0).asDouble()));
}
public static ProloGraalNumber log(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.log(args.get(0).asDouble()));
}
public static ProloGraalNumber sin(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.sin(args.get(0).asDouble()));
}
public static ProloGraalNumber cos(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.cos(args.get(0).asDouble()));
}
public static ProloGraalNumber tan(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.tan(args.get(0).asDouble()));
}
public static ProloGraalNumber asin(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.asin(args.get(0).asDouble()));
}
public static ProloGraalNumber acos(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.acos(args.get(0).asDouble()));
}
public static ProloGraalNumber atan(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.atan(args.get(0).asDouble()));
}
public static ProloGraalNumber atan2(List<ProloGraalNumber> args) {
if (args.size() != 2) return null;
return new ProloGraalDoubleNumber(new HashMap<>(), Math.atan2(args.get(0).asDouble(), args.get(1).asDouble()));
}
public static ProloGraalNumber abs(List<ProloGraalNumber> args) {
if (args.size() != 1) return null;
ProloGraalNumber arg0 = args.get(0);
if (areAllIntegers(arg0))
return new ProloGraalIntegerNumber(new HashMap<>(), Math.abs(arg0.asInteger()));
return new ProloGraalDoubleNumber(new HashMap<>(), Math.abs(arg0.asDouble()));
}
private static boolean areAllIntegers(ProloGraalNumber<?>... args){
for (ProloGraalNumber number : args)
if(!number.isInteger())return false;
return true;
}
public static Map<ProloGraalAtom, Function<List<ProloGraalNumber>,ProloGraalNumber>> getOperationsMap(){
Map<ProloGraalAtom, Function<List<ProloGraalNumber>,ProloGraalNumber>> operationsMap = new HashMap<>();
addOpperation(operationsMap,"pi", ProloGraalIsOperators::pi);
addOpperation(operationsMap,"e",ProloGraalIsOperators::e);
addOpperation(operationsMap,"tau",ProloGraalIsOperators::tau);
addOpperation(operationsMap,"+",ProloGraalIsOperators::add);
addOpperation(operationsMap,"-",ProloGraalIsOperators::substract);
addOpperation(operationsMap,"*",ProloGraalIsOperators::multiply);
addOpperation(operationsMap,"/",ProloGraalIsOperators::divide);
addOpperation(operationsMap,"**",ProloGraalIsOperators::pow);
addOpperation(operationsMap,"^",ProloGraalIsOperators::pow);
addOpperation(operationsMap,"sqrt",ProloGraalIsOperators::sqrt);
addOpperation(operationsMap,"rem",ProloGraalIsOperators::rem);
addOpperation(operationsMap,"mod",ProloGraalIsOperators::mod);
addOpperation(operationsMap,"sign",ProloGraalIsOperators::sign);
addOpperation(operationsMap,"exp",ProloGraalIsOperators::exp);
addOpperation(operationsMap,"log",ProloGraalIsOperators::log);
addOpperation(operationsMap,"sin",ProloGraalIsOperators::sin);
addOpperation(operationsMap,"cos",ProloGraalIsOperators::cos);
addOpperation(operationsMap,"tan",ProloGraalIsOperators::tan);
addOpperation(operationsMap,"asin",ProloGraalIsOperators::asin);
addOpperation(operationsMap,"acos",ProloGraalIsOperators::acos);
addOpperation(operationsMap,"atan",ProloGraalIsOperators::atan);
addOpperation(operationsMap,"atan2",ProloGraalIsOperators::atan2);
addOpperation(operationsMap,"abs",ProloGraalIsOperators::abs);
return operationsMap;
}
private static void addOpperation(Map<ProloGraalAtom, Function<List<ProloGraalNumber>,ProloGraalNumber>> operations,
String atomName,
Function<List<ProloGraalNumber>,ProloGraalNumber> function){
operations.put(new ProloGraalAtom(new HashMap<>(),atomName), function);
}
}
\ No newline at end of file
......@@ -19,7 +19,6 @@ import java.util.function.Function;
*/
public class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
private final PrintWriter writer; // used for outputting
private final ProloGraalContext context; // we keep the context for the copy method
ProloGraalVariable arg; // the variable A in is(A,B). We keep it to use it in the execute method
ProloGraalVariable arg2; // the variable B in is(A,B). We keep it to use it in the execute method
......@@ -30,7 +29,6 @@ public class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
public ProloGraalIsBuiltin(ProloGraalContext context) {
super();
this.writer = new PrintWriter(context.getOutput(), true);
this.context = context;
// create the head of this clause
......@@ -65,8 +63,6 @@ public class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
if(leftTerm.isVariable() || leftTerm.isNumber()){
ProloGraalNumber result = consultTerm(rightTerm);
if(result==null){
writer.print("op nok");
writer.flush();
return new ProloGraalFailure();
}
if(leftTerm.unify(result)){
......@@ -75,8 +71,6 @@ public class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
return new ProloGraalFailure();
}
}else{
writer.print("left term nok");
writer.flush();
return new ProloGraalFailure();
}
}
......
......@@ -7,7 +7,8 @@ import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.NodeInfo;
import org.graalvm.nativeimage.VMRuntime;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
......@@ -19,11 +20,11 @@ import java.util.stream.IntStream;
* @see ProloGraalResolverNode
* @author Martin Spoto
*/
@NodeInfo(shortName = "ProofTreeNode")
@NodeInfo(language = ProloGraalLanguage.ID,shortName = "ProofTreeNode")
public class ProloGraalProofTreeNode extends Node {
private class LocalVariables{
public Deque<ProloGraalTerm<?>> goals;
public ProloGraalTerm<?> currentGoal;
public int start;
public List<ProloGraalClause> unifiableClauses;
}
......@@ -33,9 +34,6 @@ public class ProloGraalProofTreeNode extends Node {
//Stacks used to simulate a recursive call with a while loop
private final Stack<LocalVariables> localVarsStack; // the goals of this node
private final Stack<Deque<ProloGraalTerm<?>>> goalsStack; // the goals of this node
private final Stack<Integer> startStack; // used to skip visited branches in case of redo
private final Stack<List<ProloGraalClause>> unifiableClausesStack; // store clauses that can resolve the current goal
private final Stack<ProloGraalTerm<?>> currentGoalStack; // store the current goal
private final boolean traceFlag; // is the trace ON ?
......@@ -58,9 +56,6 @@ public class ProloGraalProofTreeNode extends Node {
this.localVarsStack = new Stack<>();
this.goalsStack = new Stack<>();
goalsStack.push(goals);
this.startStack = new Stack<>();
this.unifiableClausesStack = new Stack<>();
this.currentGoalStack = new Stack<>();
}
/**
......@@ -137,16 +132,19 @@ public class ProloGraalProofTreeNode extends Node {
//we push everything into their stacks because
//we need to be sure that those variables are initialized with the right value
//when accessing directly to the RETURN case.
currentGoalStack.push(currentGoal);
unifiableClausesStack.push(unifiableClauses);
startStack.push(start);
LocalVariables localVariables = new LocalVariables();
localVariables.currentGoal = currentGoal;
localVariables.unifiableClauses = unifiableClauses;
localVariables.start = start;
localVarsStack.push(localVariables);
goalsStack.push(currentGoals);
//we don't break since we want to continue the standard behaviour of the ProofTreeNode
case RETURN:
//retrieve local variables from their stacks
currentGoal = currentGoalStack.pop();
unifiableClauses = unifiableClausesStack.pop();
start = startStack.pop();
localVariables = localVarsStack.pop();
currentGoal = localVariables.currentGoal;
unifiableClauses = localVariables.unifiableClauses;
start = localVariables.start;
currentGoals = goalsStack.pop();
for (int i = start; i < unifiableClauses.size(); i++) {
if(executeState!=ExecuteState.RETURN) {
......@@ -185,12 +183,16 @@ public class ProloGraalProofTreeNode extends Node {
// no need for distinction between facts and regular clauses
// add all the new goals
Collections.reverse(body);
body.forEach(newGoals::addFirst);
//push i because when we will pop it, we want to continue the for loop from where it stopped.
startStack.push(i);
currentGoalStack.push(currentGoal);
unifiableClausesStack.push(unifiableClauses);
localVariables.start = i;
localVariables.currentGoal = currentGoal;
localVariables.unifiableClauses = unifiableClauses;
localVarsStack.push(localVariables);
goalsStack.push(currentGoals);
//goalsStack.push(newGoals);
goalsStack.push(newGoals);
executeState = ExecuteState.BEGIN;
break;
......@@ -225,6 +227,13 @@ public class ProloGraalProofTreeNode extends Node {
break;
}
}
/*
try{
File file = File.createTempFile("SVMHeapDump-", ".hprof");
VMRuntime.dumpHeap(file.getAbsolutePath(), true);
}catch(Exception e){
e.printStackTrace();
}*/
return returnedValue;
}
}
Markdown is supported
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