<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Arne Skjærholt</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/arne_skjaerholt/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/arne_skjaerholt//291</id>
    <updated>2010-07-19T18:37:44Z</updated>
    <subtitle>Perl, Prolog, Lisp and anything else I come up with.</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>The joys of backtracking</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/07/the-joys-of-backtracking-1.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.764</id>

    <published>2010-07-19T18:18:31Z</published>
    <updated>2010-07-19T18:37:44Z</updated>

    <summary>I&apos;m pleased to say that Parrotlog is now at a point where it can actually do stuff. It can call predicates and backtrack (more on that in a moment). Unification probably works as well. Now, backtracking is tricky to get...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="backtracking" label="backtracking" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="cut" label="cut" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrotlog" label="parrotlog" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>I'm pleased to say that Parrotlog is now at a point where it can actually do stuff. It can call predicates and backtrack (more on that in a moment). Unification probably works as well.</p>

<p>Now, backtracking is tricky to get right. Currently Parrotlog has a problem with cuts. A cut is supposed to remove all choice points encountered after the currently executing predicate was invoked. Parrotlog's cut prunes all choice points since the invocation of the last predicate that matched successfully. Close, but no cigar.</p>

<p>Graham's CL implementation (that Parrotlog owes quite a bit to) solves this by taking a reference to the choice point stack on invocation and just restoring that on cut. That works because Lisp lists are cons cells, but Parrot RPAs aren't, so I'll have to be a bit more clever. At the moment, I've two possible outs: 1) Copy the whole stack on invocation. Not very sophisticated, but it works. 2) Change the mark method to generate unique marks each time the stack is mark, so that cut can backtrack arbitrarily far into the stack, as long as you know the ID of the destination.</p>

<p>I think I'm gonna take a look at the materials I have on the WAM as well, see if I can't get some inspiration from how cuts are implemented there. But first, I have to decide if I want sushi or crêpes for dinner. Choices, choices...</p>]]>
        
    </content>
</entry>

<entry>
    <title>Decoding HMMs in Perl 6</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/06/decoding-hmms-in-perl-6.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.624</id>

    <published>2010-06-08T17:21:57Z</published>
    <updated>2010-06-08T17:47:47Z</updated>

    <summary>I&apos;ve wanted to write a reasonably useful Perl 6 module for a while, and I finally realised that the Viterbi algorithm would be a pretty simple place to start (hopefully it&apos;ll be useful as well). There&apos;s a module with the...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="perl6hmm" label="perl6 hmm" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>I've wanted to write a reasonably useful Perl 6 module for a while, and I finally realised that the Viterbi algorithm would be a pretty simple place to start (hopefully it'll be useful as well).</p>

<p>There's a module with the same name on <a href="http://search.cpan.org/~koen/Algorithm-Viterbi-0.01/lib/Algorithm/Viterbi.pm">CPAN</a>, but I've based the code on a Common Lisp version I wrote for school a while back. At the moment the module is pretty basic, and doesn't support assigning probabilities to unseen data for example. The module is available on <a href="http://github.com/arnsholt/Algorithm-Viterbi">GitHub.</a></p>

<p>A more advanced version will support computing the sum of the log-probabilities rather than the product, smoothing and unobserved data, and the option of mixing in a role to domain objects so that they can be passed directly to the decode method from the client code.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Parrotlog - Parsing Prolog</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/04/parrotlog---parsing-prolog.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.457</id>

    <published>2010-04-11T01:26:54Z</published>
    <updated>2010-04-11T01:27:20Z</updated>

    <summary>For the syntax and semantics of Prolog, Parrotlog is based on a draft of the ISO/IEC Prolog standard (seeing how the actual standard costs muchos dineros). Now, the good news are that the Prolog spec is actually an operator precedence...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="grammars" label="grammars" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="nqp" label="nqp" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrotlog" label="parrotlog" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>For the syntax and semantics of Prolog, Parrotlog is based on <a href="http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/prolog/doc/standard/0.html">a draft</a> of the ISO/IEC Prolog standard (seeing how the actual standard costs muchos dineros).</p>

