Before we get a nice theory of thinnings, we need a nice theory of lists. This i...
Conor Titania Mc Bride @pigworker@types.pl
Before we get a nice theory of thinnings, we need a nice theory of lists. This is not a new trick, but it's still not so well known.
We represent values in list types not as ordinary inductive terms in some meta-level syntax tree, but as meta-level lists of list chunks. A list chunk is either a singleton element or a whole list computation that has got stuck. Nil gives the empty list of chunks, Cons conses one element chunk on the front. Append is not defined via the recursor, but rather appends these chunk lists at the meta-level, and so never gets stuck! The monoid laws hold on the nose.
The list recursor turns these list values into compositions, either of singleton steps or of stuck recursions. This composition is then applied to the base case.
Sensible people make
foldr :: (a -> b -> b) -> ([a] -> b -> b)
foldr step [] = id
foldr step (x : xs) = step x . foldr step xs
(or
foldr = foldMap `ala` Endo
, if you're me), and make
foldr step (xs ++ ys) = foldr step xs . foldr step ys
hold on the nose.
The dependently typed version is a little gnarlier but it's the same idea.