proto-voice-model-0.1.0.0
Safe HaskellSafe-Inferred
LanguageGHC2021

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

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 

Instances

Instances details
Show i => Show (Score s i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

showsPrec :: Int -> Score s i -> ShowS #

show :: Score s i -> String #

showList :: [Score s i] -> ShowS #

(NFData s, NFData i) => NFData (Score s i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

rnf :: 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 SLeft and SRight that expects arguments on both sides. Implemented as a function that expects both a right and a left hole to be complete.

Instances

Instances details
Show i => Show (TypedScore nl nr s i) Source # 
Instance details

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 # 
Instance details

Defined in Scoring.FunTyped

Methods

rnf :: TypedScore nl nr s i -> () #

val :: s -> Score s i Source #

Creates a simple value score of type ()-().

showScore :: (Show s, Show i) => Score s i -> String Source #

Returns a string representation of a Score (more compact than it's Show instance).

IDs

newtype LeftId i Source #

Newtype for the left ID of a partial score.

Constructors

LeftId i 

Instances

Instances details
Generic (LeftId i) Source # 
Instance details

Defined in Scoring.FunTyped

Associated Types

type Rep (LeftId i) :: Type -> Type #

Methods

from :: LeftId i -> Rep (LeftId i) x #

to :: Rep (LeftId i) x -> LeftId i #

Show i => Show (LeftId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

showsPrec :: Int -> LeftId i -> ShowS #

show :: LeftId i -> String #

showList :: [LeftId i] -> ShowS #

NFData i => NFData (LeftId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

rnf :: LeftId i -> () #

Eq i => Eq (LeftId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

(==) :: LeftId i -> LeftId i -> Bool #

(/=) :: LeftId i -> LeftId i -> Bool #

Ord i => Ord (LeftId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

compare :: LeftId i -> LeftId i -> Ordering #

(<) :: LeftId i -> LeftId i -> Bool #

(<=) :: LeftId i -> LeftId i -> Bool #

(>) :: LeftId i -> LeftId i -> Bool #

(>=) :: LeftId i -> LeftId i -> Bool #

max :: LeftId i -> LeftId i -> LeftId i #

min :: LeftId i -> LeftId i -> LeftId i #

Hashable i => Hashable (LeftId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

hashWithSalt :: Int -> LeftId i -> Int #

hash :: LeftId i -> Int #

type Rep (LeftId i) Source # 
Instance details

Defined in Scoring.FunTyped

type Rep (LeftId i) = D1 ('MetaData "LeftId" "Scoring.FunTyped" "proto-voice-model-0.1.0.0-IpbUkUcWDicKjQK7SlZCoP" 'True) (C1 ('MetaCons "LeftId" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 i)))

newtype RightId i Source #

Newtype for the right ID of a partial score.

Constructors

RightId i 

Instances

Instances details
Generic (RightId i) Source # 
Instance details

Defined in Scoring.FunTyped

Associated Types

type Rep (RightId i) :: Type -> Type #

Methods

from :: RightId i -> Rep (RightId i) x #

to :: Rep (RightId i) x -> RightId i #

Show i => Show (RightId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

showsPrec :: Int -> RightId i -> ShowS #

show :: RightId i -> String #

showList :: [RightId i] -> ShowS #

NFData i => NFData (RightId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

rnf :: RightId i -> () #

Eq i => Eq (RightId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

(==) :: RightId i -> RightId i -> Bool #

(/=) :: RightId i -> RightId i -> Bool #

Ord i => Ord (RightId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

compare :: RightId i -> RightId i -> Ordering #

(<) :: RightId i -> RightId i -> Bool #

(<=) :: RightId i -> RightId i -> Bool #

(>) :: RightId i -> RightId i -> Bool #

(>=) :: RightId i -> RightId i -> Bool #

max :: RightId i -> RightId i -> RightId i #

min :: RightId i -> RightId i -> RightId i #

Hashable i => Hashable (RightId i) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

hashWithSalt :: Int -> RightId i -> Int #

hash :: RightId i -> Int #

type Rep (RightId i) Source # 
Instance details

Defined in Scoring.FunTyped

type Rep (RightId i) = D1 ('MetaData "RightId" "Scoring.FunTyped" "proto-voice-model-0.1.0.0-IpbUkUcWDicKjQK7SlZCoP" 'True) (C1 ('MetaCons "RightId" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 i)))

leftSide :: Score s i -> Maybe (LeftId i) Source #

Returns the ID on the left side of an Score, or Nothing for SVal and SLeft.

a-b -> a

rightSide :: Score s i -> Maybe (RightId i) Source #

Returns the ID on the right side of an Score, or Nothing for SVal and SRight.

a-b -> b

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).

Instances

Instances details
NFData s => NFData (RightHole n s) Source # 
Instance details

Defined in Scoring.FunTyped

Methods

rnf :: RightHole n s -> () #

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 #

Combines two partially applied Scores by applying them to each other and/or multiplying the underlying semiring values. Shapes and IDs at the adjacent sides must match, otherwise Nothing is returned.

a-b × b-c -> a-c

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 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.

unsplitScores Source #

Arguments

:: forall s i. (Semiring s, Eq i, Show i, Show s) 
=> s

The score of the split operation.

-> Score s i

The Score of the left child edge.

-> Score s i

The Score of the right child edge.

-> Score s i

The Score of the parent edge, if it exists.

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

unspreadScoresLeft Source #

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 of the left child edge.

-> Score s i

The Score of the left parent edge, if it exists.

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.

unspreadScoresRight Source #

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 spread operation.

-> Score s i

The Score of the middle child edge.

-> Score s i

The Score of the right child edge.

-> Score s i

The Score of the right parent edge, if it exists.

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.