<p>Now, the good news are that the Prolog spec is actually an operator precedence grammar, which happens to be how NQP does its expression parsing as well. The bad news are that the spec uses term for everything, while NQP makes a distinction between terms (atomic expressions) and expressions (expressions, with or without operators). This means that I have to figure out if I should use <code>term</code> or <code>EXPR</code> whenever the spec says term. Let's see how deep the rabbit hole is.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Parrotlog - Backtracking</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/04/parrotlog---backtracking.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.435</id>

    <published>2010-04-10T00:50:00Z</published>
    <updated>2010-04-10T00:50:36Z</updated>

    <summary>Backtracking is probably the defining feature of Prolog. Abstractly, the execution of a Prolog program can be seen as traversing a tree, looking for a path that fits certain criteria. Each node represents a choice between several options, and are...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="backtracking" label="backtracking" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="continuations" label="continuations" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrotlog" label="parrotlog" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>Backtracking is probably the defining feature of Prolog. Abstractly, the execution of a Prolog program can be seen as traversing a tree, looking for a path that fits certain criteria. Each node represents a choice between several options, and are usually referred to as choice points (for reasons that should be obvious). If at any time the path taken is found to be inconsistent with the constraints, execution goes back to the previous choice point and tries the next option. The search is depth-first, left-to-right.</p>

<p>Now, as I've mentioned before, in Parrotlog this is implemented using continuations, based on example code from Graham's <a href="http://www.paulgraham.com/onlisp.html">On Lisp</a> book (chapter 22). Simply put, continuations allow you to restore the execution of your program to a previous state. For the C programmers, this is simillar to <code>setjmp(3)</code> and <code>longjmp(3)</code>, but returning from the originating function doesn't invalidate the saved state. On Lisp chapter 20 has more about continuations, and so do the <a href="http://docs.parrot.org/parrot/latest/html/docs/book/pir/ch06_subroutines.pod.html#Continuations">Parrot docs</a>.</p>

<p>This, then, is the core of our backtracking, and with continuations it's actually pretty simple. Each time we encounter a choice point, we just store a continuation which will try a different value from the one we're going with right now. Failure is then just a matter of popping the top continuation of the stack (since we want to backtrack to the last choice point encountered, the LIFO semantics of a stack is what we want) and invoke it, returning execution to the choice point.We do this until we find a match, or we backtrack out of the search entirely.</p>

<p>Then we have cuts. A cut can be seen as pruning the search tree, or committing to some of the choices that have been made. We implement this just like Graham's Scheme code does: we mark the limit of the cut with <code>mark()</code>, which stores a subroutine reference to <code>fail()</code> on the stack, so that failure still consists of popping the top element off the stack and invoking it. Popping the mark will just result in a recursive call to <code>fail()</code>. <code>cut()</code> simply pops items off the stack until it finds the mark.</p>

<p>The main difference between my code and Graham's (apart from the fact that my code is PIR and his is Scheme) is that I explicitly thread the stack of continuations through the various functions as their first argument. This is based primarily on a gut feeling that it might come in handy at some later point. Someone once told me that global variables were a bad idea, and I've found that to be right most of the time. Primarily I think it might be useful for the metalogical predicates like <code>findall/3</code> where the code will have several nested backtracking searches. I think a single global stack might work, but I'm not sure, and I think an explicit stack would make that code clearer anyways.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Parrotlog - Unification (again)</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/04/parrotlog---unification-again.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.443</id>

    <published>2010-04-05T14:48:56Z</published>
    <updated>2010-04-08T22:45:57Z</updated>

    <summary>In the I went with the simple solution to the problem of unification. Variables point to other variables, and in the end a variable either points to a term, or nothing. What happens when you unify X with Y, Y...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="parrotlog" label="parrotlog" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="unification" label="unification" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>In the I went with the simple solution to the problem of unification. Variables point to other variables, and in the end a variable either points to a term, or nothing. What happens when you unify X with Y, Y with Z, and Z with X should probably not be considered just yet, and will probably have to be fixed at some point. But that should be reasonably simple. Finding cycles in a graph is after all a well-known problem.</p>

<p>This means that the core infrastructure I need should now be in place: unification, backtracking and cuts (a post on those last two is coming up). Now it's time to start looking into writing the grammar, and figuring out how to represent rules and the fact database.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Parrotlog - Unification</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/04/parrotlog---unification.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.440</id>

    <published>2010-04-04T23:10:00Z</published>
    <updated>2010-04-08T22:46:26Z</updated>

    <summary>I&apos;ve finally hit the first real roadblock in the development: unification. Instantiating a free variable and unifying that with a term is simple enough (and works). The problem is when you start unifying free variables with each other. For example...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="parrotlog" label="parrotlog" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="unification" label="unification" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>I've finally hit the first real roadblock in the development: unification. Instantiating a free variable and unifying that with a term is simple enough (and works). The problem is when you start unifying free variables with each other. For example you have three free variables: X, Y, Z. You unify X with Y, and then Y with Z. Finally, unify Z with a term "foo". The value of X (and Y and Z, for that matter) should now be "foo". What stumps me is how to implement this in a sane way. I've looked at the AI::Logic and "Perl and Prolog and [...]" versions, but I haven't managed to duplicate them in NQP in way that works...</p>]]>
        
    </content>
