예제 #1
0
        public static bool Nonvar(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);
            var wamReferenceTarget = arguments[0].Dereference();

            return(!(wamReferenceTarget is WamVariable));
        }
예제 #2
0
        public static bool ListOrPartialList(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);
            var wamReferenceTarget = arguments[0].Dereference();

            return(IsListOrPartialList(wamReferenceTarget));
        }
예제 #3
0
        public ExecutionResults StepOver()
        {
            var results = WamMachine.StepOver();

            ProcessResults(results);
            return(results);
        }
예제 #4
0
        public static bool Number(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);
            var wamReferenceTarget = arguments[0].Dereference();

            return(wamReferenceTarget is WamValueInteger || wamReferenceTarget is WamValueDouble);
        }
예제 #5
0
        public static void Eval(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var expression = arguments[0].Dereference();

            var compoundTerm = expression as WamCompoundTerm;

            if (compoundTerm == null)
            {
                return;
            }

            var builder = new WamInstructionStreamBuilder();

            builder.Write(new WamInstruction(WamInstructionOpCodes.Allocate));
            for (var idx = 0; idx < compoundTerm.Functor.Arity; ++idx)
            {
                builder.Write(new WamInstruction(
                                  WamInstructionOpCodes.PutValue,
                                  compoundTerm.Children[idx],
                                  new WamInstructionRegister(WamInstructionRegisterTypes.Argument, (byte)idx)));
            }
            builder.Write(new WamInstruction(WamInstructionOpCodes.Call, compoundTerm.Functor));
            builder.Write(new WamInstruction(WamInstructionOpCodes.Deallocate));
            builder.Write(new WamInstruction(WamInstructionOpCodes.Proceed));

            machine.SetInstructionStream(builder.ToInstructionStream());
        }
예제 #6
0
        public ExecutionResults RunToSuccess()
        {
            var results = WamMachine.RunToSuccess();

            ProcessResults(results);
            return(results);
        }
예제 #7
0
        public static IEnumerable <bool> For(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 3);

            var wamReferenceTargetFrom = arguments[1].Dereference();
            var wamValueIntegerFrom    = wamReferenceTargetFrom as WamValueInteger;

            if (wamValueIntegerFrom == null)
            {
                yield break;
            }

            var wamReferenceTargetTo = arguments[2].Dereference();
            var wamValueIntegerTo    = wamReferenceTargetTo as WamValueInteger;

            if (wamValueIntegerTo == null)
            {
                yield break;
            }

            for (var index = wamValueIntegerFrom.Value; index <= wamValueIntegerTo.Value; ++index)
            {
                var wamValueIntegerResult = WamValueInteger.Create(index);
                if (machine.Unify(arguments[0], wamValueIntegerResult))
                {
                    yield return(true);
                }
                else
                {
                    yield break;
                }
            }
        }
 public static bool Callable(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 1);
     var wamReferenceTarget = arguments[0].Dereference();
     var wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;
     return wamCompoundTerm != null;
 }
        public static IEnumerable<bool> For(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 3);

            var wamReferenceTargetFrom = arguments[1].Dereference();
            var wamValueIntegerFrom = wamReferenceTargetFrom as WamValueInteger;
            if (wamValueIntegerFrom == null)
            {
                yield break;
            }

            var wamReferenceTargetTo = arguments[2].Dereference();
            var wamValueIntegerTo = wamReferenceTargetTo as WamValueInteger;
            if (wamValueIntegerTo == null)
            {
                yield break;
            }

            for (var index = wamValueIntegerFrom.Value; index <= wamValueIntegerTo.Value; ++index)
            {
                var wamValueIntegerResult = WamValueInteger.Create(index);
                if (machine.Unify(arguments[0], wamValueIntegerResult))
                {
                    yield return true;
                }
                else
                {
                    yield break;
                }
            }
        }
        public static void Eval(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget expression = arguments[0].Dereference();

            WamCompoundTerm compoundTerm = expression as WamCompoundTerm;
            if (compoundTerm == null)
            {
                return;
            }

            WamInstructionStreamBuilder builder = new WamInstructionStreamBuilder();
            builder.Write(new WamInstruction(WamInstructionOpCodes.Allocate));
            for (int idx = 0; idx < compoundTerm.Functor.Arity; ++idx)
            {
                builder.Write(new WamInstruction(
                    WamInstructionOpCodes.PutValue,
                    compoundTerm.Children[idx],
                    new WamInstructionRegister(WamInstructionRegisterTypes.Argument, (byte)idx)));
            }
            builder.Write(new WamInstruction(WamInstructionOpCodes.Call, compoundTerm.Functor));
            builder.Write(new WamInstruction(WamInstructionOpCodes.Deallocate));
            builder.Write(new WamInstruction(WamInstructionOpCodes.Proceed));

            machine.SetInstructionStream(builder.ToInstructionStream());
        }
 public static bool Is(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 2);
     var lhs = arguments[0];
     var rhs = machine.Evaluate(arguments[1]);
     return machine.Unify(lhs, rhs);
 }
