| Safe Haskell | None |
|---|---|
| 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 where
- data TypedScore (nl :: Nat) (nr :: Nat) s i where
- SVal :: forall s i. !s -> TypedScore 'Z 'Z s i
- SRight :: forall i (nl1 :: Nat) s. !(LeftId i) -> !(RightHoles nl1 s) -> TypedScore ('S nl1) 'Z s i
- SLeft :: forall (nr1 :: Nat) s i. !(LeftHoles nr1 s) -> !(RightId i) -> TypedScore 'Z ('S nr1) s i
- SBoth :: forall i (nl1 :: Nat) (nr1 :: Nat) s. !(LeftId i) -> !(BothHoles nl1 nr1 s) -> !(RightId i) -> TypedScore ('S nl1) ('S nr1) 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 :: forall s i (nl :: Nat) (n :: Nat) (nr :: Nat). (Semiring s, Eq i, Show i) => TypedScore nl n s i -> TypedScore n nr s i -> Maybe (TypedScore nl nr s i)
- plus :: forall s i (nl :: Nat) (nr :: Nat). (Semiring s, Eq i) => TypedScore nl nr s i -> TypedScore nl nr s i -> Maybe (TypedScore nl nr s i)
- unsplitScores :: (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 :: (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
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.
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 :: forall s i. !s -> TypedScore 'Z 'Z s i | Carries a fully applied value |
| SRight :: forall i (nl1 :: Nat) s. !(LeftId i) -> !(RightHoles nl1 s) -> TypedScore ('S nl1) '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 :: forall (nr1 :: Nat) s i. !(LeftHoles nr1 s) -> !(RightId i) -> TypedScore 'Z ('S nr1) 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 :: forall i (nl1 :: Nat) (nr1 :: Nat) s. !(LeftId i) -> !(BothHoles nl1 nr1 s) -> !(RightId i) -> TypedScore ('S nl1) ('S nr1) s i | A combination of |
Instances
| (NFData s, NFData i) => NFData (TypedScore nl nr s i) Source # | |
Defined in Scoring.FunTyped Methods rnf :: TypedScore nl nr s i -> () # | |
| 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 # | |
IDs
Newtype for the left ID of a partial score.
Constructors
| LeftId i |
Instances
| NFData i => NFData (LeftId i) Source # | |||||
Defined in Scoring.FunTyped | |||||
| Generic (LeftId i) Source # | |||||
Defined in Scoring.FunTyped Associated Types
| |||||
| Show i => Show (LeftId i) Source # | |||||
| 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
| NFData i => NFData (RightId i) Source # | |||||
Defined in Scoring.FunTyped | |||||
| Generic (RightId i) Source # | |||||
Defined in Scoring.FunTyped Associated Types
| |||||
| Show i => Show (RightId i) Source # | |||||
| 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 :: forall s i (nl :: Nat) (n :: Nat) (nr :: Nat). (Semiring s, Eq i, Show i) => TypedScore nl n s i -> TypedScore n nr s i -> Maybe (TypedScore nl nr s i) Source #
plus :: forall s i (nl :: Nat) (nr :: Nat). (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 TypedScores
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
| :: (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 Scores 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
| :: (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 Scores 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.