Perl 6 and Parrot - In Conversation with Moritz Lenz
Written by Nikos Vaggalis   
Wednesday, 31 October 2012

The development of Perl 6 using the Parrot VM is a case study in language development. Even if you are not about to start work on your own language right now, this interview with Moritz Lenz who is working on the Rakudo Perl 6 compiler is a fascinating view of another world.

We set  out to investigate the latest developments in Perl 6 and Parrot and find out why Perl6 stands out from other  programming languages.

Our questions were answered by  Moritz Lenz, a prominent Perl 5 hacker and member of the People of Perl 6, as well as a regular contributor to both the Perl 6 test suite and the Rakudo compiler. He is also the author of the Perlgeek.de blog

Moritz had recently attended the "Perl Reunification Summit" held, very fittingly, in the town of Perl, a village in Germany close to the border with France and Luxembourg, so the first part of our conversation was about the relationship of Perl 5 and Perl 6.

NV: Recently you went to a town called Perl for a meeting about unification of the Perl 5 and Perl 6 communities. Are the Perl5 and Perl6 two distinct communities or do they overlap?

ML: There is a certain overlap. For example p5 and p6 folks go to the same conferences, workshops, hackathons but many Perl 5 core developers ignore Perl 6 and while most Perl 6 core hackers roughly know what's going on in Perl 5 land, there had been a big lack of communication.

NV:  Is that true of the languages as well? I mean are Perl 5 and Perl 6 two distinct languages, or will past experience of Perl 5 make adoption of Perl 6 easier?

ML: They are distinct languages from the same family of languages. On the surface they look quite similar and they are designed using the same principles:

  • easy to learn (it's possible to speak "baby perl" without learning much of the language)
  • get things done quickly
  • there's more than one way to do it

On the other hand they are different enough that a Perl 5 program doesn't run on a Perl 6 compiler, or vice versa and different enough that automatic translation from one to the other is quite challenging. So if you know Perl 5, Perl 6 feels familiar, but under the hood a lot has changed.

For example, Perl 6 is object oriented to the core, where Perl 5 is not.

NV:  Are there other fundamental differences such as Perl 5 producing a Concrete syntax tree while Perl 6 has an Abstract one, and that Perl5 is tied to XS while Perl 6 runs on a VM?

ML: Well, first of all Perl 6 has multiple implementations and currently two, Rakudo and Niecza, are in competition. Rakudo runs on the parrot VM, Niecza on CLR/.NET. Perl 5 is tied to C and XS, whereas the Perl 6 compiler writers have more freedom in the choice of backends. Perl 5 compiles a program to bytecode, but always keeps that in memory. Both Rakudo and Niecza can compile to bytecode and store it on disc.

I'm not sure I understand what you mean by a "concrete syntax tree"

NV: In the sense that an abstract syntax tree is more general/vague and can be used for language interop (since it is easier to transform), for example on a VM hosting a variety of languages.

ML: I've never seen language interop on the syntax tree level; it usually works simply by calling methods on foreign objects (and that's a very low-level concept) but an abstract syntax tree can be used to compile the program to multiple backends. In fact, that's a plan for Rakudo: to target the JVM in addition to Parrot so that we can have Perl 6 on Java-based mobile devices, and can benefit from the awesome JIT compiler and garbage collectors that exist in various JVM implementations.

NV:  That is pretty interesting. So while everything started on Parrot you are looking into other directions too?

ML: Yes, and Parrot has been looking into other directions too, seeking to optimize for other languages than Perl 6.

 


NV: Are you concerned that Parrot is too tightly related to Perl 6 and therefore doesn't get that much attention from other dynamic languages?

ML: No. That was Allison Randall's concern, when she was Architect of the Parrot VM. In the end it didn't really attract developers from other dynamic languages, but made life harder for the Rakudo developers.

NV: Why is that?

ML: Well, during that time features of Parrot were optimized for general usage, even if it made them less suitable for implementing Perl 6

NV: I see. So what does Parrot offer a language implementer? Better parsing facilities or out of the box like a GC or JIT?

ML: It has a pretty good GC and there's a compiler toolkit, which makes it quite easy to parse languages, and generate parrot's assembler code from it. It also offers pretty good abstraction of OS-specific features and it lets you write extensions in C. For example, Rakudo uses a custom dispatcher for multi-dispatch and has several custom opcodes to speed up hot paths.

