Search code examples
prolog

Prolog: Assign value to Variable in Predicate


With the following rules:

test('John', ebola).
test('John', covid).
test('Maria', covid).

How can I create a predicate that would tell me if John or Maria took (both) the Ebola and Covid tests?

I want to do something similar to this (I know it's wrong, just the idea):

tests(Persona, Ebola, Covid) :-
    Ebola = test(Persona, ebola),
    Covid = test(Persona, covid).

Solution

  • Prolog is relational not functional. test(X, Y) either holds or fails and doesn't return a value like what you thought. Here's what you should have written:

    tests(Persona) :-
        test(Persona, ebola),
        test(Persona, covid).
    

    You can query tests('John') which is true since both test/2 calls succeeds. The query tests('Maria') fails because test('Maria', ebola) fails.

    Does it answer your question ?