Showing posts with label Prolog. Show all posts
Showing posts with label Prolog. Show all posts

Monday, July 1, 2013

Calling Prolog from Python

This is my shopping list of things to investigate. It's a notes page of where I am at the moment in the process of connecting Python and Prolog. I hope it might help someone who is trying to do the same thing, or perhaps thinking about trying to do the same thing. If you have any ideas then it would be great to hear from you. At the moment I am thinking that programming in C++ and using SWI-Prolog's C++ interface might be the answer.

I've been looking into this lately and have collected these  things to look at.
I can program in Prolog and like it. I'm not great at it but I love the way of thinking. I have been using Python also, not that I particularly like Python, but it's so useful and appears in so many places. (yes I've been plating with the Raspberry Pi). So, can I get the Pi to use prolog as it's decision making engine?

pyswip

https://code.google.com/p/pyswip/
This is a bridge between SWI-Prolog and Python and it seems to be the first place to visit. It works on Linux and Win32 but not sure about OSX

I looked at this first and thought that it had stopped being developed. I'm not sure. I think there was an update in December 2012.

There seems to be a nice post on someone's expereince from 2011 here: http://ryepdx.com/2011/09/prolog-in-python-pt-1/

Picstus

This is an interface between Sicstus Prolog and Python. I probably wont look at this as I am using SWI-Prolog for the time being. Sicstus costs about 165 Euros and I'm not that professional.
http://www.biolab.si/picstus/picstus.html


A Prolog Interpreter in Python

This is very interesting stuff. Excelent stuff. http://wwwold.stups.uni-duesseldorf.de/thesis/Bolz2007-Bachelorarbeit.pdf but probably it's not going to help me.
[UPDATE 2016] The above link no longer works. Try this http://stups.hhu.de/w/A_Prolog_Interpreter_in_Python

There is also some work by Chris Meyers http://www.openbookproject.net/py4fun/ a few links in this page. In summary though it's too slow for a real world solution.


SWIG

"SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages." So We could wrap Prolog with Swig.

Probably not what I want to do as a bit complex.

Pwig

This is a swig extension for Python.
http://pwig.sourceforge.net
The last activity was in 2004.
Can download from here:
http://sourceforge.net/projects/pwig/files/pwig/
SWIG is at version 2.0 but PWIG required SWIG 1.3.23 which I couldn't see. It might be there somewhere.

PyKE

Now this is interesting. http://pyke.sourceforge.net/ 
It introduces a form of logic programming into Python. There is an interesting paper at http://pyke.sourceforge.net/PyCon2008-paper.html 

It has an inference engine but the syntax is new. I suppose it would do but it's a shame not to improve my skills in Prolog. That said though, the skill in prolog is the thinking not the syntax.

PyProlog

A Python extension embedding SWI-Prolog. This was last updated in 2001 so is a tad out of date with very little or no information.



Pyrolog

I'm not sure about this one. A prolog interpreter written in RPython.
https://bitbucket.org/cfbolz/pyrolog

PyLog

http://wiki.python.org/moin/PyLog

Well I'm not sure about this. It's a first order logic library. It also

Knowrob

KnowRob is a knowledge processing system that combines knowledge representation and reasoning methods with techniques for acquiring knowledge and for grounding the knowledge in a physical system and can serve as a common semantic framework for integrating information from different sources.

Too big for my needs here but really cool.

So now what do I think?

Well I'm beginning to think I should look at coding in C or C++.
My task is for the Raspberry Pi and it's a shame to slow it up by getting into C again after so many years.  Python wold be such a fast and rapid development. Maybe I create a C interface to my Prolog. Then use Python for all other motor controlling, sensor controlling. Then, when it's a success and become a millionaire, I can refactor all the code from Python to C for the fun of it.
This is a disappointment. I'll do some more digging and write my thoughts here.



Friday, May 10, 2013

Greatest Common Divisor in Prolog

I am writing this because it's interesting and useful in seeing how Arithmetic can work in Prolog. It's from the Ivan Bratko book, Prolog Programming for Artificial Intelligence.
It's a great book.
Given two positive integers X and Y, their greatest common divisor, D, can be found according to three cases:
  1. If X and Y are equal then D is equal to X.
  2. If X < Y then D is equal to the greatest common divisor of X and the difference Y - X.
  3. If Y < X then do the same as (2.) with X and Y interchanged.


The rules can be expressed as a Prolog program by defining a three argument relation:
gcd( X, Y, D).
The three rules are then expressed as three clauses:

gcd( X,X,X).

gcd( X, Y, D) :-
   X < Y,
  Y1 is Y - X,
  gcd( X, Y1, D).

gcd( X, Y, D) :-
  Y < X,
  gcd( Y, X, D).

To me this was not immediatly apparent and I had not seen it before so I worked through Ivan Bratko's  example just to confirm it worked. Lo-and-behold it does.


scenario 1. X = 20 and Y = 25
So with this example, The greatest common divisor is 5.
We just subtract our way down, something like this:

[trace] 1 ?- gcd(20, 25, D).
   Call: (6) gcd(20, 25, _G1990) ? creep
   Call: (7) 20<25 ? creep
   Exit: (7) 20<25 ? creep
   Call: (7) _G2066 is 25-20 ? creep
   Exit: (7) 5 is 25-20 ? creep
   Call: (7) gcd(20, 5, _G1990) ? creep
   Call: (8) 20<5 ? creep
   Fail: (8) 20<5 ? creep
   Redo: (7) gcd(20, 5, _G1990) ? creep
   Call: (8) 5<20 ? creep
   Exit: (8) 5<20 ? creep
   Call: (8) gcd(5, 20, _G1990) ? creep
   Call: (9) 5<20 ? creep
   Exit: (9) 5<20 ? creep
   Call: (9) _G2069 is 20-5 ? creep
   Exit: (9) 15 is 20-5 ? creep
   Call: (9) gcd(5, 15, _G1990) ? creep
   Call: (10) 5<15 ? creep
   Exit: (10) 5<15 ? creep
   Call: (10) _G2072 is 15-5 ? creep
   Exit: (10) 10 is 15-5 ? creep
   Call: (10) gcd(5, 10, _G1990) ? creep
   Call: (11) 5<10 ? creep
   Exit: (11) 5<10 ? creep
   Call: (11) _G2075 is 10-5 ? creep
   Exit: (11) 5 is 10-5 ? creep
   Call: (11) gcd(5, 5, _G1990) ? creep
   Exit: (11) gcd(5, 5, 5) ? creep
   Exit: (10) gcd(5, 10, 5) ? creep
   Exit: (9) gcd(5, 15, 5) ? creep
   Exit: (8) gcd(5, 20, 5) ? creep
   Exit: (7) gcd(20, 5, 5) ? creep
   Exit: (6) gcd(20, 25, 5) ? creep
D = 5 .

The only maths bit is the
Y1 is Y - X 
but it shows how to use is It's a mathematical equals really.



Sunday, May 5, 2013

Prolog resources

Getting started

An introduction to logic programming through Prolog
An introduction to logic programming through Prolog was published by Prentice-Hall in 1996 but is now out of print. The author kindly provides a pdf of the text here:
http://spivey.oriel.ox.ac.uk/corner/Logic_Programming

Learn Prolog Now! website
Online
http://www.learnprolognow.org/lpnpage.php?pageid=online


Programming in Prolog: Using the ISO Standard 
by C.S. Mellish (Author), W.F. Clocksin



Logic

"Bob's book". Logic for Problem Solving
Logic for Problem Solving by Robert Kowalski is out of print but fortunatley the text can be viewed in pdf for at his web page at Imperial College.


Simply Logical, Intelligent Reasoning by Example
Simply Logical, Intelligent Reasoning by Example by Peter Flach is out of print but he kindly provides a pdf http://www.cs.bris.ac.uk/~flach/SimplyLogical.html

AI

Prolog Programming for Artificial Intelligence
by Ivan Bratko


Interesting Sites

Logic in Action
http://www.logicinaction.org

This is the new homepage of the Logic in Action Open Course Project. The project aims at the development of elementary and intermediate courses in logic in electronic form.

Wednesday, April 24, 2013

How to start swi-prolog on Mac OSX

So you've downloaded the swi-prolog package from http://www.swi-prolog.org/Download.html and you need to run it.

It runs in the terminal but you need to run, opt/local/bin/swipl

If you have a folder with your file in, e.g. Download/myfile.pl then why not navigate to that folder in terminal before running swipl. That way you can edit and create files easily in your editor of choice and load the file easily in SWI-prolog.

By the way, to load a file type, ['my file.pl'].


You can, if you want, run the PCEemacs editor. You'll want to do this sooner or later.
To run this you should type, emacs.

It may ask you to load X11. Just follow the instructions.