mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-24 03:52:50 -06:00
PGF2: introduced some type synonyms to make type signature more readable
This commit is contained in:
@@ -13,11 +13,12 @@
|
|||||||
#include <gu/exn.h>
|
#include <gu/exn.h>
|
||||||
|
|
||||||
module PGF2 (-- * PGF
|
module PGF2 (-- * PGF
|
||||||
PGF,readPGF,abstractName,startCat,
|
PGF,readPGF,AbsName,abstractName,startCat,
|
||||||
-- * Concrete syntax
|
-- * Concrete syntax
|
||||||
Concr,languages,parse,parseWithHeuristics,linearize,alignWords,
|
ConcName,Concr,Cat,languages,parse,parseWithHeuristics,linearize,
|
||||||
|
alignWords,
|
||||||
-- * Trees
|
-- * Trees
|
||||||
Expr,readExpr,showExpr,mkApp,unApp,mkStr,
|
Expr,Fun,readExpr,showExpr,mkApp,unApp,mkStr,
|
||||||
-- * Morphology
|
-- * Morphology
|
||||||
MorphoAnalysis, lookupMorpho, fullFormLexicon,
|
MorphoAnalysis, lookupMorpho, fullFormLexicon,
|
||||||
-- * Exceptions
|
-- * Exceptions
|
||||||
@@ -48,6 +49,11 @@ import Data.IORef
|
|||||||
data PGF = PGF {pgf :: Ptr PgfPGF, pgfMaster :: ForeignPtr GuPool}
|
data PGF = PGF {pgf :: Ptr PgfPGF, pgfMaster :: ForeignPtr GuPool}
|
||||||
data Concr = Concr {concr :: Ptr PgfConcr, concrMaster :: PGF}
|
data Concr = Concr {concr :: Ptr PgfConcr, concrMaster :: PGF}
|
||||||
|
|
||||||
|
type AbsName = String -- ^ Name of abstract syntax
|
||||||
|
type ConcName = String -- ^ Name of concrete syntax
|
||||||
|
type Cat = String -- ^ Name of syntactic category
|
||||||
|
type Fun = String -- ^ Name of function
|
||||||
|
|
||||||
readPGF :: FilePath -> IO PGF
|
readPGF :: FilePath -> IO PGF
|
||||||
readPGF fpath =
|
readPGF fpath =
|
||||||
do pool <- gu_new_pool
|
do pool <- gu_new_pool
|
||||||
@@ -69,7 +75,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 String Concr
|
languages :: PGF -> Map.Map ConcName Concr
|
||||||
languages p =
|
languages p =
|
||||||
unsafePerformIO $
|
unsafePerformIO $
|
||||||
do ref <- newIORef Map.empty
|
do ref <- newIORef Map.empty
|
||||||
@@ -87,7 +93,7 @@ languages p =
|
|||||||
concr <- fmap (\ptr -> Concr ptr p) $ peek (castPtr value)
|
concr <- fmap (\ptr -> Concr ptr p) $ peek (castPtr value)
|
||||||
writeIORef ref $! Map.insert name concr langs
|
writeIORef ref $! Map.insert name concr langs
|
||||||
|
|
||||||
generateAll :: PGF -> String -> [(Expr,Float)]
|
generateAll :: PGF -> Cat -> [(Expr,Float)]
|
||||||
generateAll p cat =
|
generateAll p cat =
|
||||||
unsafePerformIO $
|
unsafePerformIO $
|
||||||
do genPl <- gu_new_pool
|
do genPl <- gu_new_pool
|
||||||
@@ -98,10 +104,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 -> String
|
abstractName :: PGF -> AbsName
|
||||||
abstractName p = unsafePerformIO (peekCString =<< pgf_abstract_name (pgf p))
|
abstractName p = unsafePerformIO (peekCString =<< pgf_abstract_name (pgf p))
|
||||||
|
|
||||||
startCat :: PGF -> String
|
startCat :: PGF -> Cat
|
||||||
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 ()
|
||||||
@@ -139,7 +145,7 @@ data Expr = forall a . Expr {expr :: PgfExpr, exprMaster :: a}
|
|||||||
instance Show Expr where
|
instance Show Expr where
|
||||||
show = showExpr
|
show = showExpr
|
||||||
|
|
||||||
mkApp :: String -> [Expr] -> Expr
|
mkApp :: Fun -> [Expr] -> Expr
|
||||||
mkApp fun args =
|
mkApp fun args =
|
||||||
unsafePerformIO $
|
unsafePerformIO $
|
||||||
withCString fun $ \cfun ->
|
withCString fun $ \cfun ->
|
||||||
@@ -154,7 +160,7 @@ mkApp fun args =
|
|||||||
where
|
where
|
||||||
len = length args
|
len = length args
|
||||||
|
|
||||||
unApp :: Expr -> Maybe (String,[Expr])
|
unApp :: Expr -> Maybe (Fun,[Expr])
|
||||||
unApp (Expr expr master) =
|
unApp (Expr expr master) =
|
||||||
unsafePerformIO $
|
unsafePerformIO $
|
||||||
withGuPool $ \pl -> do
|
withGuPool $ \pl -> do
|
||||||
@@ -208,7 +214,7 @@ showExpr e =
|
|||||||
-- Functions using Concr
|
-- Functions using Concr
|
||||||
-- Morpho analyses, parsing & linearization
|
-- Morpho analyses, parsing & linearization
|
||||||
|
|
||||||
type MorphoAnalysis = (String,String,Float)
|
type MorphoAnalysis = (Fun,String,Float)
|
||||||
|
|
||||||
lookupMorpho :: Concr -> String -> [MorphoAnalysis]
|
lookupMorpho :: Concr -> String -> [MorphoAnalysis]
|
||||||
lookupMorpho (Concr concr master) sent = unsafePerformIO $
|
lookupMorpho (Concr concr master) sent = unsafePerformIO $
|
||||||
@@ -255,17 +261,17 @@ 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 -> String -> String -> Either String [(Expr,Float)]
|
parse :: Concr -> Cat -> 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
|
||||||
-> String -- ^ the start category
|
-> Cat -- ^ 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
|
||||||
-- to lookup up the default from
|
-- to lookup up the default from
|
||||||
-- the grammar flags
|
-- the grammar flags
|
||||||
-> [(String, Int -> String -> Int -> Maybe (Expr,Float,Int))]
|
-> [(Cat, Int -> String -> Int -> Maybe (Expr,Float,Int))]
|
||||||
-- ^ a list of callbacks for literal categories.
|
-- ^ a list of callbacks for literal categories.
|
||||||
-- The arguments of the callback are:
|
-- The arguments of the callback are:
|
||||||
-- the index of the constituent for the literal category;
|
-- the index of the constituent for the literal category;
|
||||||
|
|||||||
Reference in New Issue
Block a user