Portability | portable |
---|---|
Stability | stable |
Maintainer | Aarne Ranta |
This module is an Application Programming Interface to load and interpret grammars compiled in Portable Grammar Format (PGF). The PGF format is produced as a final output from the GF compiler. The API is meant to be used for embedding GF grammars in Haskell programs
- data PGF
- readPGF :: FilePath -> IO PGF
- data CId
- mkCId :: String -> CId
- wildCId :: CId
- showCId :: CId -> String
- readCId :: String -> Maybe CId
- type Language = CId
- showLanguage :: Language -> String
- readLanguage :: String -> Maybe Language
- languages :: PGF -> [Language]
- abstractName :: PGF -> Language
- languageCode :: PGF -> Language -> Maybe String
- data Type
- type Hypo = (BindType, CId, Type)
- showType :: [CId] -> Type -> String
- readType :: String -> Maybe Type
- mkType :: [Hypo] -> CId -> [Expr] -> Type
- mkHypo :: Type -> Hypo
- mkDepHypo :: CId -> Type -> Hypo
- mkImplHypo :: CId -> Type -> Hypo
- categories :: PGF -> [CId]
- startCat :: PGF -> Type
- functions :: PGF -> [CId]
- functionType :: PGF -> CId -> Maybe Type
- type Tree = Expr
- data Expr
- showExpr :: [CId] -> Expr -> String
- readExpr :: String -> Maybe Expr
- mkApp :: CId -> [Expr] -> Expr
- unApp :: Expr -> Maybe (CId, [Expr])
- mkStr :: String -> Expr
- unStr :: Expr -> Maybe String
- mkInt :: Int -> Expr
- unInt :: Expr -> Maybe Int
- mkDouble :: Double -> Expr
- unDouble :: Expr -> Maybe Double
- mkMeta :: Expr
- isMeta :: Expr -> Bool
- linearize :: PGF -> Language -> Tree -> String
- linearizeAllLang :: PGF -> Tree -> [(Language, String)]
- linearizeAll :: PGF -> Tree -> [String]
- groupResults :: [[(Language, String)]] -> [(Language, [String])]
- showPrintName :: PGF -> Language -> CId -> String
- parse :: PGF -> Language -> Type -> String -> [Tree]
- parseWithRecovery :: PGF -> Language -> Type -> [Type] -> String -> [Tree]
- parseAllLang :: PGF -> Type -> String -> [(Language, [Tree])]
- parseAll :: PGF -> Type -> String -> [[Tree]]
- compute :: PGF -> Expr -> Expr
- paraphrase :: PGF -> Expr -> [Expr]
- checkType :: PGF -> Type -> Either TcError Type
- checkExpr :: PGF -> Expr -> Type -> Either TcError Expr
- inferExpr :: PGF -> Expr -> Either TcError (Expr, Type)
- data TcError
- = UnknownCat CId
- | UnknownFun CId
- | WrongCatArgs [CId] Type CId Int Int
- | TypeMismatch [CId] Expr Type Type
- | NotFunType [CId] Expr Type
- | CannotInferType [CId] Expr
- | UnresolvedMetaVars [CId] Expr [MetaId]
- | UnexpectedImplArg [CId] Expr
- ppTcError :: TcError -> Doc
- complete :: PGF -> Language -> Type -> String -> [String]
- data ParseState
- initState :: PGF -> Language -> Type -> ParseState
- nextState :: ParseState -> String -> Either ErrorState ParseState
- getCompletions :: ParseState -> String -> Map String ParseState
- recoveryStates :: [Type] -> ErrorState -> (ParseState, Map String ParseState)
- extractTrees :: ParseState -> Type -> [Tree]
- generateRandom :: PGF -> Type -> IO [Expr]
- generateAll :: PGF -> Type -> [Expr]
- generateAllDepth :: Maybe Expr -> PGF -> Type -> Maybe Int -> [Expr]
- generateRandomFrom :: Maybe Expr -> Maybe Probabilities -> StdGen -> PGF -> Type -> [Expr]
- type Lemma = CId
- type Analysis = String
- data Morpho
- lookupMorpho :: Morpho -> String -> [(Lemma, Analysis)]
- buildMorpho :: PGF -> Language -> Morpho
- fullFormLexicon :: Morpho -> [(String, [(Lemma, Analysis)])]
- graphvizAbstractTree :: PGF -> (Bool, Bool) -> Expr -> String
- graphvizParseTree :: PGF -> CId -> Expr -> String
- graphvizDependencyTree :: String -> Bool -> Maybe Labels -> Maybe String -> PGF -> CId -> Expr -> String
- graphvizAlignment :: PGF -> Expr -> String
- browse :: PGF -> CId -> Maybe (String, [CId], [CId])
PGF
An abstract data type representing multilingual grammar in Portable Grammar Format.
Binary PGF |
readPGF :: FilePath -> IO PGFSource
Reads file in Portable Grammar Format and produces
PGF
structure. The file is usually produced with:
$ gf -make <grammar file name>
Identifiers
An abstract data type that represents identifiers for functions and categories in PGF.
Languages
This is just a CId
with the language name.
A language name is the identifier that you write in the
top concrete or abstract module in GF after the
concrete/abstract keyword. Example:
abstract Lang = ... concrete LangEng of Lang = ...
showLanguage :: Language -> StringSource
abstractName :: PGF -> LanguageSource
The abstract language name is the name of the top-level abstract module
languageCode :: PGF -> Language -> Maybe StringSource
Gets the RFC 4646 language tag
of the language which the given concrete syntax implements,
if this is listed in the source grammar.
Example language tags include "en"
for English,
and "en-UK"
for British English.
Types
type Hypo = (BindType, CId, Type)Source
Hypo
represents a hypothesis in a type i.e. in the type A -> B, A is the hypothesis
showType :: [CId] -> Type -> StringSource
renders type as String
. The list
of identifiers is the list of all free variables
in the expression in order reverse to the order
of binding.
mkType :: [Hypo] -> CId -> [Expr] -> TypeSource
creates a type from list of hypothesises, category and
list of arguments for the category. The operation
mkType [h_1,...,h_n] C [e_1,...,e_m]
will create
h_1 -> ... -> h_n -> C e_1 ... e_m
mkImplHypo :: CId -> Type -> HypoSource
creates hypothesis for dependent type with implicit argument i.e. ({x} : A)
categories :: PGF -> [CId]Source
List of all categories defined in the given grammar. The categories are defined in the abstract syntax with the 'cat' keyword.
The start category is defined in the grammar with the 'startcat' flag. This is usually the sentence category but it is not necessary. Despite that there is a start category defined you can parse with any category. The start category definition is just for convenience.
Functions
Expressions & Trees
Tree
Expr
An expression in the abstract syntax of the grammar. It could be both parameter of a dependent type or an abstract syntax tree for for some sentence.
showExpr :: [CId] -> Expr -> StringSource
renders expression as String
. The list
of identifiers is the list of all free variables
in the expression in order reverse to the order
of binding.
mkApp :: CId -> [Expr] -> ExprSource
Constructs an expression by applying a function to a list of expressions
Operations
Linearization
linearize :: PGF -> Language -> Tree -> StringSource
Linearizes given expression as string in the language
linearizeAllLang :: PGF -> Tree -> [(Language, String)]Source
Linearizes given expression as string in all languages available in the grammar.
linearizeAll :: PGF -> Tree -> [String]Source
The same as linearizeAllLang
but does not return
the language.
Parsing
parse :: PGF -> Language -> Type -> String -> [Tree]Source
Tries to parse the given string in the specified language
and to produce abstract syntax expression. An empty
list is returned if the parsing is not successful. The list may also
contain more than one element if the grammar is ambiguous.
Throws an exception if the given language cannot be used
for parsing, see canParse
.
parseAllLang :: PGF -> Type -> String -> [(Language, [Tree])]Source
Tries to parse the given string with all available languages.
Languages which cannot be used for parsing (see canParse
)
are ignored.
The returned list contains pairs of language
and list of abstract syntax expressions
(this is a list, since grammars can be ambiguous).
Only those languages
for which at least one parsing is possible are listed.
parseAll :: PGF -> Type -> String -> [[Tree]]Source
The same as parseAllLang
but does not return
the language.
Evaluation
paraphrase :: PGF -> Expr -> [Expr]Source
Type Checking
The type checker in PGF does both type checking and renaming
i.e. it verifies that all identifiers are declared and it
distinguishes between global function or type indentifiers and
variable names. The type checker should always be applied on
expressions entered by the user i.e. those produced via functions
like readType
and readExpr
because otherwise unexpected results
could appear. All typechecking functions returns updated versions
of the input types or expressions because the typechecking could
also lead to metavariables instantiations.
checkType :: PGF -> Type -> Either TcError TypeSource
Check whether a given type is consistent with the abstract syntax of the grammar.
checkExpr :: PGF -> Expr -> Type -> Either TcError ExprSource
Checks an expression against a specified type.
inferExpr :: PGF -> Expr -> Either TcError (Expr, Type)Source
Tries to infer the type of a given expression. Note that
even if the expression is type correct it is not always
possible to infer its type in the GF type system.
In this case the function returns the CannotInferType
error.
If an error occurs in the typechecking phase
the type checker returns not a plain text error message
but a TcError
structure which describes the error.
UnknownCat CId | Unknown category name was found. |
UnknownFun CId | Unknown function name was found. |
WrongCatArgs [CId] Type CId Int Int | A category was applied to wrong number of arguments.
The first integer is the number of expected arguments and
the second the number of given arguments.
The |
TypeMismatch [CId] Expr Type Type | The expression is not of the expected type.
The first type is the expected type, while
the second is the inferred. The |
NotFunType [CId] Expr Type | Something that is not of function type was applied to an argument. |
CannotInferType [CId] Expr | It is not possible to infer the type of an expression. |
UnresolvedMetaVars [CId] Expr [MetaId] | Some metavariables have to be instantiated in order to complete the typechecking. |
UnexpectedImplArg [CId] Expr | Implicit argument was passed where the type doesn't allow it |
ppTcError :: TcError -> DocSource
Renders the type checking error to a document. See Text.PrettyPrint
.
Word Completion (Incremental Parsing)
Complete the last word in the given string. If the input is empty or ends in whitespace, the last word is considred to be the empty string. This means that the completions will be all possible next words.
data ParseState Source
An abstract data type whose values represent the current state in an incremental parser.
initState :: PGF -> Language -> Type -> ParseStateSource
Creates an initial parsing state for a given language and startup category.
nextState :: ParseState -> String -> Either ErrorState ParseStateSource
From the current state and the next token
nextState
computes a new state, where the token
is consumed and the current position is shifted by one.
If the new token cannot be accepted then an error state
is returned.
getCompletions :: ParseState -> String -> Map String ParseStateSource
If the next token is not known but only its prefix (possible empty prefix)
then the getCompletions
function can be used to calculate the possible
next words and the consequent states. This is used for word completions in
the GF interpreter.
recoveryStates :: [Type] -> ErrorState -> (ParseState, Map String ParseState)Source
extractTrees :: ParseState -> Type -> [Tree]Source
This function extracts the list of all completed parse trees that spans the whole input consumed so far. The trees are also limited by the category specified, which is usually the same as the startup category.
Generation
generateRandom :: PGF -> Type -> IO [Expr]Source
Generates an infinite list of random abstract syntax expressions. This is usefull for tree bank generation which after that can be used for grammar testing.
generateAll :: PGF -> Type -> [Expr]Source
The same as generateAllDepth
but does not limit
the depth in the generation, and doesn't give an initial expression.
generateAllDepth :: Maybe Expr -> PGF -> Type -> Maybe Int -> [Expr]Source
Generates an exhaustive possibly infinite list of abstract syntax expressions. A depth can be specified to limit the search space.
Morphological Analysis
buildMorpho :: PGF -> Language -> MorphoSource
Visualizations
graphvizDependencyTree :: String -> Bool -> Maybe Labels -> Maybe String -> PGF -> CId -> Expr -> StringSource
graphvizAlignment :: PGF -> Expr -> StringSource