
Defunctionalization and Freyd’s Theorem - ingve
https://bartoszmilewski.com/2020/08/03/defunctionalization-and-freyds-theorem/
======
omginternets
These posts always seem so damn interesting, but I get lost very early on. In
this case it was at "Product is functorial in both arguments so, in
particular, we can define a functor".

Any suggestions for introductory material that might help me appreciate
Milewski's writings?

~~~
derekmcloughlin
His own introduction to category theory for programmers is good:

[https://bartoszmilewski.com/2014/10/28/category-theory-
for-p...](https://bartoszmilewski.com/2014/10/28/category-theory-for-
programmers-the-preface/)

~~~
kerkeslager
This feels like the category theory version of the Feynman Lectures.

------
gsg
Interesting if somewhat opaque. I'm familiar with defunctionalisation as an
alternative to closure conversion in whole program compilers and as a
description of how data types are derived from the lambda calculus - never
seen a category theory take on the idea. I don't seem to be able to understand
the category theory part though.

The suggestion to use a combination of CPS + defunctionalisation to serialise
closures is notable, since that pair of transformations gives a fairly close
correspondence between a subset of the lambda calculus (plus some primitives)
and abstract machine code. Some of the old-school Scheme compilers used CPS as
a low-level IR for that reason.

------
Twisol
> When you’re implementing a function, you are, in a sense, telling _apply_
> what to do with it – what code to execute. You’re implementing individual
> chunks of _apply_. These chunks are usually scattered all over your program,
> sometimes anonymously in the form of lambdas.

I feel like this is a much deeper philosophical point worth pausing and
pondering. (It's also _very_ suggestive as to the whole point of
defunctionalization -- nice foreshadowing!)

~~~
mlazos
I’ll need to reread some of his posts, but I don’t find this particularly
enlightening. Apply has always been pure variable substitution in my mind.
From this point of view, of course apply is dependent on the code snippet
you’re supplying, it’s the code that’s getting “modified” by the substitution.

~~~
Twisol
The point I took from it is that "apply" is normally given by your programming
system, and its cases are spread out over your whole program. but there's no
reason you can't build your own "apply" on your own function-like type. (The
`Expr = Lit Int | Add Expr Expr` type is probably the best-known example.)

Functions happen to be things that your PL provides natively.
Defunctionalization is all about capturing a piece of the global `apply` for
functions, and translating it into an `apply` on a custom data type.

