public static IEnumerable <bool> For(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 3);

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

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

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

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

            for (int index = wamValueIntegerFrom.Value; index <= wamValueIntegerTo.Value; ++index)
            {
                WamValueInteger wamValueIntegerResult = WamValueInteger.Create(index);
                if (machine.Unify(arguments[0], wamValueIntegerResult))
                {
                    yield return(true);
                }
                else
                {
                    yield break;
                }
            }
        }
        private WamReferenceTarget m_referenceTarget; // 4 bytes

        #endregion

        #region Constructors

        public WamInstruction(WamInstructionOpCodes opCode, WamInstructionRegister sourceRegister, Functor functor, int index, WamInstructionRegister targetRegister)
        {
            if (sourceRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "sourceRegister");
            }
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (index < 0)
            {
                throw new ArgumentException("Invalid index.", "index");
            }
            if (targetRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "targetRegister");
            }

            m_opCode = opCode;
            m_sourceRegister = sourceRegister;
            m_functor = functor;
            m_index = index;
            m_targetRegister = targetRegister;
            m_referenceTarget = null;
        }
        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 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());
        }
Example #5
0
        ExecutionResults OnLibraryCallPredicate(WamInstruction instruction, Predicate predicate)
        {
            var arguments = new WamReferenceTarget[instruction.Functor.Arity];

            for (var index = 0; index < instruction.Functor.Arity; ++index)
            {
                arguments[index] = ArgumentRegisters[index];
            }

            bool result;

            try
            {
                result = predicate.PredicateDelegate(this, arguments);
            }
            catch
            {
                // Backtrack on exception.
                //
                return(ExecutionResults.Backtrack);
            }

            if (result == false)
            {
                return(ExecutionResults.Backtrack);
            }

            InstructionPointer = InstructionPointer.GetNext();

            return(ExecutionResults.None);
        }
 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;
 }
Example #7
0
        ExecutionResults OnLibraryCallBacktrackingPredicate(WamInstruction instruction, BacktrackingPredicate predicate)
        {
            var arguments = new WamReferenceTarget[instruction.Functor.Arity];

            for (var index = 0; index < instruction.Functor.Arity; ++index)
            {
                arguments[index] = ArgumentRegisters[index];
            }

            IEnumerable <bool> enumerable;

            try
            {
                enumerable = predicate.BacktrackingPredicateDelegate(this, arguments);
            }
            catch
            {
                // Backtrack on exception.
                //
                return(ExecutionResults.Backtrack);
            }

            var enumerator = enumerable.GetEnumerator();

            ChoicePoint = new WamChoicePoint(ChoicePoint, Environment, StackIndex, ReturnInstructionPointer, ArgumentRegisters, CutChoicePoint)
            {
                BacktrackInstructionPointer = InstructionPointer.GetNext(),
                PredicateEnumerator         = enumerator
            };
            InstructionPointer  = ChoicePoint.BacktrackInstructionPointer;
            PredicateEnumerator = ChoicePoint.PredicateEnumerator;
            return(ExecutionResults.None);
        }
Example #8
0
        readonly WamReferenceTarget    _referenceTarget; // 4 bytes

        public WamInstruction(WamInstructionOpCodes opCode, WamInstructionRegister sourceRegister, Functor functor, int index, WamInstructionRegister targetRegister)
        {
            if (sourceRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "sourceRegister");
            }
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (index < 0)
            {
                throw new ArgumentException("Invalid index.", "index");
            }
            if (targetRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "targetRegister");
            }
            _opCode          = opCode;
            _sourceRegister  = sourceRegister;
            _functor         = functor;
            _index           = index;
            _targetRegister  = targetRegister;
            _referenceTarget = 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 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 Float(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

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

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

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

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

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

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

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

            return(wamReferenceTarget is WamValueDouble);
        }
Example #15
0
 public void Unbind()
 {
     if (Target == null)
     {
         throw new InvalidOperationException("Attempt to unbind an unbound variable.");
     }
     Target = null;
 }
Example #16
0
 public void Unbind()
 {
     if (Target == null)
     {
         throw new InvalidOperationException("Attempt to unbind an unbound variable.");
     }
     Target = null;
 }
        public static bool ListOrPartialList(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

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

            return(IsListOrPartialList(wamReferenceTarget));
        }
        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 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 #20
