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);
 }
        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;
                }
            }
        }
Example #3
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;
                }
            }
        }
Example #4
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));
        }
        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);
        }
        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);
        }
Example #7
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));
        }
Example #8
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));
        }
        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);
        }
Example #10
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));
        }
        public static bool Unify(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 2);

            return machine.Unify(arguments[0], arguments[1]);
        }
Example #12
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)));
        }
Example #13
0
 public static bool Unify(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 2);
     return(machine.Unify(arguments[0], arguments[1]));
 }
        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));
        }