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

1

7

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

Trackless answered 17/1, 2013 at 17:40 Comment(2)
Seven Languages in Seven Weeks, huh? :) Thanks for asking, the answer helped me out as well!Grous
This online interpreter might be useful to others as well: swish.swi-prolog.orgGrous
O
5

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.
Ophthalmia answered 17/1, 2013 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.