Skip to content

Commit 1d5ee28

Browse files
author
Steve Krouse
committed
reorganize dctp second section
1 parent 7cab41a commit 1d5ee28

File tree

1 file changed

+72
-45
lines changed

1 file changed

+72
-45
lines changed

drafts/dctp.md

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ SVGs retain all the relevant info until the very last moment when the computer f
6161

6262
#### What is a graphic?
6363

64-
Let's get a bit more mathematical with our definitions. A graphic is something that relates x- and y- coordinates to colors. You give it an x-y pair; it gives you a color.
64+
Let's get a bit more mathematical[[1](#1)] with our definitions. A graphic is something that relates x- and y- coordinates to colors. You give it an x-y pair; it gives you a color.
6565

6666
Here's the difference between bitmaps and SVGs: what kind of numbers make up the x-y pairs?
6767

@@ -87,6 +87,7 @@ const example1 = (x, y) => red
8787

8888
<iframe height="300px" width="100%" frameborder="none" scrolling="no" src="https://stevekrouse.com/embed-jsbin/?id=pinitox"></iframe>
8989
_[Example 1 GPU Code](https://jsbin.com/pinitox/edit?js,output)_
90+
[TODO convert all these to GIFs]
9091

9192
Here's another: a green circle with radius 50, centered at (100, 150).
9293

@@ -187,61 +188,74 @@ const example6 = layer([
187188
[Example 6 GPU Code](https://jsbin.com/viropan/edit?js,output)
188189
189190
190-
<!--## Interactivity-->
191+
## Interactivity
191192
192-
<!--### Behaviors and Events-->
193+
The above animations are neat, but they are static in the sense that movies are static: no interactivity. We want to be able to press, click, type, and play with our screens. Let's go deeper into the DCTP rabbithole.
193194
194-
<!--There are two main types in DCTP: Behaviors and Events.-->
195+
### Behaviors and Events
195196
196-
<!--![](https://camo.githubusercontent.com/9b8fca9342d56465fc9536efdf8b26e182958a2f/68747470733a2f2f7261776769742e636f6d2f66756e6b69612f686172656163746976652f6d61737465722f666967757265732f6265686176696f722e737667)-->
197+
The two main types in DCTP are Behaviors and Events.
197198
198-
<!--_Graphic of Behaviors from the [Hareactive documentation](https://github.com/funkia/hareactive)_-->
199+
![](https://camo.githubusercontent.com/9b8fca9342d56465fc9536efdf8b26e182958a2f/68747470733a2f2f7261776769742e636f6d2f66756e6b69612f686172656163746976652f6d61737465722f666967757265732f6265686176696f722e737667)
199200
200-
<!--Behaviors are continuous functions of time: -->
201+
_Graphic of Behaviors from the [Hareactive documentation](https://github.com/funkia/hareactive)_
201202
202-
<!--* the animation diagrams from above-->
203-
<!--* the x-y-position of your mouse-->
204-
<!--* the constant function `t -> 1`-->
205-
<!--* the Boolean valued function: `t -> t > 10`-->
203+
Behaviors are continuous functions of time:
206204
207-
<!--![](https://camo.githubusercontent.com/bdbe25d1c8cf3490714fcf8d90039be58afcda35/68747470733a2f2f7261776769742e636f6d2f66756e6b69612f686172656163746976652f6d61737465722f666967757265732f73747265616d2e737667)-->
205+
* the animation diagrams from above
206+
* the x-y-position of your mouse
207+
* the constant function `t -> 1`
208+
* the Boolean valued function: `t -> t > 10`
208209
209-
<!--_Graphic of EVents from the [Hareactive documentation](https://github.com/funkia/hareactive)_-->
210+
![](https://camo.githubusercontent.com/bdbe25d1c8cf3490714fcf8d90039be58afcda35/68747470733a2f2f7261776769742e636f6d2f66756e6b69612f686172656163746976652f6d61737465722f666967757265732f73747265616d2e737667)
210211
211-
<!--Events are discrete occurrences in time:-->
212+
_Graphic of EVents from the [Hareactive documentation](https://github.com/funkia/hareactive)_
212213
213-
<!--* the click event of your mouse-->
214-
<!--* key press events of your keyboard-->
215-
<!--* an interval of 3 seconds-->
214+
Events are discrete occurrences in time:
216215
216+
* the click event of your mouse
217+
* key press event of your keyboard
218+
* an interval of 3 seconds
217219
220+
### DCTP & HTML
218221
222+
The above animation code was purposefully simplisitic: we literally used pure, mathmatical functions. This works fine on the canvas, especially with the help of the GPU. However, functions from `x` and `y` to `Color` is likely not the way we want to build HTML-based user interfaces. Instead we will use the (experimental) [Turbine](https://github.com/funkia/turbine) framework, built on top of the [Hareactive](https://github.com/funkia/hareactive) DCTP library.
219223
220-
## Denotational Semantics
224+
Let's start with a simple counter button:
221225
222-
Now we know the CT (Continuous Time) of DCTP, but what of the D (Denotative)?
223226
224-
We've already been doing it. The original name for denotational semantics was "mathematical semantics". It was pioneered by Chris Strachey and Dana Scott in the early 1970s. It is an approach to model a programming language with mathematical objects. So above, when we modeled a graphic as a function from `x` and `y`, and then `t`, to `Color`, that was denotational semantics.
227+
### Cyclical Flows
225228
226-
In his 1977 Turing Award Lecture, [Can Programming Be Liberated from the von Neumann Style? A functional style and its algebra of programs](http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf), John Backus explains how denotational semantics uncovers the hidden complexities in imperative, von-Neumann-based languages:
229+
What if we want the button to count inside itself?
227230
228-
> the complexity of the language is mirrored in the complexity of the description, which is a bewildering collection of productions, domains, functions, and equations that is only slightly more helpful in proving facts about programs than the reference manual of the language...
231+
### Higher-order Flows
229232
230-
In other words, most programming languages lack powerful mathematical properties. For example, the lack of equational reasoning (being able to replace expressions with equal ones) makes it difficult to reason about such programs, let alone *prove* things about them. Additionally, lacking in mathematical properties prevents composability and modularity of programs. [TODO maybe join this section with the composability and modularity from the section above?]
233+
What if we want a variable number of counters? [maybe simpler]
231234
232-
### "Why program with continuous time?"
235+
### Higher-order & Cyclical Flows
233236
234-
Of course, for the computer to render the above graphics on the screen, it must at some point convert the continuous time and space to discretized pixels and ticks of time. So what's the point of programming in continuous time if it will eventually be discretized?
237+
What if we want counters that make counters that make counters...? [maybe simplier]
235238
236-
There are a [lot](https://github.com/conal/talk-2014-bayhac-denotational-design#why-continuous-time-matters) of [reasons](http://conal.net/blog/posts/why-program-with-continuous-time). The most compelling argument for me is for the same reason we use SVGs: resolution independence. We want to be able to transform our programs "in time and space with ease and without propagating and amplifying sampling artifacts." We want to discretize at the last possible moment, and stay as abstract as possible for as long as possible.
237239
238-
[TODO Explain why continuous time (and laziness) are key for compositionality and modularity, maybe citing my FRP paper]
240+
This example may seem contrived, but it's a good minimal example to test the expressivity of a UI paradigm.
241+
242+
### Visualizing Flows
239243
240-
You can learn more about denotational design and semantics from Conal's [video](https://www.youtube.com/watch?v=bmKYiUOEo2A) and [paper](http://conal.net/papers/type-class-morphisms/).
244+
While I am in love with this paradigm, it hurts my head to mentally grabble with such unweildy types. And these are just simple examples! How are mere mortals supposed to program this way?
241245
242-
## "A denotationally simple model for whole systems"
246+
I have a hunch: visualize the flows.
243247
244-
Now that you understand DCTP, here's why I think it can save programming...
248+
[TODO graphics]
249+
250+
There's been some interesting work done here [TODO linkify], and I'm hoping to further it by building similar visualization tools for Hareactive and Turbine.
251+
252+
## "Why program with continuous time?"
253+
254+
[TODO this section needs cleaning]
255+
256+
Of course, for the computer to render the above graphics on the screen, it must at some point convert the continuous time and space to discretized pixels and ticks of time. So what's the point of programming in continuous time if it will eventually be discretized?
257+
258+
There are a [lot](https://github.com/conal/talk-2014-bayhac-denotational-design#why-continuous-time-matters) of [reasons](http://conal.net/blog/posts/why-program-with-continuous-time). The most compelling argument for me is for the same reason we use SVGs: resolution independence. We want to be able to transform our programs "in time and space with ease and without propagating and amplifying sampling artifacts." We want to discretize at the last possible moment, and stay as abstract as possible for as long as possible.
245259
246260
Aren't spreadsheets wonderful? A key to their success is that there is no control flow, no sequencing of instructions. Only data flow.
247261
@@ -253,7 +267,8 @@ As Conal says:
253267
254268
Haskell is such a denotative lanugage... That is, except for monads.
255269
256-
[TODO maybe combine this section with "why program with continuous time?" and denotational sematnics]
270+
[TODO Explain why continuous time (and laziness) are key for compositionality and modularity, maybe citing my FRP paper]
271+
257272
258273
### Monads are Imperitive
259274
@@ -267,36 +282,36 @@ This state of affairs is particularly depressing because the Haskell community,
267282
268283
Let's liberate ourselves from the IO Monad by simply refusing to use it. We are free!!!!!!
269284
285+
### "A denotationally simple model for whole systems"
286+
270287
But wait... what about all the things we can't do without it? What about open and closing files and sockets, reading and writing from the console and the database? How do we do all the things we need to as self-respecting programmers?!
271288
272-
> The imperative interfaces in today OSs and data-bases are troubling at first, and indeed I often hear people (even on #haskell) using these examples as demonstration that the imperative programming model is inescapable.
289+
> The imperative interfaces in today OSs and data-bases are troubling at first, and indeed I often hear people (even on #haskell) using these examples as demonstration that the imperative programming model is inescapable.
290+
>
291+
> - [Can functional programming be liberated from the von Neumann paradigm?](http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm)
292+
293+
The answer is hidden inside FRP. You ready for it? Here it is: stop. Stop it with the files and the sockets. Stop it with the reading and writing to the console and the database. Those are imperitive things that you can only do imperitively. There's no monad magic that can save you and make imperitive things denotative. The only solution is to
273294
274-
The answer is hidden inside FRP. You ready for it? Here it is: stop. Stop it with the files and the sockets. Stop it with the reading and writing to the console and the database. Those are imperitive things that you can only do imperitively. There's no monad magic that can save you and make imperitive things denotative. The only solution is to "shift [imperitivity] out of the role of a programming model notion and into the role of implementation of a programming model notion."
295+
> move I/O entirely out of our programming model into the implementation of a denotationally simple model for whole systems.
296+
>
297+
> - [Can functional programming be liberated from the von Neumann paradigm?](http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm)
275298
276299
### ReactJS is JQuery-as-a-Service
277300
278301
One of the ways I understood ReactJS was as JQuery-as-a-Service. I would describe how I want my HTML to look like for any value of state, and then React would set to work to figure out what it needs to do (via virutal-dom-diffing) to mutate the DOM to look the way I want it to (JQuery-esque mutations). I no longer had to worry about keeping the state and the DOM in sync: React did it for me.
279302
280303
Before ReactJS, if you told me I could make TodoMVC without the imperitive DOM API of adding, removing and editing nodes, I'd tell you that you were crazy. But then React did it. Describe your UI as a function and it will do the *imperitive mutation stuff under the hood.* That's the trick. We can't actually get rid of imperitive programming, just like we can't get rid of the fact that SVGs eventually need to pixelate to show up on the screen. We can just hide those details below the programmers level of abstraction.
281304
282-
The Conal Elliott war cry:
283-
284-
> let’s explore how to move I/O entirely out of our programming model into the implementation of a denotationally simple model for whole systems.
285-
286305
### HTTP = JQuery
287306
288-
One day in the shower, I was wondering how to incorporate HTTP requests into the FRP paradigm. Then it hit me. HTTP = JQuery. HTTP requests are too low level. They are similar to manually querying or mutating the DOM via JQuery, except it's manually querying or mutating server state. We need to go higher level and build a semantic model for "whole systems."
307+
One day in the shower, I was wondering how to incorporate HTTP requests into the DCTP paradigm. Then it hit me. HTTP = JQuery. HTTP requests are too low level. They are similar to manually querying or mutating the DOM via JQuery, except it's manually querying or mutating server state. We need to go higher level and build a semantic model for "whole systems."
289308
290-
The same is true of all imperative APIs, including databases, files, sockets, etc. It's not that we have to abandon these technologies entirely. But only as the _implementation_ of a denotative programming system. The computer's job should be to make HTTP requets for you, just like React mutates the DOM on your behalf.
291-
292-
### Performance
293-
294-
It's well known that FRP has suffered space and time leaks. People often complain about the performance of mathematical or abstract language like Haskell.
295-
296-
[asked conal for help here]
309+
The same is true of all imperative APIs, including databases, files, sockets, etc. It's not that we have to abandon these technologies entirely. But only as the _implementation_ of a denotative programming system. The computer's job should making HTTP requets, reading and writing to files and databases, on your behalf, just like React mutates the DOM on your behalf.
297310
298311
## Common Gotchas
299312
313+
DCTP is notoriously difficult to understand. Even if you get most of it, you are likely not yet entirely rid of imperitive thinking habits.
314+
300315
### Thinking about DAGs
301316
302317
A very common misconception with DCTP is confusing it with its implementation details. If you catch yourself thinking of DCTP as a DAG (directed acyclic graph), you still haven't quite gotten it. The builder of a DCTP framework or library _may_ decided to choose a DAG as an internal data structure, but they may choose an entirely different underlying evaluation strategy. In fact, a DAG is likely the wrong structure for DCTP because DAGs are (definitionally) acyclic, but DCTP often requires cycles (recursively defined Behaviors and Events) to represent the full range of user interfaces.
@@ -315,6 +330,16 @@ In [What's Functional Programming All About?](http://www.lihaoyi.com/post/WhatsF
315330
316331
[TODO]
317332
333+
### Performance
334+
335+
It's well known that FRP has suffered space and time leaks. People often complain about the performance of denotational langauges like Haskell.
336+
337+
If you look at my 100x100 pixel animations running on the CPU, you may write off this paradigm for its impracticality. However, you'd simply be fooled by the von Nueman bottleneck [TODO link]. When I switched to the GPU, the code ran without a hitch.
338+
339+
Conal argues that performance criticisms of denotational programming have it exactly wrong. Not just wrong, exactly wrong. One of the main benefits of denotational programming is equational reasoning, which is the key factor that enables speeding up code: the ability to optimize it by replacing some parts with faster parts.
340+
341+
When your code is abstract and free of imperitive, technological, operational concerns, you can compile down much further and easier. Conal brags about compiling his Haskell code down to custom-made circuits for blazing speeds.
342+
318343
## Further reading
319344
320345
* [What's Functional Programming All About?](http://www.lihaoyi.com/post/WhatsFunctionalProgrammingAllAbout.html)
@@ -331,3 +356,5 @@ https://stackoverflow.com/questions/5385377/the-difference-between-reactive-and-
331356
https://stackoverflow.com/questions/1028250/what-is-functional-reactive-programming/1030631#1030631
332357
333358
https://futureofcoding.org/notes/conal-elliott/
359+
360+
<a name="1" href="#1">[1]</a> - The "D" in DCTP stands for "denotational semantics." The original name for denotational semantics was "mathematical semantics". It was pioneered by Chris Strachey and Dana Scott in the early 1970s. It is an approach to model a programming language with mathematical objects. For example, in this essay we model static graphics as a functions from `x` and `y` to `Color` and moving animations from `x`, `y`, and `t` to `Color`. You can learn more about denotational design and semantics from Conal's [video](https://www.youtube.com/watch?v=bmKYiUOEo2A) and [paper](http://conal.net/papers/type-class-morphisms/).

0 commit comments

Comments
 (0)