I am transforming ASTs and need more than simple pattern matching, thus the unification algorithm.
While this is for a .NET project and I know that I could just interoperate with a .NET PROLOG implementation, I only need to embed the unification algorithm; so PROLOG is overkill.
If I could get “Martelli, Montanari: An Efficient Unification Algorithm” written in F# that would be perfect, but I will settle for it any functional language including HASKELL and translate to F#.
In general, it is a good practice to share your attempts when asking questions on SO. People will not generally give you a complete solution for your problems – just answers when you have specific question or hints how to approach the problem.
So, I’ll share some hints about the general approach, but it is not a complete solution.
First you need to represent your AST in some way. In F#, you can do that using discriminated unions. The following supports variables, values and function applications:
Unification is a function that takes list of expressions to be unified of type
(Expr * Expr) listand returns assignments to variables (assigning an expressionExprto a variable namestring):There are a few cases that you’ll need to add. Most importantly, unifying
FunctionwithFunctionmeans that you need to check that the function names are the same (otherwise fail) and then add all argument expressions as new constraints to theremaininglist…