{-# LANGUAGE TypeApplications #-}

{- | The type @RBool@ is like 'Bool', but allows recursive definitions:

>>> :{
  let x = RB.true
      y = x RB.&& z
      z = y RB.|| RB.false
  in RB.get x
:}
True


This finds the least solution, i.e. prefers 'False' over 'True':

>>> :{
  let x = x RB.&& y
      y = y RB.&& x
  in (RB.get x, RB.get y)
:}
(False,False)

Use 'Data.Recursive.DualBool.RDualBool' from "Data.Recursive.DualBool" if you want the greatest solution.

-}
module Data.Recursive.Bool (RBool, module Data.Recursive.Bool) where


import Data.Recursive.Internal
import qualified Data.Propagator.Purify as Purify
import Data.Propagator.P2

-- $setup
-- >>> :load Data.Recursive.Bool Data.Recursive.DualBool
-- >>> :module - Data.Recursive.Bool Data.Recursive.DualBool
--
-- >>> :set -XFlexibleInstances
-- >>> import Test.QuickCheck
-- >>> import qualified Data.Recursive.Bool as RB
-- >>> instance Arbitrary RB.RBool where arbitrary = RB.mk <$> arbitrary
-- >>> instance Show RB.RBool where show = show . RB.get
--
-- >>> import qualified Data.Recursive.DualBool as RDB
-- >>> instance Arbitrary RDB.RDualBool where arbitrary = RDB.mk <$> arbitrary
-- >>> instance Show RDB.RDualBool where show = show . RDB.get

-- | Extracts the value of a 'RBool'
get :: RBool -> Bool
get :: RBool -> Bool
get (RBool Purify P2
p) = Purify P2 -> Bool
forall pa a. Propagator pa a => Purify pa -> a
Purify.get Purify P2
p

-- | prop> RB.get (RB.mk b) === b
mk :: Bool -> RBool
mk :: Bool -> RBool
mk Bool
b = OpenR RBool -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR RBool -> RBool) -> OpenR RBool -> RBool
forall a b. (a -> b) -> a -> b
$ Bool -> Purify P2
forall p a. Propagator p a => a -> Purify p
Purify.mk Bool
b

-- | prop> RB.get RB.true == True
true :: RBool
true :: RBool
true = OpenR RBool -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR RBool -> RBool) -> OpenR RBool -> RBool
forall a b. (a -> b) -> a -> b
$ Bool -> Purify P2
forall p a. Propagator p a => a -> Purify p
Purify.mk Bool
True

-- | prop> RB.get RB.false == False
false :: RBool
false :: RBool
false = OpenR RBool -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR RBool -> RBool) -> OpenR RBool -> RBool
forall a b. (a -> b) -> a -> b
$ Bool -> Purify P2
forall p a. Propagator p a => a -> Purify p
Purify.mk Bool
False

