Wiki: https://toychest.ai.uni-bremen.de/wiki/ias:load_prolog_from_lisp
===== Steps =====
- Download and compile (manually) swi-prolog. It needs to be compiled with: --enable-shared because the aptitude version does not bring the shared library which is needed for the linking from LISP
NOTE: All shared objects need to be in the path of /usr/lib/gcc-(arch)/ for the Lisp Cffi to be able to find it.
- create an ASD file which contains the needed information about cffi-load-foreign-library
- cbcl
- (load "prolog-test.asd") enter your file name here
417.04 mm
===== Example ASD Content =====
;;; Marc Fiedler
;;; Loads external libraries via CFFI
;;; This works with shared objects
(asdf:oos 'asdf:load-op :cffi)
;;; Nothing special about the "CFFI-USER" package. We're just
;;; using it as a substitute for your own CL package.
(defpackage :prolog-test-user
(:use :common-lisp :cffi))
(in-package :prolog-test-user)
;;; in order to be able to load our own custom library, we first need to load all dependencies
;;; In this particular case its swi-prolog.
;;; NOTE: This is not from the APT repository, you need to compile it yourself with --enable-shared
(define-foreign-library swi-prolog
(:darwin (:or "libswipl.so" "libswipl.so"))
(:unix "libswipl.so")
(t (:default "libswipl.so")))
(use-foreign-library swi-prolog)
;;; This is our own test library written in c, using the SWI-Prolog API and now we need to call it
;;; from inside lisp. (define-foreign-library prolog-test (:darwin (:or "libcurl.3.dylib" "libcurl.dylib")) (:unix "prolog-test.so") (t (:default "prolog-test.so"))) (use-foreign-library prolog-test)
===== Bits and pieces you need =====
-
You need a working GCC in order to compile the c-code into shared objects
-
You need the sources of swiprolog (compile it with --shared) apt does not come with a shared object
-
You need SWIG2.0 to create shadow-classes from c to lisp
===== How it works ===== Fist after the shared object of swiprolog exits in your /usr/lib/gcc-/ folder you need to write a c-program that uses the swiprolog headers and functions.
{{:ias:screen_shot_2015-12-11_at_14.24.51.png?200|}}
Now all you need to do is write a small interface file for swig, which will outline all functions used in your c program.
After that you need to run swig with swig2.0 -cffi <interfacefile.i> <outputfile.lisp>
Write an ASD file which loads the shared object of SWIProlog and your own shared object and then load the lisp file generated by SWIG if all is done and no errors accrued you can load your ASD file into lisp and start using the prolog files.
{{:ias:screen_shot_2015-12-08_at_13.25.10.png?200|}}
===== References =====
SWI Prolog sources: http://www.swi-prolog.org/git.html
SWIG Parser: http://www.swig.org