Pugs Apocryphon 1
Overview of the Pugs project

What is this document about?

The Pugs Apocrypha are a series of documents, written in question/answer format, to explain the design and implementation of Pugs. This document (PA01) is a higher-level overview of the project.

What is Pugs?

Started in February 2005, Pugs is an implementation of the Perl 6 language. Autrijus Tang is responsible for the design and development of Pugs with help from a group of committers and contributors.

What is Perl 6?

Perl 6 is the next major revision of Perl, a context-sensitive, multi-paradigmatic, practical programming language, designed by a team led by Larry Wall. The Pugs project has been enthusiastically welcomed by the Perl 6 team.

Has Perl 6 been specified?

By December 2004, most of Perl 6 has been specified as a series of Synopses. Although not considered final, it is now stable enough to be implemented. Many of the Synopses are based on Larry's Apocalypses. Sometimes the design team releases Exegeses, which explain the meaning of Apocalypses. Pugs adheres to the Synopses, referring to Apocalypses or Exegeses when a Synopsis is unclear or imprecise.

What does “Apocrypha” mean?

The word Apocrypha, from the Greek απόκρυφος, “hidden”, refers to religious works that are not considered canonical, or part of officially accepted scripture. The proper singular form in Greek is Apocryphon.

What is the relationship between Apocrypha and the Perl 6 design documents?

Apocalypses and Synopses cover the Perl 6 language in general; Apocrypha are specific to the Pugs implementation. Like Parrot Design Documents, Apocrypha will be constantly updated according to the status of Pugs.

Will Pugs implement the full Perl 6 specification?

Yes. Pugs always targets the latest revision of Perl 6 Synopses. As soon as a new revision or a new Synopsis is published, incompatibilities between Pugs and the new version will be considered bugs in Pugs.

Is Pugs free software?

Yes. It is available under both GPL version 2 and Artistic License version 2.0b5. Once the final version of Artistic 2.0 is released, Pugs will adopt it.

Is Pugs funded by the Perl Foundation?

No. After receiving three Perl Foundation grants on various projects, Autrijus decides it would be more helpful to donate time to the Perl 6 project by hacking Pugs, rather than asking TPF for money to do the same thing.

Where can I download Pugs?

