Re:Type-safe Transducers in Clojure, Scala, and Haskell
by tel on Thursday October 30, 2014 @09:19AM
(#8532710)
You can just include local state.
data Fold i o = forall st . Fold
{ merge :: st -> i -> st
, this :: st
, view :: st -> o
}
type (i ~> o) = forall r . Fold o r -> Fold i r
-- compare
type Trans i o = forall r . (r -> o -> r) -> (r -> i -> r)
Now each "step" has local pure state and no other step can break abstraction barriers and view it. This also gives you all of the effects of the indexed state passing Rich called untypeable.Fold is equivalent to an infinite state Moore machine, so a stack of composed transducers applied to a base Moore machine which produces whatever result you want can be compiled a really efficient form.
Early termination can be done by changing `merge` to `merge :: st -> i -> Either o st`.
Re:Type-safe Transducers in Clojure, Scala, and Haskell
by jules on Thursday October 30, 2014 @05:02AM
(#8531971)
You can do that, but it's more elegant if you define a functional stateful transducer analogous to scanl for lists.
Re:Type-safe Transducers in Clojure, Scala, and Haskell
by davdar on Thursday October 30, 2014 @08:51AM
(#8532590)
That's like saying "your program is more elegant if it has no monads". It's an incorrect statement. The monadic version is perfectly elegant. Even better: it's the right one.
Re:Type-safe Transducers in Clojure, Scala, and Haskell
by jules on Thursday October 30, 2014 @10:36AM
(#8533148)
By the same logic scanl should just have the type:
(b -> IO a) -> [b] -> IO [a]
instead of (a -> b -> a) -> a -> [b] -> [a]
Just putting the whole thing in the IO/ST monad isn't the right solution when you need a very specific form of local state.
Couldn't you just parametrize over IO/ST/etc.? E.g. something like
I don't have a great mental model of transducers yet, so I'm not confident about that.