[Python-ideas] Why shouldn't Python be better at implementing Domain Specific Languages?

Jonathan Fine jfine2358 at gmail.com
Fri Aug 31 06:13:00 EDT 2018


James Lu started this thread by quoting me. Thank you, James, for the
compliment. And I feel somewhat obliged to contribute here, are at
removed I started the thread.

In the message James quoted, I also said

<quote>
But most strengths, in another situation, can be a weakness. Language
design is often a compromise between conciseness and readability, ease
of use and performance, good for beginners and good for experts, and
many other factors. Such as innovation and stability.

Guido's decisions, as BDFL, have shaped Python and its community into
what it is now. It is one set of compromises. Other languages, such as
Ruby, have made different compromises. Now that the BDFL is on
vacation, the challenge is to maintain the essence of Python while
continuing to innovate.
</quote>

It's important, of course, for the developers of a DSL to understand
the domain. I'm starting to learn http://elm-lang.org/. It describes
itself as

> A delightful language for reliable webapps.
> Generate JavaScript with great performance and no runtime exceptions.

The Elm developers have learnt a great deal from Python, and I think
that we in turn can learn from them. Particularly about catching
coding errors early, with good feedback. But that's a different
thread.

So I'd say to focus on improving the API to an existing library is a
good way to develop our understanding of DSLs more generally. James
provided a Keras example

    model.add(Dense(units=64, activation='relu', input_dim=100))
    model.add(Dense(units=10, activation='softmax'))

What might be better here, if allowed, is

    model.extend([
        Dense(units=64, activation='relu', input_dim=100),
        Dense(units=10, activation='softmax'),
    ])

Another approach would be to provide a fluent interface.

    https://martinfowler.com/bliki/FluentInterface.html
    https://en.wikipedia.org/wiki/Fluent_interface

Done this want, we might get something like jQuery

   (
    model
    .dense(units=64, activation='relu', input_dim=100)
    .dense(units=10, activation='softmax')
   )

JSON and XML and YAML have already been mentioned. Here's another,
XML-ish approach.

A combined list-dictionary is quite common. It's used widely in XML
(and SGML before it). So how to create such. A few years ago I
experimented with an API such as

     A(a=1, b=2)[
         X(1, 2, 3),
          Y[ ....],
      ]

As I recall, someone told me that https://kivy.org does something
similar. Kivi and Elm, are systems I'd like to learn. Ease of use is
important in language and library design. We can learn from the
success of others, as well as from our own successes and failures
(smile).

-- 
Jonathan


More information about the Python-ideas mailing list