Search code examples
prologgnu-prolog

how can I make prolog print query results when running a prolog script


I'm new to prolog and want to save all queries in a file instead of typing them by hand.

I have these facts in facts.pl:

likes(wallace, cheese).
likes(grommit, cheese).
likes(wendolene, sheep).

friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).

After reading the answer of this question, I come up with the following code queries.pl:

main :-
    write(likes(wallace, cheese)),
    halt.

:- initialization(['facts.pl']).
:- initialization(main).

Here I want to examine if likes(wallace, cheese) holds, what I expected is outputing something like yes or no but the actual output is likes(wallace, cheese)

I've googled a lot and attempted

X = likes(wallace, cheese), write(X).

X is likes(wallace, cheese), write(X).

X := likes(wallace, cheese), write(X).

but none of them works.

It might be a really easy question for you, but I have no idea about how to get things right.

BTW, I'm using GNU Prolog 1.4.1


Solution

  • I think you need a way to 'tag' each query: here a simple way

    query(likes(wallace, cheese)).
    query(likes(mickey, whisky)).
    
    % service predicates, check the library and use that if available
    forall(X,Y) :- \+ (X, \+ Y).
    writeln(T) :- write(T), nl.
    
    main :-
        forall(query(Q), (Q -> writeln(yes:Q) ; writeln(no:Q))),
        halt.