Skip to content

Fix typo in ListT Apply #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ data ListT f a
```


#### `ZipListT`

``` purescript
newtype ZipListT f a
```


#### `nil`

``` purescript
Expand Down Expand Up @@ -205,27 +212,55 @@ zipWith :: forall f a b c. (Monad f) => (a -> b -> c) -> ListT f a -> ListT f b
```


#### `zipList`

``` purescript
zipList :: forall f a. ListT f a -> ZipListT f a
```


#### `semigroupListT`

``` purescript
instance semigroupListT :: (Applicative f) => Semigroup (ListT f a)
```


#### `semigroupZipListT`

``` purescript
instance semigroupZipListT :: (Applicative f) => Semigroup (ZipListT f a)
```


#### `monoidListT`

``` purescript
instance monoidListT :: (Applicative f) => Monoid (ListT f a)
```


#### `monoidZipListT`

``` purescript
instance monoidZipListT :: (Applicative f) => Monoid (ZipListT f a)
```


#### `functorListT`

``` purescript
instance functorListT :: (Functor f) => Functor (ListT f)
```


#### `functorZipListT`

``` purescript
instance functorZipListT :: (Functor f) => Functor (ZipListT f)
```


#### `unfoldableListT`

``` purescript
Expand All @@ -240,13 +275,27 @@ instance applyListT :: (Monad f) => Apply (ListT f)
```


#### `applyZipListT`

``` purescript
instance applyZipListT :: (Monad f) => Apply (ZipListT f)
```


#### `applicativeListT`

``` purescript
instance applicativeListT :: (Monad f) => Applicative (ListT f)
```


#### `applicativeZipListT`

``` purescript
instance applicativeZipListT :: (Monad f) => Applicative (ZipListT f)
```


#### `bindListT`

``` purescript
Expand Down Expand Up @@ -275,20 +324,41 @@ instance altListT :: (Applicative f) => Alt (ListT f)
```


#### `altZipListT`

``` purescript
instance altZipListT :: (Applicative f) => Alt (ZipListT f)
```


#### `plusListT`

``` purescript
instance plusListT :: (Monad f) => Plus (ListT f)
```


#### `plusZipListT`

``` purescript
instance plusZipListT :: (Monad f) => Plus (ZipListT f)
```


#### `alternativeListT`

``` purescript
instance alternativeListT :: (Monad f) => Alternative (ListT f)
```


#### `alternativeZipListT`

``` purescript
instance alternativeZipListT :: (Monad f) => Alternative (ZipListT f)
```


#### `monadPlusListT`

``` purescript
Expand Down
36 changes: 33 additions & 3 deletions src/Control/Monad/ListT.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Control.Monad.ListT
( ListT()
, ZipListT()
, catMaybes
, cons'
, drop
Expand Down Expand Up @@ -28,6 +29,7 @@ module Control.Monad.ListT
, wrapLazy
, zipWith
, zipWith'
, zipList
) where

import Data.Lazy
Expand All @@ -45,7 +47,9 @@ module Control.Monad.ListT
import Control.Monad.Trans

data ListT f a = ListT (f (Step a (ListT f a)))


newtype ZipListT f a = ZipListT (ListT f a)

data Step a s =
Yield a (Lazy s) |
Skip (Lazy s) |
Expand Down Expand Up @@ -195,33 +199,51 @@ module Control.Monad.ListT
zipWith f = zipWith' g where
g a b = pure $ f a b

zipList :: forall f a. ListT f a -> ZipListT f a
zipList = ZipListT

instance semigroupListT :: (Applicative f) => Semigroup (ListT f a) where
(<>) = concat

instance semigroupZipListT :: (Applicative f) => Semigroup (ZipListT f a) where
(<>) (ZipListT a) (ZipListT b) = ZipListT $ a <> b

instance monoidListT :: (Applicative f) => Monoid (ListT f a) where
mempty = nil

instance monoidZipListT :: (Applicative f) => Monoid (ZipListT f a) where
mempty = ZipListT mempty

instance functorListT :: (Functor f) => Functor (ListT f) where
(<$>) f = stepMap g where
g (Yield a s) = Yield (f a) ((<$>) f <$> s)
g (Skip s) = Skip ((<$>) f <$> s)
g Done = Done

instance functorZipListT :: (Functor f) => Functor (ZipListT f) where
(<$>) f (ZipListT a) = ZipListT $ f <$> a

instance unfoldableListT :: (Monad f) => Unfoldable (ListT f) where
-- unfoldr :: forall a b. (b -> Maybe (Tuple a b)) -> b -> ListT f a
unfoldr f b = go (f b)
where go Nothing = nil
go (Just (Tuple a b)) = cons' (pure a) (defer \_ -> (go (f b)))

instance applyListT :: (Monad f) => Apply (ListT f) where
instance applyListT :: (Monad f) => Apply (ListT f) where
(<*>) f x = do
f' <- f
x' <- x
return (f x)
return (f' x')

instance applyZipListT :: (Monad f) => Apply (ZipListT f) where
(<*>) (ZipListT a) (ZipListT b) = ZipListT $ zipWith g a b where g f x = f x

instance applicativeListT :: (Monad f) => Applicative (ListT f) where
pure = singleton

instance applicativeZipListT :: (Monad f) => Applicative (ZipListT f) where
pure = ZipListT <<< pure

instance bindListT :: (Monad f) => Bind (ListT f) where
(>>=) fa f = stepMap g fa where
g (Yield a s) = Skip (h <$> s) where h s = f a `concat` (s >>= f) -- FIXME compiler bug with overlapping instances?
Expand All @@ -236,9 +258,17 @@ module Control.Monad.ListT
instance altListT :: (Applicative f) => Alt (ListT f) where
(<|>) = concat

instance altZipListT :: (Applicative f) => Alt (ZipListT f) where
(<|>) (ZipListT a) (ZipListT b) = ZipListT $ a <|> b

instance plusListT :: (Monad f) => Plus (ListT f) where
empty = nil

instance plusZipListT :: (Monad f) => Plus (ZipListT f) where
empty = ZipListT empty

instance alternativeListT :: (Monad f) => Alternative (ListT f)

instance alternativeZipListT :: (Monad f) => Alternative (ZipListT f)

instance monadPlusListT :: (Monad f) => MonadPlus (ListT f)