@@ -0,0 +1,53 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+{-# LANGUAGE TypeApplications #-}
7+Example adapted from SYB
8+========================
11+module Main (main) where
13+import Control.Lens.Fold (toListOf)
14+import Control.Monad (void)
15+import Data.Generics.Product
20+main = void $ runTestTT tests
22+-- A parameterised datatype for binary trees with data at the leafs
23+data Tree a w = Leaf a
24+ | Fork (Tree a w) (Tree a w)
25+ | WithWeight (Tree a w) w
26+ deriving (Show, Generic)
29+mytree :: Tree Int Int
30+mytree = Fork (WithWeight (Leaf 42) 1)
31+ (WithWeight (Fork (Leaf 88) (Leaf 37)) 2)
33+-- Print everything like an Int in mytree
34+-- In fact, we show two attempts:
35+-- 1. print really just everything like an Int
36+-- 2. print everything wrapped with Leaf
37+-- So (1.) confuses leafs and weights whereas (2.) does not.
39+tests = show ( toListOf (typesDeep @Int) mytree
40+ , toListOf (param @1) mytree
43+-- original code from SYB:
44+--tests = show ( listify (\(_::Int) -> True) mytree
45+-- , everything (++) ([] `mkQ` fromLeaf) mytree
48+-- fromLeaf :: Tree Int Int -> [Int]
49+-- fromLeaf (Leaf x) = [x]
53+output = "([42,1,88,37,2],[42,88,37])"