Skip to content

Commit d2fe9c6

Browse files
Testing Alternative laws... and some of those fail too uh ohhhhhhh
1 parent 9397ea8 commit d2fe9c6

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

test/Main.purs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import Data.Unfoldable.MaybeEmpty
6161
)
6262

6363
import Data.Maybe (Maybe(..), isJust, isNothing)
64-
import Control.Alternative ((<|>), guard, empty)
64+
import Control.Alternative ((<|>), guard, empty, class Alternative, class Alt)
6565
import Data.Enum (class Enum, class BoundedEnum, succ, pred, upFrom, downFrom, upFromIncluding, enumFromTo)
6666
import Data.Tuple (snd)
6767
import Data.Tuple.Nested ((/\), type (/\))
@@ -206,7 +206,7 @@ foldSuite = describe "foldl foldr" do
206206
quickCheck \f (x :: Int) -> (foldr1 f ::<+> singleton x) === x
207207

208208
appendSuite :: Spec Unit
209-
appendSuite = describe "appends (incl. Semigroup/Alt)" do
209+
appendSuite = describe "Semigroup and Alternative" do
210210
let qc = quickCheck' 20 :: forall p. Testable p => p -> _
211211
it "Alt Trivial agrees with Alt Array" do
212212
qc \(a :: Trivial Char) b -> arrgh (a <|> b) === arrgh a <|> arrgh b
@@ -216,6 +216,47 @@ appendSuite = describe "appends (incl. Semigroup/Alt)" do
216216
qc \(a :: Trivial1 Char) (b :: Trivial Char) -> arrgh1 (a `append1` b) === arrgh1 a <|> arrgh b
217217
it "append1' agrees with Alt Array" do
218218
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)
219260

220261
applySuite :: Spec Unit
221262
applySuite = describe "Apply and Applicative" do
@@ -243,7 +284,7 @@ genericApplicativeLaws name qc witness = describe ("Applicative " <> name <> " i
243284
qc \(v :: t a) -> (pure identity) <*> v === v
244285
it "Composition: pure (<<<) <*> f <*> g <*> h ≡ f <*> (g <*> h)" do
245286
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
247288
qc \(f :: a -> a) x -> witness ((pure f) <*> (pure x)) === witness (pure (f x))
248289
it "Interchange: u <*> (pure y) ≡ (pure (_ $ y)) <*> u" do
249290
qc \(u :: t (a -> a)) y -> u <*> (pure y) === (pure (_ $ y)) <*> u

0 commit comments

Comments
 (0)