76f387d636c553beef9d311c474dee83e2f5ed5e
[ghc.git] / testsuite / tests / generics / Uniplate / GUniplate.hs
1 {-# LANGUAGE MultiParamTypeClasses #-}
2 {-# LANGUAGE TypeSynonymInstances #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE TypeOperators #-}
6 {-# LANGUAGE DefaultSignatures #-}
7 {-# LANGUAGE IncoherentInstances #-} -- necessary, unfortunately
8
9 module GUniplate where
10
11 import GHC.Generics
12
13 --------------------------------------------------------------------------------
14 -- Generic Uniplate
15 --------------------------------------------------------------------------------
16
17 class Uniplate' f b where
18 children' :: f a -> [b]
19
20 instance Uniplate' U1 a where
21 children' U1 = []
22
23 instance Uniplate' (K1 i a) a where
24 children' (K1 a) = [a]
25
26 instance Uniplate' (K1 i a) b where
27 children' (K1 _) = []
28
29 instance (Uniplate' f b) => Uniplate' (M1 i c f) b where
30 children' (M1 a) = children' a
31
32 instance (Uniplate' f b, Uniplate' g b) => Uniplate' (f :+: g) b where
33 children' (L1 a) = children' a
34 children' (R1 a) = children' a
35
36 instance (Uniplate' f b, Uniplate' g b) => Uniplate' (f :*: g) b where
37 children' (a :*: b) = children' a ++ children' b
38
39
40 class Uniplate a where
41 children :: a -> [a]
42 default children :: (Generic a, Uniplate' (Rep a) a) => a -> [a]
43 children = children' . from
44
45
46 -- Base types instances
47 instance Uniplate Char where children _ = []
48 instance Uniplate Int where children _ = []
49 instance Uniplate Float where children _ = []
50
51 instance Uniplate [a] where
52 children [] = []
53 children (_:t) = [t]