Commit 2d72176f authored by Camil Staps's avatar Camil Staps 🐧

Move Semigroup-Traversable for Maybe

parent 31e30a04
......@@ -85,7 +85,6 @@ class Foldable t where
*/
foldl1 :: (a a -> a) !(t a) -> a
instance Foldable Maybe
instance Foldable []
instance Foldable (Either a)
instance Foldable ((,) a)
......
......@@ -13,29 +13,6 @@ import StdClass
from StdOverloaded import class < (..)
from StdBool import not
instance Foldable Maybe where
fold x = foldMap id x
foldMap f x = foldr (mappend o f) mempty x
foldr _ z Nothing = z
foldr f z (Just x) = f x z
foldr` f z0 xs = foldl f` id xs z0
where f` k x z = k (f x z)
foldl _ z Nothing = z
foldl f z (Just x) = f z x
foldl` f z0 xs = foldr f` id xs z0
where f` x k z = k (f z x)
foldr1 f xs = fromMaybe (abort "foldr1: empty structure")
(foldr mf Nothing xs)
where
mf x Nothing = Just x
mf x (Just y) = Just (f x y)
foldl1 f xs = fromMaybe (abort "foldl1: empty structure")
(foldl mf Nothing xs)
where
mf Nothing y = Just y
mf (Just x) y = Just (f x y)
instance Foldable [] where
fold x = foldMap id x
foldMap f x = foldr (mappend o f) mempty x
......
......@@ -4,6 +4,9 @@ from StdOverloaded import class ==(..)
from Data.Functor import class Functor
from Control.Applicative import class Applicative, class Alternative
from Control.Monad import class Monad, class MonadPlus
from Data.Monoid import class Semigroup, class Monoid
from Data.Foldable import class Foldable
from Data.Traversable import class Traversable
/**
* The Maybe type represents an optional value by providing a constructor
......@@ -22,6 +25,11 @@ instance Alternative Maybe
instance Monad Maybe
instance MonadPlus Maybe
instance Semigroup (Maybe a) | Semigroup a
instance Monoid (Maybe a)
instance Foldable Maybe
instance Traversable Maybe
/**
* Apply a function to the the contents of a Just value and directly return
* the result, or return a default value if the argument is a Nothing value.
......
......@@ -4,6 +4,10 @@ import StdBool
import StdFunc
import StdMisc
import Data.Functor
import Data.Monoid
from Data.Foldable import class Foldable(..)
from Data.Traversable import class Traversable(traverse)
import qualified Data.Traversable as T
import Control.Applicative
import Control.Monad
......@@ -45,6 +49,45 @@ where
mplus Nothing ys = ys
mplus xs _ = xs
instance Semigroup (Maybe a) | Semigroup a
where
mappend Nothing m = m
mappend m Nothing = m
mappend (Just m1) (Just m2) = Just (mappend m1 m2)
instance Monoid (Maybe a)
where
mempty = Nothing
instance Foldable Maybe where
fold x = foldMap id x
foldMap f x = foldr (mappend o f) mempty x
foldr _ z Nothing = z
foldr f z (Just x) = f x z
foldr` f z0 xs = foldl f` id xs z0
where f` k x z = k (f x z)
foldl _ z Nothing = z
foldl f z (Just x) = f z x
foldl` f z0 xs = foldr f` id xs z0
where f` x k z = k (f z x)
foldr1 f xs = fromMaybe (abort "foldr1: empty structure") (foldr mf Nothing xs)
where
mf x Nothing = Just x
mf x (Just y) = Just (f x y)
foldl1 f xs = fromMaybe (abort "foldl1: empty structure") (foldl mf Nothing xs)
where
mf Nothing y = Just y
mf (Just x) y = Just (f x y)
instance Traversable Maybe
where
traverse _ Nothing = pure Nothing
traverse f (Just x) = Just <$> f x
sequenceA f = traverse id f
mapM f x = unwrapMonad (traverse (WrapMonad o f) x)
sequence x = 'T'.mapM id x
maybe :: w:b v:(.a -> w:b) !.(Maybe .a) -> w:b
maybe x _ Nothing = x
maybe _ f (Just x) = f x
......
......@@ -23,7 +23,6 @@ instance Semigroup (a, b) | Semigroup a & Semigroup b
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 Semigroup (Maybe a) | Semigroup a
instance Monoid [a]
instance Monoid (a -> b) | Monoid b
......@@ -32,7 +31,6 @@ instance Monoid (a, b) | Monoid a & Monoid b
instance Monoid (a, b, c) | Monoid a & Monoid b & Monoid c
instance Monoid (a, b, c, d) | Monoid a & Monoid b & Monoid c & Monoid d
instance Monoid (a, b, c, d, e) | Monoid a & Monoid b & Monoid c & Monoid d & Monoid e
instance Monoid (Maybe a)
:: Dual a = Dual a
......
......@@ -48,14 +48,6 @@ instance Semigroup (a, b, c, d, e) | Semigroup a & Semigroup b & Semigroup c & S
instance Monoid (a, b, c, d, e) | Monoid a & Monoid b & Monoid c & Monoid d & Monoid e where
mempty = (mempty, mempty, mempty, mempty, mempty)
instance Semigroup (Maybe a) | Semigroup a where
mappend Nothing m = m
mappend m Nothing = m
mappend (Just m1) (Just m2) = Just (mappend m1 m2)
instance Monoid (Maybe a) where
mempty = Nothing
instance Semigroup (Dual a) | Semigroup a where
mappend (Dual x) (Dual y) = Dual (mappend y x)
......
......@@ -5,7 +5,6 @@ from Control.Monad import class Monad
from Data.Functor import class Functor
from Data.Foldable import class Foldable
from Data.Monoid import class Monoid, class Semigroup
from Data.Maybe import :: Maybe
from Data.Either import :: Either
// Functors representing data structures that can be traversed from left to
......@@ -110,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 Maybe
instance Traversable []
instance Traversable (Either a)
......
......@@ -15,17 +15,9 @@ from Data.Either import instance Functor (Either a)
from Control.Monad import class Monad
import qualified Control.Monad as CM
from Data.Monoid import class Monoid
import Data.Maybe
import Data.Tuple
from StdFunc import o, id, flip
instance Traversable Maybe where
traverse _ Nothing = pure Nothing
traverse f (Just x) = Just <$> f x
sequenceA f = traverse id f
mapM f x = unwrapMonad (traverse (WrapMonad o f) x)
sequence x = mapM id x
instance Traversable [] where
traverse f x = foldr cons_f (pure []) x
where cons_f x ys = (\x xs -> [x:xs]) <$> f x <*> ys
......
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