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

GreedyParser

Description

This module contains a simple greedy parser for path grammars. The grammar is provided by an evaluator (Eval). In addition, the parser takes a policy function that picks a reduction option in each step.

Synopsis

Parsing State

data Trans tr Source #

A transition during greedy parsing. Augments transition data with a flag that indicates whether the transition is a transitive right (2nd) parent of a spread.

Constructors

Trans 

Fields

  • _tContent :: !tr

    content of the transition

  • _t2nd :: !Bool

    flag that indicates (transitive) right parents of spreads

Instances

Instances details
Show tr => Show (Trans tr) Source # 
Instance details

Defined in GreedyParser

Methods

showsPrec :: Int -> Trans tr -> ShowS #

show :: Trans tr -> String #

showList :: [Trans tr] -> ShowS #

data GreedyState tr tr' slc op Source #

The state of the greedy parse between steps. Generally, the current reduction consists of frozen transitions between the ⋊ and the current location and open transitions between the current location and ⋉.

⋊==[1]==[2]==[3]——[4]——[5]——⋉
  └ frozen  ┘  | └   open  ┘
            midSlice (current position)

frozen:   ==[2]==[1]==
midSlice: [3]
open:     ——[4]——[5]——

This is the GSSemiOpen case: The slice at the current pointer ([3]) is represented as an individual slice (midSlice). The frozen part is represented by a Path of frozen transitions (tr') and slices (slc). in reverse direction, i.e. from midslice back to ⋊ (excluding ⋊). The open part is a Path of open transitions (tr) and slices (slc) in forward direction from midSlice up to ⋉.

There are two special cases. All transitions can be frozen (GSFrozen), in which case state only contains the backward Path of frozen transitions (excluding ⋊ and ⋉):

⋊==[1]==[2]==[3]==⋉
                   └ current position
represented as: ==[3]==[2]==[1]==

Or all transitions can be open (GSOpen), in which case the state is just the forward path of open transitions:

⋊——[1]——[2]——[3]——⋉
└ current position
represented as: ——[1]——[2]——[3]——

The open and semiopen case additionally have a list of operations in generative order.

Constructors

GSFrozen !(Path (Maybe tr') slc) 
GSSemiOpen 

Fields

  • _gsFrozen :: !(Path (Maybe tr') slc)

    frozen transitions and slices from current point leftward

  • _gsMidSlice :: !slc

    the slice at the current posision between gsFrozen and gsOpen

  • _gsOpen :: !(Path (Trans tr) slc)

    non-frozen transitions and slices from current point rightward

  • _gsDeriv :: ![op]

    derivation from current reduction to original surface

GSOpen !(Path (Trans tr) slc) ![op] 

Instances

Instances details
(Show slc, Show o) => Show (GreedyState tr tr' slc o) Source # 
Instance details

Defined in GreedyParser

Methods

showsPrec :: Int -> GreedyState tr tr' slc o -> ShowS #

show :: GreedyState tr tr' slc o -> String #

showList :: [GreedyState tr tr' slc o] -> ShowS #

showFrozen :: Show slc => Path tr' slc -> String Source #

Helper function for showing the frozen part of a piece.

showOpen :: Show slc => Path tr slc -> String Source #

Helper function for showing the open part of a piece.

Parsing Actions

data ActionSingle slc tr s f Source #

A parsing action (reduction step) with a single parent transition. Combines the parent elements with a single-transition derivation operation.

Constructors

ActionSingle 

Fields

Instances

Instances details
(Show slc, Show tr, Show s, Show f) => Show (ActionSingle slc tr s f) Source # 
Instance details

Defined in GreedyParser

Methods

showsPrec :: Int -> ActionSingle slc tr s f -> ShowS #

show :: ActionSingle slc tr s f -> String #

showList :: [ActionSingle slc tr s f] -> ShowS #

data ActionDouble slc tr s f h Source #

A parsing action (reduction step) with two parent transitions. Combines the parent elements with a double-transition derivation operation.

Constructors

ActionDouble 

Fields

Instances

Instances details
(Show slc, Show tr, Show s, Show f, Show h) => Show (ActionDouble slc tr s f h) Source # 
Instance details

Defined in GreedyParser

Methods

showsPrec :: Int -> ActionDouble slc tr s f h -> ShowS #

show :: ActionDouble slc tr s f h -> String #

showList :: [ActionDouble slc tr s f h] -> ShowS #

type Action slc tr s f h = Either (ActionSingle slc tr s f) (ActionDouble slc tr s f h) Source #

An alias that combines ActionSingle and ActionDouble, representing all possible reduction steps.

Parsing Algorithm

parseGreedy Source #

Arguments

:: forall m tr tr' slc slc' s f h. (Monad m, MonadIO m, Show tr', Show slc, Show tr, Show s, Show f, Show h) 
=> Eval tr tr' slc slc' (Leftmost s f h)

the evaluator of the grammar to be used

-> ([Action slc tr s f h] -> ExceptT String m (Action slc tr s f h))

the policy: picks a parsing action from a list of options (determines the Monad m, e.g., for randomness).

-> Path slc' tr'

the input piece

-> ExceptT String m (Analysis s f h tr slc)

the full parse or an error message

Parse a piece in a greedy fashion. At each step, a policy chooses from the possible reduction actions, the reduction is applied, and parsing continues until the piece is fully reduced or no more reduction operations are available. Returns the full derivation from the top (⋊——⋉) or an error message.

pickRandom :: StatefulGen g m => g -> [slc] -> ExceptT String m slc Source #

A policy that picks the next action at random. Must be partially applied with a random generator before passing to parseGreedy.

Entry Points

parseRandom Source #

Arguments

:: (Show tr', Show slc, Show tr, Show s, Show f, Show h) 
=> Eval tr tr' slc slc' (Leftmost s f h)

the grammar's evaluator

-> Path slc' tr'

the input piece

-> ExceptT String IO (Analysis s f h tr slc)

a random reduction of the piece (or an error message)

Parse a piece randomly using a fresh random number generator.

parseRandom' Source #

Arguments

:: (Show tr', Show slc, Show tr, Show s, Show f, Show h, StatefulGen g IO) 
=> g

a random number generator

-> Eval tr tr' slc slc' (Leftmost s f h)

the grammar's evaluator

-> Path slc' tr'

the input piece

-> ExceptT String IO (Analysis s f h tr slc)

a random reduction of the piece (or an error message)

Parse a piece randomly using an existing random number generator.