
I made this simple state machine combinator library today. I think it works as a simple example of a good use for GADTs. https://gist.github.com/1507107 Daniel

On Wed, 2011-12-21 at 18:34 +0000, Daniel Waterworth wrote:
I made this simple state machine combinator library today. I think it works as a simple example of a good use for GADTs.
Any way to do something along the lines of type StateChange a = SC a a handleChange :: Monad m => StateChange ConnectionState -> m () handleChange (SC Closed Opening) = return () handleChange (SC Opening Closed) = return () handleChange (SC Closed Open) = return () where the last line would yield a type checking error at compile time, and the compiler can warn about handleChange not being exhaustive (for the valid state change steps)? Nicolas

I suspect it's possible to do something like that with multi parameter
type classes, but I tend to find that trying to do pseudo dependently
typed things in Haskell tends to bite me sooner or later.
Daniel
On 21 December 2011 22:24, Nicolas Trangez
On Wed, 2011-12-21 at 18:34 +0000, Daniel Waterworth wrote:
I made this simple state machine combinator library today. I think it works as a simple example of a good use for GADTs.
Any way to do something along the lines of
type StateChange a = SC a a
handleChange :: Monad m => StateChange ConnectionState -> m () handleChange (SC Closed Opening) = return () handleChange (SC Opening Closed) = return () handleChange (SC Closed Open) = return ()
where the last line would yield a type checking error at compile time, and the compiler can warn about handleChange not being exhaustive (for the valid state change steps)?
Nicolas
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Daniel Waterworth
I made this simple state machine combinator library today. I think it works as a simple example of a good use for GADTs.
Aren't your examples all special cases of the generic automaton arrow? There are two ways to represent it, both with their advantages and disadvantages: newtype Auto a b = Auto (a -> (b, Auto a b)) countFrom :: Int -> Auto a Int countFrom n = Auto (\_ -> (n, countFrom (succ n))) or: data Auto a b = forall s. Auto s ((a, s) -> (b, s)) countFrom :: Int -> Auto a Int countFrom n0 = Auto n0 (\(_, s) -> (s, succ s)) These state machines have local state and can be composed using applicative and arrow interfaces: liftA2 (+) (countFrom 3) (countFrom 5) proc x -> do n1 <- countFrom 10 -< () n2 <- someOtherMachine -< x anotherMachine -< n1 + n2 Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

I must admit, I haven't looked into arrows in a great deal of detail,
perhaps I should.
Daniel
2011/12/22 Ertugrul Söylemez
Daniel Waterworth
wrote: I made this simple state machine combinator library today. I think it works as a simple example of a good use for GADTs.
Aren't your examples all special cases of the generic automaton arrow? There are two ways to represent it, both with their advantages and disadvantages:
newtype Auto a b = Auto (a -> (b, Auto a b))
countFrom :: Int -> Auto a Int countFrom n = Auto (\_ -> (n, countFrom (succ n)))
or:
data Auto a b = forall s. Auto s ((a, s) -> (b, s))
countFrom :: Int -> Auto a Int countFrom n0 = Auto n0 (\(_, s) -> (s, succ s))
These state machines have local state and can be composed using applicative and arrow interfaces:
liftA2 (+) (countFrom 3) (countFrom 5)
proc x -> do n1 <- countFrom 10 -< () n2 <- someOtherMachine -< x anotherMachine -< n1 + n2
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Daniel Waterworth
-
Ertugrul Söylemez
-
Nicolas Trangez