Prove() private méthode

Proves the goal in the specified structure.
private Prove ( Structure goal ) : IEnumerable
goal Structure
Résultat IEnumerable
Exemple #1
0
 IEnumerable <CutState> Prover(Structure goal)
 {
     if (goal == null)
     {
         return(CutStateSequencer.Succeed());
     }
     return(context.Prove(goal));
 }
Exemple #2
0
        private static IEnumerable<CutState> IfThenImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 2) throw new ArgumentCountException("->", args, "if_condition", "then_result");

#pragma warning disable 414, 168, 219
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(args[0], "Arguments to -> must be valid subgoals."))
                // ReSharper restore UnusedVariable
            {
                // ReSharper disable UnusedVariable
                foreach (var ignore2 in context.Prove(args[1], "Arguments to -> must be valid subgoals."))
                // ReSharper restore UnusedVariable
#pragma warning restore 414, 168, 219
                {
                    yield return CutState.Continue;
                }
                yield break;
            }
        }
Exemple #3
0
        private static IEnumerable<CutState> IfThenElseImplementation(object test, object consequent, object alternative, PrologContext context)
        {
#pragma warning disable 414, 168, 219
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(test, "Arguments to -> must be valid subgoals."))
                // ReSharper restore UnusedVariable
            {
                // ReSharper disable UnusedVariable
                foreach (var ignore2 in context.Prove(consequent, "Arguments to -> must be valid subgoals."))
                    // ReSharper restore UnusedVariable
                {
                    yield return CutState.Continue;
                }
                yield break;
            }
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(alternative, "Arguments to -> must be valid subgoals."))
                // ReSharper restore UnusedVariable
            {
                yield return CutState.Continue;
            }
#pragma warning restore 414, 168, 219
        }
Exemple #4
0
 private static IEnumerable<CutState> RealOrImplementation(object[] args, PrologContext context)
 {
     foreach (var status1 in context.Prove(args[0], "Arguments to ; (or) must be valid subgoals."))
     {
         if (status1 == CutState.ForceFail)
         {
             //yield return status1;
             yield break;
         }
         yield return CutState.Continue;
     }
     foreach (var status2 in context.Prove(args[1], "Arguments to ; (or) must be valid subgoals."))
     {
         if (status2 == CutState.ForceFail)
         {
             //yield return status2;
             yield break;
         }
         yield return CutState.Continue;
     }
 }
Exemple #5
0
 private static IEnumerable<CutState> AndImplementation(object[] args, PrologContext context)
 {
     if (args.Length != 2) throw new ArgumentCountException(", (and)", args, "goal1", "goal2");
     foreach (var status1 in context.Prove(args[0], "Arguments to , (and) must be valid subgoals."))
     {
         if (status1==CutState.ForceFail)
         {
             yield return status1;
             yield break;
         }
         foreach (var status2 in context.Prove(args[1], "Arguments to , (and) must be a valid subgoals."))
         {
             if (status2 == CutState.ForceFail)
             {
                 yield return status2;
                 yield break;
             }
             yield return CutState.Continue;
         }
     }
 }
Exemple #6
0
 private static IEnumerable<CutState> FreezeImplementation(object[] args, PrologContext context)
 {
     if (args.Length != 2) throw new ArgumentCountException("freeze", args, "variable", "goal");
     Structure goal = Term.Structurify(args[1], "Goal argument to freeze must be a valid Prolog goal.");
     var v = args[0] as LogicVariable;
     if (v == null) throw new ArgumentTypeException("freeze", "variable", args[0], typeof (LogicVariable));
     object canon = Term.Deref(v);
     var canonv = canon as LogicVariable;
     if (canonv == null)
         // Variable is already instantiated - run the goal
         return context.Prove(goal);
     // Variable is uninstantiated; tag it with a suspension of goal.
     return canonv.MetaUnify(new Suspension(null, goal, context));
 }
Exemple #7
0
        private static IEnumerable<CutState> ForAllImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 2) throw new ArgumentCountException("forall", args, ":generator", ":goal");
#pragma warning disable 414, 168, 219
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(args[0], "goal argument to findall must be a valid Prolog goal."))
            {
                var gotOne = false;
                foreach (var ignore2 in context.Prove(args[1], "Arguments for forall/2 must be valid goals."))
                    // ReSharper restore UnusedVariable
#pragma warning restore 414, 168, 219
                    gotOne = true;
                if (!gotOne)
                    yield break;
            }
            yield return CutState.Continue;
        }
Exemple #8
0
        private static IEnumerable<CutState> OnceImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 1) throw new ArgumentCountException("once", args, "goal");
