Skip to content

Commit 90bee20

Browse files
authored
Merge pull request #4 from purescript/rows
Add effect row equality
2 parents 372fbb8 + df4b87b commit 90bee20

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Type/Row/Effect/Equality.purs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Type.Row.Effect.Equality
2+
( class EffectRowEquals
3+
, to
4+
, from
5+
, effTo
6+
, effFrom
7+
) where
8+
9+
-- | This type class asserts that effect rows `a` and `b` are equal.
10+
-- |
11+
-- | The functional dependencies and the single instance below will force the
12+
-- | two type arguments to unify when either one is known.
13+
-- |
14+
-- | Note: any instance will necessarily ovelap with `refl` below, so instances
15+
-- | of this class should not be defined in libraries.
16+
class EffectRowEquals (a :: # Effect) (b :: # Effect) | a -> b, b -> a where
17+
to :: forall r. r a -> r b
18+
from :: forall r. r b -> r a
19+
20+
instance refl :: EffectRowEquals a a where
21+
to a = a
22+
from a = a
23+
24+
newtype Flipmode e a eff = Flipmode (e eff a)
25+
26+
unflip :: forall e a eff. Flipmode e a eff -> e eff a
27+
unflip (Flipmode e) = e
28+
29+
-- | A version of `to` that can be applied to types like `Eff`, `Aff`, etc.
30+
effTo :: forall e a b x. EffectRowEquals a b => e a x -> e b x
31+
effTo e = unflip (to (Flipmode e))
32+
33+
-- | A version of `from` that can be applied to types like `Eff`, `Aff`, etc.
34+
effFrom :: forall e a b x. EffectRowEquals a b => e b x -> e a x
35+
effFrom e = unflip (from (Flipmode e))

test/Main.purs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Control.Monad.Eff (Eff)
55
import Control.Monad.Eff.Console (CONSOLE, log)
66
import Data.Newtype (class Newtype, unwrap)
77
import Type.Equality (class TypeEquals, to, from)
8+
import Type.Row.Effect.Equality as REE
89

910
newtype RecordNewtype = RecordNewtype
1011
{ message :: String }
@@ -15,5 +16,11 @@ instance newtypeRecordNewtype ::
1516
wrap = RecordNewtype <<< to
1617
unwrap (RecordNewtype rec) = from rec
1718

19+
class Foo f where
20+
foo :: String -> f Unit
21+
22+
instance fooEff :: REE.EffectRowEquals eff (console :: CONSOLE | e) => Foo (Eff eff) where
23+
foo = REE.effFrom <<< log
24+
1825
main :: Eff (console :: CONSOLE) Unit
19-
main = log (unwrap (RecordNewtype { message: "Done" })).message
26+
main = foo (unwrap (RecordNewtype { message: "Done" })).message

0 commit comments

Comments
 (0)