summaryrefslogtreecommitdiff
path: root/src/runtime/haskell-bind/CRuntimeFFI.hsc
blob: 24329c319bb0406e48fb0f1c7b0931a1c0a13848 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
{-# LANGUAGE ForeignFunctionInterface, ExistentialQuantification, TypeSynonymInstances, FlexibleInstances #-}
#include <pgf/pgf.h>
#include <gu/enum.h>
#include <gu/exn.h>

module CRuntimeFFI(-- * PGF
                   PGF,readPGF,abstractName,startCat,
                   -- * Concrete syntax
                   Concr,Language,{-languages,-}getConcr,parse,linearize,
                   -- * Trees
                   Expr,Tree,readExpr,showExpr,unApp,
                   -- * Morphology
                   MorphoAnalysis,lookupMorpho,fullFormLexicon,
                   printLexEntry,
                   -- * Don't export these for real, just for testing
                   generateAll, printGrammar
                   ) where

import Prelude hiding (fromEnum)
import Control.Exception
import System.IO
import System.IO.Unsafe
import CId (CId(..), 
            mkCId, wildCId,
            readCId, showCId)
import Gu
import PgfLow

import Foreign hiding ( Pool, newPool, unsafePerformIO )
import Foreign.C
import Foreign.C.String
import Foreign.Ptr


import Data.Char
import qualified Data.ByteString as BS
import Data.IORef


-----------------------------------------------------------------------------
-- How to compile
-- hsc2hs Gu.hsc CRuntimeFFI.hsc -v --cflag="-std=c99" && ghc -lpgf -lgu --make CRuntimeFFI 
-----------------------------------------------------------------------------
-- Mindless copypasting and translating of the C functions in Gu.hsc and PgfLow.hs
-- More user-friendly functions here 

-----------------------------------------------------------------------------
--Memory management, pools and outs
type Pool = ForeignPtr GuPool
type Out = (Ptr GuStringBuf, Ptr GuOut)

     
--Not used anymore, see withGuPool in Gu.hsc
newPool :: IO Pool
newPool =
  do pl <- gu_new_pool
     newForeignPtr_ pl --gu_pool_free_ptr pl

--when you create a GuOut, you create also a GuStringBuf
--and when you give GuOut to a function that outputs something,
--the result goes into that GuStringBuf
newOut :: Ptr GuPool -> IO Out 
newOut pool =
   do sb <- gu_string_buf pool
      out <- gu_string_buf_out sb
      return (sb,out)
--Don't create newOut using withGuPool inside
--Rather do like this:
{-
withGuPool $ \pl ->
  do out <- newOut pl
     <other stuff>
-}
  -- withGuPool $ \pl -> 
  --   do sb <- gu_string_buf pl
  --      out <- gu_string_buf_out sb
  --      return (sb,out)


-----------------------------------------------------------------------------
-- Functions that take a PGF.
-- PGF has many Concrs.
-- A Concr retains its PGF in a field (memory management reasons?)

data PGF = PGF {pgfPool :: Ptr GuPool, pgf :: Ptr PgfPGF} deriving Show
data Concr = Concr {concr :: (Ptr PgfConcr), concrMaster :: PGF}
type Language = CId

readPGF :: FilePath -> IO PGF
readPGF filepath =
  do pl <- gu_new_pool
     pgf <- withCString filepath $ \file -> 
              pgf_read file pl nullPtr
     return PGF {pgfPool = pl, pgf = pgf}


getConcr :: PGF -> Language -> Maybe Concr
getConcr p (CId lang) = unsafePerformIO $
    BS.useAsCString lang $ \lng -> do
        cnc <- pgf_get_language (pgf p) lng
        return (if cnc==nullPtr then Nothing else Just (Concr cnc p))



-- languages :: PGF -> [Concr]
-- languages p = undefined
--TODO 
-- void pgf_iter_languages(PgfPGF* pgf, GuMapItor* fn, GuExn* err)
-- {
-- 	gu_map_iter(pgf->concretes, fn, err);
-- }

generateAll :: PGF -> CId -> [(Tree,Float)]
generateAll p (CId cat) = unsafePerformIO $
  withGuPool $ \iterPl ->
--    withGuPool $ \exprPl -> --segfaults if I use this
      do exprPl <- gu_new_pool
         pgfExprs <- BS.useAsCString cat $ \cat ->
                     pgf_generate_all (pgf p) cat exprPl --this pool isn't freed. segfaults if I try.
         fromPgfExprEnum pgfExprs iterPl p --this pool is freed afterwards. it's used in fromPgfExprEnum, and I imagine it makes more sense to give a pool as an argument, rather than in that function create and free new pools in its body (it calls itself recursively)
         

abstractName :: PGF -> Language
abstractName p = unsafePerformIO $ fmap CId (BS.packCString =<< pgf_abstract_name (pgf p))

startCat :: PGF -> CId
startCat p = unsafePerformIO $ fmap CId (BS.packCString =<< pgf_start_cat (pgf p))

printGrammar :: PGF -> String
printGrammar p = unsafePerformIO $
  withGuPool $ \outPl ->
    withGuPool $ \printPl ->
      do (sb,out) <- newOut outPl
         pgf_print (pgf p) out nullPtr --nullPtr is for exception
         grammar <- gu_string_buf_freeze sb printPl
         peekCString grammar
 

-----------------------------------------------------------------------------
-- Expressions

--exprMaster is one of the following: 
-- * PGF 
-- * pool from which the expr is allocated
-- * iterator from generateAll
-- TODO ask more about this design
-- the master of an Expr needs to be retained because of memory management (?)
data Expr = forall a . Expr {expr :: PgfExpr, exprMaster :: a}

instance Show Expr where
  show = showExpr

instance Eq Expr where
  (Expr e1 m1) == (Expr e2 m2) = e1 == e2

type Tree = Expr


unApp :: Expr -> Maybe (CId,[Expr])
unApp (Expr expr master) = unsafePerformIO $
  withGuPool $ \pl -> do
     pgfAppl <- pgf_expr_unapply expr pl
     if pgfAppl == nullPtr
       then return Nothing
       else do 
          fun <- peekCString =<< (#peek PgfApplication, fun) pgfAppl
          arity <- (#peek PgfApplication, n_args) pgfAppl :: IO CInt 
          pgfExprs <- ptrToList pgfAppl (fromIntegral arity) --CInt to Int
          --print (arity,fun)
          let args = [Expr a master | a<-pgfExprs]
          return $ Just (mkCId fun, args)

--Krasimir recommended not to use PgfApplication, but PgfExprApp instead.
--but then we found out that some of those functions don't behave nicely 
--with the FFI, so we need to use PgfApplication anyway, unless we do some
--C coding to make the C library nicer.


readExpr :: String -> Maybe Expr
readExpr str = unsafePerformIO $
 do exprPl <- gu_new_pool  --we return this pool with the Expr
    withGuPool $ \inPl ->   --these pools are freed right after
      withGuPool $ \exnPl ->
        withCString str $ \str ->
         do guin <- gu_string_in str inPl
            exn <- gu_new_exn nullPtr gu_type__type exnPl
            pgfExpr <- pgf_read_expr guin exprPl exn
            status <- gu_exn_is_raised exn
            if (status==False && pgfExpr /= nullPtr)
              then return $ Just (Expr pgfExpr exprPl)
              else do
                   gu_pool_free exprPl --if Expr is not returned, free pool
                   return Nothing

-- TODO: do we need 3 different pools for this?
showExpr :: Expr -> String
showExpr e = unsafePerformIO $
  withGuPool $ \outPl ->
   withGuPool $ \exnPl ->
     withGuPool $ \printPl ->
       do (sb,out) <- newOut outPl
          let printCtxt = nullPtr
          exn <- gu_new_exn nullPtr gu_type__type exnPl
          pgf_print_expr (expr e) printCtxt 1 out exn
          abstree <- gu_string_buf_freeze sb printPl
          peekCString abstree


-----------------------------------------------------------------------------
-- Functions using Concr
-- Morpho analyses, parsing & linearization

type MorphoAnalysis = (CId,String,Float)


--There is no buildMorpho in the C library, just a lookupMorpho from a Concr
lookupMorpho :: Concr -> String -> [MorphoAnalysis]
lookupMorpho (Concr concr master) sent = unsafePerformIO $
  do ref <- newIORef []
     allocaBytes (#size PgfMorphoCallback) $ \cback -> 
                        do fptr <- wrapLookupMorpho (getAnalysis ref)
                           (#poke PgfMorphoCallback, callback) cback fptr
                           withCString sent $ \sent ->
                             pgf_lookup_morpho concr sent cback nullPtr

     readIORef ref
  where 
    getAnalysis :: IORef [MorphoAnalysis] -> Ptr PgfMorphoCallback -> CString -> CString -> Float -> Ptr GuExn -> IO () --IORef [(CId, String, Float)] -> Callback
    getAnalysis ref self clemma canal prob exn = do
      ans <- readIORef ref
      lemma <- fmap CId (BS.packCString clemma)
      anal  <- peekCString canal
      writeIORef ref ((lemma, anal, prob):ans)

fullFormLexicon :: Concr -> [(String, [MorphoAnalysis])]
fullFormLexicon lang = 
  let lexicon  = fullformLexicon' lang
      analyses = map (lookupMorpho lang) lexicon
  in  zip lexicon analyses
  where fullformLexicon' :: Concr -> [String]
        fullformLexicon' lang = unsafePerformIO $
          withGuPool $ \iterPl -> 
            do pl <- gu_new_pool
               lexEnum <- pgf_fullform_lexicon (concr lang) pl
               fromFullFormEntry lexEnum pl (concrMaster lang)
--Something weird happens if I use iterPl ^- here

printLexEntry :: (String, [MorphoAnalysis]) -> String
printLexEntry (lemma, anals) = 
  "Lemma: " ++ lemma ++ "\nAnalyses: " ++ show anals ++ "\n" -- map show' anals
--      where show' :: MorphoAnalysis -> String
--            show' (id,anal,prob) = showCId id ++ ", " ++ anal ++ ", " ++ show prob ++ "\n"


--Note: unlike in Haskell library, we give Concr -> ... and not PGF -> Lang -> ...
--Also this returns a list of tuples (tree,prob) instead of just trees
parse :: Concr -> CId -> String -> [(Tree,Float)]
parse (Concr lang master) (CId cat) sent = unsafePerformIO $
   withGuPool $ \iterPl ->   -- this pool will get freed eventually
     do inpool <- gu_new_pool  --these pools not ???
        outpool <- gu_new_pool --if I add them into withGuPool, I get segfault
        treesEnum <- parse_ lang cat sent inpool outpool
        fromPgfExprEnum treesEnum iterPl master --see previous fromPgfExprEnum comment, why giving a pool as an argument here instead of creating them in fromPgfExprEnum
  where  
    --tried adding withGuPool stuff inside here, segfaults as well
    parse_ :: Ptr PgfConcr -> BS.ByteString -> String -> Ptr GuPool -> Ptr GuPool -> IO (Ptr PgfExprEnum)
    parse_ pgfcnc cat sent inpool outpool =
      do BS.useAsCString cat $ \cat ->
           withCString sent $ \sent ->
             pgf_parse pgfcnc cat sent nullPtr inpool outpool


--In Haskell library, this function has type signature PGF -> Language -> Tree -> String
--Here we replace PGF -> Language with Concr
linearize :: Concr -> Tree -> String
linearize lang tree = unsafePerformIO $
  withGuPool $ \outPl ->
    withGuPool $ \linPl ->
     do (sb,out) <- newOut outPl
        pgf_linearize (concr lang) (expr tree) out nullPtr --linearization goes to stringbuf
        lin <- gu_string_buf_freeze sb linPl
        peekCString lin


-----------------------------------------------------------------------------
-- Helper functions

-- # syntax: http://www.haskell.org/ghc/docs/7.2.1/html/users_guide/hsc2hs.html
fromPgfExprEnum :: Ptr PgfExprEnum -> Ptr GuPool -> a -> IO [(Tree, Float)]
fromPgfExprEnum enum pl master = 
  do pgfExprProb <- alloca $ \ptr -> 
--                      withGuPool $ \pl -> 
                        do gu_enum_next enum ptr pl
                           peek ptr
     if pgfExprProb == nullPtr
       then return []
       else do expr <- (#peek PgfExprProb, expr) pgfExprProb
               prob <- (#peek PgfExprProb, prob) pgfExprProb
               ts <- unsafeInterleaveIO (fromPgfExprEnum enum pl master)
               return ((Expr expr master,prob) : ts)

--TODO
fromFullFormEntry :: Ptr GuEnum -> Ptr GuPool -> PGF -> IO [String]
fromFullFormEntry enum pl master = 
  do ffEntry <- alloca $ \ptr ->
                  do gu_enum_next enum ptr pl
                     peek ptr 
--   ffEntry :: Ptr PgfFullFormEntry
     if ffEntry == nullPtr
       then return []
       else do tok <- peekCString =<< pgf_fullform_get_string ffEntry
               toks <- unsafeInterleaveIO (fromFullFormEntry enum pl master)
               return (tok : toks)

-- fromFullFormEntry :: Ptr GuEnum -> PGF -> IO [String]
-- fromFullFormEntry enum master = 
--   do ffEntry <- alloca $ \ptr ->
--                   withGuPool $ \pl ->
--                     do gu_enum_next enum ptr pl
--                        peek ptr 
-- --   ffEntry :: Ptr PgfFullFormEntry
--      if ffEntry == nullPtr
--        then return []
--        else do tok <- peekCString =<< pgf_fullform_get_string ffEntry
--                toks <- unsafeInterleaveIO (fromFullFormEntry enum master)
--                return (tok : toks)