In the previous post about Frink, we explored how smoothly Frink handles units of measure. In this post, we will focus on another deeply integrated feature of Frink’s: interval arithmetic. On Frink’s homepage this feature is described as magical. The aim of this post is to try and make sense of this statement. We will now give a quick rundown of interval arithmetic and then proceed to concrete examples. For a more detailed review of the subject, you can refer to Wikipedia, or for a Frink-specific treatment, to the interval arithmetic section in Frink’s documentation. On with it.
Hello Frink
In this post, I’ll be introducing the Frink programming language. As stated on Frink’s homepage:
Frink is a practical calculating tool and programming language designed to make physical calculations simple, to help ensure that answers come out right, and to make a tool that’s really useful in the real world. It tracks units of measure (feet, meters, kilograms, watts, etc.) through all calculations, allowing you to mix units of measure transparently, and helps you easily verify that your answers make sense. It also contains a large data file of physical quantities, freeing you from having to look them up, and freeing you to make effortless calculations without getting bogged down in the mechanics.
So Frink’s “killer feature” is that you can write something like:
1 2 |
|
Joy No More
I’ve reached the end of my Joy escapade. I can definitely say that I had a great time (notice how I avoided one of my well-trodden puns here) figuring out this nifty little language.
Domain-specific Joy
In this post, our goal is to create a domain-specific language (DSL) for the state machine domain that we introduced in the last post. We already had a working example of a state machine (a totally realistic model of a baby), which suffered from being rather verbose and far from cleanly expressing our target domain.
To actually have a language, we need some kind of a syntax for it. In our case, we already have a nice textual representation of the domain, which is the ASCII table format I used to describe the different baby states (funny coincidence how that worked out so comfortably well). This format solves the repetitiveness issues and is definitely close to our domain.
State of Joy
Having finished with binary trees and getting somewhat sidetracked by metaprogramming, we are now ready for our next small project. Implementing binary trees was sort of a warm-up for getting used to Joy – now I would like to do something a bit less textbooky.
As you may recall, Joy has a very flexible syntax due to its homoiconicity (as can be seen in the metaprogramming post). Having a flexible syntax should make a language easily amenable to embedding of domain-specific languages (DSLs). Personally, I’m a big fan of DSLs, so trying to implement one in Joy seems like a nice idea for a small project.
In order to actually have a domain-specific language, we first need to come up with a domain. Having gone through a careful process of examining and comparing various domains (i.e., choosing the first thing that randomly came up in my mind at some distant point in time), I decided to model state machines. More specifically, I’ll be implementing a small language that can describe state machines augmented by a stack, which makes it a pushdown automaton. Adding a stack to our machine seems like a natural idea for a stack-based language and also makes the domain a tad more interesting; we’ll see how that works out later on.
Meta-Joy
In the previous post I was struggling with the noise generated in my code by stack manipulation-related functions. Having reached a dead end, I promised you a shiny new direction, which I’ll be introducing in this post.
To do that, we’ll have to recap one of the Joy basics, namely, its homoiconicity. In the introduction, I mentioned how Joy’s list primitive is actually a quoted program; this should be familiar if you’re acquainted with Lisp lists. So… we can easily go meta on the language by employing list manipulation. Here’s a simple example to illustrate the point:
1
|
|
Joyous Tree Friends
Having introduced Joy in the previous post, as per our grand scheme of things, we are now ready for some coding.
As a first small project, I decided to try and implement a simple binary search tree. Nothing fancy, no self-balancing or anything evil like that. Just your Plain Old Binary Search Tree (which can be handily abbreviated as POBST).
The reason for this boring textbook example is twofold. First, Joy being, for me, a rather new way of thinking about code, I didn’t want to distract myself with something overly creative. Having a ready recipe for the thing I want to code provides me with a nice distraction-free, Joyful coding experience. Second, as Joy stresses function-level programming (no values and all that), I was wondering how a data structure, which I think of as a value, would look like in this paradigm.
The Joy of Joy
Intro: [swap dip dup dip pop] dip dup dip pop
(sing aloud accompanied by a Jazz trio or even a cappella)
Anyways, this is actual Joy source code, taken from the Mathematical foundations of Joy article. No, really, you’re not squinting hard enough, there is some actual math there.
Well, this is my first language from the Perlis Languages list. It’s a concatenative language, colloquially known as a stack-based language (not sure what sort of person goes all colloquial about anything concatenative, probably the sort of person that uses the word “colloquial”). Stack-based languages tend to have very little in the sense of syntax, one may even call them “Lisp without the parentheses”. So that makes Joy a good candidate to be the first language for this little project; it’s rather simple and self-contained. You can pretty much get all the material you might need to tackle Joy from the (mirror of) the official site. Other than that, as recommended by Fogus, there’s the really nice “The Joy of Concatenative Languages” series by Daniel Spiewak; although his language of choice is Cat, it still is, as usual for Daniel’s writings, a very instructive and fun read.
Let’s take a quick tour of Joy, though for a proper introduction you should consult the official one.
Introduction
I like programming languages, and I just couldn’t find a proper excuse to learn them. So I decided to go on and learn them without an excuse.
My first attempt at doing so was via the amazing book by Bruce Tate, “Seven Languages in Seven Weeks” (great read, highly recommended).
After sparking my initial interest in learning programming languages for fun (and ruining my ability to calmly write plain old Java), that book left me with an itch for more. The itch kept on spreading when I came across a blog post “Perlis Languages” by the brilliant Fogus, go on and read it.
As I personally almost never go on and read stuff pointed to in posts, I’ll give a quick recap of that post here.
It starts off with a quote by Alan Perlis:
A language that doesn’t affect the way you think about programming is not worth knowing.
Next, Fogus goes on to explain how one can and should expand one’s views on software development, namely, by learning “Perlis Languages”:
A Perlis Language is a programming language that I believe will shake one’s views on software development to the core.