17

I currently have the need to execute prolog code in an application I am making. I am aware that Apple probably never would allow something like this in the App Store, but that is not the intention either. This is more a private project that will never reach the App Store.

Purpose

In this case prolog is used to describe an object (like for example a telephone) and its properties. The object will be drawn with OpenGL using coordinates specified in the prolog script. The reason for using prolog is that I need the ability to query the program about some of the features this object has, and prolog eases this a lot. Bottom line: I "need" to query a prolog script from my app.

Possible solutions

  • Embed an already existing implementation written in C. I am unsure if this will even work.
  • Execute the prolog code on another machine and use the network to query prolog.
  • It seems that it is possible to run some sort Ruby VM inside the app (shinycocos uses this as far as I understand), could this be used to run one of the Ruby Prolog implementations?
  • Find some alternative to Prolog. This needs to give me some of the same possibilities I get with prolog.

Sadly, google gives me close to no results at all, so I have a feeling that I might be quite alone on this project. If anyone have any experience or clue at all, I would be very thankful.

Bendik
  • 928
  • 11
  • 23
  • Would you mind to share a link to one of those working Prolog implementations written in Ruby? – Giulio Piancastelli Oct 11 '10 at 11:31
  • I did not try any of them myself, but a quick search on google gave me this result: http://eigenclass.org/hiki.rb?tiny+prolog+in+ruby – Bendik Oct 13 '10 at 12:03
  • Ah, yeah, thanks. I saw some "tiny prolog" implementations very similar to that a long time ago, but I don't think they can be compared with a full-blown system such as SWI-Prolog, YAP, SICStus Prolog, &c. – Giulio Piancastelli Oct 13 '10 at 13:37

5 Answers5

6

Having faced similar difficulties calling prolog code, albeit in a different situation, I'd recommend checking out the castor c++ library. This allows you to write logic paradigm code in native c++ without needing to extend the language at all. As castor is a header only library it is easy to compile wherever c++ is available.
Castor website: http://www.mpprogramming.com/cpp/default.aspx

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • 1
    Although it sounds like a good solution, my supervisor sadly would not accept it. We already have a large knowledge base in prolog, and porting it would take too much time. – Bendik Oct 13 '10 at 11:59
  • @Bendik How hard is prolog to parse? If it's not too difficult you might be able to go with a code-generation approach to automatically generate the relevant castor c++ code. – shuttle87 Oct 26 '15 at 17:57
  • @shuttle87 that would be like porting from WAM to castor? – Erik Kaplun Nov 16 '20 at 07:46
6

Half a year later, I would just like to provide some insight on this. I ended up writing a server with an interface to prolog in Java, accepting prolog calls through TCP. It works almost exactly like the live prolog interpreter SWI-prolog (among others) provides, and mostly works quite well. However, it is far from an optimal solution, as you can't call functions from inside prolog. You lose the possibility of having two-way communication.

If I were to start all over again, I would certainly have tried harder to compile one of the pure C implementations for iOS. I gave it a quick go, but my lack of experience stopped me from even removing all of the errors I got. Judging by the fact that you cannot have prolog running as a background process on a unmodified version of iOS as well, some major rewriting would have to be done. Because of this, one might just have to write a new implementation (perhaps inspired by some of the more lightweight ones out there) from scratch to get the perfect solution.

Bendik
  • 928
  • 11
  • 23
  • 1
    I don't get it, why does Java enter the picture? With a few lines of code you could also have a SWI-based HTTP server (see http://www.swi-prolog.org/pldoc/package/http.html) – Kaarel Jun 13 '11 at 09:21
  • 1
    Because not all of the text sent through TCP is parsed by prolog. I needed the ability to control certain behavior through "application level commands". In addition, I am much more comfortable writing in Java, so the Java interface provided me with a comfortable way of sticking to the known without too much overhead. – Bendik Jun 13 '11 at 14:21
  • FYI, you can quite easily bi-directionally make calls between Java and SWI-Prolog if you use JPL: http://www.swi-prolog.org/packages/jpl/ – Rick Moynihan Jun 15 '12 at 12:46
4

You can download SWI-Prolog's source code and compile it with XCODE for iOS platform. I've never done that, but it's certainly technically possible.

Once you do that, there are a lot of examples on how to run prolog code from C/C++, hence, you will be able to run prolog from Objective-C.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 1
    I gave it an honest try, but sadly my knowledge about C, C++ and compilation without an IDE is limited, if not non existing. I ended up implementing a network solution with sockets and a server acting as a knowledge base instead. – Bendik Oct 13 '10 at 12:01
2

FYI, you can quite easily bi-directionally make calls between Java and SWI-Prolog if you use JPL:

http://www.swi-prolog.org/packages/jpl/

It is also fully re-entrant, so you can instantiate prolog code from java, which in turn instantiates java code etc...

I did this for a number of commercial projects a few years ago when I was required to connect a Prolog based Reasoning Engine to a lot of Java code.

It does use JNI (the Java Native Interface), so you need to be careful about how you compile and link to the native api. Though if you compile it appropriately for each platform you can make it work cross platform. I had it working on OS-X, Windows, Linux & Solaris.

Rick Moynihan
  • 481
  • 3
  • 10
1

I do not know if this has been tried but there is the possibility to use the combination of NodeJS for Mobile Apps & TauProlog:

enter image description here

and

enter image description here

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111