<?xml version="1.0"?>
<!-- name="generator" content="gyre/0.0.2" -->
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">

<rss version="0.91">
  <channel>
    <title>eighty-twenty news</title>
    <link>http://www.eighty-twenty.org/index.cgi</link>
    <description></description>
    <language>en</language>

        <item>
      <title>Submatches vs. Recursion in ThiNG Patterns</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/submatch-vs-recpattern-20060522.html</link>
      <description>&lt;p&gt;Submatching in a pattern, &lt;code&gt;+var@pattern&lt;/code&gt;, looks
suspiciously similar to recursion in a
value, &lt;code&gt;var=value&lt;/code&gt;. In each case, we expect a value which
is both named and processed, and in each case, a binding is introduced
into code. The processing in the submatch case is a further
pattern-match operation, and in the recursion case it is a nested
evaluation.&lt;p&gt;

&lt;p&gt;The main difference, besides the overall context, between the two
forms is the scope of the introduced binding: in the submatch, the
pattern variable to the left of the &lt;code&gt;@&lt;/code&gt; sign is bound only
in the right-hand-side of the overall pattern, at present, whereas in
the recursive value the variable to the left of the &lt;code&gt;=&lt;/code&gt; is
bound in the right-hand-side of the recursion itself. Another
important difference is that in the submatch context,
the &lt;code&gt;+var&lt;/code&gt; is clearly just a binding form, whereas in the
recursion context, &lt;code&gt;var&lt;/code&gt; can be seen as acting both as a
binding form and a reference to the bound value. Another way of
looking at it, though, has the &lt;code&gt;var&lt;/code&gt; acting solely as a
binding form, with the right-hand-side of the &lt;code&gt;=&lt;/code&gt; being the
actual result of the expression &amp;mdash; the fact that the left and
right-hand-sides are equivalent is not relevant, in this
interpretation.&lt;/p&gt;

&lt;p&gt;What if the two forms were unified, based on their similarities,
using either &lt;code&gt;var=pattern-or-value&lt;/code&gt;
or &lt;code&gt;+var=pattern-or-value&lt;/code&gt; for both pieces of syntax? The
former seems out-of-place for submatches, since we want to clearly
mark bindings that will propagate their bound value into the code on
the right-hand-side of an object clause; and the latter seems
out-of-place in a recursive value, since &lt;code&gt;+var&lt;/code&gt; implies a
binding object, which is a first-class datum in ThiNG.&lt;/p&gt;

&lt;p&gt;Compare and contrast the following &amp;mdash; a string-interleaving
routine using the current syntax for both submatch and recursion:&lt;/p&gt;

&lt;pre class="code"&gt;
sepList: [+s: loop=[(cons +x +more@(cons _ _)): (x ++ s ++ loop more)
                    (cons +x _): x
                    _: EmptyString]]
&lt;/pre&gt;

&lt;p&gt;The same routine using &lt;code&gt;+var=pattern-or-value&lt;/code&gt; for
both:&lt;/p&gt;

&lt;pre class="code"&gt;
sepList: [+s: +loop=[(cons +x +more=(cons _ _)): (x ++ s ++ loop more)
                     (cons +x _): x
                     _: EmptyString]]
&lt;/pre&gt;

&lt;p&gt;Finally, the same routine using &lt;code&gt;var=pattern-or-value&lt;/code&gt; for
both:&lt;/p&gt;

&lt;pre class="code"&gt;
sepList: [+s: loop=[(cons +x more=(cons _ _)): (x ++ s ++ loop more)
                    (cons +x _): x
                    _: EmptyString]]
&lt;/pre&gt;

&lt;p&gt;Finally, there's the question of scope: if the same syntax is used
for both submatch and for recursion, what happens if we adjust the
scoping rules to be as similar as possible? It doesn't make sense for
recursive-value's scoping rule to be changed to be the same as
submatch's scoping rule &amp;mdash; there's no right-hand-side in a value
for the binding to be visible in! &amp;mdash; but the other way around
might be worth considering: let the binding introduced as part of a
submatch be visible as part of the right-hand-side of the
submatch. What happens then? Do we end up with infinite patterns? Do
we instead end up with a kind of unification-constraint in which an
object matching a submatch that is referred to within that same
submatch is constrained to have the same recursive topology as the
pattern itself?&lt;/p&gt;

&lt;p&gt;[&lt;b&gt;Update:&lt;/b&gt; Something I forgot to mention &amp;mdash; with the
scope rule for submatches as it stands, it's possible to evaluate and
then compile patterns ahead of execution time. Changing the scope rule
can make compilation of ThiNG code more difficult, forcing the system
to be more late-bound than perhaps it needs to be.]&lt;/p&gt;
</description>
    </item>
    <item>
      <title>Partial-evaluation of Monads in Scheme - A New Hope?</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/monads-in-scheme-20060521.html</link>
      <description>&lt;p&gt;The other day, I &lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/smalltalk/thing/monads-in-scheme-20060519.html"&gt;noted&lt;/a&gt;
that my generic-monad-in-Scheme implementation suffered from the flaw
of not being able to type-check monad assemblies before any actions
had been executed, and that in general such ahead-of-time checking is
not possible without abstract interpretation of some kind, because the
&lt;em&gt;functional&lt;/em&gt; code in the right-hand-side of the bind operator
can decide &lt;em&gt;which action&lt;/em&gt; to splice into the monad next:&lt;/p&gt;

