Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Scoring.FunTyped
Description
Semiring scores with "holes". Holes are used to express "partially applied" scores that occur when the score of a verticalization (unspread) is distributed to the two parent edges. The full score of the operation is restored when the two parent edges are eventually combined again.
This module implements partial scores as typesafe functions with phantom types
that indicate the number of holes on each side.
The grammatical combinators use an existential type Score
that reifies the phantom types as singletons,
which allows different scores to be easily stored together
and is compatible with the Score
types from the other Scoring* modules.
Thus, the grammatical combinators are partial
and fail when used with incompatible scores, indicating a parser bug.
Synopsis
- data Score s i :: Type where
- MkScore :: SNat nl -> SNat nr -> TypedScore nl nr s i -> Score s i
- data TypedScore (nl :: Nat) (nr :: Nat) s i where
- SVal :: !s -> TypedScore 'Z 'Z s i
- SRight :: !(LeftId i) -> !(RightHoles nl s) -> TypedScore ('S nl) 'Z s i
- SLeft :: !(LeftHoles nr s) -> !(RightId i) -> TypedScore 'Z ('S nr) s i
- SBoth :: !(LeftId i) -> !(BothHoles nl nr s) -> !(RightId i) -> TypedScore ('S nl) ('S nr) s i
- val :: s -> Score s i
- showScore :: (Show s, Show i) => Score s i -> String
- newtype LeftId i = LeftId i
- newtype RightId i = RightId i
- leftSide :: Score s i -> Maybe (LeftId i)
- rightSide :: Score s i -> Maybe (RightId i)
- type LeftHoles (n :: Nat) s = RightHoles n s -> s
- type RightHoles (n :: Nat) s = s -> RightHole n s
- data RightHole (n :: Nat) s
- type BothHoles (nl :: Nat) (nr :: Nat) s = RightHoles nr s -> RightHoles nl s
- times :: (Semiring s, Eq i, Show i) => TypedScore nl n s i -> TypedScore n nr s i -> Maybe (TypedScore nl nr s i)
- plus :: (Semiring s, Eq i) => TypedScore nl nr s i -> TypedScore nl nr s i -> Maybe (TypedScore nl nr s i)
- unsplitScores :: forall s i. (Semiring s, Eq i, Show i, Show s) => s -> Score s i -> Score s i -> Score s i
- unspreadScoresLeft :: forall s i. (Eq i, Show i, Semiring s, Show s) => i -> Score s i -> Score s i
- unspreadScoresRight :: forall i s. (Eq i, Semiring s, Show i, Show s) => i -> s -> Score s i -> Score s i -> Score s i
- addScores :: (Semiring s, Eq i) => Score s i -> Score s i -> Score s i
- getScoreVal :: Score s i -> s
The Score Type
data Score s i :: Type where Source #
A paritally applied score of type s
with an unknown number of holes (as used by the ChartParser).
Wraps a TypedScore
together with witnesses for the number of holes on each side.
Constructors
MkScore :: SNat nl -> SNat nr -> TypedScore nl nr s i -> Score s i |
data TypedScore (nl :: Nat) (nr :: Nat) s i where Source #
A partially applied score of type s
.
Comes in four variants,
depending on whether the score is fully applied
or needs to be combined on either or both sides.
Values that need to be combined are lists that represent scores with holes.
Each variant carries IDs of type i
that determine which objects fit on either of its sides.
Only score objects with matching IDs can be combined.
As a shorthand notation, we use a-b
to indicate a value
that depends on a
on its left and on b
on its right.
If the value does not depend on anything on either side, we use ()
,
i.e. ()-a
stands for SLeft _ a
and ()-()
stands for SVal _
.
Constructors
SVal :: !s -> TypedScore 'Z 'Z s i | Carries a fully applied value |
SRight :: !(LeftId i) -> !(RightHoles nl s) -> TypedScore ('S nl) 'Z s i | The right part of a combination, expects an argument to its left. Implemented as a function that takes a left counterpart and returns a score with fewer holes. |
SLeft :: !(LeftHoles nr s) -> !(RightId i) -> TypedScore 'Z ('S nr) s i | The left part of a combination, expects an argument to its right. Implemented as a function that takes a right hole and applies it. |
SBoth :: !(LeftId i) -> !(BothHoles nl nr s) -> !(RightId i) -> TypedScore ('S nl) ('S nr) s i | A combination of |
Instances
Show i => Show (TypedScore nl nr s i) Source # | |
Defined in Scoring.FunTyped Methods showsPrec :: Int -> TypedScore nl nr s i -> ShowS # show :: TypedScore nl nr s i -> String # showList :: [TypedScore nl nr s i] -> ShowS # | |
(NFData s, NFData i) => NFData (TypedScore nl nr s i) Source # | |
Defined in Scoring.FunTyped Methods rnf :: TypedScore nl nr s i -> () # |
IDs
Newtype for the left ID of a partial score.
Constructors
LeftId i |
Instances
Generic (LeftId i) Source # | |
Show i => Show (LeftId i) Source # | |
NFData i => NFData (LeftId i) Source # | |
Defined in Scoring.FunTyped | |
Eq i => Eq (LeftId i) Source # | |
Ord i => Ord (LeftId i) Source # | |
Defined in Scoring.FunTyped | |
Hashable i => Hashable (LeftId i) Source # | |
Defined in Scoring.FunTyped | |
type Rep (LeftId i) Source # | |
Defined in Scoring.FunTyped |
Newtype for the right ID of a partial score.
Constructors
RightId i |
Instances
Generic (RightId i) Source # | |
Show i => Show (RightId i) Source # | |
NFData i => NFData (RightId i) Source # | |
Defined in Scoring.FunTyped | |
Eq i => Eq (RightId i) Source # | |
Ord i => Ord (RightId i) Source # | |
Hashable i => Hashable (RightId i) Source # | |
Defined in Scoring.FunTyped | |
type Rep (RightId i) Source # | |
Defined in Scoring.FunTyped |
Hole Types
type LeftHoles (n :: Nat) s = RightHoles n s -> s Source #
The type of a function representing n
left holes.
type RightHoles (n :: Nat) s = s -> RightHole n s Source #
The type of a function representing n
right holes.
data RightHole (n :: Nat) s Source #
A single right hole (helper for RightHoles
).
type BothHoles (nl :: Nat) (nr :: Nat) s = RightHoles nr s -> RightHoles nl s Source #
The type of a function containing nl
left and nr
right holes.
Semiring operations
Semiring operations can be lifted to partial scores, but since it is not guaranteed that their arguments can be combined, they are partial.
times :: (Semiring s, Eq i, Show i) => TypedScore nl n s i -> TypedScore n nr s i -> Maybe (TypedScore nl nr s i) Source #
plus :: (Semiring s, Eq i) => TypedScore nl nr s i -> TypedScore nl nr s i -> Maybe (TypedScore nl nr s i) Source #
Adds two partially applied TypedScore
s
by adding their underlying (or resulting) semiring values.
This operation is only admitted
if the two scores are of the same shape and have matching IDs.
Otherwise, Nothing
is returned.
a-b + a-b -> a-b
grammatical combinators
The following combinators correspond to the unsplit and unspread operations of the path-graph grammar.
Arguments
:: forall s i. (Semiring s, Eq i, Show i, Show s) | |
=> s | The score of the split operation. |
-> Score s i | The |
-> Score s i | The |
-> Score s i | The |
Combines the Score
s of two edges with a split
operation into the score of the parent edge.
This is expected to be called on compatible scores
and will throw an error otherwise to indicate parser bugs.
a-b b-c --------- unsplit a-c
Arguments
:: forall s i. (Eq i, Show i, Semiring s, Show s) | |
=> i | The new ID that marks both parent edges |
-> Score s i | The |
-> Score s i | The |
Creates the Score
of a left parent edge from a left child edge of an unspread
.
Will throw an error if called on invalid input to indicate parser bugs.
Arguments
:: forall i s. (Eq i, Semiring s, Show i, Show s) | |
=> i | The new ID that marks both parent edges. |
-> s | The score of the |
-> Score s i | The |
-> Score s i | The |
-> Score s i | The |
Creates the Score
of a right parent edge
from the middle and right child edges of an unspread
and a spread
operation.
addScores :: (Semiring s, Eq i) => Score s i -> Score s i -> Score s i Source #
Adds two Score
s that are alternative derivations of the same transition.
This is expected to be called on compatible scores
and will throw an error otherwise to indicate parser bugs.
a-b a-b --------- add a-b
getScoreVal :: Score s i -> s Source #
Extracts the value from a fully applied Score
.
This function is intended to be used to extract the final score of the parser.
If the score is not fully applied,
throws an exception to indicate parser bugs.