Hask
From HaskellWiki
m (→"Platonic" Hask) 
(→Hask is not Cartesian closed: strict product) 

Line 33:  Line 33:  
! scope="col"  Terminal Object  ! scope="col"  Terminal Object  
! scope="col"  Sum  ! scope="col"  Sum  
+  ! scope="col"  Product  
! scope="col"  Product  ! scope="col"  Product  
    
−  ! scope="row"   +  ! scope="row"  Type 
+   <hask>data Empty</hask>  
+   <hask>data () = ()</hask>  
+   <hask>data Either a b  
+  = Left a  Right b</hask>  
+   <hask>data (a,b) = (,) a b</hask>  
+   <hask>data P a b = P !a !b</hask>  
+    
+  ! scope="row"  Requirement  
 There is a unique function   There is a unique function  
<br /><hask>u :: Empty > r</hask>  <br /><hask>u :: Empty > r</hask>  
Line 60:  Line 69:  
<hask>fst . u = f</hask>  <hask>fst . u = f</hask>  
<br /><hask>snd . u = g</hask>  <br /><hask>snd . u = g</hask>  
+   For any functions  
+  <br /><hask>f :: r > a</hask>  
+  <br /><hask>g :: r > b</hask>  
+  
+  there is a unique function  
+  <hask>u :: r > P a b</hask>  
+  
+  such that:  
+  <hask>fstP . u = f</hask>  
+  <br /><hask>sndP . u = g</hask>  
    
! scope="row"  Platonic candidate  ! scope="row"  Platonic candidate  
Line 67:  Line 86:  
<br /><hask>u1 (Right b) = g b</hask>  <br /><hask>u1 (Right b) = g b</hask>  
 <hask>u1 r = (f r,g r)</hask>   <hask>u1 r = (f r,g r)</hask>  
+   <hask>u1 r = P (f r) (g r)</hask>  
    
! scope="row"  Example failure condition  ! scope="row"  Example failure condition  
Line 77:  Line 97:  
<br /><hask>f _ = undefined</hask>  <br /><hask>f _ = undefined</hask>  
<br /><hask>g _ = undefined</hask>  <br /><hask>g _ = undefined</hask>  
+   <hask>r ~ ()</hask>  
+  <br /><hask>f _ = undefined</hask>  
+  <br /><hask>g _ = ()</hask>  
    
! scope="row"  Alternative u  ! scope="row"  Alternative u  
Line 83:  Line 106:  
 <hask>u2 _ = ()</hask>   <hask>u2 _ = ()</hask>  
 <hask>u2 _ = undefined</hask>   <hask>u2 _ = undefined</hask>  
+    
    
! scope="row"  Difference  ! scope="row"  Difference  
Line 93:  Line 117:  
 <hask>u1 _ = (undefined,undefined)</hask>   <hask>u1 _ = (undefined,undefined)</hask>  
<br /><hask>u2 _ = undefined</hask>  <br /><hask>u2 _ = undefined</hask>  
+   <hask>g _ = ()</hask>  
+  <br /><hask>(fstP . u1) _ = undefined</hask>  
 style="background: red;"   style="background: red;"  
! scope="row"  Result  ! scope="row"  Result  
+  ! scope="col"  FAIL  
! scope="col"  FAIL  ! scope="col"  FAIL  
! scope="col"  FAIL  ! scope="col"  FAIL 
Revision as of 04:39, 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 
1 Definition
The objects of Hask are Haskell types, and the morphisms from objects2 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 () = ()
3 Hask is not Cartesian closed
Actual Hask does not have sums, products, or an initial object, andInitial Object  Terminal Object  Sum  Product  Product  

Type  data Empty 
data () = () 
data Either a b = Left a  Right b 
data (a,b) = (,) a b 
data P a b = P !a !b 
Requirement  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 
For any functions
f :: r > a g :: r > b there is a unique function u :: r > P a b such that: fstP . u = f sndP . 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) 
u1 r = P (f r) (g r) 
Example failure condition  r ~ () 
r ~ () 
r ~ () f _ = () g _ = () 
r ~ () f _ = undefined g _ = undefined 
r ~ () f _ = undefined g _ = () 
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 
g _ = () (fstP . u1) _ = undefined 
Result  FAIL  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 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.