Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Defining a predicate as substitute for =/2 in Prolog

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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).
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading