Copyright | (c) 2014-2021 Dakotah Lambert |
---|---|
License | MIT |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
Extensions |
|
The purpose of this module is to define an interface to a generic, reusable impementation of finite-state automata (FSAs). The primary motivation for this is to allow for efficient analysis of stringsets in a linguistic context, although the nature of the project should allow more general use.
Synopsis
- data FSA n e = FSA {
- sigma :: Set e
- transitions :: Set (Transition n e)
- initials :: Set (State n)
- finals :: Set (State n)
- isDeterministic :: Bool
- states :: (Ord e, Ord n) => FSA n e -> Set (State n)
- isNull :: (Ord e, Ord n) => FSA n e -> Bool
- follow :: (Ord n, Ord e) => FSA n e -> [Symbol e] -> State n -> Set (State n)
- totalWithAlphabet :: (Ord e, Enum n, Ord n) => Set e -> FSA n e
- emptyWithAlphabet :: (Ord e, Enum n, Ord n) => Set e -> FSA n e
- emptyLanguage :: (Ord e, Ord n, Enum n) => FSA n e
- singletonWithAlphabet :: (Ord e, Enum n, Ord n) => Set e -> [e] -> FSA n e
- singletonLanguage :: (Ord e, Enum n, Ord n) => [e] -> FSA n e
- brzozowskiDerivative :: (Ord e, Ord n) => [e] -> FSA n e -> FSA n e
- quotLeft :: (Ord e, Ord n1, Ord n2) => FSA n1 e -> FSA n2 e -> FSA (Maybe (Either n1 ()), Maybe n2) e
- quotMid :: (Ord e, Ord n1, Ord n2, Ord n3) => FSA n1 e -> FSA n2 e -> FSA n3 e -> FSA Integer e
- quotRight :: (Ord e, Ord n1, Ord n2) => FSA n1 e -> FSA n2 e -> FSA Integer e
- kleeneClosure :: (Ord n, Ord e) => FSA n e -> FSA (Either n Bool) e
- powersetGraph :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e
- syntacticMonoid :: (Ord e, Ord n) => FSA n e -> FSA ([Maybe n], [Symbol e]) e
- residue :: (Ord n, Ord e, Enum n) => FSA n e -> FSA n e -> FSA n e
- coresidue :: (Ord n, Ord e, Enum n) => FSA n e -> FSA n e -> FSA n e
- primitiveIdeal2 :: (Ord n, Ord e) => FSA (n, [Symbol e]) e -> State (n, [Symbol e]) -> Set (State (n, [Symbol e]))
- primitiveIdealL :: (Ord n, Ord e) => FSA (n, [Symbol e]) e -> State (n, [Symbol e]) -> Set (State (n, [Symbol e]))
- primitiveIdealR :: (Ord n, Ord e) => FSA n e -> State n -> Set (State n)
- flatIntersection :: (Enum n, Ord n, NFData n, Ord e, NFData e) => [FSA n e] -> FSA n e
- flatUnion :: (Enum n, Ord n, NFData n, Ord e, NFData e) => [FSA n e] -> FSA n e
- reverse :: (Ord e, Ord n) => FSA n e -> FSA n e
- complement :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e
- complementDeterministic :: (Ord e, Ord n) => FSA n e -> FSA n e
- determinize :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e
- minimize :: (Ord e, Ord n) => FSA n e -> FSA (Set (Set n)) e
- minimizeDeterministic :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e
- normalize :: (Ord e, Ord n) => FSA n e -> FSA Integer e
- trimUnreachables :: (Ord e, Ord n) => FSA n e -> FSA n e
- minimizeOver :: (Ord e, Ord n) => (FSA n e -> Set (Set (State n))) -> FSA n e -> FSA (Set n) e
- nerode :: (Ord e, Ord n) => FSA n e -> Set (Set (State n))
- hEquivalence :: (Ord n, Ord e) => FSA (n, [Symbol e]) e -> Set (Set (State (n, [Symbol e])))
- jEquivalence :: (Ord e, Ord n) => FSA ([Maybe n], [Symbol e]) e -> Set (Set (State ([Maybe n], [Symbol e])))
- trivialUnder :: (FSA n e -> Set (Set (State n))) -> FSA n e -> Bool
- extendAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a b -> FSA (Maybe Integer, Maybe a) b
- semanticallyExtendAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a (Maybe b) -> FSA a (Maybe b)
- tierify :: (Ord a, Ord b) => Set b -> FSA a (Maybe b) -> FSA a (Maybe b)
- contractAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a b -> FSA a b
- forceAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a b -> FSA (Maybe Integer, Maybe a) b
- desemantify :: (Ord a, Ord b) => FSA a (Maybe b) -> FSA a b
- renameSymbolsBy :: (Ord e, Ord e1, Ord n) => (e -> e1) -> FSA n e -> FSA n e1
- renameStatesBy :: (Ord e, Ord n, Ord n1) => (n -> n1) -> FSA n e -> FSA n1 e
- renameStates :: (Ord e, Ord n, Ord n1, Enum n1) => FSA n e -> FSA n1 e
- data State n = State {
- nodeLabel :: n
- data Symbol e
- unsymbols :: (Collapsible s, Container c e, Monoid c) => s (Symbol e) -> c
- data Transition n e = Transition {}
- module LTK.Containers
Documentation
A finite-state automaton (FSA) is represented by a directed graph, the edges of which are labelled by formal symbols.
FSA | |
|
Instances
states :: (Ord e, Ord n) => FSA n e -> Set (State n) Source #
The collection of all states in an FSA.
follow :: (Ord n, Ord e) => FSA n e -> [Symbol e] -> State n -> Set (State n) Source #
The generalized \(\delta\) function, follow each symbol in a string in order.
Since: 0.2
Constructing simple automata
totalWithAlphabet :: (Ord e, Enum n, Ord n) => Set e -> FSA n e Source #
An automaton accepting every string over a given alphabet.
emptyWithAlphabet :: (Ord e, Enum n, Ord n) => Set e -> FSA n e Source #
An automaton accepting no strings over a given alphabet.
emptyLanguage :: (Ord e, Ord n, Enum n) => FSA n e Source #
A specialization of emptyWithAlphabet
where the alphabet
is itself empty.
singletonWithAlphabet :: (Ord e, Enum n, Ord n) => Set e -> [e] -> FSA n e Source #
An automaton that accepts only the given string, over a given alphabet.
singletonLanguage :: (Ord e, Enum n, Ord n) => [e] -> FSA n e Source #
An automaton that accepts only the given string, over the minimal alphabet required to represent this string.
Derived automata
brzozowskiDerivative :: (Ord e, Ord n) => [e] -> FSA n e -> FSA n e Source #
Return an FSA representing possible continuations from a given sequence of symbols. If the input automaton is not complete then the output may have no states when given incompatible input.
Since: 1.0
quotLeft :: (Ord e, Ord n1, Ord n2) => FSA n1 e -> FSA n2 e -> FSA (Maybe (Either n1 ()), Maybe n2) e Source #
Return an FSA representing possible continuations in the second
language from strings in the first language.
In other words, quotLeft a b
returns \(\{w : x\in a, xw\in b\}\).
Since: 1.0
quotMid :: (Ord e, Ord n1, Ord n2, Ord n3) => FSA n1 e -> FSA n2 e -> FSA n3 e -> FSA Integer e Source #
quotMid a b c
is \(\{wz : wx\in a, yx\in b, yz\in c\}\).
This lifts strings to a group, placing b-inverse between a and c.
The time complexity of this function is abysmal,
performing a left and a right quotient for each state in b
.
Since: 1.0
quotRight :: (Ord e, Ord n1, Ord n2) => FSA n1 e -> FSA n2 e -> FSA Integer e Source #
Return an FSA representing possible strings in the first language
which end with a string in the second language.
In other words, quotRight a b
is \(\{w : wx\in a, x\in b\}\).
Since: 1.0
kleeneClosure :: (Ord n, Ord e) => FSA n e -> FSA (Either n Bool) e Source #
The Kleene Closure of an automaton is the free monoid under concatenation generated by all strings in the automaton's represented stringset. The resulting automaton is nondeterministic.
powersetGraph :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e Source #
Given an automaton \(M\) with stateset \(Q\), the powerset graph of \(M\) is an automaton with stateset in the powerset of \(Q\). From a node \(\{q_1,q_2,\ldots,q_n\}\), there is an edge labelled \(\sigma\) that leads to \(\{\delta(q_1,\sigma), \delta(q_2,\sigma), \ldots, \delta(q_n, \sigma)\}\), where \(\delta\) is the transition function of the input. The initial state is \(Q\), and the result is complete.
syntacticMonoid :: (Ord e, Ord n) => FSA n e -> FSA ([Maybe n], [Symbol e]) e Source #
Given an automaton \(M\) with stateset \(Q\), the syntactic monoid of \(M\) is an automaton with stateset in \((Q\rightarrow Q)\). Here these functions are represented by lists, where \(q_i\) maps to the \(i^\text{th}\) element of the list. From a node \(\langle q_1,q_2,\ldots,q_n\rangle\), there is an edge labelled \(\sigma\) that leads to \(\langle\delta(q_1,\sigma), \delta(q_2,\sigma), \ldots, \delta(q_n, \sigma)\rangle\), where \(\delta\) is the transition function of the input. The initial state is the identity function, and the result is complete.
residue :: (Ord n, Ord e, Enum n) => FSA n e -> FSA n e -> FSA n e Source #
(residue a b)
is equivalent to (difference a b)
.
In the context of an approximation and its source,
represents the strings accepted by the approximation
that should not be.
coresidue :: (Ord n, Ord e, Enum n) => FSA n e -> FSA n e -> FSA n e Source #
(coresidue a b)
is equivalent to (complement (residue a b))
.
In the context of an approximation and its source,
represents unmet constraints of the source.
Primitive ideals
primitiveIdeal2 :: (Ord n, Ord e) => FSA (n, [Symbol e]) e -> State (n, [Symbol e]) -> Set (State (n, [Symbol e])) Source #
The primitive two-sided ideal.
Since: 0.2
primitiveIdealL :: (Ord n, Ord e) => FSA (n, [Symbol e]) e -> State (n, [Symbol e]) -> Set (State (n, [Symbol e])) Source #
The primitive left ideal.
Since: 0.2
primitiveIdealR :: (Ord n, Ord e) => FSA n e -> State n -> Set (State n) Source #
The primitive right ideal.
Since: 0.2
Transformations
flatIntersection :: (Enum n, Ord n, NFData n, Ord e, NFData e) => [FSA n e] -> FSA n e Source #
Intersect all given automata, in parallel if possible. An empty intersection is undefined. In theory it should be the total language over the total alphabet, but the latter cannot be defined. Intermediate results are evaluated to normal form.
flatUnion :: (Enum n, Ord n, NFData n, Ord e, NFData e) => [FSA n e] -> FSA n e Source #
Union all given automata, in parallel if possible. An empty union is defined as the empty language over an empty alphabet. Intermediate results are evaluated to normal form.
reverse :: (Ord e, Ord n) => FSA n e -> FSA n e Source #
The reversal of an automaton accepts the reversals of all strings accepted by the original.
complement :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e Source #
Returns an FSA
accepting all and only those strings not
accepted by the input.
complementDeterministic :: (Ord e, Ord n) => FSA n e -> FSA n e Source #
Returns the complement
of a deterministic FSA
.
The precondition that the input is deterministic
is not checked.
determinize :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e Source #
Returns a deterministic automaton representing the same stringset as the potentially nondeterministic input.
Minimization
minimize :: (Ord e, Ord n) => FSA n e -> FSA (Set (Set n)) e Source #
Returns a deterministic FSA
recognizing the same stringset
as the input, with a minimal number of states.
minimizeDeterministic :: (Ord e, Ord n) => FSA n e -> FSA (Set n) e Source #
Returns a deterministic FSA
recognizing the same stringset
as the input, with a minimal number of states.
The precondition that the input is deterministic
is not checked.
normalize :: (Ord e, Ord n) => FSA n e -> FSA Integer e Source #
Returns a normal form of the input.
An FSA is in normal form if it is minimal and deterministic,
and contains neither unreachable states nor nonaccepting sinks.
Node labels are irrelevant, so Int
is used as a small
representation.
trimUnreachables :: (Ord e, Ord n) => FSA n e -> FSA n e Source #
The input automaton with unreachable states removed.
Since: 1.0
Equivalence Classes
minimizeOver :: (Ord e, Ord n) => (FSA n e -> Set (Set (State n))) -> FSA n e -> FSA (Set n) e Source #
Returns a non-necessarily deterministic FSA
minimized over a given relation.
Some, but not all, relations do guarantee deterministic output.
The precondition that the input is deterministic
is not checked.
nerode :: (Ord e, Ord n) => FSA n e -> Set (Set (State n)) Source #
Two strings \(u\) and \(v\) are equivalent iff for all strings \(w\), \(uw\) and \(vw\) lead to states in the same equivalence class.
hEquivalence :: (Ord n, Ord e) => FSA (n, [Symbol e]) e -> Set (Set (State (n, [Symbol e]))) Source #
Given an automaton whose syntactic monoid is \(M\), two strings \(u\) and \(v\) are equivalent if \(Mu=Mv\) and \(uM=vM\).
Since: 0.2
jEquivalence :: (Ord e, Ord n) => FSA ([Maybe n], [Symbol e]) e -> Set (Set (State ([Maybe n], [Symbol e]))) Source #
Given an automaton whose syntactic monoid is \(M\), two strings \(u\) and \(v\) are equivalent iff \(MuM=MvM\)
trivialUnder :: (FSA n e -> Set (Set (State n))) -> FSA n e -> Bool Source #
An automaton is considered trivial under some equivalence relation if each of its equivalence classes is singleton.
Since: 0.2
Alphabetic Transformations
extendAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a b -> FSA (Maybe Integer, Maybe a) b Source #
Add missing symbols to the alphabet of an automaton. The result is an automaton with at least the provided alphabet that licenses exactly the same set of strings as the input.
semanticallyExtendAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a (Maybe b) -> FSA a (Maybe b) Source #
tierify :: (Ord a, Ord b) => Set b -> FSA a (Maybe b) -> FSA a (Maybe b) Source #
Convert a semantic automaton that represents a Local constraint into a new one that represents the same constraint in the associated Tier-Local class.
contractAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a b -> FSA a b Source #
Remove symbols from the alphabet of an automaton.
forceAlphabetTo :: (Ord a, Ord b) => Set b -> FSA a b -> FSA (Maybe Integer, Maybe a) b Source #
Ignore the alphabet of an automaton and use a given alphabet instead.
desemantify :: (Ord a, Ord b) => FSA a (Maybe b) -> FSA a b Source #
Remove the semantic Nothing
edges from an automaton and reflect this
change in the type.
renameSymbolsBy :: (Ord e, Ord e1, Ord n) => (e -> e1) -> FSA n e -> FSA n e1 Source #
Transform the edge labels of an automaton using a given function. If this function is not injective, the resulting FSA may not be deterministic even if the original was.
Transformations of State
labels
renameStatesBy :: (Ord e, Ord n, Ord n1) => (n -> n1) -> FSA n e -> FSA n1 e Source #
Transform the node labels of an automaton using a given function. If this function is not injective, the resulting FSA may not be deterministic even if the original was.
renameStates :: (Ord e, Ord n, Ord n1, Enum n1) => FSA n e -> FSA n1 e Source #
Equivalent to renameStatesBy
\(f\),
where \(f\) is an arbitrary injective function.
Miscellaneous
A vertex of the graph representation of an FSA
is a State
,
which can be labelled with any arbitrary value, so long as every
vertex of a single automaton is labelled with a distinct value
of the same type.
Instances
Monad State Source # | |
Functor State Source # | |
Applicative State Source # | |
Eq n => Eq (State n) Source # | |
Ord n => Ord (State n) Source # | |
Read n => Read (State n) Source # | |
Show n => Show (State n) Source # | |
Semigroup n => Semigroup (State n) Source # | |
Monoid n => Monoid (State n) Source # | |
NFData n => NFData (State n) Source # | |
The label of a Transition
.
Epsilon | The edge may be taken without consuming input. |
Symbol e | The edge requires consuming this symbol. |
unsymbols :: (Collapsible s, Container c e, Monoid c) => s (Symbol e) -> c Source #
Remove Epsilon
from a Collapsible
of Symbol
and present the unwrapped results as a new Container
.
data Transition n e Source #
The edges of an FSA
.
Instances
module LTK.Containers