Defining a predicate as substitute for =/2 in Prolog

Advertisements

For my programming course’s assignment, I have to write some Prolog code without using any pre-defined predicates (excluding , and ;), but saw no way around using =, as I had to check whether a variable A is equal to (can be identified with) some foo(B, C).

Since this isn’t allowed though, I’d like to implement my own predicate myUnification/2, which should essentially behave in the same way, but I have no idea how to go about this. I’ve tried looking at the SWI-Prolog Documentation for assistance but it only explains what the predicate does, not how it actually works internally.

>Solution :

It is right there in the docs 😀

=(Term, Term).

To use ‘unify’ instead of ‘=’, define:

unify(A, A).

You can now do magic like this:

?- unify(X, foo(a, b)).
X = foo(a, b).

Leave a ReplyCancel reply