Skip to content

Commit 7356dfd

Browse files
oh yeah zipwith is just fundamentally incompatible with Alternative lmao
1 parent d2fe9c6 commit 7356dfd

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/Data/Unfoldable/Trivial/Internal.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import Data.Filterable
4242
, filterMapDefault
4343
)
4444
import Data.Either (Either(..), either)
45-
import Control.Alternative (class Alt, class Plus, class Alternative, (<|>))
45+
import Control.Alternative (class Alt, class Plus, (<|>))
4646
import Control.Lazy (class Lazy)
4747
import Test.QuickCheck.Arbitrary (class Arbitrary, class Coarbitrary, arbitrary)
4848
import Test.QuickCheck.Gen (sized)
@@ -210,11 +210,13 @@ instance trivialSemigroup :: Semigroup (Trivial a) where
210210
instance trivialMonoid :: Monoid (Trivial a) where
211211
mempty = none
212212

213-
-- | Zipwith; chosen over the `Monad`-compatible nondet choice used for `Array` etc.
213+
-- | Zipwith; chosen over the `Monad`- and `Alternative`-compatible
214+
-- | nondet choice used for `Array` etc.
214215
-- | because that would require effectively forcing one argument and either
215216
-- | re-evaluating it constantly or storing its elements in a real container
216217
-- | at which point please please please just do that without using `Trivial`.
217218
-- | Length is the minimum of the arguments' lengths.
219+
-- | (The violated `Alternative` law is distributivity.)
218220
instance trivialApply :: Apply Trivial where
219221
apply :: forall a c. Trivial (a -> c) -> Trivial a -> Trivial c
220222
apply tg = untrivial (untrivial eApply tg)
@@ -231,8 +233,6 @@ instance trivialApply :: Apply Trivial where
231233
instance trivialApplicative :: Applicative Trivial where
232234
pure a = unfoldr (const $ Just $ a /\ unit) unit
233235

234-
instance trivialAlternative :: Alternative Trivial
235-
236236
-- | Does not and cannot memoize the values being produced to compare.
237237
-- | Please consider using Data.List.Lazy or your strict container of choice
238238
-- | instead if you have any intention of using this for anything else.

test/Main.purs

Lines changed: 18 additions & 5 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, class Alternative, class Alt)
64+
import Control.Alternative ((<|>), guard, empty, class Alternative, class Plus, 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 (/\))
@@ -216,7 +216,7 @@ appendSuite = describe "Semigroup and Alternative" 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))
219+
genericPlusLaws "Trivial" qc (Proxy :: Proxy (Trivial Int))
220220
genericAltLaws "Trivial1" qc (Proxy :: Proxy (Trivial1 Int))
221221
genericAlternativeLaws "MaybeEmpty NonEmptyList" qc (Proxy :: Proxy (MaybeEmpty NEL.NonEmptyList Int))
222222

@@ -234,16 +234,16 @@ genericAltLaws name qc _ = describe ("Alt " <> name <> " laws") do
234234
it "Distributivity: f <$> (x <|> y) ≡ (f <$> x) <|> (f <$> y)" do
235235
qc \(f :: a -> a) (x :: t a) y -> f <$> (x <|> y) === (f <$> x) <|> (f <$> y)
236236

237-
genericAlternativeLaws :: forall t a.
237+
genericPlusLaws :: forall t a.
238238
Eq (t a) =>
239239
Show (t a) =>
240-
Alternative t =>
240+
Plus t =>
241241
Arbitrary a =>
242242
Coarbitrary a =>
243243
Arbitrary (t a) =>
244244
Arbitrary (t (a -> a)) =>
245245
String -> (forall p. Testable p => p -> Aff Unit) -> Proxy (t a) -> Spec Unit
246-
genericAlternativeLaws name qc proxy = do
246+
genericPlusLaws name qc proxy = do
247247
genericAltLaws name qc proxy
248248
describe ("Plus " <> name <> " laws") do
249249
it "Left identity: empty <|> x ≡ x" do
@@ -252,6 +252,19 @@ genericAlternativeLaws name qc proxy = do
252252
qc \(x :: t a) -> x <|> empty === x
253253
it "Annihilation: f <$> empty ≡ empty" do
254254
qc \(f :: a -> a) -> f <$> empty === (empty :: t a)
255+
256+
genericAlternativeLaws :: forall t a.
257+
Eq (t a) =>
258+
Show (t a) =>
259+
Alternative t =>
260+
Arbitrary a =>
261+
Coarbitrary a =>
262+
Arbitrary (t a) =>
263+
Arbitrary (t (a -> a)) =>
264+
String -> (forall p. Testable p => p -> Aff Unit) -> Proxy (t a) -> Spec Unit
265+
genericAlternativeLaws name qc proxy = do
266+
genericAltLaws name qc proxy
267+
genericPlusLaws name qc proxy
255268
describe ("Alternative " <> name <> " laws") do
256269
it "Distributivity: (f <|> g) <*> x ≡ (f <*> x) <|> (g <*> x)" do
257270
qc \(f :: t (a -> a)) g x -> (f <|> g) <*> x === (f <*> x) <|> (g <*> x)

0 commit comments

Comments
 (0)