&lt;pre class="code"&gt;
&lt;b&gt;(define (goes-wrong)
  (run-io (mlet* ((val mread))
            (if val &lt;/b&gt;&lt;i&gt;;; if the user entered #f, we break, otherwise we're fine&lt;/i&gt;&lt;b&gt;
                (mlet* ((_ (mdisplay "Was true\n"))) (return 'apples))
                'bananas))))&lt;/b&gt; &lt;i&gt;;; Note: missing "(return ...)"!&lt;/i&gt;
&lt;/pre&gt;

&lt;p&gt;Perhaps a partial-evaluator can play the part of a
Haskell-type-system-like abstract interpretation well enough to drive
the type-safety checks through the code at PE time (loosely equivalent
to static-analysis time) for most cases (leaving the remaining cases
for runtime-detection, as in the present implementation, and perhaps
warning the user)? In order to find out, my next experiment in this
direction will be marrying the &lt;a
href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments/monad/general-monad.scm"&gt;monad
code&lt;/a&gt; to the &lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/scheme/partial-eval-20050313.html"&gt;partial-evaluator&lt;/a&gt;
I &lt;a
href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments/partial-eval/pe.scm"&gt;implemented&lt;/a&gt;
some months ago.&lt;/p&gt;
</description>
    </item>
    <item>
      <title>Monads in Scheme - A Retraction</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/monads-in-scheme-20060519.html</link>
      <description>&lt;p&gt;I don't know what crack I was smoking the other day, but my &lt;a
href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments/monad/general-monad.scm"&gt;implementation
of general monads&lt;/a&gt; not only does not have the property I &lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/smalltalk/thing/spikes-20060508.html#monads-in-scheme"&gt;claimed&lt;/a&gt;
for it, viz. that it provides the same level of type-checking as
Haskell &lt;em&gt;ahead of execution-time&lt;/em&gt;, but it &lt;em&gt;cannot&lt;/em&gt;
provide such a level of ahead-of-runtime checking, whether the hosting
language is eager or lazy.&lt;/p&gt;

&lt;p&gt;To see the first, try the following example:&lt;/p&gt;

&lt;pre class="code"&gt;
&amp;gt; &lt;b&gt;(run-io (mlet* ((_ (mdisplay "HERE\n"))
                  (_ sget))
            (return 232)))&lt;/b&gt;
&lt;i&gt;HERE
wrong monad-class ((mclass #5(struct:&amp;lt;monad-class&amp;gt; io [...]))
      (m #3(struct:&amp;lt;monad&amp;gt; #5(struct:&amp;lt;monad-class&amp;gt; state [...]) [...]))) &lt;/i&gt;
&lt;/pre&gt;

&lt;p&gt;Note how an action is performed &lt;em&gt;before&lt;/em&gt; the type error is
detected. In general, it is not possible to detect the type-error
ahead of time without abstract interpretation of the level of
sophistication that Haskell's type-checker provides, as the following
example demonstrates:&lt;/p&gt;

&lt;pre class="code"&gt;
&amp;gt; &lt;b&gt;(define (goes-wrong)
    (run-io (mlet* ((val mread))
              (if val
                  (mlet* ((_ (mdisplay "Was true\n"))) (return 'apples))
                  'bananas))))&lt;/b&gt; &lt;i&gt;;; Note: missing "(return ...)"!&lt;/i&gt;
&amp;gt; &lt;b&gt;(goes-wrong)
#t&lt;/b&gt;
&lt;i&gt;Was true
apples&lt;/i&gt;
&amp;gt; &lt;b&gt;(goes-wrong)
#f&lt;/b&gt;
&lt;i&gt;&amp;lt;monad&amp;gt;-ref: expects type &amp;lt;struct:&amp;lt;monad&amp;gt;&amp;gt; as 1st argument, given: bananas; other arguments were: 0
[...]&lt;/i&gt;
&lt;/pre&gt;

&lt;p&gt;The problem is that the system can decide how to continue based on
I/O performed at runtime, and so the composition performed by bind
cannot be checked until after the left-hand-side has been fully
evaluated. &lt;small&gt;[Aside: the &lt;code&gt;&amp;lt;monad&amp;gt;-ref&lt;/code&gt; error was
a symptom of the unexpected problem described below &amp;mdash; now that
the code has been fixed, it instead complains "&lt;code&gt;not a monad
bananas&lt;/code&gt;", as it should.]&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;This affects all my monad implementations, not just the IO
monad:&lt;/p&gt;

&lt;pre class="code"&gt;
&amp;gt; &lt;b&gt;(run-st (mlet* ((a sget)
                  (b (mdisplay "OH NO\n"))
                  (_ (sput 4)))
            (return (+ a 1)))
          2)&lt;/b&gt;
&lt;i&gt;OH NO
(3 . 4)&lt;/i&gt;
&lt;/pre&gt;

&lt;p&gt;Actually, and this is unexpected (&lt;code&gt;"OH NO"&lt;/code&gt; indeed!),
here it's even worse than just not catching the type-error before any
actions have executed: it isn't catching the error &lt;em&gt;at
all&lt;/em&gt;. Here's what that &lt;code&gt;mlet*&lt;/code&gt; expands into:&lt;/p&gt;

&lt;pre class="code"&gt;
(&amp;gt;&amp;gt;= sget
     (lambda (a)
       (&amp;gt;&amp;gt;= (mdisplay "OH NO\n")
            (lambda (b)
              (&amp;gt;&amp;gt;= (sput 4)
                   (lambda (_)
                     (return (+ a 1))))))))
&lt;/pre&gt;

&lt;p&gt;My implementation of &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt; isn't "reaching into
the lambda" in the case of the IO monad. &lt;small&gt;&lt;i&gt;[Interlude; sounds
of programming from behind the curtain.]&lt;/i&gt;&lt;/small&gt; The problem was
my use of the (bad) "delayed monad" idea in implementing the IO monad
&amp;mdash; I've just replaced that idea with a more explicit
representation of a delayed action, and the code now behaves as I
expected. How embarrassing! Here's the trace now that I've fixed that
problem:&lt;/p&gt;

&lt;pre class="code"&gt;
&amp;gt; &lt;b&gt;(mlet* ((a sget)
          (b (mdisplay "OH NO\n"))
          (_ (sput 4)))
    (return (+ a 1)))&lt;/b&gt;
&lt;i&gt;#3(struct:&amp;lt;monad&amp;gt; #5(struct:&amp;lt;monad-class&amp;gt; state [...]) [...])&lt;/i&gt;
&lt;/pre&gt;

&lt;p&gt;This shows that before we run the monad, it appears to be a
well-behaved state monad instance. Running it now causes the error to
be detected:&lt;/p&gt;

&lt;pre class="code"&gt;
&amp;gt; &lt;b&gt;(run-st (mlet* ((a sget)
                  (b (mdisplay "OH NO\n"))
                  (_ (sput 4)))
            (return (+ a 1)))
          2)&lt;/b&gt;
&lt;i&gt;wrong monad-class ((mclass #5(struct:&amp;lt;monad-class&amp;gt; state [...]))
      (m #3(struct:&amp;lt;monad&amp;gt; #5(struct:&amp;lt;monad-class&amp;gt; io [...]) [...]))) &lt;/i&gt;
&lt;/pre&gt;

&lt;p&gt;This demonstrates what I was hoping to show above, before I was
derailed by the delayed-monad issue: that the impossibility of using
this technique to type-check monad assemblies ahead of any actions
being performed applies to all monad kinds, not just the IO monad. Of
course, it's less of a problem for non-side-effecting monads, such as
the state monad, since performing an action (in the case immediately
above, reading the hidden state variable) in such a monad has no
effect on the outside world by the time the type-error is
detected.&lt;/p&gt;

&lt;p&gt;In conclusion, I was mistaken about Scheme's ability to achieve the
same level of ahead-of-execution type-checking as Haskell manages
&amp;mdash; I don't believe it to be possible without essentially
Haskell-equivalent abstract-interpretation machinery bolted on to the
language. Nonetheless, the library I've developed does catch type
errors eventually &amp;mdash; so long as the faulty code is eventually
run! This is no worse than regular dynamically-typed language code,
and is still a useful system, so I'm not completely disappointed. I
will still be able to use the idea to structure side-effects in
ThiNG.&lt;/p&gt;
</description>
    </item>
    <item>
      <title>Monads in Scheme - Update</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/monads-in-scheme-20060511.html</link>
      <description>&lt;p&gt;Just quickly revisiting the article the other day on &lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/smalltalk/thing/spikes-20060508.html"&gt;monads
in Scheme&lt;/a&gt;: I've just updated my experimental &lt;a
href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments/monad/general-monad.scm"&gt;implementation&lt;/a&gt;
to propagate monad type information both forward from the arguments to
the result of the bind operation and backward from the expected result
type to the required argument types of the bind. Now &lt;a
href="http://okmij.org/ftp/Scheme/monad-in-Scheme.html"&gt;Oleg's
example&lt;/a&gt; (see near the bottom of the page, where there's an
expression &lt;code&gt;(put-line "Enter a number: ")&lt;/code&gt;) can be
translated into the following working code:&lt;/p&gt;

&lt;pre class="code"&gt;
(define oleg-example-mixed-monad
  (mlet* ((_ (mdisplay "Enter a number: "))
          (n mread)
          (all-n (return (iota n)))
          (evens (return (run-list (mlet* ((i all-n))
                                     (if (even? i)
                                         (return i)
                                         (fail "odd"))))))
          (_ (mdisplay "Computed "))
          (_ (mdisplay (length evens)))
          (_ (mdisplay " evens\n")))
    (return evens)))
&lt;/pre&gt;

&lt;p&gt;When run with &lt;code&gt;(run-io oleg-example-mixed-monad)&lt;/code&gt;, it
produces sessions like the following (user input in &lt;i&gt;italics&lt;/i&gt;):&lt;/p&gt;

&lt;pre class="code"&gt;
Enter a number: &lt;i&gt;33&lt;/i&gt;
Computed 17 evens
(0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32)
&lt;/pre&gt;

&lt;p&gt;The system could be sweetened up with a bit of generic-function or
other OO sugar, but I'd say even without extra sugar that this
experiment has been successful in demonstrating the feasibility of the
technique. It looks like I'll be able to use the idea for structuring
effects in ThiNG.&lt;/p&gt;
</description>
    </item>
    <item>
      <title>Trait Composition in ThiNG</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/trait-composition-20060511.html</link>
      <description>&lt;p&gt;Trait composition is one of the fundamental ideas in ThiNG
r3/r4. It's used to assemble pattern-matching clauses in functions, to
assemble definitions into a module, to (functionally-) update
data-structure, to augment generic functions with new definitions, and
to assemble modules into programs.&lt;/p&gt;

&lt;pre class="code"&gt;
define cons [+car: [+cdr: [First: car Rest: cdr]]]
define map [+f: loop=[(cons +a +d): (cons (f a) (loop d)) Nil:Nil]]
&lt;/pre&gt;

&lt;p&gt;Both these definitions use objects with more than one clause - for
instance &lt;code&gt;[First: car Rest: cdr]&lt;/code&gt; contains two clauses, as
does the object after the equal-sign in &lt;code&gt;loop=[...]&lt;/code&gt; in
&lt;code&gt;map&lt;/code&gt;. Multi-clause objects are defined as being
constructed from single clauses using the trait composition operators
sum (+), override (/), remove (-) and rename (@).&lt;/p&gt;

&lt;p&gt;If we stick to an imagined syntax where only single-clause objects
and trait composition operators can be used, then the above
definitions might look like this:&lt;/p&gt;

&lt;pre class="code"&gt;
define cons [+car: [+cdr: ([First: car] + [Rest: cdr])]]
define map [+f: loop=([(cons +a +d): (cons (f a) (loop d))] + [Nil:Nil])]
&lt;/pre&gt;

&lt;p&gt;Note that sum (+) is used instead of override (/), since the
pattern-pairs &lt;code&gt;First&lt;/code&gt; vs &lt;code&gt;Rest&lt;/code&gt;, and &lt;code&gt;(cons
+a +d)&lt;/code&gt; vs &lt;code&gt;Nil&lt;/code&gt; don't overlap. We could have used
override, but we would have forgone the checks made for overlapping
patterns that the sum operator provides.&lt;/p&gt;

&lt;p&gt;Module definitions and generic functions in ThiNG work
similarly. There are two dialects of ThiNG I've been thinking about,
one where a generic function dispatches each message, and one more
traditional dialect where messages are sent directly to
explicitly-given receivers. Let's call these dialects ThiNG/1 and
ThiNG/N, for single-distinguished-receiver and multiple-dispatch (N
receivers) respectively.&lt;/p&gt;

&lt;p&gt;In ThiNG/N, you can think of each definition within a module as
being a clause within a (part of a) global generic function. Here's a
hypothetical example, recalling that &lt;code&gt;x y: z&lt;/code&gt; is syntactic
sugar for the message &lt;code&gt;[0: x y: z]&lt;/code&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;
module List = [
  [+a cons: +b]: [First: a Rest: b]

  [+f mapOver: (+head cons: +tail)]: ((f head) cons: (f mapOver: tail))
  [+f mapOver: Nil]: Nil
]
&lt;/pre&gt;

&lt;p&gt;(As you can see, this leads to quite a different style of
programming compared to ThiNG/1.) Rewriting this using
single-clause-with-trait-composition syntax gives something like:&lt;/p&gt;

&lt;pre class="code"&gt;
module List =
  [ ([0: +a] + [cons: +b]): ([First: a] + [Rest: b]) ] +
  [ ([0: +f] + [mapOver: (+head cons: +tail)]): ((f head) cons: (f mapOver: tail)) ] +
  [ ([0: +f] + [mapOver: Nil]): Nil ]
&lt;/pre&gt;

&lt;p&gt;Importing a module into the current toplevel is a matter of
replacing the current generic-function-dispatching object - let's call
it &lt;code&gt;ENV&lt;/code&gt; - with itself either summed or overridden by the
imported module definition, i.e. &lt;code&gt;List + ENV&lt;/code&gt; or &lt;code&gt;
List / ENV&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I was interested to read an &lt;a
href="http://lists.squeakfoundation.org/pipermail/squeak-dev/2002-August/043634.html"&gt;old
message from Marcel Weiher&lt;/a&gt; (who gave an interesting talk the &lt;a
href="http://www.xpdeveloper.net/xpdwiki/Wiki.jsp?page=SmalltalkParty20060408"&gt;recent
UK-Smalltalk meeting&lt;/a&gt; - see also &lt;a
href="http://www.lshift.net/blog/2006/05/05/apl-cooler-than-you-think"&gt;here&lt;/a&gt;)
in which he writes:

&lt;blockquote&gt; I do have a feeling that the direction &lt;i&gt;[toward
ultra-simplicity that Self takes]&lt;/i&gt;, while very fruitful, turned out
to be "too uniform", in some senses.  There is no fence at all to the
meta-level at one point ( object vs. class ) and still a big one at
another ( object vs. code/method ). &lt;/blockquote&gt;

&lt;p&gt;In ThiNG, the fence he talks about between objects and methods is
removed - or at least made much smaller. Objects are simultaneously
data-structures and methods/functions. If an object has a clause keyed
on a pattern containing a binding, then it acts more like a method
than a field; if the pattern doesn't contain any bindings, it acts
more like a field than a method. Lazy evaluation helps blur the
distinction here.&lt;/p&gt;
</description>
    </item>
    <item>
      <title>Recent spikes in ThiNG</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/spikes-20060508.html</link>
      <description>&lt;p&gt;
Since I last posted about ThiNG, my ideas have shifted closer to those
expressed in Jay &amp;amp; Kesner's &lt;a
href="http://www-staff.it.uts.edu.au/~cbj/Publications/purepattern.pdf"&gt;Pure
Pattern Calculus&lt;/a&gt;, and I've built a few experimental spikes to gain
some concrete experience with some of the language features and
designs I've been thinking about - among other things, that traits and
modules share a number of core characteristics, and that using monads
to structure effects can be profitable even in a dynamically-typed
environment.
&lt;/p&gt;

&lt;h5&gt;&lt;a name="traits-in-scheme"&gt;&lt;/a&gt;Traits in Scheme (&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments/traits/"&gt;source code link&lt;/a&gt;)&lt;/h5&gt;

&lt;p&gt;A few weeks ago, I implemented a simple, small &lt;a
href="http://www.iam.unibe.ch/~scg/Research/Traits/"&gt;traits&lt;/a&gt;
language based on the &lt;a
href="http://citeseer.ist.psu.edu/574534.html"&gt;formal model of
traits&lt;/a&gt; of Schärli, Nierstrasz et. al. The language has no fields
(methods suffice!) and no classes (prototypes suffice!). The
implementation, a macro which embeds traits into regular &lt;a
href="http://www.plt-scheme.org/software/mzscheme/"&gt;PLT Scheme&lt;/a&gt;, is
a toy, intended only to explore the idea of traits; in particular, it
doesn't "&lt;a
href="http://www.iam.unibe.ch/publikationen/techreports/2005/iam-05-005"&gt;flatten&lt;/a&gt;"
composed traits.&lt;/p&gt;

&lt;p&gt;PLT Scheme's &lt;a
href="http://pre.plt-scheme.org/docs/html/mzlib/mzlib-Z-H-25.html#node_chap_25"&gt;match&lt;/a&gt;
operator was used to match method signatures to method invocations,
which is a little unusual - pattern-matching in object-oriented
languages is usually restricted to searches by method name ("selector"
in Smalltalk terminology).&lt;/p&gt;

&lt;p&gt;It turned out to be &lt;em&gt;tiny&lt;/em&gt;. Traits are represented as
functions from a message to a handling thunk or false (representing
"does not understand this message"). Implementing the trait
composition operations (sum, override, remove, and rename) is done in
a combinator-like way. The core routine for finding a method to invoke
for a given message is twenty-one lines long and dirt simple.&lt;/p&gt;

&lt;p&gt;If this spike were to be taken further, perhaps integrated with an
existing OO system for Scheme, a few things would need to be
changed. Firstly, while using full pattern-matching for message
dispatch leads to a very user-friendly OO programming system, very few
existing object systems do anything more than method name searches, so
the traits implementation would have to be changed to be more method
name based; and secondly, it's not clear how a traits system would
interact with multimethod-capable generic functions. Brian Rice has
done some &lt;a
href="http://lists.tunes.org/archives/slate/2005-November/001739.html"&gt;thinking
and experiments&lt;/a&gt; along these lines in the context of &lt;a
href="http://slate.tunes.org/"&gt;Slate&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;&lt;a name="monads-in-scheme"&gt;&lt;/a&gt;Monads in Scheme (&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments/monad/general-monad.scm"&gt;source code link&lt;/a&gt;)&lt;/h5&gt;

&lt;p&gt;A couple of years ago, I stumbled across &lt;a
href="http://okmij.org/ftp/Scheme/monad-in-Scheme.html"&gt;Oleg
Kiselyov's "Monadic Programming in Scheme"&lt;/a&gt;. Right near the bottom
of the page, he points out a problem with his Scheme implementation of
monads, which is that you don't get automatic resolution of
overloading of the bind and return operators. In Haskell, the type
system figures out which monad is intended, usually without requiring
programmer-supplied type annotations, but in Scheme there's no
analogous constraint-propagation system, so you have to select which
monad you're binding or returning into explicitly.&lt;/p&gt;

&lt;p&gt;As part of thinking about the design of ThiNG, I've more-or-less
settled on a strict Haskell-style monad-based separation of effects
from computations, and I've come up with a simple runtime
constraint-propagation-based way of getting the same automatic bind-
and return-overloading as Haskell gives you, using OO dynamic dispatch
instead of a static type system. The monads are fully "type-checked"
before they're run, so no actions are taken until the whole monad is
correctly assembled.&lt;/p&gt;

&lt;p&gt;During the weekend, I implemented a first stab at the
constraint-propagation idea in Scheme. Currently it only type-checks
the monad immediately before running it, rather than type-checking
incrementally based on the best available information as the monad is
being assembled. I plan to update the implementation to do as much
early constraint propagation as possible, rather than waiting until
the last minute. This will allow Oleg's final example, involving
simultaneous use of an IO and a List monad, to be automatically
disambiguated.&lt;/p&gt;

&lt;h5&gt;&lt;a name="haskell-thing-evaluator"&gt;&lt;/a&gt;Haskell-based ThiNG r3/r4 evaluator (&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments/haskell/matcher.hs"&gt;source code link&lt;/a&gt;)&lt;/h5&gt;

&lt;p&gt;I've been meaning to experiment with &lt;a
href="http://www.haskell.org/"&gt;Haskell&lt;/a&gt; for a while, so last
weekend I built a toy evaluator for my most recent ThiNG ideas using
&lt;a href="http://www.haskell.org/ghc/"&gt;GHC&lt;/a&gt;. I used the &lt;a
href="http://www.haskell.org/ghc/docs/latest/html/libraries/parsec/Text-ParserCombinators-Parsec.html"&gt;Parsec&lt;/a&gt;
parser-combinator library to build a parser. The parser and printer
together took 86 lines of code; the evaluator itself, a mere 90
lines. The type system both helped and hindered: it caught a few
stupid errors I made while initially constructing the program, but on
the other hand also forced me into a slightly awkward design (almost
identical AST, Value and Pattern types, with concomitant
almost-duplication of code) that Scheme's unityped nature would have
allowed me to avoid.&lt;/p&gt;

&lt;p&gt;The model of the language I built has allowed me to get a clear
idea of how scoping should behave. It's also clarified the semantics
enough that I ought now to be able to build a partial-evaluator for
it, to see how hard it's going to be to get a usefully fast
implementation.&lt;/p&gt;
</description>
    </item>
    <item>
      <title>The Pure Pattern Calculus, and Extensible Records With Scoped Labels</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/related-papers-20051124.html</link>
      <description>&lt;p&gt;
Two papers very similar to the ideas I've been developing for ThiNG
have popped up in the last couple of weeks:
&lt;/p&gt;

&lt;ol&gt;

&lt;li&gt;
&lt;p&gt;
&lt;a href="http://www.cs.uu.nl/~daan/"&gt;Daan Leijen&lt;/a&gt;'s &lt;a
href="http://www.cs.uu.nl/~daan/download/papers/scopedlabels.pdf"&gt;&lt;i&gt;Extensible
records with scoped labels&lt;/i&gt;&lt;/a&gt;: describes an intriguing system for
typing records &lt;i&gt;similar&lt;/i&gt; to those I'm trying to define with
ThiNG.
&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;
&lt;a href="http://www-staff.it.uts.edu.au/~cbj/"&gt;Barry Jay&lt;/a&gt; and &lt;a
href="http://www.pps.jussieu.fr/~kesner/"&gt;Delia Kesner&lt;/a&gt;'s &lt;a
href="http://www-staff.it.uts.edu.au/~cbj/Publications/purepattern.pdf"&gt;&lt;i&gt;Pure
Pattern Calculus&lt;/i&gt;&lt;/a&gt;: a fascinating language that embeds the
lambda-calculus naturally that feels &lt;i&gt;very&lt;/i&gt; similar to ThiNG.
&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;i&gt;Update:&lt;/i&gt; Since &lt;a
href="http://www.google.co.uk/search?q=jay+kesner+%22pure+pattern+calculus%22"&gt;google
finds the paper now&lt;/a&gt;, I've added the link to Jay &amp;amp; Kesner's
paper.&lt;/p&gt;
</description>
    </item>
    <item>
      <title>Design Problems in ThiNG</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/problems-20051124.html</link>
      <description>&lt;p&gt;
At the moment, I've three main problems in the work I'm doing
designing ThiNG: syntax, semantics, and macros. (Doesn't leave much,
does it?) I'll cover the syntactic and semantic problems tonight
&amp;mdash; my thinking on macros is much too fuzzy to write down at the
moment.
&lt;/p&gt;

&lt;h5&gt;Syntax&lt;/h5&gt;

&lt;p&gt;
I'm leaning toward a concrete syntax that, like Smalltalk and Slate,
makes message-sending the only concise operation, leaving application
in particular as almost second-class. Since I expect message sending
to make up a much larger fraction of code than raw application, it
seems sensible to optimise for that case. Arranging things this way
means we can avoid a lot of &lt;code&gt;#&lt;/code&gt;quoting.
&lt;/p&gt;

&lt;ol&gt;

&lt;li&gt;
&lt;p&gt;
Message sends through the dynamic-dispatcher (arrows,
&lt;code&gt;==&amp;gt;&lt;/code&gt;, read more-or-less as "desugars to"):
&lt;/p&gt;

&lt;pre class="code"&gt;
exp1, exp2            ==&amp;gt;   (0: exp1 1: exp2)
exp1 + exp2           ==&amp;gt;   (0: exp1 #(+): exp2)
exp1 selector         ==&amp;gt;   (#selector: exp1)
selector: exp1        ==&amp;gt;   (selector: exp1)            "a problem case"
exp1 selector: exp2   ==&amp;gt;   (0: exp1 selector: exp2)    "the same case"
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;
In-place literal object construction:
&lt;/p&gt;

&lt;pre class="code"&gt;
[exp1, exp2]          ==&amp;gt;   [0: exp1 1: exp2]
[exp1 + exp2]         ==&amp;gt;   [0: exp1 #(+): exp2]
[exp1 selector]       ==&amp;gt;   [#selector: exp1]
[selector: exp1]      ==&amp;gt;   [selector: exp1]            "&lt;i&gt;not&lt;/i&gt; a problem case"
[exp1 selector: exp2] ==&amp;gt;   [0: exp1 selector: exp2]    "illegal because ambiguous"
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;
Quoting (since quotation could be implemented as a macro, this will
need to become more scheme-like in terms of tying in with macro (and
perhaps regular application) syntax):
&lt;/p&gt;

&lt;pre class="code"&gt;
# stx
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;
Application:
&lt;/p&gt;

&lt;pre class="code"&gt;
exp1 $ exp2           ==&amp;gt;   (exp1 $ exp2)
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;
The "data" equivalent of application (see also the "semantics" section
below):
&lt;/p&gt;

&lt;pre class="code"&gt;
[exp1 $ exp2]         ==&amp;gt;   [exp1 $ exp2]
&lt;/pre&gt;
&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;
The "problem case" above is interesting: in Smalltalk and Slate,
selectors, for instance &lt;code&gt;#foo:bar:&lt;/code&gt;, are written &lt;code&gt;foo:
1 bar: 2&lt;/code&gt; when they're used to send a message. The quoting
of &lt;code&gt;foo&lt;/code&gt; and &lt;code&gt;bar&lt;/code&gt; is implicit &amp;mdash; those
tokens can't be interpreted as variable references or variable
bindings. In ThiNG, on the other hand, the identity function
is &lt;code&gt;[x:x]&lt;/code&gt;, with the &lt;code&gt;x&lt;/code&gt; on the left-hand-side
of the colon a variable binding rather than a literal symbol to be
used in pattern matching. If I wanted to instead produce an object
that responded with some value when sent a literal
message &lt;code&gt;#x&lt;/code&gt;, I'd have to quote the &lt;code&gt;x&lt;/code&gt; in the
pattern thus: &lt;code&gt;[#x:x]&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
When I'm sending a message through the dynamic-dispatcher, for
instance &lt;code&gt;(foo: 1 bar: 2)&lt;/code&gt;, what happens under the hood is
something like
&lt;/p&gt;

&lt;pre class="code"&gt;
currentContext $ [foo: 1 bar: 2]
&lt;/pre&gt;

&lt;p&gt;
at which point the problem becomes apparent. Both &lt;code&gt;foo&lt;/code&gt;
and &lt;code&gt;bar&lt;/code&gt; occur as variable bindings - not as literal
symbols! What's intended is
&lt;/p&gt;

&lt;pre class="code"&gt;
currentContext $ [#foo: 1 #bar: 2]
&lt;/pre&gt;

&lt;p&gt;
On the other hand, if a user wrote &lt;code&gt;[zot: 3]&lt;/code&gt; instead
of &lt;code&gt;(zot: 3)&lt;/code&gt;, then I'd expect no such implicit quoting to
happen.
&lt;/p&gt;

&lt;p&gt;
Thinking about what Scheme might be like if it had more than the
limited notion of pattern matching it has (see these &lt;a
href="http://groups.google.co.uk/group/comp.lang.scheme/browse_thread/thread/b72d987aa6626cd2/e2f7cfa55fb51d55"&gt;two&lt;/a&gt; &lt;a
href="http://groups.google.com/group/comp.lang.scheme/browse_thread/thread/aee843bc8f6eb557/1ab3a84fcfe3c84b#1ab3a84fcfe3c84b"&gt;threads&lt;/a&gt;),
we end up in a similar situation:
&lt;/p&gt;

&lt;pre class="code"&gt;
(lambda (x y) (+ x y))    ;; bind two variables
(lambda ('x y) ...)       ;; require first argument to be symbol x,
                          ;; and bind second argument
&lt;/pre&gt;

&lt;p&gt;
I think the solution of having implicit quoting in the syntactic
context of message dispatch (round parens, rather than square
brackets) is acceptable, because it is very seldom that one will want
to send a message to the current context where the &lt;i&gt;message&lt;/i&gt;
needs to bind a variable, and should one want to do that, it is still
possible using the explicit &lt;code&gt;currentContext $ myMessage&lt;/code&gt;
syntax above. (Caveat: I'm still uncertain about the best way of
getting hold of the current context.)
&lt;/p&gt;

&lt;h5&gt;Semantics&lt;/h5&gt;

&lt;p&gt;
At present, matching the value &lt;code&gt;[#x: 1 #y: 2]&lt;/code&gt; against the
pattern &lt;code&gt;[]&lt;/code&gt; will succeed, since the empty pattern places
no requirements on its value. This is fine, except that it makes it
impossible to introduce pattern-matching based branching!
&lt;/p&gt;

&lt;p&gt;
The problem is this: Assume the existence of the empty object, and
objects constructed from other objects, and nothing else. No symbols,
integers, or other literals. With these ingredients, we can produce
values such as
&lt;/p&gt;

&lt;pre class="code"&gt;
[]
[ []      : []      ]
[ []      : [[]:[]] ]
[ [[]:[]] : []      ]
[ [[]:[]] : [[]:[]] ]
&lt;/pre&gt;

&lt;p&gt;
etc. The problem is that because there is no ordering between clauses
within an object/pattern definition, and because patterns within an
object must be nonoverlapping, and because &lt;code&gt;[]&lt;/code&gt; matches
anything (it's actually equivalent to discard!), we cannot use
pattern-matching to distinguish reliably between any two of the values
above.
&lt;/p&gt;

&lt;p&gt;
Take, for instance, the pattern
&lt;/p&gt;

&lt;pre class="code"&gt;
[           []: a     [[]: []]: b ]
&lt;/pre&gt;

&lt;p&gt;
This can be equivalently written
&lt;/p&gt;

&lt;pre class="code"&gt;
[     [[]: []]: b           []: a ]
&lt;/pre&gt;

&lt;p&gt;
Now, when applied to a value &lt;code&gt;[]&lt;/code&gt;, obviously only the
clause resulting in &lt;code&gt;a&lt;/code&gt; will match, since the guard on
the &lt;code&gt;b&lt;/code&gt; clause places requirements on the value that it
cannot satisfy. When applied to &lt;code&gt;[[]: []]&lt;/code&gt;, however, we
have a problem, since &lt;i&gt;both&lt;/i&gt; guards match
&amp;mdash; &lt;code&gt;[]&lt;/code&gt;, as a pattern, places no requirements on its
argument at all!
&lt;/p&gt;

&lt;p&gt;
When all we have is the empty object and objects constructed from it,
we cannot define non-overlapping patterns. We can fix this in (at
least) two ways: we can let clauses within an object
be &lt;i&gt;ordered&lt;/i&gt;, trying to match the leftmost first, for instance,
or we can introduce some other kind of distinction that
pattern-matching can get a grip on. I want to keep clauses unordered,
for reasons involving method dispatch that I've not fully worked out
yet, which leaves the introduction of a further distinction.
&lt;/p&gt;

&lt;p&gt;
Fortunately, there is one piece of aggregate syntax that we need for
other purposes, which can be used here without confusion: the syntax
for application (more generally, without interpretation, simply syntax
for pairs, analogous to Scheme's &lt;code&gt;(x&amp;nbsp;.&amp;nbsp;y)&lt;/code&gt;). When
interpreted as code, it means application; when interpreted as data,
I'm experimenting with the idea of making it mean extension. Assume,
for the moment, that infix &lt;code&gt;$&lt;/code&gt; is the concrete syntax for
application. Then &lt;code&gt;a $ b&lt;/code&gt; means, if interpreted as code,
"send the contents of variable &lt;code&gt;b&lt;/code&gt; as a message to the
receiver denoted by variable &lt;code&gt;a&lt;/code&gt;"; and if interpreted as
data, (loosely) "extend &lt;code&gt;a&lt;/code&gt; with the clauses
from &lt;code&gt;b&lt;/code&gt;, searching &lt;code&gt;a&lt;/code&gt; only if a search
within &lt;code&gt;b&lt;/code&gt; fails".
&lt;/p&gt;

&lt;p&gt;
We can then use pattern-matching to distinguish between
&lt;/p&gt;

&lt;pre class="code"&gt;
([] $  []) $ []          "left-heavy, a.k.a. left"
 [] $ ([]  $ [])         "right-heavy, a.k.a. right"
&lt;/pre&gt;

&lt;p&gt;
Now that we have a distinction we can make, we can define structures
akin to booleans, natural numbers, characters, and symbols: all the
literals we need for working with program representations.
&lt;/p&gt;

&lt;pre class="code"&gt;
(Pseudocode - I still need to work out how to integrate proper macros)

Define F = ([]$[])$[]
Define T = []$([]$[])

Define Nil = F
Define Cons(a, b) = [ T: a F: b ]

Define Zero = Nil (= F)
Define X * 2 + Y = Cons(Y, X), where X is a number and Y is either
  T for a one-bit, or F for a zero-bit

thus 5 = Cons(T, Cons(F, Cons(T, Nil)))
 and 2 = Cons(F, Cons(T, Nil))
       = [ T:F F:[ T:T F:F ] ]
       = [ ([]$([]$[])):(([]$[])$[])
           (([]$[])$[]):[ ([]$([]$[])):([]$([]$[]))
                          (([]$[])$[]):(([]$[])$[]) ] ]

Define a unicode character to be its codepoint as a number
Define a symbol to be a list of characters

Thus: #x = Cons('x', Nil)
         = Cons(120, Nil)
         = Cons(Cons(F, Cons(F, Cons(F, Cons(T,
                     Cons(T, Cons(T, Cons(T, Nil))))))),
                Nil)
           (etc.)
&lt;/pre&gt;

&lt;p&gt;
The naive encoding given above isn't one that would be useful in a
real implementation. For a start, it'd be very inefficient, so
meta-level objects would probably be used directly instead; and also,
even if that weren't the case, a more realistic encoding would involve
higher-level constructs, and wouldn't be built so directly on
primitive patterns of &lt;code&gt;$&lt;/code&gt;. For instance, symbols might be
represented as a &lt;i&gt;stream&lt;/i&gt; of characters, with &lt;code&gt;[#first: 'x'
#rest: #nil]&lt;/code&gt;, instead of the very low-level markers used
above. (Although that particular choice of encoding opens up another
can of worms: recursive data-structures!)
&lt;/p&gt;

&lt;p&gt;
Note that I'm still working out the precise meaning of &lt;code&gt;$&lt;/code&gt;
when interpreted as extension: for instance, when should the extension
be visible as an extension, and when should it fade into the
background, acting as if the compound object it produces is a simple
object?
&lt;/p&gt;
</description>
    </item>
    <item>
      <title>ThiNG repository available</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/thing-repo-20051103.html</link>
      <description>&lt;p&gt;
I've been hesitant to make my code publicly available, since it's
embarrassingly messy, but a couple of people have indicated they're
interested in taking a closer look, so here it is:
&lt;/p&gt;

&lt;pre class="code"&gt;
$ hg clone &lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/"&gt;http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/&lt;/a&gt;
&lt;/pre&gt;

&lt;p&gt;
The code is mostly written using &lt;a
href="http://www.call-with-current-continuation.org/"&gt;Chicken&lt;/a&gt;
(with my &lt;a
href="http://www.eighty-twenty.org/~tonyg/Darcs/chicken-sdl/"&gt;chicken-sdl&lt;/a&gt;
bindings and a version of my &lt;a
href="http://www.lshift.net/blog/2005/08/22/json-for-mzscheme-and-a-portable-packrat-parsing-combinator-library"&gt;packrat
parsing library&lt;/a&gt;), and is laid out as follows:
&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/doc"&gt;./doc&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;Contains notes on design and implementation, mostly fairly
out-of-date, but containing the odd interesting thought. There are
also some discarded experiments in concrete syntax here.&lt;/dd&gt;

&lt;dt&gt;&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/experiments"&gt;./experiments&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;Contains syntax experiments, and some spike-solutions exploring
ideas I'm considering for inclusion, such as &lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/smalltalk/transactional-memory-20050109.html"&gt;STM&lt;/a&gt;
and &lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/scheme/partial-eval-20050313.html"&gt;partial
evaluation&lt;/a&gt;.&lt;/dd&gt;

&lt;dt&gt;&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/lib"&gt;./lib&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;Contains third-party library code: at the moment, just a &lt;a
href="http://www.lshift.net/blog/2005/10/11/pregexp-over-streams-and-other-random-hackery"&gt;patched
version&lt;/a&gt; of Dorai Sitaram's &lt;a
href="http://www.ccs.neu.edu/home/dorai/pregexp/pregexp.html"&gt;pregexp&lt;/a&gt;
library.&lt;/dd&gt;

&lt;dt&gt;&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/r1"&gt;./r1&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;
  &lt;p&gt;
    A first ThiNG spike implementation, borrowing ideas heavily from
    Slate. The dispatch algorithm is a Scheme reimplementation of
    Slate's original common-lisp dispatch algorithm (the code is
    structurally almost identical). The main difference between the
    two languages is that the only available mutable state is through
    &lt;a
    href="http://www.haskell.org/ghc/docs/5.00/set/sec-concurrency-abstractions.html#SEC-MVARS"&gt;MVars&lt;/a&gt;,
    and that every subexpression is evaluated in parallel. Have a look
    at &lt;a
    href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/r1/boot.thing"&gt;boot.thing&lt;/a&gt;
    to get a feel for the language. (Note in particular the
    differences between &lt;code&gt;#map:&lt;/code&gt; and
    &lt;code&gt;#mapInOrder:&lt;/code&gt;.)
  &lt;/p&gt;
  &lt;p&gt;
    This implementation got as far as having image-based persistence,
    network I/O, a socket-based REPL, and an interface to the SDL
    graphics output and UI event input frameworks before I decided I'd
    learnt enough from it and moved on to the next iteration.
  &lt;/p&gt;
 &lt;/dd&gt;

&lt;dt&gt;&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/r2"&gt;./r2&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;This was a small step on the way to &lt;code&gt;r3&lt;/code&gt;.&lt;/dd&gt;

&lt;dt&gt;&lt;a href="http://www.eighty-twenty.org/hgwebdir.cgi/smalltalk-tng/file/tip/r3"&gt;./r3&lt;/a&gt;&lt;/dt&gt;

&lt;dd&gt;This is the spike I'm currently working on (that I &lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/smalltalk/thing/spike-2-20051017.html"&gt;mentioned&lt;/a&gt;
the other day). At the moment, it's a parser and a simple
evaluator. Both are broken right this minute, as I'm working on some
quite fundamental changes to the syntax of the language.&lt;/dd&gt;

&lt;/dl&gt;
</description>
    </item>
    <item>
      <title>Second ThiNG spike</title>
      <link>http://www.eighty-twenty.org/index.cgi/tech/smalltalk/thing/spike-2-20051017.html</link>
      <description>&lt;p&gt;
Over the weekend I built a second experimental implementation&lt;a
href="#spike-footnote-1"&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/a&gt; of ThiNG (older articles:
&lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/smalltalk/midnight-inspiration-20040916.html"&gt;1&lt;/a&gt;,
&lt;a
href="http://www.eighty-twenty.org/index.cgi/_STORY_/tech/smalltalk/message-oriented-programming-20050101.html"&gt;2&lt;/a&gt;),
a simple normal-order pattern-matching-based evaluator with a parser
built out of &lt;a
href="http://www.lshift.net/blog/2005/08/22/json-for-mzscheme-and-a-portable-packrat-parsing-combinator-library"&gt;packrat
parsing combinators&lt;/a&gt; and my &lt;a
href="http://www.lshift.net/blog/2005/10/11/pregexp-over-streams-and-other-random-hackery"&gt;hacked-on&lt;/a&gt;
version of Dorai Sitaram's &lt;a
href="http://www.ccs.neu.edu/home/dorai/pregexp/pregexp.html"&gt;pregexp&lt;/a&gt;
library.
&lt;/p&gt;

&lt;p&gt;
I've chosen to view functions and records/objects exactly the same
way: as pattern-matching message-dispatchers. Here are a few
expressions, to give some feel for the language:
&lt;/p&gt;

&lt;pre class="code"&gt;
"This is a comment, just as in Smalltalk."

[x: x]              "This is the identity function."
[x: x] 123          "This is the identity function applied to the literal integer 123."

"This function takes a 2-tuple, and returns the tuple with its elements swapped."
[[x, y]: [y, x]]

"This is a sketch of a mapping function."
define map [
  (f Nil)           : Nil
  (f [Hd: h Tl: t]) : [Hd: f h Tl: map f t]
]
&lt;/pre&gt;

&lt;p&gt;Note that objects (&lt;code&gt;[Key: value Key: value ...]&lt;/code&gt;) are
the same as functions (&lt;code&gt;[Pattern: expression Pattern: expression
...]&lt;/code&gt;) since (1) the language is lazy, and (2) atoms (keys) are
just special-cases of patterns.&lt;/p&gt;

&lt;p&gt;I noticed an interesting thing while I was implementing pattern
matching. Consider the pattern &lt;code&gt;[Hd: h Tl: t]&lt;/code&gt;. The idea is
that it should match an object that can receive &lt;code&gt;Hd&lt;/code&gt; or
&lt;code&gt;Tl&lt;/code&gt; as arguments, and bind the variables &lt;code&gt;h&lt;/code&gt;
and &lt;code&gt;t&lt;/code&gt; respectively to the results of applying the object
to those arguments. That means that when function/object syntax occurs
in
&lt;i&gt;value&lt;/i&gt; context, it is interpreted as &lt;code&gt;[pattern: value
pattern: value ...]&lt;/code&gt;, but (and this is the interesting bit) in
pattern context, it is interpreted as &lt;code&gt;[value: pattern value:
pattern ...]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When a pattern &lt;code&gt;[X: v]&lt;/code&gt; is being matched against some
function, perhaps &lt;code&gt;[X: 1 Y: 2]&lt;/code&gt;, we see:&lt;/p&gt;

&lt;pre class="code"&gt;
    [[X: v]: v] [X: 1 Y: 2]
--&gt; let v = ([X: 1 Y: 2] X) in v
--&gt; let v = 1 in v
--&gt; 1
&lt;/pre&gt;

&lt;p&gt;A slightly more complex example:&lt;/p&gt;

&lt;pre class="code"&gt;
    [[[X: 123]: [v, w]]: v] [[X: n]: [n * 2, n * 3]]
--&gt; let [v, w] = ([[X: n]: [n * 2, n * 3]] [X: 123]) in v
--&gt; let [v, w] = [123 * 2, 123 * 3] in v
--&gt; 123 * 2
--&gt; 246
&lt;/pre&gt;

&lt;p&gt;Variable references on the left-hand-side of a colon in value
context are &lt;i&gt;binding&lt;/i&gt; occurrences, and on the right-hand-side in
value context are &lt;i&gt;referencing&lt;/i&gt; occurrences; but in pattern
context this is reversed, with variables on the left-hand-side of a
colon being references and on the right-hand-side being binders. An
interesting question about the scope of variable references inside
patterns arises: what should their scope be, considering that
functions/objects do not have a defined ordering on their
pattern-matching clauses?&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;&lt;small&gt;&lt;a name="spike-footnote-1"&gt;&lt;/a&gt;Footnote [1]: The
&lt;i&gt;first&lt;/i&gt; experimental implementation was pretty much a
pure-functional dialect of &lt;a
href="http://slate.tunes.org/"&gt;Slate&lt;/a&gt;, with ML-style reference
cells (sparsely used). The system grew to the point where it could
support an SDL interface and multiple socket-based tty consoles, and
then I decided I'd learnt enough and it was time to start
again.&lt;/small&gt;&lt;/p&gt;
</description>
    </item>


  </channel>
</rss>
