I was digging through my external drive yesterday and I discovered some notes on a programming language concept. I will briefly talk about it here.
There are a few things I really like in programming languages:
- Parametric recursive data types
- Automatically checked type constraints
- Pattern matching and guards
- Automatic event management
- A concise and simple syntax (point free, if possible)
- Restartable/Resumable exceptions
- Type inference
This language concept features, at a minimum, numbers 4, 5 and 7.
In this language, just like in Java, everything is an object, of sorts. Also, just like in Java, you define each “class” inside its own file and the file name must match the class name. In the spirit of being short and concise, however, the language eliminates the redundancy and boilerplate: you do not need to define the class or give it a name, we already know its name is the same as the file name. We also already know that everything inside that file belongs in that class.
This is only a minor, cosmetic change, though. The more interesting features are the automatic event and dependency management.
Lets start with some sample code:
[Filename: ButtonClick.lang]
public
reset click -> false
click_count <- local_click_count
private
local local_click_count = 0
react click: local_click_count += 1
in some other file:
import io
import .ButtonClick
private
local clicker = ButtonClick
display: io.print “Button clicked ”
clicker.click_count
“ times.\n”
main: times [clicker.click = true, yield] 3
What this does is creates a number of cells which contain data:
- click, a cell which will reset to false every time it gets set
>>> clicker.click = true
>>> io.print clicker.click
--- false
- click_count, a cell which is bound to the value of local_click_count
- local_click_count, a local variable. This is a standard integer variable, “bound” to click_count
- react click, a function which reacts to click being modified
When click is set to true, the react click code is run. This will change the value of the local state variable. In turn, this causes the public cell to change.
The other file creates an instance of this class as the object clicker and defines two functions: display, which displays the current click count and main which is run on program start. Because of the automatic event management system, any time clicker.click_count changes, the display function gets rerun. As I stated above, setting click to true, will increment click_count, so if you set clicker.click to true, the value for click_count is incremented, click is reset to false and display is executed.
The main function simply sets clicker.click to true three times, waiting for events to be handled in between.
The concept is simple enough – modules/objects may contain mutable local state, but communications between modules can only be done through automatically managed events. Any code which relies on these events (by referencing a cell or because its a reactor) will be automatically evaluated any time the event occurs (that is, any time the dependents change).
The language could be visually enhanced too:
At this point, I’d like to point out that this is not the language I have envisioned and is not what this blog aims to be about, but it is a step in that direction. So bear with me, I will slowly transition into dataflow and hopefully design a useful, intuitive and powerful dataflow language in the process. Or at the very least, learn something useful.
