# Hask

### From HaskellWiki

(→Hask) |
|||

Line 1: | Line 1: | ||

'''Hask''' refers to a [[Category theory|category]] with types as objects and functions between them as morphisms. However, its use is ambiguous. Sometimes it refers to Haskell (''actual '''Hask'''''), and sometimes it refers to some subset of Haskell where no values are bottom and all functions terminate (''platonic '''Hask'''''). The reason for this is that platonic '''Hask''' has lots of nice properties that actual '''Hask''' does not, and is thus easier to reason in. There is a faithful functor from platonic '''Hask''' to actual '''Hask''' allowing programmers to think in the former to write code in the latter. | '''Hask''' refers to a [[Category theory|category]] with types as objects and functions between them as morphisms. However, its use is ambiguous. Sometimes it refers to Haskell (''actual '''Hask'''''), and sometimes it refers to some subset of Haskell where no values are bottom and all functions terminate (''platonic '''Hask'''''). The reason for this is that platonic '''Hask''' has lots of nice properties that actual '''Hask''' does not, and is thus easier to reason in. There is a faithful functor from platonic '''Hask''' to actual '''Hask''' allowing programmers to think in the former to write code in the latter. | ||

− | == | + | == Definition == |

The objects of '''Hask''' are Haskell types, and the morphisms from objects <hask>A</hask> to <hask>B</hask> are Haskell functions of type <hask>A -> B</hask>. The identity morphism for object <hask>A</hask> is <hask>id :: A</hask>, and the composition of morphisms <hask>f</hask> and <hask>g</hask> is <hask>f . g = \x -> f (g x)</hask>. | The objects of '''Hask''' are Haskell types, and the morphisms from objects <hask>A</hask> to <hask>B</hask> are Haskell functions of type <hask>A -> B</hask>. The identity morphism for object <hask>A</hask> is <hask>id :: A</hask>, and the composition of morphisms <hask>f</hask> and <hask>g</hask> is <hask>f . g = \x -> f (g x)</hask>. | ||

− | + | == Is '''Hask''' even a category? == | |

Consider: | Consider: | ||

Line 23: | Line 23: | ||

This might be a problem, because <hask>undef1 . id = undef2</hask>. In order to make '''Hask''' a category, we define two functions <hask>f</hask> and <hask>g</hask> as the same morphism if <hask>f x = g x</hask> for all <hask>x</hask>. Thus <hask>undef1</hask> and <hask>undef2</hask> are different ''values'', but the same ''morphism'' in '''Hask'''. | This might be a problem, because <hask>undef1 . id = undef2</hask>. In order to make '''Hask''' a category, we define two functions <hask>f</hask> and <hask>g</hask> as the same morphism if <hask>f x = g x</hask> for all <hask>x</hask>. Thus <hask>undef1</hask> and <hask>undef2</hask> are different ''values'', but the same ''morphism'' in '''Hask'''. | ||

− | + | == '''Hask''' is not Cartesian closed == | |

Actual '''Hask''' does not have sums, products, or an initial object, and <hask>()</hask> is not a terminal object. The Monad identities fail for almost all instances of the Monad class. | Actual '''Hask''' does not have sums, products, or an initial object, and <hask>()</hask> is not a terminal object. The Monad identities fail for almost all instances of the Monad class. | ||

Line 101: | Line 101: | ||

|} | |} | ||

− | == Platonic '''Hask''' == | + | == "Platonic" '''Hask''' == |

Because of these difficulties, Haskell developers tend to think in some subset of Haskell where types do not have bottoms. This means that it only includes functions that terminate, and typically only finite values. The corresponding category has the expected initial and terminal objects, sums and products. Instances of Functor and Monad really are endofunctors and monads. | Because of these difficulties, Haskell developers tend to think in some subset of Haskell where types do not have bottoms. This means that it only includes functions that terminate, and typically only finite values. The corresponding category has the expected initial and terminal objects, sums and products. Instances of Functor and Monad really are endofunctors and monads. |

## Revision as of 07:26, 23 August 2012

**Hask** refers to a category with types as objects and functions between them as morphisms. However, its use is ambiguous. Sometimes it refers to Haskell (*actual Hask*), and sometimes it refers to some subset of Haskell where no values are bottom and all functions terminate (

*platonic*). The reason for this is that platonic

**Hask****Hask**has lots of nice properties that actual

**Hask**does not, and is thus easier to reason in. There is a faithful functor from platonic

**Hask**to actual

**Hask**allowing programmers to think in the former to write code in the latter.

## Contents |

## 1 Definition

The objects of**Hask**are Haskell types, and the morphisms from objects

## 2 Is **Hask** even a category?

Consider:

undef1 = undefined :: a -> b undef2 = \_ -> undefined

Note that these are not the same value:

seq undef1 () = undefined seq undef2 () = ()

**Hask**a category, we define two functions

*values*, but the same

*morphism*in

**Hask**.

## 3 **Hask** is not Cartesian closed

Actual **Hask**does not have sums, products, or an initial object, and

Initial Object | Terminal Object | Sum | Product | |
---|---|---|---|---|

Definition | There is a unique function
u :: Empty -> r |
There is a unique function
u :: r -> () |
For any functions
f :: a -> r g :: b -> r there is a unique function u :: Either a b -> r such that: u . Left = f u . Right = g |
For any functions
f :: r -> a g :: r -> b there is a unique function u :: r -> (a,b) such that: fst . u = f snd . u = g |

Platonic candidate | u1 r = case r of {} |
u1 _ = () |
u1 (Left a) = f a u1 (Right b) = g b |
u1 r = (f r,g r) |

Example failure condition | r ~ () |
r ~ () |
r ~ () f _ = () g _ = () |
r ~ () f _ = undefined g _ = undefined |

Alternative u | u2 _ = () |
u2 _ = undefined |
u2 _ = () |
u2 _ = undefined |

Difference | u1 undefined = undefined u2 undefined = () |
u1 _ = () u2 _ = undefined |
u1 undefined = undefined u2 undefined = () |
u1 _ = (undefined,undefined) u2 _ = undefined |

Result | FAIL | FAIL | FAIL | FAIL |

## 4 "Platonic" **Hask**

Because of these difficulties, Haskell developers tend to think in some subset of Haskell where types do not have bottoms. This means that it only includes functions that terminate, and typically only finite values. The corresponding category has the expected initial and terminal objects, sums and products. Instances of Functor and Monad really are endofunctors and monads.