{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
module Data.ReactiveValue
(
ReactiveValueRead(..)
, ReactiveValueWrite(..)
, ReactiveValueReadWrite
, (=:>)
, (=:=)
, (<:=)
, ReactiveFieldRead(..)
, ReactiveFieldWrite(..)
, ReactiveFieldReadWrite(..)
, FieldGetter
, FieldSetter
, FieldNotifier
, constR
, initRW
, liftR
, (<^>)
, liftR2
, liftR3
, liftMR
, readOnly
, wrapMR
, wrapMRPassive
, eventR
, lMerge
, rMerge
, constW
, liftW
, liftW2
, (&.&)
, liftMW
, writeOnly
, wrapMW
, wrapDo
, wrapDo_
, liftRW
, liftRW2
, pairRW
, modRW
, BijectiveFunc
, bijection
, direct
, inverse
, Involution
, involution
, reactiveValueModify
, eqCheck
, passivelyR
, passivelyRW
, governingR
, governingRW
, ifRW
, ifRW_
, guardRO
, guardRO'
, ReactiveValueActivatable(..)
, ReactiveFieldActivatable
, mkActivatable
)
where
import Control.Monad (liftM, void, when)
import Data.Functor.Contravariant (Contravariant (contramap))
import Control.GFunctor (GFunctor (gmap))
class Monad m => ReactiveValueRead a b m | a -> b, a -> m where
reactiveValueOnCanRead :: a -> m () -> m ()
reactiveValueOnCanRead a
_ m ()
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
reactiveValueRead :: a -> m b
{-# MINIMAL reactiveValueRead #-}
instance ReactiveValueRead (IO a) a IO where
reactiveValueRead :: IO a -> IO a
reactiveValueRead = IO a -> IO a
forall a. a -> a
id
instance (Functor m, Monad m) => ReactiveValueRead (m a, a -> m b) a m where
reactiveValueRead :: (m a, a -> m b) -> m a
reactiveValueRead = (m a, a -> m b) -> m a
forall a b. (a, b) -> a
fst
class ReactiveValueWrite a b m | a -> b, a -> m where
reactiveValueWrite :: a -> b -> m ()
instance ReactiveValueWrite (IO a) () IO where
reactiveValueWrite :: IO a -> () -> IO ()
reactiveValueWrite IO a
m ()
_ = IO a -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void IO a
m
instance (Functor m, Monad m) => ReactiveValueWrite (a -> m b) a m where
reactiveValueWrite :: (a -> m b) -> a -> m ()
reactiveValueWrite a -> m b
f a
v = m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (a -> m b
f a
v)
instance ReactiveValueWrite (a -> m b) a m
=> ReactiveValueWrite (m a, a -> m b) a m
where
reactiveValueWrite :: (m a, a -> m b) -> a -> m ()
reactiveValueWrite (m a
_, a -> m b
f) = (a -> m b) -> a -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a -> m b
f
class (ReactiveValueRead a b m, ReactiveValueWrite a b m)
=> ReactiveValueReadWrite a b m
instance (Functor m, Monad m) => ReactiveValueReadWrite (m a, a -> m b) a m
class ReactiveValueActivatable m a where
defaultActivation :: a -> ReactiveFieldActivatable m
infix 9 =:=
infix 9 =:>
infix 9 <:=
(=:>) :: Monad m
=> (ReactiveValueRead a b m, ReactiveValueWrite c b m)
=> a
-> c
-> m ()
=:> :: a -> c -> m ()
(=:>) a
v1 c
v2 = a -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
v1 m ()
sync1
where
sync1 :: m ()
sync1 = a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
v1 m b -> (b -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= c -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite c
v2
(<:=) :: Monad m
=> (ReactiveValueRead a b m, ReactiveValueWrite c b m)
=> c
-> a
-> m ()
<:= :: c -> a -> m ()
(<:=) c
v2 a
v1 = a -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
v1 m ()
sync1
where
sync1 :: m ()
sync1 = a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
v1 m b -> (b -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= c -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite c
v2
(=:=) :: Monad m
=> (ReactiveValueReadWrite a b m, ReactiveValueReadWrite c b m)
=> a
-> c
-> m ()
=:= :: a -> c -> m ()
(=:=) a
v1 c
v2 = do
a
v1 a -> c -> m ()
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueRead a b m, ReactiveValueWrite c b m) =>
a -> c -> m ()
=:> c
v2
a
v1 a -> c -> m ()
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueRead a b m, ReactiveValueWrite c b m) =>
c -> a -> m ()
<:= c
v2
data ReactiveFieldRead m a =
ReactiveFieldRead (FieldGetter m a) (FieldNotifier m a)
newtype ReactiveFieldWrite m a =
ReactiveFieldWrite (FieldSetter m a)
data ReactiveFieldReadWrite m a =
ReactiveFieldReadWrite (FieldSetter m a) (FieldGetter m a) (FieldNotifier m a)
type ReactiveFieldActivatable m = ReactiveFieldRead m ()
instance Monad m => ReactiveValueRead (ReactiveFieldRead m a) a m where
reactiveValueOnCanRead :: ReactiveFieldRead m a -> m () -> m ()
reactiveValueOnCanRead (ReactiveFieldRead m a
_ m () -> m ()
notifier) = m () -> m ()
notifier
reactiveValueRead :: ReactiveFieldRead m a -> m a
reactiveValueRead (ReactiveFieldRead m a
getter m () -> m ()
_) = m a
getter
instance Monad m => ReactiveValueWrite (ReactiveFieldWrite m a) a m where
reactiveValueWrite :: ReactiveFieldWrite m a -> a -> m ()
reactiveValueWrite (ReactiveFieldWrite a -> m ()
setter) = a -> m ()
setter
instance Monad m => ReactiveValueRead (ReactiveFieldReadWrite m a) a m where
reactiveValueOnCanRead :: ReactiveFieldReadWrite m a -> m () -> m ()
reactiveValueOnCanRead (ReactiveFieldReadWrite FieldSetter m a
_ m a
_ m () -> m ()
notifier) = m () -> m ()
notifier
reactiveValueRead :: ReactiveFieldReadWrite m a -> m a
reactiveValueRead (ReactiveFieldReadWrite FieldSetter m a
_ m a
getter m () -> m ()
_) = m a
getter
instance Monad m => ReactiveValueWrite (ReactiveFieldReadWrite m a) a m where
reactiveValueWrite :: ReactiveFieldReadWrite m a -> a -> m ()
reactiveValueWrite (ReactiveFieldReadWrite a -> m ()
setter FieldGetter m a
_ FieldNotifier m a
_) = a -> m ()
setter
instance Monad m => ReactiveValueReadWrite (ReactiveFieldReadWrite m a) a m
type FieldGetter m a = m a
type FieldSetter m a = a -> m ()
type FieldNotifier m a = m () -> m ()
mkActivatable :: Monad m => (m () -> m ()) -> ReactiveFieldActivatable m
mkActivatable :: (m () -> m ()) -> ReactiveFieldActivatable m
mkActivatable m () -> m ()
f = m () -> (m () -> m ()) -> ReactiveFieldActivatable m
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead m ()
getter m () -> m ()
notifier
where
getter :: m ()
getter = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
notifier :: m () -> m ()
notifier = m () -> m ()
f
constR :: Monad m => a -> ReactiveFieldRead m a
constR :: a -> ReactiveFieldRead m a
constR a
e = FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m a
getter FieldNotifier m a
forall (m :: * -> *) p. Monad m => p -> m ()
notifier
where
notifier :: p -> m ()
notifier p
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
getter :: FieldGetter m a
getter = a -> FieldGetter m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
e
{-# DEPRECATED initRW "Deprecated in keera-hails-reactivevalues 0.8.0, use constR instead" #-}
initRW :: Monad m => a -> ReactiveFieldRead m a
initRW :: a -> ReactiveFieldRead m a
initRW a
e = FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m a
getter FieldNotifier m a
forall (m :: * -> *) p. Monad m => p -> m ()
notifier
where
notifier :: p -> m ()
notifier p
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
getter :: FieldGetter m a
getter = a -> FieldGetter m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
e
{-# ANN liftR "HLint: ignore Use fmap" #-}
liftR :: (Monad m, ReactiveValueRead a b m)
=> (b -> c)
-> a
-> ReactiveFieldRead m c
liftR :: (b -> c) -> a -> ReactiveFieldRead m c
liftR b -> c
f a
e = FieldGetter m c -> FieldNotifier m c -> ReactiveFieldRead m c
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m c
getter FieldNotifier m c
notifier
where
notifier :: FieldNotifier m c
notifier = a -> FieldNotifier m c
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
e
getter :: FieldGetter m c
getter = (b -> c) -> m b -> FieldGetter m c
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM b -> c
f (a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
e)
(<^>) :: (Monad m, ReactiveValueRead a b m)
=> (b -> c)
-> a
-> ReactiveFieldRead m c
<^> :: (b -> c) -> a -> ReactiveFieldRead m c
(<^>) = (b -> c) -> a -> ReactiveFieldRead m c
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueRead a b m) =>
(b -> c) -> a -> ReactiveFieldRead m c
liftR
liftR2 :: (Monad m, ReactiveValueRead a b m, ReactiveValueRead c d m)
=> (b -> d -> e) -> a -> c -> ReactiveFieldRead m e
liftR2 :: (b -> d -> e) -> a -> c -> ReactiveFieldRead m e
liftR2 b -> d -> e
f a
e1 c
e2 = FieldGetter m e -> FieldNotifier m e -> ReactiveFieldRead m e
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m e
getter FieldNotifier m e
forall (m :: * -> *) b b.
(ReactiveValueRead a b m, ReactiveValueRead c b m) =>
m () -> m ()
notifier
where
getter :: FieldGetter m e
getter = do b
v1 <- a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
e1
d
v2 <- c -> m d
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
e2
e -> FieldGetter m e
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> d -> e
f b
v1 d
v2)
notifier :: m () -> m ()
notifier m ()
p = do a -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
e1 m ()
p
c -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead c
e2 m ()
p
liftR3 :: ( Monad m, ReactiveValueRead a b m, ReactiveValueRead c d m
, ReactiveValueRead e f m)
=> (b -> d -> f -> g) -> a -> c -> e -> ReactiveFieldRead m g
liftR3 :: (b -> d -> f -> g) -> a -> c -> e -> ReactiveFieldRead m g
liftR3 b -> d -> f -> g
f a
e1 c
e2 e
e3 = FieldGetter m g -> FieldNotifier m g -> ReactiveFieldRead m g
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m g
getter FieldNotifier m g
forall (m :: * -> *) b b b.
(ReactiveValueRead a b m, ReactiveValueRead c b m,
ReactiveValueRead e b m) =>
m () -> m ()
notifier
where
getter :: FieldGetter m g
getter = do b
v1 <- a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
e1
d
v2 <- c -> m d
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
e2
f
v3 <- e -> m f
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead e
e3
g -> FieldGetter m g
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> d -> f -> g
f b
v1 d
v2 f
v3)
notifier :: m () -> m ()
notifier m ()
p = do a -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
e1 m ()
p
c -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead c
e2 m ()
p
e -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead e
e3 m ()
p
liftMR :: (Monad m, ReactiveValueRead a b m)
=> (b -> m c)
-> a
-> ReactiveFieldRead m c
liftMR :: (b -> m c) -> a -> ReactiveFieldRead m c
liftMR b -> m c
f a
e = m c -> FieldNotifier m c -> ReactiveFieldRead m c
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead m c
getter FieldNotifier m c
notifier
where
notifier :: FieldNotifier m c
notifier = a -> FieldNotifier m c
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
e
getter :: m c
getter = b -> m c
f (b -> m c) -> m b -> m c
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
e
{-# ANN wrapMR "HLint: ignore Eta reduce" #-}
wrapMR :: m a -> (m () -> m ()) -> ReactiveFieldRead m a
wrapMR :: m a -> (m () -> m ()) -> ReactiveFieldRead m a
wrapMR m a
f m () -> m ()
p = m a -> (m () -> m ()) -> ReactiveFieldRead m a
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead m a
f m () -> m ()
p
wrapMRPassive :: Monad m => m a -> ReactiveFieldRead m a
wrapMRPassive :: m a -> ReactiveFieldRead m a
wrapMRPassive m a
f = m a -> FieldNotifier m a -> ReactiveFieldRead m a
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead m a
f (m () -> FieldNotifier m a
forall a b. a -> b -> a
const (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()))
{-# ANN eventR "HLint: ignore Eta reduce" #-}
eventR :: Monad m => (m () -> m ()) -> ReactiveFieldRead m ()
eventR :: (m () -> m ()) -> ReactiveFieldRead m ()
eventR m () -> m ()
notifInstaller = m () -> (m () -> m ()) -> ReactiveFieldRead m ()
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) m () -> m ()
notifInstaller
readOnly :: ReactiveValueRead r a m => r -> ReactiveFieldRead m a
readOnly :: r -> ReactiveFieldRead m a
readOnly r
r = FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead (r -> FieldGetter m a
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead r
r) (r -> FieldNotifier m a
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead r
r)
constW :: (Monad m, ReactiveValueWrite v a m)
=> a
-> v
-> ReactiveFieldWrite m b
constW :: a -> v -> ReactiveFieldWrite m b
constW a
c v
v = FieldSetter m b -> ReactiveFieldWrite m b
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite (FieldSetter m b -> ReactiveFieldWrite m b)
-> FieldSetter m b -> ReactiveFieldWrite m b
forall a b. (a -> b) -> a -> b
$ \b
_ -> v -> a -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite v
v a
c
liftW :: (Monad m, ReactiveValueWrite a b m)
=> (c -> b) -> a -> ReactiveFieldWrite m c
liftW :: (c -> b) -> a -> ReactiveFieldWrite m c
liftW c -> b
f a
e = FieldSetter m c -> ReactiveFieldWrite m c
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite FieldSetter m c
setter
where
setter :: FieldSetter m c
setter = a -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a
e (b -> m ()) -> (c -> b) -> FieldSetter m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> b
f
liftW2 :: (Monad m, ReactiveValueWrite a b m, ReactiveValueWrite d e m)
=> (c -> (b, e)) -> a -> d -> ReactiveFieldWrite m c
liftW2 :: (c -> (b, e)) -> a -> d -> ReactiveFieldWrite m c
liftW2 c -> (b, e)
f a
e1 d
e2 = FieldSetter m c -> ReactiveFieldWrite m c
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite FieldSetter m c
forall (m :: * -> *).
(Monad m, ReactiveValueWrite a b m, ReactiveValueWrite d e m) =>
c -> m ()
setter
where
setter :: c -> m ()
setter c
x = do let (b
v1, e
v2) = c -> (b, e)
f c
x
a -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a
e1 b
v1
d -> e -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite d
e2 e
v2
(&.&) :: (Monad m, ReactiveValueWrite a b m, ReactiveValueWrite c b m)
=> a -> c -> ReactiveFieldWrite m b
&.& :: a -> c -> ReactiveFieldWrite m b
(&.&) a
v1 c
v2 = FieldSetter m b -> ReactiveFieldWrite m b
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite (FieldSetter m b -> ReactiveFieldWrite m b)
-> FieldSetter m b -> ReactiveFieldWrite m b
forall a b. (a -> b) -> a -> b
$ \b
x -> do
a -> FieldSetter m b
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a
v1 b
x
c -> FieldSetter m b
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite c
v2 b
x
liftMW :: (Monad m, ReactiveValueWrite a b m)
=> (c -> m b) -> a -> ReactiveFieldWrite m c
liftMW :: (c -> m b) -> a -> ReactiveFieldWrite m c
liftMW c -> m b
f a
e = FieldSetter m c -> ReactiveFieldWrite m c
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite FieldSetter m c
setter
where
setter :: FieldSetter m c
setter c
x = a -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a
e (b -> m ()) -> m b -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< c -> m b
f c
x
writeOnly :: ReactiveValueWrite r a m => r -> ReactiveFieldWrite m a
writeOnly :: r -> ReactiveFieldWrite m a
writeOnly r
r = FieldSetter m a -> ReactiveFieldWrite m a
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite (r -> FieldSetter m a
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite r
r)
wrapMW :: (a -> m ()) -> ReactiveFieldWrite m a
wrapMW :: (a -> m ()) -> ReactiveFieldWrite m a
wrapMW = (a -> m ()) -> ReactiveFieldWrite m a
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite
wrapDo :: m () -> ReactiveFieldWrite m a
wrapDo :: m () -> ReactiveFieldWrite m a
wrapDo = (a -> m ()) -> ReactiveFieldWrite m a
forall a (m :: * -> *). (a -> m ()) -> ReactiveFieldWrite m a
wrapMW ((a -> m ()) -> ReactiveFieldWrite m a)
-> (m () -> a -> m ()) -> m () -> ReactiveFieldWrite m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m () -> a -> m ()
forall a b. a -> b -> a
const
wrapDo_ :: m () -> ReactiveFieldWrite m ()
wrapDo_ :: m () -> ReactiveFieldWrite m ()
wrapDo_ = m () -> ReactiveFieldWrite m ()
forall (m :: * -> *) a. m () -> ReactiveFieldWrite m a
wrapDo
newtype BijectiveFunc a b = BijectiveFunc
{ BijectiveFunc a b -> (a -> b, b -> a)
unBijectiveFunc :: (a -> b, b -> a) }
bijection :: (a -> b, b -> a) -> BijectiveFunc a b
bijection :: (a -> b, b -> a) -> BijectiveFunc a b
bijection = (a -> b, b -> a) -> BijectiveFunc a b
forall a b. (a -> b, b -> a) -> BijectiveFunc a b
BijectiveFunc
{-# ANN direct "HLint: ignore Redundant bracket" #-}
direct :: BijectiveFunc a b -> (a -> b)
direct :: BijectiveFunc a b -> a -> b
direct = (a -> b, b -> a) -> a -> b
forall a b. (a, b) -> a
fst ((a -> b, b -> a) -> a -> b)
-> (BijectiveFunc a b -> (a -> b, b -> a))
-> BijectiveFunc a b
-> a
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BijectiveFunc a b -> (a -> b, b -> a)
forall a b. BijectiveFunc a b -> (a -> b, b -> a)
unBijectiveFunc
{-# ANN inverse "HLint: ignore Redundant bracket" #-}
inverse :: BijectiveFunc a b -> (b -> a)
inverse :: BijectiveFunc a b -> b -> a
inverse = (a -> b, b -> a) -> b -> a
forall a b. (a, b) -> b
snd ((a -> b, b -> a) -> b -> a)
-> (BijectiveFunc a b -> (a -> b, b -> a))
-> BijectiveFunc a b
-> b
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BijectiveFunc a b -> (a -> b, b -> a)
forall a b. BijectiveFunc a b -> (a -> b, b -> a)
unBijectiveFunc
type Involution a = BijectiveFunc a a
involution :: (a -> a) -> Involution a
involution :: (a -> a) -> Involution a
involution a -> a
f = (a -> a, a -> a) -> Involution a
forall a b. (a -> b, b -> a) -> BijectiveFunc a b
BijectiveFunc (a -> a
f, a -> a
f)
liftRW :: (Monad m, ReactiveValueReadWrite a b m)
=> BijectiveFunc b c -> a -> ReactiveFieldReadWrite m c
liftRW :: BijectiveFunc b c -> a -> ReactiveFieldReadWrite m c
liftRW (BijectiveFunc (b -> c
f1, c -> b
f2)) a
e =
FieldSetter m c
-> FieldGetter m c
-> FieldNotifier m c
-> ReactiveFieldReadWrite m c
forall (m :: * -> *) a.
FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
ReactiveFieldReadWrite FieldSetter m c
setter FieldGetter m c
getter FieldNotifier m c
notifier
where
ReactiveFieldRead FieldGetter m c
getter FieldNotifier m c
notifier = (b -> c) -> a -> ReactiveFieldRead m c
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueRead a b m) =>
(b -> c) -> a -> ReactiveFieldRead m c
liftR b -> c
f1 a
e
ReactiveFieldWrite FieldSetter m c
setter = (c -> b) -> a -> ReactiveFieldWrite m c
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueWrite a b m) =>
(c -> b) -> a -> ReactiveFieldWrite m c
liftW c -> b
f2 a
e
liftRW2 :: (Monad m, ReactiveValueReadWrite a b m, ReactiveValueReadWrite c d m)
=> BijectiveFunc e (b, d) -> a -> c -> ReactiveFieldReadWrite m e
liftRW2 :: BijectiveFunc e (b, d) -> a -> c -> ReactiveFieldReadWrite m e
liftRW2 (BijectiveFunc (e -> (b, d)
f1, (b, d) -> e
f2)) a
e1 c
e2 =
FieldSetter m e
-> FieldGetter m e
-> FieldNotifier m e
-> ReactiveFieldReadWrite m e
forall (m :: * -> *) a.
FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
ReactiveFieldReadWrite FieldSetter m e
setter FieldGetter m e
getter FieldNotifier m e
notifier
where
ReactiveFieldRead FieldGetter m e
getter FieldNotifier m e
notifier = (b -> d -> e) -> a -> c -> ReactiveFieldRead m e
forall (m :: * -> *) a b c d e.
(Monad m, ReactiveValueRead a b m, ReactiveValueRead c d m) =>
(b -> d -> e) -> a -> c -> ReactiveFieldRead m e
liftR2 (((b, d) -> e) -> b -> d -> e
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (b, d) -> e
f2) a
e1 c
e2
ReactiveFieldWrite FieldSetter m e
setter = (e -> (b, d)) -> a -> c -> ReactiveFieldWrite m e
forall (m :: * -> *) a b d e c.
(Monad m, ReactiveValueWrite a b m, ReactiveValueWrite d e m) =>
(c -> (b, e)) -> a -> d -> ReactiveFieldWrite m c
liftW2 e -> (b, d)
f1 a
e1 c
e2
pairRW :: (Monad m,
ReactiveValueReadWrite a b m,
ReactiveValueReadWrite c d m)
=> a -> c
-> ReactiveFieldReadWrite m (b, d)
pairRW :: a -> c -> ReactiveFieldReadWrite m (b, d)
pairRW = BijectiveFunc (b, d) (b, d)
-> a -> c -> ReactiveFieldReadWrite m (b, d)
forall (m :: * -> *) a b c d e.
(Monad m, ReactiveValueReadWrite a b m,
ReactiveValueReadWrite c d m) =>
BijectiveFunc e (b, d) -> a -> c -> ReactiveFieldReadWrite m e
liftRW2 (((b, d) -> (b, d), (b, d) -> (b, d)) -> BijectiveFunc (b, d) (b, d)
forall a b. (a -> b, b -> a) -> BijectiveFunc a b
bijection ((b, d) -> (b, d)
forall a. a -> a
id, (b, d) -> (b, d)
forall a. a -> a
id))
{-# INLINE eqCheck #-}
eqCheck :: (Eq v, Monad m)
=> ReactiveFieldReadWrite m v
-> ReactiveFieldReadWrite m v
eqCheck :: ReactiveFieldReadWrite m v -> ReactiveFieldReadWrite m v
eqCheck (ReactiveFieldReadWrite FieldSetter m v
setter FieldGetter m v
getter FieldNotifier m v
notifier) =
FieldSetter m v
-> FieldGetter m v
-> FieldNotifier m v
-> ReactiveFieldReadWrite m v
forall (m :: * -> *) a.
FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
ReactiveFieldReadWrite FieldSetter m v
setter' FieldGetter m v
getter FieldNotifier m v
notifier
where
setter' :: FieldSetter m v
setter' v
v = do v
o <- FieldGetter m v
getter
Bool -> FieldNotifier m v
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (v
o v -> v -> Bool
forall a. Eq a => a -> a -> Bool
/= v
v) FieldNotifier m v -> FieldNotifier m v
forall a b. (a -> b) -> a -> b
$ FieldSetter m v
setter v
v
modRW :: (Monad m, ReactiveValueReadWrite a b m)
=> (b -> c -> b) -> a -> ReactiveFieldWrite m c
modRW :: (b -> c -> b) -> a -> ReactiveFieldWrite m c
modRW b -> c -> b
f a
rv = FieldSetter m c -> ReactiveFieldWrite m c
forall (m :: * -> *) a. FieldSetter m a -> ReactiveFieldWrite m a
ReactiveFieldWrite FieldSetter m c
forall (m :: * -> *).
(ReactiveValueRead a b m, ReactiveValueWrite a b m) =>
c -> m ()
setter
where
setter :: c -> m ()
setter c
c = do b
b <- a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
rv
let b' :: b
b' = b -> c -> b
f b
b c
c
a -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a
rv b
b'
reactiveValueModify :: (Monad m, ReactiveValueReadWrite a b m)
=> a
-> (b -> b)
-> m ()
reactiveValueModify :: a -> (b -> b) -> m ()
reactiveValueModify a
r b -> b
f = a -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a
r (b -> m ()) -> (b -> b) -> b -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b
f (b -> m ()) -> m b -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
r
{-# ANN lMerge "HLint: ignore Use const" #-}
lMerge :: (Monad m, ReactiveValueRead a v m, ReactiveValueRead b v m)
=> a -> b -> ReactiveFieldRead m v
lMerge :: a -> b -> ReactiveFieldRead m v
lMerge = (v -> v -> v) -> a -> b -> ReactiveFieldRead m v
forall (m :: * -> *) a b c d e.
(Monad m, ReactiveValueRead a b m, ReactiveValueRead c d m) =>
(b -> d -> e) -> a -> c -> ReactiveFieldRead m e
liftR2 (\v
a v
_ -> v
a)
rMerge :: (Monad m, ReactiveValueRead a v m, ReactiveValueRead b v m)
=> a -> b -> ReactiveFieldRead m v
rMerge :: a -> b -> ReactiveFieldRead m v
rMerge = (v -> v -> v) -> a -> b -> ReactiveFieldRead m v
forall (m :: * -> *) a b c d e.
(Monad m, ReactiveValueRead a b m, ReactiveValueRead c d m) =>
(b -> d -> e) -> a -> c -> ReactiveFieldRead m e
liftR2 (\v
_ v
b -> v
b)
passivelyR :: (Monad m, ReactiveValueRead a b m)
=> a -> ReactiveFieldRead m b
passivelyR :: a -> ReactiveFieldRead m b
passivelyR a
rv =
FieldGetter m b -> FieldNotifier m b -> ReactiveFieldRead m b
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead (a -> FieldGetter m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
rv) (\m ()
_ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
passivelyRW :: (Monad m, ReactiveValueReadWrite a b m)
=> a -> ReactiveFieldReadWrite m b
passivelyRW :: a -> ReactiveFieldReadWrite m b
passivelyRW a
rv =
FieldSetter m b
-> FieldGetter m b
-> FieldNotifier m b
-> ReactiveFieldReadWrite m b
forall (m :: * -> *) a.
FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
ReactiveFieldReadWrite
(a -> FieldSetter m b
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite a
rv)
(a -> FieldGetter m b
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead a
rv)
(\m ()
_ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
governingR :: (ReactiveValueRead a b m, ReactiveValueRead c d m)
=> a -> c -> ReactiveFieldRead m d
governingR :: a -> c -> ReactiveFieldRead m d
governingR a
r c
c = FieldGetter m d -> FieldNotifier m d -> ReactiveFieldRead m d
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m d
getter FieldNotifier m d
notifier
where
getter :: FieldGetter m d
getter = c -> FieldGetter m d
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
notifier :: FieldNotifier m d
notifier = a -> FieldNotifier m d
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
r
governingRW :: (ReactiveValueRead a b m, ReactiveValueReadWrite c d m)
=> a -> c -> ReactiveFieldReadWrite m d
governingRW :: a -> c -> ReactiveFieldReadWrite m d
governingRW a
r c
c = FieldSetter m d
-> FieldGetter m d
-> FieldNotifier m d
-> ReactiveFieldReadWrite m d
forall (m :: * -> *) a.
FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
ReactiveFieldReadWrite FieldSetter m d
setter FieldGetter m d
getter FieldNotifier m d
notifier
where
getter :: FieldGetter m d
getter = c -> FieldGetter m d
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
setter :: FieldSetter m d
setter = c -> FieldSetter m d
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite c
c
notifier :: FieldNotifier m d
notifier = a -> FieldNotifier m d
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead a
r
ifRW :: (Monad m, ReactiveValueRead c Bool m, ReactiveValueReadWrite v a m)
=> c -> v
-> ReactiveFieldReadWrite m a
ifRW :: c -> v -> ReactiveFieldReadWrite m a
ifRW c
c v
r = FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
forall (m :: * -> *) a.
FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
ReactiveFieldReadWrite FieldSetter m a
forall (m :: * -> *) b.
(ReactiveValueRead c Bool m, ReactiveValueWrite v b m) =>
b -> m ()
setter FieldGetter m a
getter FieldNotifier m a
forall (m :: * -> *) b.
(ReactiveValueRead c Bool m, ReactiveValueRead v b m) =>
m () -> m ()
notifier
where
setter :: b -> m ()
setter b
x = do Bool
b <- c -> m Bool
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
b (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v -> b -> m ()
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite v
r b
x
getter :: FieldGetter m a
getter = v -> FieldGetter m a
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead v
r
notifier :: m () -> m ()
notifier m ()
p = do c -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead c
c (m () -> m ()
forall (m :: * -> *). ReactiveValueRead c Bool m => m () -> m ()
when' m ()
p)
v -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead v
r (m () -> m ()
forall (m :: * -> *). ReactiveValueRead c Bool m => m () -> m ()
when' m ()
p)
where
when' :: m () -> m ()
when' m ()
m = do Bool
b <- c -> m Bool
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
b m ()
m
ifRW_ :: (Monad m, ReactiveValueRead c Bool m, ReactiveValueReadWrite v a m)
=> c -> v
-> ReactiveFieldReadWrite m a
ifRW_ :: c -> v -> ReactiveFieldReadWrite m a
ifRW_ c
c v
r = FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
forall (m :: * -> *) a.
FieldSetter m a
-> FieldGetter m a
-> FieldNotifier m a
-> ReactiveFieldReadWrite m a
ReactiveFieldReadWrite FieldSetter m a
setter FieldGetter m a
getter FieldNotifier m a
forall (m :: * -> *) b.
(ReactiveValueRead c Bool m, ReactiveValueRead v b m) =>
m () -> m ()
notifier
where
setter :: FieldSetter m a
setter = v -> FieldSetter m a
forall a b (m :: * -> *).
ReactiveValueWrite a b m =>
a -> b -> m ()
reactiveValueWrite v
r
getter :: FieldGetter m a
getter = v -> FieldGetter m a
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead v
r
notifier :: m () -> m ()
notifier m ()
p = do c -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead c
c (m () -> m ()
forall (m :: * -> *). ReactiveValueRead c Bool m => m () -> m ()
when' m ()
p)
v -> m () -> m ()
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead v
r (m () -> m ()
forall (m :: * -> *). ReactiveValueRead c Bool m => m () -> m ()
when' m ()
p)
where
when' :: m () -> m ()
when' m ()
m = do Bool
x <- c -> m Bool
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
x m ()
m
guardRO :: (Monad m, ReactiveValueRead c Bool m)
=> c
-> ReactiveFieldRead m Bool
guardRO :: c -> ReactiveFieldRead m Bool
guardRO c
c = FieldGetter m Bool
-> FieldNotifier m Bool -> ReactiveFieldRead m Bool
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m Bool
getter FieldNotifier m Bool
notifier
where
getter :: FieldGetter m Bool
getter = c -> FieldGetter m Bool
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
notifier :: FieldNotifier m Bool
notifier = c -> FieldNotifier m Bool
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead c
c FieldNotifier m Bool
-> FieldNotifier m Bool -> FieldNotifier m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldNotifier m Bool
forall (m :: * -> *). ReactiveValueRead c Bool m => m () -> m ()
when'
where
when' :: m () -> m ()
when' m ()
m = do Bool
x <- c -> m Bool
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
x m ()
m
guardRO' :: (Monad m, ReactiveValueRead c a m)
=> c
-> (a -> Bool)
-> ReactiveFieldRead m a
guardRO' :: c -> (a -> Bool) -> ReactiveFieldRead m a
guardRO' c
c a -> Bool
p = FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
forall (m :: * -> *) a.
FieldGetter m a -> FieldNotifier m a -> ReactiveFieldRead m a
ReactiveFieldRead FieldGetter m a
getter FieldNotifier m a
notifier
where
getter :: FieldGetter m a
getter = c -> FieldGetter m a
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
notifier :: FieldNotifier m a
notifier = c -> FieldNotifier m a
forall a b (m :: * -> *).
ReactiveValueRead a b m =>
a -> m () -> m ()
reactiveValueOnCanRead c
c FieldNotifier m a -> FieldNotifier m a -> FieldNotifier m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldNotifier m a
forall (m :: * -> *). ReactiveValueRead c a m => m () -> m ()
when'
where
when' :: m () -> m ()
when' m ()
m = do a
x <- c -> m a
forall a b (m :: * -> *). ReactiveValueRead a b m => a -> m b
reactiveValueRead c
c
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (a -> Bool
p a
x) m ()
m
instance (Functor m, Monad m) => Functor (ReactiveFieldRead m) where
fmap :: (a -> b) -> ReactiveFieldRead m a -> ReactiveFieldRead m b
fmap = (a -> b) -> ReactiveFieldRead m a -> ReactiveFieldRead m b
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueRead a b m) =>
(b -> c) -> a -> ReactiveFieldRead m c
liftR
instance (Monad m) => Contravariant (ReactiveFieldWrite m) where
contramap :: (a -> b) -> ReactiveFieldWrite m b -> ReactiveFieldWrite m a
contramap = (a -> b) -> ReactiveFieldWrite m b -> ReactiveFieldWrite m a
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueWrite a b m) =>
(c -> b) -> a -> ReactiveFieldWrite m c
liftW
instance Monad m => GFunctor (ReactiveFieldReadWrite m) BijectiveFunc where
gmap :: BijectiveFunc a b
-> ReactiveFieldReadWrite m a -> ReactiveFieldReadWrite m b
gmap = BijectiveFunc a b
-> ReactiveFieldReadWrite m a -> ReactiveFieldReadWrite m b
forall (m :: * -> *) a b c.
(Monad m, ReactiveValueReadWrite a b m) =>
BijectiveFunc b c -> a -> ReactiveFieldReadWrite m c
liftRW