

Binding and continuations in ML
source link: https://dev.to/ducaale/binding-and-continuations-in-ml-5d3e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

If you have ever worked with Rust code before, you might have come across code like this
let a = {
let x = 5;
let y = 4;
let x = 7;
x + y
}
Enter fullscreen mode
Exit fullscreen mode
Considering your past experience with C-based language, several questions might spring to mind:
- Why is it legal to define the same variable multiple times?
- Why do blocks evaluate to the last expression in their bodies?
To get answers and more, It helps to look at a distant cousin to Rust.
Enter OCaml
NB: You can refer to https://learnxinyminutes.com/docs/ocaml/ for a quick crash course on OCaml's syntax
The same code in OCaml would be written like this:
let a =
let x = 5 in
let y = 4 in
let x = 7 in
x + y
Enter fullscreen mode
Exit fullscreen mode
Which could be viewed as syntactic sugar for the following:
(* bind is a Higher-order function that takes a value and
* a function, and returns the result of applying that value
* to the function
*)
let bind x fn = fn x;;
bind 5 (fun x ->
bind 6 (fun y ->
bind 7 (fun x ->
x + y
)
)
);;
(* Same code but without indentation.
* Notice how bind has replaced the assignment operator.
*)
bind 5 (fun x ->
bind 6 (fun y ->
bind 7 (fun x ->
x + y)));;
Enter fullscreen mode
Exit fullscreen mode
Because each line in our first OCaml snippet corresponds to a nested function in our second snippet, we can see why it is legal to define the same variables multiple times. It also shows why blocks are just expressions that evaluate to the last value in their bodies.
Killing all birds with one stone
Replacing the assignment operator with a customizable bind function is a powerful abstraction that lets us do all sorts of things that would otherwise have taken years to be implemented in the core language, including Rust's try operator and async/await syntax.
While I won't bore you with all the details of how this is done, I will leave you with some resources if you're interested in how deep the rabbit hole goes.
Recommend
-
45
Transcripts Pressler: Hi, my name is Ron Pressler, and I work at Oracle here in London as part of the Java platform group. That's the group that develops OpenJDK, designs...
-
6
Continuations From the Ground Up 06 June 2017 It’s difficult to learn functional programming without hearing about continuations. Often they’re mentioned while talking about boosting the performance of pure functiona...
-
5
An Objective-C Continuations Librarymikeash.com: just this guy, you know? An Objective-C Continuations Library by Mike Ash A couple of months ag...
-
5
Fetching data and binding it to the UI in the MAD skills seriesSecond episode in the Paging MAD skills series
-
4
Argument Binding and Introspection In PythonOctober 28th 2021 new story4Python decorators that m...
-
7
Building a Channel with Swift Concurrency Continuations Following Structured Concurrency was one of the best decisions Swift could have made when introducing
-
11
JVM continuations This library is yet another implementation of delimited continuations for JVM using bytecode instrumentation. It primarily implements resumable exceptions. And there is also a classical multi-prompt delimited continuatio...
-
6
continuations-and-reduction-semantics.md · GitHub Instantly share code, notes, and snippets.
-
9
Home ... ...
-
4
Friends of OpenJDK Today
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK