<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>A Lexical Mistake</title>
	<atom:link href="http://krysole.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://alexicalmistake.com</link>
	<description>Languages; Past, Present and Future</description>
	<pubDate>Sat, 01 Nov 2008 15:06:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>The Prototype Pattern and Extensible Compilers</title>
		<link>http://alexicalmistake.com/2008/11/the-prototype-pattern-and-extensible-compilers/</link>
		<comments>http://alexicalmistake.com/2008/11/the-prototype-pattern-and-extensible-compilers/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 15:06:22 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[Mention]]></category>

		<category><![CDATA[AST]]></category>

		<category><![CDATA[compiler]]></category>

		<category><![CDATA[language]]></category>

		<category><![CDATA[parser]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[prototype]]></category>

		<category><![CDATA[self]]></category>

		<guid isPermaLink="false">http://alexicalmistake.com/?p=75</guid>
		<description><![CDATA[During some of the early design work on the Mention programming language, I recognized that a fixed parser would be unable to deal with a great number of interesting language problems. Things like embedded SQL and other DSL language, or perhaps even a unit notation.
All of these things could be added into a syntax, I [...]]]></description>
			<content:encoded><![CDATA[<p>During some of the early design work on the Mention programming language, I recognized that a fixed parser would be unable to deal with a great number of interesting language problems. Things like embedded SQL and other DSL language, or perhaps even a unit notation.</p>
<p>All of these things could be added into a syntax, I believe, with a carefully constructed extensible parser. Each extension would become a libraries (modules) adding or replacing function points in the existing parser later reusing some of them as part of the extended syntax. (The actual parser would be based on Parsec and other similar combinator parsers. OMeta is also related to this parser architecture.)</p>
<p>The problem with adding new syntaxes to a language isn&#8217;t just the parser implementation however. Most of the really tricky problems actually come from the rest of the tool chain. Things like the compiler, debugger, profilers, editor, and a bunch of other potential tools. It had become clear to me that I wasn&#8217;t just working on an extensible parser, but rather an extensible language, and that meant an extensible compiler architecture.</p>
<p>Basically I thinking about a completely extensible intermediate language, which would power a set of semi-extensible libraries for implementing the bulk of the tool-chain. These libraries will take the leg-work out of processing the source code, and will allow the tools to manipulate or interact with the code in its intermediate form (this includes editors and analytical tools). The only problem I saw was that I had absolutely no idea how to construct such an intermediate language, little own how to write tools and libraries to interact with it.</p>
<p>Anyway, recently I run into the latest (at the time) Steve Yegge post, with a somewhat inspiring discussion about the Prototype Pattern (<a href="http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html">The Universal Design Pattern</a>). After musing about pattern (again) for a couple of days, it finally hit me, I had the architecture all wrong. This isn&#8217;t just some extensible compiler or parser&#8230;this was the Self programming language&#8230;redesigned, implemented and tailored for the task of compiler writing. The AST is simply a specialized variation of the Prototype pattern, or more succinctly, a compiler is just a constrained dialect of Self, designed for compiler tasks.</p>
<p>So this my proposal is this, use the prototype pattern for Mentions AST and build a framework of libraries and tools designed to aid in the maintenance and development of an AST and the compilers, parsers, debugger, <em>et cetera</em>, which depend on it.</p>
<p>There are already a few pitfalls that I can imagine coming out of this system. For one, a good namespace or typing system will probably be required to prevent name conflicts. That is, each extension or collection of extensions would add only private collections of attributes, while a smaller group of shared public attribute interfaces would be used to facilitate cooperation between extensions. And then there&#8217;s still the issue of expected behavior, which may be complicated by the potential for different combinations of compiler extensions to inadvertently interact in unexpected ways.</p>
<p>In any case, I believe that this will be one of the key features of the core Mention programming language and it may even make some of the more complicated features like type systems easier to implement and experiment with later on. At the very least, you can expect to see some follow up material on this when I start to get further into the implementation of the Mention.</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/11/the-prototype-pattern-and-extensible-compilers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Read Transparency</title>
		<link>http://alexicalmistake.com/2008/10/read-transparency/</link>
		<comments>http://alexicalmistake.com/2008/10/read-transparency/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 00:43:13 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[Mention]]></category>

		<category><![CDATA[functional]]></category>

		<category><![CDATA[Haskell]]></category>

		<category><![CDATA[language]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://alexicalmistake.com/?p=59</guid>
		<description><![CDATA[Over the past few months I&#8217;ve been putting together a collection of requirements, of sorts, for a new class of dynamic functional programming languages, a language that could provide powerful dynamic features on par with Smalltalk or Self (and JavaScript etc.), without losing some important features like laziness or strong static typing.
One of the requirements [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past few months I&#8217;ve been putting together a collection of requirements, of sorts, for a new class of dynamic functional programming languages, a language that could provide powerful dynamic features on par with Smalltalk or Self (and JavaScript etc.), without losing some important features like laziness or strong static typing.</p>
<p>One of the requirements I keep stumbling across is the need for referentially transparent functions that are capable of interacting directly with mutable data structures. Yeah, the kind that, by definition, are not referentially transparent.</p>
<p>At first I considered using compiler attributes to mark a piece of code that interacts with side effects vs. code that doesn&#8217;t. Something like the IO/ST monads from Haskell but general enough any function can interact with mutable variables, not just ones returning a monadic value. Unfortunately any code that interacts with mutable variables may behave radically differently when not constrained by the full monadic infrastructure or some equivalent like strictness (Scheme and ML).</p>
<p>I believe the only useful solution is to define a new property of the code&#8211;<em>read transparency</em>&#8211;which allows us to recognize code that is referentially transparent on immutable data while also referentially transparent towards unchanging mutable variables.</p>
<div style="margin-left: 5%; margin-right: 5%">A function is <em>read transparent</em> if it does not cause new side effects (either by IO, mutable state or otherwise), and the function produces equivalent result between any two calls provided the state of all arguments is equivalent (or the arguments are both immutable and the same values).</div>
<p>The above definition depends on the equivalence property of values, which I define along the lines of &#8220;any two data structures are equivalent if they are functionally the same value, or they only contain mutable variables containing equivalent state.&#8221; Given a good equivalence for all primitives this allows us to easily check for all functions which could safely be called in our new <em>semi-functional</em> programs.</p>
<p>The original motivating example for this concept was to allow functions like <code>show</code> in Haskell to work with any arbitrary data structure, mutable or not. This would greatly simplify programs like interpreters, which could use many of the same functions as pure code, without having to use somewhat cumbersome combinators like the lifting functions and unboxing functions (<code>readIORef</code>, etc.).</p>
<p>An important thing to remember about this property is that, while many previously referentially transparent functions would now only be read transparent, when used only with pure functional data, they are in fact promoted automatically to fully referentially transparent functions. That is, they remain effectively referentially transparent if you don&#8217;t care about mutable state, or to put it another way&#8211;the functions become more flexible, not less so. Only when the implementation of a function relies on language features that prevent mutable data from being used would the more restrictive referential transparency be required (like lazily evaluated results, incorporating the read operation).</p>
<p>We don&#8217;t actually have to stop there either. Just because a piece of code isn&#8217;t read transparent, that doesn&#8217;t mean that it suffers from all variations of side-effects. There are still yet more shades of grey which we can use to limit the damage caused by bugs and side-effects in our software. A property like write transparency for code that reads and writes to mutable data structures but performs no IO could be equally useful, just as the ST monad in Haskell, which cannot perform IO is useful for working with mutable state.</p>
<p>Unfortunately while our little definition is a rather interesting take on the mutable state issue, there are some rather serious caveats we still have to consider. The biggest problem we face here is that, while a read transparent function can read from mutable variables without any reprimands, it cannot use the unread mutable variable in the result, unless it uses it, still boxed. That is, if we returned a result lazily, the result might be different depending on when the result was used, definitely not something we want, and the primary reason that mutable state in Haskell is limited to the ST monad <em>et cetera</em>.</p>
<p>More concretely, we can return new mutable variables (since the result would always be equivalent), use the results from reading a mutable variable so long as we read it strictly before the function returns, and finally we can return the mutable variable verbatim as part of the result (something Haskell can do also). If any language implemented mutable variables unboxed however, they would not be usable in last context however, and you would <em>always</em> have to read from them strictly, leading to somewhat less flexibility in the language.</p>
<p>One last thing I&#8217;d like to add. The above definition is strikingly similar to something that might be rigorously defined and explored, but for some reason I haven&#8217;t done so in this post. Well, for all intents and purposes, I wouldn&#8217;t know where to start, at least not yet anyway. If and when I apply this property in an actual language implementation however, you can be sure I&#8217;ll have a rigorous writeup of it and it will likely be part of the languages spec too.</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/10/read-transparency/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Haskell and Game Development</title>
		<link>http://alexicalmistake.com/2008/10/haskell-and-game-development/</link>
		<comments>http://alexicalmistake.com/2008/10/haskell-and-game-development/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 06:29:15 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[Opinion]]></category>

		<category><![CDATA[Gamedev]]></category>

		<category><![CDATA[Haskell]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://alexicalmistake.com/?p=50</guid>
		<description><![CDATA[Like many gamedev enthusiasts, I&#8217;ve only finished a handful of computer games and, perhaps not surprisingly, most of those were never released even on my homepage either. Usually the point of game development at an enthusiast or hobbyist level is not to create gaming masterpieces anyway, but rather to simply learn about the process and [...]]]></description>
			<content:encoded><![CDATA[<p>Like many gamedev enthusiasts, I&#8217;ve only finished a handful of computer games and, perhaps not surprisingly, most of those were never released even on my homepage either. Usually the point of game development at an enthusiast or hobbyist level is not to create gaming masterpieces anyway, but rather to simply learn about the process and have fun working the code (or graphics, design, et cetera), and maybe even develop a few reasonably simple games.</p>
<p>Over the years I&#8217;ve spend quite a bit of time learning the gamedev concepts and recently I was able to apply some of them in a project, intended to test Haskell&#8217;s gamedev capabilities for hobbyist or small team game development (<a href="http://alexicalmistake.com/2008/06/language-design-and-small-team-game-development/">related post</a>). Specifically I was interested to see how the difference between Haskell and Object-Oriented languages changed the design and implementation of the game code and engine, hopefully lending itself to the eventual development of languages tailored towards game development.</p>
<p>I set out to develop one of my favorite arcade remakes&#8211;yet another variation of Geometry Wars. The premise would be to keep the game as simple as possible, only the basic gameplay would be present with a couple of levels of progression. Enough to test all of the simple elements of gameplay ready for a more substantial project. As you may have guessed from the absence of the obligatory screenshot it didn&#8217;t work out as well as I expected.</p>
<p>Luckily I did pick up a couple of things while working on the game, and better yet I may even be able to rewrite the code and find that screenshot yet. It all comes down to a couple of quirks in Haskell&#8217;s syntax which didn&#8217;t blend well with my usual game architecture. Obviously the architecture would need to be modified to suite the idiomatic style of Haskell, but I was reasonably certain (and still are) that the architecture is the simplest implementation for both Object-Oriented languages as well as functional languages, based almost solely on the requirements of the high level game data structures.</p>
<p>The Haskell code for these structures was essentially based directly on records. The records originally contained only functional values, but I quickly realized how crazy that was, which lead me to the current design which uses records full of IORefs, at least for the mutable slots. (If its not immediately obvious why the IORefs are needed, it might help to know that game objects quite often need to reference each other simply because the operations are related between the two&#8230;bullets from the player ship for example.)</p>
<p>Finally, after working with this approach for a while it eventually sunk in that I could probably write the same code in Python in not only half the time, but also quicker for the same number of overall bugs. Not because the bugs are easier to catch in Python&#8230;they really, really aren&#8217;t. But because Python allows me to test many more variations of the game. At least more variations than Haskell seems to, all of which makes me wonder if I haven&#8217;t quite tapped into the idiomatic code still.</p>
<p>So I&#8217;m not quite out of ideas yet. I believe I can tweak my accessors to reduce the namespace damage caused by them (at the expense of some verbosity, however&#8230;why the hell hasn&#8217;t this been fixed yet anyway). And there&#8217;s even some clever usage of typeclasses which could simplify the use of common algorithms between game objects. If all goes to plan I&#8217;ll have some code to post next time&#8230;</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/10/haskell-and-game-development/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Yet More Work On The Blog</title>
		<link>http://alexicalmistake.com/2008/10/yet-more-work-on-the-blog/</link>
		<comments>http://alexicalmistake.com/2008/10/yet-more-work-on-the-blog/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 02:30:04 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://alexicalmistake.com/?p=39</guid>
		<description><![CDATA[Theres been quite a lot of work going into the blog under the surface recently. The most important being an update to the current version of&#8230;well everything. The most obvious ramification of this process have been the change in theme, back to the default bundled theme (not counting the header color which is modifiable in [...]]]></description>
			<content:encoded><![CDATA[<p>Theres been quite a lot of work going into the blog under the surface recently. The most important being an update to the current version of&#8230;well everything. The most obvious ramification of this process have been the change in theme, back to the default bundled theme (not counting the header color which is modifiable in the settings page of the them). I intend to keep this version which is considerably faster (than K2) and I think still more than usable, while I start developing a completely new theme for the blog (no time frame however, as per usual).</p>
<p>The other change is not particularly obvious, but basically the site is now under the <a href="http://git.or.cz/">Git</a> DVCS. This will make it significantly simpler to manage any updates of the site while concurrently maintaining the code I write for the plugins and visual style. Basically the site is maintained through a collections of branches which are rebased into the deployed branch which is finally published to the server. The files on the server are then reset to the latest version of the deployed branch. The side affect of this being, I can update any single piece using its corresponding feature branch and simply rebase the dependent branches to the last patch on that branch, while any changes after the branch or merge point are simply re-applied afterwards (by Git, not me). Update&#8230;fix conflicts&#8230;push&#8230;done!</p>
<p></p>
<p>So I promised more actual posts I believe, I am getting to it, albeit slowly.</p>
<p>For some background into the lack of actual language related work I&#8217;ve produced lately it helps to understand, arguably the most important, project that I&#8217;m working on right now. The Mention Programming Language is a new language that I&#8217;ve had in the works since the very beginning of this blog. At one point I even had some pages dedicated to the language. Don&#8217;t bother looking, there are no posts about the language yet&#8230; I decided very early on to develop the language in the dark, at least until I was sure that it would lead to an actual release of some sort (I&#8217;ve worked on quite a few dead language designs over the years).</p>
<p>So Mention is a functional programming language with strong influences from Haskell, and Smalltalk. Yup, you heard right, the design of the language is a dynamic one, which despite the Haskell based syntax will provide a myriad of dynamic features like delegation and accessors more, much more reminiscent of Smalltalk than Haskell. There are a couple of other minor influences that creep in there every so often, due to other languages I&#8217;ve worked with or investigated (the design), but those are the main ones.</p>
<p>There&#8217;s only one problem, however. I don&#8217;t understand the math (lambda calculus and type theory) needed to implement the language yet. I&#8217;m getting there but it is taking a little while. The good news is that this wont stop me from posting for much longer. Some of the posts might be about specific things relating the the theory, but the real problem was my lack of a mathematical background. Thanks to my undergrad math text that problem is much less of an issue now and it the Mention project should be getting underway properly very soon, expect some posts on the non-implementation issues like the syntax high level semantics.</p>
<p>On another issue might now be obvious why I haven&#8217;t finished the Scheme parsing library and the corresponding series about its design and implementation&#8230; I&#8217;m not working in Scheme anymore. There is still plenty I could say about the design of Scheme and Lisp dialects, but I&#8217;ll leave that for later, but what I would like to add in post in the near future is the relationship between laziness + monads and strictness + mutable-state. For this comparison I&#8217;ll be using Scheme and Haskell (don&#8217;t worry&#8230;the code will only be for examples in both cases). What won&#8217;t be happening is any more posts or work on the Scheme parser itself.</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/10/yet-more-work-on-the-blog/feed/</wfw:commentRss>
		</item>
		<item>
		<title>New URL, Update your bookmarks&#8230;</title>
		<link>http://alexicalmistake.com/2008/09/new-url-update-your-bookmarks/</link>
		<comments>http://alexicalmistake.com/2008/09/new-url-update-your-bookmarks/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 08:13:23 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://alexicalmistake.com/2008/09/16/new-url-update-your-bookmarks/</guid>
		<description><![CDATA[After sitting on the domain name associated with my online handle (Krysole), I&#8217;ve finally decided it was time to move my blog to an URL using its own namesake.
What does this mean?
From now on the proper URL for A Lexical mistake is simply alexicalmistake.com
The configuration for the site seems to properly redirect pretty much everything [...]]]></description>
			<content:encoded><![CDATA[<p>After sitting on the domain name associated with my online handle (Krysole), I&#8217;ve finally decided it was time to move my blog to an URL using its own namesake.</p>
<p>What does this mean?</p>
<p>From now on the proper URL for A Lexical mistake is simply <a href="http://alexicalmistake.com/">alexicalmistake.com</a></p>
<p>The configuration for the site seems to properly redirect pretty much everything to the new URL (including permalinks&#8230; I&#8217;ll keep the old URL pointing to the site for this very reason).</p>
<p>On another issue, the blog very much isn&#8217;t dead, it just seems that way because I&#8217;ve been somewhat preoccupied lately. The main reason for this is my move to Linux, or more specifically Ubuntu. I&#8217;ve also been transitioning most of my mac dependencies to their web equivalents (Google reader, etc).</p>
<p>The point is I should be back to posting biweekly again (or close too), very soon now.</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/09/new-url-update-your-bookmarks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Experiments in Emacs</title>
		<link>http://alexicalmistake.com/2008/07/experiments-in-emacs/</link>
		<comments>http://alexicalmistake.com/2008/07/experiments-in-emacs/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 08:04:08 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[Blog]]></category>

		<category><![CDATA[Editor]]></category>

		<category><![CDATA[Emacs]]></category>

		<guid isPermaLink="false">http://krysole.net/2008/07/26/experiments-in-emacs/</guid>
		<description><![CDATA[One of the most difficult challenges facing me when beginning to work with both Erlang and Haskell, was the significant lack of good IDEs&#8230; Well, at least that was the theory, anyway. Instead it turned out to be a real motivator to finally get around to investigating the only IDE that counts.
The real problem wasn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most difficult challenges facing me when beginning to work with both Erlang and Haskell, was the significant lack of good IDEs&#8230; Well, at least that was the theory, anyway. Instead it turned out to be a real motivator to finally get around to investigating the only IDE that counts.</p>
<p>The real problem wasn&#8217;t that I couldn&#8217;t use Emacs for basic editing, rather I didn&#8217;t really have any idea how to use any of the extra features. Essentially it was like working with a semi-decent TextEdit.app, with code highlighting and unfamiliar keyboard shortcuts. Even after reading some of Steve Yegge&#8217;s essays I still didn&#8217;t really have any idea what I was doing.</p>
<p>Ok, so basically I&#8217;m now writing this in Emacs&#8217; SGML mode and carefully trying to get around the perverse limitations of Wordpress. (Where the heck did the allow raw HTML checkbox go for author text?) Ironically I don&#8217;t think I&#8217;ve yet gained the productivity advantages from my efforts yet, but honestly that can probably just be attributed to a slow down in my typing speed and the rather unexpected work schedule I&#8217;ve been (you never realize just how much you appreciate   sleep until you don&#8217;t get enough).</p>
<p>If I haven&#8217;t gained any real productivity, the question becomes why then am I still using Emacs to write this post? Well, as it turns out its not actually too bad for even when you don&#8217;t know all of the keyboard commands (all commands come from the keyboard, and most important commands really never migrate to them menu or at least don&#8217;t indicate they&#8217;re keyboard equivelent).</p>
<p>Even more importantly perhaps is the gains that I&#8217;ve achieved in other tasks&#8230;writing blog posts in Emacs may not be any faster for me (although I do seem to see much more of the post at any one time)&#8230;but writing code sure is. I seriously doubt that I&#8217;d change from using things like XCode for Cocoa applications or DrScheme for PLT Scheme applications (although I may change my mind on Scheme),   but at least I have direct access to the REPL in Haskell and Erlang.</p>
<p>The way most of the editing modes in Emacs actually work, it essentially will even load up a file your working directly into a REPL running side by side in another text buffer. From there you can test smaller parts of your program or even just run the whole thing from the top if you needed to. Anyone who&#8217;s worked with a Lisp dialect or   even Python or Erlang (among others) should be able to apreciate that.</p>
<p>There are a couple of things that have been bugging me so far. The first is the lack of a spell checker. Emacs does have built-in support for ispell, but as you probably guessed, it doesn&#8217;t work on my machine (actually it isn&#8217;t even installed on my cygwin install). Too make matters worse, I still need to teach Emacs how to find my Cygwin   install and use programs from it before the windows paths&#8230;ugh&#8230;</p>
<p>And the other thing doesn&#8217;t even have anything to do with Emacs, but rather, its a limitation with the Haskell code I&#8217;m working on. Essentially I don&#8217;t have a framework for OpenGL work that allows me to substitute the code on my side of the framework after the application has been started&#8230;or more to the point I can&#8217;t stop the application after it has started, forcing me to quit the entire REPL every time I   test the Game.</p>
<p>Of course the simple workaround is to just keep restarting the REPL or move out of Emacs into a Cygwin shell, but its still very   inconvenient (damn you GLUT).</p>
<p>All in all I can only really see how it goes, but at the very least it should make life just that little bit easier. At worst, I&#8217;ll just   have to resort to my aging Mac Powerbook (for the BSD layer).</p>
<p>Until next time&#8230;</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/07/experiments-in-emacs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Monad Actions&#8230; ok&#8230; sure&#8230; whatever&#8230;!?</title>
		<link>http://alexicalmistake.com/2008/07/monad-actions-ok-sure-whatever/</link>
		<comments>http://alexicalmistake.com/2008/07/monad-actions-ok-sure-whatever/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 00:35:02 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[Discovered]]></category>

		<guid isPermaLink="false">http://krysole.net/2008/07/13/monad-actions-ok-sure-whatever/</guid>
		<description><![CDATA[When I was first learning about OpenGL in Haskell it dawned on me that the example I was reading was doing some pretty crazy hung up things with Haskell style DSL techniques.
White this might make sense for a syntax based DSL (imho), I wasn&#8217;t sure this was absolutely necessary under OpenGL since the entire library [...]]]></description>
			<content:encoded><![CDATA[<p>When I was first learning about OpenGL in Haskell it dawned on me that the example I was reading was doing some pretty crazy hung up things with Haskell style DSL techniques.</p>
<p>White this might make sense for a syntax based DSL (imho), I wasn&#8217;t sure this was absolutely necessary under OpenGL since the entire library is already just a bunch of monadic actions!?</p>
<p>Ok&#8230; that&#8217;s cool, maybe it was just overlooked, and I thus devised a test. Write a small piece of monadic code that re-evaluated an action over and over again&#8230;and it worked!!!</p>
<p>w00t!!!1one</p>
<p>uh&#8230; yeah&#8230; maybe you should just read this instead&#8230; save yourself some time and embarrassment ;)</p>
<p><a href="http://haskell.org/haskellwiki/IO_inside">IO Inside</a></p>
<p>One the upside&#8230; that game is still slowly progressing&#8230;</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/07/monad-actions-ok-sure-whatever/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Haskell Does Concurrency</title>
		<link>http://alexicalmistake.com/2008/07/haskell-does-concurrency/</link>
		<comments>http://alexicalmistake.com/2008/07/haskell-does-concurrency/#comments</comments>
		<pubDate>Sat, 05 Jul 2008 07:25:14 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[Discovered]]></category>

		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[Haskell]]></category>

		<category><![CDATA[STM]]></category>

		<category><![CDATA[transactions]]></category>

		<guid isPermaLink="false">http://krysole.net/2008/07/04/haskell-does-concurrency/</guid>
		<description><![CDATA[Ok, by now we all know that Object Oriented programming is likely to go the way of Erlang, or at least inherit an Actor model of language level concurrency.
In fact, I&#8217;ve even got an object system that I&#8217;m planning to implement just to find out how well this style of concurrency works in an Object [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, by now we all know that Object Oriented programming is likely to go the way of Erlang, or at least inherit an Actor model of language level concurrency.</p>
<p>In fact, I&#8217;ve even got an object system that I&#8217;m planning to implement just to find out how well this style of concurrency works in an Object Oriented programming language. The design of the language is even more post-Self/Slate than Newspeak (although I believe that this language will just complement the Smalltalk family of languages in all honesty).</p>
<p>Well then, Haskell&#8230;Haskell is actually very interesting, in this case by being one of the very few to push the limits of locking methods for concurrency rather than using the larger granularity involved in processes and message passing (not by much, but its still something).</p>
<p>The problem is that its impossible to write locking code by hand that doesn&#8217;t have at least one race condition hiding away in the corner. Not the ones you&#8217;ve already tested for, but the ones that would cause transactions to restart&#8230;like multiple threads locking a few items and then deadlocking&#8230;or even just managing the code required to restart deadlocks&#8230;yeah&#8230;lol&#8230;</p>
<p>Haskell provides a novel approach (its actually pretty old now, but it&#8217;s really only just starting to catch on, outside of Haskell anyway)&#8230; <a href="http://en.wikipedia.org/wiki/Software_transactional_memory">Software Transactional Memory</a>.</p>
<p>STM is sometimes used in other areas of computing (OODBs, <em>et ceteral</em>), but the real gem here is that the Haskell type system nicely compartmentalized the side effects involved into a clever transaction form, within our imperative outer program. This makes working with transactions very much like the database stuff, only we&#8217;re only working with individual variables, so no complex table lookups.</p>
<p>Anyway, enough of my rambling, check it out:<br />
<a href="http://channel9.msdn.com/shows/Going+Deep/Programming-in-the-Age-of-Concurrency-Software-Transactional-Memory/">Programming in the Age of Concurrency Software Transactional Memory</a></p>
<p>And if you haven&#8217;t already, remember to look at Erlang too&#8230;after all concurrency isn&#8217;t going away, and there are only two ways to get it right!</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/07/haskell-does-concurrency/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsers and Combinators</title>
		<link>http://alexicalmistake.com/2008/06/parsers-and-combinators/</link>
		<comments>http://alexicalmistake.com/2008/06/parsers-and-combinators/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 10:36:15 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[MMeta]]></category>

		<category><![CDATA[Combinators]]></category>

		<category><![CDATA[Parsing]]></category>

		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://krysole.net/2008/06/26/parsers-and-combinators/</guid>
		<description><![CDATA[Last time I showed some brief examples of what a parser combinator is, how they work and how I intended to use them to solve the parsing problem using the Scheme programming language. This time we get to start the interesting stuff&#8230;
To get things going, last time we looked at a simple character facility which [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I showed some brief examples of what a parser combinator is, how they work and how I intended to use them to solve the parsing problem using the Scheme programming language. This time we get to start the interesting stuff&#8230;</p>
<p>To get things going, last time we looked at a simple character facility which returns a parser that will only accept that particular character. We can also use this technique to select from a range of characters.</p>
<pre lang="Scheme">
(define (char-range start end)
  (λ (state)
    (let ([rc (read-char state)])
      (cond ((eof-object? rc) fail)
            ((and (char>=? rc start)
                  (char<=? rc end))
             rc)
            (else fail)))))
</pre>
<p>We might also wish to repeat the actions of a parser, which involves writing a proper combinator. Actually, this is the first real utility parser we&#8217;ll see and it simply allows us to infinitely repeat the parser provided.</p>
<pre lang="Scheme">
(define (many p)
  (letrec ([mp (λ (state acc)
                 (aif result fail? ((with-mark p) state)
                      acc
                      (mp state (cons result acc))))])
    (λ (state)
      (reverse (mp state '())))))
</pre>
<p>This defines a small function that accumulates a list (backwards as usual) by repeatedly applying the parser (<code>p</code>). The <code>with-mark</code> stuff is the second combinator we&#8217;ll look at, which is used here to prevent the stream position from moving beyond the last match.</p>
<p>The description of <code>many</code> would be a combinator that continues to apply a parser until it fails, reverting the parser&#8217;s state to the state prior to running the final failing application of the parser.</p>
<p>The definition of <code>with-mark</code> is even simpler (or should this be <code>with-state</code>?).</p>
<pre lang="Scheme">
(define (with-mark parser)
  (λ (state)
    (let ([mark (file-position state)])
      (aif v fail? (parser state)
           (begin (file-position state mark)
                  v)
           v))))
</pre>
<p>Our parser state is of course still non-functional, based simply on the port capabilities built into Scheme. We check to make sure the parser doesn&#8217;t fail, and if it does we reset the parsers state by recording it before hand. Non-functional user state can be stored in a larger closure/environment, and this could be changed to support functional state as a second variable (in a data structure or list), but I&#8217;ll leave to a later post, or the reader of course.</p>
<p>Running our parser is actually this simple (from the <em>repl</em>).</p>
<pre lang="Scheme">
(define digit
  (char-range #\0 #\9))

(define state (open-input-string "12345"))

((many digit) state)
</pre>
<p>There is one slight problem with this&#8230; It accepts any number of digits&#8230; including zero digits! I&#8217;ve provided a second version called <code>many1</code>, but you could actually define it like the following snipped. (Note: See the bottom of this post to find the source code as it currently appears).</p>
<p>Ok, we still need a way to parse whitespace, and to abstract the tokens of whatever language we&#8217;re implementing. To help us here we can write a whitespace parser and a token combinator which will help us.</p>
<pre lang="Scheme">
(define ws-char
  (λ (state)
    (let ([rc (read-char state)])
      (cond ((eof-object? rc) fail)
            ((char-whitespace? rc) rc)
            (else fail)))))

(define whitespace
  (many1 ws-char))

(define (wstok tparser)
  (mand (opt whitespace)
        tparser))
</pre>
<p>I&#8217;d just like to point out before we more on&#8230; the <code>ws-char</code> parser here actually recognizes any Scheme whitespace character. I could have written this parser to recognize some other whitespace character set but I felt that this would serve us better for now, since we might eventually want to test our method against the existing Scheme language, or at least a sizable subset.</p>
<p>Again, using this is quite simple&#8230; to recognize a number token we can simple define the number token parser as <code>(wstok (many1 digit))</code>.</p>
<p>Now we still have one minor issue here&#8230; what if we don&#8217;t want to simply recognize the language, but also produce some direct result. For example, our parser so far can recognize number tokens, but it doesn&#8217;t return those numbers&#8230; just the lists of characters that make them up! (Without the whitespace of course.)</p>
<pre lang="Scheme">
(define t-number
  (mlet nstr (wstok (many1 digit))
    (! (string->number (list->string nstr)))))
</pre>
<p>This is where the magic begins&#8230; <code>mlet</code> and <code>!</code> are just macros which expand to scheme let and raw expressions wrapped in lambdas which require the current parser state. <code>mlet</code> then also takes a symbol to bind, and two parsers, which is why we need the <code>!</code> macro magic. Remember that parsers are just lambdas that take a single parameter&#8230; it doesn&#8217;t matter if they don&#8217;t use that parameter, or just pass it on (like <code>mlet</code>).</p>
<p>It might also be interesting to note that we could write a combinator that took a function that accepted the results of the parser, and produced a replacement result. Technically this parser is actually very much like the one we just wrote, except that it can only filter the results of a parser, and not the results of several parsers which can only be achieved though nested <code>mlet</code> expressions. Usage might look similar to the following.</p>
<pre lang="Scheme">
(define t-number
  (return nstr (wstok (many1 digit))
    (string->number (list->string nstr))))
</pre>
<p>Pretty close right.</p>
<p>By this point I thing most of you are starting to see what we&#8217;re getting to&#8230; the canonical example of an expression parser, but this time I&#8217;m actually going to add a slight twist to the story instead. We&#8217;re actually going to generate Scheme code instead, and then use the standard <code>eval</code> function instead. I know this might seem like a strange thing to do, especially since we don&#8217;t normally use <code>eval</code> in most Scheme code, but think forward a little. Normally you wouldn&#8217;t directly evaluate the code you just parsed either&#8230; think of <code>eval</code> as our interpreter, operating on an abstract syntax tree&#8230; conveniently Scheme code is already very much like an AST so we just use that.</p>
<pre lang="Scheme">
(define t-add
  (return _ (wstok-char #\+)
    '+))
</pre>
<p>This gives us our addition token returning, obviously, the Scheme <code>+</code> operator. And finally two more pieces to put it all together.</p>
<pre lang="Scheme">
(define (left-assoc term op)
  (letrec ([f (λ (state acc)
                (aif lst fail? ((with-mark (all op term)) state)
                     acc
                     (f state
                        (list (car lst) acc (cadr lst)))))]) ;; (op lhs rhs)
    (mlet result term
      (λ (state)
        (f state result)))))

(define expr
  (left-assoc t-number t-add))
</pre>
<p>Finally, to end this&#8230; rather lengthy&#8230; example. Here is a finished REPL using our parser&#8230;</p>
<pre lang="Scheme">
(define (expr-repl)
  (display ">> ")
  (let ([line (read-line)])
    (if (string-ci=? "exit" line)
        '()
        (begin
          (display " = ")
          (display (eval (expr (open-input-string line))))
          (newline)
          (expr-repl)))))
</pre>
<p>Calling it is pretty simple, and we can get results like so&#8230;</p>
<pre>
> (expr-repl)
>> 1
 = 1
>> 1+2+3+4
 = 10
>> 1 + 2 + 3 + 4
 = 10
>> exit
()
</pre>
<p>The purpose of this simple example is to show you how our combinators and point out what, in my opinion, is possibly the most important observation to make about parser combinators&#8230; all our combinators return parsers and all our parsers are not combinators. Yup, you always have to realize when you&#8217;re looking at a combinator, and when your looking at a parser. Hell, you could even be looking at a combinator that returns a combinator! (currying <em>et cetera</em>)</p>
<p>Also, in the code I&#8217;ve used above, I made a point of following a coding convention that separates top-level parser definitions by using lambdas, and combinators or simple functions which use the define syntax instead. I found that this makes it easier to visually distinguish between the two, without having to resort to an explicit naming convention.</p>
<p>Next time we&#8217;ll move beyond this simple stuff and I&#8217;ll try to investigate more complicated combinators that don&#8217;t take parsers but data structures to construct their resulting parser.</p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/06/parsers-and-combinators/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Recognize This!</title>
		<link>http://alexicalmistake.com/2008/06/recognize-this/</link>
		<comments>http://alexicalmistake.com/2008/06/recognize-this/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 16:22:35 +0000</pubDate>
		<dc:creator>Lorenz Pretterhofer</dc:creator>
		
		<category><![CDATA[MMeta]]></category>

		<category><![CDATA[Combinators]]></category>

		<category><![CDATA[Parsing]]></category>

		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://krysole.net/2008/06/15/recognize-this/</guid>
		<description><![CDATA[Ok&#8230; I may not have entirely explained the machinery behind the basic backtracking parser combinators yet, but before I get to far into the heavy stuff I thought it might be useful to discuss some basic context issues involved in writing parsers.
There are two main elements involved in writing parsers&#8230; The Why&#8230; and the How [...]]]></description>
			<content:encoded><![CDATA[<p>Ok&#8230; I may not have entirely explained the machinery behind the basic backtracking parser combinators yet, but before I get to far into the heavy stuff I thought it might be useful to discuss some basic context issues involved in writing parsers.</p>
<p>There are two main elements involved in writing parsers&#8230; The <em>Why</em>&#8230; and the <em>How</em> (to some extent <em>what</em> and <em>when</em> also fit here&#8230; these are technical aspect that is).</p>
<p><strong>Why do we write parsers?</strong></p>
<p>There are actually a few reasons, and this isn&#8217;t really a particularly interesting question for us (after all, we&#8217;re already writing a parsers aren&#8217;t we?), but I would like to briefly recap what I believe are important points to keep in mind when designing a parser combinator framework.</p>
<p>There are two main varieties of parsers, which come in the form of <em>prototypes</em> and <em>production</em> parsers. Regardless of what the parser is being used for, the purpose of the parser is either to support another process, or it is simply to test some linguistic nature of a computer language (concrete syntax typically).</p>
<p>Of these two categories, we also stumble over a hybrid variation, where a parser written for production use may end up serving a prototype role and, of course, prototype parsers may end up evolving into production parsers.</p>
<p>The importance of this categorization is the implications of generally knee-jerk decisions associated with each approach. The main decisions we need to worry about ourselves when designing a parser framework are of course the error reporting and recovery mechanisms, basic performance concerns, and to some extent the language and tool support.</p>
<p>This isn&#8217;t just limited to our intended audience either, but also to ourselves as parser implementors&#8230; its all to common to consider why we are writing a parser and select a methodology based purely on practical concerns, and yet, quite often the very notation we use to write the parser is more important. The notation can have very severe effects on the cognitive processes involved in writing a parser or computer language, and so too can the tools that support that notation.</p>
<p><strong>How do we implement parsers?</strong></p>
<p>This part is a little simpler to describe at a high level&#8230; that and we&#8217;ll be delving into the issues more closely later on anyway.</p>
<p>I personally like to break the implementation concerns into three major categories&#8230; <em>recognition</em>, <em>state</em>, and <em>output</em>.</p>
<p>Each of these can become extraordinarily complex and intertwined, but they always tend to follow the same basic trade-offs which we&#8217;ll discuss briefly here.</p>
<p><strong>Recognizing</strong> input is possibly the most obvious element of parser construction, since it tends to follow the design of the language anyway. More specifically it tends to follow the formal definition of a language, and most conventional notations use the same form as the BNF or PEG formal notation.</p>
<p>While it might seem useful to apply the same notation as the formal definition of a language, imo its not particularly friendly in practice. The main issue with this trade off is the difficulty in implementing non-trivial concerns like left-factoring, lookahead, error reporting/recovery or simply the fact that they don&#8217;t contain any proper form of abstraction (no state information or context sensitivity).</p>
<p>This the actually the reason we&#8217;re using a combinator approach&#8230; it allows us to capture the conceptual elements involved rather than directly writing a whole lot of repetitive rules, simply because the parser generator tool we selected, didn&#8217;t realize that scanners aren&#8217;t the only type of repetitive task in language writing (expression/operator grammars for example)&#8230; or even the lowly delimited list based on non-conventional parameter parsers.</p>
<p>This might actually be a good time to point out the <a href="http://www.antlr.org/">ANTLR</a> parser generator as an example of the kind of trade of we&#8217;re trying to avoid. I can&#8217;t stress enough how often I&#8217;ve wished I could refactor part of a parser involving delimited lists, enclosed parsers, associativity and precedence rules, and it just gets worse from there. The problem? No higher-level abstraction of rules (its like programming a user interface with 1st order logic&#8230; well&#8230; maybe not quite that bad but you get the idea).</p>
<p><strong>Collecting state</strong> is the second important consideration when parsing a computer language. It might be interesting to consider what kind of state you&#8217;re accumulation however. In a calculator for example, the state is actually the current number and possibly a bindings table for variables if you allow them, but for a C parser you also need the symbol table and possibly even some other annotations for the preprocessor phase.</p>
<p>The point I&#8217;m trying to make here is not that parsers must return something&#8230; we&#8217;ll get to that in a second&#8230; the point is that many parser have other state that is needed by the other software components that receive the output form. This state obviously cannot be collected directly as a result, and yet it also has to be collected directly as a result of parsing.</p>
<p>Why do I care so much about these side effects? Its a perfect opportunity to reflect on Parsec&#8217;s usage of monads&#8230; and why we don&#8217;t need them!</p>
<p>Yup&#8230; that&#8217;s right, monads primarily capture a few things, but the main elements are timely evaluation (not necessarily eager), imperative ordering of evaluation, and propagation of out-of-band state information. To paraphrase&#8230; side effects and imperative code blocks. (Monads are actually capable of quite a bit more than this, but in our case, none of that behaviour is relevant.)</p>
<p>Actually we do still need to pass state, but whether we do it globally (uh&#8230;), or through an accumulator doesn&#8217;t matter&#8230; we have a different way of writing side effects, and we&#8217;ll use that for them. Scheme also provides the begin notation for us, so we have a perfectly viable means of writing imperative code if we need it, but even in Parsec it&#8217;s rarely a necessity.</p>
<p>Finally we come to the <strong>Output form</strong>, which is almost exclusively broken down into Abstract Syntax Trees (ASTs), Compilers (intermediate or machine code output), Simple Recognizers, Translators (Scheme to C for example), and Interpreters. Of these only ASTs are used when we have reasonable levels of computation power available, while the other forms are typically relegated to AST parsers or in our case, the native pattern matching and program code of the host language.</p>
<p>The big challenge we&#8217;re facing here however is actually related somewhat to the difference between prototype and production parsers. If we wish to provide good support for prototyping, we have to realize that its not entirely uncommon for interpreters to be written without an accompanying AST representation. Unless we can make the AST representation so inexpensive to generate and extraordinarily useful, we may wish to provide support for both ASTs and interpreters.</p>
<p>Whatever trade-off we make however, as long as we realize that we need to effectively support all of the possible applications unless we expect programmers to also use another parser framework or generator. Not necessarily perfectly, but at least adequately (we need to at least make generating ASTs cheap so that programmers can work with them even if the other forms are not simple to directly implement).</p>
<p>Before I finish I&#8217;d also like to add a small note about how parsers are actually used&#8230; or more specifically that the tools we provide for testing, debugging and maintaining our parsers are possibly more important than the ability to write the parsers in the first place.</p>
<p>For those who have used <a href="http://www.antlr.org/">ANTLR</a> before, you probably realize what I&#8217;m talking about here&#8230; yes the ANTLR grammars are limited, and painful to effectively refactor, when half the time you&#8217;re only even generating ASTs anyway. But the grammar debuggers and generally the ANTLR Works IDE are exceptional tools for constructing parsers. <em>Also&#8230; I don&#8217;t mean to pay out the ANTLR parser generator&#8230; it really is the best tool I&#8217;ve used with Java or C++&#8230; its just not as powerful as I believe we can get in Scheme</em></p>
<p>While we can simplify the construction of most parsers greatly, it would be even better if we could also provide dedicated tooling for profiling, debugging, inspecting or even just testing our parsers. To this end, I&#8217;ll be adding various tooling issues interspersed with the construction of various parser facilities as we construct hopefully the canonical Scheme parser framework&#8230; or at least make the framework as useful as possible.</p>
<p><em>I don&#8217;t intend on covering any of this again in future posts in the series. Instead we&#8217;ll only be investigating the trade offs relevant for a Scheme parser combinator framework</em></p>
<p>&#8211; Lorenz</p>
]]></content:encoded>
			<wfw:commentRss>http://alexicalmistake.com/2008/06/recognize-this/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