0
 public WamInstruction(WamInstructionOpCodes opCode)
 {
     _opCode          = opCode;
     _sourceRegister  = WamInstructionRegister.Unused;
     _functor         = null;
     _index           = -1;
     _targetRegister  = WamInstructionRegister.Unused;
     _referenceTarget = null;
 }
        public static bool Randomize(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 0);

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

            return true;
        }
Example #22
0
 public WamInstruction(WamInstructionOpCodes opCode)
 {
     m_opCode = opCode;
     m_sourceRegister = WamInstructionRegister.Unused;
     m_functor = null;
     m_index = -1;
     m_targetRegister = WamInstructionRegister.Unused;
     m_referenceTarget = null;
 }
        public static bool GetSeed(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget operand = arguments[0];

            WamValueInteger seed = WamValueInteger.Create(s_seed);

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

            WamReferenceTarget operand = arguments[0];

            WamValueDouble value = WamValueDouble.Create(s_random.NextDouble());

            return(machine.Unify(operand, value));
        }
        public static bool Is(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 2);

            WamReferenceTarget lhs = arguments[0];

            WamReferenceTarget rhs = machine.Evaluate(arguments[1]);

            return(machine.Unify(lhs, rhs));
        }
Example #26
0
        public static WamCompoundTerm Create(CodeCompoundTerm codeCompoundTerm)
        {
            var functor = Functor.Create(codeCompoundTerm.Functor);
            var result  = WamCompoundTerm.Create(functor);

            for (var index = 0; index < functor.Arity; ++index)
            {
                result.Children[index] = WamReferenceTarget.Create(codeCompoundTerm.Children[index]);
            }
            return(result);
        }
 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;
 }
 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;
 }
Example #29
0
 public void Bind(WamReferenceTarget target)
 {
     if (target == null)
     {
         throw new ArgumentNullException("target");
     }
     if (Target != null)
     {
         throw new InvalidOperationException("Attempt to bind to bound variable.");
     }
     Target = target;
 }
Example #30
0
 public void Bind(WamReferenceTarget target)
 {
     if (target == null)
     {
         throw new ArgumentNullException("target");
     }
     if (Target != null)
     {
         throw new InvalidOperationException("Attempt to bind to bound variable.");
     }
     Target = target;
 }
Example #31
0
 public WamInstruction(WamInstructionOpCodes opCode, WamInstructionRegister targetRegister)
 {
     if (targetRegister.IsUnused)
     {
         throw new ArgumentException("Invalid value.", "targetRegister");
     }
     _opCode          = opCode;
     _sourceRegister  = WamInstructionRegister.Unused;
     _functor         = null;
     _index           = -1;
     _targetRegister  = targetRegister;
     _referenceTarget = null;
 }
Example #32
0
 public WamInstruction(WamInstructionOpCodes opCode, WamReferenceTarget referenceTarget)
 {
     if (referenceTarget == null)
     {
         throw new ArgumentNullException("referenceTarget");
     }
     _opCode          = opCode;
     _sourceRegister  = WamInstructionRegister.Unused;
     _functor         = null;
     _index           = -1;
     _targetRegister  = WamInstructionRegister.Unused;
     _referenceTarget = referenceTarget;
 }
Example #33
0
 public WamInstruction(WamInstructionOpCodes opCode, Functor functor)
 {
     if (functor == null)
     {
         throw new ArgumentNullException("functor");
     }
     _opCode          = opCode;
     _sourceRegister  = WamInstructionRegister.Unused;
     _functor         = functor;
     _index           = -1;
     _targetRegister  = WamInstructionRegister.Unused;
     _referenceTarget = null;
 }
Example #34
0
        void SetRegister(WamInstructionRegister register, WamReferenceTarget value)
        {
            switch (register.Type)
            {
            case WamInstructionRegisterTypes.Argument: ArgumentRegisters[register.Id] = value; break;

            case WamInstructionRegisterTypes.Permanent: Environment.PermanentRegisters[register.Id] = value; break;

            case WamInstructionRegisterTypes.Temporary: TemporaryRegisters[register.Id] = value; break;

            default:
                throw new InvalidOperationException(string.Format("Unknown register type {0}.", register.Type));
            }
        }
