@hackage mealy-arrow0.1.0.0

Monadic Mealy machines with Arrow, ArrowChoice, and ArrowLoop

mealy-arrow

Monadic Mealy machines with Category, Arrow, ArrowChoice, ArrowLoop, and Profunctor instances.

The type

newtype Auto m a b = Auto { step :: a -> m (b, Auto m a b) }

Each step consumes an input, produces an output in monad m, and returns the continuation. The state is the closure — there is no external state type. This is the Nu (greatest fixpoint / final coalgebra) encoding of a Mealy machine.

At Identity it is a pure state machine. At IO it can do effects. The full Arrow stack lets you branch (Either-routing via ArrowChoice), fan (&&&), parallelise (***), and feed back (ArrowLoop with MonadFix).

Quick example

import Control.Arrow.Mealy
import Data.Functor.Identity

counter :: Auto Identity Int Int
counter = stateful 0 $ \total x -> let s = total + x in (s, s)

>>> runIdentity $ stepN counter [1,2,3,4,5]
[1,3,6,10,15]

How is this different from the mealy package?

The Hackage package mealy is a pure, existentially-quantified fold triple for online statistics. It has Category and Profunctor but no Arrow, no effects, no branching. mealy-arrow is for interactive systems where each step may have effects and the control flow may branch.

See also

The mu-nu package builds on mealy-arrow to provide Mu–Nu pairings: pair a finite inspectable tree with an Auto machine, connected by a monoid homomorphism. This enables static analysis of compositional system architecture before execution.