Re: Accessing PowerLoom from Haskell
Anders NĂ¥sell <[email protected]> Tue, 08 Sep 2015 12:09:04 +0200
| Newsgroups | gmane.comp.ai.powerloom |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------080109070002080108070103
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 8bit
Hans,
Thanks for the very nice example. Thanks to it I manage to call PowerLoom from Haskell over C.
I have attached a file with the Haskell version of "powerloom-from-c.c", powerloom-from-haskell.hs
Compile and run like this:
$ export PL_HOME=.../powerloom-4.0.0.beta
$ g++ -I$PL_HOME/native/cpp -I$PL_HOME/native/cpp/stella/cpp-lib/gc/include -c pli-c-api.cc -DSTELLA_USE_GC
$ ghc -c powerloom-from-haskell.hs
$ ghc pli-c-api.o powerloom-from-haskell.o -o powerloom-from-haskell -L$PL_HOME/native/cpp/lib -lpowerloom -lpowerloom-extensions -lutilities -llogic -lstella -lgc -lm -lstdc++
$ export LD_LIBRARY_PATH=$PL_HOME/native/cpp/lib
$ ./powerloom-from-haskell
Sincerely
Anders
--------------080109070002080108070103
Content-Type: text/x-haskell;
name="powerloom-from-haskell.hs"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="powerloom-from-haskell.hs"
{-# LANGUAGE ForeignFunctionInterface #-}
-------------------------------------------------------------------------=
------
-- Compile and run like this:
--
-- export PL_HOME=3D.../powerloom-4.0.0.beta
-- g++ -I$PL_HOME/native/cpp -I$PL_HOME/native/cpp/stella/cpp-lib/gc/incl=
ude -c pli-c-api.cc -DSTELLA_USE_GC
-- ghc -c powerloom-from-haskell.hs
-- ghc pli-c-api.o powerloom-from-haskell.o -o powerloom-from-haskell -L$=
PL_HOME/native/cpp/lib -lpowerloom -lpowerloom-extensions -lutilities -ll=
ogic -lstella -lgc -lm -lstdc++
-- export LD_LIBRARY_PATH=3D$PL_HOME/native/cpp/lib
-- ./powerloom-from-haskell
--
{-
Initializing...
(defconcept person)
/PL-KERNEL-KB/PL-USER/PERSON
(defrelation friend-of (?x ?y)))
FRIEND-OF
(assert (and (person fred) (person susi))))
((PERSON FRED) (PERSON SUSI))
(assert (friend-of fred susi))
(FRIEND-OF FRED SUSI)
(retrieve all (person ?x))
[2015-SEP-02 13:24:04.000 PL] Processing check-types agenda...
(SUSI FRED)
(retrieve all (friend-of ?x ?y))
((FRED SUSI))
-}
-------------------------------------------------------------------------=
------
module Main where
import Foreign.Ptr
import Foreign.C.String
foreign import ccall "pli_initialize"
c_pli_initialize :: IO ()
foreign import ccall "pli_s_evaluate_to_string"
c_pli_s_evaluate_to_string :: CString -> CString -> IO CString
pli_s_evaluate_to_string :: String -> String -> IO String
pli_s_evaluate_to_string com mod =3D=20
withCString com $ \ ccom ->=20
withCString mod $ \ cmod -> do
cresult <- c_pli_s_evaluate_to_string ccom cmod
peekCString cresult
=20
=20
foreign import ccall "pli_s_change_module"
c_pli_s_change_module :: CString -> IO (Ptr ())
pli_s_change_module :: String -> IO ()
pli_s_change_module mod =3D
withCString mod $ \ cmod -> do
c_pli_s_change_module cmod
return ()
foreign import ccall "pli_object_to_string"
c_pli_object_to_string :: Ptr () -> IO CString
pli_object_to_string :: Ptr () -> IO String
pli_object_to_string p =3D do
cs <- c_pli_object_to_string p
peekCString cs
foreign import ccall "pli_s_evaluate"
c_pli_s_evaluate :: CString -> CString -> IO (Ptr ())
pli_s_evaluate :: String -> String -> IO (Ptr ())
pli_s_evaluate com mod =3D
withCString com $ \ ccom ->=20
withCString mod $ \ cmod ->=20
c_pli_s_evaluate ccom cmod
main =3D do
let mod =3D "PL-USER"
=20
putStrLn "Initializing..."
c_pli_initialize
putStrLn "\n"
let command =3D "(defconcept person)"
putStrLn command
pli_s_evaluate_to_string command mod >>=3D putStrLn
-- change mod for nicer result printing:
pli_s_change_module mod=20
let command =3D "(defrelation friend-of (?x ?y)))"
putStrLn command
pli_s_evaluate_to_string command mod >>=3D putStrLn
let command =3D "(assert (and (person fred) (person susi))))"
putStrLn command
pli_s_evaluate_to_string command mod >>=3D putStrLn
let command =3D "(assert (friend-of fred susi))"
putStrLn command
pli_s_evaluate_to_string command mod >>=3D putStrLn
let command =3D "(retrieve all (person ?x))"
putStrLn command
pli_s_evaluate_to_string command mod >>=3D putStrLn
-- passing objects:
let command =3D "(retrieve all (friend-of ?x ?y))"
putStrLn command
result <- pli_s_evaluate command mod
pli_object_to_string result >>=3D putStrLn
--------------080109070002080108070103
Content-Type: text/x-c++src;
name="pli-c-api.cc"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="pli-c-api.cc"
// g++ compilation: g++ -o globals globals.cc
// Compile like this:
// setenv PL_HOME /tmp/build-kagg
// % g++ -I$PL_HOME/native/cpp -I$PL_HOME/native/cpp/stella/cpp-lib/gc/in=
clude -c pli-c-api.cc -DSTELLA_USE_GC
#include "powerloom/powerloom-system.hh"
namespace pli {
using namespace stella;
using namespace logic;
extern "C" void pli_initialize() {
initialize();
}
extern "C" void* pli_s_evaluate(char* command, char* moduleName) {
return sEvaluate(command, moduleName, NULL);
}
extern "C" char* pli_s_evaluate_to_string(char* command, char* moduleName=
) {
Object* result;
result =3D sEvaluate(command, moduleName, NULL);
if (result !=3D NULL)
return objectToString(result);
else
return (char*) ""; // for simplicity
}
extern "C" char* pli_object_to_string(Object* object) {
if (object !=3D NULL)
return objectToString(object);
else
return NULL;
}
extern "C" void* pli_s_change_module(char* name) {
return sChangeModule(name, NULL);
}
}
--------------080109070002080108070103
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
powerloom-forum mailing list
[email protected]
http://mailman.isi.edu/mailman/listinfo/powerloom-forum
--------------080109070002080108070103--