예제 #12
0
        public ExecutionResults StepOut()
        {
            ExecutionResults results = WamMachine.StepOut();

            ProcessResults(results);

            return(results);
        }
예제 #13
0
        public static bool Is(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 2);
            var lhs = arguments[0];
            var rhs = machine.Evaluate(arguments[1]);

            return(machine.Unify(lhs, rhs));
        }
예제 #14
0
        public static bool Float(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget wamReferenceTarget = arguments[0].Dereference();

            return(wamReferenceTarget is WamValueDouble);
        }
예제 #15
0
        public static bool Callable(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);
            var wamReferenceTarget = arguments[0].Dereference();
            var wamCompoundTerm    = wamReferenceTarget as WamCompoundTerm;

            return(wamCompoundTerm != null);
        }
        public static bool Nonvar(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget wamReferenceTarget = arguments[0].Dereference();

            return !(wamReferenceTarget is WamVariable);
        }
예제 #17
0
        public ExecutionResults RunToBacktrack()
        {
            ExecutionResults results = WamMachine.RunToBacktrack();

            ProcessResults(results);

            return(results);
        }
        public static bool Float(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget wamReferenceTarget = arguments[0].Dereference();

            return wamReferenceTarget is WamValueDouble;
        }
예제 #19
0
        public void Restart()
        {
            WamMachine.Initialize();

            Synchronize();

            QueryResults = null;
        }
예제 #20
0
        public static bool NextDouble(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var operand = arguments[0];
            var value   = WamValueDouble.Create(s_random.NextDouble());

            return(machine.Unify(operand, value));
        }
예제 #21
0
        public static bool Randomize(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 0);

            s_seed = -1;
            s_random = new Random();

            return true;
        }
예제 #22
0
        public static bool GetSeed(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var operand = arguments[0];
            var seed = WamValueInteger.Create(s_seed);

            return machine.Unify(operand, seed);
        }
예제 #23
0
        public static bool NextDouble(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var operand = arguments[0];
            var value = WamValueDouble.Create(s_random.NextDouble());

            return machine.Unify(operand, value);
        }
예제 #24
0
        public static bool GetSeed(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var operand = arguments[0];
            var seed    = WamValueInteger.Create(s_seed);

            return(machine.Unify(operand, seed));
        }
예제 #25
0
        public static bool Randomize(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 0);

            s_seed   = -1;
            s_random = new Random();

            return(true);
        }
 public static bool Atomic(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 1);
     var wamReferenceTarget = arguments[0].Dereference();
     var wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;
     if (wamCompoundTerm != null)
     {
         return wamCompoundTerm.Functor.Arity == 0;
     }
     return wamReferenceTarget is WamValueInteger || wamReferenceTarget is WamValueDouble;
 }
 public static bool Compound(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 1);
     var wamReferenceTarget = arguments[0].Dereference();
     var wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;
     if (wamCompoundTerm != null)
     {
         return wamCompoundTerm.Functor.Arity > 0;
     }
     return false;
 }
예제 #28
0
        public static bool Atomic(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);
            var wamReferenceTarget = arguments[0].Dereference();
            var wamCompoundTerm    = wamReferenceTarget as WamCompoundTerm;

            if (wamCompoundTerm != null)
            {
                return(wamCompoundTerm.Functor.Arity == 0);
            }
            return(wamReferenceTarget is WamValueInteger || wamReferenceTarget is WamValueDouble);
        }
예제 #29
0
        public static bool Compound(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);
            var wamReferenceTarget = arguments[0].Dereference();
            var wamCompoundTerm    = wamReferenceTarget as WamCompoundTerm;

            if (wamCompoundTerm != null)
            {
                return(wamCompoundTerm.Functor.Arity > 0);
            }
            return(false);
        }
