module Optics.View where
import Control.Monad.Reader.Class
import Control.Monad.State
import Control.Monad.Writer
import Optics.Core
class ViewableOptic k r where
type ViewResult k r :: *
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 #-}
use
:: (ViewableOptic k a, MonadState s m)
=> Optic' k is s a
-> m (ViewResult k a)
use o = gets (gview o)
{-# INLINE use #-}
uses
:: (ViewableOptic k r, MonadState s m)
=> Optic' k is s a
-> (a -> r)
-> m (ViewResult k r)
uses o f = gets (gviews o f)
{-# INLINE uses #-}
listening
:: (ViewableOptic k r, MonadWriter s m)
=> Optic' k is s r
-> m a
-> m (a, ViewResult k r)
listening o m = do
(a, w) <- listen m
return (a, gview o w)
{-# INLINE listening #-}
listenings
:: (ViewableOptic k r, MonadWriter s m)
=> Optic' k is s a
-> (a -> r)
-> m b
-> m (b, ViewResult k r)
listenings o f m = do
(a, w) <- listen m
return (a, gviews o f w)
{-# INLINE listenings #-}