r/programming • u/alexeyr • Jan 16 '20
Defunctionalization: Everybody Does It, Nobody Talks About It
https://blog.sigplan.org/2019/12/30/defunctionalization-everybody-does-it-nobody-talks-about-it/13
u/joe462 Jan 16 '20
Applying this technique on type-level functions rather than term-level ones allows you do to do partial applications and currying which Haskell wont ordinarily allow. It's demonstrated here by the singletons package.
0
Jan 16 '20
[deleted]
14
13
u/Y_Less Jan 16 '20
You don't, for several reasons:
Mutli-line editing.
Commenting hotkeys.
Haskell has block comments:
{- This is a block comment. -}
Unlike many languages, their block comments can be nested:
{- This is commented. {- So is this. -} And so still is this. -}
2
u/IceSentry Jan 16 '20
Out of curiosity, why would nesting comments be useful? Is it for folding regions un your editor?
9
Jan 16 '20
For one it lets you use a comment block to comment out a section of code that has a comment block in it.
4
Jan 16 '20
That doesn't work in general:
putStrLn "this is not a comment: {-""Nested comments" really aren't. The outside of a comment is code; the inside is plain text. You can't meaningfully "nest" comments.
(You could argue that OCaml can, but I would argue that OCaml doesn't have real comments because it lexes the contents of its would-be comments, so you can get a tokenizer error in what you thought was a comment. You do get real nesting, though.)
2
u/Herbstein Jan 16 '20
Have you ever written a lexer/parser? Supporting nested comments does not require looking at the contents of the comment at all (other than looking for the start and end sequences).
4
Jan 16 '20
other than looking for the start and end sequences
That's the whole point. If you only scan for comment markers, you're going to ignore all other constructs (such as string literals) that would have a different meaning in normal code. See my
"{-"example above.12
u/IceSentry Jan 16 '20
Imagine having to write two symbols like
//in front of every comment.Imagine using a keybind to toggle comments that knows the context of thw language and uses the appropriate symbol automatically.
6
15
u/csjerk Jan 16 '20
In other threads over the last week, the idea keeps coming up of "engineers who are too Junior to know that the abstraction they added to the system causes more harm than good, because it complicates everything far beyond a reasonable level"
This whole post is exactly that. It's PhD /r/iamverysmart material, and technically he's correct, but dear lord, never do this nonsense in an actual system.
19
u/horsesAreSpheres Jan 16 '20
I originally thought this. Of course this is a useless optimization that makes the code harder to read and change.
But, after reading the article a second time, I realized the author doesn't advocate for defunctionalization in every situation. It's only to support the very specific design requirement of needing to send functions between different systems.
If you had a normal function
\x -> 5 < x and x < 10, you couldn't print this function. However, when at function is stored as a data type such asAnd (GreaterThan 5) (LessThan 10)it can be printed or sent between systems.So, defunctionalization is only used when you have to reason about the code. It's a way to manually create an AST that you can reason about during run time.
→ More replies (1)13
u/mode_2 Jan 16 '20
The author literally shows an example of defunctionalisation being used in a real system at the end. I really don't see how this is a complex abstraction at all, or even an abstraction. It's just a different way of writing the same code with certain benefits.
3
u/devraj7 Jan 16 '20
The above code can be automatically derived from Version 1.
How?!?
Version 1 accepts a higher order function which can be anything. How can you automatically derive Version 2 from it?
And even if you can, you are still going to fundamentally restrict the applicability of the first version.
12
u/Faucelme Jan 16 '20
Defunctionalization is a whole-program transformation. You examine the program looking for all the functions that are passed around as arguments, and create the datatype according to that. This means it's difficult to work "incrementally": you need to known the whole program.
1
u/IceSentry Jan 16 '20
You could start with a simple datatype and add more cases when needed. It's a little bit more work, but I don't think it would be particularly harder to work incrementally.
-6
u/earthboundkid Jan 16 '20
The Hacker News example is a total security bug as presented. You're running arbitrary code on your server based on something in a hidden input field? Seems like a great way to get pwned unless you've signed the field.
13
u/cowinabadplace Jan 16 '20
That's actually why it's an example of the defunctionalization. It's not arbitrary code because you've converted what would be "run this arbitrary next step" to "the defined next steps are A,B,C; select one of them; run them with this input"
6
0
Jan 16 '20
[deleted]
87
u/PeksyTiger Jan 16 '20
He just talks about converting functions wich recieve other functions as parameters to functions which recieve a data structure as parameter.
Not too differant from a "command" design pattern.
3
u/Drisku11 Jan 16 '20 edited Jan 16 '20
There's actually a fascinating idea that he gets at when discussing the Hacker News login scenario. If you think of client-server interactions through the lens of continuation passing, then you could imagine building a framework that allows you to do
async/await, but seamlessly combining multiple execution contexts by sending the defunctionalized continuation when changing contexts. In particular, two contexts could be a frontend/backend one so you could have a unified codebase for a webapp with async interactions.So with Scala/Scala.js, you could perhaps build something to let you do something like:
def submitPost(contents: String, maybeAlreadyLoggedInUser: Option[User]): Future[Client,Confirmation] = for { user <- maybeAlreadyLoggedInUser match { case Some(u) => u.toFuture case None => for { authInfo <- getAuthInfoModal // requires implicit ExecutionContext[Client] user <- login(authInfo) // requires implicit ExecutionContext[Server] } yield user post <- createPost(user, contents) // requires implicit ExecutionContext[Server] confirmation <- confirmSubmission(post) // requires implicit ExecutionContext[Client] } yield confirmationYou could require an
implicit ExecutionContextSwitcher[Client,Server]that takes care of serialization and message signing in a way where the server doesn't necessarily need to keep its state while waiting for a client response, but allows you toflatMapfrom aFuture[Client,A]to aFuture[Server,B]and vice-versa.1
u/ScientificBeastMode Jan 16 '20
Reasons for doing this vary, though. In some cases, this allows for better ergonomics with the type system (e.g. dynamic programming with Haskell or OCaml), but it also matters for performance in some cases.
1
u/EternityForest Jan 16 '20
I thought that might be what was going on! It's a bit confusing because I virtually never see anything this advanced in IRL code that I work on. I guess it's a big data/scaling/HPC thing.
0
Jan 16 '20
[deleted]
54
u/JeffJankowski Jan 16 '20
JavaScript tends to do this a lot, as well as most of the functional languages out there.
0
Jan 16 '20
[deleted]
22
4
u/lenkite1 Jan 17 '20
Not surprising that your brain stopped functioning. What else can happen after one reads an article with the term defunctionalization. ?
0
Jan 16 '20
[deleted]
5
u/shawntco Jan 16 '20
Silly question - what does "first-class object" mean exactly? And is there such thing as "second/third/etc.-class objects"?
21
u/nxsynonym Jan 16 '20
They're also known as "first class citizens" and typically mean that they are entities that support the same operations as all other entities.
In js functions are considered first class citizens because they can be used as arguments for other functions, modified, and assigned to variables (like strings, numbers, or objects).
5
Jan 16 '20
It means that functions can be treated as any other object. In C# for example functions are not first-class objects, they need a wrapper (
delegateor the more programmer friendlyAction(method without return) orFunc<T>(method with return value)). I don't know if there are any "levels of class" beyond first..5
Jan 16 '20
First-class values can be stored in variables, passed to functions, returned from functions, etc.
In most programming languages, numbers are first-class values.
Functions as first-class values are less common (but more prevalent nowadays with functional programming patterns on the rise). For example, functions (or methods) are not first class in Java: You cannot pass a method as an argument to another method, for example.
(Also, a function that returns or takes as argument another function is called a "higher-order function". All other functions are called "first-order functions", for extra confusion.)
1
u/HINDBRAIN Jan 16 '20
are not first class in Java: You cannot pass a method as an argument to another method, for example.
java.util.function.Function<Integer, Boolean> f = x -> true;
→ More replies (1)5
u/rabidcow Jan 16 '20
No, this wraps the function in an object.
I'm not sure even objects are first-class in Java, though. The only actual values are references or primitives.
→ More replies (7)3
u/NoMoreNicksLeft Jan 16 '20
Second class objects have to use the side door like the rest of the servants.
0
Jan 16 '20
[deleted]
12
u/skooterM Jan 16 '20
Java is still second class since its "Functional" interface is OOP masquerading as functional.
1
10
u/PeksyTiger Jan 16 '20
Almost all languages use function pointers or lambdas for map, filter, sorting and event handling.
10
Jan 16 '20
Notable historic exceptions being C# and Java, though both have that support today. In C# sorting was done by passing a class that implemented the IComparable<T> interface, likewise in Java. Java didn't have references to functions (or methods) at all, so event handling was done implementing a event handler interface, so you'd see stuff like
SomeWindow implements
MouseEvent,
KeyboardEvent,
PropertyChangedEvent,
MediaPropagatingOilIndustryAstroturfingAndSmearEvent5
u/jesseschalken Jan 16 '20
There's no functional difference between passing an object implementing an interface and passing a lambda or record of lambdas. You can translate one to the other without losing types, and in fact that's what most compilers for OO languages like C++, Kotlin, Java and Swift do to support lambda syntax.
3
u/Splanky222 Jan 16 '20
C++ lambdas don't have any kind of interface attached, unless you mean that "struct with operator()() defined" implements some sort of implied "Callable" interface
6
1
Jan 16 '20
Here's something I find interesting: Perl can go both ways.
You can implement a class and overload the
()(function call) operator, which lets your objects masquerade as functions (similar to C++).But you can also create a closure and bind a vtable to it, which effectively makes it an object (you can call methods on it).
2
u/jerf Jan 16 '20
In addition to the many fine replies, one of the major points of the article is that many functions you encounter that don't take function parameters are themselves "defunctionalized". An obvious case is any function you've ever run an SQL query in; you don't pass in a function to the SQL server, you pass in a super-duper "defunctionalized" parameter. When you have a function that lets you get an array of things back from a data structure according to some parameters you pass, rather than by passing a closure, it's a defunctionalized function. It encourages a point of view where you see through the question of whether you pass a function reference or a data specification of things to do in the function to see that those are just two particular manifestations of an underlying functionality.
You can write entire programs that never pass any functions around, where everything is completely defunctionalized. Almost every C program works like this already since you don't have closures anyhow.
1
u/StabbyPants Jan 16 '20
common practice is to pass an options block into a function/class where a number of the options are optional. the alternative is to have a million parameters in some order or require some builder looking pattern. when you can write literal maps, it ends up being a lot easier to read
4
u/CatMtKing Jan 16 '20 edited Jan 17 '20
There’s two concepts here: defunctionalizing and refunctionalizing.
Defunctionalization is like enumerating a list of functions that you actually use. The benefit of doing so is to allow that function to be passed as data; like sending a command to another machine, and the implementation of the command is stored in that machine. It’s like going from a generalized case to specific cases / from continuous to discrete.
Refunctionalization is the inverse - like finding the generalization. Both have useful applications, and the takeaway - I think - is that being able to go back and forth easily between the generalization and discrete cases is powerful.
0
Jan 16 '20
[deleted]
5
u/CatMtKing Jan 16 '20
Well the point (I think) is that it is used all the time whether you’re aware of it or not. The usefulness of the abstraction is in identifying it to simplify the complexity of the problem. It’s like seeing 10 different functions as 1 function that can act 10 different ways.
-3
u/_101010 Jan 17 '20
Stop calling JS a functional programming language. It's not. It's an insult to languages like Haskell.
2
u/przemo_li Jan 17 '20
Imagine two versions of the same algorithm one uses Strategy Patterns (and thus is fulfilling OCP nicely), and the other does the same but do not use Strategy Pattern, but instead relay on flags to signal which behavior should be selected.
If you turn Strategy version into non-strategy version you do defunctionalization.
It's nice angle to explain how non-strategy versions are not the best choice (most of the time).
4
0
Jan 16 '20
[deleted]
9
u/mode_2 Jan 16 '20
How can it mean anything? Whats a better name? Can't all words mean anything unless you understand them?
0
Jan 16 '20
[deleted]
13
u/notfancy Jan 16 '20
It's the dual of code-is-data: when defunctionalizing, you let data denote code.
If you're a Lisp fan you might enjoy Danvy's seminal papers on using defunctionalization to compile Scheme to efficient machine code.
5
u/mode_2 Jan 16 '20
It is a technique which can be used in languages which support that concept. But 'code is data' is far more general than this specific idea.
2
0
Jan 16 '20
[deleted]
3
u/mode_2 Jan 16 '20
What could you possibly mean?
0
Jan 16 '20
[deleted]
5
u/mode_2 Jan 16 '20
That they start by assuming that the "right" way is obviously functional programming
No they don't, they assume that the "right" way to specify custom behaviour by parameter is passing a function. That is what I would expect in any modern language, even C can pass function pointers.
and that when one does not use higher-order functions it's a sort of fall from grace of the functional heavens, thus calling this "defunctionalization".
Not really, its just a compound word to describe the removal of functions and their replacement with something else.
This is not "defunctionalization" because very few people start with higher-order functions, only to remove them later.
Perhaps in the 90s? Its still defunctionalisation anyway, because thats what its doing and is defined as, regardless of whether you think it only applies to 'very few people'.
1
u/epicwisdom Jan 16 '20
They're using function in the more broad sense that is available in every general-purpose programming language, not just FP.
1
u/sionescu Jan 16 '20
I don't know where you get that from. The post speaks very explicitly about higher-order functions.
2
u/epicwisdom Jan 16 '20
Which can be implemented in almost any popular language that would not be considered FP, including C, C++, Java, and JS.
21
u/EternityForest Jan 16 '20
"For instance, I teach programmers to tune out the noise of refactoring catalogs and learn how most familiar refactorings instead follow from a few primordial algebraic laws"
It seems like FP programmers have a really high tolerance for thinking about multiple levels of abstraction at the same time.
Doing algebra while also thinking about readability (Maybe even while also "programming" your text editor, if you use Emacs), sounds like exactly the kind of "Everything affects more than just what you changed" scenario that makes math and mechanical engineering harder than everyday coding.