For the very latest version of Pugs, check out the source from Subversion or darcs repositories. Periodic releases are available on CPAN under the Perl6-Pugs namespace. (By the way, if you'd like offline working with the Subversion repository, the svk client may be of interest. But using vanilla svn is fine.)

How do I build Pugs?

Pugs uses the standard Makefile.PL build system, as detailed in the README file. Since Pugs is written in Haskell, you will need Glasgow Haskell Compiler (GHC) 6.4 or above. Please download a binary build for your platform; compiling GHC from source code can take a very long time.

What is Haskell?

Haskell is a standardized, purely functional programming language with built-in lazy evaluation capabilities. While there are several different implementations available, currently Pugs needs to be built with GHC, because it uses several GHC-specific features.

What is GHC?

GHC is a state-of-the-art compiler and interactive environment, available under a BSD-style license. Itself written in Haskell, GHC can compile Haskell to bytecode, C code, and machine code on some platforms. GHC has an extensive library, numerous language extensions, and a very capable optimizer (with some help from a Perl 5 program). As such, it provides an excellent platform to solve Perl 6’s bootstrapping problem.

What is the Perl 6 bootstrapping problem?

The goal of the Perl 6 project is to be self-hosting: The Perl 6 compiler needs to be written in Perl 6 itself, and must parse Perl 6 source code with Perl 6 Rules, which is a subset of the Perl 6 language. The generated code must also contain an evaluator that can execute Perl 6 code on the fly. The only way to break this cycle of dependency is by first implementing some parts in other languages, then rewrite those parts in Perl 6.

What was the initial bootstrapping plan?

According to the Parrot FAQ, the initial plan was to bootstrap via Perl 5: First we extend Perl 5 to run on the Parrot virtual machine (via B::Parrot or Ponie), and then implement the Perl 6 compiler in Perl 5, which will be translated to Perl 6 via a p5-to-p6 translator. However, although part of the Rule system was prototyped in Perl 5 as Perl6::Rules, it was not mature enough to build a compiler on. As such, the plan was revised to bootstrap via C instead.

What was the revised bootstrapping plan?

According to an early 2005 proposal, the plan is to first implement the Rule engine in C (i.e. PGE), use it to parse Perl 6 into Parrot as an abstract syntax tree (AST), and then implement an AST evaluator as part of Parrot. Ponie and p5-to-p6 are still in progress, but they are no longer critical dependencies in the bootstrapping process.

How can Pugs help Perl 6 to bootstrap?

In a bootstrapping process, there are often many bottlenecks, which prevent people from working on things that depend on them. For example, one cannot easily write unit tests and standard libraries for Perl 6 without a working Perl 6 implementation, or work on an AST evaluator without an AST interface. Pugs solves such deadlocks by providing ready substitutes at various levels of the process.

How can Pugs help the Perl 6 language design?

Without a working implementation, it is very hard to spot inconsistencies and corner cases in the specification. However, if a design problem is found late into the implementation, it may require costly re-architecture for everything else. By providing a working Perl 6 implementation, Pugs can serve as a proving ground to resolve problems as early as possible, as well as encourage more people to exercise Perl 6’s features.

Why did you choose Haskell?

Many Perl 6 features have similar counterparts in Haskell: Perl 6 Rules corresponds closely to Parsec; lazy list evaluation is common in both languages; continuation support can be modeled with the ContT monad transformer, and so on. This greatly simplified the prototyping effort: the first working interpreter was released within the first week, and by the third week we have a full-fledged Test.pm module for unit testing.

Is Pugs a compiler or an interpreter?

Similar to Perl 5, Pugs first compiles Perl 6 program to an AST, then executes it using the built-in evaluator. However, in the future Pugs may also provide a compiler interface that supports different compiler backends.

Which compiler backends do you have in mind?

If implemented, the first compiler backend will likely generate Perl 6 code, similar to the B::Deparse module. The next one may generate Haskell code, which can then be compiled to C by GHC. At that point, it may make sense to target the Parrot AST interface. We can also add other backends (such as Perl 5 bytecode) if people are willing to work on them.

Do you have a roadmap for Pugs development?

The major/minor version numbers of Pugs converges to 2*π; each significant digit in the minor version represents a milestone. The third digit is incremented for each release. The current milestones are:

How portable is Pugs?

Pugs runs on Win32, Linux and many flavors of Unix systems. See GHC’s porters list and download page for details. Starting from 6.2.0, the Pugs team will also provide binary builds on selected platforms.

How fast is Pugs?

The parser part of Pugs is very fast, due to its robust underpinning in Parsec. However, the Pugs evaluator is currently not optimized at all: dispatching is around 1000 operators per second on a typical PC, which is nearly 100 times slower than Perl 5. Still, it is fast enough for prototyping language features; if you need fast operations in Pugs, please consider helping out the Compiler backend.

Is there a CPAN for Perl 6 modules?

No. The current preferred method for submitting Perl 6/Pugs modules is to ask for a committer bit at openfoundry.org and to add your module to the Pugs source tree under the modules/ directory. It is also helpful to include test suites with your modules as well, so we can know when they work.

However, all is not lost. If you are patient you can wait for Pugs::MakeMaker and Module::Install::Pugs to come out on CPAN. Once they are available individually on CPAN, you can begin submitting things to CPAN, because that means we've figured out the distribution problem. Of course, suggestions are always welcome.

Can Pugs work with Perl 5 libraries?

Not yet. However, we may write a Inline::GHC module in the future, allowing interaction between Perl 5 modules and Haskell modules, similar to Autrijus’ previous work on Inline::MzScheme; if that happens, then it is trivial to build Inline::Pugs on top of it. Alternatively, we may implement a Perl 5 source code parser that emits Pugs AST code, which will make pure Perl modules work on Pugs. Finally, it is also conceivable to compile Pugs AST into Perl 5 AST, but that is even more speculative.

Can Pugs work with Haskell libraries?

Currently, you can statically link Haskell libraries into Pugs primitives, by modifying a few lines in Prim.hs. We are considering writing a simple interface to hs-plugins, which will let Pugs dynamically load Haskell libraries, even inline Haskell code directly within Perl 6.

Can Pugs work with C libraries?

Not yet. However, HaskellDirect seems to provide an easy way to interface with C, CORBA and COM libraries, especially when combined with hs-plugins described above.

I know Perl 5, but not Haskell. Can I develop Pugs?

Sure! The standard libraries and unit tests that come with Pugs are coded in Perl 6, and there is always a need for more tests and libraries. All you need is basic familiarity of Perl 5, and a few minutes to get acquainted with some small syntax changes. You will likely pick up some Haskell knowledge along the way, too.

I know Haskell, but not Perl 5. Can I develop Pugs?

Sure! Perl 6 and Haskell have many things in common, such as type-based function dispatch, first class functions and currying, so picking up the syntax is relatively easy. Since there are always some TODO tests for features in need of implementation, it is never hard to find something to do.

I have learned some Perl 6. What can I do with Pugs?

Look at the examples/ directory to see some sample programs. Some people are already writing web applications and report systems with Pugs. If you run into a missing feature in Pugs, please let us know so we can implement it.

Where can I learn more about Haskell?

The Haskell homepage and the Wiki are good entry points. Of the many online tutorials, Yet Another Haskell Tutorial is perhaps the most accessible. Due to the ubiquitous use of Monad transformers in Pugs, All About Monads is also recommended. For books, Algorithms: A Functional Programming Approach, Haskell: The Craft of Functional Programming and The Haskell School of Expression are fine introductory materials. Finally, the #haskell channel on freenode is full of helpful and interesting people.

Where can I learn more about Perl 6?

The Perl 6 homepage provides many online documents. Every week or two, a new Perl 6 list summary will appear on Perl.com; it is a must-read for people who wish to follow Perl 6’s progress. For books, Perl 6 and Parrot Essentials and Perl 6 Now are both helpful.

Where can I learn more about implementing programming languages?

Types and Programming Languages is an essential read; Pugs started out as a self-initiated study of the text, and it continues to be an important guide during the implementation. Its sequel, Advanced Topics in Types and Programming Languages, is also invaluable. It may also help to get acquainted with other multi-paradigmatic languages, such as Mozart/Oz, Curry and O’Caml. Finally, the detailed GHC commentary describes how GHC itself was implemented.

I’d like to help. What should I do?

First, subscribe to the perl6-compiler mailing list by sending an empty mail to perl6-compiler-subscribe@perl.org. Next, join the #perl6 IRC channel on irc.freenode.net to find out what needs to be done. Commit access is handed out liberally; contact the Pugs team on #perl6 for details. See you on IRC!