[GHC] #13573: Add Foldable1 to base
GHC
ghc-devs at haskell.org
Wed Apr 26 10:06:48 UTC 2017
#13573: Add Foldable1 to base
-------------------------------------+-------------------------------------
Reporter: Iceland_jack | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone:
Component: Compiler | Version: 8.0.1
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: None/Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: #10365 | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Description changed by Iceland_jack:
@@ -24,1 +24,3 @@
- minimum1 :: (Ord a, Foldable1 f) => f a -> a
+ import qualified Data.Semigroup as S
+
+ minimum1, maximum1 :: (Ord a, Foldable1 f) => f a -> a
@@ -26,2 +28,0 @@
-
- maximum1 :: (Ord a, Foldable1 f) => f a -> a
@@ -30,1 +30,1 @@
- head1 :: Foldable1 f => f a -> a
+ head1, last1 :: Foldable1 f => f a -> a
@@ -32,0 +32,1 @@
+ last1 = S.getLast . foldMap1 S.Last
@@ -33,4 +34,1 @@
- last1 :: Foldable1 f => f a -> a
- last1 = S.getLast . foldMap1 S.Last
-
- foldr1 :: Foldable1 f => (a -> a -> a) -> (f a -> a)
+ foldr1, foldl1 :: Foldable1 f => (a -> a -> a) -> (f a -> a)
@@ -38,2 +36,0 @@
-
- foldl1 :: Foldable1 f => (a -> a -> a) -> (f a -> a)
New description:
This is a proposal to add
[https://hackage.haskell.org/package/semigroupoids-5.1/docs/Data-
Semigroup-Foldable.html Foldable1] (non-empty `Foldable`) to base
{{{#!hs
-- Data.Semigroup.Foldable
class Foldable f => Foldable1 f where
fold1 :: Semigroup m => f m -> m
foldMap1 :: Semigroup m => (a -> m) -> f a -> m
-- Possible methods
head1 :: f a -> a
last1 :: f a -> a
toNonEmpty :: f a -> NonEmpty a
}}}
along with instances and function that are only valid for non-empty
structures (details: [https://github.com/ekmett/semigroupoids/issues/49
semigroupoids issue #49],
[https://github.com/arkeet/difference/blob/0f0e14f51cf1ecd7ebf2d8c52204bd91ae3b2969/src/Data/Semigroup/Difference.hs
github])
{{{#!hs
import qualified Data.Semigroup as S
minimum1, maximum1 :: (Ord a, Foldable1 f) => f a -> a
minimum1 = S.getMin . foldMap1 S.Min
maximum1 = S.getMax . foldMap1 S.Max
head1, last1 :: Foldable1 f => f a -> a
head1 = S.getFirst . foldMap1 S.First
last1 = S.getLast . foldMap1 S.Last
foldr1, foldl1 :: Foldable1 f => (a -> a -> a) -> (f a -> a)
foldr1 f = unimprove . foldMap1 (\a -> Diff (f a) a)
foldl1 f = unimprove . getDual . foldMap1 (\a -> Dual $ Diff (flip f a) a)
}}}
Adding `foldM`, `foldM_`, `foldrM` and `foldlM` for non-empty structures
is also a possibility.
----
Currently these are partial functions in `Foldable`. This proposal does
'''not''' propose replacing partial `Foldable` functions.
----
I wanted to test the waters before submitting it to the libraries mailing
list. This may be controversial but it gives us a path to avoid partial
functions in `Foldable`.
--
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13573#comment:9>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list