|
| 1 | +So what's a DSL? |
| 2 | +====================================================================== |
| 3 | + |
| 4 | +In general, a domain specific language is simply a computer language |
| 5 | +for a specific domain. It's NOT a synonym for |
| 6 | +[jargon](http://www.catb.org/jargon/html/online-preface.html)! DSLs |
| 7 | +can be specialized for markup, like |
| 8 | +[*HTML*](https://en.wikipedia.org/wiki/HTML); for modelling, like |
| 9 | +[*EBNF*](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form); |
| 10 | +for shell scripting, like |
| 11 | +[*Bash*](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29); and |
| 12 | +more. |
| 13 | + |
| 14 | +The languages we will construct will mostly concern modelling of |
| 15 | +physics and mathematics. We will create data structures in Haskell to |
| 16 | +represent the same physics calculations that we write on paper, in |
| 17 | +such a way that we can write functions to, for example, analyze the |
| 18 | +expressions for validity. |
| 19 | + |
| 20 | +Look, we'll demonstrate. Let's say we want to model a language that is |
| 21 | +a subset to the common algebra we're all familiar with. Our language |
| 22 | +will consist expressions of a single variable and addition. For |
| 23 | +example, the following three expressions are all valid in such a language: |
| 24 | + |
| 25 | +$$x + x$$ |
| 26 | + |
| 27 | +$$x$$ |
| 28 | + |
| 29 | +$$(x + x) + (x + x)$$ |
| 30 | + |
| 31 | +When implementing a DSL, we typically start with modelling the |
| 32 | +syntax. Let's first declare a data type for the language |
| 33 | + |
| 34 | +> data Expr |
| 35 | + |
| 36 | +Then we interpret the textual description of the language. "Our |
| 37 | +language will consist of expressions of a single variable and |
| 38 | +addition". Ok, so an expression can be one of two things then: a |
| 39 | +single variable |
| 40 | + |
| 41 | +> = X |
| 42 | + |
| 43 | +or two expressions added together. |
| 44 | + |
| 45 | +> | Add Expr Expr |
| 46 | + |
| 47 | +And that's it, kind of! However, a DSL without any associated |
| 48 | +functions for validation, symbolic manipulation, evaluation, or |
| 49 | +somesuch, is really no DSL at all! We must DO something with it, or |
| 50 | +there is no point! |
| 51 | + |
| 52 | +One thing we can do with expressions such as these, is compare whether |
| 53 | +two of them are equal. Even without using any numbers, we can test |
| 54 | +this by simply counting the $x$s! If both expressions contain the same |
| 55 | +number of $x$s, they will be equal! |
| 56 | + |
| 57 | +> eq :: Expr -> Expr -> Bool |
| 58 | +> eq e1 e2 = count e1 == count e2 |
| 59 | +> where count X = 1 |
| 60 | +> count (Add e1 e2) = count e1 + count e2 |
| 61 | + |
| 62 | +We can now test whether our expressions are equal. |
| 63 | + |
| 64 | +< ghci> eq (Add (Add X X) X) (Add X (Add X X)) |
| 65 | +< True |
| 66 | + |
| 67 | +And NOW that's it (if we want to stop here)! This is a completely |
| 68 | +valid (but boring) DSL. We've modelled the syntax, and added a function |
| 69 | +that operates symbolically on our language. This is a very small and |
| 70 | +simple DSL, and you've likely done something similar before without |
| 71 | +even realizing you were constructing a DSL. It can really be that |
| 72 | +simple |
| 73 | + |
| 74 | +In other DSLs we may also look at evaluation and the semantics of a |
| 75 | +language, i.e. what is the actual type of the result when we |
| 76 | +*evaluate* or "compute" our expressions. |
| 77 | + |
| 78 | +Throughout this book we'll construct and use DSLs in many different |
| 79 | +ways. Different parts of the physics we look at will require different |
| 80 | +DSL treatments, basically. There is no *one* model to use for all |
| 81 | +DSLs. |
0 commit comments