{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Singletons.Prelude.Foldable (
PFoldable(..), SFoldable(..),
FoldrM, sFoldrM,
FoldlM, sFoldlM,
Traverse_, sTraverse_,
For_, sFor_,
SequenceA_, sSequenceA_,
Asum, sAsum,
MapM_, sMapM_,
ForM_, sForM_,
Sequence_, sSequence_,
Msum, sMsum,
Concat, sConcat,
ConcatMap, sConcatMap,
And, sAnd,
Or, sOr,
Any, sAny,
All, sAll,
MaximumBy, sMaximumBy,
MinimumBy, sMinimumBy,
NotElem, sNotElem,
Find, sFind,
FoldSym0, FoldSym1,
FoldMapSym0, FoldMapSym1, FoldMapSym2,
FoldrSym0, FoldrSym1, FoldrSym2, FoldrSym3,
Foldr'Sym0, Foldr'Sym1, Foldr'Sym2, Foldr'Sym3,
FoldlSym0, FoldlSym1, FoldlSym2, FoldlSym3,
Foldl'Sym0, Foldl'Sym1, Foldl'Sym2, Foldl'Sym3,
Foldr1Sym0, Foldr1Sym1, Foldr1Sym2,
Foldl1Sym0, Foldl1Sym1, Foldl1Sym2,
ToListSym0, ToListSym1,
NullSym0, NullSym1,
LengthSym0, LengthSym1,
ElemSym0, ElemSym1, ElemSym2,
MaximumSym0, MaximumSym1,
MinimumSym0, MinimumSym1,
SumSym0, SumSym1,
ProductSym0, ProductSym1,
FoldrMSym0, FoldrMSym1, FoldrMSym2, FoldrMSym3,
FoldlMSym0, FoldlMSym1, FoldlMSym2, FoldlMSym3,
Traverse_Sym0, Traverse_Sym1, Traverse_Sym2,
For_Sym0, For_Sym1, For_Sym2,
SequenceA_Sym0, SequenceA_Sym1,
AsumSym0, AsumSym1,
MapM_Sym0, MapM_Sym1, MapM_Sym2,
ForM_Sym0, ForM_Sym1, ForM_Sym2,
Sequence_Sym0, Sequence_Sym1,
MsumSym0, MsumSym1,
ConcatSym0, ConcatSym1,
ConcatMapSym0, ConcatMapSym1, ConcatMapSym2,
AndSym0, AndSym1,
OrSym0, OrSym1,
AnySym0, AnySym1, AnySym2,
AllSym0, AllSym1, AllSym2,
MaximumBySym0, MaximumBySym1, MaximumBySym2,
MinimumBySym0, MinimumBySym1, MinimumBySym2,
NotElemSym0, NotElemSym1, NotElemSym2,
FindSym0, FindSym1, FindSym2
) where
import Control.Applicative
import Control.Monad
import Data.Kind
import Data.List.NonEmpty (NonEmpty(..))
import Data.Monoid hiding (All(..), Any(..), Endo(..), Product(..), Sum(..))
import qualified Data.Monoid as Monoid (All(..), Any(..), Product(..), Sum(..))
import Data.Singletons.Internal
import Data.Singletons.Prelude.Base
hiding (Foldr, FoldrSym0, FoldrSym1, FoldrSym2, FoldrSym3, sFoldr)
import Data.Singletons.Prelude.Bool
import Data.Singletons.Prelude.Either
import Data.Singletons.Prelude.Eq
import Data.Singletons.Prelude.Instances
hiding (Foldl, FoldlSym0(..), FoldlSym1(..), FoldlSym2(..), FoldlSym3, sFoldl)
import Data.Singletons.Prelude.List.Internal.Disambiguation
import Data.Singletons.Prelude.Maybe
import Data.Singletons.Prelude.Monad.Internal
import Data.Singletons.Prelude.Monoid
hiding ( AllSym0, AllSym1
, AnySym0, AnySym1
, ProductSym0, ProductSym1
, SumSym0, SumSym1 )
import Data.Singletons.Prelude.Num
import Data.Singletons.Prelude.Ord
hiding ( Max, MaxSym0, MaxSym1, MaxSym2, sMax
, Min, MinSym0, MinSym1, MinSym2, sMin )
import Data.Singletons.Prelude.Semigroup.Internal
hiding ( AllSym0(..), AllSym1, SAll
, AnySym0(..), AnySym1, SAny
, FirstSym0, FirstSym1, SFirst
, LastSym0, LastSym1, SLast
, ProductSym0(..), ProductSym1, SProduct
, SumSym0(..), SumSym1, SSum )
import Data.Singletons.Promote
import Data.Singletons.Single
import Data.Singletons.TypeLits.Internal
newtype Endo a = Endo (a ~> a)
data SEndo :: forall a. Endo a -> Type where
SEndo :: Sing x -> SEndo ('Endo x)
type instance Sing = SEndo
data EndoSym0 :: forall a. (a ~> a) ~> Endo a
type instance Apply EndoSym0 x = 'Endo x
$(singletonsOnly [d|
instance Semigroup (Endo a) where
Endo x <> Endo y = Endo (x . y)
instance Monoid (Endo a) where
mempty = Endo id
|])
newtype MaxInternal a = MaxInternal (Maybe a)
data SMaxInternal :: forall a. MaxInternal a -> Type where
SMaxInternal :: Sing x -> SMaxInternal ('MaxInternal x)
type instance Sing = SMaxInternal
$(genDefunSymbols [''MaxInternal])
newtype MinInternal a = MinInternal (Maybe a)
data SMinInternal :: forall a. MinInternal a -> Type where
SMinInternal :: Sing x -> SMinInternal ('MinInternal x)
type instance Sing = SMinInternal
$(genDefunSymbols [''MinInternal])
$(singletonsOnly [d|
instance Ord a => Semigroup (MaxInternal a) where
m <> MaxInternal Nothing = m
MaxInternal Nothing <> n = n
(MaxInternal m@(Just x)) <> (MaxInternal n@(Just y))
= if x >= y then MaxInternal m else MaxInternal n
instance Ord a => Monoid (MaxInternal a) where
mempty = MaxInternal Nothing
instance Ord a => Semigroup (MinInternal a) where
m <> MinInternal Nothing = m
MinInternal Nothing <> n = n
(MinInternal m@(Just x)) <> (MinInternal n@(Just y))
= if x <= y then MinInternal m else MinInternal n
instance Ord a => Monoid (MinInternal a) where
mempty = MinInternal Nothing
|])
$(