Skip to content

Commit 5f59862

Browse files
authored
Merge pull request #72 from purescript/eq-ord-1
Add `Eq1` and `Ord1` instances, fix `Eq`/`Ord`
2 parents a0658cb + 560d086 commit 5f59862

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

src/Control/Comonad/Cofree.purs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import Control.Extend (class Extend)
1818
import Control.Monad.Free (Free, runFreeM)
1919
import Control.Monad.Rec.Class (class MonadRec)
2020
import Control.Monad.State (State, StateT(..), runState, runStateT, state)
21+
import Data.Eq (class Eq1, eq1)
2122
import Data.Foldable (class Foldable, foldr, foldl, foldMap)
2223
import Data.Lazy (Lazy, force, defer)
24+
import Data.Ord (class Ord1, compare1)
2325
import Data.Traversable (class Traversable, traverse)
2426
import Data.Tuple (Tuple(..))
2527

@@ -104,15 +106,21 @@ exploreM pair m w =
104106
eval :: forall x y. Tuple (x -> y) (Cofree g x) -> y
105107
eval (Tuple f cof) = f (extract cof)
106108

107-
instance eqCofree :: (Eq (f (Cofree f a)), Eq a) => Eq (Cofree f a) where
108-
eq x y = head x == head y && tail x == tail y
109+
instance eqCofree :: (Eq1 f, Eq a) => Eq (Cofree f a) where
110+
eq x y = head x == head y && tail x `eq1` tail y
109111

110-
instance ordCofree :: (Ord (f (Cofree f a)), Ord a) => Ord (Cofree f a) where
112+
instance eq1Cofree :: Eq1 f => Eq1 (Cofree f) where
113+
eq1 = eq
114+
115+
instance ordCofree :: (Ord1 f, Ord a) => Ord (Cofree f a) where
111116
compare x y =
112117
case compare (head x) (head y) of
113-
EQ -> compare (tail x) (tail y)
118+
EQ -> compare1 (tail x) (tail y)
114119
r -> r
115120

121+
instance ord1Cofree :: Ord1 f => Ord1 (Cofree f) where
122+
compare1 = compare
123+
116124
instance functorCofree :: Functor f => Functor (Cofree f) where
117125
map f = loop where
118126
loop fa = Cofree (f (head fa)) (_lift loop (_tail fa))

src/Control/Monad/Free.purs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import Control.Monad.Trans.Class (class MonadTrans)
1717

1818
import Data.CatList (CatList, empty, snoc, uncons)
1919
import Data.Either (Either(..))
20+
import Data.Eq (class Eq1, eq1)
2021
import Data.Foldable (class Foldable, foldMap, foldl, foldr)
2122
import Data.Maybe (Maybe(..))
23+
import Data.Ord (class Ord1, compare1)
2224
import Data.Traversable (class Traversable, traverse)
2325
import Data.Tuple (Tuple(..))
2426

@@ -38,11 +40,24 @@ data FreeView f a b = Return a | Bind (f b) (b -> Free f a)
3840

3941
data Val
4042

41-
instance eqFree :: (Functor f, Eq (f (Free f a)), Eq a) => Eq (Free f a) where
42-
eq x y = resume x == resume y
43+
instance eqFree :: (Functor f, Eq1 f, Eq a) => Eq (Free f a) where
44+
eq x y = case resume x, resume y of
45+
Left fa, Left fb -> eq1 fa fb
46+
Right a, Right b -> a == b
47+
_, _ -> false
4348

44-
instance ordFree :: (Functor f, Ord (f (Free f a)), Ord a) => Ord (Free f a) where
45-
compare x y = compare (resume x) (resume y)
49+
instance eq1Free :: (Functor f, Eq1 f) => Eq1 (Free f) where
50+
eq1 = eq
51+
52+
instance ordFree :: (Functor f, Ord1 f, Ord a) => Ord (Free f a) where
53+
compare x y = case resume x, resume y of
54+
Left fa, Left fb -> compare1 fa fb
55+
Left _, _ -> LT
56+
_, Left _ -> GT
57+
Right a, Right b -> compare a b
58+
59+
instance ord1Free :: (Functor f, Ord1 f, Ord a) => Ord1 (Free f) where
60+
compare1 = compare
4661

4762
instance freeFunctor :: Functor (Free f) where
4863
map k f = pure <<< k =<< f

src/Data/Coyoneda.purs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ module Data.Coyoneda
1010

1111
import Prelude
1212

13-
import Data.Exists (Exists, runExists, mkExists)
14-
1513
import Control.Comonad (class Comonad, extract)
1614
import Control.Extend (class Extend, (<<=))
1715
import Control.Monad.Trans.Class (class MonadTrans)
1816

17+
import Data.Eq (class Eq1, eq1)
18+
import Data.Exists (Exists, runExists, mkExists)
19+
import Data.Ord (class Ord1, compare1)
20+
1921
-- | `Coyoneda` is encoded as an existential type using `Data.Exists`.
2022
-- |
2123
-- | This type constructor encodes the contents of the existential package.
@@ -27,6 +29,18 @@ data CoyonedaF f a i = CoyonedaF (i -> a) (f i)
2729
-- | it is the _free_ `Functor` for `f`.
2830
newtype Coyoneda f a = Coyoneda (Exists (CoyonedaF f a))
2931

32+
instance eqCoyoneda :: (Functor f, Eq1 f, Eq a) => Eq (Coyoneda f a) where
33+
eq x y = lowerCoyoneda x `eq1` lowerCoyoneda y
34+
35+
instance eq1Coyoneda :: (Functor f, Eq1 f) => Eq1 (Coyoneda f) where
36+
eq1 = eq
37+
38+
instance ordCoyoneda :: (Functor f, Ord1 f, Ord a) => Ord (Coyoneda f a) where
39+
compare x y = lowerCoyoneda x `compare1` lowerCoyoneda y
40+
41+
instance ord1Coyoneda :: (Functor f, Ord1 f) => Ord1 (Coyoneda f) where
42+
compare1 = compare
43+
3044
instance functorCoyoneda :: Functor (Coyoneda f) where
3145
map f (Coyoneda e) = runExists (\(CoyonedaF k fi) -> coyoneda (f <<< k) fi) e
3246

src/Data/Yoneda.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ import Control.Comonad (class Comonad, extract)
1212
import Control.Extend (class Extend, (<<=))
1313
import Control.Monad.Trans.Class (class MonadTrans)
1414

15+
import Data.Eq (class Eq1, eq1)
16+
import Data.Ord (class Ord1, compare1)
17+
1518
-- | The Yoneda `Functor`
1619
-- |
1720
-- | `Yoneda f` is a `Functor` for any type constructor `f`.
1821
newtype Yoneda f a = Yoneda (forall b. (a -> b) -> f b)
1922

23+
instance eqYoneda :: (Eq1 f, Eq a) => Eq (Yoneda f a) where
24+
eq x y = lowerYoneda x `eq1` lowerYoneda y
25+
26+
instance eq1Yoneda :: Eq1 f => Eq1 (Yoneda f) where
27+
eq1 = eq
28+
29+
instance ordYoneda :: (Ord1 f, Ord a) => Ord (Yoneda f a) where
30+
compare x y = lowerYoneda x `compare1` lowerYoneda y
31+
32+
instance ord1Yoneda :: Ord1 f => Ord1 (Yoneda f) where
33+
compare1 = compare
34+
2035
instance functorYoneda :: Functor (Yoneda f) where
2136
map f m = Yoneda (\k -> runYoneda m (k <<< f))
2237

0 commit comments

Comments
 (0)