예제 #30
0
 public static WamMachine Create(Program program, Query query)
 {
     if (program == null)
     {
         throw new ArgumentNullException("program");
     }
     if (query == null)
     {
         throw new ArgumentNullException("query");
     }
     var wamMachine = new WamMachine(program, query);
     wamMachine.Initialize();
     return wamMachine;
 }
예제 #31
0
        public static bool SetSeed(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamValueInteger operand = arguments[0].Dereference() as WamValueInteger;
            if (operand == null)
            {
                return false;
            }

            s_seed = operand.Value;
            s_random = new Random(s_seed);

            return true;
        }
예제 #32
0
        public static bool Callable(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget wamReferenceTarget = arguments[0].Dereference();

            WamCompoundTerm wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;

            if (wamCompoundTerm != null)
            {
                return(true);
            }

            return(false);
        }
예제 #33
0
        public static WamMachine Create(Program program, Query query)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            var wamMachine = new WamMachine(program, query);

            wamMachine.Initialize();
            return(wamMachine);
        }
예제 #34
0
        public static bool SetSeed(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var operand = arguments[0].Dereference() as WamValueInteger;

            if (operand == null)
            {
                return(false);
            }

            s_seed   = operand.Value;
            s_random = new Random(s_seed);

            return(true);
        }
        public static bool Assert(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var expression = machine.Evaluate(arguments[0]);
            var codeTerm = expression.GetCodeTerm();
            if (codeTerm != null)
            {
                var codeValue = codeTerm as CodeValue;
                if (codeValue != null)
                {
                    return Convert.ToBoolean(codeValue.Object);
                }
            }

            return false;
        }
예제 #36
0
        public static bool Assert(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            var expression = machine.Evaluate(arguments[0]);
            var codeTerm   = expression.GetCodeTerm();

            if (codeTerm != null)
            {
                var codeValue = codeTerm as CodeValue;
                if (codeValue != null)
                {
                    return(Convert.ToBoolean(codeValue.Object));
                }
            }

            return(false);
        }
예제 #37
0
        private PrologMachine(Program program, Query query)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            m_wamMachine = WamMachine.Create(program, query);

            StackFrames        = new PrologStackFrameList(this);
            Arguments          = new PrologVariableList(this);
            TemporaryVariables = new PrologVariableList(this);

            Synchronize();

            QueryResults = null;
        }
예제 #38
0
        private PrologMachine(Program program, Query query)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            m_wamMachine = WamMachine.Create(program, query);

            m_stackFrames = new PrologStackFrameList(this);
            m_arguments = new PrologVariableList(this);
            m_temporaryVariables = new PrologVariableList(this);

            Synchronize();

            m_queryResults = null;
        }
예제 #39
0
        public static bool Next(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 3);

            var minValue = arguments[0].Dereference() as WamValueInteger;
            if (minValue == null)
            {
                return false;
            }

            var maxValue = arguments[1].Dereference() as WamValueInteger;
            if (maxValue == null)
            {
                return false;
            }

            var operand = arguments[2];
            var value = WamValueInteger.Create(s_random.Next(minValue.Value, maxValue.Value));

            return machine.Unify(operand, value);
        }
예제 #40
0
        public static bool Next(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 3);

            var minValue = arguments[0].Dereference() as WamValueInteger;

            if (minValue == null)
            {
                return(false);
            }

            var maxValue = arguments[1].Dereference() as WamValueInteger;

            if (maxValue == null)
            {
                return(false);
            }

            var operand = arguments[2];
            var value   = WamValueInteger.Create(s_random.Next(minValue.Value, maxValue.Value));

            return(machine.Unify(operand, value));
        }
예제 #41
0
 public static bool Fail(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 0);
     return(false);
 }
        public static bool Callable(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget wamReferenceTarget = arguments[0].Dereference();

            WamCompoundTerm wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;
            if (wamCompoundTerm != null)
            {
                return true;
            }

            return false;
        }
예제 #43
0
 public static bool CannotUnify(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 2);
     return(machine.CannotUnify(arguments[0], arguments[1]));
 }
        public static bool CannotUnify(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 2);

            return machine.CannotUnify(arguments[0], arguments[1]);
        }
