Skip to content

Commit e944185

Browse files
committed
Implement Mu, Nu, type class instances
1 parent ce84c2b commit e944185

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/Data/Eq1.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Data.Eq1 where
2+
3+
import Prelude
4+
5+
class Eq1 f where
6+
eq1 :: forall a. (Eq a) => f a -> f a -> Boolean

src/Data/Functor/Mu.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ module Data.Functor.Mu
55
) where
66

77
import Prelude
8+
import Data.TacitString as TS
9+
10+
import Data.Eq1
11+
import Data.Ord1
812

913
-- | `Mu f` is the least fixed point of a functor `f`, when it exists.
1014
data Mu f = In (f (Mu f))
@@ -14,3 +18,18 @@ roll = In
1418

1519
unroll :: forall f. Mu f -> f (Mu f)
1620
unroll (In x) = x
21+
22+
-- | To implement `Eq`, we require `f` to have higher-kinded equality.
23+
instance eqMu :: (Eq1 f) => Eq (Mu f) where
24+
eq (In x) (In y) = eq1 x y
25+
26+
-- | To implement `Ord`, we require `f` to have higher-kinded comparison.
27+
instance ordMu :: (Eq1 f, Ord1 f) => Ord (Mu f) where
28+
compare (In x) (In y) = compare1 x y
29+
30+
-- | `Show` is compositional, so we only `f` to be able to show a single layer of structure.
31+
-- Therefore, there is no need for `Show1`; we use `TacitString` in order to prevent
32+
-- extra quotes from appearing.
33+
instance showMu :: (Show (f TS.TacitString), Functor f) => Show (Mu f) where
34+
show (In x) = show $ x <#> (show >>> TS.hush)
35+

src/Data/Ord1.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Data.Ord1 where
2+
3+
import Prelude
4+
5+
class Ord1 f where
6+
compare1 :: forall a. (Ord a) => f a -> f a -> Ordering

src/Data/TacitString.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Data.TacitString
2+
( TacitString()
3+
, hush
4+
) where
5+
6+
import Prelude
7+
8+
newtype TacitString = TacitString String
9+
instance showTacitString :: Show TacitString where
10+
show (TacitString str) = str
11+
12+
hush :: String -> TacitString
13+
hush = TacitString

0 commit comments

Comments
 (0)