21import Control.Applicative
24import Data.Functor.Identity
26import Data.List as List
28import Data.Traversable
29import GHC.Exts (Constraint)
30import qualified Language.Haskell.TH as TH
34type K_Monad = Type -> Type
36type K_Trans = K_Monad -> K_Monad
49 (trans :: K_Trans) (m :: K_Monad)
53 HandleSuper eff p trans =>
54 Handle eff p (trans :: K_Trans) | eff trans -> p
56 handling :: Monad m => (HandleConstraint eff p trans m => r) -> r
58type LensLike f s t a b = (a -> f b) -> s -> f t
60type Lens s t a b = forall f. Functor f => LensLike f s t a b
62type Lens' s a = Lens s s a a
64newtype ReifiedLens s t a b = Lens (Lens s t a b)
66type ReifiedLens' s a = ReifiedLens s s a a
68class HasLens tag outer inner | tag outer -> inner where
69 lensOf :: Lens' outer inner
71instance HasLens a a a where
75view :: LensLike (Const a) s t a b -> s -> a
76view l = coerce (l Const)
79over :: LensLike Identity s t a b -> (a -> b) -> s -> t
85 HCons :: x -> HList xs -> HList (x ': xs)
87type KindOf (a :: k) = k
89type family TagsK (p :: Type) :: [Type]
90type family Tags (p :: Type) :: HList (TagsK p)
94type instance TagsK () = '[]
95type instance TagsK (Tagged t a) = '[KindOf t]
96type instance TagsK (Tagged t0 a, Tagged t1 b) = '[KindOf t0, KindOf t1]
100type instance Tags () = 'HNil
101type instance Tags (Tagged t a) = 'HCons t 'HNil
102type instance Tags (Tagged t0 a, Tagged t1 b) = 'HCons t0 ('HCons t1 'HNil)
107 names = [1..] >>= flip replicateM ['a'..'z']
108 varNames <- traverse TH.newName (take tupCount names)
110 for (List.drop 2 (List.inits varNames)) $
112 let n = List.length varNames'
113 tagsInstances <- for [() | n > 2] $ \() -> do
114 tag <- TH.newName "tag"
116 (cur:rest) = varNames'
117 tupTy = foldl TH.AppT (TH.ConT (TH.tupleTypeName n))
118 ( TH.ConT ''Tagged `TH.AppT` TH.VarT tag `TH.AppT` TH.VarT cur :
120 tupTy' = foldl TH.AppT (TH.ConT (TH.tupleTypeName (n-1)))
123 TH.TySynInstD ''TagsK (TH.TySynEqn [tupTy]
124 ( TH.PromotedConsT `TH.AppT` (TH.ConT ''KindOf `TH.AppT` TH.VarT tag) `TH.AppT`
125 (TH.ConT ''TagsK `TH.AppT` tupTy') ))
130 names = [1..] >>= flip replicateM ['a'..'z']
131 varNames <- traverse TH.newName (take tupCount names)
133 for (List.drop 2 (List.inits varNames)) $
135 let n = List.length varNames'
136 tagsInstances <- for [() | n > 2] $ \() -> do
137 tag <- TH.newName "tag"
139 (cur:rest) = varNames'
140 tupTy = foldl TH.AppT (TH.ConT (TH.tupleTypeName n))
141 ( TH.ConT ''Tagged `TH.AppT` TH.VarT tag `TH.AppT` TH.VarT cur :
143 tupTy' = foldl TH.AppT (TH.ConT (TH.tupleTypeName (n-1)))
146 TH.TySynInstD ''Tags (TH.TySynEqn [tupTy]
147 ( TH.PromotedT 'HCons `TH.AppT` TH.VarT tag `TH.AppT`
148 (TH.ConT ''Tags `TH.AppT` tupTy') ))
150 hasLensInstances <- for [0..n-1] $ \k -> do
151 tag <- TH.newName "tag"
153 (prev, cur:next) = List.splitAt k varNames'
154 tupTy = foldl TH.AppT (TH.ConT (TH.tupleTypeName n))
155 ( map TH.VarT prev ++
156 [TH.ConT ''Tagged `TH.AppT` TH.VarT tag `TH.AppT` TH.VarT cur] ++
158 cur' <- TH.newName "x"
161 TH.InstanceD Nothing []
162 (TH.ConT ''HasLens `TH.AppT` TH.VarT tag `TH.AppT` tupTy `TH.AppT` TH.VarT cur)
166 ( map TH.VarP prev ++
167 [TH.ConP 'Tagged [TH.VarP cur]] ++
170 TH.VarE 'fmap `TH.AppE`
171 (TH.LamE [TH.VarP cur']
173 ( map TH.VarE prev ++
174 [TH.ConE 'Tagged `TH.AppE` TH.VarE cur'] ++
175 map TH.VarE next ))) `TH.AppE`
176 (TH.VarE f `TH.AppE` TH.VarE cur) )
178 TH.PragmaD (TH.InlineP 'lensOf TH.Inline TH.FunLike TH.AllPhases) ]
179 return $ tagsInstances ++ hasLensInstances
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list