Example #35
0
        static bool IsPartialList(WamReferenceTarget wamReferenceTarget)
        {
            var wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;

            if (wamCompoundTerm != null)
            {
                if (wamCompoundTerm.Functor == Functor.ListFunctor)
                {
                    var tail = wamCompoundTerm.Children[1].Dereference();
                    return(tail is WamVariable || IsPartialList(tail));
                }
            }
            return(false);
        }
        public static bool Compound(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

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

            WamCompoundTerm wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;

            if (wamCompoundTerm != null)
            {
                return(wamCompoundTerm.Functor.Arity > 0);
            }

            return(false);
        }
        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;
        }
Example #38
0
        private PrologVariableList GetPermanentVariables(int stackIndex, bool getCodeTerm)
        {
            PrologVariableList result = new PrologVariableList();

            WamEnvironment environment = WamMachine.GetEnvironment(stackIndex);

            if (environment != null)
            {
                // Retrieve register name assignments from instruction stream.
                //
                Dictionary <int, string> variableNames;
                WamInstructionStream     wamInstructionStream = WamMachine.GetInstructionPointer(stackIndex).InstructionStream;
                if (wamInstructionStream != null)
                {
                    variableNames = wamInstructionStream.GetPermanentVariableAssignments();
                }
                else
                {
                    variableNames = new Dictionary <int, string>();
                }

                for (int index = 0; index < environment.PermanentRegisters.Count; ++index)
                {
                    PrologVariable variable = result.Add(string.Format("Y{0}", index));

                    string name;
                    if (variableNames.TryGetValue(index, out name))
                    {
                        variable.Name = name;
                    }

                    WamReferenceTarget referenceTarget = environment.PermanentRegisters[index];
                    if (referenceTarget != null)
                    {
                        if (getCodeTerm)
                        {
                            variable.CodeTerm = referenceTarget.GetCodeTerm();
                        }
                        else
                        {
                            variable.Text = referenceTarget.ToString();
                        }
                    }
                }
            }

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

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

            WamCompoundTerm wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;

            if (wamCompoundTerm != null)
            {
                return(wamCompoundTerm.Functor.Arity == 0);
            }

            return(wamReferenceTarget is WamValueInteger ||
                   wamReferenceTarget is WamValueDouble);
        }
Example #40
0
 public WamInstruction(WamInstructionOpCodes opCode, WamInstructionRegister sourceRegister, WamReferenceTarget referenceTarget)
 {
     if (referenceTarget == null)
     {
         throw new ArgumentNullException("referenceTarget");
     }
     if (sourceRegister.IsUnused)
     {
         throw new ArgumentException("Invalid value.", "sourceRegister");
     }
     _opCode = opCode;
     _sourceRegister = sourceRegister;
     _functor = null;
     _index = -1;
     _targetRegister = WamInstructionRegister.Unused;
     _referenceTarget = referenceTarget;
 }
        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;
        }
Example #42
0
        static bool IsList(WamReferenceTarget wamReferenceTarget)
        {
            var wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;

            if (wamCompoundTerm != null)
            {
                if (wamCompoundTerm.Functor == Functor.NilFunctor)
                {
                    return(true);
                }
                if (wamCompoundTerm.Functor == Functor.ListFunctor)
                {
                    var tail = wamCompoundTerm.Children[1].Dereference();
                    return(IsList(tail));
                }
            }
            return(false);
        }
