Skip to content

Instantly share code, notes, and snippets.

@haitlahcen
Created December 13, 2018 17:14
Show Gist options
  • Select an option

  • Save haitlahcen/667f57c15c073b1e728a500d015d3415 to your computer and use it in GitHub Desktop.

Select an option

Save haitlahcen/667f57c15c073b1e728a500d015d3415 to your computer and use it in GitHub Desktop.
hylo
data State
= Init
| Header Int
Int
| Meta
data Tree a
= Node Int (V.Vector (Tree a))
| Leaf a
deriving (Show)
makeBaseFunctor ''Tree
algSum :: TreeF Int Int -> Int
algSum (NodeF x y) = sum y
algSum (LeafF x) = x
coalg :: ([State], V.Vector Int) -> TreeF Int ([State], V.Vector Int)
coalg ([Init], v) =
let (h@(Header x _), t) = header v
in NodeF x $ V.singleton ([h], t)
coalg ([Header 0 0], v) = NodeF 0 V.empty
coalg (Header 0 0:xs, v) = NodeF 0 $ V.singleton (xs, v)
coalg (Header 0 y:xs, v) =
NodeF 0 $ V.cons ([Meta], v) $ V.singleton (Header 0 (y - 1):xs, V.tail v)
coalg (Header x y:xs, v) =
let (h, t) = header v
in NodeF x $ V.singleton (h:Header (x - 1) y:xs, t)
coalg ([Meta], v) = LeafF $ V.head v
header :: V.Vector Int -> (State, V.Vector Int)
header v = (Header (v V.! 0) (v V.! 1), V.drop 2 v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment