Difference between revisions of "Hask"
(→Hask is not Cartesian closed: strict product) 
m (→Hask is not Cartesian closed) 

Line 41:  Line 41:  
 <hask>data Either a b 
 <hask>data Either a b 

= Left a  Right b</hask> 
= Left a  Right b</hask> 

−   <hask>data (a,b) = 
+   <hask>data (a,b) = 
−  +  (,) { fst :: a, snd :: b}</hask> 

+   <hask>data P a b = 

+  P {fstP :: !a, sndP :: !b}</hask> 

 
 

! scope="row"  Requirement 
! scope="row"  Requirement 

Line 118:  Line 118:  
<br /><hask>u2 _ = undefined</hask> 
<br /><hask>u2 _ = undefined</hask> 

 <hask>g _ = ()</hask> 
 <hask>g _ = ()</hask> 

−  <br /><hask>(fstP 
+  <br /><hask>(fstP . u1) _ = undefined</hask> 
 style="background: red;" 
 style="background: red;" 

! scope="row"  Result 
! scope="row"  Result 
Revision as of 04:46, 6 September 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 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.
Contents
Definition
The objects of Hask are Haskell types, and the morphisms from objects A
to B
are Haskell functions of type A > B
. The identity morphism for object A
is id :: A
, and the composition of morphisms f
and g
is f . g = \x > f (g x)
.
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 () = ()
This might be a problem, because undef1 . id = undef2
. In order to make Hask a category, we define two functions f
and g
as the same morphism if f x = g x
for all x
. Thus undef1
and undef2
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 ()
is not a terminal object. The Monad identities fail for almost all instances of the Monad class.
Initial Object  Terminal Object  Sum  Product  Product  

Type  data Empty

data () = ()

data Either a b = Left a  Right b

data (a,b) = (,) { fst :: a, snd :: b}

data P a b = P {fstP :: !a, sndP :: !b}

Requirement  There is a unique function

There is a unique function

For any functions
there is a unique function
such that:

For any functions
there is a unique function
such that:

For any functions
there is a unique function
such that:

Platonic candidate  u1 r = case r of {}

u1 _ = ()

u1 (Left a) = f a

u1 r = (f r,g r)

u1 r = P (f r) (g r)

Example failure condition  r ~ ()

r ~ ()

r ~ ()

r ~ ()

r ~ ()

Alternative u  u2 _ = ()

u2 _ = undefined

u2 _ = ()

u2 _ = undefined


Difference  u1 undefined = undefined

u1 _ = ()

u1 undefined = undefined

u1 _ = (undefined,undefined)

g _ = ()

Result  FAIL  FAIL  FAIL  FAIL  FAIL 
"Platonic" Hask
Because of these difficulties, Haskell developers tend to think in some subset of Haskell where types do not have bottom values. 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.