Example #43
0
        private PrologVariableList GetArgumentVariables()
        {
            PrologVariableList result = new PrologVariableList();

            for (int index = 0; index < WamMachine.ArgumentRegisters.Count; ++index)
            {
                string             value           = "*";
                WamReferenceTarget referenceTarget = WamMachine.ArgumentRegisters[index];
                if (referenceTarget != null)
                {
                    value = referenceTarget.ToString();
                }

                result.Add(string.Format("A{0}", index)).Text = value;
            }

            return(result);
        }
        public WamInstruction(WamInstructionOpCodes opCode, WamInstructionRegister sourceRegister, WamReferenceTarget referenceTarget)
        {
            if (referenceTarget == null)
            {
                throw new ArgumentNullException("referenceTarget");
            }
            if (sourceRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "sourceRegister");
            }

            m_opCode          = opCode;
            m_sourceRegister  = sourceRegister;
            m_functor         = null;
            m_index           = -1;
            m_targetRegister  = WamInstructionRegister.Unused;
            m_referenceTarget = referenceTarget;
        }
        public WamInstruction(WamInstructionOpCodes opCode, Functor functor, WamInstructionRegister targetRegister)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (targetRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "targetRegister");
            }

            m_opCode          = opCode;
            m_sourceRegister  = WamInstructionRegister.Unused;
            m_functor         = functor;
            m_index           = -1;
            m_targetRegister  = targetRegister;
            m_referenceTarget = null;
        }
        public WamInstruction(WamInstructionOpCodes opCode, Functor functor, int index)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (index < 0)
            {
                throw new ArgumentException("Invalid index.", "index");
            }

            m_opCode          = opCode;
            m_sourceRegister  = WamInstructionRegister.Unused;
            m_functor         = functor;
            m_index           = index;
            m_targetRegister  = WamInstructionRegister.Unused;
            m_referenceTarget = null;
        }
        public static bool Assert(WamMachine machine, WamReferenceTarget[] arguments)
        {
            Debug.Assert(arguments.Length == 1);

            WamReferenceTarget expression = machine.Evaluate(arguments[0]);

            CodeTerm codeTerm = expression.GetCodeTerm();

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

            return(false);
        }
        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);
        }
        private static bool IsListOrPartialList(WamReferenceTarget wamReferenceTarget)
        {
            WamCompoundTerm wamCompoundTerm = wamReferenceTarget as WamCompoundTerm;

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

                if (wamCompoundTerm.Functor == Functor.ListFunctor)
                {
                    WamReferenceTarget tail = wamCompoundTerm.Children[1].Dereference();

                    return(tail is WamVariable ||
                           IsListOrPartialList(tail));
                }
            }

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

            return machine.CannotUnify(arguments[0], arguments[1]);
        }
Example #51
0
 private void Bind(WamVariable variable, WamReferenceTarget target)
 {
     variable.Bind(target);
     Trail(variable);
 }
 public static bool Fail(WamMachine machine, WamReferenceTarget[] arguments)
 {
     Debug.Assert(arguments.Length == 0);
     return false;
 }
Example #53
0
        internal WamReferenceTarget Evaluate(WamReferenceTarget wamReferenceTarget)
        {
            if (wamReferenceTarget == null)
            {
                throw new ArgumentNullException("wamReferenceTarget");
            }

            WamCompoundTerm wamCompoundTerm = wamReferenceTarget.Dereference() as WamCompoundTerm;
            if (wamCompoundTerm == null)
            {
                return wamReferenceTarget;
            }

            if (!Program.Libraries.Contains(wamCompoundTerm.Functor))
            {
                return wamReferenceTarget;
            }

            LibraryMethod method = Program.Libraries[wamCompoundTerm.Functor];
            if (method == null
                || method.CanEvaluate == false)
            {
                return wamReferenceTarget;
            }

            WamReferenceTarget[] arguments = new WamReferenceTarget[method.Functor.Arity];
            for (int index = 0; index < method.Functor.Arity; ++index)
            {
                arguments[index] = Evaluate(wamCompoundTerm.Children[index]);
            }

            Function function = method as Function;
            if (function != null)
            {
                CodeTerm[] codeTerms = new CodeTerm[method.Functor.Arity];
                for (int index = 0; index < method.Functor.Arity; ++index)
                {
                    codeTerms[index] = arguments[index].GetCodeTerm();
                }

                CodeTerm result;
                try
                {
                    result = function.FunctionDelegate(codeTerms);
                    if (result == null)
                    {
                        result = new CodeValueObject(null);
                    }
                }
                catch (Exception ex)
                {
                    result = new CodeValueException(ex);
                }

                return WamValue.Create(result);
            }

            Predicate predicate = method as Predicate;
            if (predicate != null)
            {
                bool result;
                try
                {
                    result = predicate.PredicateDelegate(this, arguments);
                }
                catch
                {
                    result = false;
                }

                return WamValueBoolean.Create(result);
            }

            return wamReferenceTarget;
        }
Example #54
0
 internal bool Unify(WamReferenceTarget lhs, WamReferenceTarget rhs)
 {
     return Unify(lhs, rhs, false);
 }
