Skip to content

Commit 57e5071

Browse files
committed
Merge pull request #3 from jdegoes/arb-instances
Arb instances
2 parents 6dc5381 + 99cf616 commit 57e5071

File tree

7 files changed

+89
-4
lines changed

7 files changed

+89
-4
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function(grunt) {
1616
src: ["<%=libFiles%>"]
1717
},
1818
tests: {
19-
src: ["tests/Tests.purs", "<%=libFiles%>"]
19+
src: ["tests/**/*.purs", "<%=libFiles%>"]
2020
}
2121
},
2222

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
instance applyListT :: (Monad f) => Apply (ListT f)
1818

19+
instance arbitraryListT :: (Monad f, Arbitrary a) => Arbitrary (ListT f a)
20+
1921
instance bindListT :: (Monad f) => Bind (ListT f)
2022

2123
instance functorListT :: (Functor f) => Functor (ListT f)
@@ -99,6 +101,8 @@
99101

100102
instance applyList :: Apply List
101103

104+
instance arbitraryList :: (Arbitrary a) => Arbitrary (List a)
105+
102106
instance bindList :: Bind List
103107

104108
instance eqList :: (Eq a) => Eq (List a)

src/Control/Monad/ListT.purs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module Control.Monad.ListT
1010
, wrapEffect
1111
, wrapLazy
1212
, unfold
13+
, iterate
1314
, fromArray
15+
, toArray
1416
, take
1517
, takeWhile
1618
, drop
@@ -22,6 +24,7 @@ module Control.Monad.ListT
2224
, head
2325
, tail
2426
, foldl
27+
, foldl'
2528
, scanl
2629
, zipWith'
2730
, zipWith
@@ -35,6 +38,9 @@ module Control.Monad.ListT
3538
import Data.Tuple
3639
import qualified Data.Array as A
3740

41+
import Test.QuickCheck
42+
import Test.QuickCheck.LCG
43+
3844
data ListT f a = ListT (f (Step a (ListT f a)))
3945

4046
data Step a s =
@@ -96,6 +102,9 @@ module Control.Monad.ListT
96102
instance monadTransListT :: MonadTrans ListT where
97103
lift = fromEffect
98104

105+
instance arbitraryListT :: (Monad f, Arbitrary a) => Arbitrary (ListT f a) where
106+
arbitrary = fromArray <$> arbitrary
107+
99108
singleton :: forall f a. (Applicative f) => a -> ListT f a
100109
singleton a = prepend a nil
101110

@@ -112,11 +121,18 @@ module Control.Monad.ListT
112121
unfold f z = ListT $ g <$> f z where
113122
g (Just (Tuple z a)) = Yield a (defer \_ -> (unfold f z))
114123
g Nothing = Done
124+
125+
iterate :: forall f a. (Monad f) => (a -> a) -> a -> ListT f a
126+
iterate f a = unfold g a where
127+
g a = pure $ Just (Tuple (f a) a)
115128

116129
fromArray :: forall f a. (Monad f) => [a] -> ListT f a
117130
fromArray xs = unfold f 0 where
118131
f n = pure $ Tuple (n + 1) <$> (xs A.!! n)
119132

133+
toArray :: forall f a. (Monad f) => ListT f a -> f [a]
134+
toArray = ((<$>) A.reverse) <<< foldl (flip (:)) []
135+
120136
take :: forall f a. (Applicative f) => Number -> ListT f a -> ListT f a
121137
take 0 fa = nil
122138
take n fa = stepMap f fa where
@@ -171,6 +187,12 @@ module Control.Monad.ListT
171187
tail :: forall f a. (Monad f) => ListT f a -> f (Maybe (ListT f a))
172188
tail l = ((<$>) snd) <$> uncons l
173189

190+
foldl' :: forall f a b. (Monad f) => (b -> a -> f b) -> b -> ListT f a -> f b
191+
foldl' f = loop where
192+
loop b l = uncons l >>= g where
193+
g Nothing = pure b
194+
g (Just (Tuple a as)) = (f b a) >>= (flip loop as)
195+
174196
foldl :: forall f a b. (Monad f) => (b -> a -> b) -> b -> ListT f a -> f b
175197
foldl f = loop where
176198
loop b l = uncons l >>= g where

src/Data/List.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ import Control.Plus
4646
import Control.Alternative
4747
import Control.MonadPlus
4848

49+
import Test.QuickCheck
50+
import Test.QuickCheck.LCG
51+
4952
data List a = Nil | Cons a (List a)
5053

5154
instance showList :: (Show a) => Show (List a) where
@@ -130,6 +133,9 @@ instance alternativeList :: Alternative List
130133

131134
instance monadPlusList :: MonadPlus List
132135

136+
instance arbitraryList :: (Arbitrary a) => Arbitrary (List a) where
137+
arbitrary = fromArray <$> arbitrary
138+
133139
fromArray :: forall a. [a] -> List a
134140
fromArray = foldr Cons Nil
135141

tests/Control/Monad/ListT.purs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Tests.Data.ListT (runListTTests) where
2+
3+
import Debug.Trace
4+
5+
import Control.Monad.Identity
6+
7+
import Test.QuickCheck
8+
import Test.QuickCheck.LCG
9+
import Data.Tuple
10+
import Data.Maybe
11+
12+
import Control.Monad.ListT
13+
import qualified Data.Array as A
14+
15+
data ZeroToTen = ZeroToTen Number
16+
17+
runZeroToTen :: ZeroToTen -> Number
18+
runZeroToTen (ZeroToTen n) = n
19+
20+
instance arbZeroToTen :: Arbitrary ZeroToTen where
21+
arbitrary = ZeroToTen <$> chooseInt 0 10
22+
23+
checkFromToArray a =
24+
(runIdentity $ (toArray <<< fromArray) a) == (a :: [Number]) <?> "toArray <<< fromArray == id"
25+
26+
checkTake (Tuple a (ZeroToTen n)) =
27+
(runIdentity $ (toArray <<< (take n) <<< fromArray) a) == A.take n (a :: [Number]) <?> "take"
28+
29+
checkIterate (ZeroToTen n) =
30+
(runIdentity $ head $ iterate ((+) 1) n) == Just n <?> "iterate"
31+
32+
runListTTests = do
33+
trace "Running ListT tests"
34+
35+
quickCheck $ checkFromToArray
36+
37+
quickCheck $ checkTake
38+
39+
quickCheck $ checkIterate

tests/Data/List/List.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Tests.Data.List where
2+
3+
import Debug.Trace
4+
5+
import Test.QuickCheck
6+
import Test.QuickCheck.LCG
7+
8+
import Data.List
9+
10+
runListTests = do
11+
trace "Running List tests"

tests/Tests.purs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ module Main where
22

33
import Debug.Trace
44

5-
import Data.List
6-
75
import Test.QuickCheck
6+
import Test.QuickCheck.LCG
7+
8+
import Tests.Data.List
9+
import Tests.Data.ListT
810

911
main = do
10-
trace "Done"
12+
runListTests
13+
runListTTests

0 commit comments

Comments
 (0)