-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathInternal.hs
More file actions
128 lines (107 loc) · 4.23 KB
/
Internal.hs
File metadata and controls
128 lines (107 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- | An internal module, providing a slightly higher level interface than
-- "Language.Souffle.Internal.Bindings".
-- It uses more commonly found data types instead of the low level C types
-- for easier integration with other parts of a Haskell application.
-- Also it takes care of garbage collection so other modules do not have
-- to take this into account anymore.
--
-- Used only internally, so prone to changes, use at your own risk.
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_)
{- | Initializes a Souffle program.
The string argument is the name of the program and should be the same
as the filename (minus the .dl extension).
The action will return 'Nothing' if it failed to load the Souffle program.
Otherwise it will return a pointer that can be used in other functions
in this module.
-}
init :: String -> IO (Maybe (ForeignPtr Souffle))
init prog = mask_ $ do
ptr <- withCString prog Bindings.init
if ptr == nullPtr
then pure Nothing
else Just <$> newForeignPtr Bindings.free ptr
{-# INLINABLE init #-}
-- | Sets the number of CPU cores this Souffle program should use.
setNumThreads :: ForeignPtr Souffle -> Word64 -> IO ()
setNumThreads prog numThreads = withForeignPtr prog $ \ptr ->
Bindings.setNumThreads ptr $ CSize numThreads
{-# INLINABLE setNumThreads #-}
-- | Gets the number of CPU cores this Souffle program should use.
getNumThreads :: ForeignPtr Souffle -> IO Word64
getNumThreads prog = withForeignPtr prog $ \ptr -> do
(CSize numThreads) <- Bindings.getNumThreads ptr
pure numThreads
{-# INLINABLE getNumThreads #-}
-- | Runs the Souffle program.
run :: ForeignPtr Souffle -> IO ()
run prog = withForeignPtr prog Bindings.run
{-# INLINABLE run #-}
-- | Load all facts from files in a certain directory.
loadAll :: ForeignPtr Souffle -> FilePath -> IO ()
loadAll prog inputDir = withForeignPtr prog $ withCString inputDir . Bindings.loadAll
{-# INLINABLE loadAll #-}
-- | Write out all facts of the program to CSV files in a certain directory
-- (as defined in the Souffle program).
printAll :: ForeignPtr Souffle -> FilePath -> IO ()
printAll prog outputDir = withForeignPtr prog $ withCString outputDir . Bindings.printAll
{-# INLINABLE printAll #-}
{-| Lookup a relation by name in the Souffle program.
Note that the returned pointer can be 'nullPtr' if it is not defined
in the Souffle program.
-}
getRelation :: ForeignPtr Souffle -> String -> IO (Ptr Relation)
getRelation prog relation = withForeignPtr prog $ \ptr ->
withCString relation $ Bindings.getRelation ptr
{-# INLINABLE getRelation #-}
{-| Serializes many facts from Datalog to Haskell.
You need to check if the passed pointers are non-NULL before passing it
to this function. Not doing so results in undefined behavior.
Passing in a different count of objects to what is actually inside the
byte buffer will crash.
-}
pushFacts :: Ptr Relation -> Ptr ByteBuf -> Word64 -> IO ()
pushFacts relation buf x =
Bindings.pushByteBuf relation buf (CSize x)
{-# INLINABLE pushFacts #-}
{-| Serializes many facts from Haskell to Datalog.
You need to check if the passed pointer is non-NULL before passing it
to this function. Not doing so results in undefined behavior.
Returns a pointer to a byte buffer that contains the serialized Datalog facts.
-}
popFacts :: Ptr Souffle -> Ptr Relation -> IO (Ptr ByteBuf)
popFacts = Bindings.popByteBuf
{-# INLINABLE popFacts #-}
{- | Checks if a relation contains a certain tuple.
Returns True if the tuple was found in the relation; otherwise False.
-}
containsFact :: Ptr Relation -> Ptr ByteBuf -> IO Bool
containsFact relation buf =
Bindings.containsTuple relation buf <&> \case
CBool 0 -> False
CBool _ -> True
{-# INLINABLE containsFact #-}