I’m constantly fighting with Prolog. Often I’d like to express functional ideas, together with relational ones:
... :- g(Y,f(X), Result)
... :- Result := g(Y,f(X))
but I typically need to refactor as as this:
... :- f(X, Tmp), g(Y, Tmp, Result),
which I think:
- is detrimental to clarity (because understanding the functional dependence of g on f requires more mental effort),
- leaves me wasting time inventing names for unwanted variables, and
- invites new programmer errors that are eliminated in languages without assignment (we may, for example, use Tmp elsewhere by mistake).
It seems to me that the difference is largely just syntax and the lack of ease in forming non-boolean expressions doesn’t benefit prolog. More general expressions are standard idiom in 100% of languages outside of logic programming, so we can say their worth has been proven.
Are there patterns to employ in prolog to achieve more functional-style readability?
For example, are there macros that can automatically rewrite:
g(Y, λ(f,X), Result)
Result <- g(Y, λ(f,X))
... :- f(X, Tmp0), g(Y, Tmp0, Result)