module Language.Souffle.Internal
( Souffle
, Relation
, ByteBuf
, init
, setNumThreads
, getNumThreads
, run
, loadAll
, printAll
, getRelation
, pushFacts
, popFacts
, containsFact
) where
import Prelude hiding ( init )
import Data.Functor ( (<&>) )
import Data.Word
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, ByteBuf )
import Control.Exception (mask_)
init :: String -> IO (Maybe (ForeignPtr Souffle))
init :: String -> IO (Maybe (ForeignPtr Souffle))
init prog :: String
prog = IO (Maybe (ForeignPtr Souffle)) -> IO (Maybe (ForeignPtr Souffle))
forall a. IO a -> IO a
mask_ (IO (Maybe (ForeignPtr Souffle))
-> IO (Maybe (ForeignPtr Souffle)))
-> IO (Maybe (ForeignPtr Souffle))
-> IO (Maybe (ForeignPtr Souffle))
forall a b. (a -> b) -> a -> b
$ 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 inputDir :: String
inputDir = 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
inputDir ((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 -> FilePath -> IO ()
printAll :: ForeignPtr Souffle -> String -> IO ()
printAll prog :: ForeignPtr Souffle
prog outputDir :: String
outputDir = 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
outputDir ((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.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 #-}
pushFacts :: Ptr Relation -> Ptr ByteBuf -> Word64 -> IO ()
pushFacts :: Ptr Relation -> Ptr ByteBuf -> Word64 -> IO ()
pushFacts relation :: Ptr Relation
relation buf :: Ptr ByteBuf
buf x :: Word64
x =
Ptr Relation -> Ptr ByteBuf -> CSize -> IO ()
Bindings.pushByteBuf Ptr Relation
relation Ptr ByteBuf
buf (Word64 -> CSize
CSize Word64
x)
{-# INLINABLE pushFacts #-}
popFacts :: Ptr Souffle -> Ptr Relation -> IO (Ptr ByteBuf)
popFacts :: Ptr Souffle -> Ptr Relation -> IO (Ptr ByteBuf)
popFacts = Ptr Souffle -> Ptr Relation -> IO (Ptr ByteBuf)
Bindings.popByteBuf
{-# INLINABLE popFacts #-}
containsFact :: Ptr Relation -> Ptr ByteBuf -> IO Bool
containsFact :: Ptr Relation -> Ptr ByteBuf -> IO Bool
containsFact relation :: Ptr Relation
relation buf :: Ptr ByteBuf
buf =
Ptr Relation -> Ptr ByteBuf -> IO CBool
Bindings.containsTuple Ptr Relation
relation Ptr ByteBuf
buf 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 containsFact #-}