Skip to content

Commit

Permalink
Promote and single Foldable, Traversable, Const, and Identity (#337)
Browse files Browse the repository at this point in the history
* Promote and single Foldable, Traversable, Const, and Identity

This completes the `Data.Singletons.Prelude.*` changes necessary to
support `Monad` and friends. This:

* Adds the appropriate modules for the `Foldable` and `Traversable`
  classes, as well as the `Identity` and `Const` newtypes.
* Uncomments various things in
  `Data.Singletons.Prelude.{Applicative,Monad}` that couldn't be
  supported before due to the lack of the classes in the previous
  bullet points.
* Reexports the appropriate `Foldable`/`Traversable` functions from
  `Data.{Promotion,Singletons}.Prelude.List` to mirror what
  `Data.List` does. This required some reorganization of internal
  modules, but was otherwise straightforward.

Checks off the second bullet point in #184.

* Use list comprehensions in a couple more places
  • Loading branch information
RyanGlScott authored Jun 14, 2018
1 parent bd71aed commit 56e4676
Show file tree
Hide file tree
Showing 28 changed files with 2,255 additions and 646 deletions.
14 changes: 11 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,17 @@ Changelog for singletons project
`(%<>)`, as they are superseded by the corresponding methods from
`PSemigroup` and `SSemigroup`.

* Add promoted and singled versions of the `Functor`, `Applicative`,
`Alternative`, `Monad`, `MonadPlus`, and `MonadZip`. This grants the ability
to promote or single `do`-notation and list comprehensions.
* Add promoted and singled versions of the `Functor`, `Foldable`,
`Traversable`, `Applicative`, `Alternative`, `Monad`, `MonadPlus`, and
`MonadZip` classes. Among other things, this grants the ability to promote
or single `do`-notation and list comprehensions.
* `Data.{Promotion,Singletons}.Prelude.List` now reexports more general
`Foldable`/`Traversable` functions wherever possible, just as `Data.List`
does.

* Add `Data.{Promotion,Singletons}.Prelude.{Const,Identity}`, which define
promoted and singled version of the `Const` and `Identity` data types,
respectively.

* Promote and single the `Down` newtype in `Data.Singletons.Prelude.Ord`.

Expand Down
11 changes: 10 additions & 1 deletion singletons.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ library
Data.Singletons.Prelude.Applicative
Data.Singletons.Prelude.Base
Data.Singletons.Prelude.Bool
Data.Singletons.Prelude.Const
Data.Singletons.Prelude.Either
Data.Singletons.Prelude.Enum
Data.Singletons.Prelude.Eq
Data.Singletons.Prelude.Foldable
Data.Singletons.Prelude.Function
Data.Singletons.Prelude.Functor
Data.Singletons.Prelude.IsString
Data.Singletons.Prelude.Identity
Data.Singletons.Prelude.Ord
Data.Singletons.Prelude.List
Data.Singletons.Prelude.List.NonEmpty
Expand All @@ -93,17 +96,21 @@ library
Data.Singletons.Prelude.Num
Data.Singletons.Prelude.Semigroup
Data.Singletons.Prelude.Show
Data.Singletons.Prelude.Traversable
Data.Singletons.Prelude.Tuple
Data.Singletons.Prelude.Void
Data.Promotion.Prelude
Data.Promotion.TH
Data.Promotion.Prelude.Applicative
Data.Promotion.Prelude.Base
Data.Promotion.Prelude.Bool
Data.Promotion.Prelude.Const
Data.Promotion.Prelude.Either
Data.Promotion.Prelude.Eq
Data.Promotion.Prelude.Foldable
Data.Promotion.Prelude.Function
Data.Promotion.Prelude.Functor
Data.Promotion.Prelude.Identity
Data.Promotion.Prelude.IsString
Data.Promotion.Prelude.Ord
Data.Promotion.Prelude.Enum
Expand All @@ -116,6 +123,7 @@ library
Data.Promotion.Prelude.Num
Data.Promotion.Prelude.Semigroup
Data.Promotion.Prelude.Show
Data.Promotion.Prelude.Traversable
Data.Promotion.Prelude.Tuple
Data.Promotion.Prelude.Void
Data.Singletons.TypeError
Expand All @@ -132,7 +140,8 @@ library
Data.Singletons.Deriving.Show
Data.Singletons.Deriving.Util
Data.Singletons.Internal
Data.Singletons.Prelude.List.NonEmpty.Internal
Data.Singletons.Prelude.List.Internal
Data.Singletons.Prelude.List.Internal.Disambiguation
Data.Singletons.Prelude.Monad.Internal
Data.Singletons.Prelude.Semigroup.Internal
Data.Singletons.Promote
Expand Down
43 changes: 28 additions & 15 deletions src/Data/Promotion/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ module Data.Promotion.Prelude (
PFunctor(Fmap, type (<$)),
PApplicative(Pure, type (<*>), type (*>), type (<*)),
PMonad(type (>>=), type (>>), Return, Fail),
-- MapM_,
type (=<<),
MapM_, Sequence_, type (=<<),

-- * Promoted 'Foldable' and 'Traversable'
PFoldable(Elem, FoldMap, Foldr, Foldl, Foldr1, Foldl1,
Maximum, Minimum, Product, Sum),
PTraversable(Traverse, SequenceA, MapM, Sequence),

-- ** Miscellaneous functions
Id, Const, (:.), type ($), type ($!), Flip, AsTypeOf, Until, Seq,
Expand All @@ -63,13 +67,9 @@ module Data.Promotion.Prelude (
Map, type (++), Filter,
Head, Last, Tail, Init, Null, Length, type (!!),
Reverse,
-- ** Reducing lists (folds)
Foldl, Foldl1, Foldr, Foldr1,
-- *** Special folds
And, Or, Any, All,
Sum, Product,
Concat, ConcatMap,
Maximum, Minimum,
-- ** Building lists
-- *** Scans
Scanl, Scanl1, Scanr, Scanr1,
Expand All @@ -80,7 +80,7 @@ module Data.Promotion.Prelude (
TakeWhile, DropWhile, Span, Break,

-- ** Searching lists
Elem, NotElem, Lookup,
NotElem, Lookup,
-- ** Zipping and unzipping lists
Zip, Zip3, ZipWith, ZipWith3, Unzip, Unzip3,
-- ** Functions on 'Symbol's
Expand Down Expand Up @@ -148,8 +148,26 @@ module Data.Promotion.Prelude (
type (>>=@#@$), type (>>=@#@$$), type (>>=@#@$$$),
type (>>@#@$), type (>>@#@$$), type (>>@#@$$$),
ReturnSym0, ReturnSym1, FailSym0, FailSym1,
MapM_Sym0, MapM_Sym1, MapM_Sym2,
Sequence_Sym0, Sequence_Sym1,
type (=<<@#@$), type (=<<@#@$$), type (=<<@#@$$$),

ElemSym0, ElemSym1, ElemSym2,
FoldMapSym0, FoldMapSym1, FoldMapSym2,
FoldrSym0, FoldrSym1, FoldrSym2, FoldrSym3,
FoldlSym0, FoldlSym1, FoldlSym2, FoldlSym3,
Foldr1Sym0, Foldr1Sym1, Foldr1Sym2,
Foldl1Sym0, Foldl1Sym1, Foldl1Sym2,
MaximumSym0, MaximumSym1,
MinimumSym0, MinimumSym1,
SumSym0, SumSym1,
ProductSym0, ProductSym1,

TraverseSym0, TraverseSym1, TraverseSym2,
SequenceASym0, SequenceASym1,
MapMSym0, MapMSym1, MapMSym2,
SequenceSym0, SequenceSym1,

IdSym0, IdSym1, ConstSym0, ConstSym1, ConstSym2,
type (.@#@$), type (.@#@$$), type (.@#@$$$),
type ($@#@$), type ($@#@$$), type ($@#@$$$),
Expand All @@ -162,11 +180,6 @@ module Data.Promotion.Prelude (
type (++@#@$$), type (++@#@$), HeadSym0, HeadSym1, LastSym0, LastSym1,
TailSym0, TailSym1, InitSym0, InitSym1, NullSym0, NullSym1,

FoldlSym0, FoldlSym1, FoldlSym2, FoldlSym3,
Foldl1Sym0, Foldl1Sym1, Foldl1Sym2,
FoldrSym0, FoldrSym1, FoldrSym2, FoldrSym3,
Foldr1Sym0, Foldr1Sym1, Foldr1Sym2,

ConcatSym0, ConcatSym1,
ConcatMapSym0, ConcatMapSym1, ConcatMapSym2,
MaximumBySym0, MaximumBySym1, MaximumBySym2,
Expand All @@ -180,7 +193,6 @@ module Data.Promotion.Prelude (
ScanrSym0, ScanrSym1, ScanrSym2, ScanrSym3,
Scanr1Sym0, Scanr1Sym1, Scanr1Sym2,

ElemSym0, ElemSym1, ElemSym2,
NotElemSym0, NotElemSym1, NotElemSym2,

ZipSym0, ZipSym1, ZipSym2,
Expand All @@ -193,8 +205,6 @@ module Data.Promotion.Prelude (

UntilSym0, UntilSym1, UntilSym2, UntilSym3,
LengthSym0, LengthSym1,
SumSym0, SumSym1,
ProductSym0, ProductSym1,
ReplicateSym0, ReplicateSym1, ReplicateSym2,
TakeSym0, TakeSym1, TakeSym2,
DropSym0, DropSym1, DropSym2,
Expand All @@ -210,8 +220,10 @@ module Data.Promotion.Prelude (

import Data.Promotion.Prelude.Applicative
import Data.Promotion.Prelude.Base
hiding (Foldr, FoldrSym0, FoldrSym1, FoldrSym2, FoldrSym3)
import Data.Promotion.Prelude.Bool
import Data.Promotion.Prelude.Either
import Data.Promotion.Prelude.Foldable
import Data.Promotion.Prelude.Functor
import Data.Promotion.Prelude.List
import Data.Promotion.Prelude.Maybe
Expand All @@ -228,4 +240,5 @@ import Data.Promotion.Prelude.Num
import Data.Promotion.Prelude.Semigroup
( PSemigroup(..), type (<>@#@$), type (<>@#@$$), type (<>@#@$$$) )
import Data.Promotion.Prelude.Show
import Data.Promotion.Prelude.Traversable
import Data.Singletons.TypeLits
20 changes: 20 additions & 0 deletions src/Data/Promotion/Prelude/Const.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----------------------------------------------------------------------------
-- |
-- Module : Data.Promotion.Prelude.Const
-- Copyright : (C) 2018 Ryan Scott
-- License : BSD-style (see LICENSE)
-- Maintainer : Richard Eisenberg ([email protected])
-- Stability : experimental
-- Portability : non-portable
--
-- Exports defunctionalization symbosl for the 'Const' data type.
--
-----------------------------------------------------------------------------

module Data.Promotion.Prelude.Const (
-- * Defunctionalization symbols
ConstSym0, ConstSym1,
GetConstSym0, GetConstSym1
) where

import Data.Singletons.Prelude.Const
67 changes: 67 additions & 0 deletions src/Data/Promotion/Prelude/Foldable.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-----------------------------------------------------------------------------
-- |
-- Module : Data.Promotion.Prelude.Foldable
-- Copyright : (C) 2018 Ryan Scott
-- License : BSD-style (see LICENSE)
-- Maintainer : Ryan Scott
-- Stability : experimental
-- Portability : non-portable
--
-- Defines the promoted version of the 'Foldable' type class.
--
----------------------------------------------------------------------------

module Data.Promotion.Prelude.Foldable (
PFoldable(..),

FoldrM, FoldlM,
Traverse_, For_, SequenceA_, Asum,
MapM_, ForM_, Sequence_, Msum,
Concat, ConcatMap, And, Or, Any, All, MaximumBy, MinimumBy,
NotElem, Find,

-- * Defunctionalization symbols
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 Data.Singletons.Prelude.Foldable
20 changes: 20 additions & 0 deletions src/Data/Promotion/Prelude/Identity.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----------------------------------------------------------------------------
-- |
-- Module : Data.Promotion.Prelude.Identity
-- Copyright : (C) 2018 Ryan Scott
-- License : BSD-style (see LICENSE)
-- Maintainer : Richard Eisenberg ([email protected])
-- Stability : experimental
-- Portability : non-portable
--
-- Exports the defunctionalization symbols for the 'Identity' data type.
--
-----------------------------------------------------------------------------

module Data.Promotion.Prelude.Identity (
-- * Defunctionalization symbols
IdentitySym0, IdentitySym1,
RunIdentitySym0, RunIdentitySym1
) where

import Data.Singletons.Prelude.Identity
3 changes: 1 addition & 2 deletions src/Data/Promotion/Prelude/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,10 @@ module Data.Promotion.Prelude.List (

) where

import Data.Singletons.Prelude.Base
import Data.Singletons.Prelude.Eq
import Data.Singletons.Prelude.List
import Data.Singletons.Prelude.Maybe
import Data.Singletons.TH
import Data.Singletons.TH (promoteOnly)

$(promoteOnly [d|

Expand Down
32 changes: 32 additions & 0 deletions src/Data/Promotion/Prelude/Traversable.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-----------------------------------------------------------------------------
-- |
-- Module : Data.Promotion.Prelude.Traversable
-- Copyright : (C) 2018 Ryan Scott
-- License : BSD-style (see LICENSE)
-- Maintainer : Ryan Scott
-- Stability : experimental
-- Portability : non-portable
--
-- Defines the promoted version of the 'Traversable' type class.
--
----------------------------------------------------------------------------

module Data.Promotion.Prelude.Traversable (
PTraversable(..),
For, ForM, MapAccumL, MapAccumR, FmapDefault, FoldMapDefault,

-- * Defunctionalization symbols
TraverseSym0, TraverseSym1, TraverseSym2,
SequenceASym0, SequenceASym1,
MapMSym0, MapMSym1, MapMSym2,
SequenceSym0, SequenceSym1,

ForSym0, ForSym1, ForSym2,
ForMSym0, ForMSym1, ForMSym2,
MapAccumLSym0, MapAccumLSym1, MapAccumLSym2, MapAccumLSym3,
MapAccumRSym0, MapAccumRSym1, MapAccumRSym2, MapAccumRSym3,
FmapDefaultSym0, FmapDefaultSym1, FmapDefaultSym2,
FoldMapDefaultSym0, FoldMapDefaultSym1, FoldMapDefaultSym2
) where

import Data.Singletons.Prelude.Traversable
Loading

0 comments on commit 56e4676

Please sign in to comment.