@@ -61,7 +61,7 @@ import Data.Unfoldable.MaybeEmpty
61
61
)
62
62
63
63
import Data.Maybe (Maybe (..), isJust , isNothing )
64
- import Control.Alternative ((<|>), guard , empty )
64
+ import Control.Alternative ((<|>), guard , empty , class Alternative , class Alt )
65
65
import Data.Enum (class Enum , class BoundedEnum , succ , pred , upFrom , downFrom , upFromIncluding , enumFromTo )
66
66
import Data.Tuple (snd )
67
67
import Data.Tuple.Nested ((/\), type (/\))
@@ -206,7 +206,7 @@ foldSuite = describe "foldl foldr" do
206
206
quickCheck \f (x :: Int ) -> (foldr1 f :: <+> singleton x ) === x
207
207
208
208
appendSuite :: Spec Unit
209
- appendSuite = describe " appends (incl. Semigroup/Alt) " do
209
+ appendSuite = describe " Semigroup and Alternative " do
210
210
let qc = quickCheck' 20 :: forall p . Testable p => p -> _
211
211
it " Alt Trivial agrees with Alt Array" do
212
212
qc \(a :: Trivial Char ) b -> arrgh (a <|> b) === arrgh a <|> arrgh b
@@ -216,6 +216,47 @@ appendSuite = describe "appends (incl. Semigroup/Alt)" do
216
216
qc \(a :: Trivial1 Char ) (b :: Trivial Char ) -> arrgh1 (a `append1` b) === arrgh1 a <|> arrgh b
217
217
it " append1' agrees with Alt Array" do
218
218
qc \(a :: Trivial Char ) (b :: Trivial1 Char ) -> arrgh1 (a `append1'` b) === arrgh a <|> arrgh1 b
219
+ genericAlternativeLaws " Trivial" qc (Proxy :: Proxy (Trivial Int ))
220
+ genericAltLaws " Trivial1" qc (Proxy :: Proxy (Trivial1 Int ))
221
+ genericAlternativeLaws " MaybeEmpty NonEmptyList" qc (Proxy :: Proxy (MaybeEmpty NEL.NonEmptyList Int ))
222
+
223
+ genericAltLaws :: forall t a .
224
+ Eq (t a ) =>
225
+ Show (t a ) =>
226
+ Alt t =>
227
+ Arbitrary a =>
228
+ Coarbitrary a =>
229
+ Arbitrary (t a ) =>
230
+ String -> (forall p . Testable p => p -> Aff Unit ) -> Proxy (t a ) -> Spec Unit
231
+ genericAltLaws name qc _ = describe (" Alt " <> name <> " laws" ) do
232
+ it " Associativity: (x <|> y) <|> z ≡ x <|> (y <|> z)" do
233
+ qc \(x :: t a ) y z -> (x <|> y) <|> z === x <|> (y <|> z)
234
+ it " Distributivity: f <$> (x <|> y) ≡ (f <$> x) <|> (f <$> y)" do
235
+ qc \(f :: a -> a ) (x :: t a ) y -> f <$> (x <|> y) === (f <$> x) <|> (f <$> y)
236
+
237
+ genericAlternativeLaws :: forall t a .
238
+ Eq (t a ) =>
239
+ Show (t a ) =>
240
+ Alternative t =>
241
+ Arbitrary a =>
242
+ Coarbitrary a =>
243
+ Arbitrary (t a ) =>
244
+ Arbitrary (t (a -> a )) =>
245
+ String -> (forall p . Testable p => p -> Aff Unit ) -> Proxy (t a ) -> Spec Unit
246
+ genericAlternativeLaws name qc proxy = do
247
+ genericAltLaws name qc proxy
248
+ describe (" Plus " <> name <> " laws" ) do
249
+ it " Left identity: empty <|> x ≡ x" do
250
+ qc \(x :: t a ) -> empty <|> x === x
251
+ it " Right identity: x <|> empty ≡ x" do
252
+ qc \(x :: t a ) -> x <|> empty === x
253
+ it " Annihilation: f <$> empty ≡ empty" do
254
+ qc \(f :: a -> a ) -> f <$> empty === (empty :: t a )
255
+ describe (" Alternative " <> name <> " laws" ) do
256
+ it " Distributivity: (f <|> g) <*> x ≡ (f <*> x) <|> (g <*> x)" do
257
+ qc \(f :: t (a -> a )) g x -> (f <|> g) <*> x === (f <*> x) <|> (g <*> x)
258
+ it " Annihilation: empty <*> f ≡ empty" do
259
+ qc \(f :: t a ) -> empty <*> f === (empty :: t a )
219
260
220
261
applySuite :: Spec Unit
221
262
applySuite = describe " Apply and Applicative" do
@@ -243,7 +284,7 @@ genericApplicativeLaws name qc witness = describe ("Applicative " <> name <> " i
243
284
qc \(v :: t a ) -> (pure identity) <*> v === v
244
285
it " Composition: pure (<<<) <*> f <*> g <*> h ≡ f <*> (g <*> h)" do
245
286
qc \(f :: t (a -> a )) g (h :: t a ) -> pure (<<<) <*> f <*> g <*> h === f <*> (g <*> h)
246
- it (" Homomorphism: (pure f) <*> (pure x) ≡ pure (f x) -- checked up to a limit because pure :: " <> name <> " a is infinite " ) do
287
+ it (" Homomorphism: (pure f) <*> (pure x) ≡ pure (f x) -- checked up to a limit if pure :: " <> name <> " a is infinite " ) do
247
288
qc \(f :: a -> a ) x -> witness ((pure f) <*> (pure x)) === witness (pure (f x))
248
289
it " Interchange: u <*> (pure y) ≡ (pure (_ $ y)) <*> u" do
249
290
qc \(u :: t (a -> a )) y -> u <*> (pure y) === (pure (_ $ y)) <*> u
0 commit comments