Alexander Kurz
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# An Axiomatic Theory of Structure # Categories, Functors, Natural Transformations (not in final form) This is the center piece of my [Short of Introduction to Monads](https://hackmd.io/@alexhkurz/H1OxumxRP). While we have already seen many examples of categories, functors and natural transformations (without calling them that), we will now meet the mathematical definitions. This will allow us, in the next lecture, to define a monad $(M,\eta,\mu)$ as a functor $M$ together with two natural transformations $\eta$ and $\mu$ satisfying the (suitably adapted) monoid laws. I started the lecture with the following tower of abstractions: - Numbers are example of monoids. - Monoids are examples of algebras. - Algebras organise themselves as categories. In the last lecture we have seen our first definition of algebraic structures as mathematical objects specified by operations and equations. Now we are going to present an **axiomatic theory of structure**. In the lecture we went through the Axioms and then tried to characterize the polymorphic functions/natural transformations of the types indicated in [polymorphic.hs](https://repl.it/@alexhkurz/polymorphic#main.hs) do exist. We skipped the other material in this write-up. Programs: [`polymorphic`](https://repl.it/@alexhkurz/polymorphic#main.hs). ## Examples In terms of the hierarchy of abstractions above, the important example is that, given a signature $\Sigma$, the collection of all $\Sigma$-algebras together with their homomorphisms forms a category. Instead of studying the structure of an algebra, we are going to study the structure of the collection of all algebras. To remember the axioms of a category, it will be good to know that a monoid is also a category. In fact, as we will see, we can think of a category as a many-sorted, or typed, monoid. Important parts of category theory are inspired by the fact that every order is also a category. But this perspective will not be exploited in this course. Just a quick remark on monoids and orders as categories: I find it intriguing that category theory combines the two most common mathematical models for irreversible phenomena, monoids and orders, in one definition. [^self-reference] [^self-reference]: Category theory is an axiomatic theory of strucure. But each category is also an example of mathematical structure. This self-reference plays an important role in various advanced areas. ## Axioms We just list the definitions. It may be helpful to interleave reading the definitions with the examples of the next section. ### Category One way to understand the definition of a category is as a "many-sorted" monoid. As we have seen in the first lecture, one can think of a monoid as a set of functions $f:A\to A$ closed under composition. Now we generalise this to $f:A\to B$ where the domain $A$ of $f$ and the codomain $B$ of $f$ are allowed to be different. **Definition:** A **category** $\cal A$ consists of a collection of "objects" and for all objects $A,B\in \cal A$ a set $\cal A(A,B)$ of "arrows". We write $f:A\to B$ for $f\in\cal A(A,B)$. For all $A\in\cal A$ there is an "identity" arrow $id_A:A\to A$. For all $A,B,C$ there is a "composition" operation $$\circ_{ABC}:{\cal A}(B,C)\times {\cal A}(A,B)\to {\cal A}(A,C)$$ We write $f\circ g=h$ for $\circ_{ABC}(f,g)=h$. The axioms are for all $h:A\to B$ \begin{gather*} h\circ id_A = h = id_B\circ h \end{gather*} and for all $f:C\to D, g:B\to C$, $h:A\to B$ \begin{gather*} (f\circ g)\circ h = f\circ (g\circ h) \end{gather*} **Exercise:** Show that a one-object category is exactly a monoid. A category in which there is at most one arrow between any two objects is called a **thin category**. **Exercise:** Show that a thin category is exactly an order (that is, a reflexive and transitive relation). ### Functor A functor generalises the notion of monoid homomorphism from monoids to categories. **Definition:** Let $\cal A, \cal B$ be two categories. A **functor** $$F:\cal A\to\cal B$$ is a function from the objects of $\cal A$ to the objects of $\cal B$ and, for each pair $A,A'$ of objects of $\cal A$ a function $$F_{AA'}:{\cal A}(A,A')\to{\cal B}(FA,FA')$$ that preserves identities and composition. **Exercise:** Show that a functor betwee one-object category is exactly a monoid homomorphism. Similarly, a functor between thin categories is exactly an order preserving map. **Exercise:** Show that `data List a = Nil | Cons a (List a)` in Haskell defines a functor. What other type constructors in Haskell are functors? ### Natural Transformations We introduced categories and functors, but, so far, conceptually, we did not do anything remarkable from the point of view of mathematics. Indeed, catgories and functors are just a variation on a familiar theme that we illustrate with the table below. |structure| morphism | |:---:|:---:| |monoid | monoid homomorphism | | order | monotone map | |graph | graph homomorphism | |category | functor | But now we will see the first definition that sets category theory apart. In fact, the story goes, categories and functors where discovered when Eilenberg and Mac Lane tried to capture the concept of a "natural map" mathematically. Technically, we extend the table above so that one obtains a good notion of "category of funtors". [^ccc] |structure| morphism | |:---:|:---:| |functor | natural transformation | [^ccc]: This remark goes beyond the scope of these lectures. One can axiomatise the set of all functions in category theoretic terms, obtaining the notion of a so-called cartesian closed category. The notion of natural transformation can be justified by the fact that it makes the category of categories a cartesian closed category. **Definition:** Let $F,G:\cal A\to\cal B$. A **natural transformation** $$\tau:F\to G$$ is a parameterised collection $\tau_A:FA\to GA$ satisfying $Gf\circ \tau_{A} = \tau_{A'}\circ Ff$, or, in a picture, ... for all $f:A\to A'$. ## More Examples The paradigmatic examples of a category are - the category of sets and functions and - the category of types and programs A large amount of category theory can be understood as an axiomatic characterisation of these two examples.[^twoexamples] So we look at these examples first. The other examples are less important for now, but illustrate that category theory reaches much further. ### Categories of types and programs The example of types and programs is very closely related to the example of sets and function. For the purposes of this section the difference is only one of emphasis and perspective. The operation $$ (-)^\ast : Set\to Set$$ that maps an alphabet $X$ to the set $X^\ast$ of words over $X$ is a functor. **Exercise:** Define $(-)^\ast$ on functions and verify that the laws of a functor are satisfied. **Notation:** In work motivated in terms of programming languages this functor is often denoted by $\sf List$. In Haskell it is written using square brackets. Indeed, if `a` is a type, then `[a]` is the type of lists over `a`. To see this for yourself, head over to [`lists`](https://repl.it/@alexhkurz/lists#main.hs) and enter the following in the console: ```haskell :type reverse ``` `reverse` is a predefined function that reverses a list. The type of reverse is ```haskell reverse :: [a] -> [a] ``` Here, `a` is a type variable that can be instantiated with any type. Indeed, the algorithm for reversing a list does not depend on the type of the elements of the list. Now it is easy to explain the idea behind the definition of a natural transformation: - `reverse` is a natural transformation. - Naturality is *exactly* the property that `reverse` can be implemented generically for all types of elements. **Exercise:** Show that `reverse` is a natural transformation. In programming languages terminology, naturality is an example of *parametric polymorphism*. **Exercise:** What other examples of parametric polymorphic functions can you find? We have just seen a first glimpse of type theory. There is a bit more in the homework below. Ask me if you want suggestions for further reading. ### Categories of sets The category $Set$ has all sets as objects and all functions as arrows. (You may want to check that the axioms of a category are satsified.) If this course was an introduction to category theory, we would now revisit definitions and construction that are defined in set-theory in terms of the elementship relation such as, for example, the cartesian product $$ A\times B = \{(a,b) \mid a\in A, b\in B\}$$ Notice that this definition makes crucial use of $\in$. But the language of category theory only has $id$ and $\circ$. So how can we define the product of two sets in the language of category theory? In fact, and maybe surprisingly, it is possible to develop set theory in the language of category theory. Ask me if you want to know more about this. (This area of mathematics is known as **topos theory**.) For now, we only need to feel the comfort of a familiar example. If in doubt of how to think of a category, just think in terms of sets and functions. [^thinkin] [^thinkin]: There are interesting categories in which the "sets and functions" intuition is misleading. But we will not encounter them in this short introduction. **Exercise:** Let $\mathcal PX$ be the set of subsets of $X$. For a function $f:X\to Y$, define $\mathcal Pf:\mathcal PX\to \mathcal PY$ as direct image, that is, $$\mathcal P f(a)=\{f(x) \mid x\in a\}$$ for all $a\subseteq X$.[^directimage] Show that $\mathcal P$ is a functor. There is an opportunity in the homework to pursue this a bit further. ### Categories of structures Slogan: Mathematical structures of any kind organise themselves in a category. The cateogy of - monoids - groups - orders - topological spaces - ... All of these categories are similar to the category of sets. We can think of the extra structure as eliminating some of the functions, namely those that are not structure preserving. Categories of structures are subcategories of the category of sets. Categories of structures are the main topic of many introductory category theory books such as the ones by Mac Lane and by Adamek, Herrlich and Strecker. ### Categories of relations What I mean here are categories where the arrows are not functions but relations. These categories have rather different properties than categories where arrows are functions. Two prominent approaches to categories of relations are allegories and cartesian bicategories. ### Categories as structures We have already seen two main examples: Every monoid is a category. Every order is a category. With some extra work one can also include metric spaces and variations into this picture. The development of these ideas leads to enriched category theory. ### Categories as logical theories In any reasonable logic there is a proof that $A$ implies $A$. We can think of this as the identity $id:A\to A$ in a category. Moreover, whenever I have a proof $f:A\to B$ and a proof $g:B\to C$ then there is a proof $f;g$ of $A\to C$. From this point of view, a category is a logical theory and, vice versa, a logical theory is a category. This idea turned out to be very fruitful. Categorical logic is a rich area of logic with close relations to topos theory, type theory, homotopy type theory. ## Theory (if there is time ... a digression in some ways, but indispensable to better understand natural transformations, polymorphism, ... now delegated to one or two optional separate lectures) Yoneda's lemma Corollary: Every group is a group of permutations. Every monoid is a monoid of functions. ## Application (I'd love to do this, but I think there will be no time and it is a digression anyway when the aim are monads ... moreover, before doing this one should spend some time on natural transformations) Something from the theory of species. Functors as mathematical structures up to isomorphism. Deriving one example of a combinatorial formula. ## Homework These exercises are about polymorphic functions/natural transformations. The first exercise is, in my opinion, essential for a deeper understanding of category theory, but it can be skipped if one is interested mainly in the monad story. ### (Programming) Polymorphic Functions as Natural Transformations From a programmer's point of view, implement as many functions of the types indicated in [`polymorphic`](https://repl.it/@alexhkurz/polymorphic#main.hs) as you can. From a mathematicians point of view try to characterise, for any given type, the set of *all* functions (considered as natural transformation between functors $Set\to Set$). It may be fewer functions than you think at first sight. [^a->a] [^a->a]: For example, there is only one function/natural transformation of type `a->a`. There are only two functions/natural transformations of type `a->a->a`. Etc ### (Maths) Polymorphic Functions as Natural Transformations This exercise is really the same as the previous one, now focussing more on the mathematics side than on the programming side. **Exercise (Optional):** These exercises (in particular the 2nd and 4th) are more challenging. Try them if you want to learn more about category theory. If you just want the fastest route to monads, ignore them. - Define for each function $A\to B$ a natural transformation $A\times X\to B\times X$. - Show that each natural transformation $A\times X\to B\times X$ is induced by a function $A\to B$. - Recall the definition of $X^A$ as the set of functions $A\to X$. Define for each function $B\to A$ a natural transformation $X^A\to X^B$. - Show that each natural transformation $X^A\to X^B$ is induced by a function $B\to A$. ### (Maths) Sets as Quotients of Lists The next exercise is one of the simplest examples of a natural transformation that is quotienting with respect to a set of equations. It illustrates that in mathematics there are functors and natural transformation that do not necessarily have an immediate implementation in a programming language. Recall the definitions of the functors ${\sf List}$ and $\mathcal P$. Define a function $${\sf List}(X)\to\mathcal PX$$ and show that is a natural transformation. ## Outlook There is so much to say ... get in touch if you want to know more ... I may fill in sth later ... ## References and Further Reading As a first introduction to category theory, I like the book "Conceptual Mathematics" by Lawvere and Schanuel but as far as I can see it is not available online. Neither is the classical "Categories for the Working Mathematician" by Mac Lane nor the monograph "Algebraic Theories" by Manes. But there is a lot of excellent material available. The first book on category theory I read as a PhD student was - Barr, Wells: [Category Theory for Computing Science](https://www.math.mcgill.ca/triples/Barr-Wells-ctcs.pdf). I think it still is a very good introduction. A more advanced book, and an excellent source on toposes and monads (called triples there), is - Barr, Wells: [Toposes, Triples and Theories](http://www.tac.mta.ca/tac/reprints/articles/12/tr12.pdf) Another classic and important reference is - Adamek, Herrlich, Strecker: [Abstract and Concrete Categories](http://katmat.math.uni-bremen.de/acc/acc.pdf). More recent introductions to category, which I have not read myself, but which look very promising and commendable are - Awodey: [Category Theory](http://scholar.google.com/scholar_url?url=https://www.researchgate.net/profile/Kameswara_Rao_Chellapilla2/post/Can_philosophy_help_to_innovate_and_develop_scientific_theory/attachment/5b632c2fb53d2f89289cfe85/AS%253A655211613933570%25401533226030818/download/Book_awodey__category_theory.pdf&hl=en&sa=X&ei=prgRYNHbLeaKywTa8IAg&scisig=AAGBfm3BlEpY04i0JDgo3_BlJZrtrYWafw&nossl=1&oi=scholarr) - Leinster: [Basic Category Theory](https://arxiv.org/pdf/1612.09375) An excellent book for learning more advanced category theory as it is used in modern mathematics is - Riehl: [Category Theory in Context](https://people.math.rochester.edu/faculty/doug/otherpapers/Riehl-CTC.pdf) [^twoexamples]: In fact, there is more than one category of sets and there is more than one category of types and programs. But we will have no time to pursue this further for now. [^directimage]: A more widely used notation for $\mathcal Pf(A)$ is $f[A]$.

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully