Skip to content

Commit 03a7b4e

Browse files
committed
Add Eq1 and Ord1 instances, fix Eq/Ord
1 parent 52687d1 commit 03a7b4e

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"purescript-catenable-lists": "^3.0.0",
2828
"purescript-exists": "^2.0.0",
2929
"purescript-inject": "^3.0.0",
30+
"purescript-prelude": "^2.4.0",
3031
"purescript-transformers": "^2.0.0",
3132
"purescript-unsafe-coerce": "^2.0.0"
3233
},

src/Control/Comonad/Cofree.purs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ module Control.Comonad.Cofree
1111
) where
1212

1313
import Prelude
14-
import Control.Monad.Free (Free, runFreeM)
1514
import Control.Alternative (class Alternative, (<|>), empty)
1615
import Control.Comonad (class Comonad, extract)
1716
import Control.Extend (class Extend)
17+
import Control.Monad.Free (Free, runFreeM)
1818
import Control.Monad.State (State, runState, state)
19+
import Data.Eq (class Eq1, eq1)
1920
import Data.Foldable (class Foldable, foldr, foldl, foldMap)
2021
import Data.Lazy (Lazy, force, defer)
22+
import Data.Ord (class Ord1, compare1)
2123
import Data.Traversable (class Traversable, traverse)
2224
import Data.Tuple (Tuple(..))
2325

@@ -83,15 +85,21 @@ explore pair m w =
8385
step :: f (Free f (a -> b)) -> State (Cofree g a) (Free f (a -> b))
8486
step ff = state \cof -> pair (map Tuple ff) (tail cof)
8587

86-
instance eqCofree :: (Eq (f (Cofree f a)), Eq a) => Eq (Cofree f a) where
87-
eq x y = head x == head y && tail x == tail y
88+
instance eqCofree :: (Eq1 f, Eq a) => Eq (Cofree f a) where
89+
eq x y = head x == head y && tail x `eq1` tail y
8890

89-
instance ordCofree :: (Ord (f (Cofree f a)), Ord a) => Ord (Cofree f a) where
91+
instance eq1Cofree :: Eq1 f => Eq1 (Cofree f) where
92+
eq1 = eq
93+
94+
instance ordCofree :: (Ord1 f, Ord a) => Ord (Cofree f a) where
9095
compare x y =
9196
case compare (head x) (head y) of
92-
EQ -> compare (tail x) (tail y)
97+
EQ -> compare1 (tail x) (tail y)
9398
r -> r
9499

100+
instance ord1Cofree :: Ord1 f => Ord1 (Cofree f) where
101+
compare1 = compare
102+
95103
instance functorCofree :: Functor f => Functor (Cofree f) where
96104
map f = loop where
97105
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
@@ -19,9 +19,11 @@ import Control.Monad.Trans.Class (class MonadTrans)
1919

2020
import Data.CatList (CatList, empty, snoc, uncons)
2121
import Data.Either (Either(..))
22+
import Data.Eq (class Eq1, eq1)
2223
import Data.Foldable (class Foldable, foldMap, foldl, foldr)
2324
import Data.Inject (class Inject, inj)
2425
import Data.Maybe (Maybe(..))
26+
import Data.Ord (class Ord1, compare1)
2527
import Data.Traversable (class Traversable, traverse)
2628
import Data.Tuple (Tuple(..))
2729

@@ -41,11 +43,24 @@ data FreeView f a b = Return a | Bind (f b) (b -> Free f a)
4143

4244
data Val
4345

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

47-
instance ordFree :: (Functor f, Ord (f (Free f a)), Ord a) => Ord (Free f a) where
48-
compare x y = compare (resume x) (resume y)
52+
instance eq1Free :: (Functor f, Eq1 f) => Eq1 (Free f) where
53+
eq1 = eq
54+
55+
instance ordFree :: (Functor f, Ord1 f, Ord a) => Ord (Free f a) where
56+
compare x y = case resume x, resume y of
57+
Left fa, Left fb -> compare1 fa fb
58+
Left _, _ -> LT
59+
_, Left _ -> GT
60+
Right a, Right b -> compare a b
61+
62+
instance ord1Free :: (Functor f, Ord1 f, Ord a) => Ord1 (Free f) where
63+
compare1 = compare
4964

5065
instance freeFunctor :: Functor (Free f) where
5166
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)