@@ -6,11 +6,14 @@ module Control.Has
12import Control.Lens
11import Control.Lens
12+-- reexport all from Reader except Control.Monad etc
13+-- to correspond mtl-2.3+
13import Control.Monad.Reader as X
14import Control.Monad.Reader as X
15+ ( MonadReader(..), asks, Reader, runReader, mapReader, withReader
16+ , ReaderT(ReaderT), runReaderT, mapReaderT, withReaderT )
14import Data.Kind
17import Data.Kind
15import GHC.Generics as G
18import GHC.Generics as G
@@ -21,11 +24,11 @@ class Has x r where
21instance {-# OVERLAPS #-} Has r r where
24instance {-# OVERLAPS #-} Has r r where
24−type family HasAll (xs :: [*]) r :: Constraint where
27+type family HasAll (xs :: [Type]) r :: Constraint where
25 HasAll '[] r = ()
28 HasAll '[] r = ()
26 HasAll (x ': xs) r = (Has x r, HasAll xs r)
29 HasAll (x ': xs) r = (Has x r, HasAll xs r)
28−data LensList (r :: *) (es :: [*]) where
31+data LensList (r :: Type) (es :: [Type]) where
29 LLNil :: LensList r '[]
32 LLNil :: LensList r '[]
30 LLCons :: Lens r r e e -> LensList r es -> LensList r (e ': es)
33 LLCons :: Lens r r e e -> LensList r es -> LensList r (e ': es)
@@ -33,7 +36,7 @@ mapLL :: Lens s s r r -> LensList r es -> LensList s es
33mapLL _ LLNil = LLNil
36mapLL _ LLNil = LLNil
34mapLL m (LLCons l ls) = LLCons (m . l) (mapLL m ls)
37mapLL m (LLCons l ls) = LLCons (m . l) (mapLL m ls)
36−type family (xs :: [*]) ++ (ys :: [*]) :: [*] where
39+type family (xs :: [Type]) ++ (ys :: [Type]) :: [Type] where
37 '[] ++ ys = ys
40 '[] ++ ys = ys
38 (x ': xs) ++ ys = x ': (xs ++ ys)
41 (x ': xs) ++ ys = x ': (xs ++ ys)
@@ -44,7 +47,7 @@ concatLL (LLCons l ls) ys = LLCons l (concatLL ls ys)
44-- borrowed from Data.HSet.Get
47-- borrowed from Data.HSet.Get
45data N = Z | S N
48data N = Z | S N
47−type family IndexOf (x :: *) (xs :: [*]) :: N where
50+type family IndexOf (x :: Type) (xs :: [Type]) :: N where
48 IndexOf x (x ': _) = 'Z
51 IndexOf x (x ': _) = 'Z
49 IndexOf x (_ ': xs) = 'S (IndexOf x xs)
52 IndexOf x (_ ': xs) = 'S (IndexOf x xs)
@@ -53,8 +56,8 @@ class (i ~ IndexOf e es) => GetLens e es i | i es -> e where
54type Elem e es = GetLens e es (IndexOf e es)
57type Elem e es = GetLens e es (IndexOf e es)
56−class Fieldy (r :: *) where
59+class Fieldy (r :: Type) where
57− type Fields r :: [*]
60+ type Fields r :: [Type]
58 type Fields r = GFields (Rep r)
61 type Fields r = GFields (Rep r)
59 lensList :: LensList r (Fields r)
62 lensList :: LensList r (Fields r)
60 default lensList
63 default lensList
@@ -65,9 +68,7 @@ class Fieldy (r :: *) where
65instance {-# OVERLAPPABLE #-} (Fieldy r, Elem e (Fields r)) => Has e r where
68instance {-# OVERLAPPABLE #-} (Fieldy r, Elem e (Fields r)) => Has e r where
66 part = getLens lensList
69 part = getLens lensList
68−class (MonadReader r m, HasAll es r)
69− => MonadHas (es :: [*]) (r :: *) (m :: * -> *) | m -> r
70−instance (MonadReader r m, HasAll es r) => MonadHas es r m
71+type MonadHas es r m = (MonadReader r m, HasAll es r)
72instance GetLens e (e ': es) 'Z where
73instance GetLens e (e ': es) 'Z where
73 getLens (LLCons l _) = l
74 getLens (LLCons l _) = l
@@ -76,16 +77,16 @@ instance (GetLens e' es n, 'S n ~ IndexOf e' (e ': es))
76 => GetLens e' (e ': es) ('S n) where
77 => GetLens e' (e ': es) ('S n) where
77 getLens (LLCons _ ls) = getLens ls
78 getLens (LLCons _ ls) = getLens ls
79−class GFieldy (r :: * -> *) where
80+class GFieldy (r :: Type -> Type) where
80− type GFields r :: [*]
81+ type GFields r :: [Type]
81 glensList :: LensList (r x) (GFields r)
82 glensList :: LensList (r x) (GFields r)
83instance GFieldyProduct r => GFieldy (D1 d (C1 c r)) where
84instance GFieldyProduct r => GFieldy (D1 d (C1 c r)) where
84 type GFields (D1 d (C1 c r)) = GFieldsProduct r
85 type GFields (D1 d (C1 c r)) = GFieldsProduct r
85 glensList = mapLL (iso unM1 M1 . iso unM1 M1) glensListProduct
86 glensList = mapLL (iso unM1 M1 . iso unM1 M1) glensListProduct
87−class GFieldyProduct (r :: * -> *) where
88+class GFieldyProduct (r :: Type -> Type) where
88− type GFieldsProduct r :: [*]
89+ type GFieldsProduct r :: [Type]
89 glensListProduct :: LensList (r x) (GFieldsProduct r)
90 glensListProduct :: LensList (r x) (GFieldsProduct r)
91instance (GFieldyProduct r, GFieldyProduct s) => GFieldyProduct (r :*: s) where
92instance (GFieldyProduct r, GFieldyProduct s) => GFieldyProduct (r :*: s) where
@@ -104,7 +105,7 @@ instance GFieldyProduct (S1 s (K1 k r)) where
104 type GFieldsProduct (S1 s (K1 k r)) = '[r]
105 type GFieldsProduct (S1 s (K1 k r)) = '[r]
105 glensListProduct = LLCons (iso unM1 M1 . iso unK1 K1) LLNil
106 glensListProduct = LLCons (iso unM1 M1 . iso unK1 K1) LLNil
107−type family AllElem (xs :: [*]) (es :: [*]) :: Constraint where
108+type family AllElem (xs :: [Type]) (es :: [Type]) :: Constraint where
108 AllElem '[] _ = ()
109 AllElem '[] _ = ()
109 AllElem (x ': xs) es = (Elem x es, AllElem xs es)
110 AllElem (x ': xs) es = (Elem x es, AllElem xs es)