PREVIOUS  TABLE OF CONTENTS  NEXT 

Just Another Perl Haiku

Damian Conway

Packages Used
Perl 5.005 or later ..CPAN
Coy 0.04 or later ..CPAN

I often think of Perl programs as the haiku of the software world.

Both are compact, dense, powerful, and frequently a little obscure.(Hmmm...wait a minute...that's also a fairly good description of me! )

So it's no surprise that haiku are popular with Perl programmers.

Even so, I had no idea how popular, until I wrote Coy.

What is Coy? Let me quote from the pod file that comes with the extension.(http://www.perl.com/CPAN/authors/id/DCONWAY/.)

Error messages
strewn across my terminal.
A vein starts to throb.

Their reproof adds the
injury of insult to
the shame of failure.

When a program dies
what you need is a moment
of serenity.

The Coy.pm
module brings tranquillity
to your debugging.

The module alters
the behavior of die and
warn (and croak and carp).

Like Carp.pm,
Coy reports errors from the
caller's point of view.
But it prefaces
the bad news of failure with
a soothing poem.

The Tao of haiku

A haiku is a
short poem that's 17
syllables in length.

Traditionally,
its topic is an image
taken from nature

(though the Japanese
understanding of "nature"
is subtle and broad).

True haiku don't try
to make a point; they merely
convey an image.

Of course, the image
itself may make a point, but
that's not the same thing!

The form developed
in the 1600's from
the longer tanka.

In fact, a haiku
is the hokku (the "starting
verse") of a tanka.

The first adept of
the haiku format was the
Zen poet Basho.

His best known poem
captures the wonder of an
everyday event.

It's: furike ya
kawazu tabikomu
mizu no oto
.

Which is normally
translated as: An old pond.
A frog jumps in. Plop!

As you see, there's no
message. The haiku is a
pure evocation.

Haiku on-line

The 5-7-5
art form is widely practiced
on the Internet.

There are many sites
devoted to haiku and
to related styles

(most notably, the
satirical variants
known as senryu).

The easiest place
to start is with a search of
the Yahoo index.
(http://search.yahoo.com/bin/search?p=haiku)

Jane Reichhold also
gives a superb summary
(http://www.faximum.com/aha.d/haidefjr.htm)
of the haiku form.

And for a deeper
view, Keiko Imaoka's
page(http://www.faximum.com/aha.d/keirule.htm)
is unsurpassed.

There is even a
strictly SF variant
known as "SciFaiku."
(http://www.scifaiku.com/)

Artificial haiku

Many websites now
also feature haiku that
are not carbon-based.

In fact, the Web is
awash with generators
of Japanese verse.

A simple search
(http://www.altavista.com/cgi-bin/query?q=%2Bgenerate+%2Bhaiku ) finds
nearly 3000 links for:
"+generate +haiku".

Silicon Graphics
even rigged a lava-lamp
(http://lavarand.sgi.com/cgi-bin/haiku.cgi )
to build (bad) haiku:

i think i'm wasted
i'll wax the cats. cool clear earth
pigs are smarter. crash

(That is one of its
clearer efforts. Mostly it
just spouts gibberish.)

In contrast, Garret
Kaminaga makes use of
a "haiku grammar".
(http://www.cs.stanford.edu/~zelenski/rsg/grammars/Haiku.g)

Its simple rules (see
Figure 1) expand to give
correct syllables.

But they don't encode
any English grammar, so
the results are poor:

empty computer
yakitori to empty
your chrysanthemums

Richard Decker and
Stuart Hirshfield also use
grammars for haiku.)
( http://odyssey.thomson.com/brookscole/compsci/aeonline/course/9/2/index.html

But theirs are based on
real English sentence structures
(as in Figure 2).

As a result, they
generate quite plausible
(and lovely) haiku:

A liquid summer
wind. Under the gusty sky
a storm whispers.
[sic]

Unfortunately,
their English grammar doesn't
encode syllables.

Consequently, most
of the haiku they produce
don't scan correctly.

As these samples show,
a haiku generator
must balance two things:
It must use correct
English syntax and it has
to track syllables.

As we'll see, Coy is
built around those two constraints:
grammar and meter.

The Coy module

Coy is simple to
use: just add use Coy; to your
existing program.

Hereafter, any
calls to die, warn, croak, or carp
produce a haiku.

This magic is wrought
by assigning a handler
to $SIG{ __ DIE __ }.

That handler passes
the string it receives to Coy's
verse generator.

It then re-calls die
with the resulting haiku
as its argument.

The same approach is
applied to $SIG{ __WARN__ },
to catch warnings too.

Apart from handlers,
use Coy also exports two
extra subroutines.

These subs, transcend() and
enlighten(), can lend your code
a Zen overtone.

(But internally,
they are each just a wrapper
around croak or carp.)

A note about the name

Just in case there's still
anyone in the U.S.
who hasn't sent mail...

Yes, I know you folks
spell the fish the same way as
the Japanese: koi.

We Aussies, like our
British forebears, prefer to
anglicize it: coy.

And the triple pun
("coy: (a) fish; (b) calm; (c) trap")
needs the English form.

But if you insist,
you can always dash this off
from your command line:

perl -> Koi.pm
print STDOUT "use Coy; 1;"
^D (or Z)

Inside the Coy module

Coy's generator
is data-driven and has
five main components:

• A cross-linkedlist of
words that provides its basic
vocabulary;

• A mechanism
for selecting relevant
words for each haiku;

• A set of sentence
templates that ensure the text
is grammatical;

• A set of routines
that correctly inflect words
within each sentence;

• An algorithmic
syllable counter, which checks
that each haiku scans.

We'll briefly look at
how each of these components
goes about its task.

Mere words

Ultimately, a
haiku is just sequence
of well-chosen words.

Coy's words are stored in
a hierarchical, cross-linked
vocabulary.

Figure 3 shows an
abbreviated sample
of that database.

The top level of
word categorization
is by subject nouns.

For each such noun, a
set of global constraints and
sounds is then given.

The sounds are used as
verbs to generate clauses
describing noises.

In addition, a
list of more general verb forms
("act") is specified.

Each such verb, listed
in third person singular,
may take attributes.

These attributes list
constraints on the verb's usage
(such as location).

The entry for "duck" =>
"swims", for instance, locates it
as "suraquatic".

Other attributes
limit the subject count for
particular verbs.

"lover" = &agt; "kisses", for
example, is limited
to exactly 2.
(What can I say? It. s a very conservative style of poetry.)

Random havesting

Coy's next component
is an association
selection system.

This system ensures
that the haiku relates to
the error message.

The message is first
scanned to find significant
words (principally nouns).

These words are found by
deleting "stop words" from the original text.

"Stops" are words such as
the, to, it, and the like, that
don't convey content.

The remaining words
become a "filter" for the
vocabulary.

Each word selected
for the haiku is compared
against this filter.

If the selected
word's associations don't
match, it's rejected.

This leads to problems
though, if the filter words are
too unusual.

In extreme cases,
they may filter out the whole
vocabulary.

To prevent this, Coy
can turn the word filter off
temporarily.

Filling in the blanks

The third component
generates the haiku, by
filling in templates.

Those templates encode
various grammatical
structures for haiku.

The generator
selects one and fills it in
with relevant words.

Figure 4 shows a
few of the grammatical
templates Coy uses.

Note that the grammar
has no terminals: they're drawn
from the database.

Templates are chosen
at random, as often as
needed (see Figure 4).

The chosen template
is then filled in with "filtered"
semi-random words.

The noun to be used
is randomly selected,
and constrains the verb.

Any later parts
of the grammar are likewise
constrained by that verb.

These are typically
adverbial phrases of
place or direction.

For instance, suppose
the filtered noun chosen is
the word hummingbird.

Immediately
this constrains the verb to words
like flies, darts, or nests.

If flies were chosen,
that would then constrain the place
to be aerial.

Whereas, if nests were
chosen, the place would have to
be arboreal.

Note that Coy needs no
A.I. techniques to enforce
these sequenced constraints.

The hierarchical
vocabulary structure
itself ensures them.

The other type of grammar

But selecting the
right parts of speech. --and words to
match. --is not enough.

The module must then
adjust the selected words'
grammatical form.

Specifically, the
words used must be inflected
for number and tense.

Lingua::EN::Inflect
is used to supply correct
noun/verb agreement.

Each time a grammar
template is filled, Coy selects
a random number.

That number becomes
the number of subjects for
the current sentence.

If 1 is chosen,
the subject noun and its verb
are left as they are.

Otherwise, Inflect's
PL sub is used to change
them both to plurals.

Lingua::EN::Inflect
can also inflect present
to continuous.

The new PART sub takes
a singular verb and forms
its participle.

This is useful to
increase the variety
of sentences used.

Thus, sometimes birds fly,
some birds are flying, and the
rest are flying birds.

Inflecting present
participles is harder
than it might first seem.

Consider the verbs:
bat, combat, eat, bite, fulfil(l),
lie, sortie,
and ski.

Each adds -ing after
it (all verbs do), but each root
inflects differently.

The rules are complex,
but the PART sub knows them all,
and can apply them.

Counting the beat

The four components
above ensure the haiku
parses and makes sense.

However, there's no
guarantee that the result
scans 5-7-5.

To ensure perfect
meter, each selected word's
syllables are checked.

This occurs while the
grammar templates are filled in
(as words are filtered).

The selector tracks
the progressive syllable
count of the words used.

If the count exceeds
17, the selector
can reject a word.

The selection can
also backtrack further, if
that's necessary.

In some cases this
might cause the template itself
to be rejected.

The template-filling
process then repeats until
the full haiku scans.

But does the bear dance?

By now you're thinking
"Who cares how the magic works:
Show me the MONEY!"

So here are a few
of Coy's more interesting
creative efforts...

Given the fatal
error: die "Bad argument",
Coy replied with this:

A pair of lovers
quarrel beside a stream. Four
thrushes fly away.

Note the allusion
to the bad argument in
the error message.

Haiku are never
repeated. A second die
"Bad argument"
gave:

Two old men fighting
under a sycamore tree.
Homer Simpson sighs.

In contrast, for a
croak "Missing file", Coy captured
the sense of loss with:

Bankei weeping by
a lake. Ryonen dying.
Seven howling bears.

Coy can't always reach
this exalted level of
(oblique) relevance.

For example, it
also produced this response
to croak "Missing file":

A swallow nesting
in the branches of an elm
tree. A waiting fox.

Sometimes Coy's output
suggests a macabre sense
of humor, as in:

A wolf leaps under
a willow. Two old men sit
under the willow.

In other cases,
its inscrutability
is most authentic:

Two young women near
Bill Clinton's office. A cat
waiting by a pond.

Extending the module

Eventually
even Coy's built-on-the-fly
haiku get boring.

The module only
has a small set of words from
which to build poems.

After a while the
same topics start recurring--
Coy's "theme" emerges.

True to its roots, Coy
always writes about fish, birds,
animals, and trees.

That fixation on
certain flora and fauna
soon begins to grate.

So Coy provides a
way to configure its own
vocabulary.

Any code placed in
~/.coyrc
runs at compile-time.

You can use that file
to extend Coy.pm's
mental horizons.

Figure 5 shows how
to add, for example, a
Jedi leitmotif.

Is there a poet in the house?

At TPC3
this year I launched a new book
about OO Perl.(http://www.manning.com/Conway/)

I had two copies
to give away, so I ran
a haiku contest.

At my talk on Coy
I asked audience to
write me some poems.

The two cleverest
(IMHO)
would each get a book.

David Adler won
the first, by demonstrating
that blackmail's an Art:

On-the-spot haiku
written on a lady's back.
Know we have pictures!
(http://www.stonehenge.com/merlyn/Pictures/99-08-TPC3/Day-2-Sun/?start=62&end=64)

Dean Hudson won by
coercion too, though his was
metaphysical:

Your book entices,
fork it over, friend Aussie.
Jedi mind tricks work.

It seemed like a good
idea at the time

Picking those two gems
from the hoard of glittering
entries was great fun.

So I decided
to expand the contest to
the whole conference.

I knew I needed
help: Mark-Jason Dominus
and Elaine Ashton.

They kindly agreed
to lend their artistic good
taste as co-judges.

Then Tim O'Reilly
generously donated
prizes: book vouchers.

Next we collected
haiku from the 800
or more delegates.

From over fifty
entries we selected two
as the joint winners.

Michael Schwern triumphed
with a meta-haiku that's
true Perl poetry:

# Life ends with a crash
require 'Coy.pm';
&laughter while $I, die;

Kevin Hackman won
too, for this chill augury
against the Dark Side:

  fall leaves blanket ground
redmond dreams darkly -- beware!
winter brings penguins

Your chance to win

Not content to rest
on our laureates, we want
to expand our scope.

We plan to run a
haiku contest for the whole
Perl community.

And in the spirit
of form following function,
we've entitled it:

Camelinguistic
Heptadecasyllable
Ideogeny

...or the CHI, for short.
(You can decide if that's Greek
or Chinese

The contest runs from
early 2000 till the
next Perl Conference.

Six prizes will be
offered. They'll be awarded
at the conference.

Watch the Perl newsgroups
in the next month for details
for how to enter.

Perl Poem: down.pl

__END__


Damian Conway
is stuck inside a haiku
and he can't get out!

PREVIOUS  TABLE OF CONTENTS  NEXT