I've been getting to grips with some Monads in Haskell recently, and to get some practice I plucked a Mealy Machine out of thin air and decided to implement it using the State Monad (it's a simple machine and a simple implementation, don't worry).
- The input is a string of characters from
+, "plus". Output a
-, "minus". Output a
n, "toggle negated". Until "toggled off", flip the sign of the output from
e, "toggle enabled". Until "toggled back on", output
- The output is just a sequence of the same length as the input, containing
-1s as outputted by the DFA. In the code below, the output is actually the sum of this sequence, acting as a quick checksum. All states are accepting states.
For example, an input of
+-n+-e+e+ would result in
[1, -1, 0, -1, 1, 0, 0, 0, -1].
To unambiguously define the behaviour, here's a pretty messy state transition diagram:
If "posate" (as the opposite of negate) isn't a word already, it is now! The transitions are labelled with the "english name", the symbol used to represent this transition, and the "output" from the machine. All states are accepting states, I left out the conventional double rings.
At last, the code:
import Control.Monad.Trans.State.Lazy import Control.Monad (foldM) data Input = Plus | Minus | ToggleNegate | ToggleEnabled type Emission = Integer type Accum = [Emission] type Output = [Emission] type Negated = Bool type Enabled = Bool toInput :: Char -> Input toInput '+' = Plus toInput '-' = Minus toInput 'n' = ToggleNegate toInput 'e' = ToggleEnabled toInput _ = error "Invalid input representation" -- Determine new state of state machine along with transition emission step :: (Negated, Enabled, Input) -> (Negated, Enabled, Emission) step (n, e, ToggleNegate) = (not n, e, 0) step (n, e, ToggleEnabled) = (n, not e, 0) step (n, False, i) = (n, False, 0) step (n, e, Plus) = (n, e, if n then -1 else 1) step (n, e, Minus) = (n, e, if n then 1 else -1) -- Helper function for "evaluate"'s foldM mapEmissions :: Accum -> Input -> State (Negated, Enabled) Output mapEmissions accum input = do (negated, enabled) <- get let (negated', enabled', emission) = step (negated, enabled, input) put (negated', enabled') return (accum ++ [emission]) -- Process an input string and return the result -- (False, True) is the start state: (not negated, enabled) evaluate :: [Input] -> Output evaluate inputs = evalState (foldM mapEmissions  inputs) (False, True) -- Convenience function for output formatting shouldEqual :: String -> Integer -> IO () shouldEqual input expected = do let actual = (sum . evaluate . map toInput) input putStrLn $ "Expected " ++ show expected ++ ", got " ++ show actual ++ ": " ++ input main :: IO () main = do "+-n--n" `shouldEqual` 2 "+e----e++" `shouldEqual` 3 "-n++e++e--+-n++" `shouldEqual` 1
I'm happy to hear any and all critiques and advice, but in particular:
- Am I using the State monad idiomatically? Can I write any bits of code more elegantly?
- Is this an appropriate use for this monad? This isn't a particularly complex task, but I feel like it makes passing around the DFA's state simpler.
Thanks for taking the time to read, and Happy New Year!