From 2a0a6529c817dfbfc38f8e355b40dfd94df3dfd5 Mon Sep 17 00:00:00 2001 From: Gary Burgess Date: Fri, 30 Nov 2018 13:14:28 +0000 Subject: [PATCH 1/2] Reorder Inject instances to allow inj :: f ~> f --- src/Data/Functor/Coproduct/Inject.purs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Data/Functor/Coproduct/Inject.purs b/src/Data/Functor/Coproduct/Inject.purs index b16950e..8aa89d3 100644 --- a/src/Data/Functor/Coproduct/Inject.purs +++ b/src/Data/Functor/Coproduct/Inject.purs @@ -10,14 +10,14 @@ class Inject f g where inj :: forall a. f a -> g a prj :: forall a. g a -> Maybe (f a) -instance injectLeft :: Inject f (Coproduct f g) where +instance injectReflexive :: Inject f f where + inj = identity + prj = Just + +else instance injectLeft :: Inject f (Coproduct f g) where inj = Coproduct <<< Left prj = coproduct Just (const Nothing) else instance injectRight :: Inject f g => Inject f (Coproduct h g) where inj = Coproduct <<< Right <<< inj prj = coproduct (const Nothing) prj - -else instance injectReflexive :: Inject f f where - inj = identity - prj = Just From 772bc6729d6405f46e7c0ee22920ea7de5bdfbbf Mon Sep 17 00:00:00 2001 From: Gary Burgess Date: Fri, 30 Nov 2018 17:57:13 +0000 Subject: [PATCH 2/2] Add injection tests --- test/Main.purs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/Main.purs b/test/Main.purs index 4ac9e34..9bf3801 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -2,7 +2,10 @@ module Test.Main where import Prelude +import Data.Const (Const) import Data.Functor.Compose (Compose(..)) +import Data.Functor.Coproduct (Coproduct, left, right) +import Data.Functor.Coproduct.Inject (inj) import Data.Identity (Identity(..)) import Data.Maybe (Maybe(..)) import Effect (Effect) @@ -10,6 +13,11 @@ import Test.Assert (assertEqual) main :: Effect Unit main = do + testComposeOrdering + testInjections + +testComposeOrdering :: Effect Unit +testComposeOrdering = do assertEqual { expected: Compose (Identity (Just true)) , actual: Compose (Identity (Just true)) @@ -18,3 +26,18 @@ main = do { expected: Compose (Identity (Just true)) > Compose (Identity (Just false)) , actual: true } + +testInjections :: Effect Unit +testInjections = do + assertEqual + { expected: Identity unit + , actual: inj (Identity unit) + } + assertEqual + { expected: left (Identity unit) :: Coproduct Identity (Const Void) Unit + , actual: inj (Identity unit) + } + assertEqual + { expected: right (Identity unit) :: Coproduct (Const Void) Identity Unit + , actual: inj (Identity unit) + }