Thursday, 13 March 2014

Another case for embedded DSLs


As already stated in my previous post I'm not very convinced about embedded DSLs, I cite:
Until recently I'd only sneer at the idea of DSL's: what the @!%*, either we have a solid API for developers, or a specialized higher level language for the specific task. And the higher level language shouldn't be done at all in the best case, because the non-technical user just wants a smooth GUI to click his requests together! So spare your DSL hype on me, go and find another gullible middle management person.
Did you notice the "until recently" moniker? As it turns out, I found some use for E-DSLs in the end, but a general suspicion remained. But recently I happened to hear a presentation by Joel Falcou and that has shown me another use for E-DSLs!

The talk was about:
... using the Boost.Proto library - a C++ EDSL toolkit - to redesign NT2, a C++ scientific computation library similar to MATLAB in term of interface.*
The talk was mainly about template metaprograming, expression templates, and integrating hardware descriptions into the system, but one phrase catched my ear, namely: " similar to MATLAB in term of interface"! Look below for how you could replace MATLAB with NT²:

Recipe:
  • Take a  .m  (i.e. MATLAB ) file, copy it to a  .cpp file
  • Add  #include <nt2/nt2.hpp>  and do cosmetic changes
  • Compile the file and link with libnt2.a
And what are the "cosmetic" changes, s'il vous plait?

MATLAB code:
R = I(: ,: ,1);
G = I(: ,: ,2);
B = I(: ,: ,3);

Y = min ( abs (0.299.* R +0.587.* G +0.114.* B) ,235);
U = min ( abs ( -0.169.*R -0.331.* G +0.5.* B) ,240);
V = min ( abs (0.5.*R -0.419.*G -0.081.* B) ,240);

equivalent NT² code:
auto R = I(_,_ ,1) ;
auto G = I(_,_ ,2) ;
auto B = I(_,_ ,3) ;
table <float , of_size_ <N,M> > Y, U, V;

Y = min ( abs (0.299* R +0.587* G +0.114* B) ,235);
U = min ( abs ( -0.169*R -0.331* G +0.5* B) ,240);
V = min ( abs (0.5*R -0.419*G -0.081* B) ,240);

What struck me, was that you can switch from the proprietary DSP system to a free C++ replacement that simply! OK; NT² isn't a perfect drop-in replacement, but it's good enough if we trade off royalties to be paid against it!

And thus the second use-case for embedded DSLs emerges: lure the user of another language into C++! Or putting it differently: give the user a C++-alternative and let him choose.

BTW: did you notice that the EDSL use-cases always involve the ominous "user", which doesn't really know C++?

--
* Slides for the talk are here.

3 comments:

  1. thanks for the shout out :)

    (E)DSL are indeed a tricky business and they have been used and abused in a very long list of dubious cases.

    We still think EDSL in c++ is a valid use case as it bridge the gap between the (potnetially) efficiency of C++ compiled code and a high level of abstraction.

    ReplyDelete
  2. @Joel
    Afer thinking again about DSL's, I came to the conclusion that they are just like LISP macros - too much of them, and you are doomed, but the right amount of them can save your day...

    OK, still not an easy relationship.

    ReplyDelete
  3. It's also the eternal problem of "when you know how to hammer thigns, everythign is a nail".

    ReplyDelete