External Publication
Visit Post

Free-like data structure:

Haskell Community [Unofficial] May 1, 2026
Source
Trees are not just a particular example. Basically all free monads are trees. For example you could write the countdown example like this: import Numeric.Natural import Control.Monad data Tree a = Fork Natural [Tree a] | Leaf a deriving Functor instance Applicative Tree where pure = Leaf (<*>) = ap instance Monad Tree where Leaf x >>= k = k x Fork n xs >>= k = Fork n (map (>>= k) xs) get :: Tree Natural get = Fork 0 (map Leaf [0..]) put :: Natural -> Tree () put n = Fork (1 + n) [Leaf ()] countdown :: Tree () countdown = do x <- get when (x > 0) $ do put (x - 1) countdown You lose some type safety and performing the get operation now takes exponential time, but this essentially contains all the same information.

Discussion in the ATmosphere

Loading comments...