mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-20 08:32:50 -06:00
added functionType in the Haskell API to the C runtime
This commit is contained in:
@@ -12,11 +12,12 @@
|
|||||||
#include <gu/enum.h>
|
#include <gu/enum.h>
|
||||||
#include <gu/exn.h>
|
#include <gu/exn.h>
|
||||||
|
|
||||||
module PGF2 (-- * PGF
|
module PGF2 (-- * CId
|
||||||
|
CId,
|
||||||
|
-- * PGF
|
||||||
PGF,readPGF,AbsName,abstractName,startCat,
|
PGF,readPGF,AbsName,abstractName,startCat,
|
||||||
-- * Concrete syntax
|
-- * Concrete syntax
|
||||||
ConcName,Concr,Cat,languages,parse,parseWithHeuristics,linearize,
|
Concr,languages,parse,parseWithHeuristics,linearize,alignWords,
|
||||||
alignWords,
|
|
||||||
-- * Trees
|
-- * Trees
|
||||||
Expr,Fun,readExpr,showExpr,mkApp,unApp,mkStr,
|
Expr,Fun,readExpr,showExpr,mkApp,unApp,mkStr,
|
||||||
-- * Morphology
|
-- * Morphology
|
||||||
@@ -42,6 +43,7 @@ import Data.Char(isUpper,isSpace)
|
|||||||
import Data.List(isSuffixOf,maximumBy)
|
import Data.List(isSuffixOf,maximumBy)
|
||||||
import Data.Function(on)
|
import Data.Function(on)
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
-- Functions that take a PGF.
|
-- Functions that take a PGF.
|
||||||
-- PGF has many Concrs.
|
-- PGF has many Concrs.
|
||||||
@@ -79,7 +81,7 @@ readPGF fpath =
|
|||||||
master <- newForeignPtr gu_pool_finalizer pool
|
master <- newForeignPtr gu_pool_finalizer pool
|
||||||
return PGF {pgf = pgf, pgfMaster = master}
|
return PGF {pgf = pgf, pgfMaster = master}
|
||||||
|
|
||||||
languages :: PGF -> Map.Map ConcName Concr
|
languages :: PGF -> Map.Map String Concr
|
||||||
languages p =
|
languages p =
|
||||||
unsafePerformIO $
|
unsafePerformIO $
|
||||||
do ref <- newIORef Map.empty
|
do ref <- newIORef Map.empty
|
||||||
@@ -108,10 +110,10 @@ generateAll p cat =
|
|||||||
exprFPl <- newForeignPtr gu_pool_finalizer exprPl
|
exprFPl <- newForeignPtr gu_pool_finalizer exprPl
|
||||||
fromPgfExprEnum enum genFPl (p,exprFPl)
|
fromPgfExprEnum enum genFPl (p,exprFPl)
|
||||||
|
|
||||||
abstractName :: PGF -> AbsName
|
abstractName :: PGF -> String
|
||||||
abstractName p = unsafePerformIO (peekCString =<< pgf_abstract_name (pgf p))
|
abstractName p = unsafePerformIO (peekCString =<< pgf_abstract_name (pgf p))
|
||||||
|
|
||||||
startCat :: PGF -> Cat
|
startCat :: PGF -> String
|
||||||
startCat p = unsafePerformIO (peekCString =<< pgf_start_cat (pgf p))
|
startCat p = unsafePerformIO (peekCString =<< pgf_start_cat (pgf p))
|
||||||
|
|
||||||
loadConcr :: Concr -> FilePath -> IO ()
|
loadConcr :: Concr -> FilePath -> IO ()
|
||||||
@@ -136,6 +138,55 @@ loadConcr c fpath =
|
|||||||
unloadConcr :: Concr -> IO ()
|
unloadConcr :: Concr -> IO ()
|
||||||
unloadConcr c = pgf_concrete_unload (concr c)
|
unloadConcr c = pgf_concrete_unload (concr c)
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Types
|
||||||
|
|
||||||
|
data Type =
|
||||||
|
DTyp [Hypo] CId [Expr]
|
||||||
|
|
||||||
|
data BindType =
|
||||||
|
Explicit
|
||||||
|
| Implicit
|
||||||
|
|
||||||
|
-- | 'Hypo' represents a hypothesis in a type i.e. in the type A -> B, A is the hypothesis
|
||||||
|
type Hypo = (BindType,CId,Type)
|
||||||
|
|
||||||
|
functionType :: PGF -> CId -> Type
|
||||||
|
functionType p fn =
|
||||||
|
unsafePerformIO $
|
||||||
|
withCString fn $ \c_fn -> do
|
||||||
|
c_type <- pgf_function_type (pgf p) c_fn
|
||||||
|
peekType c_type
|
||||||
|
where
|
||||||
|
peekType c_type = do
|
||||||
|
cid <- (#peek PgfType, cid) c_type >>= peekCString
|
||||||
|
c_hypos <- (#peek PgfType, hypos) c_type
|
||||||
|
n_hypos <- (#peek GuSeq, len) c_hypos
|
||||||
|
hs <- peekHypos (c_hypos `plusPtr` (#offset GuSeq, data)) 0 n_hypos
|
||||||
|
n_exprs <- (#peek PgfType, n_exprs) c_type
|
||||||
|
es <- peekExprs (c_type `plusPtr` (#offset PgfType, exprs)) 0 n_exprs
|
||||||
|
return (DTyp hs cid es)
|
||||||
|
|
||||||
|
peekHypos :: Ptr a -> Int -> Int -> IO [Hypo]
|
||||||
|
peekHypos c_hypo i n
|
||||||
|
| i < n = do cid <- (#peek PgfHypo, cid) c_hypo >>= peekCString
|
||||||
|
ty <- (#peek PgfHypo, type) c_hypo >>= peekType
|
||||||
|
bt <- fmap toBindType ((#peek PgfHypo, bind_type) c_hypo)
|
||||||
|
hs <- peekHypos (plusPtr c_hypo (#size PgfHypo)) (i+1) n
|
||||||
|
return ((bt,cid,ty) : hs)
|
||||||
|
| otherwise = return []
|
||||||
|
|
||||||
|
toBindType :: Int -> BindType
|
||||||
|
toBindType (#const PGF_BIND_TYPE_EXPLICIT) = Explicit
|
||||||
|
toBindType (#const PGF_BIND_TYPE_IMPLICIT) = Implicit
|
||||||
|
|
||||||
|
peekExprs ptr i n
|
||||||
|
| i < n = do e <- peekElemOff ptr i
|
||||||
|
es <- peekExprs ptr (i+1) n
|
||||||
|
return (Expr e p : es)
|
||||||
|
| otherwise = return []
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Expressions
|
-- Expressions
|
||||||
|
|
||||||
@@ -149,7 +200,7 @@ data Expr = forall a . Expr {expr :: PgfExpr, exprMaster :: a}
|
|||||||
instance Show Expr where
|
instance Show Expr where
|
||||||
show = showExpr
|
show = showExpr
|
||||||
|
|
||||||
mkApp :: Fun -> [Expr] -> Expr
|
mkApp :: String -> [Expr] -> Expr
|
||||||
mkApp fun args =
|
mkApp fun args =
|
||||||
unsafePerformIO $
|
unsafePerformIO $
|
||||||
withCString fun $ \cfun ->
|
withCString fun $ \cfun ->
|
||||||
@@ -164,7 +215,7 @@ mkApp fun args =
|
|||||||
where
|
where
|
||||||
len = length args
|
len = length args
|
||||||
|
|
||||||
unApp :: Expr -> Maybe (Fun,[Expr])
|
unApp :: Expr -> Maybe (String,[Expr])
|
||||||
unApp (Expr expr master) =
|
unApp (Expr expr master) =
|
||||||
unsafePerformIO $
|
unsafePerformIO $
|
||||||
withGuPool $ \pl -> do
|
withGuPool $ \pl -> do
|
||||||
@@ -218,7 +269,7 @@ showExpr e =
|
|||||||
-- Functions using Concr
|
-- Functions using Concr
|
||||||
-- Morpho analyses, parsing & linearization
|
-- Morpho analyses, parsing & linearization
|
||||||
|
|
||||||
type MorphoAnalysis = (Fun,String,Float)
|
type MorphoAnalysis = (String,String,Float)
|
||||||
|
|
||||||
lookupMorpho :: Concr -> String -> [MorphoAnalysis]
|
lookupMorpho :: Concr -> String -> [MorphoAnalysis]
|
||||||
lookupMorpho (Concr concr master) sent = unsafePerformIO $
|
lookupMorpho (Concr concr master) sent = unsafePerformIO $
|
||||||
@@ -265,11 +316,11 @@ getAnalysis ref self c_lemma c_anal prob exn = do
|
|||||||
anal <- peekCString c_anal
|
anal <- peekCString c_anal
|
||||||
writeIORef ref ((lemma, anal, prob):ans)
|
writeIORef ref ((lemma, anal, prob):ans)
|
||||||
|
|
||||||
parse :: Concr -> Cat -> String -> Either String [(Expr,Float)]
|
parse :: Concr -> String -> String -> Either String [(Expr,Float)]
|
||||||
parse lang cat sent = parseWithHeuristics lang cat sent (-1.0) []
|
parse lang cat sent = parseWithHeuristics lang cat sent (-1.0) []
|
||||||
|
|
||||||
parseWithHeuristics :: Concr -- ^ the language with which we parse
|
parseWithHeuristics :: Concr -- ^ the language with which we parse
|
||||||
-> Cat -- ^ the start category
|
-> String -- ^ the start category
|
||||||
-> String -- ^ the input sentence
|
-> String -- ^ the input sentence
|
||||||
-> Double -- ^ the heuristic factor.
|
-> Double -- ^ the heuristic factor.
|
||||||
-- A negative value tells the parser
|
-- A negative value tells the parser
|
||||||
|
|||||||
Reference in New Issue
Block a user