module Math.Haskellator.Internal.Utils.Stack (
Stack
, isEmpty
, mapTop
, pop
, push
, top
) where
newtype Stack a = Stack [a]
instance Functor Stack where
fmap :: forall a b. (a -> b) -> Stack a -> Stack b
fmap a -> b
f (Stack [a]
xs) = [b] -> Stack b
forall a. [a] -> Stack a
Stack ([b] -> Stack b) -> [b] -> Stack b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f [a]
xs
instance Applicative Stack where
pure :: forall a. a -> Stack a
pure a
x = [a] -> Stack a
forall a. [a] -> Stack a
Stack [a
x]
Stack [a -> b]
fs <*> :: forall a b. Stack (a -> b) -> Stack a -> Stack b
<*> Stack [a]
xs = [b] -> Stack b
forall a. [a] -> Stack a
Stack ([b] -> Stack b) -> [b] -> Stack b
forall a b. (a -> b) -> a -> b
$ [a -> b]
fs [a -> b] -> [a] -> [b]
forall a b. [a -> b] -> [a] -> [b]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [a]
xs
instance Semigroup (Stack a) where
Stack [a]
xs <> :: Stack a -> Stack a -> Stack a
<> Stack [a]
ys = [a] -> Stack a
forall a. [a] -> Stack a
Stack ([a] -> Stack a) -> [a] -> Stack a
forall a b. (a -> b) -> a -> b
$ [a]
xs [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a]
ys
instance Monoid (Stack a) where
mempty :: Stack a
mempty = [a] -> Stack a
forall a. [a] -> Stack a
Stack []
instance Foldable Stack where
foldr :: forall a b. (a -> b -> b) -> b -> Stack a -> b
foldr a -> b -> b
f b
z (Stack [a]
xs) = (a -> b -> b) -> b -> [a] -> b
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
z [a]
xs
mapTop :: (a -> a)
-> Stack a
-> Stack a
mapTop :: forall a. (a -> a) -> Stack a -> Stack a
mapTop a -> a
f (Stack (a
x:[a]
xs)) = [a] -> Stack a
forall a. [a] -> Stack a
Stack (a -> a
f a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)
mapTop a -> a
_ Stack a
s = Stack a
s
push :: a
-> Stack a
-> Stack a
push :: forall a. a -> Stack a -> Stack a
push a
x (Stack [a]
xs) = [a] -> Stack a
forall a. [a] -> Stack a
Stack (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)
pop :: Stack a
-> (a, Stack a)
pop :: forall a. Stack a -> (a, Stack a)
pop (Stack (a
x:[a]
xs)) = (a
x, [a] -> Stack a
forall a. [a] -> Stack a
Stack [a]
xs)
pop Stack a
_ = [Char] -> (a, Stack a)
forall a. HasCallStack => [Char] -> a
error [Char]
"empty stack"
top :: Stack a
-> a
top :: forall a. Stack a -> a
top (Stack (a
x:[a]
_)) = a
x
top Stack a
_ = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"empty stack"
isEmpty :: Stack a
-> Bool
isEmpty :: forall a. Stack a -> Bool
isEmpty (Stack []) = Bool
True
isEmpty Stack a
_ = Bool
False