A new Swift video series exploring functional programming and more.
#26 • Monday Aug 20, 2018 • Subscriber-only

Domain Specific Languages: Part 1

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.

#26 • Monday Aug 20, 2018 • Subscriber-only

Domain Specific Languages: Part 1

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.


Subscribe to Point‑Free

This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!

See subscription optionsorLog in

Sign up for our weekly newsletter to be notified of new episodes, and unlock access to any subscriber-only episode of your choosing!

Sign up for free episode

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.

Subscribe to Point-Free

👋 Hey there! Does this episode sound interesting? Well, then you may want to subscribe so that you get access to this episodes and more!


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.

Chapters
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