Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #11. Rename run to applyNT and nat to makeNT #12

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 0.3.2
# 0.4

* Renamed `Nat` constructor to `NT`
* Renamed `run` to `unwrapNT`
* Renamed `nat` to `wrapNT`
* Backport the `Semigroup` instance for `(:~>)` by conditionally depending on
the `semigroups` package

Expand Down
4 changes: 2 additions & 2 deletions natural-transformation.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: natural-transformation
version: 0.3.2
version: 0.4
synopsis: A natural transformation package.
description: A natural transformation transforms a container @f a@ into another
container @g a@. Natural transformations act as functor morphisms
Expand Down Expand Up @@ -43,7 +43,7 @@ test-suite natural-transformation-properties
main-is: Properties.hs
build-depends: base >= 4.7 && < 5
, containers >= 0.1 && < 0.6
, natural-transformation == 0.3.2
, natural-transformation == 0.4
, quickcheck-instances >= 0.1 && < 0.4
, tasty >= 0.8 && < 0.12
, tasty-quickcheck >= 0.8 && < 0.9
Expand Down
35 changes: 19 additions & 16 deletions src/Control/Natural.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module Control.Natural
-- * Type Synonym for a Natural Transformation
, type (~>)
-- * Conversion functions between the newtype and the synonym
, run
, nat
, wrapNT
, unwrapNT
-- * Class for Natural Transformations
, Transformation(..)
) where
Expand All @@ -48,18 +48,18 @@ type f ~> g = forall x. f x -> g x

infixr 0 :~>, $$
-- | A natural transformation suitable for storing in a container.
newtype f :~> g = Nat { ($$) :: f ~> g }
newtype f :~> g = NT { ($$) :: f ~> g }
deriving Typeable

instance C.Category (:~>) where
id = Nat id
Nat f . Nat g = Nat (f . g)
id = NT id
NT f . NT g = NT (f . g)

instance f ~ g => Semigroup (f :~> g) where
Nat f <> Nat g = Nat (f . g)
NT f <> NT g = NT (f . g)

instance f ~ g => Monoid (f :~> g) where
mempty = Nat id
mempty = NT id
mappend = (<>)

infix 0 #
Expand All @@ -74,14 +74,17 @@ class Transformation f g t | t -> f g where
(#) :: t -> forall a . f a -> g a

instance Transformation f g (f :~> g) where
Nat f # g = f g
NT f # g = f g

-- | 'run' is the nonfix version of @#@. It is used to break natural
-- transformation wrappers, including ':~>'.
run :: Transformation f g t => t -> (forall a . f a -> g a)
run = (#)
-- | 'wrapNT' builds our natural transformation abstraction out of
-- a natural transformation function.
--
-- An alias to 'NT' provided for symmetry with 'unwrapNT'.
--
wrapNT :: (forall a . f a -> g a) -> f :~> g
wrapNT = NT

-- | 'nat' builds our natural transformation abstraction out of
-- a natural transformation function.
nat :: (forall a . f a -> g a) -> f :~> g
nat = Nat
-- | 'applyNT' is the nonfix version of @#@. It is used to break natural
-- transformation wrappers, including ':~>'.
unwrapNT :: Transformation f g t => t -> (forall a . f a -> g a)
unwrapNT = (#)
2 changes: 1 addition & 1 deletion src/Control/Object.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ import Control.Natural
newtype Object f = Object (f ~> IO)

instance Transformation f IO (Object f) where
Object f # g = Nat f # g
Object f # g = NT f # g
8 changes: 4 additions & 4 deletions tests/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ prop_monoidLaws x y z t = (mappend mempty x # t) == (x # t)

-- | A natural transformations from lists to lists that 'reverse's.
listReverseNT :: [] :~> []
listReverseNT = Nat reverse
listReverseNT = NT reverse

-- | A natural transformation from lists to lists that shifts all elements to the left,
-- moving the head element to the back.
listShiftNT :: [] :~> []
listShiftNT = Nat $ \l -> case l of
listShiftNT = NT $ \l -> case l of
[] -> []
(x:xs) -> xs ++ [x]

-- | A natural transformation from lists to 'Seq's.
listSeqNT :: [] :~> Seq
listSeqNT = Nat fromList
listSeqNT = NT fromList

-- | A natural transformation from 'Seq's to lists.
seqListNT :: Seq :~> []
seqListNT = Nat toList
seqListNT = NT toList