A mealy machine is just a stateful function. Hence, two mealy machines can be composed using simple function composition. A moore machine is a restricted mealy machine with an initial output value. Is there a way to compose two moore machines? This is what I tried in Haskell:
type Mealy a b = a -> b -- a stateful function, probably using unsafePerformIO
type Moore a b = (b, Mealy a b) -- output must be consistent with current state
composeMoore :: (c, b -> c) -> (b, a -> b) -> (c, a -> c)
composeMoore (x, f) (_, g) = (x, f . g) -- is this correct?
composeMoore (_, f) (y, g) = (f y, f . g) -- or is this correct?
I believe that they are both wrong. In fact, I believe that it's not possible to compose two moore machines. However, I might be wrong. Is there a correct way of composing moore machines?
Defintion: The composition of moore machines is an operation of the type (.) :: Moore b c -> Moore a b -> Moore a c
for which the law of associativity (i.e. h . (g . f) = (h . g) . f
) holds true.
Note: This is just a theoretical question. I am not actually using Haskell to write stateful functions.