Compile Latex from Prolog - shell

I wrote a Prolog predicate that generates a Latex file, and I am looking for a way to be able to compile such a file automatically, within Prolog, to return instead the associated PDF file. I have found the shell predicate (http://www.swi-prolog.org/pldoc/man?predicate=shell/2) but I can't figure out how to properly make it work.
In case it is of any relevance, I am using SWI-Prolog 7.1.33 in a Mac.
Thanks in advance!

The traditional predicate to execute shell commands is system/1 that works in Unix-like and MS-Windows systems. For instance
?- system('echo a').
writes a a in a Unix-like system.

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.

How to call facts and rules from another file

Case: I have a file, solution.pl, where I want to use the rules of another file (this is the definition of the program, not up to me, but I think it's somewhat common).
So I'm using Ciao Prolog and the syntax seems to be ensure_loaded('c:/Path').
but when I try to use the rules of the other file, it tells me he doesn't know what rule I'm trying to use.
On the other hand, if I write on the console that same lane, and then query again, it does work.
So I was wondering maybe the syntax for doing it on the console and on a file are different??
Could someone tell me a step by step so I can figure it out if I'm missing something? What I'm doing right now is loading the program as I would do with smaller programs, then try to query rules of the file included in the ensure_loaded command. And as I said, it does seem to work if write the command outside of the program.
in solution.pl, try
:- ensure_loaded('c:/Path').
or, if the source in c:/Path has a module directive - that is, it begins with :- module(name, [exported/arity, ...]).
:- use_module('c:/Path').

How to set a break-point to a line in debugging prolog?

In Yap, I'm debugging a program and it reads a lot data from a file. I would like to ask whether there are some ways to set a break-point to a LINE in prolog file. I know there is a predicate called spy which allows you to set the program stop at an expected predicate, however if this predicate calls sub-predicates, how can I set a break-point to the sub-predicates? For example:
pA :-
pB,
pC.
How can I set a break-point to stop at pC(in case pC is a built-in predicate) since spy only allows me to set break-point at pA. Thanks very much for your answer.
The current Logtalk git version supports the definition of file line number spy points. YAP is one Prolog backend compilers supported by Logtalk. The feasibility of using the Logtalk debugger for debugging your program depends, however, if it's structured using Prolog modules. If so, it should be possible to compile your Prolog modules as objects by simply changing the file name extensions from Prolog to Logtalk (i.e. from .pl or .yap to .lgt) and using the Logtalk compiling and loading predicates.

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

How to run prolog code?

I am working on family tree in prolog. I don't have any idea how to compile and run this program. Please give me some basic steps to run it.
Assuming you are using SWI-Prolog
Step 1: Put your dictionary into a text file. Here's an example dictionary:
dog(rover).
dog(felix).
dog(benny).
Step 2: Title your dictionary "something.pl" -- I called this one dogs.pl.
Step 3: Open up SWI-Prolog from the command line. In linux, I use the command swipl at the command line. Once SWI-Prolog starts, you will see a command line that looks like ?-
Step 4: In SWI-Prolog, load your dictionary by using the consult command like so:
?- consult('dogs.pl').
Step 5: Now that your dictionary is loaded, you can use it. Here's an example using our test dictionary about dogs:
?- dog(rover).
true.
dog(X).
X = rover ;
X = felix ;
X = benny .
That should pretty much do it as far as getting your prolog programs to load and run.
Finally, here's a link for how others run Prolog:
Adventures in Prolog
When you have finished your code, do the following steps to run your code:
Step 1: open your prolog terminal.(See pic1)
Step 2: click "File" on the left of the top, then choose "Consult" to open the prolog file(your code file).(See pic2)
Step 3: type in your function name and parameters.
Well, that would depend entirely on your Prolog implementation.
The language is one thing but how to compile or run your code is a different issue.
For example, Visual Prolog uses a key sequence within the IDE, CTRL-SHIFT-B, to build the code, or ALT-F5 to run the code. You need to find the equivalent way of doing the same thing in whatever Prolog implementation you're using (or at least let us know).
There's no official standard for the Prolog built-in predicates that compile and load a source file. The most common ones are consult(File), reconsult(File), and load_files(Files, Options). The shortcut [File| Files] is also often available. You will need to consult the documentation of the Prolog system you're using. Be aware that even for the common ones above, the semantics often differ from system to system.
If you are using terminal or cmd
Navigate to the folder where you saved your kB
Run it as script using the following command
swipl -s file.pl

Resources