"Domain-Specific Languages" in Julia


#1

When I read or hear about domain-specific languages in Julia I tend to cringe a little.
In most cases it is not about a language, but rather an API.

The difference? A DSL uses a syntax appropriate for its domain. An API is restricted by the syntax rules of the hosting language, in this case Julia. For example,

DSL

SELECT * FROM table1 WHERE age>50

API

# QueryVerse
    @from i in table1
    @where i.age>50
    @select i

# JuliaDB
    filter( p->p.age > 50 , table1)

I don’t mean to pick on QueryVerse or JuliaDB specifically. There are many other examples in other domains. What they all have in common is that they are restricted to either Julia macros and/or Julia functions, both of which must adhere to Julia syntax rules.

In my opinion each Domain-Specific Language needs its own parser. The only example I know of in Julia are regular expressions. Those are parsed by a regular expression parser.

Why not do something similar with other DSL.

SQL"""
SELECT *
  FROM table1
  WHERE age > 50
"""
GAMS"""
Parameter c(i,j)  transport cost in thousands of dollars per case ;
           c(i,j) = f * d(i,j) / 1000 ;
 Variables
      x(i,j)  shipment quantities in cases
      z       total transportation costs in thousands of dollars ;
 Positive Variable x ;
 Equations
      cost        define objective function
      supply(i)   observe supply limit at plant i
      demand(j)   satisfy demand at market j ;
 cost ..        z  =e=  sum((i,j), c(i,j)*x(i,j)) ;
 supply(i) ..   sum(j, x(i,j))  =l=  a(i) ;
 demand(j) ..   sum(i, x(i,j))  =g=  b(j) ;
 Model transport /all/ ;
 Solve transport using lp minimizing z ;
 Display x.l, x.m ;
"""
XPath"""
// All <author> elements where the first <last-name> child element has the value Bob.
author[last-name [position()=1]= "Bob"]
"""

A general Julia parser-generator that can convert a true DSL to corresponding Julia expressions might be helpful here (For inspiration see ANTLR4)

Let the discussion begin :slight_smile:


#2

Parser generators do exist in the package community, See packages like

That’s an example of a Julia parser generator that translates to and from Julia expressions from another lang


#3

For reference, there’s been some relevant discussion in a previous thread: Seamlessly extending the Julia syntax.