Saving GProlog database - prolog

In my program I have a dynamic clauses, they works fine, but when I closing my program, they are disappears.
I've tryed that
saveState :-
write_pl_state_file('backup.dat').
loadState :-
file_exists('backup.dat'),
read_pl_state_file('backup.dat'); !.
but this is not works.
Is there a way to save this databse to a file?

The predicates write_pl_state_file/1 and read_pl_state_file/1 are connected with the information/state that affects parsing of terms, i.e. operator definitions, character conversion Prolog flags, etc.
So that is part of your solution (perhaps), but more fundamentally you wish to save the dynamic clause definitions, probably in a form that allows you to reinstate them by consulting a file.
The predicate listing/0 does something like this, but it displays the dynamic clauses to the "console", not to a file. Probably you want to use the underlying predicate portray_clause/2, which does allow redirecting output to a file (stream).
The author Daniel Diaz noted a slight change (adding a newline to end of output) for portray_clause/2 in recent release notes for version 1.4.0, so you may want to make sure you've got the latest version for the sake of legibility.
Added:
It appears that starting with version 1.3.2 GNU Prolog supports sending listing/0 output to the current stream (rather than just to the console as in 1.3.1 and earlier).
Here's a test case:
| ?- assertz(whoami(i)).
| ?- assertz(whoami(me)).
| ?- assertz(whoami(myself)).
which creates three clauses (facts) for a dynamic predicate whoami/1.
I then created a file myClauses.pl with the following query:
| ?- open('myClauses.pl',write,S), set_output(S), listing, close(S).
Once the stream S is closed, current output is reset to the console.
You will find that the file myClauses.pl contains a blank line followed by the three clauses, so that the source code is in proper form to be consulted. However I'm having a problem with the consult/1 predicate (and its File -> Consult... menu equivalent) in my newly installed GNU Prolog 1.4.0 under Windows. The compilation works from the command line and produces a byte-code file that load/1 can correctly handle in the console, so there's some small problem in how things are set up. I'll post a further note when I get that squared away, having sent in a bug report. I've not tried it yet under Linux.

You can use current_predicate/1 or predicate_property/2 to access predicates, and clause/2 to access the clauses for a predicate.
Then you can write a save utility by using that information.

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.

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 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.

Conversion from string to list in Prolog

I'm trying to use a piece of software and I'm running into some problems.
It's worth noting that using university computers which have prolog preinstalled(OSX) or Windows computers the same piece of software works. While it does not work on my Linux/Ubuntu machine.
The software starts up with a bash script using this form:
echo "reset_statistics(off), specific_load_predicate(filename), tokenize(\"$2\", List), tex(List, $3)." | ./prolog-executable
The prolog executable is created using swipl -c and qsave_program/1
Now I found out the predicate tokenize is of the form:
tokenize([A|As], List) :- ...
So it takes a list as input, but the bash script provides a String.
Is it possible using some sort of prolog module or extension that would automatically make the conversion from string to list? Because the same code works on other computers.
SWI-Prolog unfortunately deviates from the Prolog ISO standard in that what is surrounded by double quotes is no longer a list of character codes.
To restore the compliant behaviour in this respect, use one of the following methods:
invoke SWI-Prolog with the --traditional flag
add :- initialization(set_prolog_flag(double_quotes, codes)). to your source file.
add :- set_prolog_flag(double_quotes, codes). to your ~/.swiplrc initialization file.
In fact, instead of going with lists of character codes, I recommend you take a different route altogether and adapt your programs to use lists of characters, which means that each character is represented by an atom. This has the great advantage that toplevel answers remain very similar to what appears in queries, and are in fact quite readable by themselves. You obtain lists of characters for example by putting:
:- set_prolog_flag(double_quotes, chars).
in your ~/.swiplrc initialization file. With the value chars, you get for example:
?- Cs = "hello".
Cs = [h, e, l, l, o].
I recommend you use this value, and adapt your existing programs to work with lists of characters. Long-term, I think this will be the the best and most widely used approach, which was historically also what double-quotes were meant to denote.
Until we get (back) there, you can use one of the methods above (i.e., switch to codes as an intermediate solution, to at least work around the problems introduced by SWI 7), then adapt your libraries to work with codes and characters, and then, at last, switch to characters.

Workaround ensure_loaded/1 GNU Prolog?

Is there a workaround to make ensure_loaded/1 work
in GNU Prolog as it works in many other Prolog systems?
The goal is to have a preamble so that the rest of
code can use ensure_loaded/1 independent of whether which
Prolog system I use.
I tried the following:
:- multifile(term_expansion/2).
term_expansion((:- ensure_loaded(X)),
(:- atom_concat('<base>\\', X, Y),
include(Y))).
But the following query doesn't work:
:- ensure_loaded('suite.p').
The path calculation itself is not the issue of the question,
but the redefinition of a directive in GNU Prolog. There is
another directive that causes problems: meta_predicate/1. The
byte code crashes as follows:
Bye
A partial solution is:
ensure_loaded(File) :-
absolute_file_name(File, Path),
( predicate_property(_, prolog_file(Path)) ->
true
; consult(Path)
).
It assumes that the file defines at least one predicate but that's a sensible assumption. However, there's seems to be no way to override the native, non-functional, definition of the ensure_loaded/1 directive. A workaround would be to wrap the ensure_loaded/1 directive within an initialization/1 directive. For example:
:- initialization(ensure_loaded('suite.pl')).
Hence this being a partial solution as we're really defining an ensure_loaded/1 predicate, not a directive.
My current speculation is, that it is impossible with
the standard distribution of GNU Prolog 1.4.4. The
docu says:
The GNU Prolog compiler (section 4.4) automatically calls
expand_term/2 on each Term1 read in. However, in the current release,
only DCG transformation are done by the compiler (i.e.
term_expansion/2 cannot be used). To use term_expansion/2, it is
necessary to call expand_term/2 explicitly.
I also tried to inject some Prolog code for term_expansion/2
via the command line, but to no awail. Although the tool chain
has options such as -O, -L, -A that pass options to other tools.
There is not really an option that passes a Prolog text to the
pl2wam, in course of the execution of a consult/1 issued inside
the top-level.
At least this are my results so far.
Bye

Resources