newtype Leaf a = Leaf a deriving Show
data Branch a = Branch a a deriving Show
data Tree a = Zero a | Succ (Tree (Branch a)) deriving Show
tree0, tree1, tree2 :: Tree (Leaf Integer)
tree0 = Zero (Leaf 1)
tree1 = Succ (Zero (Branch (Leaf 1) (Leaf 2)))
tree2 = Succ (Succ (Zero (Branch (Branch (Leaf 1) (Leaf 2)) (Branch (Leaf 3) (Leaf 4)))))
mirror :: Tree (Leaf a) -> Tree (Leaf a)
mirror t = go t id
where go :: Tree b -> (b -> b) -> Tree b
go (Succ t) f = Succ (go t (\(Branch t1 t2) -> Branch (f t2) (f t1)))
go (Zero x) f = Zero (f x)