Applying predicates on a list in Prolog - prolog

I am a newbie in SWI-Prolog (5.10.5 running on win 7).
There are 3 files in an application that I am learning about:
a.pl and b.pl and c.pl
a.pl uses the predicate foldl/4, which seems to be in apply.pl.
c.pl, uses the following:
:- ensure_loaded('a.pl').
:- ensure_loaded('b.pl').
and then calls some predicates in these files.
When I run c.pl, I get exception that foldl/4 is not defined. I tried adding the following to a.pl:
:- use_module(library(apply)).
and
:- [library(apply)].
but no use.
Could you help?
Thanks in advance
Suzi

I'm running 5.10.4 on Ubuntu, and my version does not have apply:foldl, though it does have most of the other predicates in apply. The online source shows that foldl was only added on 2012-06-05, but versions 5.10.X are over 2 years old.
So it looks like the online docs are for much newer versions, which you'd need to upgrade to to have this predicate.

Related

Which Prolog implementation uses this syntax?

I have to study Prolog for the AI course at school. My teacher uses Visual Prolog which is only available on Windows. I use MacOS so I downloaded SWI-Prolog thinking it's similar. However, I noticed that the syntax is not the same. For example: the keyword if is replaced by :- in SWI-Prolog, also my teacher used this syntax:
predicates
likes(symbol, symbol)
clauses
likes(ellen, tennis).
likes(john, football).
which gives me an error in SWI-Prolog. What do I have to download to use the same syntax?

Prolog existence_error procedure in basic example

I'm trying to learn the basics of Prolog and keep running into a existence_error with the following code.
comes_after(m1, m2).
comes_after(m2, m3).
comes_after(m3, m4).
comes_after(m4, m5).
comes_after(m5, m6).
does_come_after(X, Y) :- comes_after(X, Y).
does_come_after(X, Z) :- comes_after(X, Y), does_come_after(Y, Z).
When executing a query such as does_come_after(m1, m3) I keep getting the following error.
uncaught exception: error(existence_error(procedure,comes_after/0),does_come_after/0)
Here's a screenshot showing the error:
Prolog Error
What am I doing wrong, and what should I keep in mind to avoid these errors in the future? Thanks in advance.
The error message tells you that Prolog expects a predicate comes_after/0, but none is found. Further, this problem arises when being called from a predicate does_come_after/0. Now, your definitions all use arity 2. Thus comes_after/2 and does_come_after/2. So what the system expects cannot happen.
And if it does, this must be related to your installation. You have 1.4.5 which is the most recent version, 1.4.4 the still current stable.
It is thus possible that you have another, older, system installed which interferes by providing an incompatible pl2wam compiler. To verify this, say which pl2wam or pl2wam --version.
In particular, versions from 1.3 or even 1.2 may produce such results. There is no version checking for this in GNU.
To ensure that I get always the right version, I say:
export PATH=/opt/gupu/gprolog-1.4.5/bin:${PATH}
Unfortunately, this is a problem with version 1.4.5.
Instead of downgrading, fortunately, there is a trick that you can do:
Instead of using consult(file_name) inside gprolog, you can run this command on your terminal (outside gprolog)
gplc file_name.pl
it will output an executable that you can run by
./file_name
it should solve your existence error problem.

Prolog, listing/1, Built-in System Predicates, SWI-Prolog 7.4.2

I'm trying to get the definitions for a few built-in predicates using SWI-Prolog 7.4.2. Specifically, split_string/4.
explain/1 returns:
system:split_string/4 is a built-in predicate
Summary: ``Break a string into substrings''
true.
listing/1 returns:
Foreign: system:split_string/4
true.
I've checked the System.pl file, but I cannot seem to find the definition. Is this possible at all?
SWI-Prolog is C based. Look for *.c in github, more specifically at this source.

Persistence of facts in Prolog

I'm kinda new in Prolog and I'm using SWI-Prolog v6.6 to storage asserts in a *.pl file.
:- dynamic fact/2.
assert(fact(fact1,fact2)).
With the code above I can make asserts and it works fine, but the problem is when I close SWI-Prolog and I open the *.pl file again, the asserts I've made are gone...
Is there a way to make asserts and those get stored even if I exit the Prolog process?
Sorry about my bad english and Thanks! (:
Saving state has certain limitations, also see the recent discussion on the SWI-Prolog mailing list.
I think the easiest way to persistently store facts on SWI-Prolog is to use the persistency library. For that I would rewrite your code in the following way:
:- use_module(library(persistency)).
:- persistent fact(fact1:any, fact2:any).
:- initialization(init).
init:-
absolute_file_name('fact.db', File, [access(write)]),
db_attach(File, []).
You can now add/remove facts using assert_fact/2, retract_fact/2, and retractall_fact/2.
Upon exiting Prolog the asserted facts are automatically saved to fact.db.
Example usage:
$ swipl my_facts.pl
?- assert_fact(some(fact), some(other,fact)).
true.
?- halt.
$ swipl my_facts.pl
?- fact(X, Y).
X = some(fact),
Y = some(other, fact).
If what you're after is just to get a list of certain facts asserted with a predicate, then mbratch's suggestion will work fine. But you may also want to save the state of your program in general, in which case you can use qsave_program/2. According to the swi docs, qsave_program(+File, +Options)
Saves the current state of the program to the file File. The result is a resource archive containing a saved state that expresses all Prolog data from the running program and all user-defined resources.
Documentation here http://www.swi-prolog.org/pldoc/man?section=runtime

Why am I getting an error in Prolog when trying out conc?

If I'm trying out next query in Prolog, I'm getting an error...
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.4.1)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
?- conc([a,b], [c,d], [a,b,c,d] ).
ERROR: toplevel: Undefined procedure: conc/3 (DWIM could not correct goal)
Is this because I'm not loading in a seperate file with facts and rules? Because that seems strange. Next query for instance does work:
?- member(apple, [apple, broccoli, crackers]).
true .
You are using SWI-Prolog as the interpreter and there is no built-in predicate conc shipped with it. You can have a look at append/3 (which is also a predicate from the list library, but note that in SWI this library is autoloaded while it is not always true in other implementation, eg see Yap, so you'd better use use_module).
Are you studying Prolog from Bratko's "Prolog Programming for Artificial Intelligence"?
(If the answer is yes, you'd better try to implement those predicate by yourself :))
See the FAQ. You can only enter queries at the toplevel prompt.

Resources