NV: How do you create a parser for a language on Parrot? I understand that it is not done in the traditional lex/yacc/BNF grammar way but with Perl 6 regexes?

ML: Yes. Perl 6 regexes are somewhat of a mixture of traditional regexes (for lexing), and a BNF-like structure for parsing.

One of the reasons that people think regexes are stopgap solutions is that most languages don't have a proper way to reuse regexes. In Perl 6 regexes, a regex is a method on the grammar, and you can call other regexes just as you can call other methods in normal code and you can use all the usual code reuse mechanisms, like calling other grammars ( basically delegation), inheritance, and role composition.

And more than that, it provides you with a way to trigger actions that are tied to grammar rules. So for each grammar rule, a method in a corresponding "actions" class is triggered, and that makes it easy to create a syntax tree right away

NV: Are those actions generated behind the scenes or is the developer required to write and wire them as well?

ML: The developer needs to write them, but the grammar + actions mechanism provides a convenient way to trigger them, and to assemble the result in a tree.

 

perl6

NV: What about Perl 6 mutable grammar? How does it work and how is that an advantage?

ML: For example you can allow the language you are parsing to add a new operator, and immediately use them. It also means that it is easy to create a dialect of grammar. For example, if you have a grammar for JSON, it is trivial to override the whitespace rule, and make it accept comments too (which aren't part of standard JSON).

Speaking of which, here is a JSON grammar in Perl 6: https://github.com/moritz/json/blob/master/lib/JSON/Tiny/Grammar.pm

I think it is  a nice example, because it's self-contained, useful and short. So to extend that, you'd simply write

token ws { \s* [ '//' \N* \n ]* }

and then everywhere that whitespace is allowed, // comments until the end of the line are allowed too, and you can do that in a subclass if you want.

NV: Apart from those state of the art innovations, why switch to Perl 6, what does it offer to programers, i.e what features stand up in a programming end-level? Is there a niche field that it really shines in?

ML: First, it supports both object-oriented and functional programming styles really well and then it has lots of features that no other language has combined before.

For example, Perl 6 has lazy lists, which are usually only found in pure functional languages, and it offers a meta object system, which means you can create your own object types (like classes, roles, enumerations).

Also, its function signatures are powerful enough to compete with the pattern matching facilities you might know from functional languages like Haskell. And the nice thing is that you don't have to know all those fancy features to get started -- you can write well-readable Perl 6 code with basic knowledge, but an experienced programmer can use all the "power" features.

Oh, and it is dynamically typed, but you can chose to add type annotations, and profit from enhanced speed and safety where you do.

NV: For people that have experience in other classic or static OOP languages, Roles can be used to construct Interfaces, Abstract classes, as well composition over inheritance?

ML: Right, you can use them both for interfaces as you know them from Java, and for code reuse, where they detect method conflicts at compile time, which multiple inheritance usually does not.

NV: What kind of threading model does Perl 6 adopt?

ML: That's one of the things that is still being ironed out. Larry wants some sort of unification of threads and events, but so far no compiler has decent threads support, so it is all speculative for now. There are scientific papers that achieve such a symbiosis in Haskell, in the end we want to provide something similar, but with Perl 6ish syntax.

NV: The Perl 6 designers thought of a new cool feature that they want to implement. Ultimately it has also to be translated to an underlying opcode. So who drives development and evolution on the platform? Is it Perl 6 to Parrot or the opposite?

ML:There is innovation in both projects, it's a give and take. When the Rakudo developers want to implement a new feature, sometimes they request a new feature from Parrot (or even patch it in themselves). On the other hand, if Parrot offers a new API that promises easier code generation or faster execution, it is Rakudo that follows.

NV: Nice,so in the end what would you tell aspiring language implementers so as to lure them into Parrot instead of them opting for another VM like .NET?

Look at the Squaak tutorial (http://en.wikibooks.org/wiki/Parrot_Virtual_Machine/Squaak_Tutorial) and see if you'd like to develop your compiler like this. If yes, chose Parrot.

 

Related Articles

Perl 6 Preview Release


raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.

Banner

Last Updated ( Thursday, 01 November 2012 )