</entry>

<entry>
    <title>Parrotlog - Getting started</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/04/parrotlog---getting-started.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.426</id>

    <published>2010-04-03T13:00:00Z</published>
    <updated>2010-04-08T22:46:54Z</updated>

    <summary>The inspiration for this project is primarily due to my reading On Lisp by Paul Graham, which talks about implementing non-deterministic search (or backtracking, if you will) with continuations (chapter 2022). Since I know Parrot supports continuations natively, it was...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="parrot" label="parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrotlog" label="parrotlog" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>The inspiration for this project is primarily due to my reading <a href="http://www.paulgraham.com/onlisp.html"><em>On Lisp</em> by Paul Graham</a>, which talks about implementing non-deterministic search (or backtracking, if you will) with continuations (chapter <strike>20</strike>22). Since I know Parrot supports continuations natively, it was an obvious choice. Some googling also revealed a very interesting PerlMonks post, entitled <a href="http://www.perlmonks.org/?node_id=193649"><em>Perl and Prolog and Continuations... oh my!</em></a>, which it turns out is the inspiration for Ovid's <a href="http://github.com/Ovid/AI-Logic">AI::Logic</a> as well (which I discovered a few days ago).</p>

<p>Now, on to the implementation. Most of the documents I've found tell you to start your Parrot HLL project with a script called mk_language_shell.pl, but I found that it doesn't do quite what it say on the tin. Instead I used tools/dev/create_language.pl. This script creates a basic folder hierarchy similar to the one used by Rakudo. A quick tour of the files and folders:</p>

<ul>
  <li><code>build/</code> contains everything that has to do with the build process. Most interesting is <code>PARROT_REVISION</code> which specifies which Parrot is required, and <code>Makefile.in</code> which is where the build process can be extended.</li>
  <li><code>Configure.pl</code> does what it's called. Call it with <code>--gen-parrot</code> to build the required Parrot version as well.</li>
  <li><code>t/</code> and <code>src/</code> contain the usual bits</li>
</ul>

<p>EDIT: I lied. Backtracking is chapter 22 of Graham's book. Chapter 20 is continuations.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Parrotlog - Prolog on Parrot</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/arne_skjaerholt/2010/03/parrotlog---prolog-on-parrot.html" />
    <id>tag:blogs.perl.org,2010:/users/arne_skjaerholt//291.422</id>

    <published>2010-03-30T19:20:29Z</published>
    <updated>2010-04-08T22:47:10Z</updated>

    <summary>I&apos;ve always liked Prolog as a programming language. Something about the way the language works (like the built-in backtracking) appeals to my nature, much like Perl&apos;s proclivity for linguistic floridity. Thus, I&apos;ve decided to try my hand at writing a...</summary>
    <author>
        <name>Arne Skjærholt</name>
        
    </author>
    
    <category term="parrot" label="parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrotlog" label="parrotlog" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="prolog" label="prolog" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/arne_skjaerholt/">
        <![CDATA[<p>I've always liked Prolog as a programming language. Something about the way the language works (like the built-in backtracking) appeals to my nature, much like Perl's proclivity for linguistic floridity. Thus, I've decided to try my hand at writing a Prolog compiler for the Parrot VM.</p>

<p>To try to suppress my natural tendency to move from subject to subject (being a <a href="http://en.wikipedia.org/wiki/The_Hedgehog_and_the_Fox">fox and not a hedgehog</a>) I'm going to keep a development diary here. Also, I've found that the documentation on implementing HLLs with Parrot is either thin on the ground or a bit dated. So hopefully some of my scribblings turn out to be useful to someone else.</p>

<p>I've started work on the project, and keep a repository on GitHub. If you're interested, have a look at <a href="http://github.com/arnsholt/parrotlog">http://github.com/arnsholt/parrotlog</a>.</p>]]>
        
    </content>
</entry>

</feed>
