Move Semigroup-Traversable for [a]/[]

parent 2d72176f
Pipeline #8611 passed with stage
in 1 minute and 31 seconds
......@@ -85,7 +85,6 @@ class Foldable t where
*/
foldl1 :: (a a -> a) !(t a) -> a
instance Foldable []
instance Foldable (Either a)
instance Foldable ((,) a)
......
......@@ -4,7 +4,7 @@ from StdFunc import o, id, flip
from StdMisc import abort
import Control.Applicative
from Control.Monad import class Monad(..), >>=
import qualified Data.List as DL
from Data.List import instance Semigroup [a], instance Monoid [a], instance Foldable []
import Data.Either
import Data.Monoid
import Data.Maybe
......@@ -13,17 +13,6 @@ import StdClass
from StdOverloaded import class < (..)
from StdBool import not
instance Foldable [] where
fold x = foldMap id x
foldMap f x = foldr (mappend o f) mempty x
foldr f x y = 'SL'.foldr f x y
foldr` f z0 xs = foldl f` id xs z0
where f` k x z = k (f x z)
foldl f x y = 'SL'.foldl f x y
foldl` f x y = 'DL'.foldl f x y
foldr1 f x = 'DL'.foldr1 f x
foldl1 f x = 'DL'.foldl1 f x
instance Foldable (Either a) where
foldMap _ (Left _) = mempty
foldMap f (Right y) = f y
......
......@@ -6,10 +6,11 @@ from Control.Monad import liftM, class Monad
import Data.Monoid
from Data.Func import on
from Data.Functor import class Functor (..)
from Data.List import instance Functor [], instance Applicative []
from Data.List import instance Functor [], instance Applicative [],
instance Semigroup [a], instance Monoid [a], instance Foldable [], instance Traversable []
import qualified Data.Traversable as Traversable
from Data.Traversable import class Traversable (..), instance Traversable []
from Data.Foldable import class Foldable (..), instance Foldable []
from Data.Traversable import class Traversable (..)
from Data.Foldable import class Foldable (..)
import qualified Data.Foldable as DF
from StdFunc import o, id, flip
from StdOverloaded import class < (..), class == (..), class + (..), class isEven
......
......@@ -4,6 +4,9 @@ import StdList
from Data.Generics.GenEq import generic gEq
from Data.Functor import class Functor
from Data.Maybe import :: Maybe
from Data.Monoid import class Semigroup, class Monoid
from Data.Foldable import class Foldable
from Data.Traversable import class Traversable
from Control.Applicative import class Applicative, class Alternative
from Control.Monad import class Monad, class MonadPlus
......@@ -13,6 +16,11 @@ instance Alternative []
instance Monad []
instance MonadPlus []
instance Semigroup [a]
instance Monoid [a]
instance Foldable []
instance Traversable []
/**
* An element in the list, or Nothing if it does not exist.
*/
......
......@@ -10,7 +10,12 @@ import StdTuple
import Data.Functor
import Data.Generics.GenEq
import Data.Maybe
import Data.Monoid
from Data.Foldable import class Foldable(foldMap)
from Data.Traversable import class Traversable
import qualified Data.Traversable as T
import Control.Applicative
import Control.Monad
instance Functor []
where
......@@ -33,6 +38,34 @@ where
mzero = []
mplus xs ys = xs ++ ys
instance Semigroup [a]
where
mappend xs ys = xs ++ ys
instance Monoid [a]
where
mempty = []
instance Foldable []
where
fold x = foldMap id x
foldMap f x = foldr (mappend o f) mempty x
foldr f x y = foldr f x y
foldr` f z0 xs = foldl f` id xs z0
where f` k x z = k (f x z)
foldl f x y = foldl f x y
foldl` f x y = foldl f x y
foldr1 f x = foldr1 f x
foldl1 f x = foldl1 f x
instance Traversable []
where
traverse f x = foldr cons_f (pure []) x
where cons_f x ys = (\x xs -> [x:xs]) <$> f x <*> ys
mapM f x = mapM f x
sequenceA f = 'T'.traverse id f
sequence x = 'T'.mapM id x
(!?) infixl 9 :: ![.a] !Int -> Maybe .a
(!?) [x:_] 0 = Just x
(!?) [_:xs] i = xs !? (i-1)
......
......@@ -4,19 +4,20 @@ from StdOverloaded import class +, class *, class zero, class one
from Data.Maybe import :: Maybe
import qualified StdList
class Semigroup a where
mappend :: !a a -> a
class Semigroup a
where
mappend :: !a a -> a
(<++>) infixr 6 :: a a -> a | Semigroup a
(<++>) ma mb :== mappend ma mb
(<++>) infixr 6 :: a a -> a | Semigroup a
(<++>) ma mb :== mappend ma mb
class Monoid a | Semigroup a where
mempty :: a
class Monoid a | Semigroup a
where
mempty :: a
mconcat :: !.[a] -> a | Monoid a
mconcat xs :== 'StdList'.foldr mappend mempty xs
mconcat :: !.[a] -> a | Monoid a
mconcat xs :== 'StdList'.foldr mappend mempty xs
instance Semigroup [a]
instance Semigroup (a -> b) | Semigroup b
instance Semigroup ()
instance Semigroup (a, b) | Semigroup a & Semigroup b
......@@ -24,7 +25,6 @@ instance Semigroup (a, b, c) | Semigroup a & Semigroup b & Semigroup c
instance Semigroup (a, b, c, d) | Semigroup a & Semigroup b & Semigroup c & Semigroup d
instance Semigroup (a, b, c, d, e) | Semigroup a & Semigroup b & Semigroup c & Semigroup d & Semigroup e
instance Monoid [a]
instance Monoid (a -> b) | Monoid b
instance Monoid ()
instance Monoid (a, b) | Monoid a & Monoid b
......
......@@ -6,12 +6,6 @@ from StdFunc import o, id
from Data.Maybe import :: Maybe(..)
from StdList import ++
instance Semigroup [a] where
mappend xs ys = xs ++ ys
instance Monoid [a] where
mempty = []
instance Semigroup (a -> b) | Semigroup b where
mappend f g = \x -> mappend (f x) (g x)
......
......@@ -109,8 +109,6 @@ class Traversable t | Functor t & Foldable t where
// and collect the results.
sequence :: !(t (m a)) -> m (t a) | Monad m
instance Traversable []
instance Traversable (Either a)
instance Traversable ((,) a)
......
......@@ -18,13 +18,6 @@ from Data.Monoid import class Monoid
import Data.Tuple
from StdFunc import o, id, flip
instance Traversable [] where
traverse f x = foldr cons_f (pure []) x
where cons_f x ys = (\x xs -> [x:xs]) <$> f x <*> ys
mapM f x = 'CM'.mapM f x
sequenceA f = traverse id f
sequence x = mapM id x
instance Traversable (Either a) where
traverse _ (Left x) = pure (Left x)
traverse f (Right y) = Right <$> f y
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment