Write yourself a compiler in Clojure: Dhall edition
Fabrizio Ferrai
@fabferrai
Let’s use Dhall to make something
Let’s talk about Dhall
Let’s talk about a Dhall compiler
Small FP Talk
META
META META TODAY
Story of me learning that making a compiler is not that bad
Dhall
JSON
Functional, non-Turing-complete, strongly typed language
Not-Turing-Complete is a feature
Data Code
Turing
Completeness
Border
Dhall
Abstraction power
.
Dangers:
- Security
- Bad abstraction
- Non-termination
What is this useful for
“Like, it’s another language, better be revolutionary”
Can I try it?
dhall-lang.org
Install the “dhall” cli
NEW: Clojars → [dhall-clj “0.2.0”]
dhall-lang.org
Poll:
Dhall tutorial now or later?
Dhall shines when native in langs
Problem: no integrations yet
(only Haskell)
“Alright, I’ll make one for Clojure”
Great news #1
There is a Language Standard!
(this is actually not common, reasons later)
Great news #2
The language is tiny, so implementing it looked easy
(turns out this pretty much holds)
..6 months later
f-f/dhall-clj
Making a compiler in Clojure is fun+easy+useful
Why?
I almost forgot the definition
(sorry Rich!)
Compiler definition is a bit broader now
Generically “source string to executable”
See also “transpiler”
→ “source string to other language source string”
String → Executable
Cannot do it in one shot, as e.g. gcc realized
Compilers should be modular and make use of “phases”
And often different representations of ASTs
→ ~ “more types” is “good”
Source: The ClojureScript Compiler - A Look Behind the Curtains - Maria Geller
CLJS
https://blog.rust-lang.org/2016/04/19/MIR.html
Rust
https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/HscPipe
Haskell (GHC)
Dhall Compiler Phases
Parse
Resolve Imports
Typecheck
Normalize
Emit
Source
AST
Import-free AST
Clojure data/code
Valid AST
Evaluated AST
MOST IMPORTANT SLIDE
DEMO
Let’s compile some Dhall
and go through the phases
Takeaways
Instaparse is really great and unique
github.com/Engelberg/instaparse
Main reason for quick bootstrap
Dhall grammar ~700 LOC
Maintainability needs super much attention
Having a Language Standard is NICE a
Making a (typed) language is not that hard
As in: there’s no special magic
Rough TODO list:
Things to remember
Making a compiler in Clojure is nice
Dhall is awesome
Language Standards are really nice
100% test coverage is necessary*
More maintainability, less pasta!
*for compilers at least. Your PM might not agree :)
Thanks! Questions?
@fabferrai
f-f