blob: 7f1a494a20a8a3f50d49a346222c6094b3731dfc (
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
42
43
44
45
46
47
|
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ImportQualifiedPost #-}
-- Exercise 13.2-ii
-- Use `GHC.Generics` to implement the function `exNihilo :: Maybe a`.
import GHC.Generics qualified as G
class GExNihilo a where
gexNihilo :: Maybe (a x)
instance GExNihilo G.U1 where
gexNihilo = Just G.U1
instance GExNihilo G.V1 where
gexNihilo = Nothing
instance GExNihilo (G.K1 _1 a) where
gexNihilo = Nothing
instance GExNihilo (a G.:+: b) where
gexNihilo = Nothing
instance GExNihilo (a G.:*: b) where
gexNihilo = Nothing
instance GExNihilo a => GExNihilo (G.M1 _x _y a) where
gexNihilo = G.M1 <$> gexNihilo
-- I could not figure this one out so had to look at the solution. I noticed
-- Sandy missed to provide the actual implementation of `exNihilo` so here is
-- my version:
exNihilo :: forall a. (G.Generic a, GExNihilo (G.Rep a)) => Maybe a
exNihilo = G.to <$> gexNihilo @(G.Rep a)
-- Example usage:
data MyUnit = MyUnit deriving (G.Generic, Show)
newtype MyType a = MyType a deriving (G.Generic, Show)
data MySum = MySum1 | MySum2 deriving (G.Generic, Show)
t1 = exNihilo @MyUnit -- Just MyUnit
t2 = exNihilo @(MyType ()) -- Nothing
t3 = exNihilo @MySum -- Nothing
|