module Optics.View where
import Control.Monad.Reader.Class
import Control.Monad.State
import Control.Monad.Writer
import Data.Kind
import Optics.Core
class ViewableOptic k r where
type ViewResult k r :: Type
gview
:: MonadReader s m
=> Optic' k is s r
-> m (ViewResult k r)
gviews
:: MonadReader s m
=> Optic' k is s a
-> (a -> r)
-> m (ViewResult k r)
instance ViewableOptic An_Iso r where
type ViewResult An_Iso r = r
gview = asks . view
gviews o = asks . views o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance ViewableOptic A_Lens r where
type ViewResult A_Lens r = r
gview = asks . view
gviews o = asks . views o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance ViewableOptic A_ReversedPrism r where
type ViewResult A_ReversedPrism r = r
gview = asks . view
gviews o = asks . views o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance ViewableOptic A_Getter r where
type ViewResult A_Getter r = r
gview = asks . view
gviews o = asks . views o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance ViewableOptic A_Prism r where
type ViewResult A_Prism r = Maybe r
gview = asks . preview
gviews o = asks . previews o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance ViewableOptic An_AffineTraversal r where
type ViewResult An_AffineTraversal r = Maybe r
gview = asks . preview
gviews o = asks . previews o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance ViewableOptic An_AffineFold r where
type ViewResult An_AffineFold r = Maybe r
gview = asks . preview
gviews o = asks . previews o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance Monoid r => ViewableOptic A_Traversal r where
type ViewResult A_Traversal r = r
gview = asks . foldOf
gviews o = asks . foldMapOf o
{-# INLINE gview #-}
{-# INLINE gviews #-}
instance Monoid r => ViewableOptic A_Fold r where
type ViewResult A_Fold r = r
gview = asks . foldOf
gviews o = asks . foldMapOf o
{-# INLINE gview #-}
{-# INLINE gviews #-}
guse
:: (ViewableOptic k a, MonadState s m)
=> Optic' k is s a
-> m (ViewResult k a)
guse o = gets (gview o)
{-# INLINE guse #-}
guses
:: (ViewableOptic k r, MonadState s m)
=> Optic' k is s a
-> (a -> r)
-> m (ViewResult k r)
guses o f = gets (gviews o f)
{-# INLINE guses #-}
glistening
:: (ViewableOptic k r, MonadWriter s m)
=> Optic' k is s r
-> m a
-> m (a, ViewResult k r)
glistening o m = do
(a, w) <- listen m
return (a, gview o w)
{-# INLINE glistening #-}
glistenings
:: (ViewableOptic k r, MonadWriter s m)
=> Optic' k is s a
-> (a -> r)
-> m b
-> m (b, ViewResult k r)
glistenings o f m = do
(a, w) <- listen m
return (a, gviews o f w)
{-# INLINE glistenings #-}