Exemplo n.º 1
0
        public List <UnificationResult> GetUnifier(TrsTermBase ruleHead, TrsTermBase matchTerm)
        {
            List <UnificationResult> unifiers = new List <UnificationResult>();

            for (int i = 0; i < 1; i++)
            {
                var tempResult = new UnificationResult();
                tempResult.Unifier = new List <Substitution>();
                foreach (var variable in ruleHead.GetVariables())
                {
                    tempResult.Unifier.Add(new Substitution {
                        SubstitutionTerm = new TrsNumber(i.ToString()), Variable = variable
                    });
                }
                tempResult.Succeed = true;
                unifiers.Add(tempResult);
            }
            return(unifiers);
        }
Exemplo n.º 2
0
        /// <summary>
        /// NB: this is not a general solution, only for c = b + x where + can be -, / or * and, b and x can be swapped arround.
        /// c is the match term. The rest is the head term.
        /// </summary>
        public List <UnificationResult> GetUnifier(TrsTermBase termHead, TrsTermBase matchTerm)
        {
            UnificationResult result = new UnificationResult();

            result.Succeed = false;

            // Check input
            Substitution sRhs = new Substitution
            {
                Variable         = new TrsVariable("exp_r"),
                SubstitutionTerm = termHead
            };
            Substitution sLhs = new Substitution
            {
                Variable         = new TrsVariable("exp_l"),
                SubstitutionTerm = matchTerm
            };
            var headTerm = termHead as TrsTerm;

            if (!interpreter.TypeChecker.IsSubstitutionValid(sLhs) ||
                !interpreter.TypeChecker.IsSubstitutionValid(sRhs))
            {
                return(new List <UnificationResult> {
                    result
                });
            }

            // Load problem
            interpreter.ClearExecutionCache();
            interpreter.LoadTerms(new []
            {
                new TrsTerm("rhs", new [] { termHead }),
                new TrsTerm("lhs", new [] { matchTerm })
            });

            // Solve
            while (interpreter.ExecuteRewriteStep())
            {
            }
            ;

            // Extract answer
            var runResults = interpreter.GetCurrentRewriteResult();

            foreach (var stm in runResults.ProgramOut.Statements)
            {
                var resEq = stm as TrsTerm;
                if (resEq != null &&
                    resEq.Name == "eq" &&
                    resEq.Arguments.Count == 2 &&
                    resEq.Arguments[0] is TrsNumber &&
                    resEq.Arguments[1] is TrsVariable)
                {
                    result.Succeed = true;
                    result.Unifier = new List <Substitution>();
                    result.Unifier.Add(new Substitution()
                    {
                        Variable         = resEq.Arguments[1] as TrsVariable,
                        SubstitutionTerm = resEq.Arguments[0]
                    });
                }
            }
            return(new List <UnificationResult> {
                result
            });
        }
Exemplo n.º 3
0
 static void Main(string[] args)
 {
     System.Console.WriteLine(UnificationResult.Unify(Term.NewAtom("42"), Term.NewAtom("42")));
 }