Add DFA and NFA representations

This commit is contained in:
Joscha 2019-10-24 12:28:47 +00:00
parent a6fdf546c3
commit 4cae9af848
3 changed files with 84 additions and 0 deletions

36
src/Rextra/Dfa.hs Normal file
View file

@ -0,0 +1,36 @@
module Rextra.Dfa where
-- TODO don't export internals
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
data State s t = State
{ transitions :: Map.Map t s
, defaultTransition :: s
, accepting :: Bool
}
data Dfa s t = Dfa
{ dfaStates :: Map.Map s (State s t)
, dfaEntryState :: s
}
{-
- Constructing and modifying a DFA
-}
-- TODO
{-
- "Executing" a DFA
-}
getState :: (Ord s) => Dfa s t -> s -> State s t
getState dfa s = dfaStates dfa Map.! s
transition :: (Ord s, Ord t) => Dfa s t -> s -> t -> s
transition dfa s t =
let state = getState dfa s
in case transitions state Map.!? t of
(Just nextState) -> nextState
Nothing -> defaultTransition state