Strangely Consistent

Theory, practice, and languages, braided together

Macros: "Define Your Own Literals" Day

There's clearly a blurry line between macros and slangs. We should probably keep it that way. But for this post — as I keep mapping out more of the possible futures the best of which we are aiming for — I don't exactly know if it's macros I'm talking about, or slangs.

(Meanwhile, in a parallel universe, masak-with-a-toggled-goatee writes a similarly ambiguous post in his series of investigative blogginations about slangs.)

Consider this code from the near future:

use Slang::HTML;
use Slang::SQL;

my $keyword = "dugong";
my $db = $*DB;

my $webpage = html`
    <html>
        <body>
            <h1>Results for {$keyword}</h1>

            <ul id="results">
                {list-items($db.query( sql`
                    SELECT title, snippet
                        FROM products
                        WHERE {$keyword} in title
                `))}
            </ul>
        </body>
    </html>
`;

Let's ignore the fact that mixing HTML and database calls is something that most of us did back in the nineties and would prefer not doing again. Let's also leave aside the fact that I don't know the actual syntax for introducing a slang, and I basically just picked something that didn't look awful. (Or maybe it does. Anyway, it's not my call.)

Instead, let's focus on the three awesome things with the above code:

Where's all this coming from? I've had the above long-term vision for a long time, but it was re-triggered by the paper Safely Composable Type-Specific Languages. It's nice, although the first half is lighter reading than the second. My code above is based on their code from page 2, except mine has better indentation.

I do like the idea from that paper that literals could basically be defined by module authors. And there's another sense that stirs in me that I've had lately when writing hobby Perl 6: that of defining first a model (objects), then a set of rules (functions and methods), and then a syntax (custom operators). I've only had a small taste of that kind of programming, but I like it.

Anyway, here are my takeaways from the paper:

Possible ways forward on this: both the HTML and SQL slangs are possible today, with some degree of bending the implementation. FROGGS++ is showing the way with Slang::Tuxic — let's do something similar with these.

Edit: not one minute after publishing this post, I'm made aware of Slang::SQL on Github. It's not quite there yet, but it's working its way toward the ideal I have in mind. Nice! Ok, so now I only have to rally the people to building Slang::HTML. 哈哈

Signing off with this thought:

Any sufficiently complicated syntax highlighter for Perl 6 contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of STD.pm6.