{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DeriveTraversable #-}
#if __GLASGOW_HASKELL__ < 710
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
#endif
{-# LANGUAGE Safe #-}

module Control.Monad.Logic.Sequence.Internal.Queue
(  Queue
)
where

import Data.SequenceClass hiding ((:<))
import qualified Data.SequenceClass as S

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif

#if !MIN_VERSION_base(4,8,0)
import Data.Monoid (Monoid(..))
#endif

#if MIN_VERSION_base(4,9,0) && !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup(..))
#endif

import qualified Data.Foldable as F
import qualified Data.Traversable as T
import qualified Control.Monad.Logic.Sequence.Internal.ScheduledQueue as SQ


-- | Based on the LogicT improvements in the paper, Reflection without
-- Remorse. Code is based on the code provided in:
-- https://github.com/atzeus/reflectionwithoutremorse
--
-- Note: that code is provided under an MIT license, so we use that as
-- well.

data Queue a
  = Empty
  | a :< SQ.Queue (Queue a)
  deriving (forall a b. a -> Queue b -> Queue a
forall a b. (a -> b) -> Queue a -> Queue b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Queue b -> Queue a
$c<$ :: forall a b. a -> Queue b -> Queue a
fmap :: forall a b. (a -> b) -> Queue a -> Queue b
$cfmap :: forall a b. (a -> b) -> Queue a -> Queue b
Functor, forall a. Eq a => a -> Queue a -> Bool
forall a. Num a => Queue a -> a
forall a. Ord a => Queue a -> a
forall m. Monoid m => Queue m -> m
forall a. Queue a -> Bool
forall a. Queue a -> Int
forall a. Queue a -> [a]
forall a. (a -> a -> a) -> Queue a -> a
forall m a. Monoid m => (a -> m) -> Queue a -> m
forall b a. (b -> a -> b) -> b -> Queue a -> b
forall a b. (a -> b -> b) -> b -> Queue a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Queue a -> a
$cproduct :: forall a. Num a => Queue a -> a
sum :: forall a. Num a => Queue a -> a
$csum :: forall a. Num a => Queue a -> a
minimum :: forall a. Ord a => Queue a -> a
$cminimum :: forall a. Ord a => Queue a -> a
maximum :: forall a. Ord a => Queue a -> a
$cmaximum :: forall a. Ord a => Queue a -> a
elem :: forall a. Eq a => a -> Queue a -> Bool
$celem :: forall a. Eq a => a -> Queue a -> Bool
length :: forall a. Queue a -> Int
$clength :: forall a. Queue a -> Int
null :: forall a. Queue a -> Bool
$cnull :: forall a. Queue a -> Bool
toList :: forall a. Queue a -> [a]
$ctoList :: forall a. Queue a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Queue a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Queue a -> a
foldr1 :: forall a. (a -> a -> a) -> Queue a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Queue a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Queue a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Queue a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Queue a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Queue a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Queue a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Queue a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Queue a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Queue a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Queue a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Queue a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Queue a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Queue a -> m
fold :: forall m. Monoid m => Queue m -> m
$cfold :: forall m. Monoid m => Queue m -> m
F.Foldable, Functor Queue
Foldable Queue
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Queue (m a) -> m (Queue a)
forall (f :: * -> *) a. Applicative f => Queue (f a) -> f (Queue a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Queue a -> m (Queue b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Queue a -> f (Queue b)
sequence :: forall (m :: * -> *) a. Monad m => Queue (m a) -> m (Queue a)
$csequence :: forall (m :: * -> *) a. Monad m => Queue (m a) -> m (Queue a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Queue a -> m (Queue b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Queue a -> m (Queue b)
sequenceA :: forall (f :: * -> *) a. Applicative f => Queue (f a) -> f (Queue a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Queue (f a) -> f (Queue a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Queue a -> f (Queue b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Queue a -> f (Queue b)
T.Traversable)

instance Sequence Queue where
  {-# INLINE empty #-}
  empty :: forall c. Queue c
empty = forall c. Queue c
Empty
  {-# INLINE singleton #-}
  singleton :: forall c. c -> Queue c
singleton c
a = c
a forall a. a -> Queue (Queue a) -> Queue a
:< forall (s :: * -> *) c. Sequence s => s c
S.empty
  {-# INLINE (><) #-}
  Queue c
Empty >< :: forall c. Queue c -> Queue c -> Queue c
>< Queue c
r = Queue c
r
  (c
a :< Queue (Queue c)
q) >< Queue c
r = c
a forall a. a -> Queue (Queue a) -> Queue a
:< (Queue (Queue c)
q forall (s :: * -> *) c. Sequence s => s c -> c -> s c
|> Queue c
r)
  {-# INLINE (|>) #-}
  Queue c
l |> :: forall c. Queue c -> c -> Queue c
|> c
x = Queue c
l forall (s :: * -> *) c. Sequence s => s c -> s c -> s c
>< forall (s :: * -> *) c. Sequence s => c -> s c
singleton c
x
  {-# INLINE (<|) #-}
  c
x <| :: forall c. c -> Queue c -> Queue c
<| Queue c
r = forall (s :: * -> *) c. Sequence s => c -> s c
singleton c
x forall (s :: * -> *) c. Sequence s => s c -> s c -> s c
>< Queue c
r
  {-# INLINE viewl #-}
  viewl :: forall c. Queue c -> ViewL Queue c
viewl Queue c
Empty     = forall (s :: * -> *) c. ViewL s c
EmptyL
  viewl (c
t :< Queue (Queue c)
q0) = c
t forall c (s :: * -> *). c -> s c -> ViewL s c
S.:< forall a. Queue (Queue a) -> Queue a
linkAll Queue (Queue c)
q0
    where
    linkAll :: SQ.Queue (Queue a) -> Queue a
    linkAll :: forall a. Queue (Queue a) -> Queue a
linkAll Queue (Queue a)
v = case forall (s :: * -> *) c. Sequence s => s c -> ViewL s c
viewl Queue (Queue a)
v of
      ViewL Queue (Queue a)
EmptyL -> forall c. Queue c
Empty
      Queue a
Empty S.:< Queue (Queue a)
t' -> forall a. Queue (Queue a) -> Queue a
linkAll Queue (Queue a)
t'
      (a
x :< Queue (Queue a)
q) S.:< Queue (Queue a)
t' -> a
x forall a. a -> Queue (Queue a) -> Queue a
:< (Queue (Queue a)
q forall (s :: * -> *) c. Sequence s => s c -> c -> s c
|> forall a. Queue (Queue a) -> Queue a
linkAll Queue (Queue a)
t')

#if MIN_VERSION_base(4,9,0)
instance Semigroup (Queue a) where
  {-# INLINE (<>) #-}
  <> :: Queue a -> Queue a -> Queue a
(<>) = forall (s :: * -> *) c. Sequence s => s c -> s c -> s c
(S.><)
#endif

instance Monoid (Queue a) where
  {-# INLINE mempty #-}
  mempty :: Queue a
mempty = forall (s :: * -> *) c. Sequence s => s c
S.empty
  {-# INLINE mappend #-}
#if MIN_VERSION_base(4,9,0)
  mappend :: Queue a -> Queue a -> Queue a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
#else
  mappend = (S.><)
#endif