<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8115687546775931822</id><updated>2012-01-19T12:03:36.695-08:00</updated><category term='quotation'/><category term='Named Pipes'/><category term='transhumanism'/><category term='clojure'/><category term='books'/><category term='C'/><category term='destructuring bind'/><category term='Jesse Schell'/><category term='parsing'/><category term='Integrated Development Environment'/><category term='Apple'/><category term='forth'/><category term='syntax'/><category term='Common Lisp Reader'/><category term='horror'/><category term='syntax extension'/><category term='Nyquist'/><category term='programming praxis'/><category term='Hypercard'/><category term='Smalltalk'/><category term='haskell'/><category term='Lua'/><category term='awesome?'/><category term='dynamic scope'/><category term='emacs lisp'/><category term='Neuroscience'/><category term='morse'/><category term='xah lee'/><category term='turtle graphics'/><category term='hygiene'/><category term='scheme'/><category term='sonnet'/><category term='picoLisp'/><category term='scope'/><category term='macros'/><category term='morse-code'/><category term='Foibles'/><category term='monads'/><category term='streams'/><category term='lambda'/><category term='joy'/><category term='Lisp in Small Pieces'/><category term='interpreter'/><category term='io'/><category term='Vim'/><category term='tutorials'/><category term='guido van rossum'/><category term='D.I.C.E.'/><category term='Suburbia'/><category term='vaguely incorrect'/><category term='LISP'/><category term='literate code'/><category term='screencast'/><category term='peg puzzle'/><category term='Python'/><category term='technology'/><category term='newLisp'/><category term='Matlab'/><category term='Alan Kay'/><category term='Vectorization'/><category term='Night Time'/><category term='scratch buffer'/><category term='crazy'/><category term='Programming'/><category term='Xerox PARC'/><category term='the el camino of lisps'/><category term='Brevity'/><category term='foldl'/><category term='meditation'/><category term='syntactic extension'/><category term='screencasts'/><category term='parsers'/><category term='Preprocessor'/><category term='lexical-let'/><category term='Writing'/><category term='stack languages'/><category term='morse code'/><category term='Efficiency'/><category term='code'/><category term='Common Lisp'/><category term='programming languages'/><category term='Dynabook'/><category term='update'/><category term='foldr'/><category term='dystopia'/><category term='tail calls'/><category term='emacs'/><category term='Common Lisp Packages'/><category term='Music'/><category term='factor'/><category term='Reader Macros'/><category term='metaprogramming'/><category term='racket'/><category term='extended metaphor'/><category term='Synthesizers'/><category term='pattern matching'/><category term='transdehumanism'/><category term='concatenative languages'/><category term='persistent hash tables'/><category term='abstraction'/><category term='Open Computing'/><category term='functional programming'/><category term='poetry'/><category term='iPad'/><title type='text'>Dorophone</title><subtitle type='html'>Soon to be replaced with an Elaborate Platinum Mechanism.

Ok, but seriously the blog is about programming and technology.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>55</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-3510683703473684696</id><published>2012-01-18T18:30:00.000-08:00</published><updated>2012-01-19T11:52:27.772-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='Common Lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='pattern matching'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>Shadchen: A pattern Matching Library for Elisp</title><content type='html'>&lt;h2&gt;A pattern matching library for Emacs Lisp&lt;/h2&gt;

&lt;p&gt;One of the things I like most about Racket (and other functional
programming languages) is that they have good support for pattern
matching, which is a great way to simultaneously dispatch on structure
type, enforce constraints on values held in a data structure, and bind
variables.  A tremendous amount of code is devoted to these
activities, and pattern matching combines them all in a succinct, easy
to read form.  &lt;/p&gt;

&lt;p&gt;It's been my intent for some time to write a complete pattern matching
library for Emacs Lisp, since it is a Lisp which I use very
frequently.  Shadchen is the first time any attempt to do so has
resulted in a reasonable product.  Shadchen is a Yiddish word for
matchmaker, in case you were wondering about the title.  The library
is available in my &lt;a href="https://github.com/VincentToups/emacs-utils"&gt;elisp repository&lt;/a&gt;.  Even though it comes in a giant
directory full of junk, it runs standalone.&lt;/p&gt;  [EDIT: &lt;a href="https://github.com/VincentToups/shadchen-el"&gt;Here it is in a standalone repo&lt;/a&gt;.]

&lt;h2&gt;How it Works&lt;/h2&gt;

&lt;p&gt;Shadchen's interface consists of just three forms.  &lt;code&gt;match&lt;/code&gt; is the
work horse - it actually performs a pattern match.  &lt;code&gt;match&lt;/code&gt; has the
syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(match &amp;lt;VALUE&amp;gt;
 (&amp;lt;PATTERN1&amp;gt; FORMS ...)
 (&amp;lt;PATTERN2&amp;gt; FORMS ...)
 ...
 (&amp;lt;PATTERNN&amp;gt; FORMS ...))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;VALUE&amp;gt;&lt;/code&gt; expression is evaluated, and then each pattern attempts
to match against it.  If a pattern succeeds, it's associated FORMS are
evaluated, in a context where the environment has been extended by the
pattern's bindings.  If a match fails, the next pattern is tried.  If
no patterns succeed, an error occurs.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(match (list 1 2 3)
  ((list x y z) (+ x y z)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The expression &lt;code&gt;(list x y z)&lt;/code&gt; is the pattern in the above expression.
Patterns are such that they resemble the code which creates the data
structure in question, in this case a three element list.  The pattern
&lt;code&gt;(list x y z)&lt;/code&gt; basically specifies that a match occurs when the input
value is a three element list.  When a match occurs, &lt;code&gt;x&lt;/code&gt; is bound to the
first element, &lt;code&gt;y&lt;/code&gt; to the second and so on.  Hence this form evaluates
to &lt;code&gt;6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is another example: summing a list:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun* dummy-sum (lst &amp;amp;optional (acc 0))
 (match lst
  (nil acc)
  ((cons hd tl)
   (dummy-sum tl (+ acc hd)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First we specify the pattern &lt;code&gt;nil&lt;/code&gt;, which matches only &lt;code&gt;nil&lt;/code&gt;.  When
the input is &lt;code&gt;nil&lt;/code&gt;, we return the accumulator.  Then we match against
&lt;code&gt;(cons hd tl)&lt;/code&gt; which matches a cons &lt;code&gt;pair&lt;/code&gt;, binding &lt;code&gt;hd&lt;/code&gt; to the &lt;code&gt;car&lt;/code&gt;
and &lt;code&gt;tl&lt;/code&gt; to the &lt;code&gt;cdr&lt;/code&gt;.  We then recur, adding to the accumulator.&lt;/p&gt;

&lt;p&gt;The pattern matching language is rich.  There are patterns for
matching against literals, applying functions to values, matching
against arbitrary conditions, matching against structs. &lt;/p&gt;

&lt;p&gt;Eg:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defstruct a-struct f1 f2)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With this struct defined, the pattern:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(struct a-struct (f1 (? #'numberp x)) (f2 (? #'stringp y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches only a struct whose field &lt;code&gt;f1&lt;/code&gt; is a number, and whose field
&lt;code&gt;f2&lt;/code&gt; is a string.  When that is true, &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are bound.&lt;/p&gt;

&lt;h2&gt;Extending Shadchen&lt;/h2&gt;

&lt;p&gt;The pattern matcher is user extensible using &lt;code&gt;defpattern&lt;/code&gt;.
&lt;code&gt;Defpattern&lt;/code&gt; defines a function which receives the pattern's
arguments, and returns a new pattern which effects the desired match.
For instance, the &lt;code&gt;struct&lt;/code&gt; pattern is defined thusly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun cl-struct-prepend (s)
  (intern (format "cl-struct-%s" s)))

(defun make-cl-struct-accessor (struct-name slot) 
  (intern (format "%s-%s" struct-name slot)))


(defpattern struct (struct-name &amp;amp;rest fields)
  `(and
    (? #'vectorp)
    (? #'(lambda (x) (&amp;gt; (length x) 0)))
    (? #'(lambda (o)
           (eq (elt o 0) ',(cl-struct-prepend struct-name))))
    ,@(loop for f in fields collect
            `(funcall 
              #',(make-cl-struct-accessor struct-name (car f))
              ,(cadr f)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the &lt;code&gt;defpattern&lt;/code&gt; body must return a valid pattern in terms
of previously defined patterns (or itself, patterns can be
recursive).  In this case we use the patterns &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, and
&lt;code&gt;funcall&lt;/code&gt; to create a new matcher.&lt;/p&gt;

&lt;h2&gt;Supported Patterns:&lt;/h2&gt;

&lt;p&gt;Shadchen supports the following patterns:&lt;/p&gt;

&lt;p&gt;Shadchen supports the following built-in patterns.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;SYMBOL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches anything, binding &lt;SYMBOL&gt; to that value in the body
expressions.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;KEYwORD-LITERAL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches only when the value is the same keyword.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;NUMBER-LITERAL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches only when the value is the same number.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;STRING-LITERAL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches only when the value is &lt;code&gt;string=&lt;/code&gt; is the same string.    &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(CONS &amp;lt;PATTERN1&amp;gt; &amp;lt;PATTERN2&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches any &lt;code&gt;CONS&lt;/code&gt; cell, or &lt;code&gt;NIL&lt;/code&gt;, then matches &lt;code&gt;&amp;lt;PATTERN1&amp;gt;&lt;/code&gt; and
&lt;code&gt;&amp;lt;PATTERN2&amp;gt;&lt;/code&gt;, executing the body in a context where their matches are
bound.  If the match value is NIL, then each &lt;code&gt;PATTERN&lt;/code&gt; matches against
NIL.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(LIST &amp;lt;P1&amp;gt; ... &amp;lt;PN&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches a list of length N, then matches each pattern &lt;code&gt;&amp;lt;PN&amp;gt;&lt;/code&gt; to the
elements of that list.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(LIST-REST &amp;lt;P1&amp;gt; ... &amp;lt;PN&amp;gt; &amp;lt;REST-PATTERN)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches &lt;P1&gt; - &lt;PN&gt; to elements in at list, as in the &lt;code&gt;LIST&lt;/code&gt; pattern.
The final &lt;code&gt;&amp;lt;REST-PATTERN&amp;gt;&lt;/code&gt; is matched against the rest of the list.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(QUOTE DATUM)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Only succeeds when &lt;code&gt;DATUM&lt;/code&gt; is &lt;code&gt;EQUALP&lt;/code&gt; to the match-value.  Binds no
values.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; (AND &amp;lt;P1&amp;gt; .. &amp;lt;PN&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Tests all &lt;code&gt;&amp;lt;PN&amp;gt;&lt;/code&gt; against the same value, succeeding only when all
patterns match, and binding all variables in all patterns.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; (? PREDICATE &amp;lt;PATTERN&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Succeeds when &lt;code&gt;(FUNCALL PREDICATE MATCH-VALUE)&lt;/code&gt; is true and when
&lt;code&gt;&amp;lt;PATTERN&amp;gt;&lt;/code&gt; matches the value.  Body has the bindings of &lt;code&gt;&amp;lt;PATTERN&amp;gt;&lt;/code&gt;.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt; (FUNCALL FUN &amp;lt;PATTERN&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Applies &lt;code&gt;FUN&lt;/code&gt; to the match value, then matches &lt;code&gt;&amp;lt;PATTERN&amp;gt;&lt;/code&gt; against &lt;em&gt;the
result&lt;/em&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; (BQ EXPR)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Matches as if by &lt;code&gt;BACKQUOTE&lt;/code&gt;.  If &lt;code&gt;EXPR&lt;/code&gt; is an atom, then this is
equivalent to &lt;code&gt;QUOTE&lt;/code&gt;.  If &lt;code&gt;EXPR&lt;/code&gt; is a list, each element is matches
as in &lt;code&gt;QUOTE&lt;/code&gt;, unless it is an &lt;code&gt;(UQ &amp;lt;PATTERN&amp;gt;)&lt;/code&gt; form, in which case it
is matched as a pattern.  Eg:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(match (list 1 2 3)
  ((BQ (1 (UQ x) 2)) x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Will succeed, binding &lt;code&gt;X&lt;/code&gt; to 2.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(match (list 10 2 20)
   ((BQ (1 (UQ x) 2)) x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Will fail, since &lt;code&gt;10&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; don't match.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(values &amp;lt;P1&amp;gt; ... &amp;lt;PN&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Will match multiple values produced by a &lt;code&gt;(values ...)&lt;/code&gt; form.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(struct struct-name (field-name &amp;lt;P1&amp;gt;)
                    (field-name &amp;lt;P2&amp;gt;)
                    ...
                    (field-name &amp;lt;P3&amp;gt;))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which matches when the input is a struct of type &lt;code&gt;struct-name&lt;/code&gt;, whose
fields match &lt;code&gt;&amp;lt;P1&amp;gt;&lt;/code&gt; ... &lt;code&gt;&amp;lt;PN&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;If you are an elisp hacker like me, now you don't have to envy those
snooty Racket, ML and Haskell programmers.  Happy Hacking!&lt;/p&gt;

&lt;p&gt;(PS - the library is also available in Common Lisp, check my github).&lt;/p&gt;
&lt;p&gt;(PPS - I love Racket, ML and Haskell programmers and they aren't at all snooty.&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-3510683703473684696?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/3510683703473684696/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=3510683703473684696' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3510683703473684696'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3510683703473684696'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2012/01/shadchen-pattern-matching-library-for.html' title='Shadchen: A pattern Matching Library for Elisp'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-4504032693150206044</id><published>2011-12-08T17:56:00.000-08:00</published><updated>2011-12-09T04:15:19.145-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='racket'/><category scheme='http://www.blogger.com/atom/ns#' term='pattern matching'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='interpreter'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='Lisp in Small Pieces'/><category scheme='http://www.blogger.com/atom/ns#' term='literate code'/><title type='text'>Literate, Racket-Styled Interpreter from Ch. 1 of Lisp in Small Pieces</title><content type='html'>&lt;pre&gt;&lt;code&gt;#lang racket
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We'll need Racket's mutable pairs.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require racket/mpair)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Our initial interpreter will not be very discriminating: it is
sufficient to recognize atoms, that is, values which evaluate to
themselves, by:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (atom? x) (not (pair? x)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, atoms as defined above include both symbols and things
like strings and numbers.  We will be using symbols to refer to
variables in our &lt;em&gt;interpreted&lt;/em&gt; code, though, so we need to draw a
distinction between symbols and other atoms.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (atom/symbol? x) (symbol? x))
(define (atom/non-symol? x) (and (atom? x) (not (symbol? x))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I'd like to extend my interpreter as I go, because I think that is
clearer.  We will take the following approach to allow this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (suffix item onto)
  (reverse (cons item (reverse onto))))

(define-values (interpret extend-interpreter!)
  (let* ((interpreter-handlers '())
         (extend-interpreter!
          (lambda (dispatch-function handle-function)
            (set! interpreter-handlers
                  (suffix (cons dispatch-function handle-function) interpreter-handlers))))
         (interpret
          (lambda (exp env)
            (let loop 
                [(interpreter-handlers interpreter-handlers)]
              (match interpreter-handlers
                [(list) (error (format "Can't interpret ~a - no handler matched!" exp))]
                [(cons (cons dis han) rest)
                 (if (dis exp) (han exp env)
                     (loop rest))])))))
    (values interpret extend-interpreter!)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above creates a closure over three-values, the
interpreter-handlers, a list of pairs, a function
&lt;code&gt;extend-interpreter!&lt;/code&gt;, which allows us to add a form-recognizer,
form-handler pair to this list (at the end, using &lt;code&gt;suffix&lt;/code&gt;) and a
function &lt;code&gt;interpret&lt;/code&gt;, which does the actual interpretation.&lt;/p&gt;

&lt;p&gt;Interpret loops through the handlers, testing the form it is
working on with the first function in each pair.  If the test
succeeds, it interprets the expression with the second function in
the pair.&lt;/p&gt;

&lt;p&gt;The downside to this approach is that we destructure our
expressions twice - once during matching and once during
interpretation.  The upside is that we can extend our interpreter
as we go, clearly separating concerns.  This kind of interpreter
is about as slow as it can be anyway, so it hardly matters.&lt;/p&gt;

&lt;p&gt;Now we can add our first interpretation rule:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(extend-interpreter!
 atom/non-symol?
 (lambda (val _) val))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Each interpreting function takes the form to interpret and the
environment.  However, things like "this" and 10 don't need an
environment, so we give it a name which indicates our disdain for
it, &lt;code&gt;_&lt;/code&gt;.  We simply return what we were passed in.&lt;/p&gt;

&lt;p&gt;Now a note on the environment, since we'll need it soon: since
Racket lists are made of &lt;em&gt;immutable&lt;/em&gt; pairs, and we'll need to
mutate our environment, we are going to use mutable cons cells in
our list.  The global environment, however, is the empty one:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define the-global-environment (list))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It needn't itself be mutable - adding items to the global
environment will be accomplished by &lt;code&gt;set!&lt;/code&gt; on
&lt;code&gt;the-global-environment&lt;/code&gt;, rather than mutation of the list itself.&lt;/p&gt;

&lt;p&gt;We want to support adding a binding to the environment from
"outside" the language, and extending the environment from
inside.  Respectively:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (add-global-binding name value)
  (if (symbol? name)
      (set! the-global-environment 
            (cons (mcons name value) the-global-environment))
      (error "Bindings must be to symbols.")))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This adds a new mutable cons pair to the global environment,
created with &lt;code&gt;mcons&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (extend-environment env vars vals)
  (match (list vars vals)
    [(list (list) (list)) env]
    [(list (cons key subs-keys)
           (cons val subs-vals))
     (extend-environment 
      (cons (mcons key val) env) subs-keys subs-vals)]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;extend-environment&lt;/code&gt; produces a &lt;em&gt;new&lt;/em&gt; environment
without modifying the old one.  This is important.&lt;/p&gt;

&lt;p&gt;Allowing us to say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(interpret "A string" the-global-environment)
(interpret 100 the-global-environment)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These yield their first arguments, as they should.&lt;/p&gt;

&lt;p&gt;Now let's add a rule for symbols.  First we need to know how we
will look the meaning symbols up:.  The simplest possible choice
is that we represent the environment as a list of pairs; eg
'((x . 10) (y . 11))&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (lookup s env)
  (match env
    [(list) (error (format "No binding for ~a." s))]
    [(cons (mcons key val) env)
     (if (eq? key s) val
         (lookup s env))]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;lookup&lt;/code&gt; is exactly the function which evaluates symbols.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(extend-interpreter!
 atom/symbol?
 lookup)

(interpret 'x (list (mcons 'x 100)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which evaluates to &lt;code&gt;100&lt;/code&gt;, just as our environment specifies.&lt;/p&gt;

&lt;p&gt;Now we can write the interpreter cases for special forms.  Lisp In
Small Pieces supports only &lt;code&gt;quote&lt;/code&gt;,&lt;code&gt;if&lt;/code&gt;,&lt;code&gt;begin&lt;/code&gt;,&lt;code&gt;set!&lt;/code&gt;, and
&lt;code&gt;lambda&lt;/code&gt;.  &lt;code&gt;quote&lt;/code&gt; first:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define quote-expression? (match-lambda 
                           [`(quote ,exp) #t]
                           [_ #f]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The evaluation of a quoted form is the form itself.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(extend-interpreter!
 quote-expression?
 (lambda (s env) (second s)))

(interpret '(quote x) the-global-environment)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Results in &lt;code&gt;x&lt;/code&gt;, as it should.&lt;/p&gt;

&lt;p&gt;Now for &lt;code&gt;if&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define if-expression?
  (match-lambda 
   [`(if ,test ,true ,false) #t]
   [_ #f]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We restrict ourselves to ternary if.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(extend-interpreter!
 if-expression?
 (lambda (e env)
   (match e 
     [`(if ,test ,true ,false)
      (if (interpret test env)
          (interpret true env)
          (interpret false env))])))

(interpret '(if #t 'x 'y) the-global-environment)
(interpret '(if #f 'x 'y) the-global-environment)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These expressions are &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; respectively.
If's interpreter only interprets the appropriate branch.&lt;/p&gt;

&lt;p&gt;Now we implement begin:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define begin-expression? 
  (match-lambda 
   [(cons 'begin rest) #t]
   [_ #f]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we create a struct whose sole instance is the value returned
by an empty begin statement.  We could also forbid such a statement.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(struct Empty-begin () #:transparent)
(define the-empty-begin (Empty-begin))

(define (interpret-begin e env)
  (match e
    [`(begin) the-empty-begin]
    [`(begin ,expr) (interpret expr env)]
    [(cons 'begin  (cons expr rest))
     (interpret expr env)
     (interpret-begin (cons 'begin rest) env)]))

(extend-interpreter! 
 begin-expression?
 interpret-begin)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can't really do anything interesting with begin without
implementating side effecting expressions.  However:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(interpret '(begin 'a 'b 'c) the-global-environment)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is 'c, just as we expected.&lt;/p&gt;

&lt;p&gt;Now for &lt;code&gt;set!&lt;/code&gt;, which will allow us to update the environment from
within the language itself.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define set!-expression? 
  (match-lambda
   [`(set! ,(? symbol? var) ,expr) #t]
   [_ #f]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a side effect inducing form and we'll need to use side
effects to simulate the result.  To match L.I.S.P., however, we
will not allow &lt;code&gt;set!&lt;/code&gt; to introduce new bindings, only update them.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(struct Set!-result () #:transparent)
(define set!-result (Set!-result))

(define (update! env key value)
  (match env
    [(list) (error (format "Can't find a binding ~a to update." key))]
    [(cons (and (mcons lkey val) pair) rest)
     (if (eq? key lkey) 
         (set-mcdr! pair value)
         (update! rest key value))]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;set-mcdr!&lt;/code&gt; does the work.  We search until we find the symbol
requested, and then we mutate its pair.  It is an error to set a
symbol without a binding.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (interpret-set!-expression e env)
  (match e
    [`(set! ,(? symbol? var) ,expr)
     (let ((val (interpret expr env)))
       (begin (update! env var val)
              set!-result))]))

(extend-interpreter!
 set!-expression?
 interpret-set!-expression)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We will define a test environment which has a binding for 'x&lt;/p&gt;

&lt;p&gt;And test our new form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(interpret '(begin (set! x 100) x) (list (mcons 'x 10)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, we write our implementation of lambda:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define lambda-expression? 
  (match-lambda 
   [`(lambda ,(? list?) ,@(list body ...)) #t]
   [_ #f]))

(define (interpret-lambda e env)
  (match e
    [`(lambda ,(? list? vars) ,body ...)
     (lambda values
       (interpret
        `(begin ,@body)
        (extend-environment env vars values)))]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Lambda's are represented by lambdas, which is confusing, I know
(wait till you see the continuation passing interpreter!).
However, the way this works will be clear in the invokation word.
Our lambda encloses its env, var and body lists and interprets the
latter with the environment extended with the former.  It receives
values to bind to these symbols when it is invoked.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(extend-interpreter! 
 lambda-expression?
 interpret-lambda)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This allows us to write and evaluate lambda expressions but we
can't do anything with them until we define the part of the
interpreter which invokes functions.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define invokation?
  (match-lambda 
   [(list e maybe-args ...) #t]
   [_ #f]))

(define (interpret-invokation e env)
  (match e
    [(list e maybe-args ...)
     (let ((f (interpret e env))
           (args (map (lambda (e) (interpret e env)) maybe-args)))
       (if (procedure? f)
           (apply f args)
           (error "Non-function in application position!")))]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Invoke checks to make sure its first element is actually a
function, and then evaluates the arguments.  It then passes those
values to the function produced by evaluating the first argument.
This function contains an internal call to &lt;code&gt;interpret&lt;/code&gt; in the
extended environment of the function.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(extend-interpreter!
 invokation?
 interpret-invokation)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, we should add some things to our environment so that we
can actually do things.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(add-global-binding '+ (lambda args (apply + args)))
(add-global-binding '- (lambda args (apply - args)))
(add-global-binding '* (lambda args (apply * args)))
(add-global-binding '/ (lambda args (apply / args)))

(interpret '(lambda (a b c) (+ a (+ b c))) the-global-environment)

(interpret '((lambda (a b c) (- (+ a b) c)) 1 2 3) the-global-environment)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Wheeeee!&lt;/p&gt;

&lt;p&gt;Look out for the next interpreter!  Code is available on &lt;a href="https://github.com/VincentToups/racket-lib/blob/master/in-small-pieces/chapter-01/interpreter-01.rkt"&gt;github&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-4504032693150206044?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/4504032693150206044/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=4504032693150206044' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/4504032693150206044'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/4504032693150206044'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/12/literate-racket-styled-interpreter-from.html' title='Literate, Racket-Styled Interpreter from Ch. 1 of Lisp in Small Pieces'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-8130098746070254472</id><published>2011-12-07T08:53:00.000-08:00</published><updated>2011-12-07T08:58:47.530-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='persistent hash tables'/><category scheme='http://www.blogger.com/atom/ns#' term='clojure'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Persistent Hash Tables for Elisp</title><content type='html'>&lt;p&gt;Another quick update: I've added an implementation of persisten hash
tables to my &lt;a href="https://github.com/VincentToups/emacs-utils/blob/master/persistent-hash-tables.el"&gt;emacs library&lt;/a&gt;.  These are built on top of
&lt;a href="http://dorophone.blogspot.com/2011/12/random-access-lists-for-emacs.html"&gt;random access persistent lists&lt;/a&gt; and are similar to Clojure's
persistent table datatype.  &lt;/p&gt;

&lt;p&gt;They are like hash tables (with similar performance) except that &lt;code&gt;set&lt;/code&gt;
operations return a &lt;em&gt;new&lt;/em&gt; table - the old table persists unmodified.&lt;/p&gt;

&lt;p&gt;You use them like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'persistent-hash-tables)

(setq tbl ({} 'a 10 'b 11 'c 12))
(setq tbl2 (ptbl-set tbl 'c 100))
(ptbl-get tbl 'c) ;-&amp;gt; 12 
(ptbl-get tbl2 'c) ;-&amp;gt; 100
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Such tables allow functional programming over table abstractions.
These might be a better fit than association lists if you have a large
number of associations and/or you are doing a lot of functional
updates.&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-8130098746070254472?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/8130098746070254472/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=8130098746070254472' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8130098746070254472'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8130098746070254472'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/12/persistent-hash-tables-for-elisp.html' title='Persistent Hash Tables for Elisp'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-1238286373785292543</id><published>2011-12-06T08:05:00.003-08:00</published><updated>2011-12-06T13:04:41.642-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='clojure'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Random Access Lists for Emacs</title><content type='html'>&lt;p&gt;Here is a quick post: I've added an implementation of random access lists
to my Emacs Lisp library.  These are a persistent data structure which
supports fast persistent list operations (O(1)) as well as pretty fast
random-access and persistent update (O(log(n))).  These data
structures are someplace between a list and a vector, but have the
benefit of persistence - update to a random-access-list returns a
&lt;em&gt;new&lt;/em&gt; list and leaves the old one unmodified.  These are similar to
Clojure's vector datatype.&lt;/p&gt;

&lt;p&gt;You can read about these structures in &lt;a href="http://srfi.schemers.org/srfi-101/srfi-101.html"&gt;srfi-101&lt;/a&gt;.  My
implementation is a cargo-cult port of the reference implementation 
of that srfi to Emacs Lisp.&lt;/p&gt;

&lt;h2&gt;Examples:&lt;/h2&gt;

&lt;p&gt;Creation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq r (ra:list 1 2 3 4 5)) ; -&amp;gt; [cl-struct-pl-cons 1 1 [cl-struct-pl-cons 1 2 [cl-struct-pl-cons 3 [cl-struct-pl-node 3 4 5] nil]]]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;List operations are prefixed with &lt;code&gt;ra:&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(ra:car r) ;-&amp;gt; 1
(ra:cdr r) ;-&amp;gt; [cl-struct-pl-cons 1 2 [cl-struct-pl-cons 3
[cl-struct-pl-node 3 4 5] nil]]
(ra:map (lambda (x) (+ x 1)) r) ;-&amp;gt; [cl-struct-pl-cons 1 2
[cl-struct-pl-cons 1 3 [cl-struct-pl-cons 3 [cl-struct-pl-node 4 5
6] nil]]]
(ra:cons 100 r) ; -&amp;gt;[cl-struct-pl-cons 3 [cl-struct-pl-node 100 1 2] [cl-struct-pl-cons 3 [cl-struct-pl-node 3 4 5] nil]]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And so on.&lt;/p&gt;

&lt;p&gt;Random access and update are accomplished with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(ra:list-ref r 1) ;-&amp;gt; 2
(ra:list-set r 1 1000) [cl-struct-pl-cons 1 1 [cl-struct-pl-cons 1
1000 [cl-struct-pl-cons 3 [cl-struct-pl-node 3 4 5] nil]]]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that indexing is zero based.&lt;/p&gt;

&lt;p&gt;Once can convert between &lt;code&gt;ra:lists&lt;/code&gt; and &lt;code&gt;lists&lt;/code&gt; via:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(ra:random-access-list-&amp;gt;linear-access-list r) ;-&amp;gt; (1 2 3 4 5)
(ra:linear-access-list-&amp;gt;random-access-list '(1 2 3)) ;-&amp;gt;
[cl-struct-pl-cons 3 [cl-struct-pl-node 1 2 3] nil]
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Conclusion:&lt;/h2&gt;

&lt;p&gt;I wrote this library by porting over the reference implementation.
There are many places where very large random access lists will blow
the stack.  There are also probably bugs and places where efficiency
can be improved.  Please let me know if you find any!&lt;/p&gt;

&lt;p&gt;They are available from my &lt;a href="https://github.com/VincentToups/emacs-utils/blob/master/ra-lists.el"&gt;github account&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-1238286373785292543?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/1238286373785292543/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=1238286373785292543' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1238286373785292543'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1238286373785292543'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/12/random-access-lists-for-emacs.html' title='Random Access Lists for Emacs'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-7688192568667117737</id><published>2011-12-02T05:44:00.002-08:00</published><updated>2011-12-02T05:47:33.185-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='parsers'/><category scheme='http://www.blogger.com/atom/ns#' term='update'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Better Monad Parse Updated!</title><content type='html'>&lt;p&gt;I've made a few patches to &lt;a href="http://dorophone.blogspot.com/2011/10/better-monad-parse-now-available.html"&gt;better-monad-parse&lt;/a&gt; and pushed the
new version to &lt;a href="https://github.com/VincentToups/emacs-utils/blob/master/better-monad-parse.el"&gt;github&lt;/a&gt;.  The improvements primarily deal with
performance of parsers which parse strings, which I've found is the
most common use cases.&lt;/p&gt;

&lt;p&gt;This seems to make a big usability improvement for me.&lt;/p&gt;

&lt;p&gt;I also improved the &lt;code&gt;=number&lt;/code&gt; parser, which had some unexpected behavior.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Better-monad-parse&lt;/code&gt; is a monadic parser combinator implementation in
pure Elisp which is both useful for writing parsers and understanding
how monadic computations work.&lt;/p&gt;

&lt;p&gt;Suppose you have a set of files which have names consisting of one or
more characters and then a number, which you'd like to write a
function to parse.  You can do so like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'better-monad-parse)

(defparser =file-name-parser
 (name &amp;lt;- (=&amp;gt;one-or-more =alpha))
 (number &amp;lt;- =number)
 (m-return 
  `((name . ,name)
    (number . ,number))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This binds the symbol &lt;code&gt;=file-name-parser&lt;/code&gt; to a function which takes a
string as input and returns an association list (and the leftover
input) as a result, but only if parsing succeeds.  Otherwise, it
returns &lt;code&gt;nil&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;The library provides a rich set of parsers and combinators on parsers
(like &lt;code&gt;(=&amp;gt;one-or-more ...)&lt;/code&gt; above, which takes a parser and returns
a parser which parses at least one, and potentially as many as
possible, of the specified parser.  &lt;/p&gt;

&lt;p&gt;Parsers are generic over the input type, and the library knows how to
parse strings, lists and emacs buffers by default.  Using my
implementation of &lt;a href="https://github.com/VincentToups/emacs-utils/blob/master/multi-methods.md"&gt;multimethods&lt;/a&gt;, you can extend the parser for
other input types.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;better-monad-parse&lt;/code&gt; is kind of like the opposite of slumming -
you can go visit the neighborhood of pure, functional, monadic
programming from the slums of Emacs Lisp!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-7688192568667117737?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/7688192568667117737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=7688192568667117737' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7688192568667117737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7688192568667117737'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/12/better-monad-parse-updated_8409.html' title='Better Monad Parse Updated!'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-5720953029574032603</id><published>2011-11-30T06:13:00.001-08:00</published><updated>2011-12-01T17:15:11.840-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='xah lee'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><category scheme='http://www.blogger.com/atom/ns#' term='scratch buffer'/><title type='text'>How to Make Emacs' Scratch Buffer Persistent Across Sessions</title><content type='html'>&lt;h2&gt;A Quick Elisp Tutorial&lt;/h2&gt;

&lt;p&gt;Emacs has a handy, but &lt;a href="http://xahlee.org/emacs/modernization_scratch_buffer.html"&gt;sometimes decried&lt;/a&gt;, feature called the
"&lt;em&gt;scratch&lt;/em&gt;" buffer.  This is a special buffer which is created upon
startup and allows the user to type in and evaluate Emacs Lisp code.
Handy for editing tasks too specific (or not useful enough) to put
into an function and handy for exploratory Emacs Lisp interactive
development (although this development is just as easily accomplished
in any file in Lisp mode).&lt;/p&gt;

&lt;p&gt;One problem with &lt;code&gt;*scratch*&lt;/code&gt; is that its tempting to put significant
bits of code (and other information) into it.  This isn't a problem in
itself, but &lt;code&gt;*scratch*&lt;/code&gt; isn't associated with a file, and its contents
are lost without warning when Emacs is closed.  Today we'll modify the
default behavior of Emacs so that it saves the scratch buffer to a
file on exit and loads it back in on startup.&lt;/p&gt;

&lt;p&gt;This will serve as a brief Elisp tutorial.&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/scratch-buffer.jpg" alt="Get it?" title="" /&gt;
&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
Get it?
&lt;/center&gt;&lt;/p&gt;

&lt;h2&gt;Preliminaries &lt;/h2&gt;

&lt;p&gt;If you are totally new to Emacs, I'd just put the code we are about to
write directly in your &lt;code&gt;.emacs&lt;/code&gt; or &lt;code&gt;.emacs.d/init.el&lt;/code&gt; file.  These
files are executed when emacs starts, and since we need to ensure that
our behavior happens on startup, this is a reasonable place to put
code.  After years of emacs usage, your &lt;code&gt;.emacs&lt;/code&gt; will balloon into an
unspeakable cthonian mess, and you'll have to refactor it, but by then
you'll know what you need to for that process.&lt;/p&gt;

&lt;p&gt;Let's start by declaring a few parameters:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defvar persistent-scratch-filename 
    "~/.emacs-persistent-scratch"
    "Location of *scratch* file contents for persistent-scratch.")
(defvar persistent-scratch-backup-directory 
    "~/.emacs-persistent-scratch-backups/"
    "Location of backups of the *scratch* buffer contents for
    persistent-scratch.")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;defvar&lt;/code&gt; declares variables, associates them with a value (the second
term), and a doc-string (the third term).  Providing a doc-string will
ensure that when these symbols appear in the result of an apropos
search, they will have a convenient description associated with them.
It also will help us out when we look at the code in 4 years.  Don't forget to create these directories before trying the code!  (Or, modify the code to check for their existence and create them otherwise!)&lt;/p&gt;

&lt;p&gt;Our code is going to backup any version of the scratch contents before
overwriting them, just in case something &lt;em&gt;really&lt;/em&gt; important was hiding
in there.  That means we need to copy the current scratch file
contents (not the contents of the &lt;em&gt;scratch&lt;/em&gt; buffer, but the last
version of it) to a backup file.  That file needs to have a generated
name.  We'll use the date to disambiguate scratch backups:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun make-persistent-scratch-backup-name ()
  "Create a filename to backup the current scratch file by
  concatenating PERSISTENT-SCRATCH-BACKUP-DIRECTORY with the
  current date and time."
  (concat 
   persistent-scratch-backup-directory 
   (replace-regexp-in-string 
     (regexp-quote " ") "-" (current-time-string))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, the initial string in the body of this function is
documentation.  Emacs will show us this description when it displays
information about this function.  &lt;code&gt;concat&lt;/code&gt; concatenates strings,
&lt;code&gt;current-time-string&lt;/code&gt; does what you'd think, and we use
&lt;code&gt;replace-regexp-in-string&lt;/code&gt; to remove the spaces.  &lt;code&gt;regexp-quote&lt;/code&gt;
produces a regular expression which matches exactly its input string
and nothing else.&lt;/p&gt;

&lt;h2&gt;Saving the Contents of &lt;code&gt;*scratch*&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Next is our function to save the contents of the &lt;code&gt;*scratch*&lt;/code&gt; buffer.
We will eventually place this function on a "hook" which ensures it
gets run every time emacs shuts down.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun save-persistent-scratch ()
  "Write the contents of *scratch* to the file name
  PERSISTENT-SCRATCH-FILENAME, making a backup copy in
  PERSISTENT-SCRATCH-BACKUP-DIRECTORY."
  (with-current-buffer (get-buffer "*scratch*")
    (if (file-exists-p persistent-scratch-filename)
        (copy-file persistent-scratch-filename
                   (make-persistent-scratch-backup-name)))
    (write-region (point-min) (point-max) 
                  persistent-scratch-filename)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function needs to work with the contents of the &lt;code&gt;*scratch*&lt;/code&gt;
buffer, so it uses the special form &lt;code&gt;with-current-buffer&lt;/code&gt; to create a
context where actions refer to the contents of that buffer.&lt;/p&gt;

&lt;p&gt;We then check to see whether a file containing the previous scratch
buffer is present, and if it is we use &lt;code&gt;copy-file&lt;/code&gt; to copy it to the
backup directory.  We then use &lt;code&gt;write-region&lt;/code&gt; to write the contents of
&lt;code&gt;*scratch*&lt;/code&gt; from &lt;code&gt;(point-min)&lt;/code&gt; to &lt;code&gt;(point-max)&lt;/code&gt; (that is, the whole
thing), to the scratch file location.  &lt;/p&gt;

&lt;h2&gt;Loading the Contents of &lt;code&gt;*scratch*&lt;/code&gt;&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;(defun load-persistent-scratch ()
  "Load the contents of PERSISTENT-SCRATCH-FILENAME into the
  scratch buffer, clearing its contents first."
  (if (file-exists-p persistent-scratch-filename)
      (with-current-buffer (get-buffer "*scratch*")
        (delete-region (point-min) (point-max))
        (shell-command (format "cat %s" persistent-scratch-filename) (current-buffer)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(It has been correctly pointed out in the comments that &lt;code&gt;insert-file&lt;/code&gt; is the more idiomatic way of getting file contents into a buffer, which I somehow forgot about!) We first check to see whether there is a file containing the previous
session's scratch contents.  If there is, we switch to the scratch
buffer context with &lt;code&gt;with-current-buffer&lt;/code&gt; and use &lt;code&gt;shell-command&lt;/code&gt; and
&lt;code&gt;cat&lt;/code&gt; to insert the contents into &lt;code&gt;*scratch*&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Hooking It All In&lt;/h2&gt;

&lt;p&gt;Since we are writing this in our &lt;code&gt;.emacs&lt;/code&gt; file, we can ensure that the
contents of the last session's &lt;code&gt;*scratch*&lt;/code&gt; are read in by simply
writing:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(load-persistent-scratch)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will get executed on emacs startup, just as we want.  We have to
do something more complicated to ensure that &lt;code&gt;save-persistent-scratch&lt;/code&gt;
is run whenever emacs exits.&lt;/p&gt;

&lt;p&gt;To do this, we use one of emacs's many "hooks".  A hook, in emacs
parlance, is a list of functions which emacs promises to run at
specific times or when specific events occur.  The hook we need is
&lt;code&gt;kill-emacs-hook&lt;/code&gt;.  This hook is run whenever emacs is killed for any
reason.&lt;/p&gt;

&lt;p&gt;We can use the special form &lt;code&gt;push&lt;/code&gt;, which pushes an item onto the
start of a list, to add our function.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(push #'save-persistent-scratch kill-emacs-hook)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;save-persistent-scratch&lt;/code&gt; is preceded by a &lt;code&gt;#'&lt;/code&gt;.  This tells
emacs that we want the &lt;em&gt;function&lt;/em&gt; associated with the symbol
&lt;code&gt;save-persistent-scratch&lt;/code&gt;, not the &lt;em&gt;value&lt;/em&gt; (of which there is none
anyway).  &lt;/p&gt;

&lt;h2&gt;That is It!&lt;/h2&gt;

&lt;p&gt;That's it.  Save this into your &lt;code&gt;.emacs&lt;/code&gt; and you should now have a
scratch buffer which remembers its contents between sessions.  Handy!&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-5720953029574032603?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/5720953029574032603/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=5720953029574032603' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5720953029574032603'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5720953029574032603'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/11/how-to-make-emacs-scratch-buffer.html' title='How to Make Emacs&apos; Scratch Buffer Persistent Across Sessions'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-8574271810075939121</id><published>2011-11-14T14:07:00.000-08:00</published><updated>2011-11-14T14:14:28.116-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='racket'/><category scheme='http://www.blogger.com/atom/ns#' term='books'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='abstraction'/><title type='text'>Is Abstraction a Double Edged Sword?</title><content type='html'>&lt;p&gt;I've been writing a lot about functional programming.  I do so out of a
two pronged interest, the first prong of which is that I'm interested
in understanding things &lt;em&gt;myself&lt;/em&gt;, and I find there are few better ways
to test one's understand than to try and explain that understanding to
others.  The second reason is that I'm interested in education for its
own sake.  I've been drafting some chapters for a book in pure
functional game development, mostly for reason 1, and I've run across
some insights I thought I'd blog about, to see if anyone has any
helpful comments.&lt;/p&gt;

&lt;p&gt;The issue at stake is that &lt;em&gt;abstraction&lt;/em&gt; cuts both ways.  Abstract
solutions are &lt;em&gt;clean&lt;/em&gt;, simple, re-usable and reliable.  But that very
abstraction makes them hard to get a handle on.  They seem to be
cognitively slippery.&lt;/p&gt;

&lt;h2&gt;For Instance&lt;/h2&gt;

&lt;p&gt;Consider that we wish to represent a game state purely functionally,
as an &lt;em&gt;association list&lt;/em&gt;, for instance.  The game I'm working on as an
example is a kind of farming simulation where you grow letters and
build items by spelling words.  Its game state (in Racket) may look
like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define initial-state
  '((stamina . 6)
    (total-turns . 0)
    (points . 0)
    (letters . ((f . 2) 
                (o . 4)
                (d . 2)))
    (items   . ())
    (field . ())))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is pretty straightforward and concrete, such as these things go.
Each field represents a part of the game state.  &lt;code&gt;stamina&lt;/code&gt;, for
instance, represents how many more turns we can take before we we have
to rest for the day, while &lt;code&gt;letters&lt;/code&gt; is an association-list storing
the number of each letter/crop we've got stocked up.&lt;/p&gt;

&lt;p&gt;We want, however, to avoid side effects.  That means we need functions
to create a new association list whenever we want to "change" a
field.  Eg:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (increment-points state by)
  (let ((current-points (dict-ref state 'points)))
    (dict-set state (+ current-points by))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (increment-stamina state by)
  (let ((current-stamina (dict-ref state 'stamina)))
    (dict-set state (+ current-stamina by))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My aim is to write for a completely novice programmer, and both these
solutions, I think, are pretty comprehensible for such a programmer.
More advanced functional programmers might prefer to write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (increment-field state field by)
  (let ((value (dict-ref state field)))
   (dict-set state field (+ value by))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then, perhaps:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (increment-stamina state by)
  (increment-field state 'stamina by))

(define (increment-points state by)
  (increment-field state 'points by))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Both functions are probably still comprehensible to new programmers.
&lt;code&gt;increment-field&lt;/code&gt; probably seems like a kind of &lt;em&gt;detour&lt;/em&gt; or
distraction, on the way to the concrete goal of changing our game
state.  But its a small one, and relatively concrete.  Hardly
distracting at all.&lt;/p&gt;

&lt;p&gt;If I were writing this code for myself, without worrying much about
teaching someone what I was doing, I'd take this approach:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (partial-left f . partial-args)
  (lambda rest
    (apply f (append partial-args rest))))
(define (partial-right f . partial-args)
  (lambda rest
    (apply f (append rest partial-args))))
(define (always v)
  (lambda rest v))
(define (compose f1 f2)
  (lambda (args)
    (f1 (f2 args))))

(define (dict-transform dict keys-or-key fun)
  (match keys-or-key
    [(or (? symbol? key)
         (list key))
     (let ((current (dict-ref dict key)))
       (dict-set dict key (fun current)))]
    [(list key subsequent-keys)
     (let ((sub-dict (dict-ref dict key)))
       (dict-set dict key
                 (dict-transform sub-dict subsequent-keys fun)))]))

(define (increment-field dict field by)
  (dict-transform dict field (partial-right + by)))

(define (increment-stamina dict by)
  (increment-field dict 'stamina by))
(define (increment-points dict by)
  (increment-field 'points by))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or, if we want to go really nuts:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (curry-left f)
  (lambda uncurried 
    (lambda curried 
      (apply f (append uncurried curried)))))

(define (curry-right f)
  (lambda uncurried
    (lambda curried
      (apply f (append curried uncurried)))))

(define (compose-partially f . transformers)
  (lambda args
    (apply f 
           (let loop ((fs transformers)
                      (as args)
                      (acc '()))
             (if (or (empty? as)
                     (empty? fs))
                 (reverse acc)
                 (loop (cdr fs)
                       (cdr as)
                       (cons ((car fs) (car as)) acc)))))))

(define (id x) x)

(define increment-field-2 
  (compose-partially dict-transform id id (curry-left +)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I might be ashamed to admit it publically, but I think this
implementation of &lt;code&gt;increment-field&lt;/code&gt; is kind of rad (although compose
partially could probably be made more efficient or given static
semantics).  Using combinators like &lt;code&gt;compose-partially&lt;/code&gt;, &lt;code&gt;curry-*&lt;/code&gt; and
&lt;code&gt;partial-*&lt;/code&gt; we abstract away all the details involved in writing the
our functions.  It takes a bit of thought to write this way, but this
implementation has many fewer tokens into which a mistake could creep.&lt;/p&gt;

&lt;p&gt;And yet its completely billy bollucks pedogogically.  For one thing,
we're writing a ton of functions which have nothing to do with the
goal at hand.  They'll be useful &lt;em&gt;later&lt;/em&gt; because they solve a &lt;em&gt;class&lt;/em&gt;
of functional programming problems.  And they are useful now because
they make the solution to this problem &lt;em&gt;simpler&lt;/em&gt;, but they seem like a
divergence to the reader.  &lt;/p&gt;

&lt;p&gt;They are also &lt;em&gt;abstract&lt;/em&gt; - not only do they not refer specicically to
our problem, they don't seem to refer specifically to anything.  Some
of that is naming (&lt;code&gt;curry&lt;/code&gt; is not very descriptive) but some of it is
that we are programming at the "function level," in APL/J parlance.
The objects we are manipulating are the fundamental objects of our
code themselves (functions).  These functions (&lt;code&gt;curry-*&lt;/code&gt;, &lt;code&gt;partial-*&lt;/code&gt;,
&lt;code&gt;compose&lt;/code&gt;, etc) are all about controlling when, exactly, things are
"done."  They are conspicuosly not about doing things.&lt;/p&gt;

&lt;p&gt;And then, even when we get to our function, &lt;code&gt;increment-field&lt;/code&gt;, the
implementation seems to have nothing at all to do with what we seem to
want to be doing.  No names appear, no values seem to be calculated
(although functions &lt;em&gt;are&lt;/em&gt; calculated, they seem invisible to a new
programmer).  &lt;/p&gt;

&lt;h2&gt;The Problem&lt;/h2&gt;

&lt;p&gt;Who cares, though?  If you are teaching novices, just avoid this
approach, you might say.  Fair enough, but it seems that when
functional purity is a design goal, these kinds of higher order
functions &lt;em&gt;really&lt;/em&gt; help to avoid verbose and repeatative code.  How
would you find a happy medium?  When you restrict yourself to low
level idioms, purely functional code seems to become ugly, deeply
nested, and difficult to read and write.  Even given that I've hammed
it up seriously in this post, it seems hard to find a happy medium.
What do my readers (if any there be) think?&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-8574271810075939121?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/8574271810075939121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=8574271810075939121' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8574271810075939121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8574271810075939121'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/11/is-abstraction-double-edged-sword.html' title='Is Abstraction a Double Edged Sword?'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-8800734887698461500</id><published>2011-11-05T14:07:00.000-07:00</published><updated>2011-11-05T14:17:19.339-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='racket'/><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='io'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>Understanding the Haskell IO Monad.</title><content type='html'>&lt;h2&gt;By Building a Toy Model in Scheme&lt;/h2&gt;

&lt;p&gt;Somehow or another, this blog ended up being about two basic things:
Lisp and Functional Programming.  I've talked about almost every Lisp
species there is, but there is an Elephant in the Functional
Programming room, which has only come up every so often, which is the
programming language &lt;a href="http://www.haskell.org/haskellwiki/Haskell"&gt;Haskell&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://108zenbooks.files.wordpress.com/2011/06/enso6.jpg?w=392&amp;amp;h=504" alt="Lisp!" title="" /&gt;
&lt;/center&gt;
&lt;center&gt; 
Lisp is the Tao, at least to awesome hippies like Mark Tarver.  Image
borrowed from &lt;a href="http://108zenbooks.com/tag/david-loy/"&gt;here&lt;/a&gt;.
&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;I'm quite fond of Haskell, in principle.  If Lisp is the Tao -- a kind
of informal, flexible cloud around the locus of the ideal programming
language -- then Haskell might be a rather rigidly specified diamond
right near that locus' center.  Haskell trades a lot of the looseness
of Lisp for a rigid semantics, but it seems there is a good argument
that these semantics are much closer to the ideal of Functional
Programming.&lt;/p&gt;

&lt;p&gt;I don't have much opportunity to program in Haskell, and so I don't
write a lot about it.  I do program in Lisp a lot, and I think I've
subconsciously been engaged in a long term project to improve my
understanding of Haskell by implementing toy models of parts of it in
Lisp.  Hence all the posts about monads.  I've recently started
writing a book about pure functional programming in Scheme/Racket and
initial exploratory writing has produced a few insights about Haskell's
IO Monad.  &lt;/p&gt;

&lt;p&gt;The IO Monad seems to be among Haskell's most mysterious monads for
new programmers.  Monad tutorials and explications rarely talk about
it.  Haskell tutorials introduce it as an ad-hoc: they show you how to
use it syntactically, but its rarely made naked.  And there is a
reason for this: whatever it is, precisely, that the IO Monad does
isn't expressable in regular old safe Haskell.    &lt;/p&gt;

&lt;p&gt;But its pretty important, and not just because its the only kosher way
for a Haskell programmer to communicate with the outside world.  In a
sense, a Haskell program can be viewed as a &lt;em&gt;description&lt;/em&gt; of some IO
actions.  The Haskell run time can be thought of as constructing that
IO action, and then, somehow, executing it.  If you've heard
programmers say that, even when using the IO Monad, one is still
writing "pure" code, this is what they mean: the Haskell program
itself simply &lt;em&gt;constructs&lt;/em&gt; an IO action from smaller, and ultimately
primitive IO actions.  The actions are never &lt;em&gt;run&lt;/em&gt; in the code itself,
just constructed.  Eventually the Haskell runtime executes them, but
the construction of the IO commands is itself purely a matter of
functional programming, without side effects.&lt;/p&gt;

&lt;p&gt;(Note that the programmer can bail out of this paradigm using thigns
like &lt;a href="http://users.skynet.be/jyp/html/base/System-IO-Unsafe.html"&gt;unsafePerformIO&lt;/a&gt;, but that is frowned upon, for the most
part).&lt;/p&gt;

&lt;p&gt;So its important, I think, to develop a mental model of what is going
on here.&lt;/p&gt;

&lt;h2&gt;Enter Scheme&lt;/h2&gt;

&lt;p&gt;I Love Lisp/Scheme, because using Lisp is a great way to understand
language constructs.  Lisp is a kind of programming language lab,
where a crappy, ad-hoc and slow implementation of a language feature
is always a few hundred lines of code away.  It takes serious effort
to design a non-standard language on top of Lisp for an "industrial
strength" application, but if you just want to understand something,
Lisp can take you there.  It is a great way to get to know unusual
semantics.&lt;/p&gt;

&lt;p&gt;Today we're going to use Racket, which really isn't a Scheme anymore,
so much as a Scheme superset, to build a toy model of the IO monad.
The bird's eye view is that we'll build a lexicon of IO &lt;em&gt;actions&lt;/em&gt;,
which will be regular old functions which take the IO state and maybe
do something with it, and return a value, and then we'll build some
machinery to construct more complex IO actions from simpler ones.  &lt;/p&gt;

&lt;p&gt;We'll also build an external library to execute these IO actions once
they've been built, but the intent is that the &lt;em&gt;user&lt;/em&gt; is segregated
from this execution.  They don't know anything about it, and all they
do is build IO actions and hand them off.  &lt;/p&gt;

&lt;h2&gt;Bundling Up IO&lt;/h2&gt;

&lt;p&gt;Let's start with our "low level" IO.  One possibility is that we
bundle up &lt;code&gt;standard-input&lt;/code&gt; and &lt;code&gt;standard-output&lt;/code&gt; into a type of some
kind.  IO actions will just, then, be functions which take this pair
of standard input and output and return values.  I'd like to do
something a little more fun.  Let's build a graphical user interface
that has an input window and and output window and build an IO Monad
over them, instead.  This will give us a change to play around with
Racket's GUI classes, which is a nice thing to get acquainted with.&lt;/p&gt;

&lt;h2&gt;Sketch&lt;/h2&gt;

&lt;p&gt;We'll write a function &lt;code&gt;run-io&lt;/code&gt; which will take an IO action, pop up a
window, and execute that action.  It will have the type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type io-action = io -&amp;gt; a
run-io :: io-action -&amp;gt; a
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where we mean by &lt;code&gt;a&lt;/code&gt; that the function returns the type &lt;code&gt;a&lt;/code&gt;,
which can be anything.  Our function takes a single value of type
&lt;code&gt;io-action&lt;/code&gt;, which we will get specific about in a bit.&lt;/p&gt;

&lt;p&gt;This function has to do a few things.  It needs to initialize a
&lt;code&gt;frame%&lt;/code&gt;, put some editor's in it with a layout of some kind, and then
it needs to create the &lt;code&gt;io&lt;/code&gt; object, pass it to the action, and then
return the result of the io action.  The window will be broken into an
output area and an input area.  The output area will have no special
behavior, except, of course, that we'll be able to write to it and
clear it.  The input area will be a kind of "command line," - we want
it to only accept one line at a time, which it detects by looking for
a &lt;code&gt;newline&lt;/code&gt;.  Somehow, we want each command to end up being readable
from within the monad.&lt;/p&gt;

&lt;p&gt;Text windows are represented by two classes working in tandem in
Racket, a &lt;code&gt;text%&lt;/code&gt; class and an &lt;code&gt;editor%&lt;/code&gt; class.  We can accomplish our
goals by subclassing the &lt;code&gt;text%&lt;/code&gt; part.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require racket/gui
     (only-in srfi/13 string-trim-both))

(define (string-contains-newline? str)
  (let loop ((chars (string-&amp;gt;list str)))
    (match chars 
      [(list) #f]
      [(cons #\newline rest) #t]
      [(cons x rest)
       (loop rest)])))

(define input-text% 
  (class text% 
    (field (command #f))
    (define/public (text-substring start len)
      (let loop ((acc '())
                 (i 0))
        (if (&amp;lt; i len) (loop (cons (send this get-character (+ start i)) acc) (+ i 1))
            (list-&amp;gt;string (reverse acc)))))
    (define/augment (after-insert start len) 
      (let ((string (send this text-substring start len)))
        (if (string-contains-newline? string) 
            (let ((a-command (send this text-substring 2 (+ start len))))
              (set! command (string-trim-both a-command))
              (send this erase)
              (send this insert "&amp;gt; "))
            #f)))
    (define/public (grab-command) 
      (let ((c command))
        (set! command #f)
        c))
    (super-new)
    (send this insert "&amp;gt; ")))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We've added a field to store the current command, with &lt;code&gt;#f&lt;/code&gt; no
command, and augmented the &lt;code&gt;after-insert&lt;/code&gt; method to detect command
entry and act appropriately.  The new method &lt;code&gt;grab-command&lt;/code&gt; fetches
the current command and sets the internal command to &lt;code&gt;#f&lt;/code&gt; again.  &lt;/p&gt;

&lt;p&gt;There is no easy way to make setting up a GUI look nice, as far as I
know, so here is the gory details:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (setup-window)
  (let* ((frame (new frame% 
                     [label "io toy"]
                     [width 640]
                     [height 480]))
         (pane (new vertical-pane% 
                    [min-width 640]
                    [min-height 480]
                    [parent frame]))
         (output-text (new text%))
         (output (new editor-canvas% 
                      [parent pane]
                      [style '(no-hscroll)]
                      [editor output-text]
                      [min-width 640]
                      [min-height 400]))
         (input-text (new input-text%))
         (input (new editor-canvas%
                     [parent pane]
                      [style '(no-hscroll)]
                      [editor input-text]
                      [min-width 640]
                      [min-height 40]))
         (io (lambda (input)
               (match input
                 [(? string? x) (send output-text insert input)]
                 ['clear (send output-text erase)]
                 ['fetch-command
                  (let loop 
                    ((command (send input-text grab-command)))
                    (yield)
                    (if command command (loop (send input-text
                                                    grab-command))))]))))
    (send frame show #t)
    io))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://img6.imagebanana.com/img/oga1vbh5/Selection_013.png" alt="The IO Toy" title="" /&gt;
&lt;/center&gt;
&lt;center&gt;
The IO Window in All Its Splendor
&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;This does the business of setting up our window and constructing our
"io" value, which is apparently a function which can take either
arbitrary text, which it inserts into the output buffer, or the
symbols &lt;code&gt;clear&lt;/code&gt; and &lt;code&gt;fetch-command&lt;/code&gt;.  &lt;code&gt;clear&lt;/code&gt;, straightforwardly
enough, clears the output region with the &lt;code&gt;erase&lt;/code&gt; method.
&lt;code&gt;fetch-command&lt;/code&gt; enters a loop and doesn't return until a command is
entered by the user, although it does call &lt;code&gt;yield&lt;/code&gt; in order to let the
GUI thread check for new events.  &lt;/p&gt;

&lt;p&gt;With this function, we can easily define:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (run-io action) 
 (let ((io (setup-window)))
    (action io)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now our users can concern themselves entirely with IO action
construction.  They trust us to execute the action later.  What does
an action look like?&lt;/p&gt;

&lt;p&gt;Well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (clear io) (io 'clear))
(define (get-command io) (io 'fetch-command))
(define (insert text) (lambda (io) (io text)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Are three such commands.  In fact, they are going to be our "atomic"
commands, from which we'll build everything else.  &lt;/p&gt;

&lt;p&gt;One wrinkle: &lt;code&gt;insert&lt;/code&gt; isn't itself an io action, it is a function
which &lt;em&gt;returns&lt;/em&gt; an io action which inserts the indicated piece of
text.  &lt;code&gt;insert&lt;/code&gt; is a simple kind of io-action builder.  We'll see how
to integrate io actions together in a bit.&lt;/p&gt;

&lt;p&gt;Let's wrap this all up in a module, only exporting our low level
interface:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(provide run-io clear get-command insert)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Saving the file as &lt;code&gt;toy-io.rkt&lt;/code&gt; will create a module.  You can also
download this code from my &lt;a href="https://github.com/VincentToups/racket-lib/tree/master/blog"&gt;github&lt;/a&gt; account.&lt;/p&gt;

&lt;p&gt;You've got to place that file somewhere where Racket can get to it,
and then you can fire up Racket and say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require blog/toy-io)
(run-io (insert "Testing, 1, 2, 3!"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And press go.  What this should do is pop up a window with an
interaction area and an output area with "Testing, 1, 2, 3!" in it.
Great.  Now we should focus on building up IO actions.&lt;/p&gt;

&lt;h2&gt;The IO Monad&lt;/h2&gt;

&lt;p&gt;Monads always have an associated &lt;em&gt;monadic value&lt;/em&gt; of a particular
type.  In this case, our monadic values are functions which accept an
IO object and return any old thing.  The &lt;code&gt;get-command&lt;/code&gt; is a monadic
value - it accepts the &lt;code&gt;io&lt;/code&gt; object and returns the command received
from the user.  What if you wanted a &lt;code&gt;get-two-commands&lt;/code&gt; which read two
commands and returned them as a list?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (get-two-commands io)
  (let ((c1 (get-command io))
        (c2 (get-command io)))
    (list c1 c2)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simple enough.  &lt;code&gt;get-two-commands&lt;/code&gt; is, itself, a monadic value.&lt;/p&gt;

&lt;p&gt;Now for the monad part: what if you want to define a monadic value
which gets &lt;em&gt;one&lt;/em&gt; command, and then determines what to do based on the
result of that command.  We can represent the choice we'll make about
what to do next with a function which is waiting for the previously
read command and will eventually return an io action.  Think of a
lambda expression as a computation with some &lt;code&gt;unbound&lt;/code&gt; variable which
will calculate a value when that variable is provided or &lt;code&gt;bound&lt;/code&gt;.
Hence, consider the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (io-bind io-action io-action-producer)
  (lambda (io)
    (let* ((result (io-action io))
           (next-io-action
            (io-action-producer result)))
     (next-io-action io))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;io-bind&lt;/code&gt; is our io action construction work horse.  It builds up a
new io action from an io action and an &lt;code&gt;io-action-producer&lt;/code&gt;, which is
waiting for the result of the previous io action before it can decide
what to do.  &lt;code&gt;io-bind&lt;/code&gt; returns an io action, of course.  That io
action executes the first argument to &lt;code&gt;io-bind&lt;/code&gt; when it receives the
io object.  It then passes the result of that action to the producer,
and executes the resulting io action on the io object.  &lt;/p&gt;

&lt;p&gt;This is sufficient to build up a large family of complex io actions
which depend on the results of querying the input.  We need one more
function to build our monad, though:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (io-return value)
  (lambda (io)
     value))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function makes the simplest kinds of io actions.  They don't do
anything, but they do return whatever value was specified when
&lt;code&gt;io-return&lt;/code&gt; was called.  &lt;code&gt;io-return&lt;/code&gt;, in other words, inserts values
into the io monad.&lt;/p&gt;

&lt;h2&gt;Building IO Actions&lt;/h2&gt;

&lt;p&gt;Let's build an io action that checks for the command "exit."  If
"exit" is encountered, then the io action simply returns, but if it
isn't encountered, it reads another command and so on.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define exit-flagger 
  (io-bind get-command 
           (lambda (command)
             (if (equal? command "exit") 
                 (io-return 'exit-detected)
                 exit-flagger))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you then execute &lt;code&gt;(run-io exit-flagger)&lt;/code&gt;, you'll get a window which
will keep accepting inputs until you type "exit," at which point,
control will return to the command line.&lt;/p&gt;

&lt;p&gt;Here we use &lt;code&gt;io-bind&lt;/code&gt; to construct a new io action directly, but
Haskell provides special syntax for monadic behavior.  Let's build a
version of that syntax here.&lt;/p&gt;

&lt;h2&gt;"Do" Notation&lt;/h2&gt;

&lt;p&gt;We'll create a &lt;code&gt;do-io&lt;/code&gt; form which accepts a list of expressions of two
varieties.  An expression must either evaluate to an io action, or it
must specify that a value should be bound to the result of an io
action in the following way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(command &amp;lt;- get-command)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We'd rather not get into a &lt;a href="http://dorophone.blogspot.com/2011/07/survey-of-syntactic-extension.html"&gt;deep&lt;/a&gt; &lt;a href="http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension.html"&gt;discussion&lt;/a&gt; about &lt;a href="http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension_25.html"&gt;macros&lt;/a&gt; in Scheme, so
we'll use regular old &lt;code&gt;syntax-rules&lt;/code&gt;.  Not much error handling, but
its pretty simple to understand.&lt;/p&gt;

&lt;p&gt;Consider:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-syntax do-io 
  (syntax-rules (&amp;lt;-)
   [(do-io (id &amp;lt;- io-action) expr ...)
     (io-bind io-action 
        (lambda (id)
          (do-io expr ...)))]
   [(do-io io-action expr ...)
    (io-bind io-action 
     (lambda (_) 
       (do-io expr ...)))]
   [(do-io io-action) io-action]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Our syntax-extension has three cases.  The most specific appears
first, where we detect an expression indicating a monadic binding.
We pass the monadic value &lt;code&gt;io-action&lt;/code&gt; to &lt;code&gt;io-bind&lt;/code&gt; along with a
&lt;code&gt;lambda&lt;/code&gt; expression consisting of the rest of the &lt;code&gt;do&lt;/code&gt;-expression in a
context where the &lt;code&gt;id&lt;/code&gt; is bound to the result of the action.  When we
don't have a binding expression of the form &lt;code&gt;(id &amp;lt;- io-action)&lt;/code&gt;,
expand the macro the same way, but don't even bother creating a
variable visible in the rest of the expressions.  If we only have a
single expression, then we just return its value.  &lt;/p&gt;

&lt;p&gt;We can now write our &lt;code&gt;exit-flagger&lt;/code&gt; io action as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define exit-flagger
 (do-io 
  (command &amp;lt;- get-command)
  (if (equal? command "exit") 
      (io-return 'exit-detected)
      exit-flagger)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This resembles Haskell notation, where the same thing might look
something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;exitFlagger = do
  command &amp;lt;- getCommand
  if equal? command "exit" 
     then
       return "exit-detected"
     else
       exitFlagger
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;A Simple Example&lt;/h2&gt;

&lt;p&gt;Let's write some brains into our command interpreter.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (read-command str)
  (with-handlers ([exn:fail? (lambda (e) 'error)])
    (with-input-from-string 
     (string-append "(" str ")")
     (lambda () (read)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function converts a string into a list of values.  If it doesn't
receive a string which, when wrapped with "()" evaluates to a piece of
Scheme code, it just returns &lt;code&gt;error&lt;/code&gt;.  We use it to
make dispatching on commands easier:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define symbolic-command
  (do-io 
    (cmd &amp;lt;- get-command)
    (io-return (read-command str))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (repeat-char n char)
  (let loop ((n n)
             (acc '()))
     (if (&amp;lt;= n 0) (list-&amp;gt;string acc)
         (loop (- n 1) (cons char acc)))))

(define dispatch
  (do-io
   (c &amp;lt;- symbolic-command)
   (match c
     ['error 
      (do-io
       (insert "Error reading command string.")
       dispatch)]
     [(list 'stars (? number? n))
      (do-io 
       clear
       (insert (repeat-char n #\*))
       dispatch)]
     [(list 'exit) (io-return 'exit-detected)]
     [_ (do-io
         clear
         (insert "unknown command.")
         dispatch)])))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;dispatch&lt;/code&gt; is pretty easy to understand.  It is an io action which
grabs a command, converts it to a list of atoms and lists, matches
that list for known commands, and loops, unless the command read is
&lt;code&gt;(exit)&lt;/code&gt;.  Note that &lt;code&gt;dispatch&lt;/code&gt; is defined without ever explicitely
referencing the &lt;code&gt;io&lt;/code&gt; state that is threaded through all its parts.
We've liberated the programmer from thinking about what that state is
or how it is managed.  They simply build up io actions like any other
kind of data.  &lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://img7.imagebanana.com/img/pddnw5wp/Selection_014.png" alt="dispatch in action" title="" /&gt;
&lt;/center&gt;
&lt;center&gt;
Dispatch in action!
&lt;/center&gt;&lt;/p&gt;

&lt;h2&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;Ok, so we built a low level interface which non-functionally handles
input and output operations for us.  Because input and output is
inherently stateful, there is no real way to avoid side effects in
this part of our code.&lt;/p&gt;

&lt;p&gt;However, we then realized that we could "encapsulate" input and output
operations in a few primitive io actions and provide a monadic context
wherein the user can build more complex actions from simpler ones.  Up
until the moment the user calls &lt;code&gt;run-io&lt;/code&gt;, she maintains complete
purity.  Constructing io actions can be done completely without side
effects.  &lt;/p&gt;

&lt;p&gt;For bonus, we implemented a syntax mimicing Haskell's do notation in
terms of the function &lt;code&gt;io-bind&lt;/code&gt;.  This gave us a toy model of
Haskell's io monad, which we used to build some simple io actions.  &lt;/p&gt;

&lt;p&gt;Glazed over in all this discussion were the implications of Haskell's
lazy semantics, type system, and error handling mechanisms.  We might
also begin to wonder how we combine this stateless io programming with
other, apparently stateful tasks.  We'll see how we can do that in a
later post (or in the book, if I ever write it), and how these
considerations relate to monad transformers!&lt;/p&gt;

&lt;p&gt;This code is up on my &lt;a href="https://github.com/VincentToups/racket-lib/tree/master/blog"&gt;github&lt;/a&gt;.  If you are interested in monads,
you can also check out my &lt;a href="http://planet.plt-scheme.org/package-source/toups/functional.plt/1/1/planet-docs/better-monads-guide/index.html"&gt;better-monads&lt;/a&gt; library on Planet Racket.
It doesn't include an IO monad because that is kind of ridiculous in
Scheme!&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-8800734887698461500?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/8800734887698461500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=8800734887698461500' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8800734887698461500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8800734887698461500'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/11/understanding-haskell-io-monad.html' title='Understanding the Haskell IO Monad.'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-5607879382402601746</id><published>2011-10-18T06:01:00.000-07:00</published><updated>2011-10-18T06:04:18.401-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='parsers'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>Better-monad-parse Now Available</title><content type='html'>&lt;p&gt;I've pushed &lt;code&gt;better-monad-parse.el&lt;/code&gt; to my &lt;a href="https://github.com/VincentToups/emacs-utils/blob/master/better-monad-parse.el"&gt;github repository&lt;/a&gt;.  This is
a refactoring of &lt;code&gt;monad-parse&lt;/code&gt;, principally to make its interface more
regular and grokable.  It also is more intellegently polymorphic over
different kinds of parseable objects, principally by making use of my
implementation of multimethods instead of EIEIO, which, as far as I'm
able to ascertain, doesn't allow method dispatch on built-in classes.  &lt;/p&gt;

&lt;p&gt;Unfortunately, its a bit slow, even compiled.  I'd love to have advice
on how it might be sped up, although I'm thinking its a lost cause -
this kind of programming might be too taxing for Emacs Lisp.&lt;/p&gt;

&lt;h2&gt;Notes&lt;/h2&gt;

&lt;p&gt;This library follows the following conventions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parsers are functions which accept a generic input state and
  return a list of cons cells, where the &lt;code&gt;car&lt;/code&gt; is the monadic return
  value and the &lt;code&gt;cdr&lt;/code&gt; is the unparsed input.&lt;/li&gt;
&lt;li&gt;All parsers defined by the library start with the symbol &lt;code&gt;=&lt;/code&gt;,
  eg &lt;code&gt;=nil&lt;/code&gt;, &lt;code&gt;=item&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;Parsers are often parameterized, that is, a parser will be
  created based on some value passed to a function.  Functions
  which &lt;em&gt;produce&lt;/em&gt; parsers begin with the symbol &lt;code&gt;=&amp;gt;&lt;/code&gt;, eg:
  =&gt;string, =&gt;items, =&gt;or, =&gt;and and so forth.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;New parsers should be most easily created with the &lt;code&gt;parser&lt;/code&gt; special
form, which introduces a monadic expression context for parser
construction:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; (parser 
   (n &amp;lt;- =number)
   (letters &amp;lt;- (=&amp;gt;zero-plus-more =alpha))
   (m-return (list n letters)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Produces a parser which binds n to a number parsed from the input, and
then binds letters to a list of zero or more letters parsed from the
input.  All the parses defined by the library are generic, so they can
parse buffers, strings or lists.  There is a rich vocabulary of
parsers to combine in the library, but if you wish to write your own
generic parsers, you'll need to create a &lt;a href="https://github.com/VincentToups/emacs-utils/blob/master/multi-methods.el"&gt;multimethod&lt;/a&gt; to cover all
input types.  &lt;/p&gt;

&lt;p&gt;Understanding mulitmethods is also critical if you want to extend the
parser library to cover additional input types.&lt;/p&gt;

&lt;p&gt;This library is all about defining parsers, so there are special forms
for that purpose:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser =number-then-letters
   (n &amp;lt;- =number)
   (letters &amp;lt;- (=&amp;gt;zero-plus-more =alpha))
   (m-return (list n letters)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sets &lt;code&gt;=number-then-letters&lt;/code&gt; symbol and function value to the parser
produced by the body expression evaluated in the parser monad.  This
allows you to call the parser as a function and also refer to it as a
variable, since both actions are common.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;defparser&lt;/code&gt; allows the creation of parametric parsers too:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser (=&amp;gt;specific-number-then-letters n)
   (n-parsed &amp;lt;- =number)
   (if (= n n-parsed)
       (parser 
          (letters &amp;lt;- (=&amp;gt;zero-plus-more =alpha))
          (m-return n letters))
       =nil))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;defun&lt;/code&gt;'s a function which produces the indicated parser.  Arguments
are automatically lexically closed over.  This version doesn't set the
symbol-value of the name.&lt;/p&gt;

&lt;p&gt;As stated above, the library performance is a bit slow still - I
haven't determined to what that slowness owes, but I recommend
byte-compiling everything.&lt;/p&gt;

&lt;p&gt;I'm almost certainly going to port this whole thing to Common Lisp at
some point, so I can call out to it via slime.  That should help with
performance, as I have a nagging feeling that the bottleneck is my
ad-hoc multimethod dispatch.&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-5607879382402601746?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/5607879382402601746/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=5607879382402601746' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5607879382402601746'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5607879382402601746'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/10/better-monad-parse-now-available.html' title='Better-monad-parse Now Available'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-7503270528958878913</id><published>2011-10-07T07:34:00.000-07:00</published><updated>2011-10-07T10:09:42.919-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='parsers'/><category scheme='http://www.blogger.com/atom/ns#' term='parsing'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>Update to my Monadic Parser Combinator Elisp Library (+ examples/tutorial)</title><content type='html'>&lt;p&gt;I've updated my emacs lisp monadic parser combinator library.  The
underlying mechanisms for building parsers remain unchanged, but I've
introduced a few new syntactic forms to make writing parses a little
easier than it used to be. &lt;/p&gt;

&lt;p&gt;If you want to understand how the library works, under the hood, check
out my screencast about monadic parser combinators in Elisp located
&lt;a href="http://dorophone.blogspot.com/2011/05/monadic-parser-combinators-in-elisp.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post will introduce those features and give a quick tour of how
I'm &lt;em&gt;actually&lt;/em&gt; using them in my day to day life as a researcher.&lt;/p&gt;

&lt;p&gt;To use the library, download my code from &lt;a href="https://github.com/VincentToups/emacs-utils"&gt;github&lt;/a&gt;, add the
directory to your emacs path, and then:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'monad-parse)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which will load the parser monad and its associated functions.  It
&lt;em&gt;really&lt;/em&gt; helps to byte-compile this library, because it relies on some
serious macro magic behind the scenes.  Performances is tolerable for
the compiled version, but can be a real drag without compilation.&lt;/p&gt;

&lt;p&gt;If you get an error about &lt;code&gt;filename&lt;/code&gt; being undefined during
compilation, evaluate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq filename nil)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in your scratch buffer.  This is some emacs bug that has to do with
CEDIT, which I don't even use.  Beats me!&lt;/p&gt;

&lt;p&gt;We'll be using an example from my day job today.  I work in an
electrochemistry lab.  My background is in data analysis and modeling,
and so I get lots of files containing data from my colleagues.  The
files are given human-readable names describing the kinds of data they
contain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;firstElectrode/1000uM_DA_.4-1-3bg_CV.txt
firstElectrode/500uM_DA_.4-1-3bg_CV.txt
firstElectrode/250uM_DA_.4-1-3bg_CV.txt
firstElectrode/100uM_DA_.4-1-3bg_CV.txt
firstElectrode/1000uM_DA_.4-1-3cv_CV.txt
firstElectrode/500uM_DA_.4-1-3cv_CV.txt
firstElectrode/250uM_DA_.4-1-3cv_CV.txt
firstElectrode/100uM_DA_.4-1-3cv_CV.txt
firstElectrode/1000uM_DA_.4-1-3bg_CV1.txt
firstElectrode/500uM_DA_.4-1-3bg_CV1.txt
firstElectrode/250uM_DA_.4-1-3bg_CV1.txt
firstElectrode/100uM_DA_.4-1-3bg_CV1.txt
firstElectrode/1000uM_DA_.4-1-3cv_CV1.txt
michaelElectrode/500uM_DA_.4-1-3cv_CV1.txt
michaelElectrode/250uM_DA_.4-1-3cv_CV1.txt
michaelElectrode/100uM_DA_.4-1-3cv_CV1.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I do analysis which requires that I grab all these files and process
them together, and I need to know things like what dopamine
concentration each file has and other information which is in the file
name, but not easy for a computer to get at.  My strategy is to write
a parser which converts the human readable filenames into a data
structure, and then generate a new, very easy to parse, filename to
copy the file to.  &lt;/p&gt;

&lt;p&gt;This is because I'm doing the data analysis in Matlab, where its hard
to write a general purpose parser.  Better to let emacs do the work of
parsing the complex names and generate simple names for Matlab.&lt;/p&gt;

&lt;h2&gt;Building Up a Parser&lt;/h2&gt;

&lt;p&gt;We'll start with preliminaries, little parsers we need to break up the
file names:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser slash (=string "/"))
(defparser underscore (=string "_"))
(defparser dash (=string "-"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next we need to write parsers for larger bits of text.  These are
still relatively simple.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser experiment-label
     (characters &amp;lt;- (=zero-or-more (=not (=string "/"))))
     (m-return
      (alist&amp;gt;&amp;gt; :label (coerce characters 'string))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When parsing strings and buffers, &lt;code&gt;=zero-or-more&lt;/code&gt; creates a parser
which returns a list of character codes.  We bind that list to a
variable, &lt;code&gt;characters&lt;/code&gt; and coerce it into a string, which is returned
to the caller as the monadic return value.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;defparser&lt;/code&gt;'s body allows you to write code more or less like you
would in Haskell's &lt;code&gt;do&lt;/code&gt; notation.  It expands to a &lt;code&gt;defvar&lt;/code&gt;
expression, but the body is placed inside a &lt;code&gt;parser&lt;/code&gt; special form.
&lt;code&gt;parser&lt;/code&gt; lets you create anonymous parsers.  &lt;/p&gt;

&lt;p&gt;We can test this parser like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(parse-string experiment-label "abcdef/gea") ;-&amp;gt; (:label "abcdef")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;parse-string&lt;/code&gt; returns just the parsed part of the input, and throws
away the leftovers.  It also converts its second argument to a parser
input stream.  You can use a parser directly like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(funcall experiment-label (-&amp;gt;in "abcdef/gea"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which returns the entire parser state:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(((:label "abcdef") . [object &amp;lt;parser-input-string&amp;gt; "&amp;lt;parser-input-string&amp;gt;" "abcdef/gea" 6]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The library supports non-deterministic parsing, so parsers return a
list of all the parser states produced, each of which is a pair
containing the parsed value and an object represening the rest of the
parse state.  In most cases, you'll want to use &lt;code&gt;parse-string&lt;/code&gt;.  But
the library supports buffer parsing and list parsing.&lt;/p&gt;

&lt;p&gt;Now lets build our first multi-part parser.  This will parse a
quantity and its units.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser amount
  (magnitude &amp;lt;- (=number-&amp;gt;number))
  (units &amp;lt;- (=string "uM" "nM"))
  (m-return (alist&amp;gt;&amp;gt; :amount magnitude :units units)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This parser makes use of the &lt;code&gt;=number-&amp;gt;number&lt;/code&gt; function, which returns
a parser which converts a string representation of a base 10 decimal
number into an actual number.  We then use &lt;code&gt;=string&lt;/code&gt; to capture units
of either &lt;code&gt;uM&lt;/code&gt; or &lt;code&gt;nM&lt;/code&gt;, the only units in my data set.&lt;/p&gt;

&lt;p&gt;These are packed into an association list for later use.&lt;/p&gt;

&lt;p&gt;Data files come in two flavors, "background" and "cv".  This parser
parses the string representing one of them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser cv-type
  (type &amp;lt;- (=string "bg" "cv"))
  (m-return (alist&amp;gt;&amp;gt; :cvType type)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As above, the &lt;code&gt;=string&lt;/code&gt; combinator produces a parser which matches any
of the strings it receives. &lt;/p&gt;

&lt;p&gt;A parser for substances looks like, then:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser substance
  (sub &amp;lt;- (=stringi "DA" "AA" "pH" "H2o2"))
  (m-return (alist&amp;gt;&amp;gt; :substance sub)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we use a variant of &lt;code&gt;=string&lt;/code&gt;, &lt;code&gt;=stringi&lt;/code&gt; which matches any of
the input strings without considering case.  It returns the actual
match.  &lt;/p&gt;

&lt;p&gt;Now we need a parser to deal with that soup of numbers and dashes
which is supposed to represent a voltage range:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;michaelElectrode/100uM_DA_.4-1-3cv_CV1.txt  
                          ------
                          this stuff
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here is the parser:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser voltage-range
  (low &amp;lt;- (=number-&amp;gt;number))
  dash
  (pre-decimal &amp;lt;- (=number-&amp;gt;number))
  (post-decimal &amp;lt;- (=number-&amp;gt;number))
  (m-return
   (alist&amp;gt;&amp;gt; :voltageLow low
            :voltageHigh (+ pre-decimal (/ post-decimal 10.0)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, some experiments contain multiple electrodes.  If this is the
case, the electrode number appears.  We need a parser that &lt;em&gt;tries&lt;/em&gt; to
find a number and returns 0 if it can't find one:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser electrode-number
  (n &amp;lt;- (=maybe (=number-&amp;gt;number)))
  (m-return (alist&amp;gt;&amp;gt; :electrode (if n n 0))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we put them all together:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser file
  (label &amp;lt;- experiment-label)
  slash
  (amt &amp;lt;- amount)
  underscore
  (sub &amp;lt;- substance)
  underscore
  (rng &amp;lt;- voltage-range)
  (type &amp;lt;- cv-type)
  underscore
  (=string "CV")
  (el &amp;lt;- electrode-number)
  (m-return
   (append label amt sub rng type el)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And we make it work:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; (parse-string file "michaelElectrode/100uM_DA_.4-1-3cv_CV1.txt")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Produces:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; ((:label "michaelElectrode") 
  (:amount 100) 
  (:units "uM") 
  (:substance "DA") 
  (:voltageLow 0.4) 
  (:voltageHigh 1.3) 
  (:cvType "cv") 
  (:electrode 1))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I have a utility function in my chemistry library that will convert
such an alist into a much easier to parse form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'chemistry)
(condition-&amp;gt;filename '((:label "michaelElectrode") 
  (:amount 100) 
  (:units "uM") 
  (:substance "DA") 
  (:voltageLow 0.4) 
  (:voltageHigh 1) 
  (:cvType "cv") 
  (:electrode 1)))

"label=michaelElectrode=amount=000100=units=uM=substance=DA=voltageLow=0.400000=voltageHigh=000001=cvType=cv=electrode=000001"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That is it!  &lt;/p&gt;

&lt;h2&gt;Extra Features&lt;/h2&gt;

&lt;p&gt;You can use &lt;code&gt;defparser&lt;/code&gt; to create &lt;em&gt;parameterized&lt;/em&gt; parsers too.  That
is, parsers which depend on some input.  Suppose you want a parser
which parses anything numerically equal to a particular value:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defparser (parse-this-number n)
  (parsed-number &amp;lt;- (=number-&amp;gt;number))
  (if (= parsed-number n) (m-return parsed-number)
      parser-fail))

(parse-string (parse-this-number 10) "10") ;-&amp;gt; 10
(parse-string (parse-this-number 10) "100") ;-&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/VincentToups/emacs-utils"&gt;github&lt;/a&gt; link again, if you want to play
with the library.  Documentation is coming, eventually!&lt;/p&gt;

&lt;p&gt;I'm going to do a full rewrite of this library eventually, but it will
be under a new name.  This guy should stabilize eventually.&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-7503270528958878913?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/7503270528958878913/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=7503270528958878913' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7503270528958878913'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7503270528958878913'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/10/update-to-my-monadic-parser-combinator.html' title='Update to my Monadic Parser Combinator Elisp Library (+ examples/tutorial)'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-7469377442837581288</id><published>2011-09-06T16:33:00.000-07:00</published><updated>2011-09-07T07:03:20.418-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='racket'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='syntax'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>Scheme Syntax is A Monad</title><content type='html'>&lt;h3&gt;Two Great Tastes that Taste Great Together&lt;/h3&gt;

&lt;p&gt;Monads are, in a way, all about sequences of operations which depend
on data living "inside" a data structure and produce instances of that
data structure with new data inside.  For instance, the list monad
sequences operations which depend on things &lt;em&gt;inside&lt;/em&gt; a list, and which
produce lists themselves.  The state monad sequences operations which
depend, in a sense, on data living inside a state function and result
in new state functions.  The "maybe" monad sequences operations
depending on values which produce "maybe" values (like &lt;em&gt;(Just x)&lt;/em&gt; or
&lt;em&gt;(Nothing)&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/schrute-syntax.jpg" alt="Schrute" title="" /&gt; 
&lt;/center&gt;
&lt;center&gt;Fact.&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Think about this enough and you start to look for monads whenever you
deal with any kind of data structure.  Recently, I've &lt;a href="http://dorophone.blogspot.com/2011/07/survey-of-syntactic-extension.html"&gt;thought a lot
about&lt;/a&gt; hygienic macros, of which the key component is "syntax", a
kind of decorated representation of source code.  If you write simple
macros that don't do anything like variable capture, you basically
don't have to worry about the distinction between syntax and datum,
but when you start selectively messing up hygiene, it becomes
important.&lt;/p&gt;

&lt;p&gt;And you start to notice patterns.  Specifically, you are often
"unpacking" a piece of syntax, doing something to it, and then
"resyntaxing" it when you are done.  Can we conceptualize syntax as a
monad?  If we do so, do we &lt;em&gt;get&lt;/em&gt; anything?  It is &lt;em&gt;useful&lt;/em&gt;?  I suspect
for many seasoned readers the first is obvious: any data structure
which "holds things" has an associated monad.  We'll see about the
second - I actually don't know myself, and expect to find out as I
write this post.&lt;/p&gt;

&lt;h3&gt;Better Monads&lt;/h3&gt;

&lt;p&gt;We'll be operating in my Scheme of Choice, Racket and we'll use my
monad library &lt;code&gt;better-monads&lt;/code&gt;.  To define a monad in &lt;code&gt;better-monads&lt;/code&gt;
you simply create an instance of the &lt;code&gt;monad&lt;/code&gt; struct, defined as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(struct monad (bind return plus zero) #:transparent)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Plus&lt;/code&gt; and &lt;code&gt;zero&lt;/code&gt; are optional, and you set them to &lt;code&gt;#f&lt;/code&gt; if you can't
provide meaningful values.  That is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require functional/better-monads)

(define the-id-monad
 (monad (lambda (v f) (f v))
        (lambda (v) v)
        #f #f))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates the identity monad.  We can then use it in an &lt;code&gt;mlet*&lt;/code&gt;
expression.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(mlet* in: the-id-monad
   ((x 10)
    (y (+ x 11)))
 (return (+ x y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which will be 31, because the identity monad doesn't do anything at
all.  The library defines and exports some common monads:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(mlet* in: the-list-monad 
  ((x '(a b c))
   (y '(q r s)))
 (return (list x y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Results in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;'((a q) (a r) (a s) (b q) (b r) (b s) (c q) (c r) (c s))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you aren't down with monads, read &lt;a href="http://dorophone.blogspot.com/2011/04/deep-emacs-part-1.html"&gt;my tutorials&lt;/a&gt; on using them in
Lisps.  I'm told they are helpful.&lt;/p&gt;

&lt;h3&gt;The Syntax Monad&lt;/h3&gt;

&lt;p&gt;The easiest place to start with a monad is with &lt;code&gt;return&lt;/code&gt;, since return
is our simplest &lt;em&gt;monadic function&lt;/em&gt;.  That is, there is a family of
functions which accept values and produce monadic values and return is
often one of the simplest of these functions.  What is return in the
syntax monad?&lt;/p&gt;

&lt;p&gt;The super naive but technically correct answer is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-return item)
  (datum-&amp;gt;syntax (syntax _) item))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This takes an item, any kind of datum, and wraps it into a
&lt;code&gt;syntax-object&lt;/code&gt; with &lt;code&gt;datum-&amp;gt;syntax&lt;/code&gt;.  &lt;code&gt;Datum-&amp;gt;syntax&lt;/code&gt; requires,
however, a "seed" syntax object from which to copy the syntactic
information that helps maintain (or violate) hygiene.  This version of
&lt;code&gt;return&lt;/code&gt; just creates a new &lt;code&gt;syntax&lt;/code&gt; object with &lt;code&gt;(syntax _)&lt;/code&gt; as the
seed, but this isn't very useful.  We are almost always trying to
create a syntax object which borrows someone else's scope information,
say to capture a variable.&lt;/p&gt;

&lt;p&gt;If this were Haskell, we might have to live with this sub-optimal
solution (you don't &lt;em&gt;really&lt;/em&gt; need &lt;code&gt;return&lt;/code&gt; as far as I can tell, its
just a handy way to put things into the monad).  However, we're using
Scheme and that means we can take advantage of our greater flexibility
to write this version:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-return item . rest)
 (match rest 
  [(list) (syntax-return item (syntax _))]
  [(list seed)
   (datum-&amp;gt;syntax seed item)]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function takes an optional second argument which can specify the
seed if need be.  &lt;code&gt;better-monads&lt;/code&gt; doesn't care about the fact that
this return has a different signature than other returns.  All that
matters is that everything works out at runtime.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Bind&lt;/code&gt; is often a sticking point but in the syntax monad it is very
easy to understand:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-bind stx stx&amp;lt;datum&amp;gt;)
 (stx&amp;lt;datum&amp;gt; (syntax-&amp;gt;datum stx)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we use a little notational hint I use in my code.  The &lt;code&gt;&amp;lt;datum&amp;gt;&lt;/code&gt;
part of the symbol &lt;code&gt;stx&amp;lt;datum&amp;gt;&lt;/code&gt; indicates that &lt;code&gt;stx&amp;lt;datum&amp;gt;&lt;/code&gt; is a
function waiting for a single value, the &lt;code&gt;datum&lt;/code&gt;, to produce
something, namely a piece of syntax (hence the &lt;code&gt;stx&lt;/code&gt;).  The value
&lt;code&gt;stx&lt;/code&gt; (without any &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;) is a syntax object.  &lt;code&gt;Bind&lt;/code&gt; simply extracts
the value in the syntax using &lt;code&gt;syntax-&amp;gt;datum&lt;/code&gt; and calls the function
&lt;code&gt;stx&amp;lt;datum&amp;gt;&lt;/code&gt; on that value.  The rules of our monad dictate that the
result must be some syntax, but this (again) is enforced at run-time,
not by any type-checker.&lt;/p&gt;

&lt;p&gt;We can define our monad in two ways, depending on the behavior of
&lt;code&gt;return&lt;/code&gt; we want.  The first is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require functional/point-free)

(define (the-syntax-monad&amp;lt;stx&amp;gt; stx)
 (monad syntax-bind (partial&amp;lt; syntax-return stx) #f #f))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This monad is parametrized by the syntax we want to use as the seed
for &lt;code&gt;return&lt;/code&gt; operations.  We accomplish this by partially applying
&lt;code&gt;syntax-return&lt;/code&gt; on the right with the value &lt;code&gt;stx&lt;/code&gt; passed in during the
monad creation.  &lt;code&gt;partial&amp;lt;&lt;/code&gt; means "partially apply on the right" (as
opposed to &lt;code&gt;&amp;gt;partial&lt;/code&gt;, which means "on the left") and lives in my
&lt;code&gt;point-free&lt;/code&gt; library.&lt;/p&gt;

&lt;p&gt;As I understand it, this kind of "value parametrized monad" is difficult to
represent in Haskell.  I've read that the "set monad" is hard to come
up with for just this reason.  In scheme, you just parametrize the
set monad by whatever predicate you want to constitute equality.  &lt;/p&gt;

&lt;p&gt;We can also write a regular old monad:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define the-syntax-monad (monad syntax-bind syntax-return #f #f))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we just expect the user to provide an appropriate seed
syntax-object when they call &lt;code&gt;return&lt;/code&gt;.  I am almost certainly sure you
can't do this in Haskell without jumping through big hoops.  Not
really a Haskell programmer, though, so its possible I'm wrong.&lt;/p&gt;

&lt;h3&gt;But Will it Blend?&lt;/h3&gt;

&lt;p&gt;&lt;center&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://24.media.tumblr.com/tumblr_lmvrirgMr11qaemr9o1_500.jpg" alt="Two" title="" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;/center&gt;
&lt;center&gt;
&lt;a href="http://outwiththenew.tumblr.com/post/6584647368/i-hate-cats-especially-internet-cats-but-this"&gt;This&lt;/a&gt; comes up with you google image search "Two Great Tastes"...
&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Ok, so this is cute.  We've got a monad over &lt;code&gt;syntax&lt;/code&gt; but can we use
it to &lt;em&gt;do&lt;/em&gt; anything?  Well, one application that springs to mind is
that we can use it to defined syntax-oriented versions of standard
list functions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-car stx)
 (mlet* in: (the-syntax-monad&amp;lt;stx&amp;gt; stx)
   ((contents stx))
  (return (car contents))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or, using the more convenient notion of &lt;em&gt;lifting&lt;/em&gt; a function into a monad:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-car stx) 
 ((lift1 car (the-syntax-monad&amp;lt;stx&amp;gt; stx))
  stx))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For functions of two arguments, this looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-cons hd tl)
 ((lift2 cons (the-syntax-monad&amp;lt;stx&amp;gt; tl))
  hd tl))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can use these functions like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(syntax-car (syntax a b c d)) ;-&amp;gt; (syntax a)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(syntax-cons (syntax a) (syntax b c d)) ;-&amp;gt;
  (syntax a b c d)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the second case, though, its important to realize that the syntax
&lt;code&gt;a&lt;/code&gt; has its syntactic metadata replaced with the meta-data for the
&lt;em&gt;tail&lt;/em&gt;, that is &lt;code&gt;(syntax b c d )&lt;/code&gt;.  If we want &lt;code&gt;a&lt;/code&gt; to keep its own
meta-data, we should write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-cons hd tl)
 (mlet* in: the-syntax-monad 
   ((tl-contents tl))
  (return (cons a tl-contents) tl)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We could define this in a point free way (if we wanted to show off),
as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (syntax-cons hd tl) 
 ((lift1 (&amp;gt;partial cons hd)
  (the-syntax-monad&amp;lt;stx&amp;gt; tl))
  tl))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are other various and sundry things which it might be easier to
do/define using the syntax monad, but without using it a great deal,
I'll just say that it &lt;em&gt;seems to blend ok&lt;/em&gt;.  &lt;/p&gt;

&lt;h3&gt;Conclusions&lt;/h3&gt;

&lt;p&gt;&lt;center&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://louderback.com/wp-content/uploads/images/Office-Tip-Two-Great-Tastes-That-Taste-G_9210/image.png" alt="Finally" title="" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;/center&gt;
&lt;center&gt; &lt;a href="http://louderback.com/2011/office-tip-two-great-tastes-that-taste-great-together/"&gt;Finally&lt;/a&gt;, the real deal.&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;We've shown how to define and use a &lt;code&gt;syntax&lt;/code&gt; monad for Scheme syntax
objects.  The essence of the implementation is the realization that
syntax, as a kind of wrapper over regular Scheme data, is enough like
a container that we can write a monad over it, much like we can create
a list monad over lists. &lt;/p&gt;

&lt;p&gt;The utility of the monad seems to be limited by the fact that syntax
containers aren't just dumb boxes around pieces of data, but contain
important meta-data which is intimately related to their intended
use.  It isn't clear to me whether, given this fact, really using the
syntax monad would ever be a win, but who knows?  At the very least it
is an interesting example of what I guess you could call a monad
parametrized over a &lt;em&gt;run time&lt;/em&gt; value, which is an interesting idiom,
anyway.&lt;/p&gt;

&lt;p&gt;All this code is up on my &lt;a href="https://github.com/VincentToups/racket-lib"&gt;git repository&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-7469377442837581288?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/7469377442837581288/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=7469377442837581288' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7469377442837581288'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7469377442837581288'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/09/scheme-syntax-is-monad.html' title='Scheme Syntax is A Monad'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-3542542082674601997</id><published>2011-08-30T16:57:00.001-07:00</published><updated>2011-09-01T06:10:31.293-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='turtle graphics'/><category scheme='http://www.blogger.com/atom/ns#' term='racket'/><category scheme='http://www.blogger.com/atom/ns#' term='screencasts'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='screencast'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><title type='text'>Monadic Turtle Graphics (Screencast Edition)</title><content type='html'>&lt;h2&gt;Apparently the video is still borked for many users.  Anyone here have any advice on the best way to record screencasts on Ubuntu Natty?  A trillion apologies and abasements for any inconvenience.&lt;/h2&gt;


&lt;p&gt;Hi everyone, a few months ago I posted a &lt;a href="http://dorophone.blogspot.com/2011/05/hyperturtle-monad-makes-pretty-pictures.html"&gt;lengthy&lt;/a&gt; article about using monads to simulate state for a turtle graphics system, including a "novel" monad to represent parallel graphics operations.  &lt;/p&gt;

&lt;p&gt;I had the opportunity to give that material as a talk at Racketcon (I used Racket as the demonstration language).  There is a version available of that online someplace, I believe, but I only had 15 minutes there, and its more of a half hour or longer talk.  So I recorded a screencast version, which is now available &lt;a href="https://s3.amazonaws.com/VincentToupsScreencasts/fixed-turtles-monad.ogv"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The talk is in OGV format.  If anyone needs it in AVI, I can probably accommodate, but I figure anyone who cares about monads can probably figure out how to watch an Ogg Video file.&lt;/p&gt;

&lt;p&gt;Oh yeah: all the code (including the slides, which are written in Racket)
is in the racketcon directory at &lt;a href="https://github.com/VincentToups/racket-lib/tree/master/racketcon"&gt;this github repo&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-3542542082674601997?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/3542542082674601997/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=3542542082674601997' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3542542082674601997'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3542542082674601997'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/08/monadic-turtle-graphics-screencast.html' title='Monadic Turtle Graphics (Screencast Edition)'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-1436682287568300142</id><published>2011-08-25T16:50:00.000-07:00</published><updated>2011-08-25T17:23:38.885-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='syntactic extension'/><category scheme='http://www.blogger.com/atom/ns#' term='racket'/><category scheme='http://www.blogger.com/atom/ns#' term='factor'/><category scheme='http://www.blogger.com/atom/ns#' term='picoLisp'/><category scheme='http://www.blogger.com/atom/ns#' term='Common Lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='clojure'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='hygiene'/><category scheme='http://www.blogger.com/atom/ns#' term='meditation'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>A Survey of Syntactic Extension Techniques in the Lisp Family of Languages (Part 3)</title><content type='html'>&lt;p&gt;&lt;a href="http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension.html"&gt;Last time&lt;/a&gt; we talked about the Lisps which use something we might
call &lt;code&gt;defmacro&lt;/code&gt;-style syntactic extensions (&lt;a href="http://en.wikipedia.org/wiki/Common_Lisp"&gt;Common Lisp&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Emacs_Lisp"&gt;Emacs Lisp&lt;/a&gt;,
&lt;a href="http://en.wikipedia.org/wiki/Clojure"&gt;Clojure&lt;/a&gt;, &lt;a href="http://www.paulgraham.com/arc.html"&gt;Arc&lt;/a&gt;, and things we didn't specifically talk about, like
&lt;a href="http://lush.sourceforge.net/"&gt;LuSH&lt;/a&gt;).  The &lt;a href="http://dorophone.blogspot.com/2011/07/survey-of-syntactic-extension.html"&gt;time before that&lt;/a&gt;, we talked about dynamically
scoped interpreted Lisps with &lt;a href="http://en.wikipedia.org/wiki/Fexpr"&gt;fexprs&lt;/a&gt; in which the fact that a
piece of code's value is always dynamically defined was exploited to
let various run-time approaches to staging execution stand in for
macros.  &lt;/p&gt;

&lt;p&gt;Hopefully, throughout the previous posts, I've impressed upon the
reader the importance of &lt;em&gt;scope&lt;/em&gt; in all of these discussions.  The key
reason that picoLisp or newLisp can get away with &lt;code&gt;fexrp&lt;/code&gt;-style macros
is that code literally &lt;em&gt;means what it says&lt;/em&gt; whenever it is you
evaluate it.  This has some things to recommend it, but these days
lexical scope is the biggest rooster in the yard.  Lexical scope makes
reading and writing code easier and prevents certain kinds of bugs,
but it introduces problems with &lt;code&gt;defmacro&lt;/code&gt;-style macros, which
manipulate code without knowing anything about the lexical contexts
associated with variables.  What this principally means is that a
&lt;code&gt;defmacro&lt;/code&gt;-style macro:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Will behave incorrectly if it depends on other macros/special
forms whose definitions are changed between its declaration and
interpretation. &lt;/li&gt;
&lt;li&gt;Can introduce symbol-bindings which unexpectedly shadow symbol
bindings in expanded sub-forms.  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Lisps with &lt;code&gt;defmacro&lt;/code&gt; style macros, &lt;code&gt;1&lt;/code&gt; is kind of dealt with by
having separate namespaces - generally, one just hopes that people
won't often redefine macros and, if they do, they will do so in their
own namespace.  Macros implicitly invoked by a macro expansion will
hopefully or by design expand into package-qualified symbols, which we
trust the user not to clobber without knowing what she is doing.&lt;/p&gt;

&lt;p&gt;Point &lt;code&gt;2&lt;/code&gt; is dealt with by generating symbols during macro expansion
which are guaranteed (under most circumstances) to be unique from any
other symbols which are in use.  This gives macros a means of
expanding into code which references its own private variables without
any concern of clobbering some bindings unexpectedly.  &lt;/p&gt;

&lt;p&gt;If we restrict our attention to regular old Lisp code, without macros,
in a lexical Lisp, we might begin to think that syntactic extensions
are kind of lacking when compared to other kinds of kinds of things
with names.  Of course, we can't pass syntactic extension tokens
around at run time or anything like that but we might want them to
behave in a way a little more like variables.  That is, we want them
to obey reasonable scoping relationships.  In a lexically scoped
language, for instance:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 10)) (lambda (y) (+ x y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;x&lt;/code&gt; inside the lambda will always &lt;em&gt;refer&lt;/em&gt; to the &lt;code&gt;x&lt;/code&gt; bound in the
enclosing &lt;code&gt;let&lt;/code&gt; expression.  If you want to check the value of an
expression that uses that &lt;code&gt;lambda&lt;/code&gt;, you don't have to understand the
whole history of the symbol &lt;code&gt;x&lt;/code&gt; throughout the program.  You just
need to know the value passed in (&lt;code&gt;y&lt;/code&gt;), and then go look at the &lt;em&gt;lexical
context&lt;/em&gt; of the lambda to see what &lt;code&gt;x&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;What if we could invent a macro system where this was true of macros?
Where the &lt;em&gt;meaning&lt;/em&gt; of a macro could be determined by looking at the
inputs at the location of macro expansion, but then only look at the
macro definition, and its lexical context, to understand what the
expansion means?&lt;/p&gt;

&lt;h3&gt;Extending Our Code Representation&lt;/h3&gt;

&lt;p&gt;Most Lisps represent code as a list of atoms and lists.  This is fine
for most things, but we've discussed at length how this representation
makes macro expansion problematic.  Scheme extend their code
representation to include extra information, most interestingly about
the lexical context of a piece of code.  Code, then, is represented as
&lt;em&gt;syntax objects&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;In &lt;a href="http://racket-lang.org/"&gt;Racket&lt;/a&gt;, the Scheme(+) implementation I recommend for fiddling
with Scheme, you can see this explicitly.  The &lt;code&gt;quotation&lt;/code&gt; operator
still produces &lt;em&gt;naked&lt;/em&gt; code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; 'x ;-&amp;gt; x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But the &lt;em&gt;sharp quote&lt;/em&gt; operator, short hand for &lt;code&gt;syntax&lt;/code&gt;, produces
&lt;em&gt;syntax&lt;/em&gt;, which is a data object which contains (abstractly) lists of
atoms and lists, but &lt;em&gt;decorated&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; #'x -&amp;gt; #&amp;lt;syntax:11:10 x&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Note that &lt;code&gt;#'&lt;/code&gt; used in this way is specific to Racket).&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/syntax.png" alt="Syntax" title="" /&gt;
&lt;/center&gt;
&lt;center&gt;
Syntax, unlike the raw code representation of other Lisps, contains
information about the lexical context and binding of symbols.
&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;The Racket printer shows a little of the information associated with
the syntax (line number, it appears), but lots of other information is
stored.  Racket macros transform and produce &lt;em&gt;syntax&lt;/em&gt;, not raw lists.
Except for this fact, they are the same as &lt;code&gt;defmacro&lt;/code&gt; style macros.
You may have heard people say that you can't write Scheme macros in
Scheme (as you write Lisp macros in Lisp), but this isn't really
true.  Syntax transformations have to produce and consume syntax, but
otherwise are unrestricted.  It is true that Schemes vary in the
degree to which you are provided tools to arbitrarily manipulate
syntax,  but that is at least partly a separate issue.&lt;/p&gt;

&lt;h3&gt;Introducing New Syntax&lt;/h3&gt;

&lt;p&gt;Scheme provides an operation, &lt;code&gt;define-syntax&lt;/code&gt;, which informs the
system that a symbol should be associated with a new syntax
transformer.  This is the equivalent of &lt;code&gt;defmacro&lt;/code&gt; in other Lisps.  It
has the form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-syntax &amp;lt;symbol&amp;gt; &amp;lt;syntax-transformer-expression&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first symbol is equivalent to the name of the macro in &lt;code&gt;defmacro&lt;/code&gt;
lisps.  The second expression must be a piece of Scheme code that
generates a syntax-transformer which is a regular function (as we
shall see) that must take and return syntax&lt;sup&gt;1&lt;/sup&gt;.  After executing a
&lt;code&gt;define-syntax&lt;/code&gt;, if the &lt;code&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; is detected during macro-expansion,
its contents are passed to the syntax transformer, and the results are
stitched back into the code.&lt;/p&gt;

&lt;h3&gt;Simple Tools Help Maintain Hygiene&lt;/h3&gt;

&lt;p&gt;So how do we write new syntax-transformers?  In Lisp, we just use the
list manipulation functions, quasiquote and unquote, to take apart and
build up our new source code.  Schemes provide equivalent procedures
for syntax objects, but most macros are simple enough to be written in
a high-level form which helps maintain hygiene automatically.   There
are many ways to do this, the simplest of which is &lt;code&gt;syntax-rules&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First in words: &lt;code&gt;syntax-rules&lt;/code&gt; is a form which takes a series of
&lt;em&gt;patterns&lt;/em&gt; and &lt;em&gt;expansions&lt;/em&gt; and produces a syntax-transformer
which examines syntax until a pattern matches and returns the
associated expansions.  Critically, when a pattern is matched, the
parts of the pattern are bound to pieces of syntax which can be used
in the expansion.  Let's do &lt;code&gt;let*&lt;/code&gt;, like we did &lt;a href="http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension.html"&gt;last time&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-syntax my-let*
 (syntax-rules 
  [(my-let* () body ...)
   (begin body ...)]
  [(my-let* ((id expr)) body ...)
   ((lambda (id) body ...) expr)]
  [(my-let* ((id0 expr0) (id expr) ...) body ...)
   ((lambda (id0) (my-let* ((id expr) ...) body ...)) 
    expr0)]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I think people find &lt;code&gt;syntax-rules&lt;/code&gt; confusing, when coming from
&lt;code&gt;defmacro&lt;/code&gt;, because it handles a lot of things for you.  For one
thing, the fact that this expression is a syntax transformer isn't as
obvious as it could be, but think of &lt;code&gt;syntax-rules&lt;/code&gt; as an expression
which returns a function, which is exactly what it is.  That isn't so
odd!&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;syntax-rules&lt;/code&gt; also handles pattern matching for you and binding
the results of that matching to pieces of syntax in the expansion
expression.  On top of that, neither the pattern nor the expansion
constitutes regular Scheme code.  Each is, in fact, its own unique,
but related, domain specific pattern language, a bit like a regular
expression match and replacement language for syntax.&lt;/p&gt;

&lt;p&gt;Let's look at each specific pattern and its expansion.  The first
pattern handles the case where someone invokes a &lt;code&gt;my-let*&lt;/code&gt; binding
expression without any bindings.  It matches a piece of code which
looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(my-let* () body ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is a piece of that domain specific pattern language in this
code.  The expression &lt;code&gt;body ...&lt;/code&gt; tells syntax-rules to match
zero-or-more forms of arbitrary structure and bind them to the pattern
&lt;code&gt;body ...&lt;/code&gt; in the expansion part of the clause.  Since we aren't doing
anything with the &lt;code&gt;body&lt;/code&gt; we don't care if it is there or not, since we
handle it the same way.  We expand this clause into:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(begin body ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here &lt;code&gt;body ...&lt;/code&gt; &lt;em&gt;now&lt;/em&gt; refers to a piece of bound syntax produced by the
pattern part of the expression.  When the expansion is expanded, it
will contain whatever was matched to &lt;code&gt;body ...&lt;/code&gt; in the pattern, which
might be nothing.&lt;/p&gt;

&lt;p&gt;The next clause's pattern is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(my-let* ((id expr)) body ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we instruct the pattern language to only match when the binding
part of the &lt;code&gt;let&lt;/code&gt; contains one two-element list which should be an
identifier and an expression (we will have to think about error
checking later).  The identifier is bound to &lt;code&gt;id&lt;/code&gt;, the expressions is
bound to &lt;code&gt;expr&lt;/code&gt;.  &lt;code&gt;body ...&lt;/code&gt; is bound as above.  The expansion is
given as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;((lambda (id) body ...) expr)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;body ...&lt;/code&gt;, and &lt;code&gt;expr&lt;/code&gt; were bound in the pattern, they
have special meaning in the expansion.  The symbol &lt;code&gt;lambda&lt;/code&gt;, however,
is a piece of syntax which is corresponds to the currently operating
&lt;code&gt;lambda&lt;/code&gt; syntax.  Note that &lt;code&gt;syntax-rules&lt;/code&gt; is basically handling
automatically what we would have done manually with &lt;code&gt;gensym&lt;/code&gt; and
quasiquotation/unquotation. &lt;/p&gt;

&lt;p&gt;At the risk of being tedious, lets look at the final pattern.  It
doesn't contain anything new, but it does highlight some of the
strategies often encountered in writing this kind of syntax
transformer.  The pattern is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(my-let* ((id0 expr0) (id expr) ...)
         body ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here, &lt;code&gt;var0&lt;/code&gt; and &lt;code&gt;expr0&lt;/code&gt; are bound to the first id/expr pair.  We
write out the first explicitely for two reasons.  The first is that
&lt;code&gt;(id expr) ...&lt;/code&gt; matches &lt;em&gt;zero&lt;/em&gt; or more expressions, and we want at
least one.  The second is that we need to split up the &lt;code&gt;id&lt;/code&gt; and the
&lt;code&gt;expr&lt;/code&gt; part of our match and insert the &lt;code&gt;...&lt;/code&gt; rest of them somewhere
else.  You can't do this with the &lt;code&gt;pattern ...&lt;/code&gt; match, which must
appear in the expansion as &lt;code&gt;pattern ...&lt;/code&gt;.  We could write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(my-let* ((id0 expr0) binder ...)
    body ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;of course, but by specifying a more concrete match we make our macro a
little bit better in terms of robustness.  We really want &lt;em&gt;at least&lt;/em&gt; a
pair in each binder position, so we say so directly.&lt;/p&gt;

&lt;p&gt;We expand this pattern as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;((lambda (id0) (my-let* ((id expr) ...) body ...)) expr0)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which should be understandable enough.  The main thing to note is that
we are recursively expanding this syntax-transformation with calls to
itself.  Because syntax-transformations often represent purely
functional computations on input syntax, recursion is a common (and
elegant) strategy.  &lt;/p&gt;

&lt;p&gt;The Scheme universe offers other forms that allow more complex macro
expansions, but they all owe a lot to the basic shape of
&lt;code&gt;syntax-rules&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Shortcomings of Syntax Rules&lt;/h3&gt;

&lt;p&gt;The standard way of demonstrating the shortcomings of &lt;code&gt;syntax-rules&lt;/code&gt;
is by trying to write an anaphoric &lt;code&gt;if&lt;/code&gt; macro.  Anaphoric &lt;code&gt;if&lt;/code&gt; is a
macro that binds the result of its &lt;code&gt;predicate&lt;/code&gt; part to a variable,
which is available for use inside either branch expression (often the
symbol &lt;code&gt;it&lt;/code&gt; is used for this purpose).  Since you've read &lt;a href="http://dorophone.blogspot.com/2011/07/survey-of-syntactic-extension.html"&gt;Part 1&lt;/a&gt;
and &lt;a href="http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension.html"&gt;Part 2&lt;/a&gt;, I can just explain this by writing the appropriate
macro in Common Lisp:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro anaphoric-if (pred true &amp;amp;optional false)
 `(let ((it ,pred))
    (if ,it ,true ,false)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simple!  The utility of the macro is related to the fact that &lt;code&gt;if&lt;/code&gt;
treats lots of things other than just booleans as &lt;em&gt;true&lt;/em&gt;.  Imagine you
have a function which processes a command entered by the user.  The
command is returned by the function &lt;code&gt;get-command&lt;/code&gt;.  If the user just
pressed enter, &lt;code&gt;nil&lt;/code&gt; is returned.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(anaphoric-if (get-command) (handle-command it) nil)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;it&lt;/code&gt; contains the result of &lt;code&gt;get-command&lt;/code&gt;.  Our macro has &lt;code&gt;captured&lt;/code&gt;
the symbol &lt;code&gt;it&lt;/code&gt; and is using it for its own purposes.  If we already
had an &lt;code&gt;it&lt;/code&gt; in scope, this might be inconvenient (and, in my opinion,
there are &lt;a href="http://existentialtype.wordpress.com/2011/03/15/boolean-blindness/"&gt;lots&lt;/a&gt; of &lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#Maybe_monad"&gt;things&lt;/a&gt; wrong with this
kind of macro, but some people are into it).  Misgivings about
variable capture aside, it is easy to write this variable capturing
macro in Common Lisp, and it behaves just as we expect it to with our
filthy, untutored brains.&lt;/p&gt;

&lt;p&gt;We might try to write this macro using our new knowledge of
&lt;code&gt;syntax-rules&lt;/code&gt; like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-syntax anaphoric-if
 (syntax-rules ()
  [(anaphoric-if pred true false)
   (let ((it pred))
     (if it true false))]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But if we attempt to use this syntax-transformer, we'll quickly run
into problems:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(anaphoric-if 10 (format "it is ~a~n" it) #f)
  . . reference to an identifier before its definition: it
  &amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We expected the string "It is 10\n", but we got an error!  What is
happening is that &lt;code&gt;syntax-rules&lt;/code&gt; is trying to protect us from
ourselves.  Inside the expansion we introduce a piece of syntax, &lt;code&gt;it&lt;/code&gt;.
It isn't, however, simply a symbol - it is syntax, and its metadata
tells the Scheme system that its scope is limited to the macro
expansion context.  We can use it as the predicate spot in our &lt;code&gt;if&lt;/code&gt;
statement, but any &lt;code&gt;it&lt;/code&gt; symbols appearing in the expressions matched
to &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; will only be &lt;em&gt;superficially&lt;/em&gt; similar.  The Scheme
system will know that the &lt;code&gt;it&lt;/code&gt;s in those expressions are &lt;em&gt;different&lt;/em&gt;
than the one we create in our macro expansion.  Hence, when the
evaluator encounters &lt;code&gt;it&lt;/code&gt; in one of our branches, it checks that
symbol's original lexical context (in our example, the top-level
binding environment) for a binding, which it doesn't find.  Hence the
error.&lt;/p&gt;

&lt;p&gt;We can approach the solution in two ways (at least).  The first is to
force the user to provide the symbol name herself.  Scheme will then
know that the symbol, whatever it is, is meant to be the same for all
appearances in the expansion:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-syntax named-anaphoric-if 
 (syntax-rules () 
  [(named-anaphoric-if id pred true false)
   (let ((id pred))
     (if id true false))]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or we can try to &lt;em&gt;tell scheme we want to capture the symbol &lt;code&gt;it&lt;/code&gt;&lt;/em&gt;.  I
don't know of any way to do this with &lt;code&gt;syntax-rules&lt;/code&gt;, although I
suspect that if you really wanted to bend over backwards, there might
be some way.  We need something else!  Plus, there are lots of other
idioms, not just the ugly duckling variable capture, which are hard to
express in &lt;code&gt;syntax-rules&lt;/code&gt;.  Hence, Schemers cooked up...&lt;/p&gt;

&lt;h3&gt;Syntax-case&lt;/h3&gt;

&lt;p&gt;Before we talk about &lt;code&gt;syntax-case&lt;/code&gt; I want to emphasize that both
&lt;code&gt;syntax-rules&lt;/code&gt; and &lt;code&gt;syntax-case&lt;/code&gt; are just tools that produce
syntax-transformers.  Their unusual, domain specific languages are
meant to make writing transformers correctly easier, but they are not,
themselves, the Scheme macro system.  They are just tools built on top
of that system.  The underlying system is much like the one in
&lt;code&gt;defmacro&lt;/code&gt; Lisps, except it transforms syntax, not lists.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;syntax-case&lt;/code&gt; looks a lot like &lt;code&gt;syntax-rules&lt;/code&gt;.  Here is how you'd
write the &lt;code&gt;named-anaphoric-if&lt;/code&gt; in &lt;code&gt;syntax-case&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-syntax named-anaphoric-if* 
  (lambda (stx) 
    (syntax-case stx ()
      [(named-anaphoric-if* id pred true false)
       (syntax (let ((id pred))
                 (if id true false)))])))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Almost&lt;/em&gt; identical.  Sharp eyed readers will not that the expansion
has been wrapped with a &lt;code&gt;syntax&lt;/code&gt; expression, which tells the
environment to evaluate the contents contained therein as syntax,
which means automatically handling any syntax bound by the pattern
match.  We also have to wrap &lt;code&gt;syntax-case&lt;/code&gt; in a lambda, which is the
syntax-transformer proper.  Unlike &lt;code&gt;syntax-rules&lt;/code&gt;, which is a form
resulting in a syntax-transformer, &lt;code&gt;syntax-case&lt;/code&gt; merely transforms
syntax.  It does not produce a transformer itself.  &lt;/p&gt;

&lt;p&gt;However, since the &lt;code&gt;expansion&lt;/code&gt; part of &lt;code&gt;syntax-case&lt;/code&gt; clauses is not
automatically interpreted as syntax, it allows us to finagle the
matched syntax manually.  Turns out this is just what we need to
"capture" a variable.  Here is how we do it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-syntax anaphoric-if** 
 (lambda (stx)
  (syntax-case stx ()
   [(anaphoric-if** pred true false)
    (with-syntax ([it-id (datum-&amp;gt;syntax (syntax true) 'it)])
     (syntax 
      (let ((it-id pred))
       (if it-id true false))))])))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So this is a &lt;code&gt;syntax-case&lt;/code&gt; expression with one clause, which matches
things like &lt;code&gt;(anaphoric-if** a b c)&lt;/code&gt;.  The next part of the clause
must return a piece of syntax, but it can do any arbitrary scheme
activity to produce that syntax, unlike &lt;code&gt;syntax-rules&lt;/code&gt;.  The
expression &lt;code&gt;with-syntax&lt;/code&gt; allows us to introduce an environment with
new syntax-bindings in addition to those produced by the match
expression.  We use it to introduce a binding to &lt;code&gt;it-id&lt;/code&gt;.  This means
we can use the expression &lt;code&gt;it-id&lt;/code&gt; inside our expansion (within the
&lt;code&gt;with-syntax&lt;/code&gt; body) and it will be interpreted correctly by &lt;code&gt;syntax&lt;/code&gt;
expressions.  How do we actually create a new piece of syntax, though?
The function &lt;code&gt;datum-&amp;gt;syntax&lt;/code&gt; takes a piece of data (in our case, the
symbol &lt;code&gt;it&lt;/code&gt;) and transforms it into a piece of syntax.  But syntax
contains &lt;em&gt;much more&lt;/em&gt; information than just its value, and some of that
information determines how it is interpreted during macroexpansion and
subsequent compilation.  &lt;code&gt;datum-&amp;gt;syntax&lt;/code&gt; allows us to pass in, as the
first argument, a piece of syntax from which we will &lt;em&gt;clone&lt;/em&gt; that
additional information.  Since we want to let &lt;code&gt;it&lt;/code&gt; operate in the same
environment as the &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; clauses, we clone the
syntax-metadata from the &lt;code&gt;true&lt;/code&gt; branch.  We then return a piece of
&lt;code&gt;syntax&lt;/code&gt; corresponding to our entire expression.  Again, &lt;code&gt;syntax&lt;/code&gt;
automatically handles the equivalent of quasi/un-quotation as we might
do it in Lisp.  It looks at the syntax bindings which are active and
automatically interpolates them.  We can now say, with a straight
face:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(anaphoric-if** 10 it it)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And get &lt;code&gt;10&lt;/code&gt;, as we expect to.  We now know how to write
selectively-non-hygienic macros.  Go us.&lt;/p&gt;

&lt;h3&gt;Recapitulation&lt;/h3&gt;

&lt;p&gt;We started, back in &lt;a href="http://dorophone.blogspot.com/2011/07/survey-of-syntactic-extension.html"&gt;Part 1&lt;/a&gt; by talking about dynamically scoped
Lisps where code was always written and executed in the most obvious
way possible.  These Lisps maybe have been difficult to write programs
in, or at least unfamiliar to modern Lispers and programmers at large,
but they did have one thing going for them: their treatment of
code/data was so simple that special forms could be treated
identically with functions.  These so-called &lt;code&gt;fexprs&lt;/code&gt; could choose
when to evaluate their inputs because they put the onus of maintaining
various bindings to a single symbol on the user.&lt;/p&gt;

&lt;p&gt;In these languages, macros were often considered to be less useful
than &lt;code&gt;fexprs&lt;/code&gt;, and why not?  We could see when we looked at the
&lt;code&gt;defmacro&lt;/code&gt; Lisps shared some problems which, if not really a problem
in practical use cases, at least left us philosophically unsettled.
Macros could easily be written which unintentionally captured
variables, they were not first class, and didn't follow regular
lexical binding rules.  In exchange for these shortcomings, we did get
macros which were almost as nice as &lt;code&gt;fexprs&lt;/code&gt; and worked better in
lexically scoped languages, because they forced us to emphasize the
fact that macro-expansion occurs &lt;em&gt;before&lt;/em&gt; execution.  Tools like
&lt;code&gt;gensym&lt;/code&gt; and packages/namespaces helped us avoid the worst hygiene
problems.&lt;/p&gt;

&lt;p&gt;And so we've finally arrived at Scheme, were macros, now called
&lt;code&gt;syntax-transformers&lt;/code&gt; work not on an undecorated representation of
Lisp code, as they do in &lt;code&gt;defmacro&lt;/code&gt; lisps, but on &lt;em&gt;syntax&lt;/em&gt;, a code
representation where information about context and binding is
represented as well.  Adding this additional representation to the
code allows forms like &lt;code&gt;syntax-rules&lt;/code&gt; to automatically handle syntax
transformation for us while maintaining hygiene.  &lt;/p&gt;

&lt;p&gt;Finally, we grudgingly admitted that people somewhere might want to
intentionally violate hygiene, and introduced the &lt;code&gt;syntax-case&lt;/code&gt;
expression, which allows the user to manipulate syntax-objects to the
end of capturing variables and the like.&lt;/p&gt;

&lt;h3&gt;Further Reading&lt;/h3&gt;

&lt;p&gt;There are &lt;em&gt;tons&lt;/em&gt; of things still to be said about the subject,
especially regarding error checking and reporting.  And the small, but
enthusiastic communities around newLisp and picoLisp as well as the
thriving Emacs Lisp community demonstrates that despite academic
discussion, even issues of lexical or dynamic scope is far from
resolved.  &lt;/p&gt;

&lt;p&gt;Further reading might entail getting one's head around
&lt;a href="http://download.plt-scheme.org/doc/html/syntax/stxparse.html"&gt;syntax-parse&lt;/a&gt; a special form implemented in Racket and (partially)
in Clojure, which is meant to make writing hygienic (and selectively
unhygienic) macros both easy to write and easy to make provide useful,
informative error messages.  You could also check out &lt;a href="http://web.cs.wpi.edu/~jshutt/kernel.html"&gt;Kernel&lt;/a&gt;,
which is an experimental Lisp which tries to address all of these
issues with tons of dollar signs and abstraction.&lt;/p&gt;

&lt;p&gt;As always, I hope this has been useful to someone other than myself!&lt;/p&gt;

&lt;h3&gt;Back Matter&lt;/h3&gt;

&lt;p&gt;If all you care about is Lisp, then we've reached the end of this
series on syntax.  However, discussions with a reader, Dru Nelson,
compel me to mention &lt;a href="http://factorcode.org/"&gt;The Factor Language&lt;/a&gt; as something of a
conceptual digestif.  Factor is a concatenative language which has
many similarities with Lisp, which puts it in the odd category of
being &lt;em&gt;more Lispy than Lisp&lt;/em&gt; in many respects.  &lt;/p&gt;

&lt;p&gt;Factor, for programmers unfamiliar with concatenative languages, in
its ordinary operation, does not have variables: values are passed
between subroutines (called "words") on a data stack.  I strongly
encourage readers to download Factor and give it a spin - I find
writing Factor programs to be delightful mental calisthenics.  Here is
how you calculate (3 + 13) * 6 in Factor:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;( scratchpad ) 3 13 + 6 *

--- Data stack:
96
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A Factor program is, apparently, a flat list of "words" with data flow
handled implicitly.  What is the equivalent of a "lambda" expression
in such an exotic language?  Well, its just a list!  Since there are
no named things except for words, a list of the words (and literals,
which can be thought of as words which "push" themselves onto the
stack) which make up a subroutine is all you need or can have.  In
Factor, &lt;em&gt;code really is data&lt;/em&gt; and execution of that code is &lt;em&gt;even
simpler&lt;/em&gt; than it is in dynamically scoped languages.  You can't even
make the common mistakes of dynamical languages respecting variable
binding because, well, &lt;em&gt;you don't have variables&lt;/em&gt;.  You don't need to
write &lt;em&gt;Macros&lt;/em&gt;, such as we have them in Lisp, in Factor, because your
functions are free to shuffle around, decompose, build and partially
evaluate quotations to their heart's content&lt;sup&gt;2&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Factor goes one step further towards simplicity than even &lt;a href="http://picolisp.com/5000/!wiki?home"&gt;picoLisp&lt;/a&gt;
- it completely eliminates variables, and as such, data becomes even
closer to code than it is in that language.  &lt;/p&gt;

&lt;p&gt;More Lispy than Lisp indeed!&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; I'm pretty sure that syntax, as (essentially) a wrapper
for data, is a &lt;a href="http://dorophone.blogspot.com/2011/04/deep-emacs-part-1.html"&gt;monad&lt;/a&gt;, but that is definitely another story.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;2&lt;/sup&gt; Factor actually requires that quotations have infer-able
stack-effects, which places limitations on them in practice
(surprisingly few, though!)&lt;/p&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-1436682287568300142?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/1436682287568300142/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=1436682287568300142' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1436682287568300142'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1436682287568300142'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension_25.html' title='A Survey of Syntactic Extension Techniques in the Lisp Family of Languages (Part 3)'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-3343890281486824786</id><published>2011-08-12T17:26:00.000-07:00</published><updated>2011-08-13T07:54:57.652-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='extended metaphor'/><category scheme='http://www.blogger.com/atom/ns#' term='sonnet'/><category scheme='http://www.blogger.com/atom/ns#' term='poetry'/><title type='text'>On Discovering Programming, Age 12</title><content type='html'>I discovered at once a parallel&lt;br&gt;
eroticism.  New and strange, unknown,&lt;br&gt;
fumbling, my mind hardly able to tell&lt;br&gt;
what subtle provocations meant, shown&lt;br&gt;

in stipled fading purple, inkjet blown,&lt;br&gt;
on pages already aged when I first&lt;br&gt;
touched them and, virginal, brought them home.&lt;br&gt;
Like the child I was I struggled to birth&lt;br&gt;

new sensuous faculties, learned to perch&lt;br&gt;
full, on the edge of abstract orgasms.&lt;br&gt;
To caress the invisible shapes, to lurch&lt;br&gt;
towards clasping ideas in ersatz spasms.&lt;br&gt;

&amp;nbsp I still remember my naive eros,&lt;br&gt;
&amp;nbsp from which a lifetime of pleasures arose.&lt;br&gt;

  
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-3343890281486824786?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/3343890281486824786/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=3343890281486824786' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3343890281486824786'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3343890281486824786'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/08/on-discovering-programming-age-12.html' title='On Discovering Programming, Age 12'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-5787252630388237700</id><published>2011-08-11T08:44:00.000-07:00</published><updated>2011-08-31T13:03:04.076-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='picoLisp'/><category scheme='http://www.blogger.com/atom/ns#' term='Common Lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='newLisp'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='clojure'/><category scheme='http://www.blogger.com/atom/ns#' term='metaprogramming'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='hygiene'/><category scheme='http://www.blogger.com/atom/ns#' term='quotation'/><category scheme='http://www.blogger.com/atom/ns#' term='syntax extension'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>A Survey of Syntactic Extension Techniques in the Lisp Family of Languages (Part 2)</title><content type='html'>&lt;p&gt;&lt;a href="http://dorophone.blogspot.com/2011/07/survey-of-syntactic-extension.html"&gt;Last time&lt;/a&gt; we began our survey of syntactic extension
technqiues in Lisp with a rather airy meditation on syntax, quotation,
evaluation and lambda, all by way of &lt;a href="http://picolisp.com/5000/!wiki?home"&gt;picoLisp's&lt;/a&gt; unusual dynamic,
interpreter oriented semantics.  Very briefly, we looked at the way
picoLisp lets regular, first class, functions specify that certain
arguments should be passed in unevaluated, which the function can then
selectively evaluate.  Because the language is dynamically scoped and
interpreted, this gets you as far as macros, and a little farther
(since even "special forms" are first class).  &lt;/p&gt;

&lt;p&gt;This time, we're going to attempt to be a little more direct in our
survey.  But we should still think a little bit about macros before we
jump in.  In the Lisps we'll talk about today, syntactic extension is
handled via &lt;em&gt;macros&lt;/em&gt;.  Given the flexibility of the picoLisp approach
(also known as &lt;a href="http://en.wikipedia.org/wiki/Fexpr"&gt;fexpr&lt;/a&gt;'s, and also supported by
&lt;a href="http://www.newlisp.org/downloads/newlisp_manual.html"&gt;newLisp&lt;/a&gt;, which is itself an exotic species I might write
about later), you might wonder why we'd want to use macros.  The
principal obvious disadvantage of macros is that they are not
&lt;a href="http://en.wikipedia.org/wiki/First-class_function"&gt;first class&lt;/a&gt; objects, they can't be stored in variables or passed
around, whereas picoLisp style functions/special forms are.  There are
lots of reasons they were abandoned, but one easy one to see is that
macros make separating different stages of program interpretation and
execution a bit easier.  &lt;/p&gt;

&lt;p&gt;Consider executing an &lt;code&gt;fexpr&lt;/code&gt;.  In order to correctly calculate the
value of such a first class function, we need to remember not just the
flow of values from one function to another, we also have to know the
&lt;em&gt;syntax&lt;/em&gt; of all the inputs.  Much of the efficiency of compilation is
in the ability to disregard this information, and reduce the program
to a series of simple, primitive, operations.  If our language is
lexically scoped, then, in addition to remembering the syntax of the
inputs, we also need to know the &lt;em&gt;context&lt;/em&gt; of that syntax in order to
correctly evaluate it.  I'm surely oversimplifying things here, but
you can read about the problems with &lt;code&gt;fexpr&lt;/code&gt;'s &lt;a href="http://www.nhplace.com/kent/Papers/Special-Forms.html"&gt;here&lt;/a&gt;
(the commentary on this subject at &lt;a href="http://lambda-the-ultimate.org/node/3640"&gt;Lambda the Ultimate&lt;/a&gt; is also
great)&lt;sup&gt;1&lt;/sup&gt;.  Kazimir Majorinc, by the way, has written a
&lt;a href="http://kazimirmajorinc.blogspot.com/2010/10/on-pitmans-special-forms-in-lisp.html"&gt;rebuttal&lt;/a&gt; of Pitman's original concerns.  I have to admit, I find
newLisp in particular to be a fascinating case study in alternative
approaches to Lisp and computer programming languages in general.
More people should try it out!&lt;/p&gt;

&lt;p&gt;If we stage our syntactic extensions, as in more modern/typical Lisps,
after program reading, but before execution, it simplifies the design
of our compiler/virtual machine/interpreter.  The macro systems we'll
be discussing today operate in this way, transforming an intermediate
form of Lisp code, produced by the reader, before finally handing the
result to the compiler.  The upside is the compiler need only
concern itself with the behavior of basic lisp and its small set of
special forms.  The downside is that macros are stuck working only on
source code: they cannot, in general, know anything about runtime, as
the code has not yet been run!&lt;/p&gt;

&lt;p&gt;&lt;img src="http://dl.dropbox.com/u/1076954/syntax-extension.png" alt="Transformation" title="" /&gt;&lt;/p&gt;

&lt;p&gt;The Arc Documentation has something rather insightful
to say about this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Macros live in the land of the names, not the land of the things
  they refer to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, today we'll be looking at macro systems by which the user declares
that certain symbols indicate to the intermediate processing step of
the language that some portion of code should be transformed by a
specified piece of code and the result inserted back into the code
representation where the macro appeared.&lt;/p&gt;

&lt;h2&gt;Business Time!&lt;/h2&gt;

&lt;p&gt;Macro syntax can be a bit confusing when you see it for the first
time.  It is useful, then, to consider what a macro might &lt;em&gt;do&lt;/em&gt; rather
than &lt;em&gt;how&lt;/em&gt; it is written, first.  Macros should almost always
transform code into code, with no side effects along the way, and so
we can understand a macro by writing the code before and then the code
after macro expansion.&lt;/p&gt;

&lt;p&gt;If you know some Lisp, you know that variables are introduced with
&lt;code&gt;let&lt;/code&gt; epxressions.  Each binding expression in a &lt;code&gt;let&lt;/code&gt; is executed
without any of the other bindings in scope, so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 10)
      (y (+ x 1)))
    (+ y x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Will produce an error, because &lt;code&gt;x&lt;/code&gt; is not visible until the body
portion of the &lt;code&gt;let&lt;/code&gt; expression.  If you want to nest references to
new variables, you need &lt;code&gt;let*&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let* ((x 10)
       (y (+ x 1)))
   (+ y x)) ; -&amp;gt; 21
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which does work.  Let's write a &lt;code&gt;let*&lt;/code&gt; macro as an example.
Expressions like &lt;code&gt;let*&lt;/code&gt; are all about [binding][], and that means we
can use &lt;code&gt;lambda&lt;/code&gt;s to express &lt;code&gt;let*&lt;/code&gt;.  We want:&lt;/p&gt;

&lt;p&gt;A:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(my-let* ((x 10)
          (y (+ x 1)))
      (+ y x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To expand to:&lt;/p&gt;

&lt;p&gt;B:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(funcall (lambda (x) 
   (funcall (lambda (y) (+ y x)) (+ x 1)))
   10)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Take a second to think about it, if you don't see why.  The basic
insight is that &lt;code&gt;lambda&lt;/code&gt; can be used to introduce a context where a
variable is bound, and funcall binds that variable.&lt;/p&gt;

&lt;p&gt;The transformation of some code A to some other code B is the action
of all but a few exotic macros.  This transformation happens,
conceptually, &lt;em&gt;before any code is run&lt;/em&gt; (in practice, because many
Lisps favor interactive development, macro expansion might occur after
the user has executed some code, but it is highly unusual, and
probably an error, for the macro expansion itself to depend on the
results of previous execution (except that the macro may use
previously defined functions to effect the code transformation)).  &lt;/p&gt;

&lt;p&gt;Let's write the macro, this time in Emacs Lisp:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro my-let* (bindings &amp;amp;rest body)
  (cond 
    ((empty? bindings) (cons 'progn body))
    (t
     (let ((pair (car bindings))
           (leftover-bindings (cdr bindings)))
      `(funcall 
         (lambda (,(car pair))
           (my-let* ,leftover-bindings ,@body))
         ,(cadr pair))))))

(my-let* ((x 10)
          (y (+ x 1)))
     (+ y x)) ;-&amp;gt; 21
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We need to say a bit about quasi-quotation to really understand how
this works, but there are some things we can explain right away.
Emacs Lisp represents code as a list of symbols, other atoms, and
lists.  When &lt;code&gt;my-let*&lt;/code&gt; is encountered, the source code after &lt;code&gt;my-let*&lt;/code&gt;
in the code is passed, as a list, into a function (implicitly defined
by the &lt;code&gt;defmacro&lt;/code&gt; statement.  This list is destructured by this
function using regular old argument specification (so that the first
item in the list is bound to &lt;code&gt;bindings&lt;/code&gt; and all the subsequent items
are bound to &lt;code&gt;body&lt;/code&gt;, in a list) and then the body of the &lt;code&gt;defmacro&lt;/code&gt; is
&lt;em&gt;executed&lt;/em&gt;.  It's result &lt;em&gt;must be&lt;/em&gt; a piece of code, which is inserted
wherever the macro appeared in the source code representation.  &lt;/p&gt;

&lt;p&gt;Let's focus on the first possibility, when the macro is called with an
empty set of binding forms.  Our code is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(my-let* () "Hey there.")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is slurped up into the Lisp interpreter as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(list 'my-let* '() "Hey there.")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The interpreter sees &lt;code&gt;my-let*&lt;/code&gt; and knows that this indicates a macro.
To expand this macro, it calls a function based on the &lt;code&gt;defmacro&lt;/code&gt;
expression.  This function doesn't have a name, but we can write what
it would look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun -my-let*-expander- (binders &amp;amp;rest body)
   (cond 
    ((empty? bindings) (cons 'progn body))
    (t
     (let ((pair (car bindings))
           (leftover-bindings (cdr bindings)))
      `(funcall 
         (lambda (,(car pair))
           (my-let* ,leftover-bindings ,@body))
         ,(cadr pair))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The macro expanding part of the lisp system calls this function with
the following arguments:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(-my-let*-expander- '() "Hey There")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function returns: &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;'(progn "Hey There")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the macro expanding part of the lisp system inserts that
expression, whole hog, so to speak, into the place where the original
form starting with &lt;code&gt;my-let*&lt;/code&gt; was located.  It then moves on.  Once all
the macros are expanded, the code is passed to the
compiler/interpreter/etc and then executed.  Viola!&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/viola.jpg" alt="Viola!" title="" /&gt;
&lt;/center&gt;
&lt;center&gt;Voila!&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;To understand the other branches, we need to cover a feature which
appears in most lisps: quasiquotation.&lt;/p&gt;

&lt;h2&gt;Macros &amp;amp; Quasiquotation &lt;/h2&gt;

&lt;p&gt;Lisps, more or less, represent code internally as lists or/of atoms
(things like numbers, strings, symbols).  The code &lt;code&gt;x&lt;/code&gt; represents,
more or less, a piece of code that evalutes to the value of the symbol
&lt;code&gt;x&lt;/code&gt;.  The list &lt;code&gt;(+ x 1)&lt;/code&gt; is the piece of code that evaluates to the
value of symbol &lt;code&gt;x&lt;/code&gt; plus one.  We covered last time how to enter these
unevaluated pieces of code into a running lisp session using
quotation.  The quote operator tells a Lisp not to evaluate whatever
it is applied to.  So:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq x 10)
x  ;-&amp;gt; 10
'x ;-&amp;gt; x
(+ x 1) ;-&amp;gt; 11
'(+ x 1) ;-&amp;gt; (+ x 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Quote is ok when we want to write a tiny bit of code-as-data, but when
we write macros, we often want to interpolate between data and code in
a more dynamic way.  We could, of course, use &lt;code&gt;list&lt;/code&gt; to create our
code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(list '+ 'x 10) ;-&amp;gt; (+ x 10)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because &lt;code&gt;list&lt;/code&gt; is a function, each argument is evaluated - we &lt;code&gt;quote&lt;/code&gt;
manually whatever arguments we want to leave unevaluated as we
construct our code.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Quasiquotation&lt;/em&gt; is kind of the opposite of &lt;code&gt;list&lt;/code&gt;.  A quasiquoted
expression, by default, doesn't evaluate its inputs &lt;em&gt;except&lt;/em&gt; when they
are &lt;em&gt;unquoted&lt;/em&gt;. Quasiquote is indicated by the back-quote character:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And, within a quasiquoted form, unquotation is indicated by a &lt;code&gt;,&lt;/code&gt;,
which kind of makes sense.  Quasiquotation works pretty much
identically in Emacs and Common Lisp, so fire one of them up and try
it out.  You can use &lt;a href="http://www.quicklisp.org/"&gt;quicklisp&lt;/a&gt; to install Common Lisp quickly.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`(+ x ,(+ 10 11 12)) ;-&amp;gt; (+ x 33)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Should work in either Common Lisp or Emacs.  Note, however, that
different Lisps handle symbols slightly differently.  In SBCL, a
Common Lisp implementation, &lt;code&gt;'x&lt;/code&gt; will evaluated to &lt;code&gt;X&lt;/code&gt;, and symbols
are not case-sensitive by default.  Symbols are case sensitive in
Emacs Lisp.&lt;/p&gt;

&lt;p&gt;Sometimes you want to interpolate the contents of a list into a piece
of code, in which case you say &lt;code&gt;,@&lt;/code&gt; instead of &lt;code&gt;,&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`(+ ,@(list 1 2 3)) ;-&amp;gt; (+ 1 2 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Without concerning ourselves with namespaces or packages (of which
Emacs has neither) &lt;a href="http://clojure.org/getting_started"&gt;Clojure&lt;/a&gt; macros operate in the same way as
Emacs/Common Lisp macros, but with some slight changes in the way
operations are indicated:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro my-let* [bindings &amp;amp; body]
  (cond
   (empty? bindings) (cons 'do body)
   :else
   (let [[symbol value-expr &amp;amp; leftover-bindings] bindings]
     `((fn [~symbol] (my-let* ~leftover-bindings ~@body))
       ~value-expr))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This piece of code reads just like the Common/Emacs Lisp except for a
few details.  The first is that the argument list to the macro is
specified using a Clojure vector rather than a list.  Unlike in
Common/Emacs Lisp, the default Clojure reader is able to read more
than lists and symbols.  The &lt;code&gt;[]&lt;/code&gt; syntax indicates a Clojure
persistent vector.  It is read into the code representation as a
vector, rather than as some kind of list or atom.  This is both an
important and a trivial difference.  Consider in Emacs Lisp:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(vector 1 2 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This indeed evaluates to a vector with elements &lt;code&gt;1 2 3&lt;/code&gt;.  But it is
&lt;em&gt;read&lt;/em&gt; as a list whose head is the symbol &lt;code&gt;vector&lt;/code&gt;.  Only upon
evaluation do we get a vector.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(vectorp '(vector 1 2 3)) ;-&amp;gt; nil
(listp   '(vector 1 2 3)) ;-&amp;gt; t
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Clojure, by contrast:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(vector? '[1 2 3]) ;-&amp;gt; true
(list?   '[1 2 3]) ;-&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(But see this footnote:&lt;sup&gt;2&lt;/sup&gt;).&lt;/p&gt;

&lt;p&gt;(As an aside, Clojure also supports tables as part of its code
representation, in keeping with its intent of expanding Lisp's
philosophy to data structures other than lists.)&lt;/p&gt;

&lt;p&gt;In Clojure, &lt;code&gt;do&lt;/code&gt; is how you say &lt;code&gt;progn&lt;/code&gt;, both of which introduce a
form which evaluates all its parts, and returns the result of the
last.  Next, we follow the convention that redundant parentheses in
Clojure should be elided.  Because we know &lt;code&gt;bindings&lt;/code&gt; contains a list
of pairs, we just read that list by two, rather than as an actual list
of pairs.  You see this in Clojure's &lt;code&gt;let&lt;/code&gt; form, which has one less
layer of nesting.  &lt;/p&gt;

&lt;p&gt;Emacs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let* ((x 10) (y 11)) (+ x y))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clojure:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let [x 10 y 11] (+ x y))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Note that picoLisp also elides redundant parentheses, but does not
use vectors for binding, also note that Clojure doesn't have &lt;code&gt;let*&lt;/code&gt;,
&lt;code&gt;let&lt;/code&gt; has that behavior).&lt;/p&gt;

&lt;p&gt;Also by convention, we use vectors for the &lt;em&gt;binding&lt;/em&gt; part of any
form (although this macro doesn't check for that).  We say &lt;code&gt;fn&lt;/code&gt;
instead of &lt;code&gt;lambda&lt;/code&gt; in Clojure, and we use &lt;code&gt;~&lt;/code&gt; for the unquote
operation.  In Clojure, &lt;code&gt;~@&lt;/code&gt; is the way you say
&lt;code&gt;,@&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Other than those differences, the Clojure macro has the same behavior
as the Emacs/Common Lisp Macro.  It creates an invisible function
which is used to expand code tagged with that macro name during
post-read processing, and then the code is passed to the Clojure
compiler.  It is worth noting here that Brian Goslinga has ported
Scheme-style &lt;a href="https://github.com/qbg/syntax-rules"&gt;syntax-case/rules macros to
Clojure&lt;/a&gt;, but it isn't clear to me without
further study whether they truly are hygeinic.  Claims of macro
hygiene are often exagerrated outside of the Scheme universe.&lt;/p&gt;

&lt;h2&gt;Issues with Naive Code-Rewriting Macros&lt;/h2&gt;

&lt;p&gt;The best way to understand the motivation for Scheme hygeinic macros,
as well as some of the differences between more or less conventional
macro systems in other Lisps is to understand how things can go
wrong.  Last time we talked extensively about scope and how it effects
the way we look at a piece of data that represents code.  The upshot
was that in languages where scope is &lt;em&gt;dynamic&lt;/em&gt;, which means variables
evaluate to whatever the current binding of the variable is at the
moment of evaluation, can simply represent code (and particularly the
meaning of symbols) as just lists of atoms and symbols.  A piece of
code "means" whatever it is you get by evaluating it in the context of
the current bindings from symbols to values.  Period.&lt;/p&gt;

&lt;p&gt;Lexically scoped languages, on the other hand, impose more strenuous
semantics on code.  In a lexically-scoped language, variables refer to
the &lt;em&gt;lexical environment&lt;/em&gt; (the environment around them "on the page")
when they are evaluated.  Hence, in a lexically scoped language, a
"naked" piece of code, such as the code produced by quotations, is
"impoverished" - it doesn't record in any way the lexical environment
in which the quotation was created, and so it is, in some sense,
"meaningless," at least the extent that it contains symbols which
aren't bound.  &lt;/p&gt;

&lt;p&gt;In a dynamic language, the code fragment consisting of the single
symbol &lt;code&gt;'x&lt;/code&gt; means "Whatever value &lt;code&gt;x&lt;/code&gt; has when you evaluate me."  This
representation is complete, but depends on the evaluator.  In a
lexically scoped language, where symbols, when they appear in code,
are implicitly associated by the rules of evaluation with a lexical
context they were originally created in, &lt;code&gt;'x&lt;/code&gt; is meaningless.  It
&lt;em&gt;looks&lt;/em&gt; like a piece of code, but in a real sense it isn't &lt;em&gt;quite&lt;/em&gt;
one.  &lt;/p&gt;

&lt;p&gt;Emacs/Common Lisp/Clojure style macros, however, operate on pieces of
"code" produced by ordinary quotation - that is, they operate on
"hypocode," say, which just means something not quite code.  &lt;/p&gt;

&lt;h2&gt;Making a Mess&lt;/h2&gt;

&lt;p&gt;Let's make a mess, by way of example.  Suppose we have an object
system wherin objects are collections of key -&gt; value relations in a
persistent data structure, like an association list&lt;sup&gt;3&lt;/sup&gt;.  We
will work in Emacs/Common Lisp.  &lt;/p&gt;

&lt;p&gt;Preliminaries:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun empty? (x)
  (eq x nil))

(defun set-slot (obj symbol val &amp;amp;optional acc)
  (cond 
   ((empty? obj) 
    (cons (cons symbol val) (reverse acc)))
   (t
    (let* ((first (car obj))
           (o-key (car first))
           (rest (cdr obj)))
      (if (eq symbol o-key) 
          (append (reverse acc) (cons (cons symbol val) rest))
        (set-slot rest symbol val (cons first acc)))))))

(defun get-slot (obj symbol)
  (cond
   ((empty? obj) nil)
   ((eq (car (car obj)) symbol)
    (cdr (car obj)))
   (t
    (get-slot (cdr obj) symbol))))

(get-slot (set-slot (set-slot (set-slot '() 'x 10) 'x 11) 'y 14) 'y)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Methods will just be functions whose first argument is the &lt;code&gt;self&lt;/code&gt;
object.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun make-person (first last) 
  `((:first-name . ,first)
    (:last-name . ,last)))

(defun change-first-name (self new-first)
   (set-slot self :first-name new-first))

(defun change-last-name (self new-last)
   (set-slot self :last-name new-last))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(note the use of backquote &lt;em&gt;outside&lt;/em&gt; of a macro.)&lt;/p&gt;

&lt;p&gt;Our objects are "pure" in this example - the methods &lt;code&gt;change-first-name&lt;/code&gt;
and &lt;code&gt;change-last-name&lt;/code&gt; don't modify &lt;code&gt;self&lt;/code&gt;, they return a &lt;em&gt;fresh&lt;/em&gt;
&lt;code&gt;self&lt;/code&gt; object with the appropriate changes.  I prefer this behavior,
but it makes chaining method application clumsy.  We'll develop a
macro to make method chaining for pure objects easier.&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/600full-the-bionic-woman-photo.jpg" alt="the Bionic Woman" title="" /&gt;
&lt;/center&gt;
&lt;center&gt; I don't know the Bionic Woman, but she comes up when you
google "Woman". &lt;/center&gt;&lt;/p&gt;

&lt;p&gt;I know a woman who changed both her first and last names when she got
married.  She went from Ami Culbert to Amy Klein (she had always
resented her parents for the unusual spelling of her first name).
We've got to nest our method calls or use a &lt;code&gt;let&lt;/code&gt; to affect both
renamings:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((new-self 
       (change-first-name (make-person "Ami"
                                       "Culbert") "Amy")))
   (change-last-name new-self "Klein"))
 ; -&amp;gt; ((:first-name . "Amy") (:last-name . "Klein"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let's write a macro which automatically threads an object through
method invokation.  It should expand:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(with-object o 
  (change-first-name "Amy")
  (change-last-name "Klein"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;into something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((new-self 
       (change-first-name o "Amy")))
   (change-last-name new-self "Klein"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;People often complain, inaccurately, I think, that macros are "hard to
debug" because they aren't regular functions.  It is true they aren't
invoked as regular functions in the ordinary course of events, but one
can certainly write the "business end" of the macro as a regular
function and just have the macro definition pass the quoted inputs to
this function appropriately.  Then you can interactively test the
macro expansion, build unit tests, etc.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun with-object-expander (object-expr method-applications)
  `(let* ((object ,object-expr)
          ,@(mapcar 
             (lambda (methap) 
               `(object (,(car methap) object ,@(cdr methap)))) 
            method-applications))
        object))

(with-object-expander 'test-object 
 '((change-first-name "Amy")
   (change-last-name "Klein"))) ;-&amp;gt; 
  (let* ((object test-object) 
         (object (change-first-name object "Amy")) 
         (object (change-last-name object "Klein"))) 
     object)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are new to Lisp macro writing, this probably looks like it
works just fine.  If you are a hoary old Lisper (and I know some of
you are), then this probably looks like one of those ridiculous
examples from safety videos where a goofy guy climbs up a ladder, or
some such, with an aerial around some high tension power lines, which
is exactly what it is.  Let's sally forth like Goofus, however:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro with-object (object-expression &amp;amp;rest method-exprs)
  (with-object-expander object-expression method-exprs))

(with-object (make-person "Ami" "Culbert")
  (change-first-name "Amy")
  (change-last-name "Klein")) ; -&amp;gt; 
   ((:first-name . "Amy") (:last-name . "Klein"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/goofus-and-gallant.jpg" alt="Oh Goofus" title="" /&gt;
&lt;/center&gt;
&lt;center&gt; Goofus probably programs in &lt;a href="http://www.perl.org/"&gt;Perl&lt;/a&gt;. Just sayin'.&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Indeed, apparent success!  However, imagine if we are instead taking
the new last name from an object representing Ami's fiance:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((object (make-person "Jason" "Klein")))
  (with-object (make-person "Ami" "Culbert")
    (change-first-name "Amy")
    (change-last-name (get-slot object :last-name)))) ; -&amp;gt;
  ((:first-name . "Amy") (:last-name . "Culbert"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What gives?  Amy's last name &lt;em&gt;should&lt;/em&gt; be "Klein" but it is stil
"Culbert".  What happened?  A quick look at the macro expansion of the
entire expression would help, but how do we get such a thing?
Emacs/Common/Clojure Lisps provide macro-expansion tools which take an
expression and expand macros inside of it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(macroexpand-all 
 '(let ((object (make-person "Jason" "Klein")))
  (with-object (make-person "Ami" "Culbert")
    (change-first-name "Amy")
    (change-last-name (get-slot object :last-name))))) ;-&amp;gt;

    (let ((object (make-person "Jason" "Klein")))
      (let* ((object (make-person "Ami" "Culbert"))
     (object (change-first-name object "Amy"))
     (object (change-last-name object 
               (get-slot object :last-name))))
     object))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can see what the problem is.  We bound Amy's fiance to the
symbol &lt;code&gt;object&lt;/code&gt;, but our macro expansion also used that symbol
internally, to represent the object being threaded through the method
applications, which means it was bound to the &lt;em&gt;Amy&lt;/em&gt; person by the time
the call to &lt;code&gt;get-slot&lt;/code&gt; was executed on &lt;code&gt;object&lt;/code&gt;.  Amy's last name was
just her last name, so the &lt;code&gt;change-last-name&lt;/code&gt; call changed nothing!&lt;/p&gt;

&lt;p&gt;Note that this is, essentially, an error of &lt;em&gt;context&lt;/em&gt; or &lt;em&gt;variable
interpretation&lt;/em&gt;.  Our mental model was that the &lt;code&gt;object&lt;/code&gt; symbol used
during macro expansion was unrelated to the &lt;code&gt;object&lt;/code&gt; symbol used in
the subsequent &lt;code&gt;let&lt;/code&gt; expression.  But the Lisp has no way of knowing
that.  Because we are manipulating naked code, a symbol is just a
symbol, and the behavior of our complete expansion is just whatever
behavior it looks like it would have if someone had written it by
hand.  Scheme's macro system is designed to resolve this problem by
separating contexts more aggressively, so that macro expansion doesn't
get in the way of later variable binding &lt;em&gt;unless you specifically want
it to&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We don't, however, have hygeinic macros in the Lisps we are talking
about today, so how do we avoid the problem specified above?  There
are at least two ways which are in common use.  The first is to
force the invoker of a macro to provide any names which might be used
during expansion, so they are forced to acknowledge that those names
will be rebound by the macro.  The second is to make sure that the
macro uses symbols which you are certain will never be used by the
programmer who invokes the macro.  &lt;/p&gt;

&lt;p&gt;The former strategy looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro with-object-bound-to (sym obj &amp;amp;rest meths)
  `(let* ((,sym ,obj)
          ,@(mapcar
            (lambda (m)
              `(,sym (,(car m) ,sym ,@(cdr m)))) 
            meths))
        ,sym))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Invoked as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((object (make-person "Jason" "Klein")))
      (with-object-bound-to amy 
        (make-person "Ami" "Culbert")
        (change-first-name "Amy")
        (change-last-name (get-slot object :last-name))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which has the intended effect.  However, if you don't intend on using
the value bound to &lt;code&gt;amy&lt;/code&gt; at any time, you'd probably prefer regular
old &lt;code&gt;with-object&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;We could implement with object by just trying to use a crazy symbol
for &lt;code&gt;object&lt;/code&gt;, something we'd hope the users of our macro will never
think to use.  For instance, we could prepend the letter s to the md5
hash of the word "object": sa8cfde6331bd59eb2ac96f8911c4b666:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun with-object-expander (object-expr method-applications)
  `(let* ((sa8cfde6331bd59eb2ac96f8911c4b666 ,object-expr)
          ,@(mapcar 
             (lambda (methap) 
               `(sa8cfde6331bd59eb2ac96f8911c4b666 (,(car methap)
  sa8cfde6331bd59eb2ac96f8911c4b666 
  ,@(cdr methap)))) 
            method-applications))
        sa8cfde6331bd59eb2ac96f8911c4b666))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that would probably work.  However, Lisps usually provide a
facility to generate symbols guaranteed not to be used (subject to the
understanding that if symbols are read at run-time, all bets are off -
althouh it is still unlikely there will be a problem).  In Common and
Emacs Lisp, this is accomplished via the function &lt;code&gt;gensym&lt;/code&gt;.  In these
Lisps, we'd write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun with-object-expander (object-expr method-applications)
  (let ((object-name (gensym "object-")))
  `(let* ((,object-name ,object-expr)
          ,@(mapcar 
             (lambda (methap) 
               `(,object-name (,(car methap) ,object-name ,@(cdr methap)))) 
            method-applications))
        ,object-name)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The critical points are that we've introduced a variable &lt;code&gt;object-name&lt;/code&gt;
which, at macroexpansion time, is bound to a fresh symbol.  We then
insert that symbol wherever &lt;code&gt;object&lt;/code&gt; used to be by unquoting it into
out expanded expression.&lt;/p&gt;

&lt;p&gt;Turns out this feature is so commonly used that Clojure provides
special support for it - during backquote expansion, symbols
terminated with a &lt;code&gt;#&lt;/code&gt; character are automatically bound to
appropriately generated symbols.  By appropriate we mean that
&lt;code&gt;object#&lt;/code&gt; will always be expanded to the same symbol within a single
backquote.  In this example, since some of the uses of the &lt;code&gt;object&lt;/code&gt;
symbol are generated programmatically, we'd still need to use
&lt;code&gt;gensym&lt;/code&gt;, which Clojure also provides.  &lt;/p&gt;

&lt;h2&gt;Namespaces, Packages, Hygiene&lt;/h2&gt;

&lt;p&gt;All this &lt;code&gt;gensym&lt;/code&gt; falderal is meant to help us ensure our macros are
&lt;em&gt;hygienic&lt;/em&gt;, which informally means that in the body of a macro, the
code you write behaves the way you expect it to behave, modulo
whatever changes in meaning the macro implies AND that when the macro
expands, it means what the &lt;em&gt;macro writer&lt;/em&gt; meant it to mean, regardless
of what symbols mean where the macro is expanded..  Variable capture,
the example we looked at above, is only one of many possible hygiene
complications.  Another possibility is that a macro (the dependent
macro, lets call it) might depend on &lt;em&gt;another&lt;/em&gt; macro, which itself is
defined one way when the dependent macro is written, but might be
redefined &lt;em&gt;later&lt;/em&gt; when someone else tries to expand the dependent
macro.  It's very hard to write macros that are hygienic in this sense
in the Lisps we've talked about so far, but some of them do provide
the faculties to at least help a bit.&lt;/p&gt;

&lt;p&gt;Emacs Lisp is the oddball in this department, however - it has neither
packages or namespaces or any other module system&lt;sup&gt;4&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Both Clojure and Common Lisp provide some conception of modules or
bags of symbols.  In Common Lisp these are called packages, in Clojure
they are called namespaces.  In both cases, macro writers can use
namespaces to help avoid macro expansion problems.  Consider the case
where some joker redefines the &lt;code&gt;let*&lt;/code&gt; special form.  Your macro wants
to use &lt;code&gt;let*&lt;/code&gt; as it is ordinarily defined by Common Lisp.  He wants to
use your macro, but wants to put code &lt;em&gt;in it&lt;/em&gt; that depends on his
crazy new definition of &lt;code&gt;let*&lt;/code&gt;.  If you write your macro (expander)
as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun with-object-expander (object-expr method-applications)
  (let ((object-name (gensym "object-")))
  `(cl::let* ((,object-name ,object-expr)
                ,@(mapcar 
             (lambda (methap) 
               `(,object-name (,(car methap) ,object-name ,@(cdr methap)))) 
            method-applications))
        ,object-name)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can have your cake and the user of your macro can eat it too.  The
key changes is that we've qualified our &lt;code&gt;let*&lt;/code&gt; symbol with the package
it lives in.  &lt;code&gt;cl::let*&lt;/code&gt; means we want Lisp to use the meaning of
&lt;code&gt;let*&lt;/code&gt; as defined in the &lt;em&gt;package&lt;/em&gt; &lt;code&gt;cl&lt;/code&gt; or &lt;code&gt;common-lisp&lt;/code&gt; (packages can
have nicknames in Common Lisp).  If our user redefines &lt;code&gt;let*&lt;/code&gt; in his
own package, then he can use our macro without any problems.  &lt;/p&gt;

&lt;p&gt;Clojure takes this one step further.  Backquote automatically expands
symbols to their namespace-qualified names, so macros automatically
expand to refer to the special forms as defined in whatever namespaces
they are defined in when the macro itself was defined.  Material
unquoted into the macro expansion is expanded appropriate to the
context it was introduced in.  The net result is that someone
redefines &lt;code&gt;let&lt;/code&gt; in their own package, and calls your macro, it
automatically expands to use the &lt;code&gt;let&lt;/code&gt; from the base Clojure package:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
`(let [x 10 ~'y 11] (+ x y)) ;-&amp;gt;
 (clojure.core/let [user/x 10 (quote user/y) 11] 
    (clojure.core/+
       user/x 
       user/y))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I haven't specifically mentioned Arc throughout all this, but &lt;a href="http://files.arcfn.com/doc/macro.html"&gt;Arc
macros&lt;/a&gt; basically follow the same design as those in
Common/Emacs Lisp and Clojure.  If they are more similar to any of the
above than any other, it is Emacs Lisp, since Arc doesn't appear to
have, at the moment, a namespace or package system.&lt;/p&gt;

&lt;h2&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;Good Lisp programmers use macros sparingly, for the most part, and so
it is difficult to think up examples when you'd need more macro
hygiene then what you get in Lisps with &lt;code&gt;gensym&lt;/code&gt; and packages and
namespaces.  In particular, Clojure seems to provide enough protection
that you won't accidentally shoot yourself in the foot unless you
wander off the beaten path.&lt;/p&gt;

&lt;p&gt;However, you might wonder if we can't come up with a more formal
syntactic extension scheme which really protects us in a simple to
understand way.  Something that makes macro hygiene as easy to
conceptualize as variable hygiene for regular code?  There are lots of
ways to approach this problem (more than I know of, certainly) but
next time we'll discuss the way Scheme does it.  Scheme
syntax-rules/case macros are very different compared to &lt;code&gt;defmacro&lt;/code&gt;
style forms, and their intent is to make hygiene the &lt;em&gt;default&lt;/em&gt;
behavior, so that complex macros (that, for instance, depend on one
another) can be defined easily without much extra manual fiddling
(gensym, packages, etc).  As we'll see, the solutions are related to
the difference between lexical and dynamical behaviors, a theme which
keeps coming up!&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; See also &lt;a href="http://lambda-the-ultimate.org/node/1680"&gt;Kernel&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;2&lt;/sup&gt;
I must point out that Emacs Lisp, but not Common Lisp, has a similar
read syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[a b c] ;-&amp;gt; [a b c]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which is sort of like the vector syntax except that it reads the
elements of the vector literally, as if they were quoted.  The emacs
evaluator will not evaluate the elements: when it encounters a vector
in the code it is evaluating, it just returns the vector.  The Clojure
evaluator encounters a vector, and returns the vector containing the
result of evaluating each element in the vector.&lt;/p&gt;

&lt;p&gt;In emacs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((a 10) (b 11) (c 12)) [a b c]) ;-&amp;gt; [a b c]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Clojure:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let [a 10 b 11 c 12] [a b c]) -&amp;gt; [10 11 12]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I wish Emacs had the Clojure behavior (or an extensible reader, like
Common Lisp).&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;An association list is a list of pairs (created with &lt;code&gt;cons&lt;/code&gt;).  Each
pair consists of a symbol and a value.  &lt;/p&gt;

&lt;p&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;I estimate that the emacs session I'm running right now, to write this
piece, has about 25,000 functions and symbols bound.  The fact that
emacs works at all without namespaces, with just this giant soup of
symbols, is amazing and maybe even indicative of some kind of blind
spot in our understanding of software development.  Maybe worse &lt;em&gt;is&lt;/em&gt;
better.&lt;/p&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-5787252630388237700?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/5787252630388237700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=5787252630388237700' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5787252630388237700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5787252630388237700'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension.html' title='A Survey of Syntactic Extension Techniques in the Lisp Family of Languages (Part 2)'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-197215827206061145</id><published>2011-07-30T10:10:00.000-07:00</published><updated>2011-08-16T06:42:28.405-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='picoLisp'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='programming languages'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><category scheme='http://www.blogger.com/atom/ns#' term='metaprogramming'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='syntax extension'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>A Survey of Syntactic Extension Techniques in the Lisp Family of Languages (Part 1)</title><content type='html'>&lt;p&gt;Lisp is famous for, and in some sense, defined by (see
&lt;a href="http://www.paulgraham.com/icad.html"&gt;this&lt;/a&gt;), syntactic extension.  Syntactic extension is Lisp is
a great example of a design trade off - Lisp picks a relatively high
level representation of its base syntax (lists of lists and atoms),
and then writing syntactic extensions is greatly simplified, because
they simply transform that high level representation.  Syntax
extensions don't have to worry about the nitty-gritty details of
turning character streams into some kind of program representation
(that is done for you, by the reader), and the code representation is
simple enough to modify with the language itself, so extensions are
easy to write.  The trade off is that you have to stick to Lisp's
baseline syntax (unless you get really tricky).  What this means, in
practice, is that even extended Lisp syntax will still be a list of
lists and atoms (and chock full of parentheses, for which the language
family is famous).&lt;/p&gt;

&lt;p&gt;&lt;img src="http://dl.dropbox.com/u/1076954/syntax-extension.png" alt="Transformation." title="" /&gt;&lt;/p&gt;

&lt;p&gt;Despite the simplicity of this scheme, wherein the reader transforms
text into a code-representation, which is then transformed by macros,
and then finally converted to machine code or bytecode or interpreted,
various Lisp dialects have approached the question of syntactic
extension differently.  Since I've recently gotten interested in macro
hygiene, I thought it would be nice to write a series of posts about
the different kinds of macro transformations available in various Lisp
dialects.&lt;/p&gt;

&lt;h2&gt;picoLisp&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://picoLisp.com/5000/-2.html"&gt;picoLisp&lt;/a&gt; is a pretty bizarre Lisp variant which doesn't
enjoy wide use, and has a pretty amazing set of &lt;a href="http://software-lab.de/doc/faq.html"&gt;philosophical
underpinnings&lt;/a&gt;.  PicoLisp is sufficiently different from
other Lisps that I hesitated to cover it here, but it turns out to be
extremely useful from a pedagogical point of view.  How come?  Because
the code/data relationship, which is important in all Lisps, plays an
even more central role in picoLisp.  We are going to end this post
talking about hygienic macros in Scheme, and picoLisp, in a sense,
represents the polar opposite approach, with other Lisps occupying an
arguably uncomfortable or impure middle ground.  Code and data are so
tightly coupled in picoLisp that the above picture of how syntactic
extension works doesn't even really apply.&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/david-goliath.jpg" alt="Smaller is Better" title="" /&gt;
&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;a href="http://en.wikipedia.org/wiki/Goliath"&gt;Smaller is Better&lt;/a&gt;&lt;/center&gt;&lt;/p&gt;

&lt;h3&gt;Code is Data&lt;/h3&gt;

&lt;p&gt;So what do we really mean when we say code is data?  This is a bit
fatuous, in a way - code is text, after all.  All Lisp variants I know
still require the user to write text out into a file, which is then
read by the Lisp environment and acted upon.  As suggested above,
"code is data" really means that code is transformed from text into an
intermediate form which is represented in terms of Lisp data types.
We can then use the Lisp itself to transform that data or to execute
it or interpret it.&lt;/p&gt;

&lt;p&gt;What is the simplest thing we can do with Lisp data that may represent
some code?  This one is easy: the simplest thing we can do is
&lt;em&gt;nothing&lt;/em&gt;.  Doing nothing to piece of code/data is called "quotation"
in Lisp.  It is represented by preceding an expression with the &lt;code&gt;'&lt;/code&gt;
character.  Quoted expressions aren't evaluated - when the interpreter
hits one, it just returns the data stored in the quotation.  A quoted
expression is &lt;em&gt;read&lt;/em&gt; by the reader, but not evaluated.  It is just
data.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;'a ;-&amp;gt; a
'(this is some data) ;-&amp;gt; (this is some data)
'(if T "True" "False") ;-&amp;gt; (if T "True" "False")
(if T "True" "False") ;-&amp;gt; "True"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(N.B. You could &lt;a href="http://software-lab.de/down.html"&gt;download picoLisp&lt;/a&gt; and try these
examples.)&lt;/p&gt;

&lt;p&gt;Every line above except the last is quoted, and evaluates to just the
quoted data.  The last expression isn't quoted, and so it is actually
evaluated by the interpreter.  &lt;code&gt;t&lt;/code&gt; is the true value, so the
expression evaluates to the first clause, which is the "string"
"True" (technically "True" is a symbol, since picoLisp only has one
type which encompasses both strings and symbols).  The opposite of
quotation is &lt;code&gt;eval&lt;/code&gt;uation.  This is represented as the function &lt;code&gt;eval&lt;/code&gt;
in picoLisp:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(eval '(if T "True" "False")) ;-&amp;gt; "True"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A slightly less trivial example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq x 10)
(setq q '(+ x 1))
(eval q) ;-&amp;gt; 11
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When &lt;code&gt;eval&lt;/code&gt; encounters a symbol, like &lt;code&gt;x&lt;/code&gt;, above, it just replaces it
with the current value of that symbol wherever &lt;code&gt;eval&lt;/code&gt; is called.  So,
if we run the above code and then say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let x 0 (eval q)) ;-&amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We get &lt;code&gt;1&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;This is one of the ways the picoLisp differs from most other Lisps.
This behavior is called "dynamic scope," and while most Lisps (even
those that have this behavior) look upon it as a historical relic
better thrown off or worked around, picoLisp embraces this behavior.
This is because picoLisp is aiming for simplicity in its interpreter,
and nothing is simpler than a symbol just meaning "get what the value
of this symbol is right here, right now."  As we'll see, &lt;code&gt;eval&lt;/code&gt; in
newer Lisp dialects won't work this way.  &lt;/p&gt;

&lt;p&gt;Ok, ok, we are sort of drifting.  We were talking about Macros.
Rather surprisingly, picoLisp's documentation says that:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Yes, there is a macro mechanism in picoLisp, to build and
  immediately execute a list of expressions. But it is seldom
  used. Macros are a kludge. Most things where you need macros in
  other Lisps are directly expressible as functions in picoLisp, which
  (as opposed to macros) can be applied, passed around, and debugged.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How can this be?  Well, functions in picoLisp have the option of &lt;em&gt;not
evaluating their arguments&lt;/em&gt;.  In most languages, function calls
proceed by first evaluating all of their arguments, &lt;em&gt;then&lt;/em&gt; binding
those values to the variable names and then executing the body of the
function.  This is how it works in picoLisp too, if you say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(de fun (A)
  (print "Inside")
  (print A))
(fun (prog (print "Argument evaluated.") 10))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You get:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;"Argument evaluated.""Inside" 10-&amp;gt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For non-Lispers, &lt;code&gt;prog&lt;/code&gt; is just a form which evaluates each of its
sub-forms in order and returns the last value.  We are using it here
to demonstrate when the argument expression is evaluated.&lt;/p&gt;

&lt;p&gt;So when a function is called, its arguments are evaluated.  This is
true in every Lisp I know (except for exotica like Lazy Lisp, in
Racket).  This property is usually called, by the way, eagerness: the
function is eager to know its argument's values before evaluating the
body. &lt;/p&gt;

&lt;p&gt;Unlike other Lisps, picoLisp lets you define functions which &lt;em&gt;don't&lt;/em&gt;
evaluate their arguments immediately.  This is done by providing a
single symbol instead of an argument list:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(de fun2 A
  (print "Inside")
  (print (eval (car A))))
(fun2 (prog (print "Argument evaluated.") 10))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(&lt;code&gt;car&lt;/code&gt; returns the first element of a list).&lt;/p&gt;

&lt;p&gt;Results in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;"Inside""Argument evaluated."10-&amp;gt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The argument expression is not evaluated until we (unpack it and then)
call eval on it.  Using this feature, we can do something which is
usually impossible in other languages: write &lt;code&gt;if&lt;/code&gt; as a function.  In
most languages you can't write if in any nice way.  In most Lisps,
you've got to write a macro.  In picoLisp you can just write &lt;code&gt;if&lt;/code&gt; as
an "ordinary" function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(de my-if A
 (if (eval (car A))
     (eval (cadr A))
     (eval (caddr A))))

(my-if (&amp;lt; 1 0) 
  (prog (print "True Branch Evaled") T) 
  (prog (print "False Branch Evaled") NIL))

"False Branch Evaled"-&amp;gt; NIL
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that "True Branch Evaled" is never printed.  The true branch is
never evaluated.  &lt;/p&gt;

&lt;p&gt;(Obviously we have to use the primitive &lt;code&gt;if&lt;/code&gt; - the example is meant to
highlight optional evaluation of arguments, not how a primitive like
if makes it into a language).  &lt;/p&gt;

&lt;p&gt;Because functions can do this kind of thing, you can do some
surprising things with picoLisp, like &lt;code&gt;map&lt;/code&gt; if over lists of
branches.  I find it astonishing that the picoLisp docs provide this
example and then say picoLisp goes for "The principle of least
astonishment."&lt;/p&gt;

&lt;h2&gt;Variables, Scope, Eval, Quote and Lambda&lt;/h2&gt;

&lt;p&gt;If you are used to other programming languages, picoLisp probably
seems pretty bizarre.  But, if you're a little more open minded, you
might think: wait a minute, this optional evaluation of input
expressions is actually &lt;a href="http://en.wikipedia.org/wiki/Fexpr"&gt;pretty handy&lt;/a&gt;!  I can write all my special
forms as regular functions, and then they too can be first class
values!  Why isn't every Lisp like this?&lt;/p&gt;

&lt;p&gt;Well, I can't claim to understand the entire historical development of
the Lisp family of languages, but I'm pretty sure that the main reason
other Lisps moved away from this model is that they wanted to support
&lt;em&gt;lexical scope&lt;/em&gt; (to be discussed below).  The watchword of picoLisp is
simplicity: simplicity is why it is an interpreted language, rather
than a compiled one, and simplicity shaped the design of the
interpreter.  Dynamic variable scope is the &lt;em&gt;simplest thing&lt;/em&gt;, as far
as an interpreter is concerned, and so picoLisp runs with it.  Because
code is always evaluated with respect to a dynamic environment, the
code itself doesn't need to remember anything like "Which &lt;code&gt;x&lt;/code&gt; did the
programmer mean in the peice of code &lt;code&gt;(+ x 1)&lt;/code&gt;."  It is simply assumed
that, by default, &lt;code&gt;x&lt;/code&gt; refers to the &lt;code&gt;x&lt;/code&gt; in the current dynamic
environment.  Under these semantic assumptions, a quotation is just a
lambda expression without any arguments.  &lt;code&gt;Eval&lt;/code&gt; is just &lt;code&gt;funcall&lt;/code&gt; for
functions that take no arguments.&lt;/p&gt;

&lt;p&gt;This is, in fact, literally true in picoLisp, even for functions with
arguments..  There is no lambda.  Anonymous functions are created with
&lt;code&gt;quote&lt;/code&gt;ation.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(mapcar '((X) (+ X 1)) (list 1 2 3 4))
; -&amp;gt; (2 3 4 5)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because variable binding is assumed to be handled by &lt;code&gt;eval&lt;/code&gt; and is
assumed to be related only to where &lt;code&gt;eval&lt;/code&gt; is called, not to anything
about where the lambda was defined, code in picoLisp need have no
additional information associated with it.  It literally is just data.&lt;/p&gt;

&lt;p&gt;In short, buzzword packed language: Restricting intepretation of
variable binding to dynamic scope, picoLisp unifies function
application and regular evaluation, and &lt;code&gt;quote&lt;/code&gt; and &lt;code&gt;lambda&lt;/code&gt;.  With
minimal convention, the distinction between functions and special
forms nearly vanishes!&lt;/p&gt;

&lt;h2&gt;Lexical Scope?&lt;/h2&gt;

&lt;p&gt;I keep going on about &lt;em&gt;dynamic scope&lt;/em&gt; above, and I even mentioned
&lt;em&gt;lexical scope&lt;/em&gt; above, but what is &lt;em&gt;lexical scope&lt;/em&gt; and how does it
impact upon the relationship between function application and
evaluation and between quotation and lambda?&lt;/p&gt;

&lt;p&gt;We are going to switch to &lt;em&gt;Emacs Lisp&lt;/em&gt; for some examples now, because
Emacs Lisp supports &lt;em&gt;both&lt;/em&gt; lexical and dynamic scope and forces us to
say explicitly which scoping rules we want to use.  This helps make
the discussion of scope clearer.&lt;/p&gt;

&lt;p&gt;(If you want to follow along, Download the &lt;a href="http://www.gnu.org/software/emacs/"&gt;latest emacs&lt;/a&gt;, fire
it up, and then press "Alt-x ielm *ENTER*".  This will start up an
Emacs Lisp read-eval-print loop.  There are better ways to interact
with the Emacs Lisp interpreter, but this is the most familiar for
non-emacs users.)&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://www.loufranco.com/blog/files/page2_blog_entry54_1.gif" alt="Vi propaganda!" title="" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;&lt;center&gt;Vi propaganda! (borrowed from &lt;a href="http://www.loufranco.com/blog/files/category-software-development.html"&gt;here&lt;/a&gt;).&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Emacs Lisp is a relatively old Lisp dialect, and by default it is
dynamically scoped.  There is a library that comes with Emacs that
adds a lot of features from Common Lisp, including simulation of
lexical-scope rules, so the first thing you need to do is require this
package:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP&amp;gt; (require 'cl)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A quick dynamic scope example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 10))
 (defun get-value-of-x () x)
 (get-value-of-x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Obviously evaluates to &lt;code&gt;10&lt;/code&gt;.  &lt;code&gt;get-value-of-x&lt;/code&gt; just returns the value of
&lt;code&gt;x&lt;/code&gt;, whatever it is, when it is called.  Hence:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 11))
  (get-value-of-x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Is &lt;code&gt;11&lt;/code&gt;.  If we call &lt;code&gt;get-value-of-x&lt;/code&gt; outside of a &lt;code&gt;let&lt;/code&gt; which binds
&lt;code&gt;x&lt;/code&gt;, we'll get an error.  &lt;code&gt;x&lt;/code&gt;, at that point, has no value at all.&lt;/p&gt;

&lt;p&gt;What if we use &lt;code&gt;lexical-let&lt;/code&gt; instead?  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lexical-let ((x 10))
 (defun get-value-of-x () x)
 (get-value-of-x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is still &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, perhaps surprisingly, we can now say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(get-value-of-x)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Outside of any &lt;code&gt;let&lt;/code&gt; expression which binds &lt;code&gt;x&lt;/code&gt; and get &lt;code&gt;10&lt;/code&gt; as the
answer.  Perhaps even more surprisingly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 11))
  (get-value-of-x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Is still &lt;code&gt;10&lt;/code&gt;.  Even:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lexical-let ((x 11))
  (get-value-of-x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Is still &lt;code&gt;10&lt;/code&gt;.  In the context of a &lt;code&gt;lexical-let&lt;/code&gt;, symbols bound by
that &lt;code&gt;lexical-let&lt;/code&gt; don't refer to whatever value is bound to that
symbol "right now" in the dynamical environment, they refer to the
particular lexical environment of the &lt;code&gt;lexical-let&lt;/code&gt; expression.
Conceptually, any lambda expression appearing in a &lt;code&gt;lexical-let&lt;/code&gt; has
to store more than just the list of symbols and lists that makes up
its body; it also has to store the lexical environment to which those
symbols refer.&lt;/p&gt;

&lt;p&gt;(Lexical scope is called lexical scope because variables are
interpreted by virtue of their &lt;em&gt;lexical&lt;/em&gt; context, literally where they
appear in the source code, rather than where they are evaluated when
the program is executed.)&lt;/p&gt;

&lt;p&gt;Code might still be represented as a list of symbols and lists and
atoms, but that representation &lt;em&gt;leaves out&lt;/em&gt; information about lexical
context.  When we finally reach hygienic macros, when we talk about
Scheme, we'll see that decorating the list-of-things representation of
source code with &lt;em&gt;lexical information&lt;/em&gt; will be a key feature.&lt;/p&gt;

&lt;p&gt;Let's drive this home before we adjourn part 1.  Consider:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 10))
  (setq code 'x)
  (eval code))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That is definitely &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What about if we say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(eval code)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We get an error: "void variable x".  Also expected.&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lexical-let ((x 10))
  (setq code 'x)
  (eval code))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This, maybe surprisingly, gives an error too.  The real reason is that
Emacs Lisp does some tricky things to simulate lexical scope, but it
isn't totally obvious that eval should use the current lexical scope
to evaluate terms.  The meaning of symbols passed to &lt;code&gt;eval&lt;/code&gt; in the
context of lexical scope is suddenly non-trivial.  We can't execute
the above, but lets try:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lexical-let ((x 10))
  (setq code 'x))

(let ((x 11))
  (eval code))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This evaluates to &lt;code&gt;11&lt;/code&gt;.  The variable &lt;code&gt;code&lt;/code&gt; really just contains a
piece of symbolic data.  That data doesn't remember anything about
where it was defined - it forgets its lexical context - and so it
can't evaluate correctly.  The addition of lexical scope &lt;em&gt;breaks the
symmetry&lt;/em&gt; between &lt;code&gt;quote&lt;/code&gt;ation and &lt;code&gt;lambda&lt;/code&gt; expressions!  We didn't
need &lt;code&gt;lambda&lt;/code&gt; in picoLisp because there was no lexical context to keep
track of.  In other Lisps we need &lt;code&gt;defun&lt;/code&gt; and &lt;code&gt;lambda&lt;/code&gt; to function as
"smart quotations," which remember their lexical environment so that,
upon invokation, they can behave correctly!&lt;/p&gt;

&lt;p&gt;Because of the breakdown of this symmetry, and because Emacs Lisp
doesn't provide picoLisp style functions, we need a whole new special
form in Emacs Lisp to declare syntactic extension.&lt;/p&gt;

&lt;h2&gt;Conclusions of Part 1&lt;/h2&gt;

&lt;p&gt;Lexical scope is widely agreed to be the superior semantic mode for
programming languages, principally because it makes reasoning about a
piece of code mostly about looking at the text that defines the code,
not at when the code is executed.  But now we can see that lexical
scope comes at a price: it breaks the symmetry between code and data.
We get the simplicity, as programmers, of being able to understand our
code based on its lexical environment "on the page".  But the
compiler/interpreter now has to do extra work to execute code
correctly.  Futhermore, naive modes of representing code as data are
no longer complete: lists of lists and atoms don't contain lexical
information. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://dorophone.blogspot.com/2011/08/survey-of-syntactic-extension.html"&gt;Next time&lt;/a&gt; we'll talk about macros in Emacs/Common Lisp and Clojure
(and maybe Arc (why the hell not?)).  With today's lessons in mind,
we'll see most of the conceptual problems with macros in these
systems are associated with the fact that macros manipulate an
impoverished code representation.  &lt;/p&gt;

&lt;p&gt;In subsequent posts, we'll look at macros in Scheme, and we'll see
that the representation of code in that language is decorated with
lexical information.  This makes writing "well behaved" macros easier,
but makes writing macros a little more abstract and, arguably,
difficult to understand.   &lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-197215827206061145?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/197215827206061145/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=197215827206061145' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/197215827206061145'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/197215827206061145'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/07/survey-of-syntactic-extension.html' title='A Survey of Syntactic Extension Techniques in the Lisp Family of Languages (Part 1)'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-6948066675736398013</id><published>2011-07-16T14:06:00.001-07:00</published><updated>2011-09-19T10:59:56.130-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Smalltalk'/><category scheme='http://www.blogger.com/atom/ns#' term='iPad'/><category scheme='http://www.blogger.com/atom/ns#' term='Dynabook'/><category scheme='http://www.blogger.com/atom/ns#' term='Xerox PARC'/><category scheme='http://www.blogger.com/atom/ns#' term='Hypercard'/><category scheme='http://www.blogger.com/atom/ns#' term='Apple'/><category scheme='http://www.blogger.com/atom/ns#' term='Alan Kay'/><title type='text'>Duckspeak Vs Smalltalk</title><content type='html'>&lt;h1&gt;Duckspeak Vs Smalltalk&lt;/h1&gt;

&lt;h2&gt;The Decline of the Xerox PARC Philosophy at Apple Computers&lt;/h2&gt;

&lt;p&gt;Malcolm Gladwell's recent piece, &lt;a href="http://www.newyorker.com/reporting/2011/05/16/110516fa_fact_gladwell"&gt;"Creation Myth"&lt;/a&gt;, in
the New Yorker, about innovation and implementation via Xerox PARC,
academia and Apple Computers, tells one interesting story about that
surprising time in our modern history.  But the story of the tensions
and synergies between visionaries and businessmen elides a few
interesting details about what was going on, and why, at Xerox PARC at
the time.  Gladwell's version of history features a nimble
entrepreneur, Steve Jobs, capitalizing on an idea the value of which a
monolithic company, Xerox, can't see.  But the story of Apple and
Xerox PARC is also that of a design philosophy meant to empower people
diverging into one meant to entertain them or to sell them things.&lt;/p&gt;

&lt;p&gt;When Steve Jobs visited Xerox PARC and saw the first mouse, the system
he was looking at, the Alto, was running a programming environment and
language called Smalltalk.  While the details of this system are
glossed over in the Gladwell piece, they deserve more careful
attention.  Although The Alto bears a superficial resemblance to
modern computers, it differed in one major area: the relationship
between software developers and users.&lt;/p&gt;

&lt;p&gt;For most people software is a solid edifice - it presents a few modes
of interaction to the user, maybe a special panel of customization
options somewhere, but is otherwise as opaque and unmodifiable as a
modern car.  If users bother to think about software at all, they
think of it as a product, constructed somewhere by people called
"programmers" and distributed to the user.  If that software doesn't
do what the user wants, he might send a hopeful technical support
email, or he might just shop around for something else.&lt;/p&gt;

&lt;p&gt;Of course, there is consumer software that includes more powerful
extension features, so that, in principal, the user can add their own
functionality, but these features don't seem to be popularly used.
Firefox is an example of user-extensible software, but the vast
majority of users don't use this capacity except to download what a
small percentage of computer literate users write.&lt;/p&gt;

&lt;p&gt;In other words, it is reasonably safe to say that most people who use
computers have never written software.&lt;/p&gt;

&lt;p&gt;Why should this be, and what does it have to do with Xerox PARC,
Smalltalk and Steve Jobs?  Well, an integral part of the Xerox PARC
Philosophy was to dismantle the wall between software developers
and computer users, to develop systems so easy to program that doing
so would be a natural, simple aspect of computer use.&lt;/p&gt;

&lt;p&gt;The early years of computing technology naturally produced a division
between users and programmers - programming early computers was a
highly technical discipline which required specific knowledge of the
way the idiosyncratic hardware systems in those days worked.  But
while computers rapidly increased in power, the tools that programmers
used to program them developed relatively conservatively.  It is easy
to imagine a world where those tools developed along with the
computers, until programming itself became so easy that the average
user would feel comfortable doing it.  After all, the point of any
program is to automate or facilitate tedious work, and in this respect
programming itself is no different than a word processor.&lt;/p&gt;

&lt;p&gt;That wasn't exactly how things happened, and the reason why is a
fascinating and arguably still unresolved story in and of itself.
Part of that story takes place at Xerox PARC.&lt;/p&gt;

&lt;h2&gt;The Xerox PARC Philosophy &lt;/h2&gt;

&lt;p&gt;I mentioned above that the computer Steve Jobs saw on his visit
to to Xerox PARC, the Alto, was running something called a Smalltalk
System.  Smalltalk is still around, and you can even download a
self-contained Smalltalk System called &lt;a href="http://www.squeak.org"&gt;Squeak&lt;/a&gt; and play
around with one yourself.  What you'll see, if you do, is something
which is probably very similar to what Steve Jobs saw on that day - a
desktop-ish interface, with dragable windows and clickable buttons.
And of course, you interact with the mouse.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://dl.dropbox.com/u/1076954/Selection_003.png" alt="Pharo Smalltalk" title="" /&gt;
&lt;center&gt;The Graphical Programming Environment of Pharo Smalltalk.&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Both systems also share a fascinating property which "Creation Myth"
leaves unmentioned.  In Smalltalk, you can, using something called
"The Browser," pull up the "source code" for any object in the system.
"Object" in this case means anything in the system whatsoever,
including windows, widgets, numbers.  "Source code" is the stuff that
a compiler translates into machine code so the computer can do
something with it.  If you want, you can modify that code right there,
or copy it and create a new object with user-customized behavior.
&lt;em&gt;The entire system is transparent and modifiable&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Most of the programming languages people used in 1979 would have
looked very nearly like gibberish to a lay person.  Early computers
were slow, which meant that compilers took a long time to work unless
they were very simple.  This meant that most early programming
languages were just thin shells on top of the numbers-as-command codes
of machine language.  Even by 1979, languages hadn't developed much
further in public use - corporate and government users (pretty much
the only users before personal computing) were interested in
cost-effectiveness and systems their programmers &lt;em&gt;already knew&lt;/em&gt;, so
language and system design was very conservative.  New languages came
along, but often they were incremental improvements on previous
designs.&lt;/p&gt;

&lt;p&gt;The designers of Smalltalk (Alan Kay, Dan Ingalls, and  Adele Goldberg principally, and others), given the resources and freedom of Xerox PARC, worked
actively to reverse this trend.  Whereas a hodgepodge of cultural and
technical realities constrained the way most other programming
languages looked and felt, both Smalltalk &lt;em&gt;the language and the
system&lt;/em&gt; were written from the ground up to be so easy that a child
could use them (hence the name).  It was much more ambitious than just
that, however.  Kay saw Xerox PARC as being on the vanguard of a real
revolution in human/computer interaction. In "The Early History of
Smalltalk," Alan Kay writes of this "Xerox PARC" vision of personal
computing:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;... the user interface would have to become a learning environment
  along the lines of Montessori and Bruner; and [the] needs for large
  scope, reduction in complexity, and end-user literacy would require
  that data and control structures be done away with in favor of a
  more biological scheme of protected universal cells interacting only
  through messages that could mimic desired behavior.&lt;/p&gt;

&lt;p&gt;... we were actually trying for a for a qualitative paradigm shift
  in belief structures -- a new Kuhnian paradigm in the same spirit as
  the invention of the printing press...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is obvious from the "The Early History of Smalltalk" that Alan Kay
has a direct, emotional involvement in his subject matter (he says so
in fact).  What is equally obvious is that Kay's retrospective must be
bittersweet at best.  Smalltalk and the Alto were, at the time, the
avatar of "The Xerox PARC Design Philosophy".  The systems Apple went
on to produce would imperfectly capture this philosophy, and arguably,
later, jettison it altogether.&lt;/p&gt;

&lt;p&gt;In one anecdote, Kay relates showing a custom system (built in
Smalltalk) meant to facilitate non-expert "programming," to executives
from Xerox PARC.  This system was a kind of highly advanced
programming language meant to make human-machine interaction at a very
high level intuitive for non-expert users.  At one point during a
demonstration, a vice president, after an hour of working with the
system, realized he was programming.  What they accomplished, then,
was a keystone for a software system which Kay felt bridged the gap
between the numbers coursing through a CPU somewhere, and human
intuitive reasoning.&lt;/p&gt;

&lt;p&gt;Kay viewed programming as a natural aspect of human computer
interaction, and he designed his systems to make programming the
computer as easy and intuitive as creating a new Word Document or
browsing the web is on modern computers.  When Steve Jobs visited
Xerox PARC and saw the Alto, he brought more than just the user
interface to Apple Computers, he brought an entire philosophy of
personal computing.&lt;/p&gt;

&lt;h2&gt;HyperCard&lt;/h2&gt;

&lt;p&gt;The Xerox PARC philosophy can be seen in a variety of technological
lineages still discernible in the Apple universe.  Objective C, a
variant of the Smallktalk &lt;em&gt;language&lt;/em&gt;, though without its attendant
environment (the SmallTalk system), is still in use. Kay himself is
quick to point out HyperCard, an early and still incredibly popular
application environment, which encouraged user extension and
programmability in a language called "HyperTalk," which was inspired
by Smalltalk and the Alto, was a good realization of the Xeroc PARC
philosophy on the Mac.  &lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://www.mactech.com/articles/mactech/Vol.03/03.10/HyperCardProgramming/img006.gif" alt="Hypercard
 Screenshot" title="" /&gt;&lt;/center&gt;&lt;/p&gt;
&lt;p&gt;&lt;center&gt;
 A screenshot of Hypercard in action.  Linked from
 &lt;a href="http://www.mactech.com/articles/mactech/Vol.03/03.10/HyperCardProgramming/index.html"&gt;here.&lt;/a&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;HyperCard, like much of the work from this period, defies comparison
to modern software.  Although often described as a kind of hypertexual
rolodex, its "cards" could contain more than static information - they
could also contain user-created multimedia and interactive components.
Users would begin by adding cards for various pieces of information,
but then, say as a card representing sales data grew to require a
calculator, an interactive component for that purpose could be added.
These components were themselves added interactively from within
HyperCard.&lt;/p&gt;

&lt;p&gt;HyperCard, and the people using it, organically grew many applications
which left a permanent mark on computer history.  A particularly
telling fact is that the original version of the game Myst, a fantasy
adventure game, was a HyperCard App.  On the other side of the
Atlantic, figuratively and literally, Renault, a french car
manufacturer, used HyperCard to maintain its business inventory.
HyperCard became the program its users needed it to be because it was
open, extensible and encouraged user programming and interaction as a
fundamental use-case.  Even modern extensible software like Firefox
tends to separate use from extension development - the average user
might have no idea that Firefox supports user extension.  In
Hypercard, these features were "on the surface" of the design.&lt;/p&gt;

&lt;p&gt;HyperCard also illustrates some of the difficulties that might be
responsible for the gradual shift away from Xerox PARC-like open
models of personal computing.  According to rumor, the developer of
HyperCard, Bill Atkinson, allegedly&lt;sup&gt;1&lt;/sup&gt; gave the product to
Apple in 1987, with the understanding that it would be distributed for
free with each Mac. The program was an immediate success.  HyperCard
produced a tremendous amount of feedback from the community, but since
it was a free product, Apple wasn't sure how much internal resources
should be devoted to handling HyperCard development.&lt;/p&gt;

&lt;p&gt;Perhaps seeking a way of turning the HyperCard phenomenon into a
revenue stream, Apple eventually transferred HyperCard development to
a subsidiary company, which attempted to transform it into a
profitable business model.  HyperCard was no longer released for free,
but a locked down version, capable of playing, but not developing,
HyperCard Applications was freely available.  The "developer's
edition," recognizable as just Hypercard, was available for purchase.
In an effort to make HyperCard into a business model, Apple
inadvertantly had separated users into "developers" and "users."
This, combined with the development of work-alikes with more features,
seemed to destroy HyperCard's momentum, and, despite later attempts at
revival at Apple, the system fell out of use&lt;sup&gt;2&lt;/sup&gt;.&lt;/p&gt;

&lt;h2&gt;Waiting for the Dynabook&lt;/h2&gt;

&lt;p&gt;Alan Kay invented the laptop computer - at least he developed a
concept computer called The &lt;a href="http://thinkubator.ccsp.sfu.ca/Dynabook/"&gt;Dynabook&lt;/a&gt; which for all
intents and purposes was a modern laptop &lt;em&gt;and more&lt;/em&gt;.  He envisioned
that such a system, directed mostly at children (but usable by adults)
would run Smalltalk, and while its possible to build the conceptual
system Kay imagined in 1968 today, he still believes that the Dynabook
doesn't exist.  Although &lt;em&gt;tablet&lt;/em&gt; computers resemble the Dynabook
superficially, and the One Laptop Per Child project comes close, Kay
believes that his essential vision is unfulfilled.  Kay points out,
when asked about this, that the necessary technologies for a Dynabook
device are quite old, but that corporate and cultural practices simply
haven't caught up to using them appropriately.&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="http://dl.dropbox.com/u/1076954/File-Dynabook.png" alt="The Dynabook" title="" /&gt;&lt;/center&gt;&lt;/p&gt;
&lt;p&gt;&lt;center&gt;
Sketch of the Dynabook design (from Wikipedia.)&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Consider by contrast any one of Apple's iDevices.  The touch screen,
networking capability and user friendly design are reminiscent of the
Dynabook, but, whereas on a Smalltalk system one could click on any
widget and see and modify the source code, an iPad is essentially
completely locked down.  Not only does Apple require a license to
develop and sell software for the iDevices in their "App Store," but
to even develop, for personal use, software for your own device, a
separate "Developer's Kit" (and the Apple Computer to run it on) must
be acquired.  Whereas Smalltalk was designed from the bottom up to
facilitate programming for young and inexperienced users, the iPad
targets its development tools, which are arguably byzantine by the
standards of Smalltalk, to a relatively small group of developers.  On
top of that, software is only distributable after passing through an
often arbitrary and, in any case, secretive Apple review process.&lt;/p&gt;

&lt;p&gt;While the Dynabook was meant to be a device deeply rooted in the ethos
of active education and human enhancement, the iDevices are
essentially glorified entertainment and social interaction (and
tracking) devices, and Apple controlled revenue stream generators for
developers.  The entire "App Store" model, then works to divide the
world into developers and software users, whereas the Xerox PARC
philosophy was for there to be a continuum between these two
states. The Dynabook's design was meant to recruit the user into the
system as a fully active participant.  The iDevice is meant to show
you things, and to accept a limited kind of input - useful for 250
character Tweets and Facebook status updates, all without giving you
the power to upset Content Creators, upon whom Apple depends for its
business model.  Smalltalk was created with the education of
adolescents in mind - the iPad thinks of this group as a market
segment.&lt;/p&gt;

&lt;p&gt;HyperCard was, by comparison, much closer to the Dynabook ethos.  In a
sense, the iPad &lt;em&gt;is&lt;/em&gt; the failed "HyperCard Player" brought to
corporate fruition, able to run applications but completely unsuited
for developing them, both in its basic design (which prioritizes
pointing and clicking as the mechanism of interaction), in the
conceptual design of its software, and in the social and legal
organization of its software distribution system.&lt;/p&gt;

&lt;p&gt;It is interesting that at one point, Jobs (who could not be reached
for comment) described &lt;em&gt;his&lt;/em&gt; vision of computers as "interpersonal
computing," and by that standard, his machines are a success.  It is
just a shame that in an effort to make interpersonal engagement over
computers easy and ubiquitous, the goal of making the computer itself
easily engaging has become obscured.  In a world where centralized
technology like Google can literally give you a good guess at any
piece of human knowledge in milliseconds, its a real tragedy that the
immense power of cheap, freely available computational systems remains
locked behind opaque interfaces, obscure programming languages, and
expensive licensing agreements.&lt;/p&gt;

&lt;p&gt;The last 30 years have accustomed us to breakneck advancements in the
technology we use every day, and yet at the personal level these
advancements have been limited almost exclusively to communication and
entertainment - so much so that arguably the public lacks even the the
vocabulary to express what it is that modern computing could be doing
for them or what they could be doing with modern computing.
Spreadsheets are the closest most people get to "computing" with their
personal computers.  The electronic spreadsheet, which is itself an
adaptation of an analog technology, was conceptualized in 1961.&lt;/p&gt;

&lt;p&gt;If you ask Alan Kay about personal computing now, he is remarkably
upbeat.  In his view, the rapid development of technology simply
outpaces the ability of corporate and educational systems to adapt,
and this leads to a "pop culture" of sorts which dominates the culture
of computer use.  In other words, the divide between users and
programmers, or at least between the truly computer literate and the
merely casual computer user, isn't a top down phenomena imposed upon
the people by those in control of technology.  It is an inevitable
result of the rapid pace of development.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I think one of the main consequences of the inventions of personal
 computing and the world wide Internet is that everyone gets to be a
 potential participant, and this means that we now have the entire
 bell curve of humanity trying to be part of the action. This will
 dilute good design (almost stamp it out) until mass education can
 help most people get more savvy about what the new medium is all
 about. (This is not a fast process). What we have now is not
 dissimilar to the pop music scene vs the developed music culture (the
 former has almost driven out the latter -- and this is only possible
 where there is too little knowledge and taste to prevent it). Not a
 pretty sight.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Alan Kay is still pushing for more symbiotic conceptualization of
human/computer interaction, although he describes Smalltalk as part of
his "distant past".  He presently heads a non-profit
organization he co-founded called "Viewpoints Research Institute,"
whose purpose is to continue to consider the questions of educational
and personal computing.  We'd never have gotten the iPhone if it
hadn't been for his influence at Xerox PARC.  Maybe one day we'll be
lucky enough to get the Dynabook.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; Bill Atkinson is presently a nature photographer and
couldn't be reached for comment.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;2&lt;/sup&gt; However, HyperCard's influence is still felt today.
Last year, Dale Dougherty, editor of Make Magazine, wrote in wired
that the iPad needed a HyperCard-type application.  Tilestack, a web
based, HyperCard-a-like with a pay-to-distribute model, recently went
bust.  Squeak Smalltalk includes a "Morph," a kind of extendable
program, which is loosely based on HyperCard.  Although from a
parallel technological lineage altogether, Emacs, which is still in
wide use, and which the author used to write this article, resembles
HyperCard in many respects.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Copyright J. Vincent Toups 2011&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-6948066675736398013?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/6948066675736398013/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=6948066675736398013' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/6948066675736398013'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/6948066675736398013'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/07/duckspeak-vs-smalltalk.html' title='Duckspeak Vs Smalltalk'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-3946133368928314299</id><published>2011-06-22T05:02:00.000-07:00</published><updated>2011-08-26T16:36:53.666-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Smalltalk'/><category scheme='http://www.blogger.com/atom/ns#' term='Open Computing'/><category scheme='http://www.blogger.com/atom/ns#' term='Xerox PARC'/><category scheme='http://www.blogger.com/atom/ns#' term='Apple'/><category scheme='http://www.blogger.com/atom/ns#' term='Alan Kay'/><title type='text'>Cross Post (also on The New Yorker) about Smalltalk</title><content type='html'>A letter to the editor I wrote to the New Yorker is in this month.  See it &lt;a href="http://www.newyorker.com/magazine/letters/2011/06/27/110627mama_mail3"&gt;here&lt;/a&gt; or read it in replicate here:
&lt;br&gt;&lt;br&gt;

"When Jobs visited Xerox PARC and saw the company’s Alto computer, the windowed environment that so impressed him was running a system called Smalltalk, invented by Alan Kay, Dan Ingalls, and others. It was designed specifically with children in mind, to make examination, modification, and extension of the system easy for non-technical users. For example, in most Smalltalk systems, any visible object can be clicked on, and its source code inspected, copied, and modified. In contrast, the iPad and other iDevices available from Apple hide the source code for the system (by both legal and technical artifice). This barrier effectively destroys the system as a platform for exploratory, educational use. The Apple / Xerox story isn’t just one of a nimble young company snatching innovation from the jaws of an aging dinosaur. It is also the story of a corporate entity neutering a design philosophy meant to empower computer users and replacing it with one meant to sell them things."
&lt;br&gt;&lt;br&gt;

I'm writing a full length article on this subject which I may publish here.  Wait for it!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-3946133368928314299?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/3946133368928314299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=3946133368928314299' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3946133368928314299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3946133368928314299'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/06/cross-post-also-on-new-yorker-about.html' title='Cross Post (also on The New Yorker) about Smalltalk'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-2305593286409876428</id><published>2011-05-30T07:43:00.000-07:00</published><updated>2011-05-31T12:18:22.913-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='turtle graphics'/><category scheme='http://www.blogger.com/atom/ns#' term='racket'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorials'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><title type='text'>A Hyperturtle Monad Makes Pretty Pictures</title><content type='html'>&lt;h2&gt;In the Monads of Madness&lt;/h2&gt;

&lt;p&gt;One thing about monad tutorials is that they almost universally cover
simple monads - often the most complex monad you get is the Sequence
monad.  The Error or Maybe monads are also common examples, but in
those dynamic languages which most folks are familiar with there are
already facilities for dealing with Errors or Exceptions and so these
monads seem like trinkets.  Why bother?&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monad-turtles-polygon-figure-5.png" alt="Spirals" title="" /&gt;
(Keep reading, I promise we will make pretty pictures.)&lt;/p&gt;

&lt;p&gt;This seems to lead to the commonly expressed opinion that monads
aren't really useful in anything but Haskell, and that unless you live
there, there isn't any point in understanding them.  I can see this
perspective, particularly because unless you work in Lisp or want to
bang around with some heavyweight meta-programming tool like Metalua,
the syntax which makes using monads convenient isn't available
anyway.  &lt;/p&gt;

&lt;p&gt;Another part of me, however, thinks its too bad.  There are some
problems which find elegant expression by recourse to monadic
computation.  Monadic parser combinators are an example, I think.  I'd
always found parsing to be an unapproachable activity, requiring
special use tools and an understanding of and separate domain specific
languages to program them.  If one develops monadic parser combinators
in Lisp, with the appropriate binding expressions to clean up the
monadic details, parsing pops out as just one more programming task.  &lt;/p&gt;

&lt;p&gt;This post covers another example of a monad coming to the rescue when
a complex problem needs solving.  It is also an example of a monad
which I've never seen before (although it is some unnamed state
flavored monad transformer on the result of using the standard state
monad transformer on the sequence monad, I believe).  I'm going to try
and present things in the order they occured to me, and hopefully give
one a flavor of how one constructs a monad for a specific purpose,
rather than grabbing one off the shelf.  Also, for variety, we'll be
using Scheme, rather than Emacs Lisp.  While the code here is probably
a stone's throw away from any Scheme implementation, I'm working in
Racket.&lt;/p&gt;

&lt;h2&gt;Remember Turtle Graphics?&lt;/h2&gt;

&lt;p&gt;Back when I was but a wee lad in Louisiana, I went to nerd camp.
This was a program called ADVANCE for kids who wanted to spend three
weeks of their summer in intense study - you could get credit to skip
highschool classes.  For brainy kids, this was also a place where you
could expect to be around a lot of other brainy kids, and if that
didn't exactly lift the pall of awkwardness which hung over most of
us, it surely made us feel less strange for it all.  &lt;/p&gt;

&lt;p&gt;Anyway, I never took a programming class any of the summers I spent at
ADVANCE, but I do remember walking past the computer labs a couple of
times and seeing a little blinking triangle and a window beneath.
This is probably familiar to some other readers.  It was some
implementation of &lt;a href="http://en.wikipedia.org/wiki/Turtle_graphics"&gt;Turtle Graphics&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In a Turtle Graphics system one is given a special programming
language with most of the usual fixings and one other distinguishing
feature: the language includes primitive operators for controlling the
motion of that little triangle on the screen - the titular Turtle.
You can tell the turtle to move forward or to rotate in place or to
lift up or place the "pen" it carries in its little forelimb.  When
the pen is down, the turtle leaves a line on the screen.&lt;/p&gt;

&lt;p&gt;That first figure up there, for instance, was produced using the
library we'll build today.  The following code does it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(turtles-go
  (mlet* m-turtles
         ((new-pos (jump-to 150 150))
          (len     (setl 'len 2))
          (new-pos (n-times
                    (mlet* m-turtles
                      ((len (getl 'len))
                       (p   (move* len))
                       (r   (turn (/ pi 1.99)))
                       (len (setl 'len (+ len 2))))
                      (m-return p))
                    200)))
         (m-return new-pos)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here is the result again, since its not on the screen anymore:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monad-turtles-polygon-figure-5.png" alt="Spirals" title="" /&gt;&lt;/p&gt;

&lt;p&gt;You can imagine that with such a system, one can very quickly convince
the turtle to draw all sorts of spirographical images, telling the
turtle to repeat some sequence of commands which accumulate into
elaborate patterns.  Pretty neat stuff.&lt;/p&gt;

&lt;p&gt;At first I wanted to create a turtle graphics system in a purely
functional style (at least as purely functional as possible), as a
learning exercise.  But then I got to thinking:&lt;/p&gt;

&lt;h2&gt;What about a Hyperturtle?&lt;/h2&gt;

&lt;p&gt;Symmetry is an extremely important aesthetic principle which is
somewhat difficult to capture in Turtle Graphics.  It you want to
describe a system with bilateral symmetry using the turtle, you've got
to convince him to traverse the two sides of the figure in one go.
If you want to draw a Y, for instance, you have to say something like
(in pseudo code):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(face north)
(move 10)
(rotate 45)
(move 5)
(rotate 180) ; these lines describe 
(move 5)     ; backtracking 
(rotate 90) 
(move 5)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What if you could just move the Turtle up to the stem of the Y and
then tell it to rotate BOTH 45 degrees and -45 degrees and then tell
it to follow the rest of the instructions.  To satisfy your demands,
the Turtle must split itself into two turtles, but after that point
each turtle follows exactly the same directions, and you save yourself
a few commands and some complexity.&lt;/p&gt;

&lt;p&gt;An even simpler approach might be to tell the turtle to have two
different "helicities," that is, one turtle interprets &lt;code&gt;(turn 45)&lt;/code&gt; as
meaning to turn clockwise, and the other interprets it to mean
counter-clockwise.  Now any sequence of drawing commands will produce
a symmetric figure, all for free.  State dependent computations with
multiple possible outcomes?  &lt;/p&gt;

&lt;p&gt;Is anyone else's monad sense tingling!?&lt;/p&gt;

&lt;h2&gt;Monads in Scheme (a diversion)&lt;/h2&gt;

&lt;p&gt;If you've read any of my &lt;a href="http://dorophone.blogspot.com/2011/04/deep-emacs-part-1.html"&gt;other posts about
monads&lt;/a&gt;, you know how this is going to go.
We are going to define a macro to facilitate monadic computation.
Since this is a kind of advanced article, I'm going to assume you
have the basic idea about &lt;code&gt;bind&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt; more or less down.  If I
say that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(mlet* sequence-monad
  ((x '(1 2 3))
   (y '(4 5 6)))
 (m-return (+ x y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Expands to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(sequence-bind '(1 2 3) 
 (lambda (x)
  (sequence-bind '(4 5 6)
    (lambda (y)
      (sequence-return (+ x y))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should follow what I mean and why.  As it happens, this monadic
binding expression is defined in the racket library associated with
this article, in &lt;code&gt;functional/monads&lt;/code&gt;.  (Except there sequence-monad is
called &lt;code&gt;m-list&lt;/code&gt;).  The &lt;code&gt;mlet*&lt;/code&gt; defined in that library provides one
additional feature which I've found useful.  You can interpolate
monadic and non-monadic binding:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(mlet* sequence-monad
  ((x '(1 2 3))
   (non-monadically: 
     ((q (+ x 1))))
   (y '(4 5 6)))
 (m-return (+ q y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Causes &lt;code&gt;q&lt;/code&gt; to be bound only once per &lt;code&gt;x&lt;/code&gt;, rather than bound as a
monadic value.  We're going to be liberally using &lt;code&gt;mlet*&lt;/code&gt; throughout 
the rest of the article, so if you don't see how it works, check out
the &lt;a href="http://dorophone.blogspot.com/2011/04/deep-emacs-part-1.html"&gt;other monad tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Turtle Functions&lt;/h2&gt;

&lt;p&gt;Let's backtrack and think about the regular old turtle monad.  A basic
turtle might be implemented as a dictionary containing keys like
&lt;code&gt;position&lt;/code&gt;, &lt;code&gt;direction&lt;/code&gt;, &lt;code&gt;pen-status&lt;/code&gt;, etc.  In the land of
imperative, side-effect producing programs we'd probably just have a
global dictionary someplace holding these values.  In an object
oriented language we might do something obscene like a singleton
class.  Both would involve side effects.  Suppose we wanted to avoid
side effects forever (or at least until we had to talk to the hardware
handling rendering)?&lt;/p&gt;

&lt;p&gt;The obvious solution is to thread our turtle state through tons of
functions which non-destructively modify it.  Our turtle may as well
carry around a representation of whatever "drawing" it has done in its
mind, too.  Once we pass the state through all of the state-dependent
functions we want to, we'll extract that representation of the drawing
and pass it on to our GUI to draw.  The modification of the memory of
the screen will be the only conceptual side effect.&lt;/p&gt;

&lt;p&gt;What about querying the state?  We might want, for instance, to make
our turtle avoid certain regions of space, which means we'll need to
make inquiries into the state of the turtle, and we'd like to do so in
a standard way.  We'd also like to be able to combine queries with
modification.  What kind of function enables this behavior?  Well,
consider these:&lt;/p&gt;

&lt;p&gt;(Note for non-lispers, &lt;code&gt;cons&lt;/code&gt; simply &lt;code&gt;cons&lt;/code&gt;structs a &lt;em&gt;pair&lt;/em&gt; or tuple.)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require racket/dict)

(define (turtle-position^ turtle)
 (cons (dict-ref turtle 'position) turtle))

(define (try-to-move turtle amt)
 (let* ((pos (dict-ref turtle 'position))
        (dir (dict-ref turtle 'direction))
        (new-position 
         (move-point-in-dir pos dir amt)))
  (if (point-on-screen? new-position)
      (cons new-position (dict-set turtle 'position new-position))
      (cons #f turtle))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, in order to let our turtle-dependent function communicate
information to us other than a possibly modified state value, we will
let them return a &lt;code&gt;pair&lt;/code&gt; of things.  The first part of the pair is the
returned value, the second part is the possibly modified state.
&lt;code&gt;turtle-position^&lt;/code&gt; just fetches the turtle's position into the first
part of the pair, returning the unmodified turtle in the second part
of the pair.  &lt;code&gt;try-to-move&lt;/code&gt; actually uses the facility to return
values in a non-trivial way.  It calculates the new position of the
turtle, tests to see if it is on the screen and then returns the new
point and modified turtle if it is, and otherwise simply returns &lt;code&gt;#f&lt;/code&gt;
and the unmodified turtle.  A caller of this function can inspect the
first value to see if it succeeded without knowing too much about the
internal representation of the turtle itself.  That's hott.&lt;/p&gt;

&lt;p&gt;I want to point out something about the &lt;code&gt;try-to-move&lt;/code&gt; function at this
point.  Note that it takes more arguments than just the turtle.
Somewhere down the line we might want to reduce it to a function of
just the turtle-state.  In fact, this is such a common thing to do
that we should really rewrite the function in a &lt;code&gt;curried&lt;/code&gt; form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (try-to-move amt)
 (lambda (turtle)
   (let* ((pos (dict-ref turtle 'position))
          (dir (dict-ref turtle 'direction))
          (new-position 
           (move-point-in-dir pos dir amt)))
    (if (point-on-screen? new-position)
        (cons new-position (dict-set turtle 'position new-position))
        (cons #f turtle)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we've got a function which returns a compliant turtle function - a
function of just state and which returns a pair.  When we finally
figure out the machinery to combine such functions, we can just pass
in &lt;code&gt;amt&lt;/code&gt; before we pass the resulting state function to that
machinery, where it can be handled in a uniform way.&lt;/p&gt;

&lt;p&gt;Now that we've factored the functionality in this way, we can
recognize &lt;code&gt;try-to-move&lt;/code&gt; as a function which produces a
turtle-function.  This is an important class of functions which we'll
call "turtle combinators."  Think of such a function as a turtle
function "waiting" for a value before it commits to being a particular
turtle function.  &lt;code&gt;try-to-move&lt;/code&gt; represents the family of all movement
functions parametrized by &lt;code&gt;amt&lt;/code&gt;.  Each call to &lt;code&gt;try-to-move&lt;/code&gt; returns
such a function.&lt;/p&gt;

&lt;h2&gt;Thinking Monadically&lt;/h2&gt;

&lt;p&gt;Ok, so we've cooked up an ad-hoc formalism for dealing with our purely
functional turtle.  Lets relate this to a monad.  A monad relates the
following types of interest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Values
 These are just any old kind of value held "in" the monad.&lt;/li&gt;
&lt;li&gt;Monadic Values
 These are the "container" into which values go.&lt;/li&gt;
&lt;li&gt;Monadic Functions
 These are functions which take a Value and return a
 Monadic Value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Graphically:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monadic-types-of-interest-small.png" alt="monadic-types" title="" /&gt;&lt;/p&gt;

&lt;p&gt;So one way of thinking about monads that comes up a lot is that there
is a monad for every data structure.  Lists form a monad, Trees form a
monad, the maybe monad is just a data type consisting of two kinds of
things, &lt;code&gt;(Just &amp;lt;something&amp;gt;)&lt;/code&gt; and &lt;code&gt;(None)&lt;/code&gt; (in much the same way that a
list is either an element and a pointer to another list or nil).&lt;/p&gt;

&lt;p&gt;We can imagine that a function of state which returns a value and a
new state is a kind of data structure.  The analogy isn't that
tortured, really.  You can put an element "into" a state function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (insert-into-state x)
  (lambda (state) 
    (list x state)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns a state function that itself returns &lt;code&gt;x&lt;/code&gt; when you ask for it
with a state.  In terms of our types of interest: &lt;code&gt;x&lt;/code&gt; is a Value,
&lt;code&gt;(lambda (state) ...)&lt;/code&gt; is a Monadic Value and &lt;code&gt;insert-into-state&lt;/code&gt; is
a Monadic Function.&lt;/p&gt;

&lt;p&gt;You get data out of a state function by passing a state
in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (val-of pair) (car pair))
(define (state-of pair) (cdr pair))
(val-of ((insert-into-state 10) '() )) -&amp;gt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the sake of driving the analogy home,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(first (list 10)) -&amp;gt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Type note: 10 is a Value, &lt;code&gt;(list 10)&lt;/code&gt; is a Monadic Value.)&lt;/p&gt;

&lt;p&gt;is the equivalent code for the data structure called a list.  We put
things in, and take them out again.  So if these state functions are a
data structure, what is the associated monad?  Welp, a monad exists
when you have a &lt;code&gt;return&lt;/code&gt; and a &lt;code&gt;bind&lt;/code&gt; operation.  For the list monad
these are:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (list-return x) (list x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Type note: &lt;code&gt;list-return&lt;/code&gt; is a Monadic Function.)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (reduce f lst)
  (foldl f (car lst) (cdr lst)))

(define (list-bind monadic-val monadic-fun)
  (reduce append (reverse (map monadic-fun monadic-val))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It should be reasonably clear why &lt;code&gt;insert-into-state&lt;/code&gt; is
&lt;code&gt;state-return&lt;/code&gt;.  It constitutes the operation of placing any object
into the monad.  &lt;/p&gt;

&lt;p&gt;Graphically:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monadic-return-small.png" alt="return" title="" /&gt;&lt;/p&gt;

&lt;p&gt;What about bind?  Well, squint at the list-bind
operation and dig this: its essence is that it takes the monadic
function (a function which represents a monadic value "waiting" for
some input to compute itself) maps it over the values in the monad,
and collects each of them into a complete list.  Monadic functions
must take values and return monadic values, so the map returns a list
of lists, which we &lt;code&gt;reduce&lt;/code&gt; with &lt;code&gt;append&lt;/code&gt;.  If you squint, you can
pull out the following all right description for what bind does:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Monadic bind takes values out of the monad, applies a monadic
function to them, which results in lots of little monadic values.
These monadic values are combined, finally, into one big monadic
value, which is returned.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Graphically: &lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monadic-bind.png" alt="bind" title="" /&gt;&lt;/p&gt;

&lt;p&gt;List-bind takes the elements out of a list, applies a function to them
to generate a bunch of lists, and combines them together.&lt;/p&gt;

&lt;p&gt;What is the equivalent operation for the state monad?  Well, bind has
to return a monadic value, which is a state function in our case.  So
we know that bind will return a lambda.  And we know bind has to
extract the value from the state function passed in, and we know to do
that, bind will need the state value, so everything interesting has to
happen &lt;em&gt;inside&lt;/em&gt; the returned state function.  What has to happen in
there?&lt;/p&gt;

&lt;p&gt;Well, we will apply the monadic value to the state passed into the
function we are returning.  This generates a pair, the first part of
which is the "value" extracted from the monad.  We'll pass this value
to the monadic function, which will generate a new state function.  We
will then pass the new state function the second part of the
previously produced pair, which is the new state value.  The return
value of the new state function on the new-state will be the return
value of the function we return.&lt;/p&gt;

&lt;p&gt;Ok, I admit, that is a lot to follow.  Read the code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (state-bind state-fun state-fun-producer)
  (lambda (state)
    (let* ((pair (state-fun state))
           (val1 (val-of pair))
           (new-state (state-of pair))
           (new-state-fun 
            (state-fun-producer val1)))
      (new-state-fun new-state))))
(define state-return insert-into-state)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It is slightly more obvious as source code than text.  That has got to
mean something about language, the power of abstraction, etc.  The
state monad is already implemented in &lt;code&gt;functional/monads&lt;/code&gt;, so we can
write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require functional/monads
         racket/dict)

(define (st-fetch key . or-val)
  (if (not (empty? or-val))
   (lambda (state) 
    (list (hash-ref state key or-val) state))
   (lambda (state)
    (list (hash-ref state key) state))))

(define (st-set key val)
  (lambda (state)
    (list val (hash-set state key val))))

(define (point+ p1 p2)
  (map + p1 p2))

(define (dir-&amp;gt;vec dir)
  (list (cos dir) (sin dir)))

(define (point* p scalar)
  (map (lambda (x) (* x scalar)) p))

(define (move amount)
  (mlet* m-state
    ((position (st-fetch 'position '(0 0)))
     (direction (st-fetch 'direction 0))
     (non-monadically:
      ((new-position (point+ position 
                      (point* amount
                        (dir-vec direction))))))
     (_ (st-set 'position new-position)))
   (m-return new-position)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let's focus on the last function - &lt;code&gt;move&lt;/code&gt;.  The body is a monadic
expression in the state monad, which means that it returns a function
itself.  Specifically, it returns a function which takes a state and
returns a value/state pair.  Each expression in the binding part of
the &lt;code&gt;mlet*&lt;/code&gt; threads the value produced by each right hand expression
through state-bind and binds the variable in the left hand side to the
"value" part of the value returned by the state function in the
subsequent expressions.  The body of &lt;code&gt;mlet*&lt;/code&gt; provides a final location
to evaluate expression in the monad.  It must evaluate to a state
function, hence we use &lt;code&gt;m-return&lt;/code&gt;.  &lt;/p&gt;

&lt;h2&gt;The Hyperturtle Monad&lt;/h2&gt;

&lt;p&gt;Our mask slipped above and we just started calling the turtle monad
the state monad, which is exactly what it is.  Astute readers might
notice that it is also, for all intents and purposes, equivalent to
the parser monad (types notwithstanding, obviously) seen from a
different perspective.  &lt;/p&gt;

&lt;p&gt;The whole reason I'm writing this up is because I've never read a
really lengthy development of a &lt;em&gt;novel&lt;/em&gt; monad.  Most tutorials say
"you don't really have to understand how to write one, just how to use
them," but I think this is a shame.  As we'll see, the right monad
will enable an almost magical extension to the notion of turtle
graphics.&lt;/p&gt;

&lt;p&gt;Ok, so lets think some about the hyperturtle we want to create.  The
basic idea is that our turtle can be told to do multiple things &lt;em&gt;at
once&lt;/em&gt;.  That is, we want to be able to say something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(parallel-rotate (/ pi 2) (- (/ pi 2)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And have our system interpret it as a command which results in the
turtle splitting into &lt;em&gt;two&lt;/em&gt; turtles, one which rotates +pi/2 and the
other which rotates -pi/2.  In other words, we want to allow multiple
possible outcomes to each monadic function, which sounds a lot like
the list monad.  Monadic functions of the list monad take a single
value and return a list of values.  Depending on one's perspective, it
can be meaningful to view this list of results as a list of possible
return values over which subsequent computation in the monad is split:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(mlet* m-list
 ((x '(1 2 3))
  (y (list (+ x 1) (- x 1))))
 (m-return (list x y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Will evaluate to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;((1 2) (1 0) (2 3) (2 1) (3 4) (3 2))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;by this very logic.  For each possible value of &lt;code&gt;x&lt;/code&gt; we have multiple
values of &lt;code&gt;y&lt;/code&gt;.  We finally return all the combinations of these
possible bindings in a list of possible results.  &lt;/p&gt;

&lt;p&gt;But the hyperturtle monad can't be the list monad alone.  After all,
each expression in the binding has to depend on the previous turtle
state, and the result of a monadic expression needs to be like a state
function somehow.  Turns out there is a standard way of constructing
the monad we are interested in using monad-transformers, but we'll do
it the hard way for now so as to elucidate how one goes about it.&lt;/p&gt;

&lt;p&gt;Let's think carefully.  We still need state functions, but now they
are going to be returning "virtually" more than one possible outcome.
Since each possible outcome might also involve a separate modification
of the state, as well as a distinct value, it might make sense for our
functions to accept a single state but return a &lt;em&gt;list&lt;/em&gt; of value/state
pairs.  So, our monadic values are functions which take a state and
return a list of possible outcomes.&lt;/p&gt;

&lt;p&gt;What does return look like for this hypothetical monad?  Well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (pstate-return val)
  (lambda (state)
    (list (pair val state))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Bind is going to be a bit more difficult, but we can keep our head
straight by just remembering what bind always does.  It returns a new
monadic value, so its got to return a function which takes a state and
returns a list of pairs.  Inside that function, we've got to use the
state to produce a list of state/val pairs.  Then we need to use each
pair to produce the &lt;em&gt;next&lt;/em&gt; state function, which we apply to the
new-state to get a list of state/val pairs.  We collect all these
pairs back together into a single list of state/val pairs, which is
our return value.&lt;/p&gt;

&lt;p&gt;Might be clearer in code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (pstate-bind pstate-fun pstate-fun-producer)
  (lambda (state)
    (let* ((pairs (pstate-fun state))
           (new-results
             (map 
               (lambda (pair)
                 (let ((val (car pair))
                       (state (cdr pair)))
                  ((pstate-fun-producer val) state)))
               pairs)))
      (reduce append new-results))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, so there we go.  We've got a parallel state monad where we can
orchestrate multiple results.  But wait?  What if turtles need to
share information with one another or reference a "global" state?  For
instance, a turtle might wish to avoid an area if a line has already
been drawn there.  We need an additional level of &lt;em&gt;global&lt;/em&gt; state?  How
might we represent that?  &lt;/p&gt;

&lt;p&gt;One way is to consider functions which accept a &lt;em&gt;pair&lt;/em&gt; of states, one
representing a possible "local" state and the other representing the
shared "global" state.  These functions want to reflect that the local
state might split into many new local states (or none at all), and
they also might modify the global state.  So its got to return the
list of new local states, the new global state and the local results
of each possible outcome.  The functions can sensibly return this
collection of things by returning a pair consisting of a list of
value/local-state pairs and a global state.  Consider the return
operation for this new monad:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (ps-return val)
 (lambda (st-pair)
  (pair (list (pair val (car st-pair)))
        (cdr st-pair))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We return a state function which destructures the st-pair input,
returning a pair containing a list with one element, a val/local-state
pair, and the global state.&lt;/p&gt;

&lt;p&gt;Now the coup de grace, what is the bind operation for this crazy
monad?  Well, it is similar to the bind operation for the
sequence-of-state monad, &lt;em&gt;except&lt;/em&gt; we have to deal with the global
state.  Each monadic value needs to use the previously calculated
global state function.  The named let expression below amounts to a
fold operation over the global state.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (ps-bind monadic-val monadic-fun)
  (lambda (state-doublet)
    (let* ((results/global
            (monadic-val state-doublet))
      (let loop ((global-state (cdr results/global))
                 (pairs (car results/global))
                 (results-acc '()))
         (if (empty? pairs) (reduce append (reverse results-acc))
             (let* ((pair (car pairs))
                    (rest (cdr pairs))
                    (val (car pair))
                    (new-mv (monadic-fun val))
                    (new-res (new-mv (cons (cdr pair)
                               global-state))))
              (loop (cdr new-res)
                    rest
                    (cons (car new-res) results-acc)))))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, this is kind of complicated - but we've expressed it in just 17
lines of code.  Include return and we're up to 21 lines.  Once we
define a few "primitive" monadic functions/values we'll be able to
write incredibly concise and arguably expressive hyperturtle code.&lt;/p&gt;

&lt;h2&gt;Useful Hyperturtle Functions&lt;/h2&gt;

&lt;p&gt;We'll want to tell a branch of our computation to simply terminate.
This corresponds to returning no values whatever in the local part of
a state function, and leaving the global state unmodified:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (poof st-doublet)
  (cons '() (cdr st-doublet)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We're going to use the good old association list to represent our
local and global states.  That way we can add and remove arbitrary
data from them.  Just in case we want to use another table abstraction
later, though, we're going to use racket's dict library to wrap them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require racket/dict)

(define (setl key val) ; setl = set local
 (lambda (state-doublet)
   (cons (list (cons val
                     (dict-set (car state-doublet) key val)))
         (cdr state-doublet))))

(define (setg key val) ; setg = set global
 (lambda (state-doublet)
   (cons (list (cons val (car state-doublet)))
         (hash-set (cdr state-doublet) key val))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, so our pattern sense should be tingling.  We keep writing
functions which take some parameters now and return a state function.
Let's whip up some notation for this kind of common task.  Eventually
our library of combinators/monadic functions/monadic values will be
large enough that merely &lt;code&gt;mlet*&lt;/code&gt; will be sufficient to write most
functions of interest, but until then, its nice to have some syntactic
sugar.  Consider the hygienic macro:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require racket/match)

(define-syntax (define&amp;lt;turtles&amp;gt; stx)
  (syntax-case stx (^)
    [(_ (id lstate-id gstate-id) body ...)
     (syntax (define (id dblt)
               (match dblt
                 [(cons lstate-id gstate-id) 
                  (begin body ...)])))]
    [(_ (id lstate-id gstate-id ^ var) body ...)
     (syntax (define (id var)
               (lambda (dblt)
                 (match dblt
                   [(cons lstate-id gstate-id)
                    (begin body ...)]))))]
    [(_ (id lstate-id gstate-id ^ var ...) body ...)
     (syntax (define (id var ...)
               (lambda (dblt)
                 (match dblt
                   [(cons lstate-id gstate-id)
                    (begin body ...)]))))]
    [(_ id val)
     (syntax (lambda (dblt)
               (match dblt
                 [(cons lstate-id gstate-id) 
                  (cons (list (cons val lstate-id)) gstate-id)])))]))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This lets us write the above two functions like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define&amp;lt;turtles&amp;gt; (setl local global ^ key val)
  (cons (list (cons val (dict-set local key val))) global))

(define&amp;lt;turtles&amp;gt; (setg local global ^ key val)
  (cons (list (cons val local) (dict-set global key val))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ahhhhhh.  Much more concise.  For those not too familiar with
syntax-rules style macros, this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define&amp;lt;turtles&amp;gt; (setg local global ^ key val)
  (cons (list (cons val local) (dict-set global key val))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;expands to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (setg key val)
 (lambda (state)
  (match state
   [(cons local global)
    (begin (cons (list (cons val local) (dict-set global key
 val))))])))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;^&lt;/code&gt; character is just a piece of visual lint meant to help
distinguish between the fact that the first two arguments are curried
off from the rest of the arguments.&lt;/p&gt;

&lt;p&gt;We've now constructed enough machinery to understand the actual code
used in my implementation.  There is just one note I should make
before we switch over to looking at actual code.  Since we are
returning a list of pairs &lt;em&gt;and&lt;/em&gt; we're using association-lists, which
are just lists, after all, and lists are made of pairs, I felt that we
were just overburdening &lt;code&gt;cons&lt;/code&gt; a little too much to make the code easy
to debug.  Hence, instead of using &lt;code&gt;pairs&lt;/code&gt; where I wasn't using a
list, I created a new structure:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define doublet (a b))
(doublet-a (doublet 10 11)) -&amp;gt; 10
(doublet-b (doublet 'x 'y)) -&amp;gt; 'y
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So with doublets, &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define&amp;lt;turtles&amp;gt; (setg local global ^ key val)
  (cons (list (cons val local) (dict-set global key val))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Becomes&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define&amp;lt;turtles&amp;gt; (setg local global ^ key val)
  (doublet (list (doublet val local) (dict-set global key val))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This change gives the Racket environment a little hand when it comes
to checking types.  &lt;/p&gt;

&lt;p&gt;I promise, things are about to get RAD.  We just need a few more
primitives:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define&amp;lt;turtles&amp;gt; (getg lo g ^ symbol)
  (doublet (list (doublet (dict-ref g symbol #f) lo))
        g))

(define&amp;lt;turtles&amp;gt; (getl lo g ^ symbol)
  (doublet (list (doublet (dict-ref lo symbol #f) lo))
        g))

(define&amp;lt;turtles&amp;gt; (getl-or lo g ^ symbol or-val)
  (doublet (list (doublet (dict-ref lo symbol or-val) lo))
        g))

(define&amp;lt;turtles&amp;gt; (getg-or lo g ^ symbol or-val)
  (doublet (list (doublet (dict-ref g symbol or-val) lo))
        g))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These primitives let us fetch values from either the local state or
the global one.  &lt;/p&gt;

&lt;p&gt;Ok, now here is the cool step.  Consider the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define&amp;lt;turtles&amp;gt; (split-setl lo g ^ key vals)
  (doublet 
   (map (lambda (val)
          (doublet val (dict-set lo key val)))
        vals)
   g))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function lives in the turtle-state monad, evidently enough.  It
takes a key and a &lt;em&gt;list&lt;/em&gt; of values in &lt;code&gt;vals&lt;/code&gt;.   It then returns a
&lt;em&gt;list of&lt;/em&gt; new local states where the key has been set to each of the
values in turn.  Any further state function when used within the monad
will now execute in multiple contexts, one for each possible
assignment of the &lt;code&gt;key&lt;/code&gt;.  &lt;/p&gt;

&lt;h2&gt;Representing the Drawing&lt;/h2&gt;

&lt;p&gt;All turtles will share a global representation of the drawing, which
we will store in a global &lt;code&gt;key&lt;/code&gt; as a series of objects to draw.
Hence, the function to add a line to a drawing might be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(struct point (x y))
(struct line (p1 p2))

(define (add-line x1 y1 x2 y2)
  (mlet* m-turtles 
         ((lines (getg 'draw-these))
          (lines (setg 'draw-these (cons (line 
                                          (point x1 y1)
                                          (point x2 y2))
                                         (if lines lines '())))))
         (m-return lines)))

(define (add-line-pts p1 p2)
  (mlet* m-turtles 
         ((lines (getg-or 'draw-these '()))
          (lines (setg 'draw-these (cons (line 
                                          p1
                                          p2)
                                         lines))))
         (m-return lines)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These functions also provide an example of using the turtle monad with
monadic notation to write monadic functions.  In &lt;code&gt;add-line&lt;/code&gt; we write a
function which takes four values and returns a state function produced
by the &lt;code&gt;mlet*&lt;/code&gt; form using the &lt;code&gt;turtles-monad&lt;/code&gt; (incidentally, this is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define m-turtles
  (list (cons 'bind m-bind)
        (cons 'return m-return)
        (cons 'plus #f)
        (cons 'zero poof)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;).&lt;/p&gt;

&lt;p&gt;Monads are simply dictionaries containing the appropriate operators.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;add-line&lt;/code&gt; fetches the value of the global key &lt;code&gt;draw-these&lt;/code&gt; or an
empty list, conses a new line onto that list, and finally sets the
value of that key with &lt;code&gt;setg&lt;/code&gt;.  To finally draw our drawing, we'll
call our monadic function on an initial state (&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define init-state (doublet '() '()))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;), take apart the returned state doublet, grab the &lt;code&gt;draw-these&lt;/code&gt; data
from the global state, and pass it to Racket's GUI library.  This
tutorial is already too long to provide all the code to do that, but
you can download it all from my &lt;a href="https://github.com/VincentToups/racket-lib/blob/master/roguelikes/turtles-monad.md"&gt;github&lt;/a&gt;.  The
interface is easy enough.  The function called &lt;code&gt;turtles-go&lt;/code&gt; takes a
turtle-state fun and applies it to an empty initial state, pulls out
the drawing after the function is finished, and draws it.&lt;/p&gt;

&lt;p&gt;Let's practice using the turtle monad to draw something.  How about a
regular n-gon?  A regular n-gon is defined by an edge length and a
number of sides.  A regular n-gon's interior angles are all the same
and add up to (n-2)&lt;em&gt;pi radians, which means they must each be
((n-2)&lt;/em&gt;pi)/n radians.  To draw such a figure, we can just move by and
edge length, rotate by the appropriate angle and repeat the process
until we reach the number of sides the polygon has.  &lt;/p&gt;

&lt;p&gt;We'll need a move and a turn function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (turn amount)
  (mlet* m-turtles
         ((helicity (getl-or 'helicity 1))
          (facing (getl-or 'facing (/ pi 2)))
          (new-facing (setl 'facing
                            (+ (* amount helicity) facing))))
         (m-return new-facing)))

(define (move amt)
  (mlet* m-turtles
         ((pos (getl-or 'pos (point 0 0)))
          (facing (getl-or 'facing (/ pi 2)))
          (non-monadically: 
           ((facing-vector 
             (direction-&amp;gt;vector facing))))
          (new-pos (setl 'pos (point+ 
                               pos (scale facing-vector amt)))))
         (m-return new-pos)))

(define (move-line amt)
  (mlet* m-turtles
         ((pos (getl-or 'pos (point 0 0)))
          (new-pos (move amt)))
         (add-line-pts pos new-pos)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The turtle's current direction is stored in the local variable
&lt;code&gt;facing,&lt;/code&gt; its current position stored in &lt;code&gt;pos&lt;/code&gt;.  &lt;code&gt;turn&lt;/code&gt; also uses a
value called &lt;code&gt;helicity&lt;/code&gt; to determine &lt;em&gt;which way&lt;/em&gt; the turtle turns.
For now, we'll always use the default value of &lt;code&gt;1&lt;/code&gt;.  &lt;code&gt;move-line&lt;/code&gt; just
does the appropriate move and adds a line between the previous and
new-current point.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (n-times fun n)
  (if (= n 1) fun
      (mlet* m-turtles
             ((_ fun))
             (n-times fun (- n 1)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;n-times&lt;/code&gt; takes a monadic function and causes it to execute &lt;code&gt;n&lt;/code&gt;
times.  Ok, here is our polygon drawing routine:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (n-gon n edge-len)
  (let* ((int-ang (/ (* (- n 2) pi) n))
         (ext-ang (- pi int-ang)))
    (n-times
     (mlet* m-turtles
            ((p (move-line edge-len))
             (a (turn ext-ang)))
            (m-return p))
     n)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And here is how we use it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(turtles-go
 (mlet* m-turtles
        ((p (jump-to 150 150))
         (p2 (n-gon 6 30)))
        (m-return p2)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And here is the result:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monad-turtles-polygon-figure.png" alt="polyfig" title="" /&gt;&lt;/p&gt;

&lt;p&gt;So we're still in regular old turtle territory.  Can we see an example
of hyper turtle action?  Well, what would you think the following will
produce?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(turtles-go
 (mlet* m-turtles
        ((p (split-setl
             'pos
             (list (point 150 150)
                   (point 200 150)
                   (point 150 200)
                   (point 200 200))))
             (p2 (n-gon 6 30)))
            (m-return p2)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Instead of just &lt;code&gt;jump&lt;/code&gt;ing &lt;code&gt;to&lt;/code&gt; the single point &lt;code&gt;150 150&lt;/code&gt; we've
instructed our turtle to jump &lt;em&gt;in parallel&lt;/em&gt; to four different points,
and then to cruise along drawing a polygon just as before.  What is
the result?&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monad-turtles-polygon-figure-2.png" alt="polyfig2" title="" /&gt;&lt;/p&gt;

&lt;p&gt;We can do some pretty neat things in short order by changing the way
motion commands are interpreted by the turtle.  Remember the helicity
option?  Helicity changes the way individual turtles interpret turn
commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(turtles-go
 (mlet* m-turtles
        ((p (split-setl
             'pos
             (list (point 150 150)
                   (point 200 150)
                   (point 150 200)
                   (point 200 200))))
         (h (split-setl
             'helicity '(-1 1)))
          (p2 (n-gon 6 30)))
         (m-return p2)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Results in:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monad-turtles-polygon-figure-3.png" alt="p3" title="" /&gt;&lt;/p&gt;

&lt;p&gt;We can do tons of things by modifying helicity, but lets do one more
different trick:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(turtles-go
 (mlet* m-turtles
        ((p (jump-to 150 150))
         (n (split-setl
             'n '(3 5 7 9 11 13)))
          (p2 (n-gon n 30)))
         (m-return p2)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Results in:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://s3.amazonaws.com/VincentToupsScreencasts/monad-turtles-polygon-figure-4.png" alt="p4" title="" /&gt;&lt;/p&gt;

&lt;p&gt;Eee gads!  We've drawn the first six prime n-gons all at once!  I
leave you to consider what could be done with the following
combinator:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (move* amt)
  (mlet* m-turtles 
         ((jitter-mag (getl-or 'jitter-mag 0))
          (non-monadically:
           ((amt (+ amt (random-normal 0 jitter-mag)))))
          (pos (getl-or 'pos (point 0 0)))
          (_ (move amt))
          (pos2 (getl 'pos))
          (f (getl-or 'move*-fun (lambda () add-line-pts)))
          (_ (f pos pos2)))
         (m-return pos2)))
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Musings on Monads&lt;/h2&gt;

&lt;p&gt;Besides drawing pretty pictures, what are we trying to accomplish
here?  Well, if you go sniffing around the internet about monads,
you'll often hear that monads "don't work" programming languages
besides Haskell.  Reasons proferred include "it is easier to use
side-effects," "there isn't syntactic support," and "you need static
typing."  I'm not a Haskell programmer, so I can't speak too
authoritatively on the subject static typing, but I think that at
least in Lisp dialects, where the second argument can be easily
dispensed with, the first argument doesn't hold water.&lt;/p&gt;

&lt;p&gt;If you look up the Forth pages on the C2 Wiki you'll eventually come
across the programmer's meme: &lt;a href="http://c2.com/xp/DoTheSimplestThingThatCouldPossiblyWork.html"&gt;Do the Simplest Thing That Could
Possibly Work&lt;/a&gt;.  This means what it says: when you've got a
problem, don't worry about picking an optimal solution, just do the
simplest thing that could possibly work.  &lt;/p&gt;

&lt;p&gt;In my mind, this was the simplest implementation of a hyperturtle
graphics system. &lt;/p&gt;

&lt;p&gt;Now, I know that is going to seem pretty crazy to a person just trying
to wrap their heads around monads.  Surely, you'll object, this can't
be the simplest possible implementation!  To that, I'd say go back and
look at the implementation of &lt;code&gt;return&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt; which together add
up to 21 or so lines of code.  If we had the right monad transformers
at our disposal, we could write the same monad in one line of code!
Assuming we had a handle on each of the monads in question, what we'd
have accomplished is the creation of a sub-langage with entirely novel
semantics in a few tokens.  Add the &lt;code&gt;mlet*&lt;/code&gt; syntax (itself about 20
lines of syntax-case macro) and the minimal subset of &lt;code&gt;turtle&lt;/code&gt;
primitive functions, and we are suddenly looking at a
domain-specific-language &lt;em&gt;on steroids&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;But this is a very simple solution!  You write a short bind and return
function, use the already "standard" monadic syntax, and inside those
expressions, you've got a whole new programming language.  Because
everything is purely functional, up to this point you've made &lt;em&gt;no hard
design decisions&lt;/em&gt;.  You haven't declared any global variables, thought
about how to represent the persistent state of anything.  The monad
provides a clean bed to write code in - and you don't need much code
to get somewhere interesting.&lt;/p&gt;

&lt;p&gt;Performance might be a problem - but I'd suggest that as a prototype
system the monad provides excellent semantics for any more efficient
implementation to target.  This is enhanced because of the very
compactness of the implementation!  You know what your system does
when you design it this way, because all the important behavior is
specified in one place.&lt;/p&gt;

&lt;p&gt;Secondly, I hope the article has provided an example of the process by
which monads are constructed and used that isn't particularly
artificial.  Most monad tutorials provide ad-hoc examples which don't
do anything interesting, and use pre-fab monads.  I hope this
development of a free-form monad for a novel problem is interesting.  &lt;/p&gt;

&lt;p&gt;And that is about it!&lt;/p&gt;

&lt;p&gt;As always, the code for this project is hosted on &lt;a href="https://github.com/VincentToups/racket-lib/blob/master/roguelikes/turtles-monad.md"&gt;my
github&lt;/a&gt;.  Comments, critiques, corrections are
extremely welcome!&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-2305593286409876428?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/2305593286409876428/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=2305593286409876428' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/2305593286409876428'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/2305593286409876428'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/05/hyperturtle-monad-makes-pretty-pictures.html' title='A Hyperturtle Monad Makes Pretty Pictures'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-677838871298315523</id><published>2011-05-25T17:03:00.001-07:00</published><updated>2011-05-26T18:47:15.599-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='parsers'/><category scheme='http://www.blogger.com/atom/ns#' term='awesome?'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Monadic Parser Combinators in Elisp, A Screencast</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-GYJu10jKqEw/Td2bEbUSzkI/AAAAAAAAAG0/m7t15oHOLWc/s1600/haskell-curry-says.png"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 320px; height: 279px;" src="http://3.bp.blogspot.com/-GYJu10jKqEw/Td2bEbUSzkI/AAAAAAAAAG0/m7t15oHOLWc/s320/haskell-curry-says.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5610811211128163906" /&gt;&lt;/a&gt;

&lt;p&gt; &lt;h6&gt;Edit: Hosting back up, split over Ubuntu One and Amazon S3.  Should be good for awhile!  Sorry for any inconvenience.&lt;/h6&gt;&lt;/p&gt;  

&lt;p&gt;So I recently gave a talk to the &lt;a href="http://trifunc.org/"&gt;Triangle Area Functional Programmer's Group&lt;/a&gt; about Monadic Parser Combinators.  If you don't know what these are, they essentially constitute a formalism for creating purely functional parsers by building them up from simpler ones.  I really dig their minimal elegance, pure functionality and the fact that they are a monad that isn't the list or maybe monad, which is always what people use as examples.  Oh, and the whole thing is in Emacs Lisp, which I think has lots of didactic benefits.&lt;/p&gt;
&lt;br&gt;
&lt;h2&gt;Oh Where Can I Get A Screencast of this Fascinating Talk??&lt;/h2&gt;
&lt;br&gt;
&lt;p&gt;If you'd like to see the talk, I've create a screencast of it in either &lt;a href="http://ubuntuone.com/p/vqU/"&gt;AVI&lt;/a&gt; or &lt;a href="https://s3.amazonaws.com/VincentToupsScreencasts/talk.ogv"&gt;OGV&lt;/a&gt; format.  If you can use Ogg Video (and you should) that version is a lot cleaner looking and easier to read.&lt;/p&gt;  

&lt;p&gt;I'd like to point everyone to Drew Crampsie's &lt;a href="http://common-lisp.net/~dcrampsie/smug.html"&gt;SMUG Library&lt;/a&gt; for Common Lisp.  Although I develop the MPCs from the ground up in the screencast, the basic shape of the library owes itself to Drew's very nice implementation notes on the library.  And SMUG is more powerful in a variety of ways too.  Check it out.&lt;/p&gt;

&lt;p&gt;As usual, the whole talk (which is a series of elisp files) is available on my &lt;a href="https://github.com/VincentToups/emacs-utils/tree/master/parser-pres"&gt;github&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-677838871298315523?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/677838871298315523/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=677838871298315523' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/677838871298315523'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/677838871298315523'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/05/monadic-parser-combinators-in-elisp.html' title='Monadic Parser Combinators in Elisp, A Screencast'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-GYJu10jKqEw/Td2bEbUSzkI/AAAAAAAAAG0/m7t15oHOLWc/s72-c/haskell-curry-says.png' height='72' width='72'/><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-3703150982257395302</id><published>2011-04-25T18:51:00.000-07:00</published><updated>2011-04-29T07:21:56.180-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reader Macros'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy'/><category scheme='http://www.blogger.com/atom/ns#' term='joy'/><category scheme='http://www.blogger.com/atom/ns#' term='factor'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='stack languages'/><category scheme='http://www.blogger.com/atom/ns#' term='forth'/><category scheme='http://www.blogger.com/atom/ns#' term='Common Lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='clojure'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Emacs Lisp Curios (some useful, some fanciful)</title><content type='html'>&lt;h1&gt;A Quick Overview of the Curios in this Library&lt;/h1&gt;

Since I've been place on the planet emacs blog roll, I thought I'd do a quick summary of the things it might be interested in that I've worked on.  Code is available on &lt;a href="https://github.com/VincentToups/emacs-utils"&gt;my github&lt;/a&gt;.

&lt;h2&gt;Clojure-Style Destructuring Bind/Function Definitions&lt;/h2&gt;

&lt;p&gt;This library, &lt;code&gt;defn.el&lt;/code&gt;, supports an approximation of Clojure's
destructuring bind syntax.  If you are familiar with Clojure, the
following code will be recognizable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'defn)
(defn prod 
  ([[el &amp;amp; tail :as lst] acc]
   (if lst (recur tail (* el acc))
       acc))
  ([lst] (prod lst 1)))
(prod '(1 2 3 4)); -&amp;gt; 24
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above code demonstrates the declaration of a function, &lt;code&gt;prod&lt;/code&gt;
which has two arities: the number of arguments determines which body
is executed.  If one arg is passed in, prod calls itself with the
appropriate initial value for its accumulator.  If two arguments are
provided, it uses a &lt;code&gt;recur&lt;/code&gt; expression to call itself repeatedly,
calculating the produce of a list passed in.  This is done without
growing the stack.  You can recur within a single body perpetually.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;defn&lt;/code&gt; and its anonymous cousin &lt;code&gt;fn&lt;/code&gt; support destructuring on hash
tables and association lists where each element in the list is a
proper list:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn dst-demo [[:: x :x y :y]]
  (list x y))

(dst-demo (tbl! :x 10 :y 11)); -&amp;gt; (10 11)
(dst-demo '((:x 40) (:y 50))); -&amp;gt; (40 50)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Default values may be provided for both sequential and table forms
using the &lt;code&gt;:or&lt;/code&gt; keyword (clojure doesn't support &lt;code&gt;:or&lt;/code&gt; for sequences).
Destructuring can be nested arbitrarily deeply as well.  There are
also &lt;code&gt;let&lt;/code&gt; forms which provide the same destructuring for &lt;code&gt;let&lt;/code&gt;-like
situations.  All these forms expand to lexical-closures, but there are
forms like &lt;code&gt;defn_&lt;/code&gt; which are identical but create dynamic scopes
instead.&lt;/p&gt;

&lt;p&gt;As with most heavy macro magic, you can't really use this library
without byte compiling, but when this is done, it can be reasonably
zippy. &lt;/p&gt;

&lt;h2&gt;Standalone Recur&lt;/h2&gt;

&lt;p&gt;I really like the &lt;code&gt;recur&lt;/code&gt; feature from Clojure.  I like it so much I
factored it out of the &lt;code&gt;defn&lt;/code&gt; implementation so that it can be used in
situations where a lighter touch is required.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'recur)
(recur-defun* prod (lst &amp;amp;optional (acc 1))
  (if (empty? lst) 
      acc
      (recur (cdr lst) (* acc (car lst)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, this won't blow the stack.  The lambda list supports the whole
&lt;code&gt;cl&lt;/code&gt; style lambda list.  Also provided is a &lt;code&gt;let&lt;/code&gt; form which can recur
on itself, &lt;code&gt;recur-let&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Multi-methods&lt;/h2&gt;

&lt;p&gt;Another Clojure-inspired feature is multi-methods.  These are a kind
of proto-multiple dispatch "object" system, by which I mean they are
slightly more general than any concrete object system.  You can use
them to implement a single-dispatch object system-like functionality,
for instance:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'multi-methods)
(defmulti volcalize-n-times (lambda (&amp;amp;rest args)
                          (pseudo-class-of (car args)))
  "Volcalization multi-method.") ;Must be executed before defunmethod

(defunmethod volcalize-n-times :cat (thing n)
  (loop for i from 1 to n do (print "Meow")))

(defunmethod volcalize-n-times :dog (thing n)
  (loop for i from 1 to n do (print "Woof")))

(defunmethod volcalize-n-times :human (thing n)
  (loop for i from 1 to n do (print "Lorem ipsum dolor sit
  amet, consectetur adipiscing elit. Nulla sed sapien ligula. Sed
  luctus cursus consequat. Morbi egestas magna auctor dui
  sagittis bibendum. Ut in felis sit amet eros eleifend ultricies
  gravida id ligula. Suspendisse potenti. Nulla semper porttitor
  massa, sed feugiat mauris tempor nec.")))

(defun pseudo-class-of (val)
  (gethash :pseudo-class val :thing))

(volcalize-n-times (tbl! :pseudo-class :cat :name 'tibald) 10)
; prints "meow" 10 times.
(volcalize-n-times (tbl! :pseudo-class :human :name 'vincent) 2)
; prints lorem-ipsum fragment twice.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Multimethods match their arguments via &lt;code&gt;isa?&lt;/code&gt; predicate, which can be
extended with functions like &lt;code&gt;derives&lt;/code&gt;.  For instance:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;($ :cat derives-from :quadraped)
($ :dog derives-from :quadraped)
($ :human derives-from :biped)
($ :emu derives-from :biped)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;$&lt;/code&gt; is a simple infix macro that swaps the function position
with the first argument position in the top level of the subsequent
forms.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmulti n-legs 
  (lambda (&amp;amp;rest args)
   (pseudo-class-of (car args)))
  "Number of legs multi-method")

(defunmethod n-legs :biped (o) 2)
(defunmethod n-legs :quadraped (o) 4)

(n-legs (tbl! :pseudo-class :human)) ;-&amp;gt; 2
(n-legs (tbl! :pseudo-class :cat)) ;-&amp;gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By writing the appropriate dispatch function you can create many kinds
of crazy object systems.  Dispatch is cached, so if the &lt;code&gt;derives&lt;/code&gt;
hierarchy doesn't change, dispatch is fast after the first method
call.&lt;/p&gt;

&lt;h2&gt;Partial Application&lt;/h2&gt;

&lt;p&gt;Particularly because emacs lisp is dynamically scoped, its handy to have
functions to create functions which handle lexical static for you.  If
you &lt;code&gt;(require 'functional)&lt;/code&gt;, you'll have access to these functions,
which make point free programming more fun:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(mapcar (par #'+ 5) '(1 2 3));-&amp;gt; (6 7 8)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;par&lt;/code&gt; partially applies a function on the right.  &lt;code&gt;pal&lt;/code&gt; does the same
on the left.  Both always return a function, so you can partially
apply more arguments than a function has, in which case when you call
the result, you'll get an error.  Considering that elisp functions
have variable and unlimited arity, this seemed like the most idiomatic
accomidation.  There are also some other interesting things in
&lt;code&gt;functional.el&lt;/code&gt;, though it probably should be called &lt;code&gt;point-free.el&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;With-stack&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;with-stack.el&lt;/code&gt; is an emacs lisp embedded stack language in the mode
of Factor or Joy.  It looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(||| 4 4 + ) ;-&amp;gt; 8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The macro &lt;code&gt;|||&lt;/code&gt; introduces a stack language form.  Subsequent objects
are interpreted as in a stack languages, with atomic forms pushing
themselves onto the stack and symbols acting as "words" which
manipulate the stack.  The file &lt;code&gt;stack-words.el&lt;/code&gt; defines many basic
stack words, but you can define your own using the &lt;code&gt;word:&lt;/code&gt; word.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(||| word: plus-four 4 + end: 10 plus-four ) ;-&amp;gt; 14
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;|||&lt;/code&gt; returns whatever is on the top of the stack at the end of
evaluation.  The stack is discarded between invocations of &lt;code&gt;|||&lt;/code&gt;.  The
language supports the notion of regular "words" and parsing or
immediate words, which are executed when they are encountered during
the compilation scan of the stack language segment.  An immediate word
is written in the stack language itself, and sees the "future" stream
of tokens as its stack.  It may manipulate that stack as it sees fit
before terminating, at which point, compilation continues till the
next immediate word or the end of the stack segment.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(||| immediate-word: -lisp-val: '1&amp;gt;eval swap end:)
(||| -lisp-val: '(+ 1 1) ) ;-&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The language supports Factor/Joy style quotations and &lt;code&gt;stack-words&lt;/code&gt;
defines the most common combinators (&lt;code&gt;if&lt;/code&gt;,&lt;code&gt;loop&lt;/code&gt;,&lt;code&gt;bi&lt;/code&gt;, etc).  You can
call emacs functions using a bit of sugar (shown above).  The syntax
&lt;code&gt;2&amp;gt;concat&lt;/code&gt;, for instance, means "call concat with two values from the
stack."  These glue-words are assumed to return 1 value, so don't
forget to &lt;code&gt;drop&lt;/code&gt; nils.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;That is a bunch of the crazy stuff I have been working on.  There are
other projects in a half-way state of completion.  All of these
projects are on &lt;a href="https://github.com/VincentToups/emacs-utils"&gt;my github&lt;/a&gt;, as is this document.  They depend on my
giant pile of utils library &lt;code&gt;utils.el&lt;/code&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-3703150982257395302?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/3703150982257395302/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=3703150982257395302' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3703150982257395302'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/3703150982257395302'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/04/emacs-lisp-curios-some-useful-some.html' title='Emacs Lisp Curios (some useful, some fanciful)'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-4615368830451387495</id><published>2011-04-22T08:13:00.000-07:00</published><updated>2011-04-27T06:06:15.499-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='lexical-let'/><category scheme='http://www.blogger.com/atom/ns#' term='scope'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic scope'/><category scheme='http://www.blogger.com/atom/ns#' term='the el camino of lisps'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Lexical-let Corner Case</title><content type='html'>&lt;p&gt;Emacs 24 will have actual lexical binding, but until that becomes the standard, we've got to satisfy ourselves with &lt;code&gt;cl.el&lt;/code&gt;'s &lt;code&gt;lexical-let&lt;/code&gt; form, a codewalker which does some magical things to simulate lexical scope.  Today I discovered the following corner case when combining lexical and dynamic scope on variables with the same name.  Consider:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq x "Hi there")
(defun return-x () x)
(return-x) -&amp;gt; "Hi there"
(let ((x "Yo."))
  (return-x)) -&amp;gt; "Yo." ; Dynamic scope in action, 'x is shadowed.
(lexical-let ((x "Yo."))
  (return-x)) -&amp;gt; "Hi there." 
; lexical-let doesn't create a dynamic binding, so x
; is not shadowed
(lexical-let ((x "Yo."))
  (let ((x x))
    (return-x))) -&amp;gt; "Hi there", but should be "Yo."
; This is unexpected behavior.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What is the problem?  The &lt;code&gt;let&lt;/code&gt; expression in the last example is rewritten by the codewalker, which replaces the x in the binding form with a hidden name.  This prevents &lt;code&gt;x&lt;/code&gt; from being dynamically bound, even though the &lt;code&gt;let&lt;/code&gt; should shadow the &lt;code&gt;x&lt;/code&gt; inside &lt;code&gt;return-x&lt;/code&gt;.  I am not sure if there is a way around this that isn't incredibly baroque.  Probably best to just be aware of it.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-4615368830451387495?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/4615368830451387495/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=4615368830451387495' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/4615368830451387495'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/4615368830451387495'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/04/lexical-let-corner-case.html' title='Lexical-let Corner Case'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-7819365718841464578</id><published>2011-04-16T13:32:00.000-07:00</published><updated>2011-04-18T06:16:18.835-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='peg puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='Common Lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='streams'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Deep Emacs Lisp Part 2</title><content type='html'>&lt;h2&gt;Streams&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://dorophone.blogspot.com/2011/04/deep-emacs-part-1.html"&gt;Last time&lt;/a&gt; we went through a lengthy development of monads in emacs
lisp.  Most of our attention was focused on just getting the idea
down, and we developed a pretty full list monad.  &lt;/p&gt;

&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/--4XksWj1UV8/TaoAU4H1zxI/AAAAAAAAAGA/mW3rVkO2Cds/s1600/Ambro-creek.jpg"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 240px; height: 320px;" src="http://1.bp.blogspot.com/--4XksWj1UV8/TaoAU4H1zxI/AAAAAAAAAGA/mW3rVkO2Cds/s320/Ambro-creek.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5596285845623918354" /&gt;&lt;/a&gt;

&lt;p&gt;It was intimated at the time that the list monad can be thought of as
a "possibilities" monad.  That is, we have functions which depend on a
single input, but can produce many possible outcomes.  &lt;/p&gt;

&lt;p&gt;Suppose we wish to understand the probabilty of of rolling a given
number when combining N dice with different numbers of sides.
Characterizing a particular fair die is easy enough.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'utils)
(defun die-outcomes (n-sides) 
  (range 1 (+ n-sides 1)))
(die-outcomes 6) ;-&amp;gt; (1 2 3 4 5 6)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Note: All the code here depends on my &lt;code&gt;utils.el&lt;/code&gt; and other packages
available from &lt;a href="https://github.com/VincentToups/emacs-utils" title="Emacs Utils"&gt;my github page&lt;/a&gt;).  &lt;/p&gt;

&lt;p&gt;You might want to know: what is the probability of rolling a 10 when I
roll three six sided dice?  You might have noticed that &lt;code&gt;die-outcomes&lt;/code&gt;
is a monadic function in the list monad.  It takes a number and
returns a list of outcomes.  How can we use the monad to calculate the
probability in question?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'monads)
(let* ((outcomes (mlet*_ monad-seq
                  ((d1 (die-outcomes 6))
                   (d2 (die-outcomes 6))
                   (d3 (die-outcomes 6)))
                  (m-return (+ d1 d2 d3))))
       (n-outcomes (length outcomes))
       (n-tens (length (filter (par #'= 10) outcomes) )))
   (* 100 (/ n-tens (* 1.0 n-outcomes)))); -&amp;gt;  12.5 percent chance
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a kind of interesting thing, if you think about it.  While
this certainly represents a brute force approach, the really neat
thing is that we only just described the possible outcomes of each
die.  This is a very easy problem.  We then just let the list monad
sort out the details.  &lt;/p&gt;

&lt;p&gt;I just joined the Triangle Area Functional Programmers Group, and they
recently considered the "Cracker Barrel Peg Board Puzzle" question.
If you aren't familiar with the game, here is a guy solving it with a
mnemonic device:&lt;/p&gt;

&lt;iframe title="YouTube video player" width="480" height="390"
src="http://www.youtube.com/embed/QMHGesvEyXQ" frameborder="0"
allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;But mnemonic devices are pretty crappy programs for pretty crappy
computers (brains).  Can we do better?  Well, in the spirit of the
above dice example, instead of considering the problem "how do we
solve the peg board puzzle?", let us consider instead the simpler
problem: given a peg board, how do we enumerate all the moves that we
can make on any given turn?&lt;/p&gt;

&lt;p&gt;We're going to be working functionally, so lets decide an on a
persistent representation of a game board.  By persistent we here mean
that when we modify such a structure, we actually get a copy of that
structure back.  One such representation is :&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun fresh-board ()
  (alist&amp;gt;&amp;gt;
   0 '(0)
   1 '(0 1)
   2 '(0 1 2)
   3 '(0 1 2 3)
   4 '(0 1 2 3 4)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We just represent a board as an alist with row indexes as keys and
each row represented by a list of occupied positions.  &lt;code&gt;alist&amp;gt;&amp;gt;&lt;/code&gt; is a
function from my utilities which just turns the above into:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;((0 (0)) (1 (0 1)) (2 (0 1 2)) (3 (0 1 2 3)) (4 (0 1 2 3 4)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Adding a removing a peg are somewhat obvious.  We just dip into the
alist to the appropriate row and remove the number of the peg we want
to take out.  Adding is the opposite operation.  We will handle error
checking at another level of abstraction, but we will at least
maintain at this point that each row contains only unique peg
positions.&lt;/p&gt;

&lt;p&gt;We'll represent positions as cons cells with column as the cdr and row
as the car.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun pos (x y)
  (cons x y))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And we'll be destructuring positions a lot, so lets whip up a quick
macro for that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro let-pos (peg-binders &amp;amp;rest body)
  (cond ((empty? peg-binders) `(progn ,@body))
        (t (let ((binder (car peg-binders))
                 (peg-sym (gensym "peg-")))
             `(let* ((,peg-sym ,(cadr binder))
                     (,(car (car binder)) (car ,peg-sym))
                     (,(cadr (car binder)) (cdr ,peg-sym)))
                (let-pos ,(cdr peg-binders) ,@body))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let-pos (((x y) (pos 3 4)))
  (list :x x :y y)) ;-&amp;gt; (:x 3 :y 4)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are more general destructuring bind operations lurking in the
fetid depths of my emacs lisp library, but clarity demands we use this
simple solution.  Note that the macro accepts any number of position
binding forms.&lt;/p&gt;

&lt;p&gt;Now those board-related functions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun peg-at-board? (board pos)
  (let-pos (((x y) pos))
           (mlet* monad-maybe^i
                  ((row (alist board y))
                   (at? ($ x in row)))
                  at?)))

(defun remove-peg (board pos)
  (let-pos (((x y) pos))
           (alist-conjugate board
                            y
                            (lambda (row)
                              (filter 
                               (f-not (par #'= x)) row)))))

(defun n-sort-cons (n n-list)
  (cond ((empty? n-list) (list n))
        ((= n (car n-list)) n-list)
        (($ n &amp;lt; (car n-list)) (cons n n-list))
        (t (cons (car n-list) (n-sort-cons n (cdr n-list))))))

(defun add-peg (board pos)
  (let-pos (((x y) pos))
           (alist-conjugate board
                            y (pal #'n-sort-cons x))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note: I snuck in a &lt;code&gt;maybe-monad&lt;/code&gt; use up there.  We will talk about it in a bit.&lt;/p&gt;

&lt;p&gt;Don't worry too hard about these functions.  They just do what they say
on the tin, with the understanding that they return a &lt;em&gt;new&lt;/em&gt; board with
the indicated changes, rather than modifying the board.  &lt;code&gt;n-sort-cons&lt;/code&gt;
is not tail recursive, but since there are no more than five pegs in a
row, we aren't likely to blow the stack.&lt;/p&gt;

&lt;p&gt;It will be handy to simulate moving pegs in a way that results in
failure when we try to move off the board.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun on-board? (pos)
  (let-pos (((x y) pos))
           (and (&amp;gt;= y 0)
                (&amp;lt;  y 5)
                (&amp;gt;= x 0)
                (&amp;lt;= x y))))

(defun move1 (pos direction)
  (let-pos (((x y) pos))
           (let* ((new-pos
                   (case direction
                     (:nw (pos (- x 1) (- y 1)))
                     (:ne (pos x (- y 1)))
                     (:e (pos (- x 1) y))
                     (:w (pos (+ x 1) y))
                     (:sw (pos x (+ y 1)))
                     (:se (pos (+ x 1) (+ y 1))))))
             (if (on-board? new-pos)
                 new-pos
               nil))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;move1&lt;/code&gt; takes a starting position and a direction (one of &lt;code&gt;(:nw :ne :e :w
:sw :se)&lt;/code&gt;) and returns the new position, if it is on the board.  Using
&lt;code&gt;move1&lt;/code&gt; we can define &lt;code&gt;move-n&lt;/code&gt; which just repeats this process N
times, or until we fall off the board.  You can see this code on the
github, its straightforward.  &lt;/p&gt;

&lt;p&gt;Ok, finally something interesting: &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun generate-hop (board pos dir)
  (lexical-let ((pos pos))
    (mlet*_ monad-maybe^i 
            ((origin-occupied? (peg-at-board? board pos))
             (over (move1 pos dir))
             (target (move-n 2 pos dir))
             (over-occupied? (peg-at-board? board over))
             (target-empty? (not (peg-at-board? board target))))
            `((:remove ,pos)
              (:remove ,over)
              (:place ,target)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function takes a board, a position, and a direction and generates
a hop in that direction if one is allowed.  It returns nil otherwise.
This kind of thing is built for the maybe monad, so most of the
function lives there.  It is a good time to remind ourselves of how
monads work.  &lt;code&gt;mlet*_&lt;/code&gt; tells use we are going to be chaining our
binding through a monad, in this case &lt;code&gt;monad-maybe^i&lt;/code&gt;, which is
defined in &lt;code&gt;monads.el&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defvar monad-maybe^i
  (tbl!
   :m-zero nil
   :m-return (lambda (x) x)
   :m-bind (lambda (v f)
             (if (not v) v
               (funcall f v))))
  "The (implicit) MAYBE monad.  
   NIL indicates failure.  
  MaybeVal is the identity.  
  Just is the identity.")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This monad is a variation on the regular old &lt;code&gt;maybe&lt;/code&gt; monad which just
lets the programmer express failure with a regular old lisp &lt;code&gt;nil&lt;/code&gt;
instead of a tagged value.  Hence the zero of this monad is just nil.
Return is the identity function, and bind is &lt;code&gt;almost&lt;/code&gt; funcall, but not
quite.  If the monadic value passed in is nil, it doesn't apply the
monadic function.  It just returns nil.  The net effect is that if any
expression in the &lt;code&gt;mlet*&lt;/code&gt; expression above is nil, the whole
expression evaluates to &lt;code&gt;nil&lt;/code&gt;, short circuiting through the subsequent
expressions.  Handy.  Monads are cool (see footnote 1 for why this 
monad has an &lt;code&gt;^i&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Ok, so now we have a function which can generate a hop.  It returns a
list of instructions on how that hop is implemented which we can use
to modify a board to effect that hop.  These instructions can be
thought of as a really dead simple programming language, so we can
write a function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun interpret-hop (board hop)
  (reduce 
   (lambda (board move) 
     (let ((what (car move))
           (where (cadr move)))
      (case what
        (:remove (remove-peg board where))
        (:place  (add-peg board where)))))
   hop
   :initial-value board))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which takes a hop and modifies the board to account for that move.
Apart from a bit of book keeping, we've solved the peg-puzzle.  We can
write a function which returns all the legal hops for a given board
quite easily using the &lt;code&gt;list-monad&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun generate-hops (board)
  (if (board-solved? board) (list board)
      (mlet*_ monad-seq^i 
        ((direction       (list :nw :ne :e :w :sw :se))
         (position        (generate-positions))
         (hop             (generate-hop 
                           board position
                           direction)))
       hop)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Astute readers might notice we could partially apply the board
argument of &lt;code&gt;generate-hop&lt;/code&gt; and then &lt;code&gt;lift-2&lt;/code&gt; the resulting function
into the sequence monad, then apply it to the lists of positions and
directions.  The provided approach is probably easier to read for people
not too familiar with monads.)&lt;/p&gt;

&lt;p&gt;Now solving the problem is just matter of applying generate-hops
thirteen times, taking care to produce a new set of boards from the
generate hops at each step.  &lt;/p&gt;

&lt;p&gt;If you try this, you'll find that it takes a very long time.  The peg
game has a pretty large state space, and its a hassle to generate ALL
solutions when we really don't want all of them, at least not all at
once.  Can we recapture the elegance of this simple, declarative
solution without having to calculate &lt;em&gt;every single&lt;/em&gt; win condition?&lt;/p&gt;

&lt;h2&gt;Enter Streams&lt;/h2&gt;

&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-6LG29Tz9LGQ/Tan_0z3yjtI/AAAAAAAAAF4/LVuc8H8dzaw/s1600/800px-Arkhangelskaia_oblast_Plesetskiy_Raion.JPG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;" src="http://1.bp.blogspot.com/-6LG29Tz9LGQ/Tan_0z3yjtI/AAAAAAAAAF4/LVuc8H8dzaw/s320/800px-Arkhangelskaia_oblast_Plesetskiy_Raion.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5596285294727040722" /&gt;&lt;/a&gt;

&lt;p&gt;One way we can use essentially the same methodology but not have to
calculate all the answers is to use &lt;em&gt;lazy&lt;/em&gt; lists or &lt;em&gt;streams&lt;/em&gt;.  Emacs
doesn't have them, so we are going to have to roll our own, but they
aren't conceptually that difficult.  A stream is simply a conceptual
pair of objects.  The first object represents the head of a the
stream.  The second object is a &lt;em&gt;function&lt;/em&gt; which returns the rest of
the stream when called.  From these simple beginnings we can produce a
data structure with all sorts of unusual behaviors.  For instance, its
possible to create an infinitely long stream of, for instance, all the
integers starting with 1 or all the Fibonacci numbers.  Even though
such streams are conceptually infinite, we can pass then around and
operate on them almost as we would any list.  We can even do things
which seem counter-intuitive at first, for instance, mapping a
function over an infinite stream to produce a new, infinite stream.&lt;/p&gt;

&lt;p&gt;Streams take some getting used to, but the process can be very
enlightening.  For instance, one learns quickly why Haskellers are not
as concerned about non-tail recursion when writing stream functions.
If the recursion occurs inside the "future" of the stream, you get a
free trampoline.&lt;/p&gt;

&lt;p&gt;Laziness, however, takes special care in Emacs Lisp, because variables
are not lexically scoped by default.  This means every time you create
a lambda which you intend to be called later, you have to make sure to
explicitly &lt;code&gt;lexically-let&lt;/code&gt; over it, indicating whatever variables the
lambda depends upon.  In a way, this is good, because it forces us to
think consciously about closures, which are important ideas in
functional programming. However, it can get syntactically busy to be
wrapping up things in &lt;code&gt;lexical-let&lt;/code&gt; forms all the time, particularly
because we often want only to create a lexical copy of a dynamically
bound variable.&lt;/p&gt;

&lt;p&gt;Ergo, our very first step in creating a stream library is to create a
nice form for delaying computations.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun single-symbol-list? (item)
  (and (listp item)
       (= (length item) 1)
       (symbolp (car item))))
(defun binderish? (item)
  (and (listp item)
       (= (length item) 2)
       (symbolp (car item))))

(defun with-form-&amp;gt;binder (item)
  (cond ((symbolp item )(list item item))
        ((listp item)
         (cond ((single-symbol-list? item)
                (cons (car item) item))
               ((binderish? item)
                item)
               (t (error "with-forms require symbols, 
                          a single symbol list, or a binder-like 
                          expression.  Got %S." item))))
        (t (error "with-forms require symbols, 
                   a single symbol list, or a binder-like 
                   expression.  Got %S." item))))

(defmacro* later (expr &amp;amp;key (with nil) (with* nil))
  (cond (with 
         `(lexical-let ,(mapcar #'with-form-&amp;gt;binder with)
            (later ,expr :with* ,with*)))
        (with* 
         `(lexical-let* ,(mapcar #'with-form-&amp;gt;binder with*)
            (later ,expr)))
        (t `(lambda () ,expr))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The macro &lt;code&gt;later&lt;/code&gt; takes a single expression and wraps it in a &lt;code&gt;lambda&lt;/code&gt;
with no arguments.  This is exactly the sort of lambda which forms the
tail of streams.  Because the contents of the tail expression often
depend on variables dynamically bound at the time the &lt;code&gt;lambda&lt;/code&gt; is
created, later lets you specify which values to produce a closure over
in several ways.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(later 10) ;-&amp;gt; (lambda () 10)
(let ((x 10))
  (later x :with (x))) -&amp;gt; (let ((x 10))
                            (lexical-let ((x x))
                             (lambda () x )))
(let ((x 10)) 
   (later y :with ((y (+ x 1))))) -&amp;gt;
 (let ((x 10)
    (lexical-let ((y (+ x 1)))
      (lambda () y))))

(let ((x 10))
  (later z :with* ((y (+ x 1))
                   (z (+ x y))))) -&amp;gt;
  (let ((x 10))
    (lexical-let* ((y (+ x 1))
                   (z (+ x y)))
      (lambda () z)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In words, &lt;code&gt;later&lt;/code&gt; takes an expression and a list of binding
expressions which are similar to &lt;code&gt;let&lt;/code&gt; binders, but which can be
single symbols, which expand to a &lt;code&gt;(x x)&lt;/code&gt; binding expression.  Now we
are equipped to do a reasonable job implementing streams.&lt;/p&gt;

&lt;p&gt;(Note: These streams are based loosely on those covered in "The
Reasoned Schemer", although they've been adapted to stand alone from
the Kanren interpreter and also to be more easily understood (to me)).&lt;/p&gt;

&lt;p&gt;We could represent streams as cells, build them using "cons," etc, but
I prefer to have a bit more error detection built into the
implementation.  I don't want to accidentally use a list as a stream
or vice versa.  Emacs provides some basic facilities for defining new
"types," with &lt;code&gt;defstruct&lt;/code&gt; (see Footnote 2).  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defstruct stream head future)
(defun stream (hd &amp;amp;optional future)
  (make-stream :head hd :future future))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This defines the functions &lt;code&gt;make-stream&lt;/code&gt;, &lt;code&gt;stream-head&lt;/code&gt;,
&lt;code&gt;stream-future&lt;/code&gt; and &lt;code&gt;stream-p&lt;/code&gt;.  Good enough for use to get started,
certainly.  The function &lt;code&gt;stream&lt;/code&gt; is just the stream analog of &lt;code&gt;cons&lt;/code&gt;,
it creates a stream cell.  We allow the tail to be optional, because
we'll want to create streams of one element frequently.
Incidentally, &lt;code&gt;stream&lt;/code&gt; with one element is our &lt;code&gt;return&lt;/code&gt; operation
for the stream monad.&lt;/p&gt;

&lt;p&gt;What about &lt;code&gt;car&lt;/code&gt; and &lt;code&gt;cdr&lt;/code&gt;?  Stream &lt;code&gt;car&lt;/code&gt; (&lt;code&gt;scar&lt;/code&gt;) is easy enough:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun scar (stream)
  (cond ((not stream) nil)
        (t 
         (progn
           (if (not (stream-p stream))
               (error "Tried to take the scar of a non-stream %S." stream))
           (stream-head stream)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first cond checks for the nil stream, which we represent with
&lt;code&gt;nil&lt;/code&gt;.  If the stream isn't nil, it returns the head portion of the
stream.  Easy enough.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun scdr (stream)
  (cond ((not stream) nil)
        (t
         (progn
           (if (not (stream-p stream))
               (error "Tried to take the scdr of a non-stream %S." stream))
           (let ((fut (stream-future stream)))
             (if fut
                 (progn 
                   (if (not (functionp fut)) 
                    (error "The future of a stream must be 
                            either nil or a function.  Instead its %S" fut))
                   (let ((fut (funcall fut)))
                     (if (not (stream-p fut)) 
                      (error "The future of a stream must 
                              evaluate to a stream.  Instead it was %S" fut))
                     fut))
               nil))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;scdr&lt;/code&gt; is longer, but it is simple enough.  Check for nil, in which
case the &lt;code&gt;scdr&lt;/code&gt; is nil.  Otherwise, grab the lambda in the second half
of the stream and call it, checking to make sure that the output is
itself a stream.  Return that.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;defstruct&lt;/code&gt; created &lt;code&gt;stream-p&lt;/code&gt; for us, which tests to see if an object
counts as a stream, but we want the definition to include the &lt;code&gt;nil&lt;/code&gt;
object, so we should define for later:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun stream? (x) (or (not x) (stream-p x)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I like the question-mark indicates predicate style better anyway.&lt;/p&gt;

&lt;p&gt;It is convenient to split a stream into three cases.  The first case
is the nil stream, the second is a stream with one element, and the
third is a stream with a future.  Lots of algorithms need to act
differently in these cases, so we should write a macro over them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun stream-future-nil? (object)
  (nil? (stream-future object)))
(defun stream-with-future? (object)
  (not (nil? (stream-future object))))

 (defmacro stream-case (stream
                        nil-case
                        =a=expressions
                        =a-f=expressions)
   (with-gensyms 
    (stream%)
    `(let ((,stream% ,stream))
       (if (not (stream? ,stream%)) 
          (error "Stream-case needs a stream 
                  input, got instead %S." ,stream%))
       (cond 
        ((nil? ,stream%)
         ,@nil-case)
        ((stream-future-nil? ,stream%)
         (let ((,(car (car =a=expressions)) (scar ,stream%)))
           ,@(cdr =a=expressions)))
        ((stream-with-future? ,stream%)
         (let ((,(car (car =a-f=expressions)) (scar ,stream%))
               (,(cadr (car =a-f=expressions)) (stream-future ,stream%)))
           ,@(cdr =a-f=expressions)))
        (t (error "Couldn't figure out what to do 
                   with stream %S.  This should never 
                   happen." ,stream%))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Stream case figures out which condition our stream is in, and then
destructures the stream into the appropriate parts.  When the stream
is nil, the first body form is simply executed.  When the stream is a
singleton, the value contained in the stream is bound to a symbol the
user passes in and the bodyforms are executed in that context, and
when there is a value and a future-stream, those are bound to user
defined symbols in a body for that case.  It is all wrapped up in a
hidden &lt;code&gt;cond&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((s (stream 'x (later (stream 'y nil)))))
  (stream-case s
    ((print "this is not executed because s is not nil")
    ((a) (print "If the stream had a single element, a would be
  bound to it here"))
    ((a f) 
     (print "This case is executed becase s has a future, a is the
  `scar` of s, f is the function which produces the future
  stream.")))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;About the simplest function we can write acting on streams is
&lt;code&gt;take-n&lt;/code&gt;, which simply takes a limited number of elements from a
stream and converts them into a list.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun take-n (stream n &amp;amp;optional acc)
 (if (= n 0) (reverse acc)
   (stream-case stream 
     ((reverse acc))
     ((a) (reverse (cons a acc)))
     ((a f) (take-n 
             (funcall f) (- n 1) (cons a acc))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function will return fewer than the requested number of elements
if the stream ends before &lt;code&gt;n&lt;/code&gt; is reached.  Readers might notice this
is a tail-recursive function.  Emacs doesn't support tail-recursion
natively, but I wrote a library that does, so we can define this
function this way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(require 'recur)
(recur-defun* take-n (stream n &amp;amp;optional acc)
  (if (= n 0) (reverse acc)
    (stream-case stream
                 ((reverse acc))
                 ((a) (reverse (cons a acc)))
                 ((a f) (recur (funcall f) (- n 1) (cons a acc))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will never blow the stack, which is important, since we want to
take many elements off a stream without worrying about whether the
stream is shorter than the recursion limit.&lt;/p&gt;

&lt;p&gt;Ok.  With just these few functions we can play with some non-trivial
streams.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defvar *ones*
   (stream 1 (later *ones*))
   "Infinite stream of ones.")

(defun ints-from (n)
  (stream n (later (ints-from (+ n 1)) :with (n))))

(take-n (ints-from 5) 10) ;-&amp;gt; (5 6 7 8 9 10 11 12 13 14)
(take-n (ints-from 10) 10);-&amp;gt; (10 11 12 13 14 15 16 17 18 19)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These are really just parlor tricks, though.  See if you can figure
out how to define &lt;code&gt;forever,&lt;/code&gt; which is a function which takes a value
and returns a stream of that value forever.  Or &lt;code&gt;repeating,&lt;/code&gt; which
takes a list and returns a stream which is that list repeated over and
over again.  &lt;/p&gt;

&lt;p&gt;Note that even though functions like &lt;code&gt;ints-from&lt;/code&gt; appear to call
themselves, they do so only &lt;em&gt;after&lt;/em&gt; they return their stream.  There
isn't any real recursive call - the stream serves as a trampoline.
These functions won't blow the stack.&lt;/p&gt;

&lt;h2&gt;Building Towards the Stream Monad&lt;/h2&gt;

&lt;p&gt;Streams are obviously somewhat analogous to lists.  Lists form a monad
with &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;map-cat&lt;/code&gt; as the return and bind operations
respectively.  We'd like to form a monad with streams too.  Then we
could do some truly interesting things with &lt;code&gt;mlet*&lt;/code&gt; like notation,
cuing up potentially infinite calculations into a nice list-like
package.  &lt;/p&gt;

&lt;p&gt;We've already determined that the &lt;code&gt;return&lt;/code&gt; operation for a stream
monad would simply be the function which puts a single value into a
singleton stream:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun stream-return (x) (stream x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We need to build up to &lt;code&gt;mapcat&lt;/code&gt; for streams, and then we will have our
monad.  &lt;/p&gt;

&lt;p&gt;Streams get a little confusing at this point.  The key to keeping our
head straight is to remember that the tail of the stream &lt;em&gt;always&lt;/em&gt;
needs to be handled in such a way as to maintain the laziness of the
stream.  When you &lt;code&gt;map&lt;/code&gt; over a stream, you don't actually visit the
rest of the stream at the time of the &lt;code&gt;map&lt;/code&gt;.  You simply return a new
stream whose future includes the fact that it needs to apply a
function to each value before returning it.  &lt;code&gt;stream-map&lt;/code&gt; is just as
lazy, in other words, as our streams.&lt;/p&gt;

&lt;p&gt;Let's write &lt;code&gt;smapcar&lt;/code&gt;, that is &lt;code&gt;stream-map-car.&lt;/code&gt;  The exact analog of
the list &lt;code&gt;mapcar&lt;/code&gt; function.  It will take a function and a stream and
return a new stream which is that function applied to every element in
the input stream.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun smapcar (f stream)
  (stream-case stream
    (nil) ; empty stream -&amp;gt; empty stream
    ((a) ; singleton stream -&amp;gt; singleton stream of (f a)
     (stream (funcall f a) nil))
    ((a future)
     (stream (funcall f a)
       (later (smapcar f (funcall future)) :with (f future))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The only case which might be confusing is the last case.  The
&lt;code&gt;stream-case&lt;/code&gt; expression extracts &lt;code&gt;a&lt;/code&gt;, the value at the head of the
stream, and &lt;code&gt;future&lt;/code&gt;, the function which returns the future of the
stream.  We form a new stream whose head is &lt;code&gt;(f a)&lt;/code&gt; and whose future
is the mapping of &lt;code&gt;f&lt;/code&gt; onto the &lt;code&gt;future&lt;/code&gt; of the input stream.  This
mapping occurs &lt;code&gt;later&lt;/code&gt;, only when someone asks for the future of the
output stream.&lt;/p&gt;

&lt;p&gt;It is possible to write a generalization of this function which maps a
multi-input function over multiple streams.  I'll leave such a
function to the reader, but I will provide here a function &lt;code&gt;smapcar2&lt;/code&gt;
which maps over two streams, because it will let us construct some
interesting streams.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun smapcar2 (f-of-2 stream1 stream2)
  (stream-case stream1
               (nil)
               ((a) (stream-case stream2
                     (nil)
                     ((b) (stream (funcall f-of-2 a b) nil))
                     ((b g)
                      (stream (funcall f-of-2 a b) nil))))
               ((a f)
                (stream-case stream2 
                 (nil)
                 ((b) (stream (funcall f-of-2 a b) nil))
                 ((b g)
                   (lexical-let ((f-of-2 f-of-2)
                                 (f f)
                                 (g g))
                    (stream (funcall f-of-2 a b) 
                     (lambda () (smapcar2 f-of-2 (funcall f)
                      (funcall g))))))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With &lt;code&gt;smapcar2&lt;/code&gt; we can finally create things like the stream of Fibonacci
Numbers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defvar fibs (stream 1
                      (later 
                        (stream 1 
                                (later (smapcar2 #'+ fibs (scdr fibs))))))
   "The stream of Fibonacci numbers")
(take-n fibs 10) ;-&amp;gt; (1 1 2 3 5 8 13 21 34 55)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Whee!  An eternal golden braid, etc!&lt;/p&gt;

&lt;h2&gt;Stream Map &amp;amp; Concatenate &lt;/h2&gt;

&lt;p&gt;We might think that all we need to do now is define &lt;code&gt;stream-map&lt;/code&gt; and
&lt;code&gt;stream-cat&lt;/code&gt; and then &lt;code&gt;stream-map-cat&lt;/code&gt; would be the composition of
these functions.  This might almost be made to work except that either
operation might be produce an infinite stream of results.  It is more
straightforward to interleave the concatenation with the map
operation.  We can define &lt;code&gt;stream-cat&lt;/code&gt; by itself:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun stream-cat (stream1 stream2)
  (stream-case 
   stream1
   (stream2)
   ((a) (stream-case stream2 
                     ((stream a nil))
                     ((b) (stream a (lexical-let ((b b)) 
                                      (lambda () (stream b nil)))))
                     ((b g)
                      (stream a (lexical-let ((b b)
                                              (g g))
                                  (lambda () (stream b g)))))))
   ((a f)
    (stream-case stream2
                 ((stream a f))
                 ((b) (stream a 
                              (lexical-let ((f f)
                                            (stream2 stream2))
                                (lambda () (stream-cat (funcall f) stream2)))))
                 ((b g) 
                  (stream a 
                          (lexical-let ((f f)
                                        (stream2 stream2))
                            (lambda () (stream-cat (funcall f) stream2)))))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function is a doozy, but the upshot is simple.  If either stream
is empty, we return the other stream, obviously.  Otherwise we lazily
pass stream two down stream one, until we find the tail of stream one,
whereupon we fasten stream two.  A call to stream cat doesn't
instantaneously modify the entirety of &lt;code&gt;stream1&lt;/code&gt;, it should be noted.
It modifies just the tail, essentially passing instructions "down the
line" to further modify each tail until a &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can now define &lt;code&gt;stream-map-cat&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(recur-defun* stream-map-cat (mf stream)
  (lexical-let ((mf mf))
    (stream-case stream
                 (nil)
                 ((a) (funcall mf a))
                 ((a f) 
                  (lexical-let ((interior-stream (funcall mf a))
                                (f f))
                    (stream-case 
                     interior-stream
                     ((recur mf (funcall f)))
                     ((b) (stream b 
                                  (later
                                   (stream-map-cat mf (funcall f)))))
                     ((b g) (stream b
                                    (lexical-let ((g g))
                                      (later 
                                       (stream-cat (funcall g)
                                                   (stream-map-cat mf (funcall f)))))))))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;stream-map-cat&lt;/code&gt; is actually pretty easy to understand if you remember
that &lt;code&gt;mf&lt;/code&gt; is going to be a monadic function of the stream monad, and
so whatever is passed &lt;em&gt;in&lt;/em&gt;, &lt;code&gt;mf&lt;/code&gt; always returns a stream (see Footnote 3 for a subtlety).  Obviously
if the input stream is &lt;code&gt;nil&lt;/code&gt;, the output is also.  If input has a
single element, then &lt;code&gt;mf&lt;/code&gt; on that element is the output stream.   When
there is a non-trivial stream, then we evaluate the monadic function
to get the &lt;code&gt;interior-stream&lt;/code&gt; and lazily concatenate it on the tail of
the result of &lt;code&gt;map-cat&lt;/code&gt; on the rest of the input stream.&lt;/p&gt;

&lt;p&gt;That is it.  We have the stream monad:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq monad-stream 
      (tbl! :m-bind #'stream-bind
            :m-return #'stream-return
            :m-zero nil))
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;So what can we do with it?&lt;/h2&gt;

&lt;p&gt;We'll, besides implement Kanren, the point of all this, I can think of
at least one interesting example.  Emacs provides functions to
generate random numbers with a uniform distribution.  &lt;code&gt;random*&lt;/code&gt; from
the &lt;code&gt;cl.el&lt;/code&gt; library is able to produce such numbers in any given
range.  Consider the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun* random-numbers (lim &amp;amp;optional (state (make-random-state)))
  (let* ((*random-state* state)
         (val (random* lim)))
    (lexical-let ((new-state (make-random-state))
                  (lim lim))
      (stream val (lambda () 
                    (random-numbers lim new-state))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function takes a limit and an optional state and returns a stream
of uniformly distributed numbers.  Using a dynamically bound
&lt;code&gt;*random-state*&lt;/code&gt; and the fact that &lt;code&gt;make-random-state&lt;/code&gt; returns the
current state, the resulting stream returns an infinite, but
repeatable, list of uniformly distributed numbers.  &lt;/p&gt;

&lt;p&gt;Uniform distributions are fine, but we often want Guassian numbers.
There are several ways to get them if you have uniform distributions.
One way is to take the average of many uniformly distributed numbers.
These averages will have a Gaussian distribution which can be scaled
to whatever standard deviation is desired.  A way which is less
computationally intensive, however, is the &lt;a href="http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform"&gt;Box-Muller transformation&lt;/a&gt;,
which takes two independent distributions of uniformly distributed
numbers and returns two distributions of Guassian numbers.  If &lt;code&gt;u&lt;/code&gt; and
&lt;code&gt;v&lt;/code&gt; are uniformly distributed, then &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;p = sqrt(-2*log u)*cos(2 pi v)
q = sqrt(-2*log u)*sin(2 pi v)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;p&lt;/code&gt; and &lt;code&gt;q&lt;/code&gt; are two independent but &lt;em&gt;normally&lt;/em&gt; distributed numbers.
If we don't care about the seeds in particular, only that they are
different, we can create a stream of Guassian numbers like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun zip-streams (&amp;amp;rest streams)
  (apply #'smapcar* #'list streams))


(setq normal-numbers 
      (mlet**_ monad-stream ((pair
                  (zip-streams (random-numbers 1.0)
                               (random-numbers 1.0
                     (make-random-state t)))))
               (lexical-let ((u (car pair))
                             (v (car pair))
                             (r (sqrt (* -2 (log  u))))
                             (s (* 2 pi v)))
                 (stream (* r (cos s))
                         (later (stream (* r (sin s)) nil))))))
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;The monad handles collecting the two results into a single stream for
us, and the result is itself an infinite list of normally distributed
numbers.  I don't know about you, but I think that is pretty cool.&lt;/p&gt;

&lt;h2&gt;Don't Interleave Just Yet&lt;/h2&gt;

&lt;p&gt;Because streams can be infinite, sometimes we don't want to strictly
concatenate them.  We can certainly concatenate a stream of infinite
ones and a stream of infinite twos, but the result will be,
effectively, a stream of infinite ones.  The twos will be conceptually
at the end of this stream, but you'll never reach them.  Because of
this, we sometimes want to &lt;em&gt;interleave&lt;/em&gt; the results of monadic
functions.  Hence, this library provides an alternative monad
&lt;code&gt;monad-stream^i&lt;/code&gt; which has this behavior.  In this case &lt;code&gt;i&lt;/code&gt; means
"interleave" rather than &lt;code&gt;implicit.&lt;/code&gt;  I'm going to have to do
something about that.&lt;/p&gt;

&lt;h2&gt;The Peg Puzzle&lt;/h2&gt;

&lt;p&gt;If you return to the peg puzzle, making the minor changes you need to
adapt the functions which enumerate possible moves so that they return
streams, you can use almost the exact same code to produce a lazy-list
of all possible solutions to the peg game.  The full code is available
in the &lt;code&gt;examples&lt;/code&gt; directory.  Using this code, one can &lt;code&gt;queue up&lt;/code&gt; all
the solutions to the puzzle, actually examining and returning only
those which are needed.&lt;/p&gt;

&lt;p&gt;I've implemented a slightly more complex version of the peg puzzle
problem so that we end up with not just a stream of solved puzzles,
but a stream of solved puzzles AND the included solution as a list of
instructions.  It's too lengthy to reproduce here, but you can see the
code &lt;a href="https://github.com/VincentToups/emacs-utils/blob/master/examples/peg-puzzle-streams.el" title="Peg Puzzle"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As always, all the code here, and this tutorial, are available from my
&lt;a href="https://github.com/VincentToups/emacs-utils" title="Emacs Utils"&gt;github account&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Footnote 1: I've found it handy define a set of "implicit" monads, the
defining feature of which is bind does a lot more heavy lifting,
basically squeezing non-monadic values into the monad if it encounters
them during its work.  For instance, the implicit list monadic bind
operation takes two arguments.  When the first is a list, bind is
identical to the non-implicit list monad, but when it isn't a list it
is wrapped in one before things proceed.  Similarly, monadic functions
may return non-lists, and when this happens, they are simply loaded
into lists before being treated normally.  The end effect is that you
don't have to decorate all your expressions with &lt;code&gt;m-return&lt;/code&gt; all the
time.  The only time you need to use one is when you really mean to
return a &lt;code&gt;list of nil&lt;/code&gt; rather than &lt;code&gt;nil&lt;/code&gt; itself.&lt;/p&gt;

&lt;p&gt;This might be a crazy thing, but seems ok to me!  It seems to be more
appropriate for dynamically typed languages because functions are
always modifying their behavior to fit the input types anyway.&lt;/p&gt;

&lt;p&gt;Footnote 2: Deftype produces a type which is unfortunately true under
&lt;code&gt;vectorp&lt;/code&gt;.  Too bad. &lt;/p&gt;


&lt;p&gt;Footnote 3: 
The function shown here is technically correct, in terms of the final
values that appear in the stream and where.  However, it is not as
lazy as it could be.  The key recognition is that, while &lt;code&gt;mf&lt;/code&gt; is not
the tail of a stream, the partial application of &lt;code&gt;mf&lt;/code&gt; to &lt;code&gt;a&lt;/code&gt; is a
function which takes no arguments and returns a stream.  That is,
&lt;code&gt;(lambda () (funcall mf a))&lt;/code&gt; is an acceptable stream tail, and we
don't need to evaluate &lt;code&gt;mf a&lt;/code&gt; to use it as such.  The following code
implements this fully lazy version of &lt;code&gt;stream-map-cat&lt;/code&gt;, here called
&lt;code&gt;stream-map-cat-tail&lt;/code&gt;.  The only thing you need to understand in
addition to the above is that &lt;code&gt;par&lt;/code&gt; in the following code stands for
&lt;code&gt;partially apply on the right&lt;/code&gt;.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun stream-cat-tail (head-stream tail)
  (if (stream? head-stream)
      (stream-case head-stream
                   ((funcall tail))
                   ((a) (stream a tail))
                   ((a f)
                    (stream a 
                            (later 
                             (stream-cat-tail (funcall f) tail) :with (f tail)))))
    (stream-cat-tail (funcall head-stream) tail)))

(defun* stream-map-cat-tail (mf instream)
  (stream-case instream
               (nil)
               ((a) (funcall mf a))
               ((a f)
                (let ((tail (par mf a)))
                  (stream-cat-tail tail
                                   (later
                                    (stream-map-cat-tail mf (funcall f))
                                    :with (mf f)))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;stream-cat-tail&lt;/code&gt; pins the function &lt;code&gt;tail&lt;/code&gt; at the end of the stream
&lt;code&gt;head-stream&lt;/code&gt;.  It turns out to be convenient to pass functions
returning streams into the &lt;code&gt;head-stream&lt;/code&gt; position, so this function
checks for that case and extracts the head stream if needed.  The
library actually uses these functions rather than the slightly less
lazy version in the tutorial body.  I found that version easier to
understand at first.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-7819365718841464578?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/7819365718841464578/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=7819365718841464578' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7819365718841464578'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7819365718841464578'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/04/deep-emacs-lisp-part-2.html' title='Deep Emacs Lisp Part 2'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/--4XksWj1UV8/TaoAU4H1zxI/AAAAAAAAAGA/mW3rVkO2Cds/s72-c/Ambro-creek.jpg' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-7401657156835283293</id><published>2011-04-09T08:27:00.000-07:00</published><updated>2011-05-27T04:07:29.761-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorials'/><category scheme='http://www.blogger.com/atom/ns#' term='stack languages'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='concatenative languages'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Deep Emacs Lisp Part 1 (Basically, a Monad Tutorial)</title><content type='html'>&lt;h2&gt;Implementing Monads&lt;/h2&gt;

(Note: everything in this tutorial (and this tutorial) is available on my &lt;a href="https://github.com/VincentToups/emacs-utils"&gt;github&lt;/a&gt;)

&lt;p&gt;I've started working on an implementation of Kanren, the logic
mini-language covered in &lt;a href="http://tinyurl.com/2hl5bu" title="The Reasoned Schemer"&gt;The Reasoned Schemer&lt;/a&gt;, in Elisp.  I
want to write about this process vis-a-vis the unique considerations of
Emacs Lisp as a language, but its a middle sized project, which
depends on several pieces of code, which themselves depend on code
which I haven't described in detail anywhere.  Kanren depends on a
monad (the "stream of substitutions" monad, for the nerds out there).
An implementation of this monad is a good place to start when thinking
about implementing Kanren itself, but I've never written up a piece of
documentation on the implementation of Monads I've cooked up for
Elisp, so I thought I'd start there.&lt;/p&gt;

&lt;p&gt;So, this document one part monad tutorial and one part introduction to
the &lt;code&gt;monads.el&lt;/code&gt; implementation.  &lt;/p&gt;

&lt;p&gt;First, credit where its due: the following implementation of Monads
owes is basic form to the &lt;a href="http://richhickey.github.com/clojure-contrib/monads-api.html" title="Clojure Contrib Monad Library"&gt;monad library in
Clojure-contrib&lt;/a&gt;.  I wrote it while reading through Jim
Duey's completely excellent &lt;a href="http://intensivesystems.net/writings.html" title="Clojure Monad Tutorials"&gt;monad tutorials for
clojure&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Besides minor considerations associated with dealing with the default
dynamic scope in Elisp, the implementation here is basically that of
the Clojure Contrib library.  And I could never have understood what I
was doing without the tutorial.  &lt;/p&gt;

&lt;h3&gt;Onto the Monads&lt;/h3&gt;

&lt;p&gt;We've reached the monad tutorial part of this document.  Like most
people, I found monads pretty hard to understand when I first
encountered them.  Most monad tutorials speak in Haskell, which I
think can be doubly confusing because its hard to tell exactly where
Haskell ends and where monads begin, since Haskell has special syntax
built in to the language just for the purpose of dealing with monads.
You can, of course, use the functions &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt; outside of do
notation, as many tutorials do, but at least dealing with &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt;
requires a fair amount of comfort with Haskell's infix operator
system.  At least it seemed that way to me. &lt;/p&gt;

&lt;p&gt;I really had to implement monads in Elisp to finally get it.  I think
this worked because every part of the monad beastiary had to be built
from the ground up.  If you want  "do notation," for instance, it
isn't just there, you have to write a macro for it, and understand
it.  The uniformity of Lisp syntax (which some people hate, I know)
also helps keep things focused on the ideas.  That is the approach we
will take here.  We'll start just kicking around some general ideas
and gradually build up to &lt;code&gt;do&lt;/code&gt; notation, making constant analogy to
well understood lisp and programming concepts.  I think this is better
than throwing oneself in head first. &lt;/p&gt;

&lt;h3&gt;Notes on Types&lt;/h3&gt;

&lt;p&gt;I think of a monad as a way of decorating function composition.  If I
have a group of functions that conform to a particular input and
output constraint, a monad associated with that constraint tells me
how to combine those functions so that something interesting happens.
The fact that these functions all have the same type of input and
output is critical, otherwise the "plumbing" specified by the monad
won't work.  Such functions are called "monadic functions" and when we
try to understand monads, or a specific monad, we must meditate on the
type of these functions.&lt;/p&gt;

&lt;p&gt;When coming to monads from dynamic languages, it can be easy to gloss
over all the type language used to describe them.  But it really helps
to think about types in a loosy-goosey way, at the very least, because
type constraints are what make the plumbing in a monad work.  Monads
generally contain things.  In statically typed languages the monad
type depends on the type of the things it holds, so it would be
specified, but we'll just refer to these things as "values".  Their
type isn't terribly important except that they are distinct from the
type of &lt;em&gt;monadic values&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;A &lt;em&gt;monadic value&lt;/em&gt; can be thought of as an abstract container which
contains regular values.  Sometimes this is a rather obvious kind
of relationship.  A list "contains" values, for instance.  Sometimes
it can be much more abstract.&lt;/p&gt;

&lt;p&gt;Finally, there are &lt;em&gt;monadic functions&lt;/em&gt;.  A monadic function is a
function which takes a regular value and returns a &lt;em&gt;monadic value&lt;/em&gt;.
It is important to think about these types while we work on the rest
of the tutorial, so take a second to look at the diagrams, and then
get into the habit of calling to mind the types of various objects
whenever you get confused.  It really helped me.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://ubuntuone.com/p/vti/" alt="Types of Interest" title="Types of Interest" /&gt;&lt;/p&gt;

&lt;h3&gt;The List or Sequence Monad&lt;/h3&gt;

&lt;p&gt;We'll be talking about the sequence monad first.  We will represent
sequences with regular old lisp lists, which can hold values of any
lisp type.  The monadic values, then, are lists.  Lists "contain"
(they also contain) lisp data.  Let us meditate upon the type of
monadic functions in the sequence monad.&lt;/p&gt;

&lt;p&gt;==== this space reserved for meditation ==== &lt;/p&gt;

&lt;p&gt;If you guessed in your meditation that the monadic functions
associated with the sequence or list monad (we'll use the two
interchangeably from here on) are functions which take a single value,
which is any lisp data, and return a list of lisp-values, you got it.&lt;/p&gt;

&lt;p&gt;So, whatever the hell a "list monad" is, its got something to do with
composing functions which take a single lisp value and return a list.  &lt;/p&gt;

&lt;p&gt;What kind of functions might do this?  Well, a common idea is that
functions in the list monad represent computations with multiple
possible outcomes or values.  A person might have multiple friends, so
a function &lt;code&gt;get-friends&lt;/code&gt; might return a &lt;em&gt;list&lt;/em&gt; of persons rather than
a single person.  We might want to go on to calculate those persons'
friends (or enemies, relatives, lovers).  All these &lt;code&gt;get-&lt;/code&gt; functions
would be monadic functions in the list monad, and the list monad will
help use deal with them.&lt;/p&gt;

&lt;p&gt;Let's start kicking around ideas about composing these functions.  To
do that we need a few functions to consider explicitly.  What are the
simplest monadic functions of the list monad?  Well &lt;code&gt;list&lt;/code&gt; is
certainly such a function when you call it with one argument, so lets
put that on the pile.  In that spirit, consider:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defun stub (x) nil)
(defun list-return (x) (list x))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src="http://ubuntuone.com/p/vtm/" alt="monadic return" title="monadic return" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;list-return&lt;/code&gt; can be thought of as a sort of "default" or "simplest
possible" monadic function.  We've just wrapped up &lt;code&gt;list&lt;/code&gt; so that it
takes but one argument.  Every monad needs a return, and every return
does something like the above - it wraps its input into the monad in a
simple way.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stub&lt;/code&gt; is also a monadic function - it ignores its inputs and returns
the empty list.  &lt;/p&gt;

&lt;p&gt;Ok, lets write some semi-meaningful list-monadic functions.  Consider:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defvar *people* '(:ted :lea :leo :james 
                   :harvey :sally :jane :andrew 
                   :catherine) 
                   "A list of all the people that matter.")
(defvar *friends-db* 
      '((:ted (:lea :leo :sally :andrew :catherine :leo :jane))
        (:lea (:ted :leo :jane :andrew :harvey :sally :catherine))
        (:leo (:ted :lea :ted :harvey :sally :jane :andrew 
                    :catherine :harvey :andrew :catherine))
        (:james (:jane :harvey :jane))
        (:harvey (:leo :lea :leo :james :harvey :harvey :sally))
        (:sally (:ted :leo :lea :harvey :jane :andrew))
        (:jane (:lea :leo :james :sally :ted :james :andrew :catherine))
        (:andrew (:ted :lea :leo :sally :jane :leo))
        (:catherine (:ted :leo :lea :leo :jane :catherine :catherin))) 
"Our database of friend connections.")

(defun friends-of (person) 
  "Return a list of all the people in friends-db."
  (alist *friends-db* person)
         ;alist is a function which retrieves
         ;a key's data from an association list.
)

(defun mutual-friends-with (person1)
  "Return a monadic function which returns 
   the friends person1 has in common with person2."
  (lexical-let ((p1-friends (friends-of person1)))
    (lambda (person2) 
      (let ((p2-friends (friends-of person2)))
       (filter (lambda (friend)
                 (in friend p1-friends)) p2-friends)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Friends-of&lt;/code&gt; is more or less obvious enough.  It simply retrieves
the database entry in the friends database, here represented as an
association list.  It takes a person and returns a list of persons, so
it is a monadic function.  &lt;/p&gt;

&lt;p&gt;The second function is a little more complicated.  It takes a person
and returns a function, so its not a monadic function itself.  It
does, however, return a monadic function - one parameterized on
&lt;code&gt;person1&lt;/code&gt;.  It returns the friends of &lt;code&gt;person2&lt;/code&gt; that are also friends
with &lt;code&gt;person1&lt;/code&gt;.  This business of having a function which returns a
&lt;em&gt;parameterized&lt;/em&gt; monadic function is pretty common, and it will lend
monadic expressions a certain domain-specific-language feel, as you'll
see.  Note that we need a &lt;code&gt;lexical-let&lt;/code&gt; in the function body to create
a closure around &lt;code&gt;p1-friends&lt;/code&gt;.  If we used a &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;p1-friends&lt;/code&gt; would
be out of scope by the time someone called the &lt;code&gt;lambda&lt;/code&gt;.  If this is
confusing, see the wikipedia article on &lt;a href="http://en.wikipedia.org/wiki/Scope_(programming)" title="Scope in Programming"&gt;scope in programming
languages&lt;/a&gt;.  Emacs has a default dynamic scope, but most modern
languages are lexical&lt;sup&gt;2&lt;/sup&gt;.   &lt;/p&gt;

&lt;p&gt;But we were meditating on function composition.  Before going any
further lets implement a function which composes functions.  The only
wrinkle here is that order of composition is reversed from the order
of application (&lt;code&gt;f1&lt;/code&gt; is applied before &lt;code&gt;f2&lt;/code&gt;) because this looks more
natural in an applicative language.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defun compose2 (f2 f1)
  "Compose F2 and F1 by returning a new function which
   calls F1 on its arguments, then calls F2 on the result"
   (lexical-let ((f1 f1)
                 (f2 f2))
    (lambda (&amp;amp;rest args)
      (funcall f2 (apply f1 args)))))

(defun compose (&amp;amp;rest fs)
  "Compose the functions FS (FN FN-1 FN-2 ... F1) 
   beggining with F1 and working backwards."
  (let ((rfs (reverse fs)))
   (reduce
     (lambda (acc-f f)
       (compose2 f acc-f))
     (cdr rfs)
     :initial-value (car rfs))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let's try just composing some of our list monadic functions.  We might
actually want a function to calculate the &lt;code&gt;friends-of&lt;/code&gt; the
&lt;code&gt;friends-of&lt;/code&gt; someone:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(compose #'friends-of #'friends-of)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because this is Elisp, this doesn't cause an error.  Of course, if we
think about this, we know it can't work.  As soon as well call this
function, we'll get an error (actually, it will return nil, because 
a list of friends is not a key in the friends db), because the second friends-of will be applied to a list, rather than a single person.  Try it, if you are
having trouble seeing it.&lt;/p&gt;

&lt;p&gt;Suppose we were dead set on jamming these two functions together and,
in particular, we want to make sure that the resulting function can be
used wherever either of the two functions might have been used.  That
is, we want the result type to be the same as the input types.  There
are several approaches, obviously, but here is a sort of obvious one
(read the comments here, they are important):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defun list-func-compose (f2 f1)
   (lexical-let ((f1 f1)
                 (f2 f2))
    (lambda (arg) ; lambda need take only a single arg, as f1 must
      (let* ((r1 (funcall f1 arg)) 
        ; r1 is a list, because it is the result of f1, a monadic function.
        ; f2 accepts regular old values, though, and r1 could
        ; concievably have many values in it.
             (r2s (mapcar f2 r1)))
          ; now r2s contain the results of applying f2 to ALL
          ; the values in r1.  Each of these values in r2s is
          ; itself a list, because f2 always returns a list (it 
          ; is a monadic function itself)
             (reduce #'append r2s)
          ; Well, we want a list of things in the monad, not a
          ; list of lists, so we just append all the r2s together 
          ; (see footnote #1).
          ; Now we've got a list of lisp values, which  means
          ; that this lambda is a monadic function in its own
          ; right.  Success!!
          ))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(n.b. If something about &lt;code&gt;r2s&lt;/code&gt; type strikes you as odd, see footnote 1.)&lt;/p&gt;

&lt;p&gt;Pause to consider what happened here.  We apply &lt;code&gt;f1&lt;/code&gt; to the input,
which produces a list (a &lt;em&gt;monadic value&lt;/em&gt;).  &lt;code&gt;F2&lt;/code&gt; takes values, not
monadic values, so we just &lt;code&gt;mapcar&lt;/code&gt; &lt;code&gt;f2&lt;/code&gt; over the values in the list.
&lt;code&gt;F2&lt;/code&gt;, like &lt;code&gt;f1&lt;/code&gt; returns a list, so now we have a list of lists, which
we concatenate into a big list, which is now our output monadic value.&lt;/p&gt;

&lt;p&gt;I hate when, in the course of a didactic development, something
springs from nowhere with the proviso that "it will be useful later."
This kind of thing is typical of physics and math texts, and I think
its part of the reason they are so difficult to read.  However, for
the sake of future utility, we are going to factor out a piece of the
above code.  Consider the function &lt;code&gt;list-bind&lt;/code&gt; which takes a monadic
value and a monadic function and returns a new monadic value:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defun list-bind (mon-val mon-fun) ; -&amp;gt; mon-val 
       (let ((results (mapcar mon-fun mon-val)))
          (reduce #'append results)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can rewrite &lt;code&gt;list-func-compose&lt;/code&gt; like this, now:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defun list-func-compose (f2 f1)
  (lexical-let ((f1 f1)
                (f2 f2))
    (lambda (value) 
       (let ((monadic-value (funcall f1 value)))
          (list-bind monadic-value f2)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, what is so special &lt;em&gt;a priori&lt;/em&gt; about &lt;code&gt;list-bind&lt;/code&gt; (&lt;em&gt;post priori&lt;/em&gt;,
its one of the monad functions which any monad needs)?  &lt;code&gt;List-bind&lt;/code&gt;
is, in a way, the fundamental operation relating monadic functions and
monadic values in the list monad.  &lt;code&gt;List-func-compose&lt;/code&gt; might seem more
directly useful, but it is kind of strange that the resulting function
takes a non-monadic value and returns a monadic one.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;List-func-compose&lt;/code&gt; relates monadic functions and other monadic
functions, but it doesn't reveal much about how they are combined with
values.  &lt;code&gt;List-bind&lt;/code&gt; describes that process, and it turns out this is
more interesting.&lt;/p&gt;

&lt;p&gt;Another way of thinking of it is that if &lt;code&gt;list&lt;/code&gt; describes how to turn
a value into a monadic value, &lt;code&gt;list-bind&lt;/code&gt; relates a mondadic function
to a function which both operates on and returns monadic values.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://ubuntuone.com/p/vtn/" alt="Monadic Bind" title="Monadic Bind" /&gt;&lt;/p&gt;

&lt;p&gt;Ok, believe it or not, we've basically covered the entire sequence
monad already.  All that is left is to really get the idea and to
understand how it relates to Haskell's do notation and the canonical
monad operations.  But it bears repeating - "do notation" isn't the
same thing as monads.  It is just syntactic sugar.  Its might be best
to understand monads before do notation, depending on your
temperament. &lt;/p&gt;

&lt;p&gt;Let's try things out, however.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(funcall (list-func-compose #'friends-of #'friends-of) :leo) 
(:lea
 :leo :sally :andrew :catherine :leo :jane :ted :leo :jane :andrew
 :harvey :sally :catherine :lea :leo :sally :andrew :catherine :leo
 :jane :leo :lea :leo :james :harvey :harvey :sally :ted :leo :lea
 :harvey :jane :andrew :lea :leo :james :sally :ted :james :andrew
 :catherine :ted :lea :leo :sally :jane :leo :ted :leo :lea :leo
 :jane :catherine :catherin :leo :lea :leo :james :harvey :harvey
 :sally :ted :lea :leo :sally :jane :leo :ted :leo :lea :leo :jane
 :catherine :catherin)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(We're probably really interested in the unique values of this list.
We could use something called the set monad to get those, or we could 
just pass this result through &lt;code&gt;unique&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Pretty anti-climactic, for sure.  But we can do more interesting
things:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(funcall 
  (list-func-compose 
     (mutual-friends-with :lea) #'friends-of)
  :leo)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This composition gives a list of the friends of Leo who are also
friends with Lea.  As indicated above, what we've done here is
inserted a parameterized monadic function into the composition.  This
turns out to be useful.  It also hints at the next step.  What if we
wanted to parameterize a monadic function based on some of the values
"coming down the pipe" in the monad?  How could we do that?  In a
regular function composition all those values are invisible.  Is there
way we can name them which preserves the utility of this approach?&lt;/p&gt;

&lt;h3&gt;Stepping Back&lt;/h3&gt;

&lt;p&gt;Monads in one sentence: a monad is set of operations which relate
specific functions called &lt;em&gt;monadic functions&lt;/em&gt; and specific values,
which are either naked &lt;em&gt;values&lt;/em&gt; (the input type to &lt;em&gt;monadic
functions&lt;/em&gt;) or &lt;em&gt;monadic values&lt;/em&gt;, which is the output type of monadic
functions.  &lt;/p&gt;

&lt;p&gt;In particular, the &lt;em&gt;bind&lt;/em&gt; operation knows how to pull values out of
monadic values, apply monadic functions to them, and collect all the
resulting monadic values into one big monadic value.  Using &lt;em&gt;bind&lt;/em&gt; we
can compose or otherwise manipulate monadic functions in a controlled
way.&lt;/p&gt;

&lt;p&gt;Everything else is window dressing.&lt;/p&gt;

&lt;h3&gt;Window Dressing&lt;/h3&gt;

&lt;p&gt;Let's talk about &lt;code&gt;let*&lt;/code&gt;.  We've been twisting up our brains into knots
over monads, so let's let those knots untwist and return to this simple,
clean, construct.  In Lisp, variable bindings are explicitly
introduced.  You don't just declare a variable and go, you create a
context for that variable with a &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;let*&lt;/code&gt; statement.  You
probably know that if you only had &lt;code&gt;lambda&lt;/code&gt; and &lt;code&gt;defmacro&lt;/code&gt; you could
define &lt;code&gt;let&lt;/code&gt; like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defmacro my-let (bindings &amp;amp;rest body)
  (funcall (lambda ,(mapcar #'car bindings)
             ,@body)
      ,@(mapcar #'cadr bindings)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;eg:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(my-let ((x 1) (y 2)) (+ x y))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;expands to&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(funcall (lambda (x y) (+ x y)) 1 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It is clear that x and y can't depend upon one another in this form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(funcall (lambda (x y) (+ x y)) 1 (+ x 2))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Obviously won't work (why?).  &lt;code&gt;let*&lt;/code&gt; is the form which lets us &lt;em&gt;chain
together&lt;/em&gt; multiple variable bindings so that previous bindings are
active during the expression form of subsequent bindings.  Of course
you can implement it as a series of nested &lt;code&gt;let&lt;/code&gt;s, but its useful to
write an implementation using only &lt;code&gt;lambda&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defun empty? (x) (not x)) ; sugar to test if a list is empty.
                           ; nil is the empty list, (not nil) -&amp;gt; t
(defmacro my-let* (bindings &amp;amp;rest body)
  (cond
    ((empty? bindings) `(progn ,@body))
    (t 
     `(funcall (lambda (,(car (car bindings)))
          (my-let* ,(cdr bindings) ,@body))
        ,(cadr (car bindings))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don't get spooked by this recursive macro.  All it means is that&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(my-let* ((x 10)
          (y (+ x 11)))
   (+ x y))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;expands to&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(funcall 
      (lambda (x)
        (funcall (lambda (y) (progn (+ x y))) (+ x 11))
     10))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That is, each subsequent bind value expression is evaluated in the
context of a function where the previous binding symbols are already
bound. &lt;/p&gt;

&lt;p&gt;Holy balls!  This is almost function composition!  The &lt;code&gt;lambda&lt;/code&gt;
containing our body (the &lt;code&gt;progn&lt;/code&gt; form) is &lt;code&gt;f1&lt;/code&gt; and each containing
&lt;code&gt;lambda&lt;/code&gt; represents a composition onto this inner function.  The only
difference is that we're threading function &lt;em&gt;application&lt;/em&gt; through the
composition in such a way as to provide named values into more deeply
nested contexts.  Things are about to get serious!&lt;/p&gt;

&lt;p&gt;Also, we are just throwing the word bind around like crazy!  Does this
use of the word bind have something to do with the &lt;code&gt;list-bind&lt;/code&gt; function?&lt;/p&gt;

&lt;p&gt;You bet it does!  You know, ordinarily I could take or leave Lisp-2's.
It seems kind of crufty to me to have to worry about a second
namespace, to have to remember to type (funcall f-var a b c) or
(funcall #'f a b c) but in this one case, its actually really useful
that we have to write out funcall explicitly.  Its useful because it
reminds us something is going on there.  What would happen if we
replaced those &lt;code&gt;funcall&lt;/code&gt;s in the above expansion of &lt;code&gt;let*&lt;/code&gt; with some
other function?  Let's kick this can around a bit, and see if
something falls out, shall we?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defun regular-bind (v f)
  (funcall f v))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So in the &lt;code&gt;let*&lt;/code&gt; expansion funcall is only ever taking functions with
one argument, so the first thing we do is declare a new function which
applies a single argument function to a single value.  Why did we name
it &lt;code&gt;regular-bind&lt;/code&gt;?  Well, think about the expression:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(regular-bind 10 (lambda (x) (+ x 1))); -&amp;gt; 11
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In &lt;code&gt;(lambda (x) (+ x 1))&lt;/code&gt; &lt;code&gt;x&lt;/code&gt; is &lt;em&gt;unbound&lt;/em&gt;.  That is what a lambda is,
essentially, a chunk of code in which the arguments are unbound,
waiting to be &lt;em&gt;bound&lt;/em&gt; so the body can be evaluated.  &lt;code&gt;Regular-bind&lt;/code&gt; is
just a function which &lt;em&gt;binds&lt;/em&gt; a value (10, above) to a variable and
executes the code.  Note: &lt;code&gt;funcall&lt;/code&gt; takes &lt;code&gt;f&lt;/code&gt; and then &lt;code&gt;v&lt;/code&gt;, but
&lt;code&gt;regular-bind&lt;/code&gt; takes &lt;code&gt;v&lt;/code&gt; and then &lt;code&gt;f&lt;/code&gt;.  This might make more sense
depending on how you read code ("bind v in f") or it might not.  It is
just convention.  It is called &lt;code&gt;regular-bind&lt;/code&gt; because it doesn't
really do anything that funcall doesn't already do.  Monads, though,
are all about &lt;em&gt;irregular&lt;/em&gt; bind operations.&lt;/p&gt;

&lt;p&gt;Let's rewrite &lt;code&gt;let*&lt;/code&gt; so that it uses bind instead of funcall:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defmacro my-let* (bindings &amp;amp;rest body)
  (cond
    ((empty? bindings) `(progn ,@body))
    (t 
     `(regular-bind
        ,(cadr (car bindings))
        (lambda (,(car (car bindings)))
                      (my-let* ,(cdr bindings) ,@body))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Easy enough.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;let*&lt;/code&gt; is how you thread together computations in the "identity
monad".  You may have heard someone say something like "regular lisp
lives in the identity monad."  This is what they mean.  Normally, all
bind does is associate a value with a symbol and go.  The above &lt;code&gt;let*&lt;/code&gt;
expression now expands to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(funcall 
      (lambda (x)
        (regular-bind  (+ x 11) (lambda (y) (progn (+ x y)))))
     10)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should be chomping at the bit now, because we've basically
invented do notation.  The question you should be asking is "What
happens when we replace bind with some other function?"  Let's make a
slightly newer macro to play with that idea.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defmacro monadic-let*-inner (bind-symbol binders &amp;amp;rest body)
   (cond 
     ((empty? binders) `(progn ,@body))
     (t 
       (let ((symbol (car (car binders)))
             (bind-expression (cadr (car binders)))
             (leftover-bindings (cdr binders)))
      `(funcall ,bind-symbol 
          ,bind-expression
           (lambda (,symbol)
              (lexical-let ((,symbol ,symbol)) ; create a lexical 
                                               ; over ,symbol.  
                                               ; we want one
                                               ; for lots of monads.
               (monadic-let*-inner 
                   ,bind-symbol ,leftover-bindings ,@body))))))))

(defmacro monadic-let* (bind-f-expression binders &amp;amp;rest body)
  (let ((bind-symbol (gensym "temporary-bind-symbol-")))
    `(let ((,bind-symbol ,bind-f-expression))
          (monadic-let*-inner ,bind-symbol ,binders ,@body))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This new form takes an expression which evaluates to a bind function,
a set of let-like binders, and a list of body forms and then it just
builds the same code as a &lt;code&gt;let*&lt;/code&gt; except that it uses the specified
&lt;code&gt;bind&lt;/code&gt; operation rather than a mere funcall.  &lt;code&gt;Let*&lt;/code&gt; sequences
computations.  &lt;code&gt;Monadic-let*&lt;/code&gt; sequences operations &lt;em&gt;through a monad,&lt;/em&gt;
the behavior of which is specified in &lt;code&gt;bind&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Let's try this crazy thing out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(monadic-let* 
       #'list-bind 
       ((x '(1 2 3)) 
        (y (list (+ x 1) (- x 1))))
     (list y)) ; -&amp;gt; (2 0 3 1 4 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Congratulations, friends, we are now living in the list monad.  &lt;/p&gt;

&lt;p&gt;Let's think about what this is doing one more time.  We have a series
of expressions (the right hand portion of each binding form).  The
monadic bind operation assumes each expression is a monadic value.  In
our case, these values are lists of lisp data.  Rather than bind the
result of this expression to the symbol on its left directly, our bind
specifies that we should bind the symbol to each value inside the
monad in turn, evaluate the subsequent binding expressions in the same
way, evaluate the body, which must be a monadic value, collect each of
those values, and then, finally, combine them back into a monadic
value again before returning the result.&lt;/p&gt;

&lt;h3&gt;That is really complicated&lt;/h3&gt;

&lt;h4&gt;Why would anyone do this?&lt;/h4&gt;

&lt;p&gt;The reasons for doing something like this are as varied as the kinds
of monads that are out there.  In Haskell, monads are a way of writing
imperative style code in a pure fashion, but even in non-static,
non-pure languages, monads can pull some neat tricks.  The sequence
monad, combined with &lt;code&gt;monadic-let*&lt;/code&gt; is essentially a list
comprehension.  Suppose we want to collect all the combinations of two
numbers less than 100 whose sum is less than 10.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(monadic-let*
 #'list-bind 
 ((x (range 1 101))
  (y (range 1 101)))
  (if (&amp;lt; (+ x y)  10) (list (vector x y)) nil))

;-&amp;gt; ((1 1) (1 2) (1 3) (1 4) (1 5) (1 6) (1 7) (1 8) 
       (2 1) (2 2) (2 3) (2 4) ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we simply return the empty list when we don't want to put
anything into the monad because appending the empty list onto
something does not change it.  Other applications in an impoverished
language like emacs is to use the continuation monad to fake
co-routines or to use a stream monad to make dealing with lazy lists
easier.  We'll revisit this in another post.&lt;/p&gt;

&lt;h3&gt;Notes about the monads.el library&lt;/h3&gt;

(note: I've made some changes to the `monads.el` library so that there are monad sequencing forms that cover a large range of possible intents.  I've also renamed some of the forms, which I've tried to reflect here.  If you can't get some code to run, look at `monads.el`.  Each of the forms is documented.)

&lt;p&gt;It turns out its handy in the context of monadic operations to have
&lt;code&gt;bind&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt; (and some other things) dynamically defined, so in
the &lt;code&gt;monads.el&lt;/code&gt; library, &lt;code&gt;monadic-let*&lt;/code&gt; is slightly different.  In
that library, monads are defined as hash tables containing &lt;code&gt;:m-bind&lt;/code&gt; and
&lt;code&gt;:m-return&lt;/code&gt; keys which associate with the appropriate functions.  Eg:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(defvar monad-seq 
  (tbl! 
   :m-zero (list)
   :m-return (lambda (x) (list x))
   :m-bind (lambda (v f) (apply #'append (mapcar f v))))
 "The list/sequence monad.  
  Combine computations over multiple possibilities.")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note here that &lt;code&gt;tbl!&lt;/code&gt; is is just sugar for &lt;code&gt;make-hash-table&lt;/code&gt;.  The
nearest equivalent to what we've called &lt;code&gt;monadic-let*&lt;/code&gt; is &lt;code&gt;mlet&lt;/code&gt;
which takes as its first argument a monad represented as a hash table.
In addition to the bind-chaining described above, it introduces a
dynamic context wherein the functions &lt;code&gt;m-bind&lt;/code&gt; and &lt;code&gt;m-return&lt;/code&gt; are
defined for that monad.  Because we're in emacs, and everything just
sits in one giant rotting pool of symbols, we prefix the monadic
functions with &lt;code&gt;m-&lt;/code&gt; to avoid future name collisions.  
&lt;pre&gt;&lt;code&gt;
(mlet
 monad-seq
 ((x (range 1 101))
  (y (range 1 101)))
 (if (&amp;lt; (+ x y) 10) (m-return (list x y)) nil))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that we can use &lt;code&gt;m-return&lt;/code&gt; inside &lt;code&gt;mlet&lt;/code&gt;-like forms, because
they define them automatically.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Monads.el&lt;/code&gt; defines several common monads in addition to the
sequence monad.  These include the state monad, the continuation
monad, the set monad (parameterized on an equality predicate), an
Error monad, and a Maybe monad.  &lt;code&gt;Monad-parse.el&lt;/code&gt; defines a monadic
parser combinator library which is really useful (to me, anyway).&lt;/p&gt;

&lt;h3&gt;Sujar&lt;/h3&gt;

&lt;p&gt;The monad library can make use of my implementation of Clojure-style
destructuring bind too.  One can write the above expression as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(domonad monad-seq 
 [x (range 1 101)
  y (range 1 101)]
 (if (&amp;lt; (+ x y) 10) (m-return (list x y)) nil))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Besides the superficial change of using a vector for the binding
expressions, you can use arbitrary nested binding forms inside.  If
you are familiar with clojure, it works basically like that, except
that tables are destructured with &lt;code&gt;[:: ]&lt;/code&gt; instead of &lt;code&gt;{ }&lt;/code&gt; and you can
use table-destructuring on hash tables and alists.  &lt;/p&gt;

&lt;h3&gt;Conclusions&lt;/h3&gt;

&lt;p&gt;For me, monads didn't hit home until I walked through the process of
creating a new monad from scratch based on my own ideas.  This is
described in &lt;code&gt;weighted-graph-monad.el&lt;/code&gt;, but in order to move forward
in our discussion of Kanren, the logic language, we'll need to develop
a stream monad, which facilitates work with lazy lists.  That will be
the &lt;a href="http://dorophone.blogspot.com/2011/04/deep-emacs-lisp-part-2.html"&gt;next example&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Footnote 1: Here is a bit of a confusing thing.  The monadic values of
the list monad are lists of lisp data types.  So a list of lists is,
in fact, a monadic value, which means that &lt;code&gt;r2s&lt;/code&gt; above could be
considered, itself, to be a monadic value.  If this didn't occur to
you, just skip the rest of this note.  I bring it up only because if
it has occured to you, it might be somewhat confusing.  You might ask:
why don't we just return &lt;code&gt;r2s&lt;/code&gt; instead of &lt;code&gt;(reduce #'append r2s)&lt;/code&gt;?
This kind of of conceptual confusion is related to the fact that Lisp
is not statically typed (or is &lt;a href="http://existentialtype.wordpress.com/2011/03/19/dynamic-languages-are-static-languages/" title="Inflammatory Writ"&gt;statically typed but you are
restricted to a single type of which the various "types" of values
constitute classes&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;In a statically typed language the sequence monad would be
parameterized on the type of its contents, which we'd specify where we
were using it.  So we'd have a type like &lt;code&gt;SeqM Int&lt;/code&gt; (read that as "the
sequence of ints monad") which would tell use that our monadic values
were lists of integers &lt;em&gt;only&lt;/em&gt;, and consequently &lt;code&gt;r2s&lt;/code&gt; above would be
manifestly &lt;em&gt;not&lt;/em&gt; a monadic value, and it would be more obvious that we
needed to append its contents together before recapturing a monadic
value.  In a dynamic language this isn't as obvious, but its still
conceptually necessary.  The upshot is that the fact that &lt;code&gt;r2s&lt;/code&gt; is a
monadic value is a &lt;em&gt;coincidence&lt;/em&gt;, the essence of the list monad is
still to &lt;em&gt;combine&lt;/em&gt; the results of monadic functions, rather than to
merely &lt;em&gt;collect&lt;/em&gt; them, which is what returning &lt;code&gt;r2s&lt;/code&gt; itself would
represent. If you &lt;em&gt;did&lt;/em&gt; read this despite my warning, you're probably
really confused.  Its ok.  This stuff is confusing at first.)&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Footnote 2: This is a long, diversionary footnote.  Probably its best
to finish the rest of the document before reading.&lt;/p&gt;

&lt;p&gt;For some reason I find it useful/pleasurable/amusing to consider the
question of monads in languages without scope at all.  Namely, stack
languages like &lt;a href="http://factorcode.org/" title="Factor Language"&gt;Factor&lt;/a&gt; or &lt;a href="http://www.latrobe.edu.au/philosophy/phimvt/joy/j02maf.html" title="Joy"&gt;Joy&lt;/a&gt;.  Not only are these languages
without scope, by default, variables are not even named.  Values are
merely pushed onto and pulled off of a stack which is passed
implicitly between "words," the analog of functions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(require 'with-stack)
(||| 4 4 + 3 -) ; -&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we are using a stack language interpreter which lives inside
emacs lisp.  You can get "with-stack.el" from my github.&lt;/p&gt;

&lt;p&gt;Without a means of binding variables, "lambdas" become merely
"quotations," simple lists of literals and words which can be executed
by words like &lt;code&gt;call&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(||| 4 '(4 +) call '(3 -) call) ; -&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the "emacs stack language" quotation serves directly as the quote
operator, and quotations are represented as lists.  Executing a
quotation is the same looping over it, executing each word as it is
encountered, or pushing atoms onto the stack.  &lt;/p&gt;

&lt;p&gt;The nakedness of these quotations encourages languages to use them as
blocks in control structures:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(||| 1.0 1&amp;gt;random* .5 &amp;gt; '("true" print) '("false" print) if)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above will print "true" 50% of the time and "false" 50% of the
time.  I don't want to get bogged down with a full &lt;code&gt;with-stack&lt;/code&gt;
tutorial, but "1.0 1&gt;random*" means "push 1.0 onto the stack and call
the emacs lisp function &lt;code&gt;random*&lt;/code&gt; with 1 argument from the stack,
pushing the result".  The word &lt;code&gt;if&lt;/code&gt; takes a boolean and two
quotations, executing the first when the bool is true and the second
when it is false.  &lt;/p&gt;

&lt;p&gt;Ok, so quotations are lambdas.  What are monadic quotations of the
list monad?  These must be quotations which take a value off the stack
and return a list of values.  The quotation version of &lt;code&gt;return&lt;/code&gt; for the
list monad would be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(||| word: list-return 1&amp;gt;list end:)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That is, take a value off the stack and return a list with that value
in it.  &lt;/p&gt;

&lt;p&gt;What about &lt;code&gt;bind&lt;/code&gt;?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(require 'stack-words)
(||| word: list-bind ;( mv f (v ;; mv) ;; mv )
     map '(append) reduce end:)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Characteristically, the word version is quite terse.  Interestingly,
we hardly need to have an analog of do notation, because variables are
not named and application is merely juxtaposition:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(||| word: bi&amp;gt;list ;( item qtn1 qtn2 ;; ( res1 res2 ) )
     bi 2&amp;gt;list end:)
(||| '(1 2 3) 
     '( '(1 +) '(1 -) bi&amp;gt;list )
     list-bind
     '( '(3 +) '(3 -) bi&amp;gt;list )
     list-bind) ;-&amp;gt; (5 -1 3 -3 6 0 4 -2 7 1 5 -1)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(The word &lt;code&gt;bi&amp;gt;list&lt;/code&gt; takes an item and two quotations, applies the
quotations each to the item and collects the results into a list of
two elements.  It assumes the quotations take one item off and push
one onto the stack.)&lt;/p&gt;

&lt;p&gt;Wherever we want to use monadic behavior, we simply replace &lt;code&gt;call&lt;/code&gt;
with &lt;code&gt;list-bind&lt;/code&gt;.  We can write a word to make this more pleasant:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
(||| word: fold-bind-into ; ( qtnseq bind-qtn ;; qtnseq )
      '( curry ) curry map 1&amp;gt;flatten-once end:)
(||| word: monadically ; ( init-val qtnseq bind-qtn ;; result )
       fold-bind-into call end:)

(||| '(1 2 3)
       {{ '( '(1 +) '(1 -) bi&amp;gt;list )
          '( '(3 +) '(3 -) bi&amp;gt;list ) }}
     '(list-bind) monadically )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where &lt;code&gt;{{&lt;/code&gt; is a parsing word which makes a list of what is on the
stack between &lt;code&gt;{{&lt;/code&gt; and &lt;code&gt;}}&lt;/code&gt;.  We could have used quote to make the
list of quotations, but we'd be getting a lot of quote marks on the
screen.  All this word does is fold the contents of a binding
quotation (here a simple call to &lt;code&gt;list-bind&lt;/code&gt;) into the sequence of
quotations we want to operate monadically.  Then we simply call the
resulting quotation.  &lt;/p&gt;

&lt;p&gt;Assuming some comfort with the idioms of concatenative languages, the
result is an incredibly simple demonstration of the essence of monadic
operation.  It strikes me that it is often the lowest-level words
which are the hardest to read and write in concatenative languages,
while high level code has an almost comic simplicity. I wonder what this means?&lt;/p&gt;

&lt;hr /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-7401657156835283293?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/7401657156835283293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=7401657156835283293' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7401657156835283293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/7401657156835283293'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/04/deep-emacs-part-1.html' title='Deep Emacs Lisp Part 1 (Basically, a Monad Tutorial)'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-1039148804693286855</id><published>2011-03-21T09:14:00.000-07:00</published><updated>2011-04-09T18:55:41.354-07:00</updated><title type='text'>The Fetishist</title><content type='html'>Slow mottled gray skies, the empty plains&lt;br&gt;
somewhere in the blown out corridor from&lt;br&gt;
Houston to Galveston.  Highway and plane &lt;br&gt;
noise, far enough for privacy but frisson-&lt;br&gt;
near enough for wanderers to run, run &lt;br&gt;
the risk of observation, forced sight:&lt;br&gt;
so much more than the dead camera, glum&lt;br&gt;
in its facile adsorption of light.&lt;br&gt;
An old abandoned pool languishing right&lt;br&gt; 
behind an encroached upon foundation,&lt;br&gt;
obscenely, a chimney still stands, a blight&lt;br&gt;
within a blight within a blight within station-&lt;br&gt;
ary air. He mugs against the gray sky&lt;br&gt;
and falls into shit for the camera's eye.&lt;br&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-1039148804693286855?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/1039148804693286855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=1039148804693286855' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1039148804693286855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1039148804693286855'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2011/03/fetishist.html' title='The Fetishist'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-208881952141787264</id><published>2010-08-21T09:07:00.000-07:00</published><updated>2011-04-17T08:43:55.810-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reader Macros'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='vaguely incorrect'/><category scheme='http://www.blogger.com/atom/ns#' term='scope'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>How to use a codewalking macro to implement a lexically scoped delay in Emacs Lisp</title><content type='html'>&lt;p&gt;Lazyness is all the rage in these days of Haskell and Clojure, and it
comes in many forms.  Haskell is lazy by default, clojure supports
laziness extensively, but it is still voluntary.  Some scheme
implimentations provide &lt;em&gt;promises&lt;/em&gt;, which are delayed computations,
and almost every high level programming language provides the basic
building block of laziness via closures and anonymous functions.  &lt;/p&gt;

&lt;p&gt;We might, for instance, implement laziness in Scheme with a macro
which transforms:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(delay expr1 ... exprN)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lambda () expr1 ... exprN)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and then &lt;code&gt;force&lt;/code&gt; is simply implemented as a function call:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (force thunk) (thunk))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Something which I think is a key insight here is that &lt;em&gt;lambda is
intelligent quotation&lt;/em&gt;.  I'm not a programming language theorist, so I
don't know if this constitutes a particularly novel insight, but to me
it was a crucial step towards understanding the semantics underlying
most programming languages.  What does "lambda is intelligent
quotation" mean, though?  Well, we can imagine writing a very
primitive &lt;code&gt;delay&lt;/code&gt; macro thusly (in some kind of lisp):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro (delay . args) `(quote (begin ,@args)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and then &lt;code&gt;force&lt;/code&gt; is just &lt;code&gt;eval&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define force eval)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While this certainly "gets the job done" for expressions containing
only atoms, it doesn't work on any expression containing variables.
Depending on how your Lisp works, and when you call &lt;code&gt;force&lt;/code&gt;, you can
get anything from a straight up error to a mysterious result.  If you
use a &lt;code&gt;lambda&lt;/code&gt; instead, however, you tell your language "quote this
expression, but make sure all the symbols in the expression refer to
the values described by the lexical context."  &lt;/p&gt;

&lt;p&gt;So here is a question: if we want to implement &lt;code&gt;delay&lt;/code&gt; and &lt;code&gt;force&lt;/code&gt; in
emacs lisp, how do we do it?  An extremely naive guess would be just
to use &lt;code&gt;lambda&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro delay (&amp;amp;rest body) `(lambda () ,@body))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Force is still:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun force (thunk) (funcall thunk))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But this doesn't quite work as we expect.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((thunk 
  (let ((x 10))
    (delay (+ x x)))))
      (force thunk))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will produce an error - that &lt;code&gt;x&lt;/code&gt; is undefined.  This is because,
vexingly, Emacs Lisp is an old Lisp and has dynamic scope.  That means
&lt;code&gt;x&lt;/code&gt; in the lambda created by delay refers to whatever the outermost
&lt;code&gt;x&lt;/code&gt; binding is, which, by the time we've arrived at the  &lt;code&gt;force&lt;/code&gt; is
just nothing.  &lt;/p&gt;

&lt;p&gt;"Fear not," I hear you say!  "I've read that Emacs Lisp has lexical
closures via the cl.el package.  Why don't we just use those?" &lt;/p&gt;

&lt;p&gt;The item in question is &lt;code&gt;cl.el&lt;/code&gt;'s &lt;code&gt;lexical-let&lt;/code&gt; form.  &lt;code&gt;Lexical-let&lt;/code&gt;
lets you do things like &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(funcall (lexical-let ((x 10))
           (lambda () (+ x 101)))) ; -&amp;gt; 111
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It uses some trickery which I frankly haven't gotten too deeply into
to simulate a lexical closure so that lambda grabs the right variable
in the body of a &lt;code&gt;lexical-let&lt;/code&gt;.  However, note that we have to
enumerate the variables we'd like to close over.  We could write a
macro to the effect of &lt;code&gt;delay-with&lt;/code&gt; which had a body and a list of
variables to close over, but can we do better?&lt;/p&gt;

&lt;p&gt;We can, in fact, do better with a &lt;code&gt;codewalking macro&lt;/code&gt;.  Most macros
which take a "body" do something simple.  A good example is emacs
lisp's &lt;code&gt;save-excursion&lt;/code&gt;, which basically just inserts code to save and
restore the current editing state around an unmodified body.  &lt;/p&gt;

&lt;p&gt;A codewalking macro takes a set of lisp forms, walks over them,
inspecting and transforming, and finally generates a perhaps radically
transformed new set of lisp expressions.  We can write a codewalking
macro to implement &lt;code&gt;delay&lt;/code&gt;.  All it has to do is walk over the body of
the code and collect all the symbols and functions that are lexically
free in it, and then lexically close around these symbols.  Then, as
if by magic, delay becomes a "smart" quotation.&lt;/p&gt;

&lt;p&gt;So if our body is something like &lt;code&gt;(+ x y z)&lt;/code&gt;, then our job is pretty
easy: &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;z&lt;/code&gt; are obviously free variables in the
expression, so our macro takes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(delay (+ x y z))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lexical-let ((x x)
              (y y)
              (z z))
  (lambda () (+ x y z)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we put this in context:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq thunk (let ((x 10) (y 11) (z 12))
              (delay (+ x y z))))
(force thunk) ; -&amp;gt; 33
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that above the &lt;code&gt;let&lt;/code&gt; expression forms a dynamic scope, but our
delay macro calculates the free variables and lexically closes over
them before the dynamic values are popped of the stack.  Then, when
&lt;code&gt;thunk&lt;/code&gt; is called, everything works out all right.&lt;/p&gt;

&lt;p&gt;A couple of objections ought to be appearing in your mind right now.
One of them focuses on the fact that lisp expressions are not as
simple as just function calls, and that, in particular, a variety of
lisp expressions introduce variable bindings which influence nested
expressions.  Most obviously,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq thunk (let ((y 1001) 
                  ((x 10)))
            (delay (let ((x (+ x 100)))
                  (+ x y)))))
(force thunk)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;involves some thinking about what values we should close over.  Rather
obviously we need to capture the lexical binding of &lt;code&gt;y&lt;/code&gt;, but what
about &lt;code&gt;x&lt;/code&gt;.  Within the body of the inner &lt;code&gt;let&lt;/code&gt; expression, &lt;code&gt;x&lt;/code&gt; will
be dynamically bound, but the value it takes depends on the
non-lexically bound outer &lt;code&gt;x&lt;/code&gt;.  If we traversed the code tree, just
counting &lt;code&gt;x&lt;/code&gt; occurences, we might miss the fact that &lt;code&gt;let&lt;/code&gt; treats the
symbol part of its binding expressions differently than the value
parts.  Not only that, but we have to consider the dynamic and lexical
binding of function applications, which are treated separately in
emacs lisp because it is a lisp-2.  This problem is tractable,
however, because emacs lisp has a small number of simple fundamental
expression types.  We can, therefore, hope to write a parser which
traverses arbitrary lisp code, collecting information about which
variables are locally bound or lexically free within an expression.&lt;/p&gt;

&lt;p&gt;Here comes the second objection, however.  It is true that lisp
defines only a small number of fundamental special forms, but any
arbitrary piece of lisp code may be peppered with a variety of user
defined special forms created via macros.  It may be within our
capabilities to parse a lisp expression containing only fundamental
forms, but can we reasonably expect ourselves to parse the binding
information contained in the clauses of a &lt;code&gt;loop&lt;/code&gt; expression, much less
any unique, ad-hoc, user created macro?  The good news is that we
don't have to - we can call &lt;code&gt;macroexpand-all&lt;/code&gt; on the body before
parsing it, guaranteeing that we will have only naked lisp to parse.&lt;/p&gt;

&lt;p&gt;Now that those objections are out of the way, we can begin to tackle
the problem formally.  We'd like to write a function which reads a
piece of lisp code and and records the number of times a symbol is
used without a binding, either as a function or as a value.  Once we
have this list we can wrap up our expanded body in &lt;code&gt;lexical-let&lt;/code&gt; and
&lt;code&gt;labels&lt;/code&gt; forms, to provide local definitions of functions and values
which are lexically scoped.&lt;/p&gt;

&lt;p&gt;The skeleton to collect the symbol usage information is, thus;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun* collect-usage-info (form &amp;amp;optional (global-info (alist&amp;gt;&amp;gt;))
                                 (local-info (alist&amp;gt;&amp;gt;)))
"(collect-usage-info FORM &amp;amp;optional GLOBAL-INFO LOCAL-INFO)

Collects information about the usage of variables and functions in the form FORM.
When called recursively, it may be appropriate to provide GLOBAL-INFO, an alist 
describing the collected information so far, and LOCAL-INFO, an alist describing
the binding information of variables locally.  

GLOBAL-INFO is returned.  It is an alist of the form;
  (list 
    (&amp;lt;symbol&amp;gt; (&amp;lt;n-bound-symbol-usages&amp;gt;
               &amp;lt;n-bound-function-usages&amp;gt;
               &amp;lt;n-unbound-symbol-usages&amp;gt;
               &amp;lt;n-unbound-symbol-usages&amp;gt;))
    ...)

LOCAL-INFO is recursively defined, but is of the form
  (list 
    (&amp;lt;symbol&amp;gt; (&amp;lt;bound-as-symbol&amp;gt;
               &amp;lt;bound-as-function&amp;gt;))
    ...)

  Where bound-as-(function/symbol) are either t or nil."
  (cond
   ((symbolp form)
    (cond ((eq 't form) global-info)
          ((eq 'nil form) global-info)
          (t
           (dlet [[bound-s bound-f] (symbol-binding-info form local-info)
                  [s-count f-count s-unbound-count f-unbound-count] (symbol-bind-counts form global-info)]
             (alist&amp;gt;&amp;gt; global-info form (list 
                                        (if bound-s (+ 1 s-count) s-count)
                                        f-count
                                        (if (not bound-s) (+ 1 s-unbound-count) s-unbound-count)
                                        f-unbound-count))))))
   ((listp form)
    (cond 
     ((quotep form)
      global-info)
     ((prog-like form)
      (collect-usage-info-prog-like form global-info local-info))
     ((letp form)
      (collect-usage-info-let form global-info local-info))
     ((let*p form)
      (collect-usage-info-let* form global-info local-info))
     ((function-formp form)
      (collect-usage-info-function form global-info local-info))
     ((ifp form)
      (collect-usage-info-if form global-info local-info))
     ((condp form)
      (collect-usage-info-cond form global-info local-info))
     ((defunp form)
      (collect-usage-info-defun form global-info local-info))
     ((setqp form)
      (collect-usage-info-setq form global-info local-info))
     (t ; function application
      (collect-usage-info-prog-like (cdr form)
                                    (collect-usage-info-function `(function ,(car form)) global-info local-info)
                                    local-info))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Anyone familiar with SICP will recognize this pattern.  We have a case
statement which determines what sort of expression we are looking at,
and then dispatches handling of that expression type to a specific
handler, which may call &lt;code&gt;collect-usage-info&lt;/code&gt; recursively.  The base
cases are symbols and atoms.  Atoms are ignored, whereas symbols, when
encountered, are counted appropriately depending on the information in
&lt;code&gt;local-info&lt;/code&gt;.  You can see all the details in the source code,
available at my git-hub, but to give you an idea of how this works,
lets look at the case of the &lt;code&gt;let&lt;/code&gt; expression.  &lt;/p&gt;

&lt;p&gt;We detect a let-expression with the &lt;code&gt;letp&lt;/code&gt; predicate;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun letp (form) 
   (and (non-empty-listp form)
        (eq (car form) 'let)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(all these predicate functions are defined in &lt;code&gt;macro-utils.el&lt;/code&gt;.)&lt;/p&gt;

&lt;p&gt;Handling a &lt;code&gt;let&lt;/code&gt; form is relatively simple.  We will read each binding
clause, count the variable usage in the expression providing the value
thereof, and then collect all the symbols bound in the expression.  We
then call &lt;code&gt;collect-usage-info-prog-like&lt;/code&gt; on the body, providing an
augmented &lt;code&gt;local-info&lt;/code&gt; which indicates that the variables described in
the &lt;code&gt;let&lt;/code&gt; expression now have bindings.
&lt;code&gt;collect-usage-info-prog-like&lt;/code&gt; simply folds &lt;code&gt;collect-usage-info&lt;/code&gt; over
the forms in the body.&lt;/p&gt;

&lt;p&gt;This idea is repeated for all forms in the basic emacs-lisp language.
Using this function we can finally define our delay macro thusly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defmacro* delay (&amp;amp;body body)
  (let* ((expanded (macroexpand-all `(progn ,@body)))
         (info (collect-usage-info expanded))
         (unbound-symbols (get-unbound-symbols-list info))
         (unbound-functions (get-unbound-function-symbols-list info))
         (temporary-function-names 
           (loop for f in unbound-functions collect (gensym (format "%s-old-" f)))))
           `(lexical-let ,(loop for s in unbound-symbols collect `(,s ,s))
                          (labels ,(loop for old-f in temporary-function-names 
                                          and f in unbound-functions collect 
                                          (let ((arglist (gensym "arglist")))
                                            `(,old-f (&amp;amp;rest ,arglist) (apply #',f ,arglist))))
                                   (labels ,(loop for f in unbound-functions 
                                                   and old-f in temporary-function-names collect 
                                                   (let ((arglist (gensym "arglist")))
                                                     `(,f (&amp;amp;rest ,arglist) (apply #',old-f ,arglist))))
                                            (lambda () ,expanded))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As promised, there isn't much by way of magic here.  We expand the
&lt;code&gt;body&lt;/code&gt; as a progn-like form, collect the information about variable
binding from it, and then create the appropriate lexical closures.
The only wrinkle is in the precise use of the &lt;code&gt;labels&lt;/code&gt; form.
Functions introduced in a &lt;code&gt;labels&lt;/code&gt; form are allowed to be mutually and
self recursive, so we can't just do the equivalent of:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 10))
   (lexica-let ((x x))
       (+ x x)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which is,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(flet ((f (q) (+ q 1)))
  (labels ((f (&amp;amp;rest arglist) (apply #'f arglist)))
      (f 1)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because the labels form above introduces a recursive function &lt;code&gt;f&lt;/code&gt;
which calls itself indefinitately.  There are several possible
solutions to the problem.  The simplest, but perhaps not the best, is
to use two independent &lt;code&gt;labels&lt;/code&gt; forms, the first of which creates new
bindings for the functions, and the second of which creates the
lexical bindings with the correct names.  Perhaps the ultimate
solution is to pick new names, and walk the code, replacing each
function application with the new name.  This is probably overkill for
this example.&lt;/p&gt;

&lt;p&gt;So we've finally arrived!  Now we can delay and force to our heart's
content, and emacs will do its best to pretend it is a lexically
scoped language:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(let ((x 10))
   (setq thunk (delay (+ x 1))))
(force thunk) ;-&amp;gt; 11 
x ;-&amp;gt; undefined variable
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As an exercise to the reader, you can try to implement two functions
which are provided in the source code, but not reproduced here:
&lt;code&gt;lexical-lambda&lt;/code&gt; and &lt;code&gt;lexical-defun&lt;/code&gt;.  They have the following
behaviors.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(funcall 
  (let ((x 10))
    (lexical-lambda (y) (+ y x)))
  100) ; -&amp;gt; 110


(let ((x 10))
  (lexical-defun f (y) (+ x y)))

(f 10) ;-&amp;gt; 20
&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-208881952141787264?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/208881952141787264/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=208881952141787264' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/208881952141787264'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/208881952141787264'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2010/08/how-to-use-codewalking-macro-to.html' title='How to use a codewalking macro to implement a lexically scoped &lt;code&gt;delay&lt;/code&gt; in Emacs Lisp'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-8288888016017289873</id><published>2010-06-29T08:35:00.001-07:00</published><updated>2010-06-29T10:07:27.423-07:00</updated><title type='text'>The Fork As Blasphemer</title><content type='html'>&lt;p&gt;Few artifacts have insinuated themselves so comfortably into our lives as the modern fork, which is neither given with a superstitious penny taped to its tines&lt;sup&gt;1&lt;/sup&gt; nor is it ever mightier than the sword. This comfortable arrangement is reflected in the difficulty with which the amateur researcher finds information about the utensil. So far as I am able to ascertain, UNC has only two books on the subject of the history of flatware and one of which is a children's book. That book, "From Hand to Mouth," devotes one casually written chapter to the fork - a volume of text amounting to no more than 15 pages with large type, simple sentences and many illustrations.&lt;/p&gt;

&lt;p&gt;If, in frustration, one consults the internet&lt;sup&gt;2&lt;/sup&gt;, they will find a similar paucity of information. The wikipedia article on the fork appears to be lifted in its entirety from an article written for the Society for Creative Anachronism about the proper use of the fork while pretending to be a person from Europe's past. The basic format and flow of this article strongly suggests that its one source was "From Hand to Mouth," although a second 1927 book is cited which was unavailable to me.&lt;/p&gt;

&lt;p&gt;The fork's history, like that of many human conveniences, is lost to antiquity. Its steely fingers merely rise up from the mists of time sometime before the 11th century, when it was introduced in Europe for the purposes of eating. Before then, forks were used to serve and mince meat and to heave hay to horses, but at the table we still ate with our hands. In what may be one of the earliest uses of the fork, some mice asphyxiate themselves on forked twigs&lt;sup&gt;3&lt;/sup&gt;, running, I suppose, from whatever agonies mouse life presents.&lt;/p&gt;

&lt;p&gt;How Europeans began to use the fork for more than feeding and slicing pigs is better understood. The story goes that in the 11th Century Domenico Selvo, the Doge of Venice, acquired somehow a Byzantine wife. In the East the fork was apparently in common use at tables and she brought with her to that watery city a set of flatware for her personal use. This, surprisingly, engendered the scorn of the locals, who saw it as excessively delicate&lt;sup&gt;4&lt;/sup&gt;. Apparently her table manners were so noteworthy that when she died shortly after arriving, the passing was blamed on her use of the fork, which, the suggestion goes, caused her to waste away. But physical deterioration was the least of the problems facing people who chose to eat without dirtying their hands.&lt;/p&gt;

&lt;p&gt;"God in his wisdom has provided man with natural forks - his fingers. Therefore it is an insult to Him to substitute artificial metallic forks for them when eating." Giblin attributes this quotation to mysterious "church leaders" and provides only the most tenuous of context. The idea that such a statement carried even an iota of the Magisterium of the Roman Catholic Church is not so much doubtful as it is ridiculous, yet, this tiny quotation may be one of the most interesting things about the fork, as it implies that it was resisted for a moment on account of its violation of a divinely decreed natural order.&lt;/p&gt;

&lt;p&gt;This is not the only instance of this kind of reasoning in response to new technology. In 1752 or thereabouts Benjamin Franklin succeeded in demonstrating&lt;sup&gt;5&lt;/sup&gt; the sameness of electricity and the substance of lightning. Previous to this discovery, the caprices of the heavens had been associated for some time (perhaps all time) with the caprices of Heaven - and upon the introduction of Franklin's lightning rods, which sprouted, like fungus after a rain, on the roofs of Churches and Gunpowder Warehouses all across The United States, objections were raised to their use on account of the way they seemed to divert Divine Punishment. "An Entertainment for Angels," a very entertaining little book (even for us corporeals) about electricity in the Enlightenment, points out that the Papacy supported the use of rods to protect churches, but local leaders frequently objected on religious grounds&lt;sup&gt;6&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;What is interesting about both the controversy over the lightning rod and the fork is that neither are ongoing. The lightning rod and the fork have integrated themselves into our pictures of the human world. Franklin said of the objections to the rod "Surely the Thunder of Heaven is no more supernatural than the Rain, Hail or Sunshine of Heaven, against the inconvenience of which we guard by Roofs and Shades without Scruple." Like rain, hail and sunshine, both lightning and messy eating have become simple inconveniences to avoid rather than indications of a Divine Will.&lt;/p&gt;

&lt;p&gt;These two worlds humans live in, one of unavoidable divine consequences and another of mutable human problems, still coexist today of course. A person hardly passes without relatives declaring that it was, in the end, God's Will that he be taken - and yet before the death frequently every measure of human ingenuity is applied to the frustration of that Will. That the theology of free will is intricate and sensible is in many ways besides the point, since it hardly enters into the thinking of the average person. To be caught in a rain storm might have led to a protracted death by consumption in the time of Jane Austin, but now days a common cold isn't worth a moment's thought or prayer. Because we have wrested from the common cold its fatal power we no longer think of it as representing anything but the mundane.&lt;/p&gt;

&lt;p&gt;Can we imagine a world in which all human inconveniences have been dispensed with by science? Where voracious nano-machines destroy viruses and bacteria with an efficiency to which evolution is simply unable to adapt, where death is forstalled indefinitely by careful genetic manipulation? In such a world, where death will be reduced to gnawing at the edge of the human world in the realm of freak accidents, we will no doubt continue to blame our mountain climbing accidents, our spontaneous human combustions and other unavoidable inconveniences on some external agency.&lt;/p&gt;

&lt;p&gt;In any case, the fork is stuck comfortably in the heart of human destiny.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Footnotes:&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; Apparently it is the tradition in some cultures to attach a penny to the blade of a knife given as a gift. The penny is immediately returned to the giver as "payment" for the knife so as to mitigate the negative consequences of giving a knife, which supposedly heralds the end of the friendship.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;2&lt;/sup&gt; Who looks on the internet after checking the library?&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;3&lt;/sup&gt; This may be a lie.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;4&lt;/sup&gt; At least it was surprising to me initially. I then imagined what my reaction would be to a fellow diner taking from her hand bag a sterilized assortment of surgical tools and, with excessive precision, quartering and requartering her food (perhaps with the aid of a protractor or a compass) and then carefully placing it into her mouth one morsel at a time. To a Medieval whose every meal had been taken with his hands to no noticeable harm, the Princess' caution would probably have seemed similarly strange.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;5&lt;/sup&gt; "An Entertainment for Angels" by Patricia Fara tells that the first experiment was actually conducted by Frenchmen, with a retired soldier and a priest being the first humans to draw electricity from the sky. The famous kite scene was played out slightly later by Franklin, and so the image of the scientist replaced that of the soldier and the priest - a fairly evocative circumstance.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;6&lt;/sup&gt; The debate eventually solidified somewhat into an argument about whether rods should be pointed or rounded. For political and scientific reasons, the main proponents of using rounded rods were Jean Nollet of France and Benjamin Wilson of Great Britain. For religious and practical reasons, it was thought that using rounded rods was preferable because they did not attract lightning as well, the idea being to avoid the collateral damage of lightning strikes and specifically attracting the wrath of God. Pointed rods eventually won out, but apparently the debate still churns in the form of lightning rod dissipators - whose stated purpose is to reduce the likelihood of lightning strikes and whose efficacy is still debated.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-8288888016017289873?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/8288888016017289873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=8288888016017289873' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8288888016017289873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8288888016017289873'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2010/06/fork-as-blasphemer_29.html' title='The Fork As Blasphemer'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-60818669341543731</id><published>2010-04-12T05:58:00.000-07:00</published><updated>2011-08-02T15:29:08.733-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='technology'/><category scheme='http://www.blogger.com/atom/ns#' term='D.I.C.E.'/><category scheme='http://www.blogger.com/atom/ns#' term='Jesse Schell'/><category scheme='http://www.blogger.com/atom/ns#' term='transdehumanism'/><category scheme='http://www.blogger.com/atom/ns#' term='transhumanism'/><category scheme='http://www.blogger.com/atom/ns#' term='dystopia'/><category scheme='http://www.blogger.com/atom/ns#' term='horror'/><title type='text'>Farmville and the Face of Transdehumanism</title><content type='html'>&lt;p&gt;At this year's D.I.C.E. conference (Design, Innovate, Communicate, Entertain) Carnegie Melon's Jesse Schell gave a presentation which, even as it diverged into parody towards its energetic end, presented us with an incredibly depressing view of the future.  In 1984, George Orwell writes "If you want a picture of the future, imagine a boot stamping on a human face - forever."  Happily, this industrial image, from an industrial era, has not come to pass, but I don't think it is because of any underestimation of the benign nature of humankind on Orwell's part.  On the contrary, our negative impulses have only found more efficient means of expression, so that the idea of exploitation by a monolithic force seems quaint in a world of constant advertising, 20 page credit card contracts, and a political system so based on subverting the average citizen's rational decision making that the fate of the nation depends on who more skillfully manipulates the psychological vapors of the electorate.  It is no exaggeration to say that we have entire industries devoted to rational subversion of the human will.  If anyone can provide a better definition for contemporary advertising and political consulting businesses, I'd love to hear it.
&lt;/p&gt;&lt;br&gt;
&lt;p&gt;Almost every aspect of post-industrial collective action is rationalized or in the process of being rationalized.  Research is conducted on the best way to squeeze every ounce of efficiency out of workers for the lowest possible salaries.  Google, with its vast infrastructure, is leading the way in making sure that every ad you see is targeted directly to your preferences.  Modernity is swiftly devoting itself to the problem of identifying what it is you want, and offering it to you just as you want it the most.  In other words, the world is learning ever more clever ways to selfishly manipulate your psychology.
&lt;/p&gt;&lt;br&gt;
&lt;p&gt;Jesse Schell's talk extrapolates the primitive kind of manipulations you see in Facebook games like Farmville and Mafia Wars.  He openly discusses the ways in which these games exploit the psychology of adults and children, suggesting that such exploitation is the future of game design.  He imagines a world in which everything, from your reading habits, to your sleeping habits, is monitored at all times by benevolent corporations, who use that data to tug constantly on the strings of your motivation, to direct you to buy, to offer you your desires.  He imagines a government which uses a vast sensor network to gently encourage prosocial behavior.
&lt;/p&gt;&lt;br&gt;
&lt;p&gt;In one example, he hypothesizes that your toothbrush will be equipped with a sensor that monitors the amount of time spent brushing your teeth. Although he flirts with the possible benefits to dental hygiene, he eventually comes right out and says that the real value will come from the creation of a brushing "game" that will help drive sales for toothpaste companies.  With each instance, he dangles a possible good in front of the audience, only to say, actually, there is a profit stream to be made here.  If we get a little benefit out of it, all the better!
&lt;/p&gt;&lt;br&gt;
&lt;p&gt;His vision of perpetual surveillance by a myriad of omnipresent sensing devices, all as a means of shaping human behavior is similar to the vision of the transhumanists, where individuals extend their awareness and capabilities with the same kind of technology in the service of their will.  The transhumanist imagines that he will be at the center of a phalanx of personal information collectors, the product of which he can analyze with ever increasing computational resources made available by ever-cheapening, general purpose computer hardware.  But Schell's vision is the perverse opposite of transhumanism: technology becomes the means by which external forces exert a super-human control over the individual, not with the Orwell's boot or his comparatively clunky "Ministry of Information," but with rationally constructed, constantly adjusted psychological lures. Technology does not become ever more cheap and multipurpose, used by its owner for its owner's purpose.  It becomes fractured, each device a sensor only nominally controlled by the "owner", its collected data analyzed by the distributor, not the user.  This philosophical universe which tolerates such a conception of the future is transdehumanism.  In it, a distributed, technological storm turns the average person into a constantly manipulated purchaser of both real and psychological goods.
&lt;/p&gt;&lt;br&gt;
&lt;p&gt;I find the idea very distasteful, and I believe that the vast majority of humans, if they were to lay it up against whatever measuring stick they use to tally value, would agree.  Schell's talk should serve only as a roadmap of where not to go.
&lt;/p&gt;&lt;br&gt;
&lt;p&gt;The last ten years have seen an incredible proliferation of technology into the hands of people ordinarily deprived of such powerful means of cultural expression.  It was a common assumption that technology would inevitably cause all ships to rise, enhancing the expression of the individual will which is so crucial to the human experience.  Schell's imaginary future shows a different path, one which we should avoid. While continued agitation for open computing platforms and software is a good start, we should reconfigure our society to prevent the manipulation facilitated by our ever growing understanding of human psychology.  Right now if an advertiser invents a new mechanism of capturing your attention, or discovers via focus group testing a way of increasing the impulse buy rate of candy at a grocery store, we at best muster a vague sense of unease.  But such advancements are an assault on our ability to exercise our wills and and we should react accordingly.  The alternative is to imagine a future where a cloud of media suffocates a human face, forever.&lt;br&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-60818669341543731?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/60818669341543731/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=60818669341543731' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/60818669341543731'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/60818669341543731'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2010/04/farmville-and-face-of-transdehumanism.html' title='Farmville and the Face of Transdehumanism'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-5836731154018808801</id><published>2009-05-30T08:55:00.000-07:00</published><updated>2009-05-30T20:37:31.584-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='emacs lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='destructuring bind'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='clojure'/><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Clojure-style destructuring binding for Emacs Lisp</title><content type='html'>I like to write Lisp, but like a lot of people it took me a long time to get used to the verbosity of the language and the number of parenthesis.  In a lot of ways, complaining about parenthesis and the fundamentally nested style of lisp is praising with faint damnation, since it is exactly this property which encourages the programmer to write short, concise functions and the "grow the solution" rather than attack it top down&lt;sup&gt;1&lt;/sup&gt;.

Also, like a lot of people, I hate boiler plate code.  One area in which Clojure really improves on traditional lisps is its support for destructuring bind.  With this construct, the decomposition of data structures is represented concisely in the binding form of a let or lambda list, so that the body of the function can focus exclusively on logic, rather than decomposition.  Thus, a simple implementation of map might look like:
&lt;pre&gt;&lt;code&gt;
(define (map f lst)
  (if (empty? lst) '())
      (cons (f (car lst)) (map f (cdr lst)))))
&lt;/code&gt;&lt;/pre&gt;
While a Clojure implementation would look like 
&lt;pre&gt;&lt;code&gt;
(defn map [f [head &amp; tail :as lst]]
  (if (empty? lst) '()
      (cons (f head) (map f tail)))
&lt;/code&gt;&lt;/pre&gt;
Which is shorter and clearer and has fewer parenthesis.  The real power of destructuring bind becomes obvious when you realize you can use it with anonymous functions and the reduce/fold operation to decompose complex accumulators in a concise manner, as in this code:
&lt;pre&gt;&lt;code&gt;
(defn file-name-parser [next-character 
      [total-ac 
       element-ac]]
  (cond 
   (nil? (last element-ac)) ;; first call
   (do
  (list 
   total-ac ;; should be nil anyway
   (conj element-ac next-character)))

   (same-class? next-character (last element-ac))
   (do 
  (list
   total-ac
   (conj element-ac next-character)))

   (not-same-class? next-character (last element-ac))
   (do 
  (list
   (conj total-ac element-ac)
   [next-character]))))

(defn parse-file-name [name]
  (let 
   [[total rem] (foldl
         file-name-parser (list [] []) (seq name))]
 (map (comp parse-number-or-leave #(apply str %)) (conj total rem))))
&lt;/code&gt;&lt;/pre&gt;
I like this aspect of clojure a lot.  However, like most people, the lisp I write the most code in is emacs lisp.  So I wrote a destructuring bind and defn form for emacs lisp which mimics clojure's style.  With it you can write in emacs lisp:
&lt;code&gt;&lt;pre&gt;
(defn demo [a b [c d] &amp; rest] (list a b c d rest))
(demo 1 2 '( 3 4 ) 5 6 7) -&gt; (1 2 3 4 (5 6 7))
&lt;/pre&gt;&lt;/code&gt;
It supports destructuring on hash-tables also, but since emacs lisp does not have reader macros or any other way to directly represent them, you need to indicate a hash table with the special form &lt;code&gt;::&lt;/code&gt;.
&lt;code&gt;&lt;pre&gt;
(defn demo [a b [c [:: d :x]]] (list a b c d)))
(demo 1 2 (list 3 (tbl! :x 33))) -&gt; (1 2 3 33)
&lt;/pre&gt;&lt;/code&gt;
(tbl! is a short hand for creating a hash table, it is also included with the code).

The functions are lexically scoped but otherwise can be used as emacs functions, including using the (interactive) form.  The code also includes a clojure-style destructuring let form called &lt;code&gt;dlet&lt;/code&gt;.  ie:
&lt;code&gt;&lt;pre&gt;
(dlet [x 10 [a b] (list 1 2)] (+ a b x)) -&gt; 13
&lt;/pre&gt;&lt;/code&gt;
Both also support the &lt;code&gt;:as name&lt;/code&gt; feature of clojure's binding forms.
&lt;code&gt;&lt;pre&gt;
(dlet [ [a b :as lst] (list 1 2) ] (list a b lst)) -&gt; (1 2 (1 2))
&lt;/pre&gt;&lt;/code&gt;
It doesn't yet support default values (even of nil) for sequences or hash tables and it probably has a few bugs, but you call pull it from github with:
&lt;code&gt;&lt;pre&gt;
git://github.com/VincentToups/emacs-utils.git
&lt;/pre&gt;&lt;/code&gt;
Anyway, it is neat and hopefully useful and if anyone wants to see what a language extension in emacs lisp &lt;i&gt;might&lt;/i&gt; look like, or just get a feel for the power of Lisp, then the code might be interesting.

(Edit: I added a &lt;code&gt;fn&lt;/code&gt; form to the library as well as made a few bug-fixes.  Unfortunately because of limitations within Emacs Lisp, you cannot quite use macros that expand into lambdas exactly like you would use lambdas, but &lt;code&gt;fn&lt;/code&gt; should be ok everywhere that you would pass a function around.  The only place it doesn't work, I think, is in the call-position of an s-expression encountered by the reader.)

&lt;sup&gt;1&lt;/sup&gt; Also true of Forth and Factor.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-5836731154018808801?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/5836731154018808801/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=5836731154018808801' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5836731154018808801'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5836731154018808801'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2009/05/clojure-style-destructuring-binding-for.html' title='Clojure-style destructuring binding for Emacs Lisp'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-1731998486724156164</id><published>2009-05-19T07:40:00.001-07:00</published><updated>2009-05-19T07:43:21.273-07:00</updated><title type='text'>On "Buy Local"</title><content type='html'>(crosspost from the comments to &lt;a href="http://distributism.blogspot.com/2009/05/buying-american.html"&gt;Distributist Review&lt;/a&gt;.)

I think the issues around globalism are complex.

For instance, when you say "Buy local," what do you mean? Do you mean culturally local, geographically local, or "energetically" local (as in, it does not require much expenditure of energy to move the good from the provider to the consumer).

I am pretty sure a definition of local based on cultural similarity is exactly what people mean when they say Buy American is a chauvinistic movement - its subtext (albeit barely sub) is "Americans deserve to have jobs more than people in another society". The idea that one person deserves a living and another does not depending on whether they are of a particular culture is obviously amoral. I think most moralists agree that the culture one incidentally belongs to ought not to prevent you from working and living.

If this is the case, then saying it is geographic locality which matters (and cultural locality is just a coincidence due to the fact that people with similar culture tend to live near one another) then we are in the position of making an even more ridiculous moral statement: that people deserve to work based on whether they live near you or not. This is even more coincidental than the culture to which one belongs.

By local we must therefore mean energetically local - that is, that it does not require very much energy to transport the good from the producer to the consumer. This definition, however, must depend on the state of technology surrounding the economic transaction. If we invented teleporters powered by solar energy, for instance, then everyone would be local to you. Intellectual trade has already reached this point - no one talks of "supporting local bloggers" because the blog is an essentially free to move object.

One consequence of accepting this view is that if technological changes, such as the invention of fossil fuel powered vehicles and ships, occur, then the notion of energetically local will necessarily change. This is quite arguably the cause of the phenomenon of globalization in the last two centuries.

It seems to me that if someone in Bangalore makes something which is expensive or impossible to make here in North Carolina and we make something else that they want (even if that thing is just capital) then it makes more sense, both morally and practically, to buy from the people in Bangalore. They, after all, need to make a living just like anyone else, and their local circumstances might not allow for them to diversify their production sufficiently to support a good quality of life if they were restricted to local trade.

It seems like what you really object to is the people in North Carolina (for instance) "producing" nothing but capital. But we both agree that this is (in the long term) impossible. In any case, it is a separate problem if North Carolinians are not actually producing valuable commodities. One not really addressable by merely buying local (except that doing so artificially constrains us). Shouldn't we be more concerned about making sure our investments are sound in the long term and that our transactions are moral and forget about geography or culture altogether when making economic decisions?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-1731998486724156164?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/1731998486724156164/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=1731998486724156164' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1731998486724156164'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/1731998486724156164'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2009/05/on-buy-local.html' title='On &quot;Buy Local&quot;'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-8137879300200195168</id><published>2009-04-30T08:54:00.000-07:00</published><updated>2009-04-30T09:01:01.763-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='morse'/><category scheme='http://www.blogger.com/atom/ns#' term='factor'/><category scheme='http://www.blogger.com/atom/ns#' term='morse-code'/><category scheme='http://www.blogger.com/atom/ns#' term='code'/><category scheme='http://www.blogger.com/atom/ns#' term='programming praxis'/><category scheme='http://www.blogger.com/atom/ns#' term='morse code'/><title type='text'>Morse Code in Factor</title><content type='html'>&lt;style&gt;.NULL {
color: #000000;
}
.COMMENT1 {
color: #cc0000;
}
.COMMENT2 {
color: #ff8400;
}
.COMMENT3 {
color: #6600cc;
}
.COMMENT4 {
color: #cc6600;
}
.DIGIT {
color: #ff0000;
}
.FUNCTION {
color: #9966ff;
}
.INVALID {
background: #ffffcc;
color: #ff0066;
}
.KEYWORD1 {
color: #006699;
font-weight: bold;
}
.KEYWORD2 {
color: #009966;
font-weight: bold;
}
.KEYWORD3 {
color: #0099ff;
font-weight: bold;
}
.KEYWORD4 {
color: #66ccff;
font-weight: bold;
}
.LABEL {
color: #02b902;
}
.LITERAL1 {
color: #ff00cc;
}
.LITERAL2 {
color: #cc00cc;
}
.LITERAL3 {
color: #9900cc;
}
.LITERAL4 {
color: #6600cc;
}
.MARKUP {
color: #0000ff;
}
.OPERATOR {
color: #000000;
font-weight: bold;
}
&lt;/style&gt;
            
I am getting the hang of this.  This is a string&gt;morse code and morse code&gt;string converter as per &lt;a href="http://programmingpraxis.wordpress.com/2009/04/28/morse-code/"&gt;this&lt;/a&gt; programming praxis problem.  My implementation ignores extra whitespace in the morse code stream.

            &lt;pre&gt;&lt;span class="MARKUP"&gt;USING: &lt;/span&gt;&lt;span class="LITERAL2"&gt;ascii&lt;/span&gt;&lt;span class="LITERAL2"&gt; &lt;/span&gt;&lt;span class="LITERAL2"&gt;lists&lt;/span&gt;&lt;span class="LITERAL2"&gt; &lt;/span&gt;&lt;span class="LITERAL2"&gt;sequences&lt;/span&gt;&lt;span class="LITERAL2"&gt; &lt;/span&gt;&lt;span class="LITERAL2"&gt;regexp&lt;/span&gt;&lt;span class="LITERAL2"&gt; &lt;/span&gt;&lt;span class="MARKUP"&gt;;&lt;/span&gt;

&lt;span class="MARKUP"&gt;: ch&amp;gt;morse&lt;/span&gt; ch&amp;gt;upper &lt;span class="COMMENT3"&gt;( &lt;/span&gt;&lt;span class="COMMENT4"&gt;char&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;--&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT4"&gt;morse&lt;/span&gt;&lt;span class="COMMENT4"&gt;-&lt;/span&gt;&lt;span class="COMMENT4"&gt;str&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;)&lt;/span&gt;
         &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 0&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 1&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 2&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 3&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 4&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 5&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 6&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 7&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 8&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 9&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: A&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: B&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: C&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: D&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: E&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: F&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: G&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: H&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: I&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: J&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: K&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: L&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: M&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: N&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: O&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: P&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: Q&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: R&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: S&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: T&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: U&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: V&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: W&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: X&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;  
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: Y&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;  
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: Z&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; drop &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; case &lt;span class="MARKUP"&gt;;&lt;/span&gt;

&lt;span class="MARKUP"&gt;: morse&amp;gt;ch&lt;/span&gt; &lt;span class="COMMENT3"&gt;( &lt;/span&gt;&lt;span class="COMMENT4"&gt;morse&lt;/span&gt;&lt;span class="COMMENT4"&gt;-&lt;/span&gt;&lt;span class="COMMENT4"&gt;substring&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;--&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT4"&gt;char&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;)&lt;/span&gt;
         &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 0&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 1&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 2&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 3&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 4&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 5&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 6&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 7&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 8&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: 9&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: A&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: B&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: C&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: D&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: E&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: F&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: G&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: H&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: I&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: J&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: K&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: L&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: M&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: N&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: O&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: P&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: Q&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: R&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: S&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: T&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: U&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: V&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: W&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; 
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: X&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;  
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: Y&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;  
           &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;—&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;•&lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="LITERAL2"&gt;CHAR: Z&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt;
           &lt;span class="OPERATOR"&gt;[&lt;/span&gt; drop &lt;span class="DIGIT"&gt;32&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; case &lt;span class="MARKUP"&gt;;&lt;/span&gt;


&lt;span class="MARKUP"&gt;: string&amp;gt;morse&lt;/span&gt; &lt;span class="COMMENT3"&gt;( &lt;/span&gt;&lt;span class="COMMENT4"&gt;string&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;--&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT4"&gt;morse&lt;/span&gt;&lt;span class="COMMENT4"&gt;-&lt;/span&gt;&lt;span class="COMMENT4"&gt;string&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;)&lt;/span&gt;
         sequence&amp;gt;cons &lt;span class="OPERATOR"&gt;{&lt;/span&gt; &lt;span class="OPERATOR"&gt;}&lt;/span&gt; &lt;span class="OPERATOR"&gt;[&lt;/span&gt; ch&amp;gt;morse suffix &lt;span class="OPERATOR"&gt;]&lt;/span&gt; foldl
         &lt;span class="LITERAL1"&gt;"&lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt; &lt;/span&gt;&lt;span class="LITERAL1"&gt;"&lt;/span&gt; join &lt;span class="MARKUP"&gt;;&lt;/span&gt;

&lt;span class="MARKUP"&gt;: morse&amp;gt;string&lt;/span&gt; &lt;span class="COMMENT3"&gt;( &lt;/span&gt;&lt;span class="COMMENT4"&gt;morse&lt;/span&gt;&lt;span class="COMMENT4"&gt;-&lt;/span&gt;&lt;span class="COMMENT4"&gt;string&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;--&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT4"&gt;string&lt;/span&gt;&lt;span class="COMMENT4"&gt; &lt;/span&gt;&lt;span class="COMMENT3"&gt;)&lt;/span&gt;
         R/ &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &lt;span class="OPERATOR"&gt;]&lt;/span&gt;&lt;span class="OPERATOR"&gt;{&lt;/span&gt;2,&lt;span class="OPERATOR"&gt;}&lt;/span&gt;/ re-split
         &lt;span class="OPERATOR"&gt;[&lt;/span&gt; &amp;gt;string morse&amp;gt;ch &lt;span class="OPERATOR"&gt;]&lt;/span&gt; map &amp;gt;string &lt;span class="MARKUP"&gt;;&lt;/span&gt; &lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-8137879300200195168?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/8137879300200195168/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=8137879300200195168' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8137879300200195168'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/8137879300200195168'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2009/04/morse-code-in-factor.html' title='Morse Code in Factor'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-4470142493239757518</id><published>2009-04-29T17:49:00.000-07:00</published><updated>2009-04-29T19:01:16.413-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='tail calls'/><category scheme='http://www.blogger.com/atom/ns#' term='foldr'/><category scheme='http://www.blogger.com/atom/ns#' term='guido van rossum'/><category scheme='http://www.blogger.com/atom/ns#' term='scheme'/><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><category scheme='http://www.blogger.com/atom/ns#' term='foldl'/><title type='text'>The Python Tail Call Hullabu</title><content type='html'>Guido Van Rossum has done it again, causing a great blogging uproar over his decision to keep tail call elimination out of Python.  The argument boils down to Van Rossum feeling as though tail calls encourage a kind of non-obvious style of programming and make debugging more complicated anyway (you can read the whole thing &lt;a href="http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html"&gt;here&lt;/a&gt;.

Those few (none) of you who have followed this blog might guess, given my penchant for functional languages, that I am on the pro-tail-call-elimination group and, I suppose, I probably am&lt;sup&gt;1&lt;/sup&gt;.  However, my position is perhaps more nuanced, at least enough that I thought it worth explicating.

The problem might be cast into relief by pointing out we'd really like, as programmers of an abstract bent, to ignore the existence of the computer our code is running on entirely.  Even the Turing machine, arguably the least abstract conception of computing, has an infinite amount of tape.  The lambda calculus doesn't have any machine aspect at all - no memory, processors, none of that crap.  In a completely abstract world, the difference between a tail call and a call in a non-tail position is immaterial - you have an infinite amount of stack (or whatever it is) to accumulate, and all that really matters is whether the computation terminates (and not even that, if you are working on streams or some other meaningfully infinite data structure).

In real life, much to the chagrin of us abstract types, computers have a finite amount of call stack and all sorts of other limitations.  But the elimination of tail calls lets us pretend, in certain cases and for certain, carefully crafted algorithms, that the limitation of the stack does not exist.  There is a grace and elegance to this which is very attractive, which is why we like it.  But it also is clunky and weird.  Why, after all, should a tail call be different than a call in a non-tail position?  In Scheme, nothing distinguishes a tail call syntactically or semantically (at least not without bursting the bubble of abstraction around the language).  As far as the abstract nature of programming is concerned tail and non-tail calls are identical.  So even though tail calls let us code in a way which seems abstract, they are really coding to an optimization.  In other words, the most abstract of us are spending a lot of time thinking about a non-abstract question anyway.  The tail call is a sort of a farce.

It is this very reality which I think the non-abstract crowd claims is confusing, and when I really think about it, I can see the point.

I'd still like it in Python, but I guess I might just have moved on to other languages.



&lt;sup&gt;1&lt;/sup&gt; I'd rather have proper lambdas first.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-4470142493239757518?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/4470142493239757518/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=4470142493239757518' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/4470142493239757518'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/4470142493239757518'/><link rel='alternate' type='text/html' href='http://dorophone.blogspot.com/2009/04/python-tail-call-hullabu.html' title='The Python Tail Call Hullabu'/><author><name>J.V. Toups</name><uri>http://www.blogger.com/profile/06898786195752837412</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://2.bp.blogspot.com/_q-KqPstDuuE/SagpQuYpHtI/AAAAAAAAAEQ/9Us0yI07JYg/s1600-R/n2729953_40634428_1942.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8115687546775931822.post-5464220502058180990</id><published>2009-04-15T18:11:00.000-07:00</published><updated>2009-04-15T18:17:56.515-07:00</updated><title type='text'>Rail-Fence Cypher (Amateur Hour at the Factor Lounge)</title><content type='html'>Hi folks.  Decided to learn a stack based language - considered forth for a time but felt that I didn't often have a domain where it would be useful (I learn most of my fun languages, eventually, by writing all my little day to day shell scripts in them).  Stack based languages are both wildly different and surprisingly similar to applicative languages.  My code is still very amateurish (in particular there is a combinator I kept wanting but couldn't find - it would apply a quotation to n elements on the stack and leave the results in the same place), but I think I am getting the hang of it.  True to its name, factoring code is much easier when all the variable binding and associated cruft is implicit.  There is something to be said for that.

The problem is the rail-fence cypher, which you can read about &lt;a href="http://programmingpraxis.wordpress.com/2009/03/31/rail-fence-cipher/"&gt;here&lt;/a&gt;.  My solution is nothing special, so far as reasoning goes and I use a Matlab accented trick to undo the cypher.  I don't know if there is a smarter way.

&lt;code&gt;&lt;pre&gt;
USING: sequences quotations grouping lists arrays fry ;

: make&lt; ( n -- quotation )
         1 { dup &lt; } insert-nth &gt;quotation ;
: -&gt;n ( n -- seq )
         make&lt; 0 swap [ 1 + dup 1 - ] [ ] produce swap drop ;
: nth-or ( or i seq -- i-or )
         dup length pick swap &lt; [ nth swap drop ] [ drop drop ] if ;
: prefix-if-not-f ( seq it -- seq )
         dup [ prefix ] [ drop ] if ;
: nths-if ( seq-seq n -- nths )
         '[ f _ rot nth-or prefix-if-not-f ]
         swap sequence&gt;cons swap { } swap foldl reverse ;  
: invert-about ( n a -- o )
         dup dup '[ dup _ &lt;= [ ] [ _ - _ swap - ] if ] call ;
: inverter ( a -- quot )
         [ invert-about ] curry ;

: height-sequence ( str n -- str n hs )
         over length -&gt;n over 1 - 2 * [ mod ] curry map
         over 1 - inverter map pick zip ;
: height= ( pair n -- bool ) swap first = ;
: fing-heights= ( n seq -- seq ) swap [ height= ] curry filter ;
: construct-height-filter-quot ( seq -- quot )
         [ fing-heights= ] curry [ [ second ] map prefix ] compose ;
: railfence ( str n -- enc )
         height-sequence
         construct-height-filter-quot
         swap -&gt;n sequence&gt;cons swap { } swap
         foldl reverse concat &gt;string swap drop ;

: index-array ( enc n -- array )
         over length -&gt;n &gt;string swap railfence &gt;array swap zip ;
: compare-pairs ( o1 o2 -- r )
         first swap first swap &lt;=&gt; ;
: ~railfence ( enc n -- str )
         index-array [ compare-pairs ] sort [ second ] map &gt;string ;
&lt;/pre&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8115687546775931822-5464220502058180990?l=dorophone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dorophone.blogspot.com/feeds/5464220502058180990/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8115687546775931822&amp;postID=5464220502058180990' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687546775931822/posts/default/5464220502058180990'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8115687
