Categories
Ads
Your advertisement could be here -- Contact me!.
Thu, 03 May 2012
SQLite support for DBIish
Permanent link
DBIish, the new database interface for Rakudo Perl 6, now has a working SQLite backend. It uses prepared statements and placeholders, and supports standard CRUD operations.
Previously the SQLite driver would randomly report "Malformed UTF-8 string" or segfault, but usually worked pretty well when run under valgrind. The problem turned out to be a mismatch between the caller's and the callee's ideas about memory management.
In particular, parrot's garbage collector would deallocate strings passed to sqlite3_bind_text after the call was done, but sqlite wants such values to stay around until the next call to sqlite3_step in the very least.
Fixing this mismatch was enabled by this patch, which lets you mark strings as explicitly managed. Such strings keep their marshalled C string equivalent around until they are garbage-collected themselves. So now the sqlite driver keeps a copy of the strings as long as necessary, and the SQLite tests pass reliably.
Currently it still needs the cstr branches in the nqp and
zavolaj repositories, but they will be merged soon -- certainly before the May
release of Rakudo.
Sat, 28 Apr 2012
Meet DBIish, a Perl 6 Database Interface
Permanent link
In the aftermath of the Oslo Perl 6 hackathon 2012, I have decided to fork and rename MiniDBI. MiniDBI is intended as a compatible port of Perl 5's excellent DBI module to Perl 6. While working on the MiniDBI backends, I noticed that I became more and more unhappy with that. Perl 6 is sufficiently different from Perl 5 to warrant different design decisions in the database interface layer.
Meet DBIish. It started with MiniDBI's code base, but has some substantial deviations from MiniDBI:
- Connection information is passed by named arguments to the driver (instead of a single DSN string)
- Different naming of several methods. There's not much point in having
both
fetchrow_arrayandfetchrow_arrayrefin Rakudo.fetchrowsimply returns an array or a list, and the caller decides what to do with it. - Backends only need to implement
fetchrowandcolumn_names, and get all the other fetching methods (likefetchrow-hash,fetchall-hash) for free. - Error handling from DB connection and statement handle are unified into a single row
The latter two changes brought quite a reduction in backend code size.
My plans for the future include experimenting with different names and maybe totally different APIs. When a language has lazy lists, one can simply return all rows lazily, instead of encouraging the user to fetch the rows one by one.
Currently the Postgresql and mysql backends support basic CRUD operations, Postgresql with proper prepared statements and placeholders. An SQLite backend is under way, but still needs better support from our native call interface.
Mon, 23 Apr 2012
Perl 6 Hackathon in Oslo: Report From The Second Day
Permanent link
Second day of the Perl 6 Patterns Hackathon. My plans to get the rest of placeholders and prepared statements working in the Postgresql backend for MiniDBI succeed about 10 minutes after midnight. I just wanted to give them a very quick try before going to bed, and was successful. Then I went to sleep.
It was night, and it was morning. Second day.
Next I wrote an SQLite backend for MiniDBI. It blocked on missing features in our native call infrastructure, on which arnsholt worked in parallel. So I haven't had a chance to try the SQLite backend yet. It probably requires some substantial amount of work before it will run, but at least it compiles.
I also investigated prepared statements and placeholders for the mysql backend. This is much less straight forward, because it requires filling in members of structs, not just function calls. This by itself wouldn't be much a problem, our native call infrastructure supports that. The problem is that it's a struct of mixed "private" and "public" members, so modelling the structure in Perl 6 requires modeling private data of the mysql client library. While possible, I don't find it desirable, because it is rather fragile.
Another notable event was the hacking dojo, where about 8 of us collaborated to write a roman numeral conversion, using pair programming, and fixed cycles of first writing a failing test, then getting it to run in the simplest possible way, and finally refactoring it. It was quite an interesting and fun experience.
I spent much of the rest of the hackathon discussing things. For example Patrick Michaud gave a quick walk through of how lists and related types are implemented and iterated in Rakudo.
In the evening we had very tasty Vietnamese food, and generally a good time.
Again it was a very productive and enjoyable day, and I'm very grateful for being invited to the Hackathon.
Sat, 21 Apr 2012
Perl 6 Hackathon in Oslo: Report From The First Day
Permanent link
Yesterday I arrived in the beautiful city of Oslo to attend the Perl 6 Patterns Hackathon. Yesterday we visited a pub, had great discussions, food and beverages, and generally a very good time.
Today we met at 10 am, and got straight to hacking. We are located in an office in the 6th floor of a big building, with a nice view over the center of town, harbor, and even the Holmenkollen.
I worked on the backtrace printer, which in alarmingly many cases reported
Error while creating error string: Method 'message' not found for
invocant of class 'Any', which wasn't too helpful.
It turns out there were actually two causes. One was a subtle error in the
backtrace printer that was triggered by stricter
implementation of the specification, which was easy to find. The
second bug was harder to find, considering that you don't get easily get
backtraces from errors within the backtrace printer. In the end it was the
usage of a code object in boolean context, which turned out to be harmful.
Because regexes are also code objects, and in boolean context they search for
the outer $_ variable and try to match the regex against it.
Which failed. Hard to find, but easy to fix.
My second big project today was database connectivity. Part of it was pestering Jonathan to fix the issues that arose from module precompilation mixed with calling C modules, and testing all the iterations he produced. I'm happy to report that it now works fine, which speeds up development quite a bit.
I also fixed the postgres driver. The root cause for the failing tests turned out to be rather simple too (a missing initialization), so simple that it's embarrassing how long it took me to find out. On the plus side I improved the code quite a bit in passing.
So now all tests in MiniDBI pass, which is a nice milestone, and an indication that we need more tests.
Tomorrow I plan to change the postgres driver to use proper prepared statments.
But the real value of such hackathon comes from interacting with the other hackers. I'm very happy about lots of discussions with other core hackers, as well as feedback and patches from new users and hackers.
At this occasion I'd also like to thank the organizers, Salve J. Nilsen, Karl Rune Nilsen and Jan Ingvoldstad. It has been a great event so far, both fun and productive. You are doing a great service to the Perl 6 community, and to the hackers you have invited.
Thu, 12 Apr 2012
Rakudo Hack: Dynamic Export Lists
Permanent link
Rakudo's meta programming capabilities are very good when it comes to objects, classes and methods. But sometimes people want to generate subroutines on the fly and use them, and can't seem to find a way to do it.
The problem is that subroutines are usually stored (and looked up from) in
the lexical pad (ie the same as my-variables), and those lexpads
are immutable at run time.
Today I found a solution that lets you dynamically install subroutines with
a computed name into a module, and you can then use that module
from elsewhere, and have all the generated subroutines available.
module A { BEGIN { my $name = 'foo'; my $x = sub { say 'OH HAI from &foo' } but role { method name { $name } }; trait_mod:<is>(:export, $x); } }
Inside the module first we need a BEGIN block, so that the
is export trait will run while the module is being compiled, and
thus knows which module to associate the subroutine to.
Next comes the actual code object that is to be installed. Since the
export trait inspects the name of the subroutine, we need to give
it one. Doing that dynamically can be done by overriding the name
method, here by mixing in a role with such a method into the code object.
Finally comes the part where the export trait is applied. The code here uses knowledge of the calling conventions that hide behind a trait.
A different script can then write
use A; foo();
And access the dynamically exported sub just like any other.
In future there will hopefully be much nicer APIs for this kind of fiddling, but for now I'm glad that a workaround has been found.
Tue, 10 Apr 2012
Perl 6 Hackathon in Oslo: Be Prepared!
Permanent link
The Oslo Perl Mongers invite to the Perl 6 Patterns Hackathon in Oslo. I have previously suggested that we hack on database connectivity, and so far only got positive feedback. If you want to help, here is what you can do to be prepared:
- Get a github account
- Build and install Rakudo
- Build and install zavolaj/NativeCall
- download MiniDBI
- install and prepare databases to talk to
To hack efficiently on those projects, and to benefit from last-minute fixes, you should obtain Rakudo, NativeCall and MiniDBI from their git source repositories -- that last release is already outdated.
Here are the instructions in detail. If at any point you run into problems, feel free to ask on the #perl6 IRC channel or the perl6-users@perl.org mailing list.
Get a Github account
All the interesting Perl 6 code lives in git repositories on github. If you don't have an account already, sign up -- it's free.
Build and install Rakudo
This step is described well on the Rakudo homepage. Please follow the instruction in section "Building the compiler from source".
For the following steps it is important that you have a fresh
perl6 executable file in your $PATH. If you have downloaded
rakudo to /home/you/p6/rakudo/, you can run the command
PATH=$PATH:/home/you/p6/rakudo/install/bin
(and put it in your ~/.bashrc file if you want it permanently available, not just in this shell).
Build and install zavolaj/NativeCall
NativeCall.pm is the high-level interface for calling C functions from Perl 6 code. Install it:
$ git clone git://github.com/jnthn/zavolaj.git $ cd zavolaj $ cp lib/NativeCall.pm6 ~/.perl6/lib/
If you download and install ufo, you can use it create a
Makefile for zavolaj. Then you can also run make test. On Linux it might not find the
test libraries (which is mostly harmless, because you usually call libraries
that are installed into your operating system, like those from mysql or
postgres). In this case you should run LD_LIBRARY_PATH=. make
test instead.
Download MiniDBI
That's not hard at all:
$ git clone git://github.com/mberends/MiniDBI.git
Install and Prepare Databases
So far, MiniDBI has (somewhat limited) support for mysql and postgres. Since it is always easiest to start from (at least somewhat) working code, I strongly recommend that you install at least one of those database engines.
Most modern Linux systems allow an easy installation via the package manager, and there are installers available for other operating systems. Be sure to also install the headers or development files if they come as extra packages.
Mysql
As mysql root user, run these statements:
CREATE DATABASE zavolaj; CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testpass'; GRANT SELECT ON mysql.* TO 'testuser'@'localhost'; GRANT CREATE ON zavolaj.* TO 'testuser'@'localhost'; GRANT DROP ON zavolaj.* TO 'testuser'@'localhost'; GRANT INSERT ON zavolaj.* TO 'testuser'@'localhost'; GRANT DELETE ON zavolaj.* TO 'testuser'@'localhost'; GRANT LOCK TABLES ON zavolaj.* TO 'testuser'@'localhost'; GRANT SELECT ON zavolaj.* TO 'testuser'@'localhost';
Postgres
Launch psql as the postgres user and run these
statements:
CREATE DATABASE zavolaj; CREATE ROLE testuser LOGIN PASSWORD 'testpass'; GRANT ALL PRIVILEGES ON DATABASE zavolaj TO testuser;
You should now be able to connect with:
psql --host=localhost --dbname=zavolaj --username=testuser --password
(psql will ask you for the password. Enter testpass).
Other Databases
If you want to work on a backend for another database, it helps to have that database installed. Sqlite is an obvious choice (easy to install, zero setup), but of course there are other free database too, like firebird.
Project ideas
There is a lot of stuff to do. What follows is only a loose, incomplete collection of ideas.
- Fix the postgres backend to actually pass its tests
- Both mysql and postgres backends don't implement placeholders properly; change them (or one of them) to pass the placeholder values out of band.
- Write an sqlite backend
- Currently the user builds a DSN ("data source name") string out of the driver name, database name, db host name and so on, and then the driver parses it again. One could change that to pass all the information as named parameters instead.
- Improve test coverage. For example test that numbers round-trip with the correct types.
- Write a small application that uses a database. That's the best way to see if MiniDBI and the backends work.
Tue, 20 Mar 2012
Upcoming Perl 6 Hackathon in Oslo, Norway
Permanent link
The Oslo Perl Mongers are inviting to the Perl 6 Patterns Hackathon in Oslo in one month, and I very much look forward to being there.
Hackathons can be quite fun and productive if many programmers focus on the same goal. So to make the hackathon a success, I'm willing to work on whatever we decide to set as our goal(s).
One topic that is dear to me, and that is approachable by a horde of programmers (and guided by one or two Rakudo core hackers) is bringing database access into a usable state.
With muchly improved support for calling C functions and NativeCall.pm we should have enough infrastructure for access mysql, postgres and SQLite databases. MiniDBI aims to provide some basic convenience, but currently only the mysql driver partially works.
I believe that with concentrated effort, MiniDBI and the rest of the infrastructure can be improved to the point that it is usable, and other modules can start to rely on it. Databases usable in Perl 6, doesn't that sound good?
I'll see what kind of feedback I get on this idea, and if it's positive, I'll follow up with instructions on how to install the prerequisites for hacking on MiniDBI and its drivers.
Tue, 28 Feb 2012
Current State of Exceptions in Rakudo and Perl 6
Permanent link
It's been a while since my last update on my grant work on exceptions for Perl 6 and Rakudo, and I can report lots of progress.
The work on Rakudo's exception system made me realize that we conflated two concepts in the base exception type: on the one hand the infrastructure for reporting errors and backtraces, and on the other hand holding some sort of error message as an attribute.
As a result, we now have a base class called Exception from
which all exception types must inherit. When a non-Exception
object is passed to die(), it is wrapped in an object of class
X::AdHoc. Other error classes can decide to generate the error
message without having an attribute for it (for example hard-coded in a
method).
Typed exceptions are now thrown not only from the setting, but also from the compiler itself, namely the grammar and the action methods. In fact the majority of errors from these two parts of the compiler are now handled with dedicated exception types.
The most user-visible change is a new and improved backtrace printer, which produces usually much shorter and more readable backtraces. The old one is still available on demand. Consider the program
sub f { g() for 1..10; } sub g { die 'OH NOEZ' } f;
The old backtrace printer produced:
OH NOEZ in sub g at /home/moritz/p6/rakudo/ex.pl:4 in block <anon> at /home/moritz/p6/rakudo/ex.pl:2 in method reify at src/gen/CORE.setting:4471 in method reify at src/gen/CORE.setting:4376 in method reify at src/gen/CORE.setting:4376 in method gimme at src/gen/CORE.setting:4740 in method eager at src/gen/CORE.setting:4715 in method eager at src/gen/CORE.setting:1028 in sub eager at src/gen/CORE.setting:5000 in sub f at /home/moritz/p6/rakudo/ex.pl:2 in block <anon> at /home/moritz/p6/rakudo/ex.pl:5 in <anon> at /home/moritz/p6/rakudo/ex.pl:1
Where the eager, gimme and reify methods come from the 'for' lop, which is
compiled to the equivalent of eager (1..10).map: { g() }.
The new backtrace printer produces
OH NOEZ in sub g at ex.pl:4 in sub f at ex.pl:2 in block <anon> at ex.pl:5
It is also a special pleasure to report that after a walk through a change to throw a typed exception, we've received a pull request by a new developer which also changes an exception from X::AdHoc to a dedicated type.
Sat, 18 Feb 2012
Results from the Prisoner's Dilemma Challenge
Permanent link
The Iterated Prisoner's Dilemma Challenge is now closed; several interesting solutions have been submitted.
Of the basic strategies, tit-for-tat (doing what the opponent did the last time, starting off with cooperating) is usually the strongest. Since the random strategy is, well, random, the results fluctuate a bit.
Most submitted strategies are a variation on tit-for-tat, modified in some way or another to make it stronger. All submissions contained a strategy that is stronger than tit-for-tat when tested against the basic strategies only, though the interaction with other new strategies made some of them come out weaker than tit-for-tat.
Submitted Strategies
Without any further ado, here are the strategies and a few comments on them.
Turn the Other Cheek
## Dean Serenevy; received on 2012-02-07 %strategies<turn-other-cheek-no-deal-with-devil-once-bit-twice-shy-variety-is-the-spice-o-life> = sub (:@mine, :@theirs, *%) { my ($bitten, $shy, $they-coop) = (0, 0, False); for @mine Z @theirs -> $me, $them { if $them { $they-coop = True; } if $me and !$them { $bitten++; $shy = 0; } if !$me { $shy++ } } return True if 0 == $bitten; # Cooperate if we have never been bitten return True if 1 == $bitten and 0 == $shy; # Turn the other cheek once return False unless $they-coop; # Screw you too! return $shy >= (2 ** ($bitten-1)).rand # Once-bitten rand() shy };
Inevitable Betrayal
## Andrew Egeler, received 2012-02-09 %strategies<inevitable-betrayal> = &inevitable-betrayal; sub inevitable-betrayal (:@theirs, :$total, *%) { +@theirs < ($total-1) ?? @theirs[*-1] // True !! False } %strategies<evil-inevitable-betrayal> = &evil-inevitable-betrayal; sub evil-inevitable-betrayal (:@theirs, :$total, *%) { +@theirs < ($total-1) ?? @theirs[*-1] // False !! False }
These are variations on tit-for-tat and evil-tit-for-tat which always defect in the last round, because then the opponent can't retaliate anymore.
In a typical Iterated Prisoner's Dilemma contest, strategies don't know how many rounds are being played, just to avoid this behavior.
Tit for D'oh and Watch for Random
## Solomon Foster, receievd 2012-02-10 %strategies<tit-for-doh> = -> :@theirs, :$total, *% { @theirs < $total - 1 ?? (@theirs[*-1] // True) !! False } %strategies<watch-for-random> = -> :@theirs, *% { @theirs > 10 && @theirs.grep(* == False) > 5 ?? False !! (@theirs[*-1] // True) };
tit-for-doh is the same as inevitable-betrayal. watch-for-random defects forever once the opponent has defected too often.
Me
## Audrey Tang, received 2012-02-17 %strategies<me> = -> :@theirs, *% { my role Me {}; (@theirs[*-1] // Me).does(Me) but Me };
This strategy uses a mixin in its returned boolean values to find out when
it plays against itself, or against a strategy that copies its values from
@theirs (ie tit-for-tat derivatives), in which case it cooperates.
This games the system, though doesn't explicitly violates the stated rules.
Audrey also deserves two dishonorable mentions for two solutions that game the test harness or the other strategies by exploiting the technically imperfect sandboxing:
au => -> :@theirs, *% { use MONKEY_TYPING; my role TRUE {}; augment class Bool { method Stringy(Bool:D:) { self.^does(TRUE) ?? 'True' !! 'False' } } False but TRUE; }, amnesia => -> :@mine, :@theirs, *% { my role Uh {}; my $rv = (@theirs[*-1] // Uh).does(Uh) but Uh; @mine = @theirs = (); $rv; },
Those two strategies did not compete in the tournament
Lenient in the Beginning, Then Strict
I've written my own two strategies before the tournament started. Here is the original, I've only changed the signatures to run under current Niecza:
# a tit for tat, but a bit more friendly at the beginning # to avoid hacking on forever on evil-tit-for-tat, # but be very stringent when the other one defects too often sub moritz-ctft(:@theirs, :$total, *%) { return True if @theirs < 3; return False if @theirs.grep(*.not).elems > ($total / 10); @theirs[*-1]; }; %strategies<moritz-ctft> = &moritz-ctft; # the evil clone... sub moritz-ectft(:@theirs, :$total, *%) { return True if @theirs < 3; return False if @theirs.grep(*.not).elems > ($total / 10); # did you believe in happy ends? return False if @theirs + 1 == $total; @theirs[*-1]; }; %strategies<moritz-ectft> = &moritz-ectft;
Results
The results vary quite a bit between runs, mostly because of the random strategy.
Here is the output from a sample run. Please don't use this for determining the "winner", because it is just a random sample with no statistical significance.
SUMMARY 2588 moritz-ectft 2577 me 2560 moritz-ctft 2491 inevitable-betrayal 2483 tit-for-tat 2480 tit-for-doh 2399 turn-other-cheek-no-deal-with-devil-once-bit-twice-shy-variety-is-the-spice-o-life 2319 watch-for-random 2272 good 1876 evil-inevitable-betrayal 1862 evil-tit-for-tat 1538 random 1145 bad
You see, inevitable-betrayal and tit-for-doh are exactly the same strategy, but the random fluctuations place them on different sides of tit-for-tat. Which is why I won't declare a winner at all, there is simply no fair way to determine one.
At first I was surprised how well the me strategy performed. But then I noticed that with the given game harness, a strategy fighting against itself counts double (once for the first copy, once for the second copy). With only 13 strategies participating, and such close results, harmonizing perfectly with yourself gives you a critical advantage.
Visualizations
For each strategy you can find an image that shows how it worked with or against another strategy. Green means cooperate, red means defect, and the height of the bar is proportional to the resulting score.
- bad
- evil-inevitable-betrayal
- evil-tit-for-tat
- good
- inevitable-betrayal
- me
- moritz-ctft
- moritz-ectft
- random
- tit-for-doh
- tit-for-tat
- turn-the-other-cheek-no-deal...
- watch-for-random
Trying to Be Fair
In an attempt to reduce the impact of the random strategy, I've changed it to use the same random sequence against each player (and of course against itself, which totally skews that particular result).
Again the rankings vary between different runs of the same program, but now at least same strategies produce mostly the same result (turn-the-other-cheek also has a random component). An example output from such a run is
SUMMARY 2558 moritz-ectft 2543 moritz-ctft 2532 me 2457 inevitable-betrayal 2457 tit-for-doh 2445 tit-for-tat 2387 turn-other-cheek-no-deal-with-devil-once-bit-twice-shy-variety-is-the-spice-o-life 2314 watch-for-random 2248 good 1856 evil-inevitable-betrayal 1844 evil-tit-for-tat 1359 random 1100 bad
TL;DR
It was a lot of fun! Thanks to everybody who submitted a strategy.
Tue, 07 Feb 2012
Mini-Challenge: Write Your Prisoner's Dilemma Strategy
Permanent link
Here is a small task we considered for the Perl 6 Coding Contest, but not chose to not pursue. But it's a nice little challenge for your leisure time.
In the Prisoner's Dilemma, two suspected criminals can choose to not betray each other (which we call "cooperate"), or betraying the other ("defecting"). If only one suspect betrays the other, the traitor gets released and the betrayed one gets a long sentence; if both betray each other, both get a rather long sentence. If both cooperate, both get rather short sentences.
It becomes more interesting when the dilemma is repeated multiple times. Now instead of prison sentences the contestants are assigned scores, which add up over multiple rounds.
I challenge you to write one or two strategies for the iterated prisoner's dilemma, and send them to moritz@faui2k3.org no later than Friday February 17.
You'll find some basic strategies and a harness here. It runs on both newest Rakudo and Niecza.
The scoring is as follows, where True means cooperate and
False means defect:
my %scoring =
'True True' => [4, 4],
'True False' => [0, 6],
'False True' => [6, 0],
'False False' => [1, 1],
Your strategy should be a subroutine or block that accepts the named
parameters mine and theirs, which are lists
of previous decisions of your own algorithm and of its opponents, and
total, which is the number of laps to be played. It should
return True if it wishes to cooperate, and False to
defect.
Here is an example strategy that starts off with cooperating, and then randomly chooses a previous reaction of the current opponent:
sub example-strategy(:@theirs, *%) {
@theirs.roll // True;
}
Your strategy or strategies will play against each other and against the example strategies in the gist above. It is not allowed to submit strategies that commit suicide to actively support another strategy.
I too have written two strategies that will take participate in the contest. Here is the checksum to convince you I won't alter the strategies in response to the submissions:
6d4ba99b66e4963a658c8dcfc72922dd0f74e0ad prisoner-moritz.pl
Sat, 31 Dec 2011
Perl 6 in 2011 - A Retrospection
Permanent link
The change of year is a good occasion to look back. Here I want to reflect on the development of Perl 6, its compilers and ecosystem.
At the start of the year, masak's Perl 6 Coding Contest continued from 2010, concluding in the announcement of the winner. I must admit that I still haven't read all the books I won :-)
Specification
2011 was a rather quiet year in terms of spec changes; they were a mixture of responses to compiler writer and user feedback, and some simplifications and cleanups.
Positional parameters used to be allowed to be called by name; this feature is now gone. That both makes the signature binder simpler, and removes accidental dependencies on names that weren't meant to be public. Read the full justification for more background.
A small change that illustrates the cleanup of old, p5-inherited features was the change that made &eval stop catching exceptions. There is really no good reason for it to catch them, except Perl 5 legacy.
say now uses a different stringification than
print. The reasoning is that print is aimed at
computer-readable output, whereas say is often used for
debugging. As an example, undefined values stringify to the empty string
(and produce a warning), whereas say calls the .gist
method on the object to be said, which produces the type name on undefined
values.
An area that has been greatly solidified due to implementation progress is Plain Old Documentation or Pod. Tadeusz SoĊnierz' Google Summer of Code project ironed out many wrinkles and inconsistencies, and changed my perception of this part of the spec from "speculative" to "under development".
Rakudo
Rakudo underwent a huge refactoring this year; it is now bootstrapped by a new compiler called "nqp", and uses a new object model (nom).
It allows us to gain speed and memory advantages from gradual typing; for example the mandelbrot fractral generator used to take 18 minutes to run on a machine of mine, and now takes less than 40 seconds. Speedups in other areas are not as big, but there is still much room for improvement in the optimizer.
With the nom branch came support for different object representations. It makes it possible to store object attributes in simple C-like structs, which in turn makes it much easier and more convenient to interoperate with C libraries.
Tadeusz' work on Pod gave Rakudo support for converting Pod to plain text and HTML, and attach documentation objects to routines and other objects.
Rakudo now also has lazy lists, much better role handling, typed
exceptions for a few errors, the -n and -p command
line options, support for big integers, NFA-based support for proto regexes
and improvements to many built-in functions, methods and operators.
Niecza
It is hard to accurately summarize the development of Niecza in a few sentences; instead of listing the many, many new features I should give an impression on how it feels and felt for the user.
At the start of 2011, programming in niecza was a real adventure. Running some random piece of Perl 6 code that worked with Rakudo rarely worked, most of the time it hit a missing built-in, feature or bug.
Now it often just works, and usually much faster than in Rakudo. There are still some missing features, but Stefan O'Rear and his fellow contributors work tirelessly on catching up to Rakudo, and it some areas Niecza is clearly ahead (for example Unicode support in regexes, and longest-token matching).
Since Niecza is implemented on top of the Common Language Runtime (CLR) (which means .NET or mono), it makes it easy to use existing CLR-based libraries. Examples include an interactive fractal generator and a small Tetris game in Perl 6.
Perlito
Perlito aims to be a minimal compiler with multiple backends, which can be used for embedding and experimenting with Perl 6. It had several releases in 2011, and has interesting features like a Javascript backend.
Ecosystem
The presence of two usable compilers (and in the case of Rakudo, two viable but very different branches) has led to many questions about the different compilers. The new Perl 6 Compiler Feature matrix tries to answer the questions about the state of the implemented features in the compilers.
With Panda we now have a module installer that actually works with Rakudo. It still has some lengths to go in terms of stability and feature completeness, but it is fun to work with.
The new Perl 6 Modules page gives an overview of existing Perl 6 modules; we hope to evolve it into a real CPAN equivalent.
Community
This year we had another Perl 6 Advent Calendar, with much positive feedback both from the Perl 6 community and the wider programming community.
We were also happy to welcome several new prolific contributors to the Perl 6 compilers and modules. The atmosphere in the community still feels relaxed, friendly and productive -- I quite enjoy it.
The year ends like it started: with a Perl 6 Coding Contest. This is a good opportunity to dive into Perl 6, provide feedback to compiler writers, and most of all have fun.
Mon, 19 Dec 2011
Fourth Grant Report: Structured Error Messages
Permanent link
Progress on my grant for error message is slow but steady. Since my last report, I've done the following things:
- Merged the
nom-exceptionsRakudo branch, so now you can reliably throw Perl 6 objects as exceptions. - Implemented several error classes and roles in Rakudo
- Started to throw typed errors both from runtime libraries and from inside the compiler
- Hacked the default exception printer Rakudo to be much more flexible, for example you can now write exception classes that supress backtraces from the standard handler.
- Wrote tests for typed run time and compile time errors, and at the same time developed a test function that makes it easy to write such tests.
It's time for a quick review of how far I am along the various deliverables in the original grant proposal.
- D1: Specification. I think the hard work here is done already, what remains to do is finding good default and how to manipulate them (for example, how to generally switch on/off printing of backtraces?).
- D2: Error catalogue, tests: I've not worked on this one too much. The error classes and roles so far mostly served to exercise the implementation; going through the existing errors from the various compilers and formalizing them will be quite a bit of work, but only moderately complicated.
- D3: Implementation, documentation. Like D1, the hard part is mostly done. We can now throw errors from within the compiler actions and from the setting, next up will be the grammar. Then all places where errors are thrown need to be changed to use the new error classes. Again that'll be much work, but easy to do. Documentation is still missing.
All in all I feel I'm well on the way, and most complex decisions have been made.
For a more user oriented view of the new exception system I'd like to point you to my Perl 6 advent calendar post on exceptions.
Wed, 12 Oct 2011
The Three-Fold Function of the Smart Match Operator
Permanent link
In Perl 5, if you want to match a regex against a particular
string, you write $string =~ $regex.
In the design process of Perl 6, people have realized that you cannot
only match against regexes, but lots of other things can act as
patterns too: types (checking type conformance), numbers, strings,
junctions (composites of values), subroutine signatures and so on. So
smart matching was born, and it's now written as
$topic ~~ $pattern. Being a general comparison mechanism
is the first function of the smart match operator.
But behold, there were problems. One of them was the perceived need
for special syntactic forms on the right hand side of the smart match
operator to cover some cases. Those were limited and hard to
implement. There was also the fact that now we had two different ways
to invoke regexes: smart matching, and direct invocation as
m/.../, which matches against the topic variable
$_. That wasn't really a problem as such, but it was an
indicator of design smell.
And that's where the second function of the smart match operator
originated: topicalization. Previously, $a ~~ $b mostly
turned into a method call, $b.ACCEPTS($a). The new idea
was to set
the topic variable to $a in a small scope, which allowed many
special cases to go away. It also nicely unified with
given $topic { when $matcher { ... } }, which was
already specified as being a topicalizer.
In the new model, MATCH ~~ PAT becomes something like
do { $_ = MATCH; PAT.ACCEPTS($_) } -- which means that if
MATCH accesses $_, it automatically does what the
user wants.
Awesomeness reigned, and it worked out great.
Until the compiler writers actually started to implement a few more
cases of regex matching. The first thing we noticed was that
if $str ~~ $regex { ... } behaved quite unexpectedly.
What happend was that $_ got set to $str,
the match was conducted and returned a Match object. And then called
$match.ACCEPTS($str), which failed. A quick hack around
that was to modify Match.ACCEPTS to always return the
invocant (ie the Match on which it was called), but of course that was
only a stop gap solution.
The reason it doesn't work for other, more involved cases of regex invocations is that they don't fit into the "does $a match $b?" schema. Two examples:
# :g for "global", all matches
my @matches = $str ~~ m:g/pattern/;
if $str ~~ s/pattern/substitution/ { ... }
People expect those to work. But global matching of a regex isn't a simple conformance check, and that is reflected in the return value: a list. So should we special-cases smart-matching against a list, just because we can't get global matching to work in smart-matching otherwise? (People have also proposed to return a kind of aggregate Match object instead of a list; that comes with the problem that Match objects aren't lazy, but lists are. You could "solve" that with a LazyMatch type; watch the pattern of workarounds unfold...)
A substitution is also not a simple matching operation. In Perl 5, a s/// returns the number of successful substitutions. In Perl 6, that wouldn't work with the current setup of the smart match operator, where it would then smart-match the string against the returned number of matches.
So to summarize, the smart match operator has three functions: comparing values to patterns, topicalization, and conducting regex matches.
These three functions are distinct enough to start to interact in weird ways, which limits the flexibility in choice of return values from regex matches and substitutions.
I don't know what the best way forward is. Maybe it is to reintroduce a dedicated operator for regex matching, which seems to be the main feature with which topicalization interacts badly. Maybe there are other good ideas out there. If so, I'd love to hear about them.
Mon, 29 Aug 2011
Third Grant Report: Structured Error Messages
Permanent link
Progress on my grant for error message is slower than expected, as expected :-). Yes, you've read that sentence before.
In the past months, general hacking on the nom branch of Rakudo was just too much fun -- and partially a prerequisite for the exceptions work.
I did manage to redo the backtraces that are generated from error messages.
Backtraces are now generated mostly in Perl 6 code, making them much more
hackable. There's a Backtrace class, which is a list of
Backtrace::Frame objects, each knowing the code object associated
with it, as well as line number and file. (This is both specced and works in
Rakudo)
Routines can have the is hidden_from_backtrace trait, which
makes them not show up in the default backtrace stringification (one can still
request a .full string representation). This is useful for
routines which are internally used to generate exceptions, like
die().
Rakudo also has a --ll-exceptions command line option which
provides PIR-level backtraces, in the rare case the Perl 6 level backtraces
hide too much information.
I've also started the nom-exceptions branch in Rakudo, which
aims at lifting current limitations in Rakudo's exception handling. Currently
die() and friends generate a parrot exception, and then there's a
routine that fills the error variable $!. This routine generates
a new Exception object, and sticks the parrot exception into
it.
This practice means that if you create a subclass of
Exception, instantiate it and throw it, you still only get an
Exception in the error handler, not an object of the subclass.
Since the actual exception type is very important for the ongoing work, that
has to change. The branch mentioned earlier allows one to generate a Perl 6
exception, and pass that on as the payload of the parrot exception, which is
then unwrapped when filling $!.
As a proof of concept this works, but it suffers from not being robust
enough -- as it is, we could accidentally unwrap the payload of a
CONTROL exception, placing meaningless junk into $!.
So this needs a bit more work, which I plan to do this week (or next, if it
proves to be more difficult than anticipated).
As always, your feedback is very welcome.
Wed, 24 Aug 2011
Why Rakudo needs NQP
Permanent link
Rakudo, a popular Perl 6 compiler, is built on top of a smaller compiler called "NQP", short for Not Quite Perl.
Reading through a recent ramble by chromatic, I felt like he said "Rakudo needs NQP to be able to ditch Parrot, once NQP runs on a different platform" (NQP is the "another layer", which sits between Rakudo and Parrot, mentioned in the next-to-final paragraph).
I'm sure chromatic knows that VM independence is the least important reason for having NQP at all, but the casual reader might not, so let me explain the real importance of NQP for Rakudo here.
The short version is just a single word: bootstrapping.
The longer version is that large parts of Rakudo are written in Perl 6 itself (or a subset thereof), and something is needed to break the circularity.
In particular the base of the compiler is written in a subset of Perl 6, and NQP compiles those parts to bytecode, which can then compile the rest of the compiler.
This is not just because we have a fancy for Perl 6, and thus want to write as much of the code in Perl 6, but there are solid technical reasons for writing the compiler in Perl 6.
In Perl 6, the boundary between run time and compile time is blurred, as well as the boundary between the compiler, the run time library and user-space code. For example you alter the grammar with which your source code is parsed, by injecting your own grammar rules.
"Your own grammar rules" above refers to user-space code, while the grammar that is being altered is part of the compiler. If we had written the compiler in something else than Perl 6 (for example Java), it would be horribly difficult to inject user-space Perl 6 code into compiled code from a different language.
And the code not only needs to be injected, but the data passed back and forth between the compiler and the user space need to be Perl 6 objects, so all important data structures in the compiler need to be Perl 6 based anyway.
And it's not just for grammar modifications: At its heart, Perl 6 is an object oriented language. When the compiler sees a class definition, it translates them to a series of method calls on the metaobject, which again needs to be a Perl 6 object, otherwise it wouldn't be easily usable and extensible from the user space.
Now you might think that grammar modifications and changes to the
Metaobject are pretty obscure features, and you could get along just fine
with an incomplete Perl 6 compiler that neglected those two areas. But even
then you'd have lots of interactions between run time and compile time. For
example consider a numeric literal like 42. Obviously that needs
to be constructed of type Int. What's less obvious is that it
needs to be constructed to be of type Int at compile time already, because
Perl 6 code can run interleaved with the compilation. So the compiler needs to
be able to handle Perl 6 objects in all their generality, which is a huge pain
if the compiler is not written in Perl 6.
Rakudo has cheated on that front in the past, and consequently has had lots of bugs and limitations due to non-Perl 6 objects leaking out at unexpected ends. If you ever got a "Null PMC Access" from Rakudo, you know what I mean.
The lesson we learned was that you need a Perl 6 compiler to implement a Perl 6 compiler, even if that first Perl 6 compiler can handle only a rather limited subset of Perl 6.
And there are also quite some benefits to this approach. For example NQP's new regex engine is implemented as a role in NQP. It is mixed into an NQP class which allows us to build Rakudo, but it is also mixed in a Perl 6 class, which allows the generation of Perl 6-level Match objects without any need to create NQP-level match objects first, and then wrap them in Perl 6 Match objects.
That's what NQP does for us. It allows us to actually write a Perl 6 compiler.