{-# LANGUAGE RankNTypes #-}
module Fresnel.Effect
( -- * Access
  -- ** Reader
  view
, views
  -- ** State
, use
, uses
  -- * Assignment
, assign
, modifying
, (.=)
, (%=)
, (?=)
, (<~)
  -- ** Arithmetical
, (+=)
, (-=)
, (*=)
, (//=)
) where

import           Control.Algebra
import qualified Control.Effect.Reader as R
import qualified Control.Effect.State as S
import qualified Fresnel.Getter as O
import qualified Fresnel.Setter as O

view :: Has (R.Reader r) sig m => O.Getter r a -> m a
view :: forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
Getter r a -> m a
view Getter r a
o = (r -> a) -> m a
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
(r -> a) -> m a
R.asks (Getter r a -> r -> a
forall s a. Getter s a -> s -> a
O.view Optic' p r a
Getter r a
o)

views :: Has (R.Reader r) sig m => O.Getter r a -> (a -> b) -> m b
views :: forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (Reader r) sig m =>
Getter r a -> (a -> b) -> m b
views Getter r a
o a -> b
f = (r -> b) -> m b
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
(r -> a) -> m a
R.asks (Getter r a -> (a -> b) -> r -> b
forall s a r. Getter s a -> (a -> r) -> s -> r
O.views Optic' p r a
Getter r a
o a -> b
f)


use :: Has (S.State s) sig m => O.Getter s a -> m a
use :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
Getter s a -> m a
use Getter s a
o = (s -> a) -> m a
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> a) -> m a
S.gets (Getter s a -> s -> a
forall s a. Getter s a -> s -> a
O.view Optic' p s a
Getter s a
o)

uses :: Has (S.State s) sig m => O.Getter s a -> (a -> b) -> m b
uses :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Getter s a -> (a -> b) -> m b
uses Getter s a
o a -> b
f = (s -> b) -> m b
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> a) -> m a
S.gets (Getter s a -> (a -> b) -> s -> b
forall s a r. Getter s a -> (a -> r) -> s -> r
O.views Optic' p s a
Getter s a
o a -> b
f)


assign, (.=) :: Has (S.State s) sig m => O.Setter s s a b -> b -> m ()

assign :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> b -> m ()
assign Setter s s a b
o b
v = (s -> s) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
(s -> s) -> m ()
S.modify (Setter s s a b -> b -> s -> s
forall s t a b. Setter s t a b -> b -> s -> t
O.set Optic p s s a b
Setter s s a b
o b
v)

.= :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> b -> m ()
(.=) = Setter s s a b -> b -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> b -> m ()
assign

infix 4 .=


modifying, (%=) :: Has (S.State s) sig m => O.Setter s s a b -> (a -> b) -> m ()

modifying :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> (a -> b) -> m ()
modifying Setter s s a b
o a -> b
f = (s -> s) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
(s -> s) -> m ()
S.modify (Setter s s a b -> (a -> b) -> s -> s
forall s t a b. Setter s t a b -> (a -> b) -> s -> t
O.over Optic p s s a b
Setter s s a b
o a -> b
f)

%= :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> (a -> b) -> m ()
(%=) = Setter s s a b -> (a -> b) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> (a -> b) -> m ()
modifying

infix 4 %=



(?=) :: Has (S.State s) sig m => O.Setter s s a (Maybe b) -> b -> m ()
Setter s s a (Maybe b)
o ?= :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a (Maybe b) -> b -> m ()
?= b
v = Optic p s s a (Maybe b)
Setter s s a (Maybe b)
o Setter s s a (Maybe b) -> Maybe b -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> b -> m ()
.= b -> Maybe b
forall a. a -> Maybe a
Just b
v


infix 4 ?=


(<~) :: Has (S.State s) sig m => O.Setter s s a b -> m b -> m ()
Setter s s a b
o <~ :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> m b -> m ()
<~ m b
m = m b
m m b -> (b -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Setter s s a b -> b -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> b -> m ()
assign Optic p s s a b
Setter s s a b
o

infixr 2 <~


(+=), (-=), (*=) :: (Has (S.State s) sig m, Num a) => O.Setter s s a a -> a -> m ()
Setter s s a a
o += :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Has (State s) sig m, Num a) =>
Setter s s a a -> a -> m ()
+= a
n = Setter s s a a -> (a -> a) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> (a -> b) -> m ()
modifying Optic p s s a a
Setter s s a a
o (a -> a -> a
forall a. Num a => a -> a -> a
+ a
n)
Setter s s a a
o -= :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Has (State s) sig m, Num a) =>
Setter s s a a -> a -> m ()
-= a
n = Setter s s a a -> (a -> a) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> (a -> b) -> m ()
modifying Optic p s s a a
Setter s s a a
o (a -> a -> a
forall a. Num a => a -> a -> a
subtract a
n)
Setter s s a a
o *= :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Has (State s) sig m, Num a) =>
Setter s s a a -> a -> m ()
*= a
n = Setter s s a a -> (a -> a) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> (a -> b) -> m ()
modifying Optic p s s a a
Setter s s a a
o (a -> a -> a
forall a. Num a => a -> a -> a
* a
n)

(//=) :: (Has (S.State s) sig m, Fractional a) => O.Setter s s a a -> a -> m ()
Setter s s a a
o //= :: forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Has (State s) sig m, Fractional a) =>
Setter s s a a -> a -> m ()
//= a
n = Setter s s a a -> (a -> a) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a b.
Has (State s) sig m =>
Setter s s a b -> (a -> b) -> m ()
modifying Optic p s s a a
Setter s s a a
o (a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
n)

infix 4 +=, -=, *=, //=