#pragma warning disable 414, 168, 219
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(args[0], "Argument to once/1 must be a valid subgoal."))
#pragma warning restore 414, 168, 219
            {
                // ReSharper restore UnusedVariable
                yield return CutState.Continue;
                yield break;
            }
        }
Exemple #9
0
        private static IEnumerable<CutState> CallWithStepLimitImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 2) throw new ArgumentCountException("call_with_step_limit", args, "max_steps", ":goal");
            object arg = Term.Deref(args[0]);
            var v = arg as LogicVariable;
            if (v != null)
                throw new InstantiationException(v, "first argument to call_with_step_limit/2 must be instantiated to an integer.");

            int newStepLimit = Convert.ToInt32(arg);
            int previousLimit = context.StepLimit;
            int previousRemaining = context.StepsRemaining;
            context.StepLimit = newStepLimit;
            context.StepsRemaining = newStepLimit;
#pragma warning disable 0168
                // ReSharper disable once UnusedVariable
                foreach (var ignore in context.Prove(args[1], "Argument to step_limit/2 must be a valid goal"))
#pragma warning restore 0168
                {
                    yield return CutState.Continue;
                }
            context.StepLimit = previousLimit;
            context.StepsRemaining = previousRemaining;
        }
Exemple #10
0
 private static IEnumerable<CutState> CheckImplementation(object[] args, PrologContext context)
 {
     if (args.Length != 1)
         throw new ArgumentCountException("check", args, "goal");
     using (var e = context.Prove(args[0], "Goal to check is not a valid Prolog goal.").GetEnumerator())
     {
         if (e.MoveNext())
             yield return CutState.Continue;
         else
             throw new InvalidOperationException("Check failed: " + ISOPrologWriter.WriteToString(args[0]));
     }
 }
Exemple #11
0
        private static IEnumerable<CutState> ArgMinImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 3) throw new ArgumentCountException("arg_min", args, "template", "score", "goal");
            object template = Term.Deref(args[0]);
            object score = Term.Deref(args[1]);
            float bestScore = 0;
            object bestObj = null;
            bool gotOne = false;
#pragma warning disable 414, 168, 219
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(args[2], "Goal argument to arg_min must be a valid Prolog goal."))
            // ReSharper restore UnusedVariable
#pragma warning restore 414, 168, 219
            {
                float newScore = Convert.ToSingle(Term.Deref(score));
                if (!gotOne || newScore < bestScore)
                {
                    gotOne = true;
                    bestScore = newScore;
                    bestObj = Term.CopyInstantiation(template);
                }
            }
            if (gotOne)
            {
#pragma warning disable 414, 168, 219
                // ReSharper disable UnusedVariable
                foreach (var ignore in Term.Unify(score, bestScore))
                    foreach (var ignore2 in Term.Unify(template, bestObj))
                        // ReSharper restore UnusedVariable
#pragma warning restore 414, 168, 219
                        yield return CutState.Continue;
            }
        }
Exemple #12
0
        private static IEnumerable<CutState> SumallImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 3) throw new ArgumentCountException("sumall", args, "numberVar", "generator", "sum");
            var numberVar = Term.Deref(args[0]) as LogicVariable;
            if (numberVar == null)
                throw new ArgumentTypeException("sumall", "numberVar", args[0], typeof(LogicVariable));
            //object bag = null;
            double sum = 0;
#pragma warning disable 414, 168, 219
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(args[1], "goal argument to findall must be a valid Prolog goal."))
            // ReSharper restore UnusedVariable
#pragma warning restore 414, 168, 219
            {
                sum += Convert.ToDouble(numberVar.Value);
            }
            return Term.UnifyAndReturnCutState(args[2], sum);
        }
Exemple #13
0
        private static List<object> SolutionList(PrologContext context, object template, object goal, int maxSolutions, bool deleteDuplicates)
        {
            var bag = new List<object>();
#pragma warning disable 414, 168, 219
            // ReSharper disable UnusedVariable
            foreach (var ignore in context.Prove(goal, "goal argument to findall must be a valid Prolog goal."))
                // ReSharper restore UnusedVariable
#pragma warning restore 414, 168, 219
            {
                object instance = Term.CopyInstantiation(template);
                bag.Add(instance);
                if (--maxSolutions <= 0)
                    break;
            }
            if (deleteDuplicates)
                Term.Sort(bag, true);
            return bag;
        }