예제 #45
0
        public static bool FindAll(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 3);

            var arg0 = arguments[0].Dereference();
            var arg1 = arguments[1].Dereference();
            var arg2 = arguments[2].Dereference();

            var variable = arg0 as WamVariable;
            if (variable == null)
            {
                return false;
            }

            var goal = arg1 as WamCompoundTerm;
            if (goal == null)
            {
                return false;
            }

            var result = arg2 as WamVariable;
            if (result == null)
            {
                return false;
            }

            var builder = new WamInstructionStreamBuilder();
            builder.Write(new WamInstruction(WamInstructionOpCodes.Allocate));
            for (var idx = 0; idx < goal.Functor.Arity; ++idx)
            {
                builder.Write(new WamInstruction(
                    WamInstructionOpCodes.PutValue,
                    goal.Children[idx],
                    new WamInstructionRegister(WamInstructionRegisterTypes.Argument, (byte)idx)));
            }
            builder.Write(new WamInstruction(WamInstructionOpCodes.Call, goal.Functor));
            builder.Write(new WamInstruction(WamInstructionOpCodes.Success));

            machine.PushContext(builder.ToInstructionStream());

            var values = new List<WamReferenceTarget>();

            try
            {
                var results = machine.RunToSuccess();
                while (results == ExecutionResults.Success)
                {
                    var value = variable.Clone();
                    values.Add(value);

                    results = machine.RunToSuccess();
                }
            }
            finally
            {
                machine.PopContext(true);
            }

            // Unbind the variable from the last results found by the goal.
            //
            variable.Unbind();

            // Unify the output variable with the list of values.
            //
            return machine.Unify(result, WamReferenceTarget.Create(values));
        }
 public static bool Fail(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 0);
     return false;
 }
예제 #47
0
        public static bool FindAll(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 3);

            WamReferenceTarget arg0 = arguments[0].Dereference();
            WamReferenceTarget arg1 = arguments[1].Dereference();
            WamReferenceTarget arg2 = arguments[2].Dereference();

            WamVariable variable = arg0 as WamVariable;

            if (variable == null)
            {
                return(false);
            }

            WamCompoundTerm goal = arg1 as WamCompoundTerm;

            if (goal == null)
            {
                return(false);
            }

            WamVariable result = arg2 as WamVariable;

            if (result == null)
            {
                return(false);
            }

            WamInstructionStreamBuilder builder = new WamInstructionStreamBuilder();

            builder.Write(new WamInstruction(WamInstructionOpCodes.Allocate));
            for (int idx = 0; idx < goal.Functor.Arity; ++idx)
            {
                builder.Write(new WamInstruction(
                                  WamInstructionOpCodes.PutValue,
                                  goal.Children[idx],
                                  new WamInstructionRegister(WamInstructionRegisterTypes.Argument, (byte)idx)));
            }
            builder.Write(new WamInstruction(WamInstructionOpCodes.Call, goal.Functor));
            builder.Write(new WamInstruction(WamInstructionOpCodes.Success));

            machine.PushContext(builder.ToInstructionStream());

            List <WamReferenceTarget> values = new List <WamReferenceTarget>();

            try
            {
                ExecutionResults results = machine.RunToSuccess();
                while (results == ExecutionResults.Success)
                {
                    WamReferenceTarget value = variable.Clone();
                    values.Add(value);

                    results = machine.RunToSuccess();
                }
            }
            finally
            {
                machine.PopContext(true);
            }

            // Unbind the variable from the last results found by the goal.
            //
            variable.Unbind();

            // Unify the output variable with the list of values.
            //
            return(machine.Unify(result, WamReferenceTarget.Create(values)));
        }
 public static bool Number(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 1);
     var wamReferenceTarget = arguments[0].Dereference();
     return wamReferenceTarget is WamValueInteger || wamReferenceTarget is WamValueDouble;
 }
예제 #49
0
 public static bool CopyTerm(WamMachine machine, WamReferenceTarget[] arguments)
 {
     throw new NotImplementedException();
 }
 public static bool List(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 1);
     var wamReferenceTarget = arguments[0].Dereference();
     return IsList(wamReferenceTarget);
 }
 public static bool CopyTerm(WamMachine machine, WamReferenceTarget[] arguments)
 {
     throw new NotImplementedException();
 }