-- | prop> RB.get (r1 RB.&& r2) === (RB.get r1 && RB.get r2)
(&&) :: RBool -> RBool -> RBool
&& :: RBool -> RBool -> RBool
(&&) = OpenR (RBool -> RBool -> RBool) -> RBool -> RBool -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR (RBool -> RBool -> RBool) -> RBool -> RBool -> RBool)
-> OpenR (RBool -> RBool -> RBool) -> RBool -> RBool -> RBool
forall a b. (a -> b) -> a -> b
$ (P2 -> P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2 -> Purify P2
forall pa a pb b pc c.
(Propagator pa a, Propagator pb b, Propagator pc c) =>
(pa -> pb -> pc -> IO ()) -> Purify pa -> Purify pb -> Purify pc
Purify.def2 ((P2 -> P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2 -> Purify P2)
-> (P2 -> P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2 -> Purify P2
forall a b. (a -> b) -> a -> b
$ \P2
p1 P2
p2 P2
p ->
    P2 -> IO () -> IO ()
whenTop P2
p1 (P2 -> IO () -> IO ()
whenTop P2
p2 (P2 -> IO ()
setTop P2
p))

-- | prop> RB.get (r1 RB.|| r2) === (RB.get r1 || RB.get r2)
(||) :: RBool -> RBool -> RBool
|| :: RBool -> RBool -> RBool
(||) = OpenR (RBool -> RBool -> RBool) -> RBool -> RBool -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR (RBool -> RBool -> RBool) -> RBool -> RBool -> RBool)
-> OpenR (RBool -> RBool -> RBool) -> RBool -> RBool -> RBool
forall a b. (a -> b) -> a -> b
$ (P2 -> P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2 -> Purify P2
forall pa a pb b pc c.
(Propagator pa a, Propagator pb b, Propagator pc c) =>
(pa -> pb -> pc -> IO ()) -> Purify pa -> Purify pb -> Purify pc
Purify.def2 ((P2 -> P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2 -> Purify P2)
-> (P2 -> P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2 -> Purify P2
forall a b. (a -> b) -> a -> b
$ \P2
p1 P2
p2 P2
p -> do
    P2 -> IO () -> IO ()
whenTop P2
p1 (P2 -> IO ()
setTop P2
p)
    P2 -> IO () -> IO ()
whenTop P2
p2 (P2 -> IO ()
setTop P2
p)

-- | prop> RB.get (RB.and rs) === and (map RB.get rs)
and :: [RBool] -> RBool
and :: [RBool] -> RBool
and = OpenR ([RBool] -> RBool) -> [RBool] -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR ([RBool] -> RBool) -> [RBool] -> RBool)
-> OpenR ([RBool] -> RBool) -> [RBool] -> RBool
forall a b. (a -> b) -> a -> b
$ ([P2] -> P2 -> IO ()) -> [Purify P2] -> Purify P2
forall pa a pb b.
(Propagator pa a, Propagator pb b) =>
([pa] -> pb -> IO ()) -> [Purify pa] -> Purify pb
Purify.defList (([P2] -> P2 -> IO ()) -> [Purify P2] -> Purify P2)
-> ([P2] -> P2 -> IO ()) -> [Purify P2] -> Purify P2
forall a b. (a -> b) -> a -> b
$ [P2] -> P2 -> IO ()
go
  where
    go :: [P2] -> P2 -> IO ()
go [] P2
p = P2 -> IO ()
setTop P2
p
    go (P2
p':[P2]
ps) P2
p = P2 -> IO () -> IO ()
whenTop P2
p' ([P2] -> P2 -> IO ()
go [P2]
ps P2
p)

-- | prop> RB.get (RB.or rs) === or (map RB.get rs)
or :: [RBool] -> RBool
or :: [RBool] -> RBool
or = OpenR ([RBool] -> RBool) -> [RBool] -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR ([RBool] -> RBool) -> [RBool] -> RBool)
-> OpenR ([RBool] -> RBool) -> [RBool] -> RBool
forall a b. (a -> b) -> a -> b
$ ([P2] -> P2 -> IO ()) -> [Purify P2] -> Purify P2
forall pa a pb b.
(Propagator pa a, Propagator pb b) =>
([pa] -> pb -> IO ()) -> [Purify pa] -> Purify pb
Purify.defList (([P2] -> P2 -> IO ()) -> [Purify P2] -> Purify P2)
-> ([P2] -> P2 -> IO ()) -> [Purify P2] -> Purify P2
forall a b. (a -> b) -> a -> b
$  \[P2]
ps P2
p ->
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ @[] (P2 -> P2 -> IO ()
`implies` P2
p) [P2]
ps

-- | prop> RB.get (RB.not r1) === not (RDB.get r1)
not :: RDualBool -> RBool
not :: RDualBool -> RBool
not = OpenR (RDualBool -> RBool) -> RDualBool -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR (RDualBool -> RBool) -> RDualBool -> RBool)
-> OpenR (RDualBool -> RBool) -> RDualBool -> RBool
forall a b. (a -> b) -> a -> b
$ (P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2
forall pa a pb b.
(Propagator pa a, Propagator pb b) =>
(pa -> pb -> IO ()) -> Purify pa -> Purify pb
Purify.def1 ((P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2)
-> (P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2
forall a b. (a -> b) -> a -> b
$ \P2
p1 P2
p -> do
    P2 -> P2 -> IO ()
implies P2
p1 P2
p

-- | The identity function. This is useful when tying the knot, to avoid a loop that bottoms out:
--
-- > let x = x in RB.get x
--
-- will not work, but
--
-- >>> let x = RB.id x in RB.get x
-- False
--
-- does.
--
-- prop> RB.get (RB.id r) === RB.get r
id :: RBool -> RBool
id :: RBool -> RBool
id = OpenR (RBool -> RBool) -> RBool -> RBool
forall a. Coercible a (OpenR a) => OpenR a -> a
openR (OpenR (RBool -> RBool) -> RBool -> RBool)
-> OpenR (RBool -> RBool) -> RBool -> RBool
forall a b. (a -> b) -> a -> b
$ (P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2
forall pa a pb b.
(Propagator pa a, Propagator pb b) =>
(pa -> pb -> IO ()) -> Purify pa -> Purify pb
Purify.def1 ((P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2)
-> (P2 -> P2 -> IO ()) -> Purify P2 -> Purify P2
forall a b. (a -> b) -> a -> b
$ \P2
p1 P2
p ->
    P2 -> P2 -> IO ()
implies P2
p1 P2
p