Which Prolog implementation uses this syntax? - prolog

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?

Related

Dealing with dynamic predicates that cause compilation failures

I am attempting to run an example GNU Prolog program used as an example during my course work. The code is pulled directly from https://www.cpp.edu/~jrfisher/www/prolog_tutorial/2_17pl.txt and was shown working at one point by my professor.
However, when I run the provided example code, I get the following compilation warning:
| ?- consult('C:/Users/Chase/Desktop/Prolog files/newAnimal.pro').
compiling C:/Users/Chase/Desktop/Prolog files/newAnimal.pro for byte code...
C:/Users/Chase/Desktop/Prolog files/newAnimal.pro:74:12: syntax error: . or operator expected after expression
1 error(s)
compilation failed
The line that is keeping the program from compiling correctly is:
:- dynamic yes/1,no/1.
Which I read up on here: https://www.swi-prolog.org/pldoc/man?predicate=dynamic/1
However, despite attempting to rewrite and reformat the section, I could still not get it to compile.
Any help on why the provided code may not be running?
I am using a Windows GUI GNU Prolog console V1.4.5
The ISO Prolog standard doesn't require dynamic(or multifile or discontiguous) to be declared as an operator. A few systems do it (e.g. SWI-Prolog like you mentioned) but not GNU Prolog. Thus, to ensure code portability, avoid using dynamic as an operator. Write instead:
:- dynamic(yes/1).
:- dynamic(no/1).
Or:
:- dynamic((yes/1, no/1)).
Or:
:- dynamic([yes/1, no/1]).
These are the standard conforming alternatives for declaring multiple predicates as dynamic.
Also, GNU Prolog have a fine manual (part of its installation) which you should refer to when using GNU Prolog.

Prolog Dynamic Predicates Basic Usage

with this knowledge base using https://swish.swi-prolog.org
:- dynamic happy/1.
go:-
assert(happy(mia)),
write(happy(mia)).
if I run go. I get
happy(mia)
true
If I just have
:- dynamic happy/1.
assert(happy(mia)).
and run happy(mia), I get false.
What fundamental concept am I missing please?
When you write:
assert(happy(mia)).
you are (re)defining the predicate assert/1, not calling it as in your definition of the go/0 predicate. Thus, happy(mia) is never added to the database. The query fails as the predicate happy/1 is know by the system (thanks to the dynamic/1 directive) but have no clauses.
Most Prolog systems prevent the redefinition of standard built-in predicates. But assert/1 is a legacy/deprecated predicate. That explains why SWI-Prolog doesn't complain about the redefinition. Always use the standard assertz/1 predicate instead of assert/1.

SWI-prolog semweb library processing of URI

Being new to prolog I am reading existing code (as well as trying to write some code). Having some prior background in semweb I started to play with it and see something that is confusing me. Example assertion:
?- rdf_assert(ex:bob, rdf:type, foaf:'Person').
I also did find the following in the documentation:
Remember: Internally, all resources are atoms. The transformations
above are realised at compile-time using rules for goal_expansion/2
provided by the rdf_db library
Am I correct in assuming that somehow the library is treating the three URIs as atoms? I thought that the compiler would treat this as module_name:predicate, but that does not seem to be the case. If that is true, could you please provide a simple example on how this could be done in prolog?
Thanks
Prolog is not a functional language. This implies 2+3 does not evaluate to 5 and is just a term that gets meaning from the predicate that processes it. Likewise, ex:bob is just a term that has no direct relations to modules or
predicates. Only predicates such call/1 will interpret this as "call bob in the module ex".
Next to that, (SWI-)Prolog (most Prolog's, but not all) have term expansion that allows you to rewrite the term that is read before it is handed to the compiler. That is used to rewrite the argument of rdf/3: each appearance of prefix:local is expanded to a full atom. You can check that by using listing/1 on predicates that call rdf/3 using the prefix notation.
See also rdf_meta

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.

Applying predicates on a list in 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.

Resources