{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreicqdl746au2gtxhkjb5etzutifxu6hmqdja7weslvzjdqvfyiipxa",
"uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mkrvcrjt5jm2"
},
"path": "/t/free-like-data-structure/14012#post_4",
"publishedAt": "2026-05-01T08:42:44.000Z",
"site": "https://discourse.haskell.org",
"textContent": "Trees are not just a particular example. Basically all free monads are trees. For example you could write the countdown example like this:\n\n\n import Numeric.Natural\n import Control.Monad\n data Tree a = Fork Natural [Tree a] | Leaf a deriving Functor\n instance Applicative Tree where\n pure = Leaf\n (<*>) = ap\n instance Monad Tree where\n Leaf x >>= k = k x\n Fork n xs >>= k = Fork n (map (>>= k) xs)\n\n get :: Tree Natural\n get = Fork 0 (map Leaf [0..])\n\n put :: Natural -> Tree ()\n put n = Fork (1 + n) [Leaf ()]\n\n countdown :: Tree ()\n countdown = do\n x <- get\n when (x > 0) $ do\n put (x - 1)\n countdown\n\n\nYou lose some type safety and performing the get operation now takes exponential time, but this essentially contains all the same information.",
"title": "Free-like data structure:"
}