{-# LANGUAGE Safe, DefaultSignatures, GADTs, UndecidableInstances, BangPatterns #-}
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-}
module Data.Property
(
Prop (..), get, set, setProp,
GetProp (..),
SetProp (..),
ModifyProp (..), InsertProp (..), DeleteProp (..), SwitchProp (..)
)
where
import Data.Functor
default ()
data Prop m field record
where
Incr :: (Monad m, SwitchProp field a) =>
field m record a -> Prop m field record
Decr :: (Monad m, SwitchProp field a) =>
field m record a -> Prop m field record
Switch :: (Monad m, SwitchProp field a) =>
field m record a -> Int -> Prop m field record
(:=) :: (Monad m, SetProp field record) =>
field m record a -> a -> Prop m field record
(::=) :: (Monad m, SetProp field record) =>
field m record a -> (record -> a) -> Prop m field record
(:~) :: (Monad m, ModifyProp field record) =>
field m record a -> (a -> a) -> Prop m field record
(::~) :: (Monad m, ModifyProp field record) =>
field m record a -> (record -> a -> a) -> Prop m field record
(:=+) :: (Monad m, InsertProp field record many) =>
field m record (many a) -> a -> Prop m field record
(:+=) :: (Monad m, InsertProp field record many) =>
a -> field m record (many a) -> Prop m field record
(:~=) :: (Monad m, DeleteProp field record many, Eq a) =>
field m record (many a) -> a -> Prop m field record
get :: (Monad m, GetProp field record) => field m record a -> record -> m a
get :: field m record a -> record -> m a
get = field m record a -> record -> m a
forall (field :: (* -> *) -> * -> * -> *) record (m :: * -> *) a.
(GetProp field record, Monad m) =>
field m record a -> record -> m a
getRecord
set :: (Monad m) => record -> [Prop m field record] -> m ()
set :: record -> [Prop m field record] -> m ()
set = record -> [Prop m field record] -> m ()
forall (m :: * -> *) record (field :: (* -> *) -> * -> * -> *).
Monad m =>
record -> [Prop m field record] -> m ()
setProp
setProp :: (Monad m) => record -> [Prop m field record] -> m ()
setProp :: record -> [Prop m field record] -> m ()
setProp record
record = (Prop m field record -> m ()) -> [Prop m field record] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((Prop m field record -> m ()) -> [Prop m field record] -> m ())
-> (Prop m field record -> m ()) -> [Prop m field record] -> m ()
forall a b. (a -> b) -> a -> b
$ \ Prop m field record
prop -> case Prop m field record
prop of
Incr field m record a
field -> field m record a -> record -> m ()
forall (field :: (* -> *) -> * -> * -> *) a (m :: * -> *) record.
(SwitchProp field a, Monad m) =>
field m record a -> record -> m ()
incRecord field m record a
field record
record
Decr field m record a
field -> field m record a -> record -> m ()
forall (field :: (* -> *) -> * -> * -> *) a (m :: * -> *) record.
(SwitchProp field a, Monad m) =>
field m record a -> record -> m ()
decRecord field m record a
field record
record
Switch field m record a
field Int
n -> field m record a -> record -> Int -> m ()
forall (field :: (* -> *) -> * -> * -> *) a (m :: * -> *) record.
(SwitchProp field a, Monad m) =>
field m record a -> record -> Int -> m ()
switchRecord field m record a
field record
record Int
n
field m record a
field ::= record -> a
upd -> field m record a -> record -> a -> m ()
forall (field :: (* -> *) -> * -> * -> *) record (m :: * -> *) a.
(SetProp field record, Monad m) =>
field m record a -> record -> a -> m ()
setRecord field m record a
field record
record (record -> a
upd record
record)
field m record a
field := a
val -> field m record a -> record -> a -> m ()
forall (field :: (* -> *) -> * -> * -> *) record (m :: * -> *) a.
(SetProp field record, Monad m) =>
field m record a -> record -> a -> m ()
setRecord field m record a
field record
record a
val
field m record a
field ::~ record -> a -> a
upd -> m a -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m a -> m ()) -> m a -> m ()
forall a b. (a -> b) -> a -> b
$ field m record a -> record -> (a -> a) -> m a
forall (field :: (* -> *) -> * -> * -> *) record (m :: * -> *) a.
(ModifyProp field record, Monad m) =>
field m record a -> record -> (a -> a) -> m a
modifyRecord field m record a
field record
record (record -> a -> a
upd record
record)
field m record a
field :~ a -> a
upd -> m a -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m a -> m ()) -> m a -> m ()
forall a b. (a -> b) -> a -> b
$ field m record a -> record -> (a -> a) -> m a
forall (field :: (* -> *) -> * -> * -> *) record (m :: * -> *) a.
(ModifyProp field record, Monad m) =>
field m record a -> record -> (a -> a) -> m a
modifyRecord field m record a
field record
record a -> a
upd
field m record (many a)
field :=+ a
val -> m (many a) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (many a) -> m ()) -> m (many a) -> m ()
forall a b. (a -> b) -> a -> b
$ a -> field m record (many a) -> record -> m (many a)
forall (field :: (* -> *) -> * -> * -> *) record (many :: * -> *)
(m :: * -> *) a.
(InsertProp field record many, Monad m) =>
a -> field m record (many a) -> record -> m (many a)
prependRecord a
val field m record (many a)
field record
record
a
val :+= field m record (many a)
field -> m (many a) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (many a) -> m ()) -> m (many a) -> m ()
forall a b. (a -> b) -> a -> b
$ a -> field m record (many a) -> record -> m (many a)
forall (field :: (* -> *) -> * -> * -> *) record (many :: * -> *)
(m :: * -> *) a.
(InsertProp field record many, Monad m) =>
a -> field m record (many a) -> record -> m (many a)
appendRecord a
val field m record (many a)
field record
record
field m record (many a)
field :~= a
val -> m (many a) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (many a) -> m ()) -> m (many a) -> m ()
forall a b. (a -> b) -> a -> b
$ a -> field m record (many a) -> record -> m (many a)
forall (field :: (* -> *) -> * -> * -> *) record (many :: * -> *)
(m :: * -> *) a.
(DeleteProp field record many, Monad m, Eq a) =>
a -> field m record (many a) -> record -> m (many a)
deleteRecord a
val field m record (many a)
field record
record
class GetProp field record
where
getRecord :: (Monad m) => field m record a -> record -> m a
class SetProp field record
where
setRecord :: (Monad m) => field m record a -> record -> a -> m ()
class ModifyProp field record
where
default modifyRecord :: (Monad m, GetProp field record, SetProp field record) =>
field m record a -> record -> (a -> a) -> m a
modifyRecord :: (Monad m) => field m record a -> record -> (a -> a) -> m a
modifyRecord field m record a
field record
record a -> a
f = do
a
old <- field m record a -> record -> m a
forall (m :: * -> *) (field :: (* -> *) -> * -> * -> *) record a.
(Monad m, GetProp field record) =>
field m record a -> record -> m a
get field m record a
field record
record
let new :: a
new = a -> a
f a
old
field m record a -> record -> a -> m ()
forall (field :: (* -> *) -> * -> * -> *) record (m :: * -> *) a.
(SetProp field record, Monad m) =>
field m record a -> record -> a -> m ()
setRecord field m record a
field record
record a
new
a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
new
class SwitchProp field a
where
incRecord :: (Monad m) => field m record a -> record -> m ()
decRecord :: (Monad m) => field m record a -> record -> m ()
switchRecord :: (Monad m) => field m record a -> record -> Int -> m ()
class InsertProp field record many
where
prependRecord :: (Monad m) => a -> field m record (many a) -> record -> m (many a)
appendRecord :: (Monad m) => a -> field m record (many a) -> record -> m (many a)
class DeleteProp field record many
where
deleteRecord :: (Monad m, Eq a) => a -> field m record (many a) -> record -> m (many a)