Skip to content

Commit

Permalink
remove demos
Browse files Browse the repository at this point in the history
  • Loading branch information
G3Kappa committed Mar 4, 2024
1 parent 1b77afd commit caa465e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
21 changes: 9 additions & 12 deletions Ergo/Lang/Compiler/ExecutionGraph.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Ergo.Interpreter;
using Ergo.Runtime.BuiltIns;
using System.Diagnostics;

namespace Ergo.Lang.Compiler;

Expand Down Expand Up @@ -62,12 +61,11 @@ public static class ExecutionGraphExtensions

public static ExecutionGraph ToExecutionGraph(this Predicate clause, DependencyGraph graph, Dictionary<Signature, CyclicalCallNode> cyclicalCallMap = null)
{
var scope = graph.KnowledgeBase.Scope/*.WithCurrentModule(clause.DeclaringModule)*/;
var root = ToExecutionNode(clause.Body, graph, scope, cyclicalCallMap: cyclicalCallMap);
var root = ToExecutionNode(clause.Body, graph, graph.KnowledgeBase.Scope, clause.DeclaringModule, cyclicalCallMap: cyclicalCallMap);
return new(clause.Head, root);
}

public static ExecutionNode ToExecutionNode(this ITerm goal, DependencyGraph graph, Maybe<InterpreterScope> mbScope = default, InstantiationContext ctx = null, Dictionary<Signature, CyclicalCallNode> cyclicalCallMap = null)
public static ExecutionNode ToExecutionNode(this ITerm goal, DependencyGraph graph, Maybe<InterpreterScope> mbScope = default, Maybe<Atom> callerModule = default, InstantiationContext ctx = null, Dictionary<Signature, CyclicalCallNode> cyclicalCallMap = null)
{
ctx ??= CompilerContext;
cyclicalCallMap ??= new();
Expand All @@ -76,7 +74,7 @@ public static ExecutionNode ToExecutionNode(this ITerm goal, DependencyGraph gra
var scope = mbScope.GetOr(graph.KnowledgeBase.Scope);// Handle the cyclical call node if it's present
if (goal is NTuple tup)
{
var list = tup.Contents.Select(x => ToExecutionNode(x, graph, scope, ctx, cyclicalCallMap)).ToList();
var list = tup.Contents.Select(x => ToExecutionNode(x, graph, scope, callerModule, ctx, cyclicalCallMap)).ToList();
if (list.Count == 0)
return TrueNode.Instance;
if (list.Count == 1)
Expand All @@ -95,15 +93,15 @@ public static ExecutionNode ToExecutionNode(this ITerm goal, DependencyGraph gra
{
if (arity == 2 && WellKnown.Operators.If.Synonyms.Contains(functor))
{
return new IfThenNode(ToExecutionNode(args[0], graph, scope, ctx, cyclicalCallMap), ToExecutionNode(args[1], graph, scope, ctx, cyclicalCallMap));
return new IfThenNode(ToExecutionNode(args[0], graph, scope, callerModule, ctx, cyclicalCallMap), ToExecutionNode(args[1], graph, scope, callerModule, ctx, cyclicalCallMap));
}
if (arity == 2 && WellKnown.Operators.Disjunction.Synonyms.Contains(functor))
{
if (args[0] is Complex { Functor: var functor1, Arity: var arity1, Arguments: var args1 } && arity1 == 2 && WellKnown.Operators.If.Synonyms.Contains(functor1))
{
return new IfThenElseNode(ToExecutionNode(args1[0], graph, scope, ctx, cyclicalCallMap), ToExecutionNode(args1[1], graph, scope, ctx, cyclicalCallMap), ToExecutionNode(args[1], graph, scope, ctx, cyclicalCallMap));
return new IfThenElseNode(ToExecutionNode(args1[0], graph, scope, callerModule, ctx, cyclicalCallMap), ToExecutionNode(args1[1], graph, scope, callerModule, ctx, cyclicalCallMap), ToExecutionNode(args[1], graph, scope, callerModule, ctx, cyclicalCallMap));
}
return new BranchNode(ToExecutionNode(args[0], graph, scope, ctx, cyclicalCallMap), ToExecutionNode(args[1], graph, scope, ctx, cyclicalCallMap));
return new BranchNode(ToExecutionNode(args[0], graph, scope, callerModule, ctx, cyclicalCallMap), ToExecutionNode(args[1], graph, scope, callerModule, ctx, cyclicalCallMap));
}
}
// If 'goal' isn't any other type of node, then it's a proper goal and we need to resolve it in the context of 'clause'.
Expand Down Expand Up @@ -169,7 +167,7 @@ void Node(ITerm goal)
foreach (var clause in node.Clauses
.OrderByDescending(x => scope.Modules.TryGetValue(x.DeclaringModule, out var m) ? m.LoadOrder : 0))
{
if (Clause(clause).TryGetValue(out var match))
if (Clause(clause, callerModule.GetOr(scope.Entry)).TryGetValue(out var match))
newMatches.Add(match);
}
matches.AddRange(newMatches);
Expand All @@ -179,11 +177,10 @@ void Node(ITerm goal)
c.Ref.Node = ret;
}
}
Maybe<ExecutionNode> Clause(Predicate clause)
Maybe<ExecutionNode> Clause(Predicate clause, Atom callerModule)
{
var facts = new List<Predicate>();
goal.GetQualification(out var head);
Debug.WriteLine(clause.Head.GetSignature().Explain());
if (scope.Modules.TryGetValue(clause.DeclaringModule, out var module))
{
var sig = clause.Head.Qualified(clause.DeclaringModule).GetSignature();
Expand All @@ -194,7 +191,7 @@ Maybe<ExecutionNode> Clause(Predicate clause)
{
args = args.SetItem(i, meta.Arguments[i] switch
{
'+' when !args[i].GetQualification(out _).HasValue => args[i].Qualified(scope.Entry),
'+' when !args[i].GetQualification(out _).HasValue => args[i].Qualified(callerModule),
_ => args[i],
});
}
Expand Down
4 changes: 2 additions & 2 deletions Ergo/Runtime/Built-Ins/Prologue/Retract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public Retract()
public override ErgoVM.Op Compile()
{
return RetractOp;
void RetractOp(ErgoVM vm)
static void RetractOp(ErgoVM vm)
{
if (Retract(vm, vm.Arg(0), all: false))
{
vm.PushChoice(RetractOp);
vm.Solution();
}
else vm.Fail();
vm.Fail();
}
}
}
2 changes: 1 addition & 1 deletion Ergo/Runtime/Built-Ins/Prologue/RetractAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public RetractAll()
{
vm.Solution();
}
else vm.Fail();
else vm.Success();
};
}
10 changes: 5 additions & 5 deletions Ergo/Runtime/Built-Ins/_Shared/DynamicPredicateBuiltIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ protected static bool Retract(ErgoVM vm, ITerm term, bool all)
return false;
}

if (vm.KB.Scope.Entry != match.Predicate.DeclaringModule)
{
vm.Throw(ErgoVM.ErrorType.CannotRetractImportedPredicate, sig.Explain(), vm.KB.Scope.Entry.Explain(), match.Predicate.DeclaringModule.Explain());
return false;
}
//if (vm.KB.Scope.Entry != match.Predicate.DeclaringModule)
//{
// vm.Throw(ErgoVM.ErrorType.CannotRetractImportedPredicate, sig.Explain(), vm.KB.Scope.Entry.Explain(), match.Predicate.DeclaringModule.Explain());
// return false;
//}

toRemove.Add(match.Predicate.Head);

Expand Down

0 comments on commit caa465e

Please sign in to comment.