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
Code online here.
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!
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!