Exemple #14
0
        private static IEnumerable<CutState> ForAllUniqueImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 3)
                throw new ArgumentCountException("for_all_unique", args, "-Template", ":generator", ":goal");
            var template = Term.Deref(args[0]);
            foreach (var templateValue in SolutionList(context, template, Term.Deref(args[1]), int.MaxValue, true))
#pragma warning disable 414, 168, 219
                // ReSharper disable UnusedVariable
                foreach (var ignore in Term.Unify(template, templateValue))
                {
                    var gotOne = false;
                    foreach (var ignore2 in context.Prove(args[2], "Arguments for forall/2 must be valid goals."))
                        // ReSharper restore UnusedVariable
#pragma warning restore 414, 168, 219
                        gotOne = true;
                    if (!gotOne)
                        yield break;
                }
            yield return CutState.Continue;
        }
Exemple #15
0
 private static IEnumerable<CutState> NotImplementation(object[] args, PrologContext context)
 {
     if (args.Length != 1) throw new ArgumentCountException("not", args, "goal");
     LogicVariable v = Term.FindUninstantiatedVariable(args[0]);
     if (v != null)
         throw new InstantiationException(v, "Argument to not must be a ground literal (i.e. contain no unbound variables).");
     using (var e = context.Prove(args[0], "Argument to not must be a valid term to prove.").GetEnumerator())
     {
         if (!e.MoveNext() || e.Current == CutState.ForceFail)
             yield return CutState.Continue;
     }
 }
Exemple #16
0
 private static IEnumerable<CutState> NotPlusImplementation(object[] args, PrologContext context)
 {
     if (args.Length != 1) throw new ArgumentCountException("\\+", args, "goal");
     using (var e = context.Prove(args[0], "Argument to \\+ must be a valid term to prove.").GetEnumerator())
     {
         if (!e.MoveNext() || e.Current == CutState.ForceFail)
             yield return CutState.Continue;
     }
 }
Exemple #17
0
 private static IEnumerable<CutState> BeginImplementation(object[] args, PrologContext context)
 {
     var enumerators = new IEnumerator<CutState>[args.Length];
     int i=0;
     try
     {
         for (; i<args.Length; i++)
         {
             var goal = args[i];
             var goalStructure = Term.Structurify(goal, "Argument to begin is not a valid goal.");
             if (goalStructure.IsFunctor(Symbol.Dot, 2))
                 FunctionalExpression.Eval(goalStructure, context);
             else
             {
                 var enumerator = context.Prove(goalStructure).GetEnumerator();
                 enumerators[i] = enumerator;
                 if (!enumerator.MoveNext() || enumerator.Current == CutState.ForceFail)
                     throw new GoalException(goal, "Goal failed");
             }
         }
         yield return CutState.Continue;
     }
     finally
     {
         while ((--i)>=0)
             enumerators[i].Dispose();
     }
 }
Exemple #18
0
 private static IEnumerable<CutState> IgnoreImplementation(object[] args, PrologContext context)
 {
     if (args.Length != 1) throw new ArgumentCountException("ignore", args, "goal");
     using (var e = context.Prove(args[0], "Argument to ignore/n must be a valid subgoal.").GetEnumerator())
     {
         e.MoveNext();  // Ignore whether it succeeded.
         yield return CutState.Continue;
     }
 }
Exemple #19
0
        private static IEnumerable<CutState> CallImplementation(object[] args, PrologContext context)
        {
            switch (args.Length)
            {
                case 0:
                    throw new ArgumentCountException("call", args, "goal", "optionalAdditionalArguments", "...");

                case 1:
                    return IgnoreCuts(context.Prove(args[0], "Argument to call must be a valid subgoal."));

                default:
                    // More than 1 argument - add other arguments to the end of the predicate
                    {
                        object goal = Term.Deref(args[0]);
                        var t = goal as Structure;

                        if (t != null)
                        {
                            var goalArgs = new object[t.Arguments.Length + args.Length - 1];
                            t.Arguments.CopyTo(goalArgs, 0);
                            Array.Copy(args, 1, goalArgs, t.Arguments.Length, args.Length - 1);

                            return IgnoreCuts(context.KnowledgeBase.Prove(t.Functor, goalArgs, context, context.CurrentFrame));
                        }
                        var s = goal as Symbol;
                        if (s != null)
                        {
                            var goalArgs = new object[args.Length - 1];
                            Array.Copy(args, 1, goalArgs, 0, args.Length - 1);

                            return IgnoreCuts(context.KnowledgeBase.Prove(s, goalArgs, context, context.CurrentFrame));
                        }
                        throw new ArgumentException("Argument to call must be a valid subgoal.");
                    }
            }
        }