Hask
From HaskellWiki
(Difference between revisions)
Benmachine (Talk  contribs) (id is a function :)) 

(16 intermediate revisions by one user not shown)  
Line 1:  Line 1:  
−  '''Hask'''  +  '''Hask''' is the [[Category theorycategory]] of Haskell types and functions. 
−  == '''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 > 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:  
+  
+  <haskell>  
+  undef1 = undefined :: a > b  
+  undef2 = \_ > undefined  
+  </haskell>  
+  
+  Note that these are not the same value:  
+  
+  <haskell>  
+  seq undef1 () = undefined  
+  seq undef2 () = ()  
+  </haskell>  
+  
+  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 12:  Line 32:  
! scope="col"  Sum  ! scope="col"  Sum  
! scope="col"  Product  ! scope="col"  Product  
+  ! scope="col"  Product  
+    
+  ! scope="row"  Type  
+   <hask>data Empty</hask>  
+   <hask>data () = ()</hask>  
+   <hask>data Either a b  
+  = Left a  Right b</hask>  
+   <hask>data (a,b) =  
+  (,) { fst :: a, snd :: b}</hask>  
+   <hask>data P a b =  
+  P {fstP :: !a, sndP :: !b}</hask>  
    
−  ! scope="row"   +  ! scope="row"  Requirement 
 There is a unique function   There is a unique function  
−  <hask>u :: Empty > r</hask>  +  <br /><hask>u :: Empty > r</hask> 
 There is a unique function   There is a unique function  
−  <hask>u :: r > ()</hask>  +  <br /><hask>u :: r > ()</hask> 
 For any functions   For any functions  
−  <hask>f :: a > r</hask>  +  <br /><hask>f :: a > r</hask> 
−  <hask>g :: b > r</hask>  +  <br /><hask>g :: b > r</hask> 
there is a unique function  there is a unique function  
Line 27:  Line 58:  
such that:  such that:  
<hask>u . Left = f</hask>  <hask>u . Left = f</hask>  
−  <hask>u . Right = g</hask>  +  <br /><hask>u . Right = g</hask> 
 For any functions   For any functions  
−  <hask>f :: r > a</hask>  +  <br /><hask>f :: r > a</hask> 
−  <hask>g :: r > b</hask>  +  <br /><hask>g :: r > b</hask> 
there is a unique function  there is a unique function  
Line 37:  Line 68:  
such that:  such that:  
<hask>fst . u = f</hask>  <hask>fst . u = f</hask>  
−  <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"   +  ! scope="row"  Candidate 
 <hask>u1 r = case r of {}</hask>   <hask>u1 r = case r of {}</hask>  
 <hask>u1 _ = ()</hask>   <hask>u1 _ = ()</hask>  
 <hask>u1 (Left a) = f a</hask>   <hask>u1 (Left a) = f a</hask>  
−  <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 50:  Line 92:  
 <hask>r ~ ()</hask>   <hask>r ~ ()</hask>  
 <hask>r ~ ()</hask>   <hask>r ~ ()</hask>  
−  <hask>f _ = ()</hask>  +  <br /><hask>f _ = ()</hask> 
−  <hask>g _ = ()</hask>  +  <br /><hask>g _ = ()</hask> 
 <hask>r ~ ()</hask>   <hask>r ~ ()</hask>  
−  <hask>f _ = undefined</hask>  +  <br /><hask>f _ = undefined</hask> 
−  <hask>g _ = undefined</hask>  +  <br /><hask>g _ = undefined</hask> 
+   <hask>r ~ ()</hask>  
+  <br /><hask>f _ = ()</hask>  
+  <br /><hask>g _ = undefined</hask>  
    
! scope="row"  Alternative u  ! scope="row"  Alternative u  
Line 61:  Line 106:  
 <hask>u2 _ = ()</hask>   <hask>u2 _ = ()</hask>  
 <hask>u2 _ = undefined</hask>   <hask>u2 _ = undefined</hask>  
+    
    
! scope="row"  Difference  ! scope="row"  Difference  
−   <hask>  +   <hask>u1 undefined = undefined</hask> 
−  <hask>  +  <br /><hask>u2 undefined = ()</hask> 
−   <hask>  +   <hask>u1 _ = ()</hask> 
−  <hask>  +  <br /><hask>u2 _ = undefined</hask> 
−   <hask>  +   <hask>u1 undefined = undefined</hask> 
−  <hask>  +  <br /><hask>u2 undefined = ()</hask> 
−   <hask>  +   <hask>u1 _ = (undefined,undefined)</hask> 
−  <hask>  +  <br /><hask>u2 _ = undefined</hask> 
+   <hask>f _ = ()</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  
Line 79:  Line 128:  
}  }  
−  == Platonic '''Hask''' ==  +  == "Platonic" '''Hask''' == 
−  Because of these difficulties, Haskell developers tend to think in some subset of Haskell where types do not have  +  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, and instances of Functor and Monad really are endofunctors and monads. 
== Links ==  == Links == 
Latest revision as of 20:35, 13 September 2012
Hask is the category of Haskell types and functions.
The objects of Hask are Haskell types, and the morphisms from objectsA
B
A > B
A
id :: A > A
f
g
f . g = \x > f (g x)
Contents 
[edit] 1 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 () = ()
undef1 . id = undef2
f
g
f x = g x
x
undef1
undef2
[edit] 2 Hask is not Cartesian closed
Actual Hask does not have sums, products, or an initial object, and()
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
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 
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 _ = () 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 
f _ = () (fstP . u1) _ = undefined 
Result  FAIL  FAIL  FAIL  FAIL  FAIL 
[edit] 3 "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, and instances of Functor and Monad really are endofunctors and monads.