How to display all the results in one line in SWI PROLOG? - prolog

I have a small databse in the *.pl file which contains various instances of 3 facts:
male(NAME)
female(NAME)
parents(CHILD_NAME, MOTHER_NAME, FATHER_NAME)
and one question:
brother(A, B) :- male(A), parents(A ,X, Y), parents(B, X, Y), X\==Y.
which tells when X is a brohter of Y. The question is: is here any way to display all the answers in one line while running the question without hitting ';' for every new instance?

I think I would write once an utility like
writeall(Q) :- forall(Q,writeln(Q)).
In SWI-prolog there is an handy place where to store utility snippets like this one. From the IDE, click the menu
Edit\Prolog preferences
and save the snippet there. It get stored in ~/.swiplrc on Linux, MacOS, or into an equivalent configuration file under Windows.

Related

Problems with library(lambda), currying and SWI Prolog

So I run into some troubles while (ab?)using
lambda.pl.
I do a "use_module(library(lambda))."
in the first lines of a file that
I consult via ["a.prolog"].
Then I get an "undefined procedure ()/3"
and some gibberish afterwards.
The same happens for any order of use_modules.
It happens whether I load a.prolog via
[...], consult or as a script from the cmdline.
I reduced the script to the currying-example from Rosseta code
https://rosettacode.org/wiki/Currying#Prolog
use_module(library(lambda)).
:- initialization(main, main).
main :-
N = 5, F = \X^Y^(Y is X+N), maplist(F, [1,2,3], L),
print(L).
It doesn't work.
It works, however, if I a manually load 'lambda'
at the swipl-prompt and immeditately consult
a.prolog. Then the goal N=5,.... works just fine.
If I, however, first consult a.prolog; then manually
use_module and then run the query, I get the error.
Reconsulting doesn't help onwards.
Somehow, the first command at the prompt needs to
be use_module.
Or do I get the loading mechanism completely wrong?
If so, please apologize; but I would love get a
hint how to solve this.
This is a common error when first using modules.
Please have a look at this line:
use_module(library(lambda)).
This is just a fact, saying "use_module(library(lambbda)) holds".
What you want instead is a directive.
A directive is a term with primary functor (:-)/1. That is, you want:
:- use_module(library(lambda)).
EDIT: For the particular case of library(lambda), I would like to add that there is a page with a lot of useful information about it that is a bit hard to find:
http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord
"Hiord" stands for higher order.

Prolog Returning Elements Within Facts to The User

I have a Prolog knowledge base with some "symptom" facts called facts.pl. Here is an example of my KB:
symptom("Typhoid", "muscle_pain").
symptom("Typhoid", "bloating").
symptom("Meningitis", "headache").
symptom("Meningitis", "fever").
symptom("Meningitis", "stiff neck" ).
symptom("Measles", "cough").
symptom("Measles", "runny_nose").
I have written a short prolog program in another file called "diseaseSearch.pl". This program consults facts.pl and is supposed to allow the user to enter a disease name and prints the disease's corresponding symptoms to the screen.
My code:
:- write('loading disease database'), nl.
:- [facts].
:- write('disease database loaded'), nl, nl.
getsymptoms:-
write('> Enter a diseae name followed by a period.'), nl,
write('For Example: Measles'), nl,
write('Disease Name?:'),
read(Input), nl,
symptom(Input,Output),
write(Output).
If I enter "Measles." the output should be "cough" and "runny_nose". However, with the code above no matter which disease I enter it always returns the result from the first fact which is "muscle_pain". SWI output found here
I found a similar method from an online tutorial, I am trying to learn the basics of Prolog input and output right now. Am I on the right track? Some tips to solve this problem would be greatly appreciated!
I guess you are entering Measles without " " and prolog takes it as a variables. Either you should enter it with "". If you enter Measles then it's a variables but if you enter "Measles" then it's a term.
If you want to enter without annotations then you need to make a database in which you have all terms(means that they start with small letter) then you don't need annotation.

Open .pl file and close current one in SWI - Prolog

