Domain Specific Language (DSL) friendly syntax

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Domain Specific Language (DSL) friendly syntax

Sebastian Wiesendahl
Hello,

I am very new to julia and haven't worked much with the language yet. Especially not for writing a DSL, thus I have a some of questions:

1. Which kind of operators are supported right now?
  • prefix
  • postfix
  • circumfix

2. Why are there some operators which are not a function (e.g. the "Short-Circuit Evaluation")? Are there also others which are not a function?


3. I wonder if it is possible to influence the operator precedence within a macro or a closure? If so, this would enable highly cutomizable DSLs.

4. When can I leave out braces and when not? Is it defined by an operator precedence or by fixed rules? Where does all the syntax sugar come from?


One aim of all these questions is to know, how difficult it would be to supoprt a syntax like the following example:

x
= |-1|

instead of

x = abs(-1)


I hope there are some of you interested in the topic of DSLs and that my questions are not to broad.

Reply | Threaded
Open this post in threaded view
|

Re: Domain Specific Language (DSL) friendly syntax

Mike Innes
1. Julia's set of operators and precendence rules is fixed, but very comprehensive. I don't know if there's a good way to explore them but if you try various unicode operator characters you can see how they behave.

2. `&&` can't be implemented as a function because it acts a bit like an if statement – it doesn't always evaluate its arguments, which functions do. Macros can still catch these and change their semantics, though.

3. Technically yes, but in general it's not a good idea. You'll end up with strange edge cases and it makes it harder to read the code, because the structure you see in the code isn't what's actually running.

Check out the manual section on macros for more info on what is and isn't possible, although the short answer to your final question is "no". (Technically you can put whatever syntax you want in a string macro, but it's best to stick to Julia's syntax if possible.)

On Mon, 21 Mar 2016 at 22:08 Sebastian Wiesendahl <[hidden email]> wrote:
Hello,

I am very new to julia and haven't worked much with the language yet. Especially not for writing a DSL, thus I have a some of questions:

1. Which kind of operators are supported right now?
  • prefix
  • postfix
  • circumfix

2. Why are there some operators which are not a function (e.g. the "Short-Circuit Evaluation")? Are there also others which are not a function?


3. I wonder if it is possible to influence the operator precedence within a macro or a closure? If so, this would enable highly cutomizable DSLs.

4. When can I leave out braces and when not? Is it defined by an operator precedence or by fixed rules? Where does all the syntax sugar come from?


One aim of all these questions is to know, how difficult it would be to supoprt a syntax like the following example:

x
= |-1|

instead of

x = abs(-1)


I hope there are some of you interested in the topic of DSLs and that my questions are not to broad.

Reply | Threaded
Open this post in threaded view
|

Re: Domain Specific Language (DSL) friendly syntax

Yichao Yu
In reply to this post by Sebastian Wiesendahl
On Mon, Mar 21, 2016 at 2:21 PM, Sebastian Wiesendahl
<[hidden email]> wrote:

> Hello,
>
> I am very new to julia and haven't worked much with the language yet.
> Especially not for writing a DSL, thus I have a some of questions:
>
> 1. Which kind of operators are supported right now?
>
> prefix
> postfix
> circumfix

only prefix

>
> 2. Why are there some operators which are not a function (e.g. the
> "Short-Circuit Evaluation")? Are there also others which are not a function?
>

Because those are not functions and rather control flow syntax. There
are a few others that are not just function (:: for example)

>
> 3. I wonder if it is possible to influence the operator precedence within a
> macro or a closure? If so, this would enable highly cutomizable DSLs.

Not currently.

>
> 4. When can I leave out braces and when not? Is it defined by an operator
> precedence or by fixed rules? Where does all the syntax sugar come from?

Predefined in parser.

>
>
> One aim of all these questions is to know, how difficult it would be to
> supoprt a syntax like the following example:
>
> x = |-1|
>
> instead of
>
> x = abs(-1)
>
>
> I hope there are some of you interested in the topic of DSLs and that my
> questions are not to broad.
>
Reply | Threaded
Open this post in threaded view
|

Re: Domain Specific Language (DSL) friendly syntax

Stefan Karpinski
In reply to this post by Sebastian Wiesendahl
On Mon, Mar 21, 2016 at 2:21 PM, Sebastian Wiesendahl <[hidden email]> wrote:
Hello,

1. Which kind of operators are supported right now?
  • prefix
  • postfix
  • circumfix
All of the above. However, circumfix operators are limited to paired braces.

2. Why are there some operators which are not a function (e.g. the "Short-Circuit Evaluation")? Are there also others which are not a function?

The arguments of functions are evaluated before the function itself is evaluated (Julia has strict evaluation). This implies that operators that don't evaluate all their operands cannot be functions. The ~ is a somewhat oddball operator which lowers to a macro call instead of a function call so that we can emulate R's formula notation. This is a somewhat controversial feature that there's fairly widespread interest in reverting.

3. I wonder if it is possible to influence the operator precedence within a macro or a closure? If so, this would enable highly cutomizable DSLs.

I think you're unlikely to be able to convince people that this is a good idea (full disclosure: myself included). The principle that I would primarily use to argue against such flexibility is that the meaning of an expression should be locally evident – in particular, how to parse it should not depend on global state. Fortunately, there are generally accepted precedences for mathematical operators and there's not much need to fiddle with them in practice.

4. When can I leave out braces and when not? Is it defined by an operator precedence or by fixed rules? Where does all the syntax sugar come from?

There is built-in operator precedence defined here. Operators with higher precedence do not require parentheses to associate more tightly than operators with lower precedence.
 
One aim of all these questions is to know, how difficult it would be to supoprt a syntax like the following example:
 
x = |-1|
 
instead of

x = abs(-1)


See https://github.com/JuliaLang/julia/issues/13880. To summarize, the pros are:
  • it looks cute
The cons are:
  • very hard to parse correctly
  • almost certainly introduces at least white-space sensitivity
  • doesn't nest, so you can't express even simple expression like abs(1 - abs(x)) with this syntax
 So it was decided against.