blob: e7d57dea708b9adbdb7878ee13bbd12bdac3f54b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DefaultSignatures #-}
module GMap (
-- * Generic map class
GMap(..)
) where
import GHC.Generics
--------------------------------------------------------------------------------
-- Generic map
--------------------------------------------------------------------------------
class GMap t where
gmap :: (a -> b) -> t a -> t b
default gmap :: (Generic1 t, GMap (Rep1 t)) => (a -> b) -> t a -> t b
gmap f = to1 . gmap f . from1
instance GMap Par1 where gmap f (Par1 x) = Par1 $ f x
instance GMap f => GMap (Rec1 f) where gmap f (Rec1 x) = Rec1 $ gmap f x
instance GMap U1 where gmap _ U1 = U1
instance GMap (K1 i c) where gmap _ (K1 x) = K1 x
instance (GMap a) => GMap (M1 i d a) where gmap f (M1 x) = M1 $ gmap f x
instance (GMap a, GMap b) => GMap (a :+: b) where
gmap f (L1 x) = L1 $ gmap f x
gmap f (R1 x) = R1 $ gmap f x
instance (GMap a, GMap b) => GMap (a :*: b) where
gmap f (x :*: y) = gmap f x :*: gmap f y
-- Base types instances
instance GMap [] where gmap = map
instance GMap Maybe where gmap = fmap
instance GMap ((,) a) where gmap f ~(x, y) = (x, f y)
|