-- |
-- Module: Data.Greskell.NonEmptyLike
-- Description: Class of non-empty containers
-- Maintainer: Toshio Ito <debug.ito@gmail.com>
--
-- @since 1.0.0.0
module Data.Greskell.NonEmptyLike
  ( NonEmptyLike(..)
  ) where

import qualified Data.Foldable as F
import Data.List.NonEmpty (NonEmpty(..))
import Data.Semigroup (Semigroup, (<>))
import qualified Data.Semigroup as S

-- | Non-empty containers. Its cardinality is one or more.
--
-- @since 1.0.0.0
class F.Foldable t => NonEmptyLike t where
  -- | Make a container with a single value.
  singleton :: a -> t a
  -- | Append two containers.
  append :: t a -> t a -> t a
  -- | Convert the container to 'NonEmpty' list.
  toNonEmpty :: t a -> NonEmpty a

-- | 'append' is '<>' from 'Semigroup'.
instance NonEmptyLike NonEmpty where
  singleton :: a -> NonEmpty a
singleton a
a = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []
  append :: NonEmpty a -> NonEmpty a -> NonEmpty a
append = NonEmpty a -> NonEmpty a -> NonEmpty a
forall a. Semigroup a => a -> a -> a
(<>)
  toNonEmpty :: NonEmpty a -> NonEmpty a
toNonEmpty = NonEmpty a -> NonEmpty a
forall a. a -> a
id
  
-- | 'append' is '<>' from 'Semigroup'.
instance NonEmptyLike S.First where
  singleton :: a -> First a
singleton = a -> First a
forall a. a -> First a
S.First
  append :: First a -> First a -> First a
append = First a -> First a -> First a
forall a. Semigroup a => a -> a -> a
(<>)
  toNonEmpty :: First a -> NonEmpty a
toNonEmpty (S.First a
a) = a -> NonEmpty a
forall (t :: * -> *) a. NonEmptyLike t => a -> t a
singleton a
a

-- | 'append' is '<>' from 'Semigroup'.
instance NonEmptyLike S.Last where
  singleton :: a -> Last a
singleton = a -> Last a
forall a. a -> Last a
S.Last
  append :: Last a -> Last a -> Last a
append = Last a -> Last a -> Last a
forall a. Semigroup a => a -> a -> a
(<>)
  toNonEmpty :: Last a -> NonEmpty a
toNonEmpty (S.Last a
a) = a -> NonEmpty a
forall (t :: * -> *) a. NonEmptyLike t => a -> t a
singleton a
a