Prolog Programming in Ubuntu
Asked Answered
A

6

41

I have an interest in playing and fuxing with prolog, I have installed the swi-prolog and added the repository, just in case anyone is interested on which one commands I used:

% sudo apt-add-repository ppa:swi-prolog/stable
% sudo apt-get update
% sudo apt-get install swi-prolog

How do I actually begin to write prolog codes on my linux machine? for my regular programming I use VIM to write/edit/debug and terminal to compile. Can I use vim to write prolog? How do i compile or use the prolog interpreter(i think that is what it is called)?

Ailee answered 7/12, 2013 at 23:24 Comment(3)
Note that SWI-Prolog can be installed directly from the Ubuntu repositories (i.e. without PPA): sudo apt-get swi-prolog.Reportage
Thanks @Flux, but it should be sudo apt-get install swi-prologFarfamed
@Genius Yes. Thanks. That was a typo.Reportage
F
53

Yes, you can use any text editor, incl. VIM. Once you have written a Prolog source file, say, file.pl, you can load it into SWI-Prolog like so:

swipl -s file.pl

This will compile your file and take you to an interactive shell where you can then ask queries against the definitions in your file.

If you want to use your Prolog program in batch mode, you can use:

swipl -s file.pl -t goal

where goal is the goal/query you want to evaluate. Note that in this case you won't be getting the option to ask for alternative solutions.

Fishgig answered 8/12, 2013 at 0:50 Comment(2)
Thank you, I will play around with those, is there a command where I can exit the the prolog shell? I hate to open and close my terminal for every time I want to re-run or edit the codeAilee
Syntax highlighting in VIM and the workaround for VIM recognising ".pl" files as Perl and not Prolog are discussed here: #19611234Greenhaw
P
12

On Ubunutu, I started off using emacs, which at least does syntax highlighting:

http://www.swi-prolog.org/FAQ/GnuEmacs.html

(2 emacs suggestions on that page ^)

But now I use prolog in anger, I use an Eclipse plugin called PDT:

http://sewiki.iai.uni-bonn.de/research/pdt/docs/v2.1/start

Especially useful is the real-time line by line debug and trace, so you can step into, step over individual predicates, monitor variables names etc.. just like an other real IDE you would find in eclipse.

Probably only worth installing if you're going to use it a LOT, since the install is a lot of work, but it's a great IDE.

But if you like your low level editors like VIM, you will have to use the debug and trace tools built into swi-prolog, see:

http://www.swi-prolog.org/pldoc/man?section=debugger

To work out how the strange and beautiful prolog interpreter works, using a tracer of some kind is a must-have.

Pula answered 9/12, 2013 at 18:55 Comment(1)
thanks for the suggestion, will definitely try it out, I was never too big on IDE's especially eclipse, I am most comfortable with VIM, I have used emacs before and will give that a tryAilee
D
2

I personally use gprolog or swipl in the interpreted environment. So you write facts and rules in a mydb.pl file, and open the interpreter in the same directory. Once the prompt shows up you can query

['mydb.pl'].

for loading your database. now you can either see the warnings\errors or start querying from inside the prolog interpreter.

Deloresdeloria answered 13/3, 2018 at 12:32 Comment(0)
Q
1

buddy I also use vim to edit prolog code, What I personally do is I save my prolog file with the '.pl' extension, and then on the terminal, I use prolog interactive environment to consult my file e.g:

To initiate a prolog interactive environment just type On terminal:

prolog

Now that you have entered in SWI-prolog you can use 'consult' i.e pre-defined pseudo-predicates allow one to load Prolog code into a running Prolog interpreter:

?- consult("filename.pl")

that's it!

Quaggy answered 31/3, 2021 at 5:45 Comment(0)
C
0

You can use any text editor to write your code. Just make sure to save your code with the .pl extension like fibo.pl. After that open the terminal and go to the location where you have saved your code. After that type prolog After that write the name of your file without .pl extension and end it with . ['fibo']. and press return

Eg - cd /home/student/14917
prolog
['fibo'].


Here fibo.pl is my program name

Clarkia answered 6/3, 2019 at 8:0 Comment(0)
B
0

I use SWI prolog with Sublime Text on mac. Works really nice. In Sublime Text you just hit cmd - B to run the code, and the output appears in a window within Sublime Text. There is a package for it here.

Bergren answered 7/3, 2019 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.