Re: Implementation Language Choice

Joachim Durchholz <joachim.durchholz@web.de>
2 Mar 2004 11:04:06 -0500

          From comp.compilers

Related articles
[3 earlier articles]
Re: Implementation Language Choice basile-news@starynkevitch.net (Basile Starynkevitch \[news\]) (2004-02-13)
Re: Implementation Language Choice joachim.durchholz@web.de (Joachim Durchholz) (2004-02-13)
Re: Implementation Language Choice kevin@albrecht.net (Kevin Albrecht) (2004-02-13)
Re: Implementation Language Choice lex@cc.gatech.edu (Lex Spoon) (2004-02-26)
Re: Implementation Language Choice joachim.durchholz@web.de (Joachim Durchholz) (2004-02-26)
Re: Implementation Language Choice gdr@integrable-solutions.net (Gabriel Dos Reis) (2004-02-27)
Re: Implementation Language Choice joachim.durchholz@web.de (Joachim Durchholz) (2004-03-02)
Re: Implementation Language Choice la@iki.fi (Lauri Alanko) (2004-03-02)
Re: Implementation Language Choice lex@cc.gatech.edu (Lex Spoon) (2004-03-02)
Re: Implementation Language Choice torbenm@diku.dk (2004-03-06)
Re: Implementation Language Choice bettini@dsi.unifi.it (Lorenzo Bettini) (2004-03-06)
Re: Implementation Language Choice joachim.durchholz@web.de (Joachim Durchholz) (2004-03-11)
Re: Implementation Language Choice mayan@sandbridgetech.com (Mayan) (2004-03-19)
[1 later articles]
| List of all articles for this month |

From: Joachim Durchholz <joachim.durchholz@web.de>
Newsgroups: comp.compilers
Date: 2 Mar 2004 11:04:06 -0500
Organization: Oberberg Online Infosysteme
References: 04-02-109 04-02-131 04-02-149 04-02-164 04-02-174
Keywords: design
Posted-Date: 02 Mar 2004 11:04:06 EST

Gabriel Dos Reis wrote:


> Joachim Durchholz <joachim.durchholz@web.de> writes:
>
>> [...] it's beyond me why [pattern matching] never made it into
>> mainstream imperative languages; it's exceedingly useful.
>
> Probably because people have been using the more general Visitor
> Pattern?


It's just the other way round: pattern matching is more general than
Visitor.


Visitor is for iterating through collections of polymorphic data.
Pattern matching is for inspecting and acting upon a single instance
of some polymorphic data. Actually it's the reverse of OO-style
polymorphism: in OO, the set of operations is fixed and the set of
types is extensible; pattern matching assumes a fixed set of types and
an extensible set of operations.


Here's how pattern matching works:


1. You define a union of record types. Something like
            Tree = Leaf value: int | Node left: Tree; right: Tree
which translates to a record with a "selector" field that distinguishes
the "int" case from the "left: Tree; right: Tree" case, and a memory
area that either contains the int or the two Tree fields. (I tried to
write this in C, but the syntax got so riddled with irrelevant detail
that I decided to stick with the prose description.)


2. Whenever your code does something with a Tree data object, it does
pattern matching, like this:
      case t of
          Leaf i: ... // Do something with i.
          Node l, r: ... // Do something with l and r.
      end
The "i" above gets bound to the "int" field in the Tree (Leaf variant)
record.
The "l" and "r" above get bound to the "left" and "right" fields in the
Tree (Node variant) record.


The nice thing about pattern matching is that it does in one fell swoop
what you want anyway: detect which case is at hand, and access the
fields in it in some handy local variables because you don't want to
write t->value, t->left, t->right all over the place.
Even nicer is the following: accessing t->value in the "l, r" branch
would be perfectly valid C, and require a run-time check in Pascal; with
pattern matching, "i" is out of scope in the "l, r" branch and vice
versa, so any accidental misuse will be caught at compile time.


Regards,
Jo
--
Currently looking for a new job.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.