module Plutarch.Extra.Optics (
HasLabelled,
HasLabelledGetters,
inspect,
inspects,
guarantee,
guarantees,
) where
import Control.Monad.Reader (MonadReader, asks)
import Data.Kind (Constraint)
import Data.Maybe (fromMaybe)
import GHC.TypeLits (Symbol)
import Optics.AffineFold (An_AffineFold, preview, previews)
import Optics.Getter (A_Getter, view, views)
import Optics.Label (LabelOptic)
import Optics.Optic (Is, Optic')
type family
HasLabelled
(opt :: Type)
(k :: Type)
(s :: Type)
(labels :: [(Symbol, Type)]) ::
Constraint
where
HasLabelled opt k s '[] = (k `Is` opt)
HasLabelled opt k s ('(sym, t) ': labels) =
(LabelOptic sym k s s t t, HasLabelled opt k s labels)
{-# DEPRECATED HasLabelledGetters "Use HasLabelled A_Getter instead." #-}
type HasLabelledGetters (k :: Type) (s :: Type) (labels :: [(Symbol, Type)]) =
HasLabelled A_Getter k s labels
{-# INLINEABLE inspect #-}
inspect ::
forall
(m :: Type -> Type)
(r :: Type)
(k :: Type)
(is :: [Type])
(a :: Type).
(MonadReader r m, Is k A_Getter) =>
Optic' k is r a ->
m a
inspect :: forall (m :: Type -> Type) r k (is :: [Type]) a.
(MonadReader r m, Is k A_Getter) =>
Optic' k is r a -> m a
inspect Optic' k is r a
opt = forall r (m :: Type -> Type) a. MonadReader r m => (r -> a) -> m a
asks (forall k (is :: [Type]) s a.
Is k A_Getter =>
Optic' k is s a -> s -> a
view Optic' k is r a
opt)
{-# INLINEABLE inspects #-}
inspects ::
forall
(m :: Type -> Type)
(r :: Type)
(k :: Type)
(is :: [Type])
(a :: Type)
(b :: Type).
(MonadReader r m, Is k A_Getter) =>
Optic' k is r a ->
(a -> b) ->
m b
inspects :: forall (m :: Type -> Type) r k (is :: [Type]) a b.
(MonadReader r m, Is k A_Getter) =>
Optic' k is r a -> (a -> b) -> m b
inspects Optic' k is r a
opt a -> b
f = forall r (m :: Type -> Type) a. MonadReader r m => (r -> a) -> m a
asks (forall k (is :: [Type]) s a r.
Is k A_Getter =>
Optic' k is s a -> (a -> r) -> s -> r
views Optic' k is r a
opt a -> b
f)
{-# INLINEABLE guarantee #-}
guarantee ::
forall (k :: Type) (is :: [Type]) (s :: Type) (a :: Type).
(Is k An_AffineFold) =>
a ->
Optic' k is s a ->
s ->
a
guarantee :: forall k (is :: [Type]) s a.
Is k An_AffineFold =>
a -> Optic' k is s a -> s -> a
guarantee a
x Optic' k is s a
opt = forall a. a -> Maybe a -> a
fromMaybe a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (is :: [Type]) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview Optic' k is s a
opt
{-# INLINEABLE guarantees #-}
guarantees ::
forall (k :: Type) (is :: [Type]) (s :: Type) (a :: Type) (b :: Type).
(Is k An_AffineFold) =>
b ->
Optic' k is s a ->
(a -> b) ->
s ->
b
guarantees :: forall k (is :: [Type]) s a b.
Is k An_AffineFold =>
b -> Optic' k is s a -> (a -> b) -> s -> b
guarantees b
x Optic' k is s a
opt a -> b
f = forall a. a -> Maybe a -> a
fromMaybe b
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (is :: [Type]) s a r.
Is k An_AffineFold =>
Optic' k is s a -> (a -> r) -> s -> Maybe r
previews Optic' k is s a
opt a -> b
f