{-# LANGUAGE FlexibleContexts #-}

module Data.Optics.Operators where

import Control.Monad.State (MonadState, modify)
import Optics.Core (A_Setter, Is, Optic', (%~))

{- |
Modify the target of the optic by adding a value.

@
data Person = Person { age :: 'Int' } deriving ('GHC.Generics.Generic')

f :: 'MonadState' Person m => m ()
f = #age += 1
@
-}
(+=) :: (Is k A_Setter, MonadState s m, Num a) => Optic' k is s a -> a -> m ()
Optic' k is s a
o += :: forall (k :: OpticKind) (s :: OpticKind)
       (m :: OpticKind -> OpticKind) (a :: OpticKind) (is :: IxList).
(Is k A_Setter, MonadState s m, Num a) =>
Optic' k is s a -> a -> m ()
+= a
a = forall (s :: OpticKind) (m :: OpticKind -> OpticKind).
MonadState s m =>
(s -> s) -> m ()
modify forall (a :: OpticKind) b. (a -> b) -> a -> b
$ Optic' k is s a
o forall (k :: OpticKind) (is :: IxList) (s :: OpticKind)
       (t :: OpticKind) (a :: OpticKind) (b :: OpticKind).
Is k A_Setter =>
Optic k is s t a b -> (a -> b) -> s -> t
%~ (forall (a :: OpticKind). Num a => a -> a -> a
+ a
a)

infixr 4 +=
{-# INLINE (+=) #-}

{- |
Modify the target of the optic by subtracting a value.

@
data Person = Person { age :: 'Int' } deriving ('GHC.Generics.Generic')

f :: 'MonadState' Person m => m ()
f = #age -= 1
@
-}
(-=) :: (Is k A_Setter, MonadState s m, Num a) => Optic' k is s a -> a -> m ()
Optic' k is s a
o -= :: forall (k :: OpticKind) (s :: OpticKind)
       (m :: OpticKind -> OpticKind) (a :: OpticKind) (is :: IxList).
(Is k A_Setter, MonadState s m, Num a) =>
Optic' k is s a -> a -> m ()
-= a
a = forall (s :: OpticKind) (m :: OpticKind -> OpticKind).
MonadState s m =>
(s -> s) -> m ()
modify forall (a :: OpticKind) b. (a -> b) -> a -> b
$ Optic' k is s a
o forall (k :: OpticKind) (is :: IxList) (s :: OpticKind)
       (t :: OpticKind) (a :: OpticKind) (b :: OpticKind).
Is k A_Setter =>
Optic k is s t a b -> (a -> b) -> s -> t
%~ forall (a :: OpticKind). Num a => a -> a -> a
subtract a
a

infixr 4 -=
{-# INLINE (-=) #-}

{- |
Modify the target of the optic by multiplying a value.

@
data Person = Person { age :: 'Int' } deriving ('GHC.Generics.Generic')

f :: 'MonadState' Person m => m ()
f = #age *= 1
@
-}
(*=) :: (Is k A_Setter, MonadState s m, Num a) => Optic' k is s a -> a -> m ()
Optic' k is s a
o *= :: forall (k :: OpticKind) (s :: OpticKind)
       (m :: OpticKind -> OpticKind) (a :: OpticKind) (is :: IxList).
(Is k A_Setter, MonadState s m, Num a) =>
Optic' k is s a -> a -> m ()
*= a
a = forall (s :: OpticKind) (m :: OpticKind -> OpticKind).
MonadState s m =>
(s -> s) -> m ()
modify forall (a :: OpticKind) b. (a -> b) -> a -> b
$ Optic' k is s a
o forall (k :: OpticKind) (is :: IxList) (s :: OpticKind)
       (t :: OpticKind) (a :: OpticKind) (b :: OpticKind).
Is k A_Setter =>
Optic k is s t a b -> (a -> b) -> s -> t
%~ (forall (a :: OpticKind). Num a => a -> a -> a
* a
a)

infixr 4 *=
{-# INLINE (*=) #-}

{- |
Modify the target of the optic by dividing a value.

@
data Person = Person { age :: 'Int' } deriving ('GHC.Generics.Generic')

f :: 'MonadState' Person m => m ()
f = #age //= 1
@
-}
(//=) :: (Is k A_Setter, MonadState s m, Fractional a) => Optic' k is s a -> a -> m ()
Optic' k is s a
o //= :: forall (k :: OpticKind) (s :: OpticKind)
       (m :: OpticKind -> OpticKind) (a :: OpticKind) (is :: IxList).
(Is k A_Setter, MonadState s m, Fractional a) =>
Optic' k is s a -> a -> m ()
//= a
a = forall (s :: OpticKind) (m :: OpticKind -> OpticKind).
MonadState s m =>
(s -> s) -> m ()
modify forall (a :: OpticKind) b. (a -> b) -> a -> b
$ Optic' k is s a
o forall (k :: OpticKind) (is :: IxList) (s :: OpticKind)
       (t :: OpticKind) (a :: OpticKind) (b :: OpticKind).
Is k A_Setter =>
Optic k is s t a b -> (a -> b) -> s -> t
%~ (forall (a :: OpticKind). Fractional a => a -> a -> a
/ a
a)
{-# INLINE (//=) #-}

infixr 4 //=