Domain‑Specific Languages: Part 1

Episode #26 • Aug 20, 2018 • Subscriber-Only

We interact with domain-specific languages on a daily basis, but what does it take to build your own? After introducing the topic, we will begin building a toy example directly in Swift, which will set the foundation for a future DSL with far-reaching applications.

Domain‑Specific Languages: Part 1
Introduction
00:05
Definitions
00:42
An arithmetic expression DSL
03:30
Adding multiplication to our DSL
09:38
Transforming our DSL
12:02
Adding a variable to our DSL
19:13
To be continued…
22:31

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Introduction

Today we are going to talk about a concept known as “domain-specific languages”, and in particular “embedded domain-specific languages”. It may sound like a jargony term, but it’s something that you have definitely come across and you may even use it on a daily basis.

After giving the upfront definitions so that we all understand what a domain-specific language is, we will create one right in Swift and progressively add more and more advanced features to it. It’s a toy example, but it contains a lot of the core ideas and it can be a lot of fun to play with.

This episode is for subscribers only.

Subscribe to Point-Free

Access this episode, plus all past and future episodes when you become a subscriber.

See plans and pricing

Already a subscriber? Log in

Exercises

  1. Improve the simplify function to also recognize the following patterns:

    • Factorize the c out of this expression: a * c + b * c.
    • Reduce 1 * a and a * 1 to just a.
    • Reduce 0 * a and a * 0 to just 0.
    • Reduce 0 + a and a + 0 to just a.
    • Are there any other simplification patterns you know of that you could implement?
  2. Enhance Expr to allow for any number of variables. The eval implementation will need to change to allow passing values in for all of the variables introduced.

  3. Implement infix operators * and + to work on Expr to get rid of the .add and .mul annotations.

  4. Implement a function varCount: (Expr) -> Int that counts the number of .var’s used in an expression.

  5. Write a pretty printer for Expr that adds a new line and indentation when printing the sub-expressions inside .add and .mul.

Downloads

Sample Code

0026-edsls-pt1