Example #55
0
        private void SetRegister(WamInstructionRegister register, WamReferenceTarget value)
        {
            switch (register.Type)
            {
                case WamInstructionRegisterTypes.Argument: ArgumentRegisters[register.Id] = value; break;
                case WamInstructionRegisterTypes.Permanent: Environment.PermanentRegisters[register.Id] = value; break;
                case WamInstructionRegisterTypes.Temporary: TemporaryRegisters[register.Id] = value; break;

                default:
                    throw new InvalidOperationException(string.Format("Unknown register type {0}.", register.Type));
            }
        }
Example #56
0
        public WamInstruction(WamInstructionOpCodes opCode, WamInstructionRegister sourceRegister, WamInstructionRegister targetRegister)
        {
            if (sourceRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "sourceRegister");
            }
            if (targetRegister.IsUnused)
            {
                throw new ArgumentException("Invalid value.", "targetRegister");
            }

            m_opCode = opCode;
            m_sourceRegister = sourceRegister;
            m_functor = null;
            m_index = -1;
            m_targetRegister = targetRegister;
            m_referenceTarget = null;
        }
Example #57
0
        public WamInstruction(WamInstructionOpCodes opCode, Functor functor)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }

            m_opCode = opCode;
            m_sourceRegister = WamInstructionRegister.Unused;
            m_functor = functor;
            m_index = -1;
            m_targetRegister = WamInstructionRegister.Unused;
            m_referenceTarget = null;
        }
Example #58
0
 internal bool CannotUnify(WamReferenceTarget lhs, WamReferenceTarget rhs)
 {
     return !Unify(lhs, rhs, true);
 }
Example #59
0
        private bool Unify(WamReferenceTarget lhs, WamReferenceTarget rhs, bool testOnly)
        {
            if (lhs == null)
            {
                throw new ArgumentNullException("lhs");
            }
            if (rhs == null)
            {
                throw new ArgumentNullException("rhs");
            }

            lhs = lhs.Dereference();
            rhs = rhs.Dereference();

            WamVariable lhsVariable = lhs as WamVariable;
            WamVariable rhsVariable = rhs as WamVariable;

            WamCompoundTerm lhsCompoundTerm = lhs as WamCompoundTerm;
            WamCompoundTerm rhsCompoundTerm = rhs as WamCompoundTerm;

            WamValue lhsValue = lhs as WamValue;
            WamValue rhsValue = rhs as WamValue;

            // Ensure that each term is either a compound term or variable.
            //
            Debug.Assert(lhsVariable != null || lhsCompoundTerm != null || lhsValue != null);
            Debug.Assert(rhsVariable != null || rhsCompoundTerm != null || rhsValue != null);

            // Ensure if that we've dereferenced to a variable that it is unbound.
            //
            Debug.Assert(lhsVariable == null || lhsVariable.Target == null);
            Debug.Assert(rhsVariable == null || rhsVariable.Target == null);

            if (lhsVariable != null)
            {
                if (rhsVariable != null)
                {
                    if (lhsVariable.Generation < rhsVariable.Generation)
                    {
                        if (!testOnly) Bind(rhsVariable, lhs);
                    }
                    else
                    {
                        if (!testOnly) Bind(lhsVariable, rhs);
                    }
                }
                else
                {
                    if (!testOnly) Bind(lhsVariable, rhs);
                }
            }
            else
            {
                if (rhsVariable != null)
                {
                    if (!testOnly) Bind(rhsVariable, lhs);
                }
                else
                {
                    if (lhsCompoundTerm != null && rhsCompoundTerm != null)
                    {
                        if (lhsCompoundTerm.Functor != rhsCompoundTerm.Functor)
                        {
                            return false;
                        }

                        for (int index = 0; index < lhsCompoundTerm.Functor.Arity; ++index)
                        {
                            if (!Unify(lhsCompoundTerm.Children[index], rhsCompoundTerm.Children[index], testOnly))
                            {
                                return false;
                            }
                        }
                    }
                    else if (lhsValue != null && rhsValue != null)
                    {
                        if (!lhsValue.Object.Equals(rhsValue.Object))
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return true;
        }
Example #60
0
        public WamInstruction(WamInstructionOpCodes opCode, WamReferenceTarget referenceTarget)
        {
            if (referenceTarget == null)
            {
                throw new ArgumentNullException("referenceTarget");
            }

            m_opCode = opCode;
            m_sourceRegister = WamInstructionRegister.Unused;
            m_functor = null;
            m_index = -1;
            m_targetRegister = WamInstructionRegister.Unused;
            m_referenceTarget = referenceTarget;
        }