Categories for the working hacker

https://www.youtube.com/watch?v=gui_SE8rJUM

Another banger by Philip Wadler (the guy who added generics to Java). Though the essence of the talk is:


Questions

  1. Why can't the compiler permit arbitrarily big tuples by generating functions for them? If A×(B×C)(A×B)×C then why can't we just parse tuple definitions like x : (a,b,c) instead as x : (a,(b,c)) and take f: (a,b,c) -> d to mean g : (a,(b,c)) -> d where f (a,b,c) = g (a,(b,c)). Then if we write (a,b,c) we can take fst : (a,b,c) -> a to mean fst : (a,(b,c)) -> a, and snd : (a,b,c) -> b to mean snd : (a(b,c,)) -> b where snd abc = snd . snd $ abc. Generically, we can generate get_i : (a,b,...,n) -> t_i where t_1 = a, t_2 = b, ..., t_n = n and the compiler can yell at you if you don't satisfy 1i<n ?
    1. You can encode these yourself of course. Also, probably, 27 (as is the case in Haskell) is probably practically enough. However, it hurts only a little bit that the compiler doesn't recognise this. Perhaps with a preprocessor, this would be perfectly fine.
data Product a b =
	Pair { fst :: a, snd :: b}

-- shorthand
-- (a,b) = Product a b

-- Generated by compiler when it sees (a,b,c)
module T3 (..) where
to_pair_3 : (a,b,c) -> (a,(b,c))
to_pair_3 (a,b,c) = (a,(b,c))

get_1 : (a,b,c) -> a
get_1 = fst . to_pair_3

get_2 : (a,b,c) -> b
get_2 = fst . snd . to_pair_3

get_3 : (a,b,c) -> c
get_3 = snd . snd . to_pair_3

-- Generated by compiler when it sees (a,b,c,d)
module T4 (..) where

to_pair_4 : (a,b,c,d) -> (a,(b,(c,d)))
to_pair_4 (a,b,c,d) = (a,(b,(c,d)))

get_1 : (a,b,c,d) -> a
get_1 = fst . to_pair_4

get_2 : (a,b,c,d) -> b
get_2 = fst . snd . to_pair_4

get_3 : (a,b,c,d) -> c
get_3 = fst . snd . snd . to_pair_4

get_4 : (a,b,c,d) -> d
get_4 = snd . snd . snd . to_pair_4