{-# LANGUAGE TypeFamilies #-}
module Data.Recursive.Set
( R
, mkR
, getR
, module Data.Recursive.Set
) where
import qualified Data.Set as S
import Data.Coerce
import Data.Monoid
import Control.Monad
import Data.Recursive.R.Internal
import Data.Recursive.Propagator.Naive
import Data.Recursive.Propagator.P2
rEmpty :: Eq a => R (S.Set a)
rEmpty :: forall a. Eq a => R (Set a)
rEmpty = forall a. HasPropagator a => a -> R a
mkR forall a. Set a
S.empty
rInsert :: Ord a => a -> R (S.Set a) -> R (S.Set a)
rInsert :: forall a. Ord a => a -> R (Set a) -> R (Set a)
rInsert a
x = forall a b.
(HasPropagator a, HasPropagator b) =>
(Prop a -> Prop b -> IO ()) -> R a -> R b
defR1 forall a b. (a -> b) -> a -> b
$ forall b a. Eq b => (a -> b) -> Prop a -> Prop b -> IO ()
lift1 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> Set a -> Set a
S.insert a
x
rDelete :: Ord a => a -> R (S.Set a) -> R (S.Set a)
rDelete :: forall a. Ord a => a -> R (Set a) -> R (Set a)
rDelete a
x = forall a b.
(HasPropagator a, HasPropagator b) =>
(Prop a -> Prop b -> IO ()) -> R a -> R b
defR1 forall a b. (a -> b) -> a -> b
$ forall b a. Eq b => (a -> b) -> Prop a -> Prop b -> IO ()
lift1 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> Set a -> Set a
S.delete a
x
rFilter :: Ord a => (a -> Bool) -> R (S.Set a) -> R (S.Set a)
rFilter :: forall a. Ord a => (a -> Bool) -> R (Set a) -> R (Set a)
rFilter a -> Bool
f = forall a b.
(HasPropagator a, HasPropagator b) =>
(Prop a -> Prop b -> IO ()) -> R a -> R b
defR1 forall a b. (a -> b) -> a -> b
$ forall b a. Eq b => (a -> b) -> Prop a -> Prop b -> IO ()
lift1 forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> Set a -> Set a
S.filter a -> Bool
f
rUnion :: Ord a => R (S.Set a) -> R (S.Set a) -> R (S.Set a)
rUnion :: forall a. Ord a => R (Set a) -> R (Set a) -> R (Set a)
rUnion = forall a b c.
(HasPropagator a, HasPropagator b, HasPropagator c) =>
(Prop a -> Prop b -> Prop c -> IO ()) -> R a -> R b -> R c
defR2 forall a b. (a -> b) -> a -> b
$ forall c a b.
Eq c =>
(a -> b -> c) -> Prop a -> Prop b -> Prop c -> IO ()
lift2 forall a. Ord a => Set a -> Set a -> Set a
S.union
rUnions :: Ord a => [R (S.Set a)] -> R (S.Set a)
rUnions :: forall a. Ord a => [R (Set a)] -> R (Set a)
rUnions = forall a b.
(HasPropagator a, HasPropagator b) =>
([Prop a] -> Prop b -> IO ()) -> [R a] -> R b
defRList forall a b. (a -> b) -> a -> b
$ forall b a. Eq b => ([a] -> b) -> [Prop a] -> Prop b -> IO ()
liftList forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
S.unions
rIntersection :: Ord a => R (S.Set a) -> R (S.Set a) -> R (S.Set a)
rIntersection :: forall a. Ord a => R (Set a) -> R (Set a) -> R (Set a)
rIntersection = forall a b c.
(HasPropagator a, HasPropagator b, HasPropagator c) =>
(Prop a -> Prop b -> Prop c -> IO ()) -> R a -> R b -> R c
defR2 forall a b. (a -> b) -> a -> b
$ forall c a b.
Eq c =>
(a -> b -> c) -> Prop a -> Prop b -> Prop c -> IO ()
lift2 forall a. Ord a => Set a -> Set a -> Set a
S.intersection
rMember :: Ord a => a -> R (S.Set a) -> R Bool
rMember :: forall a. Ord a => a -> R (Set a) -> R Bool
rMember a
x = forall a b.
(HasPropagator a, HasPropagator b) =>
(Prop a -> Prop b -> IO ()) -> R a -> R b
defR1 forall a b. (a -> b) -> a -> b
$ \Prop (Set a)
ps Prop Bool
pb -> do
let update :: IO ()
update = do
Set a
s <- forall a. Prop a -> IO a
readProp Prop (Set a)
ps
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. Ord a => a -> Set a -> Bool
S.member a
x Set a
s) forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce P2 -> IO ()
setTop Prop Bool
pb
forall a. Prop a -> IO () -> IO ()
watchProp Prop (Set a)
ps IO ()
update
IO ()
update
rNotMember :: Ord a => a -> R (S.Set a) -> R (Dual Bool)
rNotMember :: forall a. Ord a => a -> R (Set a) -> R (Dual Bool)
rNotMember a
x = forall a b.
(HasPropagator a, HasPropagator b) =>
(Prop a -> Prop b -> IO ()) -> R a -> R b
defR1 forall a b. (a -> b) -> a -> b
$ \Prop (Set a)
ps Prop (Dual Bool)
pb -> do
let update :: IO ()
update = do
Set a
s <- forall a. Prop a -> IO a
readProp Prop (Set a)
ps
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. Ord a => a -> Set a -> Bool
S.member a
x Set a
s) forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce P2 -> IO ()
setTop Prop (Dual Bool)
pb
forall a. Prop a -> IO () -> IO ()
watchProp Prop (Set a)
ps IO ()
update
IO ()
update
rDisjoint :: Ord a => R (S.Set a) -> R (S.Set a) -> R (Dual Bool)
rDisjoint :: forall a. Ord a => R (Set a) -> R (Set a) -> R (Dual Bool)
rDisjoint = forall a b c.
(HasPropagator a, HasPropagator b, HasPropagator c) =>
(Prop a -> Prop b -> Prop c -> IO ()) -> R a -> R b -> R c
defR2 forall a b. (a -> b) -> a -> b
$ \Prop (Set a)
ps1 Prop (Set a)
ps2 (PDualBool P2
pb) -> do
let update :: IO ()
update = do
Set a
s1 <- forall a. Prop a -> IO a
readProp Prop (Set a)
ps1
Set a
s2 <- forall a. Prop a -> IO a
readProp Prop (Set a)
ps2
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall a. Ord a => Set a -> Set a -> Bool
S.disjoint Set a
s1 Set a
s2) forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce P2 -> IO ()
setTop P2
pb
forall a. Prop a -> IO () -> IO ()
watchProp Prop (Set a)
ps1 IO ()
update
forall a. Prop a -> IO () -> IO ()
watchProp Prop (Set a)
ps2 IO ()
update
IO ()
update