Monads & Gonads Douglas Crockford Today Monads
JO
Published · 62 slides · 0 views
1 / 1
Description
Monads Gonads Douglas Crockford Today Monads Managing Asynchronicity Syntaxation Principles of Security Go To There and Back Again Quality Functional Programming Programming with functions. FORTRAN II (1958) FUNCTION name(parameters)
Related Topics
Share
Embed code
Download this presentation From Below
"Monads & Gonads Douglas Crockford Today Monads" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
01
Monads & Gonads Douglas Crockford<br>
02
Today Monads
Managing Asynchronicity
Syntaxation
Principles of Security
Go To There and Back Again
Quality<br>
Managing Asynchronicity
Syntaxation
Principles of Security
Go To There and Back Again
Quality<br>
03
Functional Programming Programming with functions.<br>
04
FORTRAN II (1958) FUNCTION name(parameters)
COMMON ...
name = expression
RETURN
END<br>
COMMON ...
name = expression
RETURN
END<br>
05
First Class FunctionsHigher Order FunctionsLexical Closure<br>
06
Pure Functional Programming More mathematical.<br>
07
Functions as maps<br>
08
MemoizationCaching<br>
09
Programming without side-effects. Remove assignment, loops (use recursion instead), freeze all array literals and object literals.
Remove Date and Math.random.<br>
Remove Date and Math.random.<br>
10
In the real world, everything changes. Immutability makes it hard to interact with the world.<br>
11
Monads A loophole in the function contract.<br>
12
In order to understand monads, you need to first learn Haskell and Category Theory.<br>
13
In order to understand monads, you need to first learn Haskell and Category Theory. In order to understand burritos, you must first learn Spanish.<br>
14
(M t) → (t → M u) → (M u) Not tonight, Josephine.<br>
15
…you must first learn JavaScript function unit(value)
function bind(monad, function (value))
All three functions return a monad.<br>
function bind(monad, function (value))
All three functions return a monad.<br>
16
Axioms bind(unit(value), f) ==== f(value)
bind(monad, unit) ==== monad
bind(bind(monad, f), g)
====
bind(monad, function (value) {
return bind(f(value), g);
})<br>
bind(monad, unit) ==== monad
bind(bind(monad, f), g)
====
bind(monad, function (value) {
return bind(f(value), g);
})<br>
17
bind(monad, func)monad.bind(func) The OO transform.<br>
18
function MONAD() {
return function unit(value) {
var monad = Object.create(null);
monad.bind = function (func) {
return func(value);
};
return monad;
};
}
Context Coloring<br>
return function unit(value) {
var monad = Object.create(null);
monad.bind = function (func) {
return func(value);
};
return monad;
};
}
Context Coloring<br>
19
function MONAD() {
return function unit(value) {
var monad = Object.create(null);
monad.bind = function (func) {
return func(value);
};
return monad;
};
}
Macroid<br>
return function unit(value) {
var monad = Object.create(null);
monad.bind = function (func) {
return func(value);
};
return monad;
};
}
Macroid<br>
20
function MONAD() {
return function unit(value) {
var monad = Object.create(null);
monad.bind = function (func) {
return func(value);
};
return monad;
};
}
var identity = MONAD();
var monad = identity("Hello world.");
monad.bind(alert);<br>
return function unit(value) {
var monad = Object.create(null);
monad.bind = function (func) {
return func(value);
};
return monad;
};
}
var identity = MONAD();
var monad = identity("Hello world.");
monad.bind(alert);<br>
21
Axioms unit(value).bind(f) ==== f(value)
monad.bind(unit) ==== monad
monad.bind(f).bind(g)
====
monad.bind(function (value) {
return f(value).bind(g);
})<br>
monad.bind(unit) ==== monad
monad.bind(f).bind(g)
====
monad.bind(function (value) {
return f(value).bind(g);
})<br>
22
Axioms bind(bind(monad, f), g)
monad.bind(f).bind(g)<br>
monad.bind(f).bind(g)<br>
23
The Ajax Monad monad.bind(f).bind(g)<br>
24
Interstate (2001) new Interform("text")
.moveTo(100, 100)
.setSize(400, 32)
.moveInside()
.setBgColor("pink")
.select()
.setZIndex(20000)
.on("escapekey", "erase");<br>
.moveTo(100, 100)
.setSize(400, 32)
.moveInside()
.setBgColor("pink")
.select()
.setZIndex(20000)
.on("escapekey", "erase");<br>
25
ADsafe (2007) var input = dom.q("input_text")
.on("enterkey", function (e) {
dom.q("#ROMAN_RESULT")
.value(roman(input
.getValue()));
input
.select();
})
.focus();<br>
.on("enterkey", function (e) {
dom.q("#ROMAN_RESULT")
.value(roman(input
.getValue()));
input
.select();
})
.focus();<br>
26
monad.bind(func) monad.bind(func, [ a, b, c ])monad.method() monad.method(a, b, c)<br>
27
monad.bind(func) monad.bind(func, [ a, b, c ])monad.method() monad.method(a, b, c)<br>
28
function MONAD() {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func) {
return func(value);
};
return monad;
}
return unit;
}<br>
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func) {
return func(value);
};
return monad;
}
return unit;
}<br>
29
function MONAD() {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func.apply(undefined,
[value].concat(Array.prototype
.slice.apply(args || [])));
};
return monad;
}
return unit;
}<br>
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func.apply(undefined,
[value].concat(Array.prototype
.slice.apply(args || [])));
};
return monad;
}
return unit;
}<br>
30
function MONAD() {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
return monad;
}
return unit;
}<br>
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
return monad;
}
return unit;
}<br>
31
function MONAD() {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
return monad;
}
unit.method = function (name, func) {
prototype[name] = func;
return unit;
};
return unit;
}<br>
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
return monad;
}
unit.method = function (name, func) {
prototype[name] = func;
return unit;
};
return unit;
}<br>
32
function MONAD() {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
return monad;
}
unit.lift = function (name, func) {
prototype[name] = function (...args) {
return unit(this.bind(func, args));
};
return unit;
};
return unit;
}<br>
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
return monad;
}
unit.lift = function (name, func) {
prototype[name] = function (...args) {
return unit(this.bind(func, args));
};
return unit;
};
return unit;
}<br>
33
var ajax = MONAD()
.lift("alert", alert);
var monad = ajax("Hello world.");
monad.alert();<br>
.lift("alert", alert);
var monad = ajax("Hello world.");
monad.alert();<br>
34
null Null Pointer Exception<br>
35
Maybe NaN<br>
36
function MONAD(modifier) {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
if (typeof modifier === "function") {
modifier(monad, value);
}
return monad;
}
return unit;
}<br>
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function (func, args) {
return func(value, ...args));
};
if (typeof modifier === "function") {
modifier(monad, value);
}
return monad;
}
return unit;
}<br>
37
var maybe = MONAD(function (monad, value) {
if (value === null || value === undefined) {
monad.is_null = true;
monad.bind = function () {
return monad;
};
}
});
var monad = maybe(null);
monad.bind(alert);<br>
if (value === null || value === undefined) {
monad.is_null = true;
monad.bind = function () {
return monad;
};
}
});
var monad = maybe(null);
monad.bind(alert);<br>
38
Our Friend the Monad The Identity Monad
The Ajax Monad
The Maybe Monad<br>
The Ajax Monad
The Maybe Monad<br>
39
Concurrency Threads are evil.<br>
40
Turn Based Processing Single-threaded. Race free. Deadlock free.
The Law of Turns: Never wait. Never block. Finish fast.
Events. Message passing. Threads. Mutexs.
Web browsers.
Most UI frameworks.
Servers: Elko, Twisted, Nodejs.
Asynchronicity can be hard to manage.<br>
The Law of Turns: Never wait. Never block. Finish fast.
Events. Message passing. Threads. Mutexs.
Web browsers.
Most UI frameworks.
Servers: Elko, Twisted, Nodejs.
Asynchronicity can be hard to manage.<br>
41
Promises Promises are an excellent mechanism for managing asynchronicity.
A promise is an object that represents a possible future value.
Every promise has a corresponding resolver that is used to ultimately assign a value to the promise.
A promise can have one of three states: "kept", "broken", or "pending".<br>
A promise is an object that represents a possible future value.
Every promise has a corresponding resolver that is used to ultimately assign a value to the promise.
A promise can have one of three states: "kept", "broken", or "pending".<br>
42
Promises A promise is an event generator. It fires its event when the value of the promise is ultimately known.
At any time after the making the promise, event handling functions can be registered with the promise, which will be called in order with the promise’s value when it is known.<br>
At any time after the making the promise, event handling functions can be registered with the promise, which will be called in order with the promise’s value when it is known.<br>
43
Promises A promise can accept functions that will be called with the value once the promise has been kept or broken.
promise.when(success, failure) returns another promise for the result of your success function.<br>
promise.when(success, failure) returns another promise for the result of your success function.<br>
44
Make a vow var my_vow = VOW.make();
.keep(value)
.break(reason)
.promise
.when(kept, broken)<br>
.keep(value)
.break(reason)
.promise
.when(kept, broken)<br>
45
Filesystem API read_file(name)
.when(function success(string) {
...
}, failure);<br>
.when(function success(string) {
...
}, failure);<br>
46
Exceptions Exceptions modify the flow of control by unwinding the state.
In a turn based system, the stack is empty at the end of every turn.
Exceptions cannot be delivered across turns.
Broken promises can.<br>
In a turn based system, the stack is empty at the end of every turn.
Exceptions cannot be delivered across turns.
Broken promises can.<br>
47
Breakage flows to the end my_promise
.when(success_a)
.when(success_b)
.when(success_c, failure);
success_a gets the value of my_promise
success_b gets the value of success_a
success_c gets the value of success_b
unless any promise breaks: failure gets the reason<br>
.when(success_a)
.when(success_b)
.when(success_c, failure);
success_a gets the value of my_promise
success_b gets the value of success_a
success_c gets the value of success_b
unless any promise breaks: failure gets the reason<br>
48
Composition f()
.when(function (f_value) {
return g(f_value);
})
.when(function (g_value) {
...
})
====
f()
.when(function (f_value) {
return g(f_value)
.when(function (g_value) {
...
});
})<br>
.when(function (f_value) {
return g(f_value);
})
.when(function (g_value) {
...
})
====
f()
.when(function (f_value) {
return g(f_value)
.when(function (g_value) {
...
});
})<br>
49
A promise is a monad The value is not known when the monad is made.
Each promise is linked to two resolver functions, keep and break, that determine the promise’s success and value.
when can take two functions, bind only one.<br>
Each promise is linked to two resolver functions, keep and break, that determine the promise’s success and value.
when can take two functions, bind only one.<br>
50
var VOW = (function () {
// function enlighten...
return {
make: function make() {
...
}
};
})();<br>
// function enlighten...
return {
make: function make() {
...
}
};
})();<br>
51
var VOW = (function () {
// function enlighten...
return {
make: function make() {
...
}
};
})(); Dog balls<br>
// function enlighten...
return {
make: function make() {
...
}
};
})(); Dog balls<br>
52
var VOW = (function () {
// function enlighten...
return {
make: function make() {
...
}
};
}());<br>
// function enlighten...
return {
make: function make() {
...
}
};
}());<br>
53
make: function make() {
var breakers = [], fate, keepers = [], status = "pending";
// function enqueue here...
// function herald here...
return {
break: function (reason) {
herald("broken", reason, breakers);
},
keep: function (value) {
herald("kept", value, keepers);
},
promise: {
...
}
};
}<br>
var breakers = [], fate, keepers = [], status = "pending";
// function enqueue here...
// function herald here...
return {
break: function (reason) {
herald("broken", reason, breakers);
},
keep: function (value) {
herald("kept", value, keepers);
},
promise: {
...
}
};
}<br>
54
promise: {
is_promise: true,
when: function (kept, broken) {
var vow = make();
switch (status) {
case "pending":
enqueue("keep", kept, vow);
enqueue("break", broken, vow);
break;
case "kept":
enqueue("keep", kept, vow);
enlighten(keepers, fate);
break;
case "broken":
enqueue("break", broken, vow);
enlighten(breakers, fate);
break;
}
return vow.promise;
}
}<br>
is_promise: true,
when: function (kept, broken) {
var vow = make();
switch (status) {
case "pending":
enqueue("keep", kept, vow);
enqueue("break", broken, vow);
break;
case "kept":
enqueue("keep", kept, vow);
enlighten(keepers, fate);
break;
case "broken":
enqueue("break", broken, vow);
enlighten(breakers, fate);
break;
}
return vow.promise;
}
}<br>
55
make: function make() {
var breakers = [], fate, keepers = [], status = "pending";
// function enqueue here...
// function herald here...
return {
break: function (reason) {
herald("broken", reason, breakers);
},
keep: function (value) {
herald("kept", value, keepers);
},
promise: {
...
}
};
}<br>
var breakers = [], fate, keepers = [], status = "pending";
// function enqueue here...
// function herald here...
return {
break: function (reason) {
herald("broken", reason, breakers);
},
keep: function (value) {
herald("kept", value, keepers);
},
promise: {
...
}
};
}<br>
56
function enqueue(resolution, func, vow) {
var queue = resolution === "keep"
? keepers
: breakers;
queue[queue.length] = typeof func !== "function"
? vow[resolution]
: function (value) {
try {
var result = func(value);
if (result &&
result.is_promise === true) {
result.when(vow.keep,
vow["break"]);
} else {
vow.keep(result);
}
} catch (e) {
vow["break"](e);
}
};
}<br>
var queue = resolution === "keep"
? keepers
: breakers;
queue[queue.length] = typeof func !== "function"
? vow[resolution]
: function (value) {
try {
var result = func(value);
if (result &&
result.is_promise === true) {
result.when(vow.keep,
vow["break"]);
} else {
vow.keep(result);
}
} catch (e) {
vow["break"](e);
}
};
}<br>
57
function herald(state, value, queue) {
if (status !== "pending") {
throw "overpromise";
}
fate = value;
status = state;
enlighten(queue, fate);
keepers.length = 0;
breakers.length = 0;
}<br>
if (status !== "pending") {
throw "overpromise";
}
fate = value;
status = state;
enlighten(queue, fate);
keepers.length = 0;
breakers.length = 0;
}<br>
58
function enlighten(queue, fate) {
queue.forEach(function (func) {
setImmediate(func, fate);
});
}
https://github.com/douglascrockford/monad<br>
queue.forEach(function (func) {
setImmediate(func, fate);
});
}
https://github.com/douglascrockford/monad<br>
59
var VOW = (function () {
function enlighten(queue, fate) {
queue.forEach(function (func) {
setImmediate(func, fate);
});
}
return {
make: function make() {
var breakers = [], fate, keepers = [], status = "pending";
function enqueue(resolution, func, vow) {
var queue = resolution === "keep" ? keepers : breakers;
queue[queue.length] = typeof func !== "function" ? vow[resolution] : function (value) {
try {
var result = func(value);
if (result && result.is_promise === true) {
result.when(vow.keep, vow["break"]);
} else {
vow.keep(result);
}
} catch (e) {
vow["break"](e);
}
};
}
function herald(state, value, queue) {
if (status !== "pending") {
throw "overpromise";
}
fate = value;
status = state;
enlighten(queue, fate);
keepers.length = 0;
breakers.length = 0;
}
return {
break: function (reason) {
herald("broken", reason, breakers);
},
keep: function (value) {
herald("kept", value, keepers);
},
promise: {
is_promise: true,
when: function (kept, broken) {
var vow = make();
switch (status) {
case "pending":
enqueue("keep", kept, vow);
enqueue("break", broken, vow);
break;
case "kept":
enqueue("keep", kept, vow);
enlighten(keepers, fate);
break;
case "broken":
enqueue("break", broken, vow);
enlighten(breakers, fate);
break;
}
return vow.promise;
}
}
};
}
};
}());<br>
function enlighten(queue, fate) {
queue.forEach(function (func) {
setImmediate(func, fate);
});
}
return {
make: function make() {
var breakers = [], fate, keepers = [], status = "pending";
function enqueue(resolution, func, vow) {
var queue = resolution === "keep" ? keepers : breakers;
queue[queue.length] = typeof func !== "function" ? vow[resolution] : function (value) {
try {
var result = func(value);
if (result && result.is_promise === true) {
result.when(vow.keep, vow["break"]);
} else {
vow.keep(result);
}
} catch (e) {
vow["break"](e);
}
};
}
function herald(state, value, queue) {
if (status !== "pending") {
throw "overpromise";
}
fate = value;
status = state;
enlighten(queue, fate);
keepers.length = 0;
breakers.length = 0;
}
return {
break: function (reason) {
herald("broken", reason, breakers);
},
keep: function (value) {
herald("kept", value, keepers);
},
promise: {
is_promise: true,
when: function (kept, broken) {
var vow = make();
switch (status) {
case "pending":
enqueue("keep", kept, vow);
enqueue("break", broken, vow);
break;
case "kept":
enqueue("keep", kept, vow);
enlighten(keepers, fate);
break;
case "broken":
enqueue("break", broken, vow);
enlighten(breakers, fate);
break;
}
return vow.promise;
}
}
};
}
};
}());<br>
60
Our Friend the Monad The Identity Monad
The Ajax Monad
The Maybe Monad
The Promise Monad<br>
The Ajax Monad
The Maybe Monad
The Promise Monad<br>
61
;<br>
62
Further Viewing Carl Hewitt. The Actor Model (everything you wanted to know, but were afraid to ask)
http://channel9.msdn.com/Shows/Going+Deep/Hewitt-Meijer-and-Szyperski-The-Actor-Model-everything-you-wanted-to-know-but-were-afraid-to-ask
Mark Miller. Secure Distributed Programming with Object-capabilities in JavaScript
http://www.youtube.com/watch?v=w9hHHvhZ_HY
http://www.youtube.com/watch?v=oBqeDYETXME<br>
http://channel9.msdn.com/Shows/Going+Deep/Hewitt-Meijer-and-Szyperski-The-Actor-Model-everything-you-wanted-to-know-but-were-afraid-to-ask
Mark Miller. Secure Distributed Programming with Object-capabilities in JavaScript
http://www.youtube.com/watch?v=w9hHHvhZ_HY
http://www.youtube.com/watch?v=oBqeDYETXME<br>