{-# Language LambdaCase #-}
module Language.Souffle.Internal
( Souffle
, Relation
, RelationIterator
, Tuple
, init
, setNumThreads
, getNumThreads
, run
, loadAll
, printAll
, getRelation
, getRelationIterator
, relationIteratorHasNext
, relationIteratorNext
, allocTuple
, addTuple
, containsTuple
, tuplePushInt
, tuplePushString
, tuplePopInt
, tuplePopString
) where
import Prelude hiding ( init )
import Data.Functor ( (<&>) )
import Data.Word
import Data.Int
import Foreign.Marshal.Alloc
import Foreign.Storable
import Foreign.C.String
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Ptr
import qualified Language.Souffle.Internal.Bindings as Bindings
import Language.Souffle.Internal.Bindings
( Souffle, Relation, RelationIterator, Tuple )
init :: String -> IO (Maybe (ForeignPtr Souffle))
init :: String -> IO (Maybe (ForeignPtr Souffle))
init prog :: String
prog = do
Ptr Souffle
ptr <- String -> (CString -> IO (Ptr Souffle)) -> IO (Ptr Souffle)
forall a. String -> (CString -> IO a) -> IO a
withCString String
prog CString -> IO (Ptr Souffle)
Bindings.init
if Ptr Souffle
ptr Ptr Souffle -> Ptr Souffle -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Souffle
forall a. Ptr a
nullPtr
then Maybe (ForeignPtr Souffle) -> IO (Maybe (ForeignPtr Souffle))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (ForeignPtr Souffle)
forall a. Maybe a
Nothing
else ForeignPtr Souffle -> Maybe (ForeignPtr Souffle)
forall a. a -> Maybe a
Just (ForeignPtr Souffle -> Maybe (ForeignPtr Souffle))
-> IO (ForeignPtr Souffle) -> IO (Maybe (ForeignPtr Souffle))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FinalizerPtr Souffle -> Ptr Souffle -> IO (ForeignPtr Souffle)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr Souffle
Bindings.free Ptr Souffle
ptr
{-# INLINABLE init #-}
setNumThreads :: ForeignPtr Souffle -> Word64 -> IO ()
setNumThreads :: ForeignPtr Souffle -> Word64 -> IO ()
setNumThreads prog :: ForeignPtr Souffle
prog numThreads :: Word64
numThreads = ForeignPtr Souffle -> (Ptr Souffle -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Souffle
prog ((Ptr Souffle -> IO ()) -> IO ())
-> (Ptr Souffle -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Souffle
ptr ->
Ptr Souffle -> CSize -> IO ()
Bindings.setNumThreads Ptr Souffle
ptr (CSize -> IO ()) -> CSize -> IO ()
forall a b. (a -> b) -> a -> b
$ Word64 -> CSize
CSize Word64
numThreads
{-# INLINABLE setNumThreads #-}
getNumThreads :: ForeignPtr Souffle -> IO Word64
getNumThreads :: ForeignPtr Souffle -> IO Word64
getNumThreads prog :: ForeignPtr Souffle
prog = ForeignPtr Souffle -> (Ptr Souffle -> IO Word64) -> IO Word64
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Souffle
prog ((Ptr Souffle -> IO Word64) -> IO Word64)
-> (Ptr Souffle -> IO Word64) -> IO Word64
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Souffle
ptr -> do
(CSize numThreads :: Word64
numThreads) <- Ptr Souffle -> IO CSize
Bindings.getNumThreads Ptr Souffle
ptr
Word64 -> IO Word64
forall (f :: * -> *) a. Applicative f => a -> f a
pure Word64
numThreads
{-# INLINABLE getNumThreads #-}
run :: ForeignPtr Souffle -> IO ()
run :: ForeignPtr Souffle -> IO ()
run prog :: ForeignPtr Souffle
prog = ForeignPtr Souffle -> (Ptr Souffle -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Souffle
prog Ptr Souffle -> IO ()
Bindings.run
{-# INLINABLE run #-}
loadAll :: ForeignPtr Souffle -> FilePath -> IO ()
loadAll :: ForeignPtr Souffle -> String -> IO ()
loadAll prog :: ForeignPtr Souffle
prog str :: String
str = ForeignPtr Souffle -> (Ptr Souffle -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Souffle
prog ((Ptr Souffle -> IO ()) -> IO ())
-> (Ptr Souffle -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> (CString -> IO ()) -> IO ()
forall a. String -> (CString -> IO a) -> IO a
withCString String
str ((CString -> IO ()) -> IO ())
-> (Ptr Souffle -> CString -> IO ()) -> Ptr Souffle -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Souffle -> CString -> IO ()
Bindings.loadAll
{-# INLINABLE loadAll #-}
printAll :: ForeignPtr Souffle -> IO ()
printAll :: ForeignPtr Souffle -> IO ()
printAll prog :: ForeignPtr Souffle
prog = ForeignPtr Souffle -> (Ptr Souffle -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Souffle
prog Ptr Souffle -> IO ()
Bindings.printAll
{-# INLINABLE printAll #-}
getRelation :: ForeignPtr Souffle -> String -> IO (Ptr Relation)
getRelation :: ForeignPtr Souffle -> String -> IO (Ptr Relation)
getRelation prog :: ForeignPtr Souffle
prog relation :: String
relation = ForeignPtr Souffle
-> (Ptr Souffle -> IO (Ptr Relation)) -> IO (Ptr Relation)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Souffle
prog ((Ptr Souffle -> IO (Ptr Relation)) -> IO (Ptr Relation))
-> (Ptr Souffle -> IO (Ptr Relation)) -> IO (Ptr Relation)
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Souffle
ptr ->
String -> (CString -> IO (Ptr Relation)) -> IO (Ptr Relation)
forall a. String -> (CString -> IO a) -> IO a
withCString String
relation ((CString -> IO (Ptr Relation)) -> IO (Ptr Relation))
-> (CString -> IO (Ptr Relation)) -> IO (Ptr Relation)
forall a b. (a -> b) -> a -> b
$ Ptr Souffle -> CString -> IO (Ptr Relation)
Bindings.getRelation Ptr Souffle
ptr
{-# INLINABLE getRelation #-}
getRelationIterator :: Ptr Relation -> IO (ForeignPtr RelationIterator)
getRelationIterator :: Ptr Relation -> IO (ForeignPtr RelationIterator)
getRelationIterator relation :: Ptr Relation
relation =
Ptr Relation -> IO (Ptr RelationIterator)
Bindings.getRelationIterator Ptr Relation
relation IO (Ptr RelationIterator)
-> (Ptr RelationIterator -> IO (ForeignPtr RelationIterator))
-> IO (ForeignPtr RelationIterator)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FinalizerPtr RelationIterator
-> Ptr RelationIterator -> IO (ForeignPtr RelationIterator)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr RelationIterator
Bindings.freeRelationIterator
{-# INLINABLE getRelationIterator #-}
relationIteratorHasNext :: ForeignPtr RelationIterator -> IO Bool
relationIteratorHasNext :: ForeignPtr RelationIterator -> IO Bool
relationIteratorHasNext iter :: ForeignPtr RelationIterator
iter = ForeignPtr RelationIterator
-> (Ptr RelationIterator -> IO Bool) -> IO Bool
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr RelationIterator
iter ((Ptr RelationIterator -> IO Bool) -> IO Bool)
-> (Ptr RelationIterator -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr RelationIterator
ptr ->
Ptr RelationIterator -> IO CBool
Bindings.relationIteratorHasNext Ptr RelationIterator
ptr IO CBool -> (CBool -> Bool) -> IO Bool
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
CBool 0 -> Bool
False
CBool _ -> Bool
True
{-# INLINABLE relationIteratorHasNext #-}
relationIteratorNext :: ForeignPtr RelationIterator -> IO (Ptr Tuple)
relationIteratorNext :: ForeignPtr RelationIterator -> IO (Ptr Tuple)
relationIteratorNext iter :: ForeignPtr RelationIterator
iter = ForeignPtr RelationIterator
-> (Ptr RelationIterator -> IO (Ptr Tuple)) -> IO (Ptr Tuple)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr RelationIterator
iter Ptr RelationIterator -> IO (Ptr Tuple)
Bindings.relationIteratorNext
{-# INLINABLE relationIteratorNext #-}
allocTuple :: Ptr Relation -> IO (ForeignPtr Tuple)
allocTuple :: Ptr Relation -> IO (ForeignPtr Tuple)
allocTuple relation :: Ptr Relation
relation =
Ptr Relation -> IO (Ptr Tuple)
Bindings.allocTuple Ptr Relation
relation IO (Ptr Tuple)
-> (Ptr Tuple -> IO (ForeignPtr Tuple)) -> IO (ForeignPtr Tuple)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FinalizerPtr Tuple -> Ptr Tuple -> IO (ForeignPtr Tuple)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr Tuple
Bindings.freeTuple
{-# INLINABLE allocTuple #-}
addTuple :: Ptr Relation -> ForeignPtr Tuple -> IO ()
addTuple :: Ptr Relation -> ForeignPtr Tuple -> IO ()
addTuple relation :: Ptr Relation
relation tuple :: ForeignPtr Tuple
tuple =
ForeignPtr Tuple -> (Ptr Tuple -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Tuple
tuple ((Ptr Tuple -> IO ()) -> IO ()) -> (Ptr Tuple -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr Relation -> Ptr Tuple -> IO ()
Bindings.addTuple Ptr Relation
relation
{-# INLINABLE addTuple #-}
containsTuple :: Ptr Relation -> ForeignPtr Tuple -> IO Bool
containsTuple :: Ptr Relation -> ForeignPtr Tuple -> IO Bool
containsTuple relation :: Ptr Relation
relation tuple :: ForeignPtr Tuple
tuple = ForeignPtr Tuple -> (Ptr Tuple -> IO Bool) -> IO Bool
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Tuple
tuple ((Ptr Tuple -> IO Bool) -> IO Bool)
-> (Ptr Tuple -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Tuple
ptr ->
Ptr Relation -> Ptr Tuple -> IO CBool
Bindings.containsTuple Ptr Relation
relation Ptr Tuple
ptr IO CBool -> (CBool -> Bool) -> IO Bool
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
CBool 0 -> Bool
False
CBool _ -> Bool
True
{-# INLINABLE containsTuple #-}
tuplePushInt :: Ptr Tuple -> Int32 -> IO ()
tuplePushInt :: Ptr Tuple -> Int32 -> IO ()
tuplePushInt tuple :: Ptr Tuple
tuple i :: Int32
i = Ptr Tuple -> CInt -> IO ()
Bindings.tuplePushInt Ptr Tuple
tuple (Int32 -> CInt
CInt Int32
i)
{-# INLINABLE tuplePushInt #-}
tuplePushString :: Ptr Tuple -> String -> IO ()
tuplePushString :: Ptr Tuple -> String -> IO ()
tuplePushString tuple :: Ptr Tuple
tuple str :: String
str =
String -> (CString -> IO ()) -> IO ()
forall a. String -> (CString -> IO a) -> IO a
withCString String
str ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr Tuple -> CString -> IO ()
Bindings.tuplePushString Ptr Tuple
tuple
{-# INLINABLE tuplePushString #-}
tuplePopInt :: Ptr Tuple -> IO Int32
tuplePopInt :: Ptr Tuple -> IO Int32
tuplePopInt tuple :: Ptr Tuple
tuple = (Ptr CInt -> IO Int32) -> IO Int32
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO Int32) -> IO Int32)
-> (Ptr CInt -> IO Int32) -> IO Int32
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr CInt
ptr -> do
Ptr Tuple -> Ptr CInt -> IO ()
Bindings.tuplePopInt Ptr Tuple
tuple Ptr CInt
ptr
(CInt res :: Int32
res) <- Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
ptr
Int32 -> IO Int32
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int32
res
{-# INLINABLE tuplePopInt #-}
tuplePopString :: Ptr Tuple -> IO String
tuplePopString :: Ptr Tuple -> IO String
tuplePopString tuple :: Ptr Tuple
tuple = (Ptr CString -> IO String) -> IO String
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CString -> IO String) -> IO String)
-> (Ptr CString -> IO String) -> IO String
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr CString
ptr -> do
Ptr Tuple -> Ptr CString -> IO ()
Bindings.tuplePopString Ptr Tuple
tuple Ptr CString
ptr
CString
cstr <- Ptr CString -> IO CString
forall a. Storable a => Ptr a -> IO a
peek Ptr CString
ptr
String
str <- CString -> IO String
peekCString CString
cstr
CString -> IO ()
forall a. Ptr a -> IO ()
free CString
cstr
String -> IO String
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
str
{-# INLINABLE tuplePopString #-}