ghc-internal-9.1300.0: Basic libraries
Copyright(c) Ashley Yakeley 2007
LicenseBSD-style (see the LICENSE file in the distribution)
Maintainerashley@semantic.org
Stabilitystable
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

GHC.Internal.Control.Category

Description

 
Synopsis
  • class Category (cat :: k -> k -> Type) where
    • id :: forall (a :: k). cat a a
    • (.) :: forall (b :: k) (c :: k) (a :: k). cat b c -> cat a b -> cat a c
  • (<<<) :: forall {k} cat (b :: k) (c :: k) (a :: k). Category cat => cat b c -> cat a b -> cat a c
  • (>>>) :: forall {k} cat (a :: k) (b :: k) (c :: k). Category cat => cat a b -> cat b c -> cat a c

Documentation

class Category (cat :: k -> k -> Type) where Source #

A class for categories.

In mathematics, a category is defined as a collection of objects and a collection of morphisms between objects, together with an identity morphism id for every object and an operation (.) that composes compatible morphisms.

This class is defined in an analogous way. The collection of morphisms is represented by a type parameter cat, which has kind k -> k -> Type for some kind variable k that represents the collection of objects; most of the time the choice of k will be Type.

Examples

Expand

As the method names suggest, there's a category of functions:

instance Category (->) where
  id = \x -> x
  f . g = \x -> f (g x)

Isomorphisms form a category as well:

data Iso a b = Iso (a -> b) (b -> a)

instance Category Iso where
  id = Iso id id
  Iso f1 g1 . Iso f2 g2 = Iso (f1 . f2) (g2 . g1)

Natural transformations are another important example:

newtype f ~> g = NatTransform (forall x. f x -> g x)

instance Category (~>) where
  id = NatTransform id
  NatTransform f . NatTransform g = NatTransform (f . g)

Using the TypeData language extension, we can also make a category where k isn't Type, but a custom kind Door instead:

type data Door = DoorOpen | DoorClosed

data Action (before :: Door) (after :: Door) where
  DoNothing :: Action door door
  OpenDoor :: Action start DoorClosed -> Action start DoorOpen
  CloseDoor :: Action start DoorOpen -> Action start DoorClosed

instance Category Action where
  id = DoNothing

  DoNothing . action = action
  OpenDoor rest . action = OpenDoor (rest . action)
  CloseDoor rest . action = CloseDoor (rest . action)

Methods

id :: forall (a :: k). cat a a Source #

The identity morphism. Implementations should satisfy two laws:

Right identity
f . id = f
Left identity
id . f = f

These essentially state that id should "do nothing".

(.) :: forall (b :: k) (c :: k) (a :: k). cat b c -> cat a b -> cat a c infixr 9 Source #

Morphism composition. Implementations should satisfy the law:

Associativity
f . (g . h) = (f . g) . h

This means that the way morphisms are grouped is irrelevant, so it is unambiguous to write a composition of morphisms as f . g . h, without parentheses.

Instances

Instances details
Monad m => Category (Kleisli m :: Type -> Type -> Type) Source #

Since: base-3.0

Instance details

Defined in GHC.Internal.Control.Arrow

Methods

id :: Kleisli m a a Source #

(.) :: Kleisli m b c -> Kleisli m a b -> Kleisli m a c Source #

Category (Coercion :: k -> k -> Type) Source #

Since: base-4.7.0.0

Instance details

Defined in GHC.Internal.Control.Category

Methods

id :: forall (a :: k). Coercion a a Source #

(.) :: forall (b :: k) (c :: k) (a :: k). Coercion b c -> Coercion a b -> Coercion a c Source #

Category ((:~:) :: k -> k -> Type) Source #

Since: base-4.7.0.0

Instance details

Defined in GHC.Internal.Control.Category

Methods

id :: forall (a :: k). a :~: a Source #

(.) :: forall (b :: k) (c :: k) (a :: k). (b :~: c) -> (a :~: b) -> a :~: c Source #

Category (->) Source #

Since: base-3.0

Instance details

Defined in GHC.Internal.Control.Category

Methods

id :: a -> a Source #

(.) :: (b -> c) -> (a -> b) -> a -> c Source #

Category ((:~~:) :: k -> k -> Type) Source #

Since: base-4.10.0.0

Instance details

Defined in GHC.Internal.Control.Category

Methods

id :: forall (a :: k). a :~~: a Source #

(.) :: forall (b :: k) (c :: k) (a :: k). (b :~~: c) -> (a :~~: b) -> a :~~: c Source #

(<<<) :: forall {k} cat (b :: k) (c :: k) (a :: k). Category cat => cat b c -> cat a b -> cat a c infixr 1 Source #

Right-to-left composition. This is a synonym for (.), but it can be useful to make the order of composition more apparent.

(>>>) :: forall {k} cat (a :: k) (b :: k) (c :: k). Category cat => cat a b -> cat b c -> cat a c infixr 1 Source #

Left-to-right composition. This is useful if you want to write a morphism as a pipeline going from left to right.