ekmett / machines

Networks of composable stream transducers
Other
339 stars 46 forks source link

Cochoice instance for Mealy #98

Open shachaf opened 6 years ago

shachaf commented 6 years ago

Retracing some paths in Process Algebra defines a trace/feedback operator for Mealy machines that connects an output back to an input. This is like ArrowLoop but with a sum instead of a product. It can be implemented like this:

trace :: forall e a b. Mealy (Either e a) (Either e b) -> Mealy a b
trace m0 = Mealy $ \x -> go m0 (Right x)
  where
    go :: Mealy (Either e a) (Either e b) -> Either e a -> (b, Mealy a b)
    go (Mealy f) x = case f x of
      (Left e, m) -> go m (Left e)
      (Right y, m) -> (y, trace m)

Maybe this would be useful for other machines as well.

shachaf commented 6 years ago

Oh, this would be a Cochoice instance. Which it looks like Mealy doesn't have. Probably worth adding.

ekmett commented 6 years ago

Did we finally find a use for Cochoice?

ekmett commented 6 years ago

That is a rather sensible instance even.

ekmett commented 6 years ago

Ah, it's the (->) instance in disguise if you view Mealy a b, morally as a [a] -> b.