Lens: zooming newtype

痴心易碎 提交于 2019-12-05 18:42:47

I'd suggest to make state of Awesome explicit:

{-# LANGUAGE TemplateHaskell, GeneralizedNewtypeDeriving, TypeFamilies,
    FlexibleInstances, FunctionalDependencies #-}
import Control.Applicative
import Control.Monad.Error
import Control.Monad.State
import Control.Lens
import Control.Lens.Zoom
import Control.Lens.Internal.Zoom

data AwesomeState = AwesomeState
    { _someRecord :: String
    -- ...
    }

$(makeLenses ''AwesomeState)

newtype Awesome s a = Awesome (StateT s (ErrorT String IO) a)
  deriving ( Functor, Applicative, Monad
           , MonadIO, MonadError String
           , MonadState s)

Then you can define Zoom instance for it as follows:

type instance Zoomed (Awesome s) = Focusing (ErrorT String IO)

instance Zoom (Awesome s) (Awesome t) s t where
    zoom l (Awesome m) = Awesome (zoom l m)

Then you'll have

zoom someRecord :: Awesome String a -> Awesome AwesomeState a
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!