If you go back to the tutorials you've been looking at, they should be making clear (as @PauloMoura points out) that there's a difference between consulting a file in Prolog and interacting with Prolog at it's interactive prompt.
If you simply run Prolog without consulting a file, you can manually enter facts and rules, then press ctrl-D
to finish. For example:
? - [user].
|: human(ann).
|: human(george).
|: human(mike).
|: % user://1 compiled 0.00 sec, 2 clauses
true.
Then you can query by typing (at the shown prompt):
?- human(Who).
Who = ann ;
Who = george ;
Who = mike.
?- bagof(H, human(H), Humans).
Humans = [ann, george, mike].
?-
If you want to put things in a file, you can put your facts in a file, say, humans.pl
:
human(ann).
human(george).
human(mike).
Then consult the file from the Prolog prompt:
?- [humans].
% humans compiled 0.00 sec, 5 clauses
true.
And then do my queries (again, manually in this case) shown above with the same results:
?- human(Who).
Who = ann ;
Who = george ;
Who = mike.
?- bagof(H, human(H), Humans).
Humans = [ann, george, mike].
These outputs are provided courtesy of the interactive prompt. If you want to have everything in the file, you need to have a predicate in the file which would use I/O functions to output the information you want.
So we can make humans.pl
look like this:
human(ann).
human(george).
human(mike).
?- human(Who), write(Who), nl, fail.
?- bagof(H, human(H), Humans), write(Humans), nl.
And then you'd get:
?- [humans].
ann
george
mike
Warning: /home/mark/src/prolog/_play_/humans.pl:5:
Goal (directive) failed: user: (human(_G606),write(_G606),nl,fail)
[ann,george,mike]
% humans compiled 0.00 sec, 5 clauses
true.
?-
The warning comes because the first query fails (used to do the backtrack for more results). you can get rid of that failure just by adding an alternative true path to that query:
?- human(H), write(H), nl, fail ; true.
Or, you can introduce a single predicate to output all the humans:
human(ann).
human(george).
human(mike).
show_humans :-
human(H),
write(H), nl,
fail.
show_humans.
?- show_humans.
?- bagof(H, human(H), Humans), write(Humans), nl.
Which results in:
?- [humans].
ann
george
mike
[ann,george,mike]
% humans compiled 0.00 sec, 7 clauses
true.
main.
? – lurker