{-# LANGUAGE RankNTypes #-}
module Fresnel.Semigroup.Cons1
( -- * Non-empty cons lists
  Cons1(..)
  -- * Construction
, singleton
, cons
) where

import Data.Foldable (toList)
import Data.Foldable1

-- Non-empty cons lists

newtype Cons1 a = Cons1 { forall a. Cons1 a -> forall r. (a -> r) -> (a -> r -> r) -> r
runCons1 :: forall r . (a -> r) -> (a -> r -> r) -> r }

instance Show a => Show (Cons1 a) where
  showsPrec :: Int -> Cons1 a -> ShowS
showsPrec Int
_ = [a] -> ShowS
forall a. Show a => [a] -> ShowS
showList ([a] -> ShowS) -> (Cons1 a -> [a]) -> Cons1 a -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cons1 a -> [a]
forall a. Cons1 a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

instance Semigroup (Cons1 a) where
  Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
a1 <> :: Cons1 a -> Cons1 a -> Cons1 a
<> Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
a2 = (forall r. (a -> r) -> (a -> r -> r) -> r) -> Cons1 a
forall a. (forall r. (a -> r) -> (a -> r -> r) -> r) -> Cons1 a
Cons1 (\ a -> r
f a -> r -> r
g -> (a -> r) -> (a -> r -> r) -> r
forall r. (a -> r) -> (a -> r -> r) -> r
a1 (\ a
a -> a -> r -> r
g a
a ((a -> r) -> (a -> r -> r) -> r
forall r. (a -> r) -> (a -> r -> r) -> r
a2 a -> r
f a -> r -> r
g)) a -> r -> r
g)

instance Foldable Cons1 where
  foldMap :: forall m a. Monoid m => (a -> m) -> Cons1 a -> m
foldMap a -> m
f (Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
r) = (a -> m) -> (a -> m -> m) -> m
forall r. (a -> r) -> (a -> r -> r) -> r
r a -> m
f (m -> m -> m
forall a. Semigroup a => a -> a -> a
(<>) (m -> m -> m) -> (a -> m) -> a -> m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f)
  foldr :: forall a b. (a -> b -> b) -> b -> Cons1 a -> b
foldr a -> b -> b
f b
z (Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
r) = (a -> b) -> (a -> b -> b) -> b
forall r. (a -> r) -> (a -> r -> r) -> r
r (a -> b -> b
`f` b
z) a -> b -> b
f

instance Foldable1 Cons1 where
  foldMap1 :: forall m a. Semigroup m => (a -> m) -> Cons1 a -> m
foldMap1 a -> m
f (Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
r) = (a -> m) -> (a -> m -> m) -> m
forall r. (a -> r) -> (a -> r -> r) -> r
r a -> m
f (m -> m -> m
forall a. Semigroup a => a -> a -> a
(<>) (m -> m -> m) -> (a -> m) -> a -> m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f)
  foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Cons1 a -> b
foldrMap1 a -> b
f a -> b -> b
g (Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
r) = (a -> b) -> (a -> b -> b) -> b
forall r. (a -> r) -> (a -> r -> r) -> r
r a -> b
f a -> b -> b
g

instance Functor Cons1 where
  fmap :: forall a b. (a -> b) -> Cons1 a -> Cons1 b
fmap a -> b
h (Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
r) = (forall r. (b -> r) -> (b -> r -> r) -> r) -> Cons1 b
forall a. (forall r. (a -> r) -> (a -> r -> r) -> r) -> Cons1 a
Cons1 (\ b -> r
f b -> r -> r
g -> (a -> r) -> (a -> r -> r) -> r
forall r. (a -> r) -> (a -> r -> r) -> r
r (b -> r
f (b -> r) -> (a -> b) -> a -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
h) (b -> r -> r
g (b -> r -> r) -> (a -> b) -> a -> r -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
h))


-- Construction

singleton :: a -> Cons1 a
singleton :: forall a. a -> Cons1 a
singleton a
a = (forall r. (a -> r) -> (a -> r -> r) -> r) -> Cons1 a
forall a. (forall r. (a -> r) -> (a -> r -> r) -> r) -> Cons1 a
Cons1 (\ a -> r
f a -> r -> r
_ -> a -> r
f a
a)

cons :: a -> Cons1 a -> Cons1 a
cons :: forall a. a -> Cons1 a -> Cons1 a
cons a
a (Cons1 forall r. (a -> r) -> (a -> r -> r) -> r
r) = (forall r. (a -> r) -> (a -> r -> r) -> r) -> Cons1 a
forall a. (forall r. (a -> r) -> (a -> r -> r) -> r) -> Cons1 a
Cons1 (\ a -> r
f a -> r -> r
g -> a -> r -> r
g a
a ((a -> r) -> (a -> r -> r) -> r
forall r. (a -> r) -> (a -> r -> r) -> r
r a -> r
f a -> r -> r
g))