I have created two different .pl files in SWI-Prolog for a text adventure game. They are two different missions.
Is there any way at the end of the first mission to open the second mission (the second .pl file) and close the first one?
Also, what would be better: To create N .pl files for my N missions or one big .pl file?
I agree with your initial impulse in thinking that using a number of module files would be best. I imagine that one reason for using different files would be to provide different name spaces for facts and rules which would be best expressed using the same predicates. So that, for instance, Description would be different for room(1, Description) in mission 1 than in mission 2.
One way of achieving this would be by accessing private, non-exported predicates in each of the different mission-modules. (Aside: I read Jan Wielemaker caution against this practice somewhere, but I'm not sure why, nor am I sure that I did read this.)
Here's a possible pattern I threw together:
Given a main file, 'game.pl', with the following program,
:- use_module([mission1, mission2]).
start :-
playing(mission1).
playing(CurrentMission) :-
read(Command),
command(CurrentMission, Command),
playing(CurrentMission).
command(_, quit) :- write('Good bye.'), halt.
command(CurrentMission, Command) :-
( current_predicate(CurrentMission:Command/_) % Makes sure Command is defined in the module.
-> CurrentMission:Command % Call Command in the current mission-module
; write('You can\'t do that.'), % In case Command isn't defined in the mission.
).
and these mission modules,
In file 'mission1.pl':
:- module(mission1, []).
turn_left :-
write('You see a left-over turnip').
eat_turnip :-
write('You are transported to mission2'),
playing(mission2). % Return to the prompt in `game` module, but with the next module.
In file 'mission2.pl':
:- module(mission2, []).
turn_left :-
write('You see a left-leaning turncoat.').
Then we can play this shitty game:
?- start.
|: turn_left.
You see a left-over turnip
|: eat_turnip.
You are transported to mission2
|: turn_left.
You see a left-leaning turncoat.
|: quit
|: .
Good bye.
The specifics of this program are problematic for a number of reasons. For instance, I expect we might rather have a single predicate that handles navigating through places, and that we'd rather describe places and object that react to different commands in our missions, rather than account for every possible command. But the general principle of using the different files would still work.
Another approach would be to use consult/1 and unload_file/1 to load and unload modules, in which case you should be able to use their public, exported predicates instead of calling them by module. Documentation for those and related predicates can be found in the manual in the section "Loading Prolog Source Files".

Turbo Prolog's 'save' analogue in SWI-Prolog

Is there any SWI's analogue for Turbo's save function, which saves into a file facts, previously loaded via consult and then appended via assert?
I've not found any save-like functions in manual. May be try the following replacement:
% Save whole DB into file
save(FileName) :-
open(FileName, update, F),
with_output_to(S, listing),
close(F).
Or even shorter:
save(FileName) :-
tell(FileName), listing, told.

Defining predicates in SICStus Prolog / SWI-Prolog REPL

I am reading http://cs.union.edu/~striegnk/learn-prolog-now/html/node3.html#subsec.l1.kb1,
but I am having trouble running the following predicate:
SICStus 4.0.1 (x86-win32-nt-4): Tue May 15 21:17:49 WEST 2007
| ?- woman(mia).
! Existence error in user:woman/1
! procedure user:woman/1 does not exist
! goal: user:woman(mia)
| ?-
If, on the other hand, I write it to a file and run consult the file, it seems to work fine...
Am I only allowed to define predicates in a file having later to consult them? Can't I just do it in the editor itself?
It's a little annoying to make predicates in the repl. You could do
| ?- ['user'].
woman(mia).
^D
ie consult user input, or
| ?- assertz(woman(mia)).
assert it. Both awkward IMO -- there might be a better way, though, I just don't know it. In general it is easier to use a script.
You should enter woman(mia). into a file to assert it as a fact. If you write it into the interpreter, it's taken as a query, not a fact.
From the SWI Prolog FAQ:
Terms that you enter at the toplevel are processes as queries, while
terms that appear in a file that is loaded into Prolog is processed as
a set of rules and facts. If a text reads as below, this is a rule.
carnivore(X) :- animal(X), eats_meat(X).
Trying to enter this at the toplevel results in the error below. Why?
Because a rule is a term :-(Head, Body), and because the toplevel
interprets terms as queries. There is no predicate with the name :-
and two arguments.
?- carnivore(X) :- animal(X), eats_meat(X). ERROR: Undefined
procedure: (:-)/2 ERROR: Rules must be loaded from a file ERROR:
See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
Isn't this stupid? Well, no. Suppose we have a term
eats_meat(rataplan). If this appears in a file, it states the fact
that rataplan eats meat. If it appears at the toplevel, it asks Prolog
to try proving whether rataplan eats meat.
If a text reads
:- use_module(library(clpfd)).
This is a directive. Directives are similar to queries, but instead of
asking the toplevel to do something, they ask the compiler to do
something. Like rules and facts, such terms belong in files.
Instead of writing to a file you can also use assert in the toplevel (as explained later in the FAQ as well).

Resources