Make constructing NFAs and DFAs more usable

This commit is contained in:
Joscha 2019-10-26 10:12:35 +00:00
parent 02bf60b095
commit c44f4c090b
2 changed files with 20 additions and 5 deletions

View file

@ -56,8 +56,10 @@ dfa stateMap entryState =
let myDfa = Dfa{stateMap=stateMap, entryState=entryState}
in if integrityCheck myDfa then Just myDfa else Nothing
dfa' :: (Ord s) => [(s, State s t)] -> s -> Maybe (Dfa s t)
dfa' states entryState = dfa (Map.fromList states) entryState
dfa' :: (Ord s, Ord t) => [(s, [(t, s)], s, Bool)] -> s -> Maybe (Dfa s t)
dfa' states entryState =
let stateList = map (\(s, ts, dt, a) -> (s, State (Map.fromList ts) dt a)) states
in dfa (Map.fromList stateList) entryState
{-
- Executing

View file

@ -7,6 +7,8 @@ module Rextra.Nfa (
, specialTokens
, accepts
-- ** Constructing
, only
, allExcept
, nfa
, nfa'
-- ** Properties
@ -105,9 +107,20 @@ nfa stateMap entryState exitStates =
let myNfa = Nfa{stateMap=stateMap, entryState=entryState, exitStates=exitStates}
in if integrityCheck myNfa then Just myNfa else Nothing
-- | A version of 'nfa' using argument formats that should be easier to work with.
nfa' :: (Ord s) => [(s, State s t)] -> s -> [s] -> Maybe (Nfa s t)
nfa' states entryState exitStates = nfa (Map.fromList states) entryState (Set.fromList exitStates)
only :: (Ord t) => [t] -> TransitionCondition t
only = Only . Set.fromList
allExcept :: (Ord t) => [t] -> TransitionCondition t
allExcept = AllExcept . Set.fromList
nfa' :: (Ord s)
=> [(s, [(TransitionCondition t, s)], [s])]
-> s
-> [s]
-> Maybe (Nfa s t)
nfa' states entryState exitStates =
let stateList = map (\(s, ts, et) -> (s, State ts (Set.fromList et))) states
in nfa (Map.fromList stateList) entryState (Set.fromList exitStates)
{-
- Executing