Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lenses and TypeFamilies

I've encountered a problem of using Control.Lens together with
datatypes while using the -XTypeFamilies GHC pragma.

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies    #-}

import Control.Lens (makeLenses)

class SomeClass t where
  data SomeData t :: * -> *

data MyData = MyData Int

instance SomeClass MyData where
  data SomeData MyData a = SomeData {_a :: a, _b :: a}

makeLenses ''SomeData

The error message is: reifyDatatype: Use a value constructor to reify a data family instance.

Is there any way to overcome it, maybe using some functional from Control.Lens?

like image 492
errfrom Avatar asked Sep 14 '25 23:09

errfrom


1 Answers

The most sensible thing would be to just define those lenses yourself... it's not like it's very difficult:

a, b :: Lens' (SomeData MyData a) a
a = lens _a (\s a' -> s{_a=a'})
b = lens _b (\s b' -> s{_b=b'})

or even

a, b :: Functor f => (a -> f a) -> SomeData MyData a -> f (SomeData MyData a)
a f (SomeData a₀ b₀) = (`SomeData`b₀) <$> f a₀
b f (SomeData a₀ b₀) =   SomeData a₀  <$> f b₀

...which doesn't use anything from the lens library at all, but is fully compatible to all lens combinators.

like image 89
leftaroundabout Avatar answered Sep 17 '25 00:09

leftaroundabout