<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://www.foldr.org/~michaelw/log/theme/style/rss.css" type="text/css"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xml:lang="en">

	<channel>
		<title>Random Bits and Pieces :: programming</title>
		<link>http://www.foldr.org/~michaelw/log</link>
		<description>Michael Weber</description>
		<language>en</language>
		<lastBuildDate>Sun, 20 Jul 2008 11:23:00 GMT</lastBuildDate>
		<generator>blosxom ver.2.0</generator>


		<item>
			<title>Recreational Package Hacking</title>
			<link>http://www.foldr.org/~michaelw/log/programming/lisp/defpackage-form</link>
			<guid isPermaLink="true">http://www.foldr.org/~michaelw/log/programming/lisp/defpackage-form</guid>
			<category>http://www.foldr.org/~michaelw/log/programming/lisp/</category>
			<pubDate>Sun, 20 Jul 2008 11:23:00 GMT</pubDate>
			<content:encoded><![CDATA[
<img src="http://www.foldr.org/~michaelw/log/static/programming/lisp/lisp.png" alt="Lisp Logo (by Conrad Barsky)" />

<p>
  Did you ever want to recreate the source form of
  a <a href="http://www.lisp.org/HyperSpec/Body/syscla_package.html">package</a>,
  to see what state it is in currently?
  With <a href="http://www.foldr.org/~michaelw/lisp/defpackage-form.lisp">defpackage-form</a>,
  you can!
</p>

<p>
  Then again, I seem to fiddle too much with packages lately.  In
  particular, currently I am experimenting with a new work flow of
  Lisp <q>package management.</q>
</p>

<h3>Package Forms during Development</h3>

<p>When writing new Lisp code, I start with just the essentials of
  a new package in my current Emacs buffer:
</p>    
<pre class="common-lisp"><code class="common-lisp">
(<a href="http://www.lisp.org/HyperSpec/Body/mac_defpackage.html">defpackage</a> #:foo
  (:use #:cl))
</code></pre> 

<p>(This is basically what I get
  with <a href="http://www.foldr.org/~michaelw/lisp/redshank/"><kbd>C-x
      C-r P RET RET RET</kbd></a>.)</p>

<p>During development, I find it distracting to remind myself of
  keeping the package form up-to-date.  When I decide to
  import new symbols or shadow others I manipulate the package object
  directly, instead of updating the above <code>DEFPACKAGE</code> form
  and reevaluating it: <code>(import 'bar:baz)</code>, <code>(shadow
  'qux)</code>, <code>(use-package 'cl-fred)</code>, etc..  All this
  can be automated with a few editor key bindings, and the effects are
  visible in my working environment immediately.</p>

<p>At this point, I seldomly bother with export lists, because I might
  change names around later on, and also I am working
  mostly <em>in</em> the package.</p>

<p>At the end of the hacking session, I can just
  evaluate <code>(defpackage-form 'foo)</code> to get the current
  state of package <code>FOO</code> conveniently as
  a <code>DEFPACKAGE</code> form, which I can then use to replace the
  initial stub.  Eventually, if the code grows big enough to split it
  up, I move the package form to a separate
  file <tt>packages.lisp</tt>.</p>

<p>At least in the first iteration, the export list is likely still
  incomplete, so I can choose to include all symbols
  whose <a href="http://www.lisp.org/HyperSpec/Body/glo_h.html#home_package">home
  package</a> is the package I am working on, nicely sorted by name.
  Then I remove those symbols which are not meant for exporting.  Like
  that, it is easy to bulk export many symbols at once.  As a common
  convention, symbols starting with <code>%</code> are meant to be
  internal.  They can be omitted from the export list automatically.
</p>

<div style="float:right; width:25em; 
            border: thick solid #aaa; 
            margin:1ex 1em 0.5ex 2em; padding: 1ex 1em;
            font-family: sans-serif; font-size: 90%;">
  <h3><a href="http://www.lisp.org/HyperSpec/Body/sec_22-3.html"><code>FORMAT</code> Controls</a>,
    the Ultimate Line Noise</h3>
  <p>
    Package forms print neatly (and by that I mean closer to how I
    would layout them manually) with the following additions:
  </p>
  <pre class="common-lisp"><code class="common-lisp">
(defun pprint-defpackage (stream defpackage-form)
  (format stream "~:&lt;~W~^ ~3I~:_~W~^~1I~@{~:@_~:&lt;~W~^ ~:I~@_~@{~W~^ ~_~}~:&gt;~}~:&gt;"
          defpackage-form))

(set-pprint-dispatch '(cons (member defpackage))
                     'pprint-defpackage)
</code></pre>
  <p>Current versions of <a href="http:/www.sbcl.org/">SBCL</a> (newer
    than <a href="http://article.gmane.org/gmane.lisp.steel-bank.devel/11510">1.0.17.36</a>)
    come with this style included already.  I also toyed with a
    two-column layout, but it is harder to edit.
  </p>
</div>

<h3>Incremental Updates</h3>

<p>Incremental updates of package forms are a little trickier, though.
  The generated form could, for example, be compared to the original
  with <a href="http://www.foldr.org/~michaelw/log/programming/lisp/diff-sexp">diff-sexp</a>.  I have
  not automated that stage much yet.  If there are only few additions,
  it is probably easier to just add them manually.
</p>

<p>Otherwise, I could imagine
  an <a href="http://www.gnu.org/software/emacs/">Emacs</a>
  <a href="http://www.gnu.org/software/emacs/elisp/html_node/Saving-Buffers.html#Saving-Buffers"><code>before-save-hook</code></a>
  which checks whether the export list of a package matches
  the <code>DEFPACKAGE</code> form in <tt>packages.lisp</tt> (or
  wherever it is stored&mdash;this could be figured out with source
  locations), and some more automation and integration with Emacs.
</p>

<p>Very unfortunately, some loss of information occurs on the round
  trip from package form to object and back:</p>
<ul>
  <li>Reader conditionals inside <code>DEFPACKAGE</code> forms cannot
    be recreated easily.  Neither can comments, for what it's
    worth.</li>
  <li>There appears to be no way to figure out which package a symbol
    was actually imported from.  For example, if we
    export <code>CL:NIL</code> from package <code>FOO</code> and then
    import <code>FOO:NIL</code> into some other
    package <code>BAR</code>, it will appear the same as
    if <code>CL:NIL</code> was imported directly
    into <code>BAR</code>.</li>
  <li>The default <code>:USE</code> list (i.e., when none is
    specified) is implementation dependent, which naturally can cause
    some implementation dependent packages to show up in this
    list.</li>
</ul>
]]></content:encoded>
			<comments>http://www.foldr.org/~michaelw/log/programming/lisp/defpackage-form#writeback</comments>
		</item>

		<item>
			<title>How Lisp Systems Look Different</title>
			<link>http://www.foldr.org/~michaelw/log/programming/lisp/lisp-systems-look-different</link>
			<guid isPermaLink="true">http://www.foldr.org/~michaelw/log/programming/lisp/lisp-systems-look-different</guid>
			<category>http://www.foldr.org/~michaelw/log/programming/lisp/</category>
			<pubDate>Thu, 17 Jul 2008 14:36:00 GMT</pubDate>
			<content:encoded><![CDATA[
<img src="http://www.foldr.org/~michaelw/log/static/programming/lisp/lisp.png" alt="Lisp Logo (by Conrad Barsky)" />

<p>
  Dozsa et al.,
  "<a href="http://www.iam.unibe.ch/~scg/Archive/Papers/Dozs08aLispLooksDifferent.pdf">How
    Lisp Systems Look Different</a>"
</p>

<blockquote>
  In this paper we propose a suite of new visualizations that reveal
  the special traits of the Lisp language and thus help in
  understanding complex Lisp systems. To validate our approach we
  apply them on several large Lisp case studies, and summarize our
  experience in terms of a series of recurring visual patterns that we
  have detected.
</blockquote>

<p>One of the case studies is <a href="http://www.sbcl.org/">SBCL</a>.</p>
]]></content:encoded>
			<comments>http://www.foldr.org/~michaelw/log/programming/lisp/lisp-systems-look-different#writeback</comments>
		</item>

		<item>
			<title>Applied Lisp</title>
			<link>http://www.foldr.org/~michaelw/log/programming/lisp/applied-lisp</link>
			<guid isPermaLink="true">http://www.foldr.org/~michaelw/log/programming/lisp/applied-lisp</guid>
			<category>http://www.foldr.org/~michaelw/log/programming/lisp/</category>
			<pubDate>Thu, 17 Jul 2008 09:05:00 GMT</pubDate>
			<content:encoded><![CDATA[
<img src="http://www.foldr.org/~michaelw/log/static/programming/lisp/lisp.png" alt="Lisp Logo (by Conrad Barsky)" />

<p>
  It amused me for while that I found myself writing something similar
  to the following piece of code:
</p>

<pre class="common-lisp"><code class="common-lisp">
<span class="paren">(</span><span class="keyword">defun</span> <span class="function-name">%apply</span> <span class="paren">(</span>function arg <span class="type">&amp;rest</span> args<span class="paren">)</span>
  <span class="paren">(</span>apply #'apply function arg args<span class="paren">))</span>

<span class="paren">(</span><span class="keyword">defun</span> <span class="function-name">%funcall</span> <span class="paren">(</span>function <span class="type">&amp;rest</span> args<span class="paren">)</span>
  <span class="paren">(</span>%apply function args<span class="paren">))</span>
</code></pre>

<p>
  I will leave it to you, dear reader, to figure out what the
  double <code>APPLY</code> is good for.  Surprisingly, there is even
  some reason behind this madness, namely the ability to mangle
  the <code>function</code> argument before calling.
</p>
]]></content:encoded>
			<comments>http://www.foldr.org/~michaelw/log/programming/lisp/applied-lisp#writeback</comments>
		</item>

		<item>
			<title>Erlang meets Lisp (again)</title>
			<link>http://www.foldr.org/~michaelw/log/programming/lisp/erlang-common-lisp</link>
			<guid isPermaLink="true">http://www.foldr.org/~michaelw/log/programming/lisp/erlang-common-lisp</guid>
			<category>http://www.foldr.org/~michaelw/log/programming/lisp/</category>
			<pubDate>Sun, 06 Jul 2008 10:29:00 GMT</pubDate>
			<content:encoded><![CDATA[
<img src="http://www.foldr.org/~michaelw/log/static/programming/lisp/lisp.png" alt="Lisp Logo (by Conrad Barsky)" />

<p>
  There seems to be an interesting attraction
  between <a href="http://www.erlang.org/">Erlang</a> and Lisp and
  several times it has been tried to marry them, in different ways.
  <a href="http://bc.tech.coop/">Bill Clementson</a> wrote about it
  already in his article
  <q><a href="http://bc.tech.coop/blog/060111.html">Concurrent/Parallel
      Programming - The Next Generation</a></q>.  Here is an updated list:
</p>

<ul>
  <li><a href="http://www.iro.umontreal.ca/~etos/">ETOS</a>, an Erlang
    to (Gambit) Scheme compiler, dating from 1997, was probably the
    first attempt to combine Erlang and Lisp.  One of the things that
    came out of ETOS is Marc Feeley's
    paper <q><a href="http://www.erlang.se/workshop/feeley.ps">A Case for
        the Unified Heap Approach to Erlang Memory Management</a></q>.
  </li>
  <li><a href="http://www.dirkgerrits.com/programming/erlisp/">Erlisp</a>
    was another attempt, this time in Common Lisp.  It's a (partial)
    reimplementation of the Erlang concurrency model.  I don't think
    anybody is working actively on it at the moment.</li>
  <li>
    <a href="http://common-lisp.net/project/cl-muproc/">CL-MUPROC</a>
    is a Common Lisp library which strives to offer some of the
    multiprocessing abstractions found in Erlang, very much in the
    same way as Erlisp.  It implements some of Erlang's
    fault-tolerance mechanisms, but not yet distributed
    operations.</li>
  <li><a href="http://code.google.com/p/distel/">Distel</a>, a
    distributed <a href="http://www.gnu.org/software/emacs/manual/elisp.html">Emacs
    Lisp</a> with Erlang-style processes and message passing, and the
    Erlang distribution protocol, originally developed
    by <a href="http://lukego.livejournal.com/">Luke Gorrie</a>
    of <a href="http://lambda-the-ultimate.org/">LtU</a>,
    <a href="http://common-lisp.net/project/slime/">SLIME</a>, 
    <a href="http://lukego.livejournal.com/tag/olpc">OLPC</a>,
    and other fame.  Some time ago, Bill Clementson wrote
    a <a href="http://bc.tech.coop/blog/070528.html">nice article
    about Distel</a>.</li>
  <li><a href="http://code.google.com/p/termite/">Termite</a>, another
    Erlang-like distributed programming system, this time written
    in <a href="http://www.iro.umontreal.ca/~gambit">Gambit-C</a>
    Scheme.</li>
  <li><a href="http://groups.google.com/group/erlang-questions/msg/4a9127e701f5e2db">Lisp
      Flavoured Erlang</a> (LFE) is a Lisp syntax front-end to the
      Erlang compiler by Robert Virding (one of the inventors of
      Erlang).  This is an interesting one, because it comes out of
      the Erlang community for a change, and instead of porting
      Erlang ideas to Lisp, it goes the other route: porting Lisp
      ideas to Erlang.</li>
  <li><a href="http://common-lisp.net/project/erlang-in-lisp/">Erlang-in-Lisp</a>,
    a <strike>Google</strike> <a href="http://www.lispnyc.org/soc2008.clp">LispNYC
    Summer of Code 2008</a> project, again very much in the spirit of
    Erlisp, it seems.  The project is in its early stages, let's see
    how far this goes.</li>
  <li><a href="http://berlinbrowndev.blogspot.com/2008/07/code-snippet-erlang-server-common-lisp.html">Erlang
      Server, Common Lisp Client</a> by Berlin Brown appears to be the
    latest attempt of integrating Erlang and Lisp.  They are connected
      via sockets and exchange messages in a custom protocol.  This
      has the advantage that either side can easily be replaced by a
      different implementation.</li>
  <li><a href="http://list.cs.brown.edu/pipermail/plt-scheme/2008-July/025776.html">erlang-scheme</a>
    is a port of Distel from ELisp to Scheme.
  </li>
</ul>

<p>And this list does not even include projects like (again
  defunct?) <a href="http://community.schemewiki.org/kali-scheme/">Kali
  Scheme</a>, which are clearly related.
</p>

<p>
  Personally, I think that the reimplementation approach will have a
  tough stance against integration approaches like Distel.  They lock
  out either one or the other of the two language eco-systems:
  libraries, development tools, etc., and recreating this is a lot of
  work (but don't let that stop you!)  With Distel, I can choose to
  program parts of the application in ELisp or Erlang, whatever is a
  better fit.
</p>

<p>
  Distel implements Erlang's on-the-wire protocol, which is nice
  because there is no need to mess around with a <em>Foreign Function
  Interface</em>.  Alternatively, one could bind to the Erlang C
  libraries.  As far as rapid prototyping is concerned, this should be
  the fastest and most straight-forward approach.  I wonder why
  everybody is doing it the hard way (reimplementation)?
</p>
<h3><a id="erlang-common-lisp-1" class="updatetitle">UPDATE 2008-07-06: Forgot ETOS
</a><br /></h3>
<div><p>A helpful reader
  on <a href="http://www.reddit.com/r/programming/info/6qgwl/comments/c04lmum">reddit</a>
  rightly pointed out that I forgot to
  include <a href="http://www.iro.umontreal.ca/~etos/">ETOS</a>, now
  rectified.
</p>
</div>

<h3><a id="erlang-common-lisp-2" class="updatetitle">UPDATE 2008-07-06: Erlang Interoperability
</a><br /></h3>
<div><p>
  Bill Clementson wrote a nice summary of
  the <a href="http://bc.tech.coop/blog/070719.html">Erlang
  Interoperability</a> options.  Thanks again, Bill!
</p>
</div>

<h3><a id="erlang-common-lisp-3" class="updatetitle">UPDATE 2008-07-10: PLT Scheme meets Erlang
</a><br /></h3>
<div><p>
<a href="http://www.wisdomandwonder.com/link/257/erlang-scheme-interop">Grant
  Rettke</a> pointed me to another Lisp-Erlang
  marriage: <a href="http://list.cs.brown.edu/pipermail/plt-scheme/2008-July/025776.html">erlang-scheme</a>
</p>
</div>

]]></content:encoded>
			<comments>http://www.foldr.org/~michaelw/log/programming/lisp/erlang-common-lisp#writeback</comments>
		</item>

		<item>
			<title>Munging Data with CLAWK</title>
			<link>http://www.foldr.org/~michaelw/log/programming/lisp/clawk</link>
			<guid isPermaLink="true">http://www.foldr.org/~michaelw/log/programming/lisp/clawk</guid>
			<category>http://www.foldr.org/~michaelw/log/programming/lisp/</category>
			<pubDate>Sun, 27 Jan 2008 14:15:00 GMT</pubDate>
			<content:encoded><![CDATA[
<img src="http://www.foldr.org/~michaelw/log/static/programming/lisp/lisp.png" alt="Lisp Logo (by Conrad Barsky)" />

<p>Recently, I made good use
  of <a href="http://www.geocities.com/mparker762/">Michael
  Parker</a>'s <a
  href="http://www.geocities.com/mparker762/clawk">CLAWK</a>, a
  Lisp-embedded variant of <a
  href="http://en.wikipedia.org/wiki/AWK_(programming_language)">AWK</a>.
</p>

<p>So far, I had used the real AWK, along with perl and other parts of
  the Unix toolbox to analyze data from experiments, and munge it into
  HTML and LaTeX tables.  However, this time I expected the
  experiments to be carried out in
  a <q>run&ndash;tweak&ndash;rerun</q> fashion with several
  iterations, and I did not want to reparse several hundred megabytes
  each time, just for a change in table layout, or for adding another
  analysis.  (Also, I had not much time for lispy things recently, so
  this was a good way to sneak some Lisp back into my current C++
  hell...)
</p>

<p>
  Enter CLAWK.  The following function parses my benchmark data into
  Lisp objects:
</p>

<pre class="common-lisp"><code class="common-lisp"
><span class="paren">(</span><span class="keyword">defvar</span> <span class="variable-name">*foo-table*</span> <span class="paren">(</span>make-hash-table <span class="builtin">:test</span> 'equal<span class="paren">))</span>

<span class="paren">(</span>defawk parse-foo-benchmark <span class="paren">(</span><span class="type">&amp;aux</span> model<span class="paren">)</span>
  <span class="paren">(</span>#/^memtime/
   <span class="paren">(</span><span class="keyword">when</span> model
     <span class="paren">(</span>emit-model model *foo-table*<span class="paren">))</span>
   <span class="paren">(</span><span class="keyword">let</span> <span class="paren">((</span>path <span class="paren">(</span>parse-namestring $6<span class="paren">)))</span>
     <span class="paren">(</span>setf model <span class="paren">(</span>make-instance 'foo-model-record <span class="builtin">:filename</span> path<span class="paren">))))</span>
  <span class="paren">(</span>#/^Instantiator: Explored/
   <span class="paren">(</span>setf <span class="paren">(</span>get-states model<span class="paren">)</span> $#3
         <span class="paren">(</span>get-transitions model<span class="paren">)</span> $#6
         <span class="paren">(</span>get-bfs-levels model<span class="paren">)</span> $#9
         <span class="paren">(</span>completed? model<span class="paren">)</span> t<span class="paren">))</span>
  <span class="paren">((</span>string= $6 <span class="string">"elapsed"</span><span class="paren">)</span>
   <span class="paren">(</span>setf <span class="paren">(</span>get-generation-time model<span class="paren">)</span> $#5<span class="paren">))</span>
  <span class="paren">((</span>string= $13 <span class="string">"RSS"</span><span class="paren">)</span>
   <span class="paren">(</span>setf <span class="paren">(</span>get-generation-memory model<span class="paren">)</span> <span class="paren">(</span>parse-integer $15 <span class="builtin">:junk-allowed</span> t<span class="paren">)))</span>
  <span class="paren">(</span>END
   <span class="paren">(</span><span class="keyword">when</span> model
     <span class="paren">(</span>emit-model model *foo-table*<span class="paren">))</span>
   *foo-table*<span class="paren">))</span>
</code></pre>

<p>Parsing the bulk of my data with the above function takes about
  80&nbsp;seconds, probably slower than it would be with AWK.
  However, once parsed I have all the data available at my fingertips
  inside the Lisp image.  I can prod it with
  the <a href="http://common-lisp.net/project/slime/">SLIME</a>
  inspector, identify outliers, and play around with it in the REPL
  until I am satisfied.  In addition, I can selectively rerun parts of
  the experiments, and parse just the newly produced output to update
  the in-memory representation, again in no time flat.  Beats the Unix
  <q>everything is a byte stream</q> way every day of the week.  
</p>
<p>Rendering the results as HTML is a breeze
  with <a href="http://weitz.de/cl-who/">CL-WHO</a>.  The same holds
  for reordering columns, marking interesting entries
  programmatically, cross-referencing with other entries (or earlier
  versions of the data) and refining the analyses as much as I wish,
  with instant feedback.
</p>

<p style="margin-top:4ex;">
  Unfortunately, CLAWK has acquired some bitrot since its release in
  2002&nbsp;(?).  I needed to tweak it slightly to make it compile
  (only tested with <a href="http://www.sbcl.org/">SBCL</a>).  When I
  tried to contact Michael Parker, his email bounced, so I decided to
  put up a patched version locally until the changes are folded back
  into his distribution.  In
  addition, <a href="http://www.foldr.org/~michaelw/lisp/clawk/#clawk">CLAWK is now
  ASDF-installable</a> (along with its
  dependency, <a href="http://www.foldr.org/~michaelw/lisp/clawk/#regex">REGEX</a>),
  courtesy of
  <a href="http://www.foldr.org/~michaelw/lisp/redshank/">Redshank</a>'s ASDF Defsystem
  skeleton.
</p>

<p>If time permits, I will fix up the code some more to get rid of the
  warnings, and perhaps
  allow <a href="http://weitz.de/cl-ppcre/">CL-PPCRE</a> as
  alternative regular expression engine.  However, patches from the
  open-source fairies are very welcome, too.
</p>
]]></content:encoded>
			<comments>http://www.foldr.org/~michaelw/log/programming/lisp/clawk#writeback</comments>
		</item>

	</channel>
</rss>
