9

Hask is usually thought to be the category whose objects are types and morphisms are functions. However, I've seen Conor McBride (@pigworker) warn against the use of Hask multiple times (1, 2, 3):

  • I would discourage talk of "the Hask Category" because it subconsciously conditions you against looking for other categorical structure in Haskell programming.

  • Note, I dislike the use of "Hask" as the name of the "category of Haskell types and functions": I fear that labelling one category as the Haskell category has the unfortunate side-effect of blinding us to the wealth of other categorical structure in Haskell programming. It's a trap.

  • I wish people wouldn't call it "Hask", though: it threatens to limit the imagination.

What other categories can we see in Haskell?

In one of his answers, he touches upon some of these ideas, but I wonder if someone could expand upon it; and I wonder if there are even more examples.

[...] there's a ton of categorical structure lurking everywhere, there's certainly a ton of categorical structure available (possibly but not necessarily) at higher kinds. I'm particularly fond of functors between indexed families of sets.

Community
  • 1
  • 1
Dan Oneață
  • 968
  • 7
  • 14
  • 3
    Well, there is a ton of categorical structure lurking _everywhere across mathematics_. Haskell simply inherits a big chunk of that. Nevertheless, I disagree that it doesn't make sense to single out **Hask**, because that category _does_ get preferential treatment through the language's syntax. Namely, lambdas etc. give you very nice access to the cartesian-closed property of **Hask**, but not of any other category (though there are [approaches for that](http://conal.net/blog/posts/haskell-to-hardware-via-cccs) too). – leftaroundabout Jun 07 '18 at 08:20
  • 2
    What I do agree with is that we should always keep our eyes open for other categories. But IMO this is only unfolds its full potential if we can abstract over them within the language. Unfortunately, the standard [`Category`](http://hackage.haskell.org/package/base-4.11.1.0/docs/Control-Category.html#t:Category) / `Arrow` classes are too restricted to express most interesting categories – ironically, because they _don't_ restrict their objects but have to operate on all Haskell types. So to move forward, we should more widely adapt category classes that allow finer control. – leftaroundabout Jun 07 '18 at 08:25
  • 1
    Gabriel Gonzalez has done a lot of work on "other" categories in Haskell. His `pipes` library is explicitly designed around a type that [is part of five different categories](https://hackage.haskell.org/package/pipes-4.3.9/docs/Pipes-Core.html#g:2). – Carl Jun 07 '18 at 14:08
  • 1
    Perhaps the more interesting thing about **Hask** is that thee's [no such category](http://math.andrej.com/2016/08/06/hask-is-not-a-category/). – n. m. could be an AI Jun 07 '18 at 15:33
  • This question is quite broad. – Benjamin Hodgson Jun 08 '18 at 00:03

3 Answers3

6

Constraints in Haskell also form a category. The objects are the constraints, and the arrows mean "this constraint implies this other constraint". So every constraint implies itself, and there's an arrow between Monad f and Applicative f, between Ord a and Eq a and between Ord a and Ord [a].

It is a thin category, so there is at most one arrow between two objects.

danidiaz
  • 26,936
  • 4
  • 45
  • 95
  • 1
    And apparently, the upcoming `QuantifiedContexts` extension also has a categorical interpretation, but that is beyond my ken https://mail.haskell.org/pipermail/libraries/2018-January/028399.html https://ghc.haskell.org/trac/ghc/ticket/5927#comment:19 – danidiaz Jun 07 '18 at 18:45
  • This post describes a category in which morphisms are properties about types: https://markkarpov.com/post/smart-constructors-that-cannot-fail.html – danidiaz Jun 16 '18 at 15:28
4

Gabriel Gonzalez has blogged about this. Here's one such post: http://www.haskellforall.com/2012/08/the-category-design-pattern.html

In it, he calls Hask "the function category", and also discusses "the Kleisli category" and "the pipes category." These are all examples of instances of the Category typeclass in Haskell. The Category typeclass in Haskell is a subset of the categories you can find in Haskell.

Dan Burton
  • 53,238
  • 27
  • 117
  • 198
2

I once uploaded an educational package that demonstrates one example of this. I called it MHask.

http://hackage.haskell.org/package/MHask

Copied from the hackage page:

MHask is the category where

  • The objects are Haskell types of kind (* → *) that have an instance of Prelude.Monad
  • An arrow from object m to object n is a Haskell function of the form (forall x. m x → n x)
  • Arrow composition is merely a specialization of Haskell function composition
  • The identity arrow for the object m is the Prelude.id function in Haskell, specialized to (forall x. m x → m x)

Caveat emptor; I have not looked at this in a long time. There may be mistakes.

Dan Burton
  • 53,238
  • 27
  • 117
  • 198
  • 1
    N.B. not all categories in Haskell are of the form of Hask and MHask. So I think McBride's warning applies here too. – Dan Burton Jun 07 '18 at 14:55
  • Those typically show up in the wild through the means of [the *mmorph* package](https://hackage.haskell.org/package/mmorph-1.1.2/docs/Control-Monad-Morph.html) – duplode Jun 09 '18 at 14:14