@@ -16,20 +16,17 @@ module Ether.Internal
16 , HandleSuper
16 , HandleSuper
17 , HandleConstraint
17 , HandleConstraint
18 , Handle(..)
18 , Handle(..)
19− , makeTupleInstancesHasLens
22import Control.Applicative
21import Control.Applicative
23import Data.Coerce
22import Data.Coerce
24import Data.Functor.Identity
23import Data.Functor.Identity
25import Data.Kind
24import Data.Kind
25+import Data.List as List
26import Data.Tagged
26import Data.Tagged
27+import Data.Traversable
27import GHC.Exts (Constraint)
28import GHC.Exts (Constraint)
28−
29+import qualified Language.Haskell.TH as TH
29−import Ether.Internal.HasLens
30−import Ether.Internal.Tags
31−import Ether.Internal.TH_TupleInstances (makeTupleInstancesHasLens)
32−import Ether.Internal.TupleInstances ()
34data TAGGED e t
31data TAGGED e t
@@ -57,12 +54,120 @@ class
58 handling :: Monad m => (HandleConstraint eff p trans m => r) -> r
55 handling :: Monad m => (HandleConstraint eff p trans m => r) -> r
57+type LensLike f s t a b = (a -> f b) -> s -> f t
59+type Lens s t a b = forall f. Functor f => LensLike f s t a b
61+type Lens' s a = Lens s s a a
60newtype ReifiedLens s t a b = Lens (Lens s t a b)
63newtype ReifiedLens s t a b = Lens (Lens s t a b)
62type ReifiedLens' s a = ReifiedLens s s a a
65type ReifiedLens' s a = ReifiedLens s s a a
67+class HasLens tag outer inner | tag outer -> inner where
68+ lensOf :: Lens' outer inner
70+instance HasLens a a a where
73+instance HasLens t (Tagged t a) a where
74+ lensOf = \f -> fmap coerce . f . coerce
64view :: LensLike (Const a) s t a b -> s -> a
76view :: LensLike (Const a) s t a b -> s -> a
65view l = coerce (l Const)
77view l = coerce (l Const)
67over :: LensLike Identity s t a b -> (a -> b) -> s -> t
79over :: LensLike Identity s t a b -> (a -> b) -> s -> t
68over = coerce
80over = coerce
84+ HCons :: x -> HList xs -> HList (x ': xs)
86+type KindOf (a :: k) = k
88+type family TagsK (p :: Type) :: [Type]
89+type family Tags (p :: Type) :: HList (TagsK p)
93+type instance TagsK () = '[]
94+type instance TagsK (Tagged t a) = '[KindOf t]
99+ varName k = TH.mkName ('a' : show k)
100+ tagName k = TH.mkName ('t' : show k)
101+ for [2..tupCount] $ \n -> do
103+ tupTy = List.foldl' TH.AppT (TH.ConT (TH.tupleTypeName n)) $
104+ (\k -> TH.ConT ''Tagged `TH.AppT`
105+ TH.VarT (tagName k) `TH.AppT`
106+ TH.VarT (varName k)) <$> [0..n-1]
107+ tagsList = List.foldr
108+ (\a b -> TH.PromotedConsT `TH.AppT` a `TH.AppT` b)
110+ (TH.AppT (TH.ConT ''KindOf) . TH.VarT . tagName <$> [0..n-1])
112+ TH.TySynInstD ''TagsK (TH.TySynEqn [tupTy] tagsList)
116+type instance Tags () = 'HNil
117+type instance Tags (Tagged t a) = 'HCons t 'HNil
122+ varName k = TH.mkName ('a' : show k)
123+ tagName k = TH.mkName ('t' : show k)
124+ for [2..tupCount] $ \n -> do
126+ tupTy = List.foldl' TH.AppT (TH.ConT (TH.tupleTypeName n)) $
127+ (\k -> TH.ConT ''Tagged `TH.AppT`
128+ TH.VarT (tagName k) `TH.AppT`
129+ TH.VarT (varName k)) <$> [0..n-1]
130+ tagsList = List.foldr
131+ (\a b -> TH.PromotedT 'HCons `TH.AppT` a `TH.AppT` b)
132+ (TH.PromotedT 'HNil)
133+ (TH.VarT . tagName <$> [0..n-1])
135+ TH.TySynInstD ''Tags (TH.TySynEqn [tupTy] tagsList)
140+ varName k = TH.mkName ('a' : show k)
142+ for [2..tupCount] $ \n ->
143+ for [0..n-1] $ \k -> do
145+ tag = TH.mkName "tag"
146+ prev = varName <$> [0..k-1]
148+ next = varName <$> [k+1..n-1]
149+ tupTy = foldl TH.AppT (TH.ConT (TH.tupleTypeName n))
150+ ( map TH.VarT prev ++
151+ [TH.ConT ''Tagged `TH.AppT` TH.VarT tag `TH.AppT` TH.VarT cur] ++
154+ cur' = TH.mkName "x"
157+ TH.InstanceD Nothing []
158+ (TH.ConT ''HasLens `TH.AppT` TH.VarT tag `TH.AppT` tupTy `TH.AppT` TH.VarT cur)
162+ ( map TH.VarP prev ++
163+ [TH.ConP 'Tagged [TH.VarP cur]] ++
166+ TH.VarE 'fmap `TH.AppE`
167+ (TH.LamE [TH.VarP cur']
169+ ( map TH.VarE prev ++
170+ [TH.ConE 'Tagged `TH.AppE` TH.VarE cur'] ++
171+ map TH.VarE next ))) `TH.AppE`
172+ (TH.VarE f `TH.AppE` TH.VarE cur) )