<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Greg Heartsfield</title>
 <link href="http://gregheartsfield.com/atom.xml" rel="self"/>
 <updated>2010-07-24T21:28:56+00:00</updated>
 <id>http://gregheartsfield.com/</id>
 <author>
   <name>Greg Heartsfield</name>
   <email>scsibug@imap.cc</email>
 </author>

 
 <entry>
   <title>Version Control Diagrams with TikZ</title>
   <link href="http://gregheartsfield.com/2009/05/26/version-control-diagrams-with-tikz.html"/>
   <updated>2009-05-26T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2009/05/26/version-control-diagrams-with-tikz</id>
   <content type="html">&lt;h1&gt;Version Control Diagrams with TikZ&lt;/h1&gt;
&lt;p&gt;When discussing branching and merging, it can be useful to illustrate &lt;a href=&quot;http://www.cmcrossroads.com/bradapp/acme/branching/&quot;&gt;common patterns&lt;/a&gt; with version control diagrams.  A convenient method for making these is with the &lt;a href=&quot;http://sourceforge.net/projects/pgf/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;PGF&lt;/span&gt;/TikZ package&lt;/a&gt; within LaTeX.  The following are some simple examples with source code.&lt;/p&gt;
&lt;h2&gt;Feature Branch&lt;/h2&gt;
&lt;p&gt;A simple feature branch, that is merged back into the trunk.  &lt;a href=&quot;/files/vcd/vcd_feature.tex&quot;&gt;Source Code&lt;/a&gt;.&lt;br /&gt;
&lt;img src=&quot;/files/vcd/vcd_feature.png&quot; alt=&quot;Feature branch merged into trunk&quot; /&gt;&lt;/p&gt;
&lt;h2&gt;Feature Branches with Synchronization&lt;/h2&gt;
&lt;p&gt;A couple of feature branches that are synchronized with trunk, and one is shown being merged back into trunk. &lt;a href=&quot;/files/vcd/vcd_feature_synch.tex&quot;&gt;Source Code&lt;/a&gt;.&lt;br /&gt;
&lt;img src=&quot;/files/vcd/vcd_feature_synch.png&quot; alt=&quot;Feature branch synchronization with trunk&quot; /&gt;&lt;/p&gt;
&lt;h2&gt;Release Branches&lt;/h2&gt;
&lt;p&gt;Multiple release branches, with changes merged to trunk.  &lt;a href=&quot;/files/vcd/vcd_release.tex&quot;&gt;Source Code&lt;/a&gt;.&lt;br /&gt;
&lt;img src=&quot;/files/vcd/vcd_release.png&quot; alt=&quot;Multiple release branches&quot; /&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Introducing cabal2doap</title>
   <link href="http://gregheartsfield.com/2009/01/04/introducing-cabal2doap.html"/>
   <updated>2009-01-04T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2009/01/04/introducing-cabal2doap</id>
   <content type="html">&lt;h1&gt;Introducing cabal2doap&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://hackage.haskell.org/&quot;&gt;Hackage&lt;/a&gt; is a fantastic resource for developers, but one disadvantage is that non-Haskellers rarely get exposed to the great software that is being developed in Haskell.  Hackage lists nearly 1,000 projects currently, but checking on &lt;a href=&quot;http://freshmeat.net/&quot;&gt;freshmeat.net&lt;/a&gt;, there are only 73 Haskell projects.  As a developer, I don&amp;#8217;t want to have to submit and maintain project pages at Hackage, Freshmeat, Sourceforge, etc., but of course, I still want the largest audience possible.&lt;/p&gt;
&lt;p&gt;One way to spread knowledge about software projects is to publish description-of-a-project (&lt;a href=&quot;https://trac.usefulinc.com/doap&quot;&gt;&lt;span class=&quot;caps&quot;&gt;DOAP&lt;/span&gt;&lt;/a&gt;) files.  These are formal descriptions of project stuff:  homepage, maintainers, contributors, language, versions, and much more.  There is a large overlap between the &lt;span class=&quot;caps&quot;&gt;DOAP&lt;/span&gt; vocabulary and &lt;a href=&quot;http://haskell.org/cabal&quot;&gt;Cabal&lt;/a&gt;, the packaging system used for all Hackage projects.  So, for the past few days I&amp;#8217;ve been toying around with making a &lt;a href=&quot;http://gregheartsfield.com/cabal2doap&quot;&gt;Cabal to &lt;span class=&quot;caps&quot;&gt;DOAP&lt;/span&gt; converter&lt;/a&gt;, which is now ready for others to use.&lt;/p&gt;
&lt;p&gt;Running the program in the root of a cabalized project will output &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt;-&lt;span class=&quot;caps&quot;&gt;RDF&lt;/span&gt;.  If the project is a Darcs repository, the commit history will be scanned, and developers will be noted in the output as well.&lt;/p&gt;
&lt;p&gt;I hope that this will lead in the near future to Hackage generating &lt;span class=&quot;caps&quot;&gt;DOAP&lt;/span&gt; data every time a new project is uploaded.  There is lots of important data that is best gleaned directly from Hackage, such as the past releases, and download locations.  I plan on making future improvements to aid in Hackage-cabal2doap integration, especially in the area of processing multiple Cabal files to build up release history.  Before that though, I&amp;#8217;d like to see if/how my projects become more accessible by virtue of having &lt;span class=&quot;caps&quot;&gt;DOAP&lt;/span&gt; data published.  For example, how well aggregation services like &lt;a href=&quot;http://doapspace.org/&quot;&gt;Doapspace&lt;/a&gt; and &lt;a href=&quot;http://doapstore.org&quot;&gt;doap:store&lt;/a&gt; function.&lt;/p&gt;
&lt;p&gt;A neat way to understand some of the power of representing data formally in &lt;span class=&quot;caps&quot;&gt;RDF&lt;/span&gt; is to view it in graph form, such as that provided by the &lt;a href=&quot;http://www.w3.org/RDF/Validator/&quot;&gt;W3C &lt;span class=&quot;caps&quot;&gt;RDF&lt;/span&gt; Validation Service&lt;/a&gt;.  For example, the following is a representation of the &lt;span class=&quot;caps&quot;&gt;DOAP&lt;/span&gt; data published for cabal2doap itself.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/images/cabal2doap_doap_graph.png&quot;&gt;&lt;img alt=&quot;Graph of Cabal2DOAP project information&quot; src=&quot;http://gregheartsfield.com/images/cabal2doap_doap_graph_small.png&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can start with the &lt;a href=&quot;http://gregheartsfield.com/cabal2doap/&quot;&gt;official project homepage&lt;/a&gt;, or its &lt;a href=&quot;http://hackage.haskell.org/cgi-bin/hackage-scripts/package/cabal2doap&quot;&gt;home on Hackage&lt;/a&gt;.  If you have Firefox and &lt;a href=&quot;http://www.w3.org/2005/ajar/tab&quot;&gt;Tabulator&lt;/a&gt; or &lt;a href=&quot;http://sioc-project.org/firefox&quot;&gt;Semantic Radar&lt;/a&gt;, you can browse to a couple projects I host, which have linked &lt;span class=&quot;caps&quot;&gt;DOAP&lt;/span&gt; data generated from cabal2doap.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://gregheartsfield.com/hS3/&quot;&gt;hS3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://gregheartsfield.com/cabal2doap/&quot;&gt;cabal2doap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://gregheartsfield.com/fractal-hs/&quot;&gt;fractal-hs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>hS3, with ByteString</title>
   <link href="http://gregheartsfield.com/2008/12/23/hs3-with-bytestring.html"/>
   <updated>2008-12-23T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2008/12/23/hs3-with-bytestring</id>
   <content type="html">&lt;h1&gt;hS3, with ByteString&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://www.galois.com/&quot;&gt;Galois&lt;/a&gt; has released an &lt;a href=&quot;http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HTTP&quot;&gt;updated haskell &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; module&lt;/a&gt;, supporting &lt;tt&gt;ByteString&lt;/tt&gt; requests and responses, instead of just &lt;tt&gt;String&lt;/tt&gt;.  This provides a significant performance improvement, and dramatically reduces memory usage.  Since &lt;a href=&quot;http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hS3&quot;&gt;hS3&lt;/a&gt; uses &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; for all network communication, it wasn&amp;#8217;t really usable for files larger than a few megabytes.  I&amp;#8217;ve pushed a new released of hS3 (0.4), and did some quick measurements to see the improvements.&lt;/p&gt;
&lt;p&gt;I generated a 1MB file of random data, and uploaded it to S3 using &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;-3000 (String), and &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;-4000 (ByteString.Lazy).  Real time was limited by network bandwidth, so there was no improvement in that regard.  User (&lt;span class=&quot;caps&quot;&gt;CPU&lt;/span&gt;) time was greatly improved:&lt;/p&gt;
&lt;pre&gt;
HTTP-3000 (String):         35.859s
HTTP-4000 (ByteString.Lazy): 0.072s
&lt;/pre&gt;
&lt;p&gt;Using the &lt;span class=&quot;caps&quot;&gt;GHC&lt;/span&gt; profiler, I also uploaded a 5MB file using the old and new libraries, which demonstrates the reduced memory requirements.  The vertical axis is the same on both graphs, but I&amp;#8217;ve had to reduce the scale of the horizontal (time) axis on the second graph, since the ByteString version only required about 1/500th &lt;span class=&quot;caps&quot;&gt;CPU&lt;/span&gt; to execute.&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/images/hs3_bytestring/hs3_string_big.png&quot;&gt;&lt;img src=&quot;http://gregheartsfield.com/images/hs3_bytestring/hs3_string.png&quot; alt=&quot;hS3 benchmark with String type&quot;/&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://gregheartsfield.com/images/hs3_bytestring/hs3_bytestring_big.png&quot;&gt;&lt;img src=&quot;http://gregheartsfield.com/images/hs3_bytestring/hs3_bytestring.png&quot; alt=&quot;hS3 benchmark with ByteString type&quot;/&gt;&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Euler's Line in MetaPost</title>
   <link href="http://gregheartsfield.com/2008/08/31/eulers-line-in-metapost.html"/>
   <updated>2008-08-31T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2008/08/31/eulers-line-in-metapost</id>
   <content type="html">&lt;h1&gt;Euler&amp;#8217;s Line in MetaPost&lt;/h1&gt;
&lt;p&gt;Euler&amp;#8217;s line, showing that the orthocenter, centroid, and circumcenter are collinear.  Created with MetaPost.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://gregheartsfield.com/images/eulers_line.png&quot; alt=&quot;Eulers line&quot; /&gt;&lt;/p&gt;
&lt;p&gt;This was all defined in MetaPost parametrically, so it&amp;#8217;s trivial to create the line &lt;a href=&quot;http://gregheartsfield.com/images/eulers_line_b.png&quot;&gt;through any (non-equilateral) triangle&lt;/a&gt;.  Showing the&lt;a href=&quot;http://gregheartsfield.com/images/eulers_line_npc.png&quot;&gt; center of the 9-point circle is collinear&lt;/a&gt; as well is easy too.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/code/eulers_line.mp&quot;&gt;Source here.&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>ICFP '08 Contest</title>
   <link href="http://gregheartsfield.com/2008/07/20/icfp08-contest.html"/>
   <updated>2008-07-20T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2008/07/20/icfp08-contest</id>
   <content type="html">&lt;h1&gt;&lt;span class=&quot;caps&quot;&gt;ICFP&lt;/span&gt; &amp;#8217;08 Contest&lt;/h1&gt;
&lt;p&gt;I gave the &lt;a href=&quot;http://icfpcontest.org/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;ICFP&lt;/span&gt; contest&lt;/a&gt; a try again this year, and had a really good time.  The task was to write a controller for a martian rover, capable of avoiding basic obstacles like boulders, as well as enemy martians of unknown intelligence.  Telemetry input streamed from a network socket to the controller, and basic control commands such as accelerate or turn left, had to be sent back.&lt;/p&gt;
&lt;p&gt;Unlike previous years, where it took some time (several hours of implementing an interpreter spec) to really understand the scope of the problem, you knew what you were getting into after reading the very concise task description.  I immensely enjoyed the previous formats, but change is good.&lt;/p&gt;
&lt;p&gt;Haskell seemed as sensible a choice as anything else, so I went with it.  Sending and receiving formatted data (a sample server was provided) over a network socket was the first task, and that was thankfully straightforward.  4 lines (connect, unbuffer, receive, send), and no more I/O to bother with.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/repos/icfp08/src/ServerMessages.hs&quot;&gt;Parsing messages&lt;/a&gt; from the server was a bit clumsy, I have promised myself I will learn Parsec soon.  &lt;a href=&quot;http://gregheartsfield.com/repos/icfp08/src/ClientMessages.hs&quot;&gt;Sending messages&lt;/a&gt; was nicer though, since I just had to define the datatype, and make a simple instance of Show.&lt;/p&gt;
&lt;p&gt;I was somewhat familiar with &lt;a href=&quot;http://www.haskell.org/yampa/&quot;&gt;Yampa&lt;/a&gt; before the contest, but I could probably spend a whole weekend just fully understanding how it works, so I decided to stick to a simple turn-based strategy.  For every telemetry message sent by the server, I would compute the command sequence to send to the rover, and then wait for new telemetry.  The server latency was small enough (~100ms) to make this acceptable.&lt;/p&gt;
&lt;p&gt;The biggest challenge was deciding how to structure the event loop, and create composeable strategies for the rover.  Given the initial message, and a telemetry message, the controller could probably do a satisfactory job of deciding what commands to send.  I had some grand plans of updating a global view of the map, as well as tracking acceleration/braking constants, so I wanted to keep track of some changing state for each turn.  My simple solution, which I think was the right choice given the time constraints, was to create a datatype for the rover/world state, and write strategies as transformers of that state.  The natural choice to handle this was the State monad.  The world (init message), the rover (acceleration/turning state), and the current and past telemetry were encapsulated inside the monad, and the return value was a list of command messages.  Each strategy would be able to inspect the telemetry and current rover state, and make changes, or do nothing.  By ordering these strategies, I was able to make a rover that charged ahead towards the goal, until it detected something in its path (causing it to turn), or detected it was going to overshoot its goal (causing it to slow down).  Strategies could be combined in order, with the last one able to override any previous decisions.&lt;/p&gt;
&lt;p&gt;Strategies manipulated the desired state of the rover, which was represented as a &lt;a href=&quot;http://gregheartsfield.com/repos/icfp08/src/RoverStateMachine.hs&quot;&gt;state machine&lt;/a&gt;.  Commands had to be sent that took the rover from its previous state (described in telemetry messages) to the desired state.  Making each of the state components an instance of Enum made this transition simple.  I just had to count how many applications of the succ or pred functions were necessary to move the state to the goal.&lt;/p&gt;
&lt;p&gt;Finally, in the &lt;a href=&quot;http://gregheartsfield.com/repos/icfp08/src/Main.hs&quot;&gt;main program&lt;/a&gt;, the state transforming function was mapped over the parsed network input, and the result was evaluated, and sent back over the wire.&lt;/p&gt;
&lt;p&gt;You can view my solution here: &lt;a href=&quot;http://gregheartsfield.com/repos/icfp08/&quot;&gt;Darcs repo of icfp08&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Cheers to the organizers, who did a really fantastic job creating the task (crazy difficult, but very approachable), manning &lt;span class=&quot;caps&quot;&gt;IRC&lt;/span&gt;, and keeping track of issues/&lt;span class=&quot;caps&quot;&gt;FAQ&lt;/span&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Byteflow</title>
   <link href="http://gregheartsfield.com/2008/05/26/byteflow.html"/>
   <updated>2008-05-26T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2008/05/26/byteflow</id>
   <content type="html">&lt;h1&gt;Byteflow&lt;/h1&gt;
&lt;p&gt;Wordpress is out, &lt;a href=&quot;http://byteflow.su/&quot;&gt;Byteflow&lt;/a&gt; is in.  I was primarily looking for a small, fun codebase to hack on, and WP/&lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; was anything but that.&lt;/p&gt;
&lt;p&gt;I spent the past few days getting up to speed with &lt;a href=&quot;http://www.djangoproject.com/&quot;&gt;Django&lt;/a&gt;, and in fact I&amp;#8217;ve already submitted several patches against byteflow, so it is definitely quick to get going with.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve also spent quite a while learning how to get around within &lt;a href=&quot;http://www.selenic.com/mercurial/wiki/&quot;&gt;Mercurial&lt;/a&gt;.  Maybe i&amp;#8217;ll change my mind after doing some more work with it, but I find the usability very poor compared to &lt;a href=&quot;http://darcs.net/&quot;&gt;Darcs&lt;/a&gt;.  &lt;a href=&quot;http://www.selenic.com/mercurial/wiki/index.cgi/MqExtension&quot;&gt;Mercurial Queues&lt;/a&gt; are probably the neatest feature, but that too seems to regularly lead to disaster.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve written a WordPress importer as well, which I used to import all the content from my old blog: &lt;a href=&quot;http://gregheartsfield.com/code/WXRImporter.py&quot;&gt;WXRImporter.py&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>QuickCheck Love</title>
   <link href="http://gregheartsfield.com/2007/10/22/quickcheck-love.html"/>
   <updated>2007-10-22T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/10/22/quickcheck-love</id>
   <content type="html">&lt;h1&gt;QuickCheck Love&lt;/h1&gt;
&lt;p&gt;If you actually want to get started with QuickCheck, check out the &lt;a href=&quot;http://haskell.org/haskellwiki/Introduction_to_QuickCheck&quot;&gt;HaskellWiki&lt;/a&gt;.  The following is just meant to give a simple motivating example for using QuickCheck.&lt;/p&gt;
&lt;p&gt;We&amp;#8217;ve implemented some simple symmetric block ciphers in Haskell, all of which have encryption/decryption functions with types like the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;tea_enc :: Int                              -- Cycle count
        -&amp;gt; (Word32, Word32, Word32, Word32) -- 128-bit key
        -&amp;gt; (Word32, Word32)                 -- Input block
        -&amp;gt; (Word32, Word32)                 -- Output block
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For these ciphers, we want to verify the very simple property that when using the same key, encrypting and then decrypting a block will yield that same block.  That property is represented with the code below (which takes the encryption/decryption ciphers, a key, and an input block as arguments):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;test_identity enc dec key input =
    input == dec key (enc key input)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we want to use QuickCheck to randomly generate keys and input blocks, and check the identity property.  To do that, we&amp;#8217;ll change the function to just take the cipher algorithms as arguments, and use QuickCheck to generate the rest.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;test_identity enc dec =
    quickCheck (\key input -&amp;gt;
                input == dec key (enc key input))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how it&amp;#8217;s almost identical to our original function that tests a single instance of the property.  The only change was moving the arguments that we want to vary into a lambda function, and handing it off to the &lt;code&gt;quickCheck&lt;/code&gt; function.  QuickCheck figures out how to generate the required arguments, pumps out arbitrary values, and tests them against the given property.&lt;/p&gt;
&lt;p&gt;Finally, we can run some tests on our cipher (using 32 cycles):&lt;/p&gt;
&lt;pre&gt;TEA&amp;gt; test_identity (xtea_encode 32) (xtea_decode 32)
OK, passed 100 tests.
&lt;/pre&gt;
&lt;p&gt;Let&amp;#8217;s deliberately cause the property to be invalidated by changing just the encoding function and re-running the test:&lt;/p&gt;
&lt;pre&gt;TEA&amp;gt; test_identity (xtea_encode 32) (xtea_decode 32)
Falsifiable, after 1 tests:
(3,4294967295)
(2,1,0,4294967295)
&lt;/pre&gt;
&lt;p&gt;Which tells us after the very first test, it was able to find an input block and key that make the property false.  Notice the numbers QuickCheck selected for its first test, they do not appear random, and in fact, QuickCheck doesn&amp;#8217;t necessarily generate random values for types.  In this case, it is starting with what look like typical corner cases (very large numbers, zero, small numbers).&lt;/p&gt;
&lt;p&gt;Note: QuickCheck isn&amp;#8217;t natively able to generate arbitrary values for the type Word32, so we had to tell it how to do so, with the following code, which maps arbitrary integers onto Word32 values (courtesy of a haskell-cafe post by Sebastian Sylvan):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;instance Arbitrary Word32 where
    arbitrary = do c &amp;lt;- arbitrary :: Gen Integer
		   return (fromIntegral c)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By making any user-defined type an instance of &lt;code&gt;Arbitrary&lt;/code&gt;, you can have QuickCheck automatically generate tests for properties that take that type as an argument.&lt;/p&gt;
&lt;p&gt;xUnit tests definitely have their place.  Some ciphers have sets of test vectors for instance, which exploit very specific corner cases in the algorithm.  But for testing general properties of code, the quickcheck style can be very intuitive.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Pre-signing URIs for expiration with S3</title>
   <link href="http://gregheartsfield.com/2007/09/30/presign-uris-for-expiration-with-s3.html"/>
   <updated>2007-09-30T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/09/30/presign-uris-for-expiration-with-s3</id>
   <content type="html">&lt;h1&gt;Pre-signing URIs for expiration with S3&lt;/h1&gt;
&lt;p&gt;This is a neat feature of S3 that I wasn&amp;#8217;t aware of until after digging through the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; docs.  You can generate  URIs to S3 resources, which expire after a given time.  It&amp;#8217;s in the latest release of &lt;a href=&quot;/hS3/doc&quot;&gt;hS3&lt;/a&gt;, which includes &lt;a href=&quot;/hS3/examples/preSignedRequest.hs&quot;&gt;an example&lt;/a&gt; for generating a &lt;span class=&quot;caps&quot;&gt;URI&lt;/span&gt; valid for a certain number of seconds in the future.&lt;/p&gt;
&lt;p&gt;Instead of making an object public via an &lt;span class=&quot;caps&quot;&gt;ACL&lt;/span&gt;, you create a signature of the resource and an expiration date.  This signature is added as a query element to the &lt;span class=&quot;caps&quot;&gt;URI&lt;/span&gt;, and then given out to users.  The end result looks like the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;http://s3.amazonaws.com:80/hS3/LICENSE
?AWSAccessKeyId=09MD8BAR1GEXCERHT1R2
&amp;amp;Expires=1191208823
&amp;amp;Signature=bY6Luynk8mxbzaO8yv2Pcd3kd1U%3d
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which is generated by code like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;do uri &amp;lt;- publicUriForSeconds connection object exp_seconds
   putStrLn uri
&lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Haskell S3 Library</title>
   <link href="http://gregheartsfield.com/2007/09/23/haskell-s3-library.html"/>
   <updated>2007-09-23T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/09/23/haskell-s3-library</id>
   <content type="html">&lt;h1&gt;Haskell S3 Library&lt;/h1&gt;
&lt;p&gt;Amazon says:&lt;br /&gt;
&lt;i&gt;Amazon S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;After hacking away for most of my evenings this week, and the weekend, I&amp;#8217;ve got something that I don&amp;#8217;t feel terribly bad about releasing.  Still needs a good deal of work, but the fundamental operations work.  I&amp;#8217;m sure there will be some &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; changes in the future to accomodate some of the missing features.&lt;/p&gt;
&lt;p&gt;What works:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Bucket &lt;a href=&quot;http://gregheartsfield.com/hS3/examples/createBucket.hs&quot;&gt;creation&lt;/a&gt;, &lt;a href=&quot;http://gregheartsfield.com/hS3/examples/listBuckets.hs&quot;&gt;listing&lt;/a&gt;, and &lt;a href=&quot;http://gregheartsfield.com/hS3/examples/deleteBucket.hs&quot;&gt;deletion&lt;/a&gt;.&lt;/li&gt;
	&lt;li&gt;Object &lt;a href=&quot;http://gregheartsfield.com/hS3/examples/sendObject.hs&quot;&gt;creation&lt;/a&gt;, &lt;a href=&quot;http://gregheartsfield.com/hS3/examples/getObject.hs&quot;&gt;retrieval&lt;/a&gt;, &lt;a href=&quot;http://gregheartsfield.com/hS3/examples/listObjects.hs&quot;&gt;searching&lt;/a&gt;, and &lt;a href=&quot;http://gregheartsfield.com/hS3/examples/deleteObject.hs&quot;&gt;deletion&lt;/a&gt;.&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://gregheartsfield.com/hS3/examples/sendObject.hs&quot;&gt;Setting ACLs&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Issues:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;It&amp;#8217;s slooow.  The &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; library doesn&amp;#8217;t use ByteStrings, so sending multi-megabyte files is painful.&lt;/li&gt;
	&lt;li&gt;Requires the absolute latest &lt;a href=&quot;http://haskell.org/crypto/&quot;&gt;Crypto&lt;/a&gt; and &lt;a href=&quot;http://haskell.org/http/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;&lt;/a&gt; libraries, since I submitted patches for &lt;span class=&quot;caps&quot;&gt;HMAC&lt;/span&gt;-SHA1, and &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;DELETE&lt;/span&gt; during the course of writing the library.&lt;/li&gt;
	&lt;li&gt;To use the more advanced features, such as ACLs, metadata, hierarchical listings, you&amp;#8217;ll probably have to spend some time with the &lt;a href=&quot;http://docs.amazonwebservices.com/AmazonS3/2006-03-01/&quot;&gt;Amazon docs&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Coming Soon:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Better documentation, examples, and test cases.&lt;/li&gt;
	&lt;li&gt;Other Amazon Web Services, in the order that they interest me (&lt;a href=&quot;http://aws.amazon.com/sqs&quot;&gt;&lt;span class=&quot;caps&quot;&gt;SQS&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;http://aws.amazon.com/ec2&quot;&gt;EC2&lt;/a&gt;, &lt;a href=&quot;http://aws.amazon.com/fps&quot;&gt;&lt;span class=&quot;caps&quot;&gt;FPS&lt;/span&gt;&lt;/a&gt;&amp;#8230;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&amp;#8217;s how you get started.  The latest version is available via darcs:&lt;/p&gt;
&lt;pre&gt;darcs get http://gregheartsfield.com/repos/hS3/&lt;/pre&gt;
&lt;p&gt;Or, get the official &lt;a href=&quot;http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hS3&quot;&gt;release&lt;/a&gt; from Hackage.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/repos/hS3/doc/&quot;&gt;Documentation here&lt;/a&gt;, the &lt;a href=&quot;http://gregheartsfield.com/repos/hS3/doc/api/Network-AWS-AWSConnection.html&quot;&gt;AWSConnection&lt;/a&gt;, &lt;a href=&quot;http://gregheartsfield.com/repos/hS3/doc/api/Network-AWS-S3Bucket.html&quot;&gt;S3Bucket&lt;/a&gt;, and &lt;a href=&quot;http://gregheartsfield.com/repos/hS3/doc/api/Network-AWS-S3Object.html&quot;&gt;S3Object&lt;/a&gt; modules are where you&amp;#8217;ll want to begin.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Haskell HMAC</title>
   <link href="http://gregheartsfield.com/2007/09/16/haskell-hmac.html"/>
   <updated>2007-09-16T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/09/16/haskell-hmac</id>
   <content type="html">&lt;h1&gt;Haskell &lt;span class=&quot;caps&quot;&gt;HMAC&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;A necessary part of the &lt;a href=&quot;http://en.wikipedia.org/wiki/Amazon_S3&quot;&gt;Amazon S3&lt;/a&gt; library I&amp;#8217;m planning on putting some effort into is &lt;a href=&quot;http://en.wikipedia.org/wiki/HMAC&quot;&gt;&lt;span class=&quot;caps&quot;&gt;HMAC&lt;/span&gt;&lt;/a&gt;-SHA1, for authentication.  It is missing from any public Haskell libraries I could find, so I put one together during my weekend in Austin.  Hopefully it will make a nice addition (after some cleanup) to the excellent &lt;a href=&quot;http://www.haskell.org/crypto/&quot;&gt;Crypto&lt;/a&gt; library, on which it depends for message digest functions.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;----------------------------------------------------------------------
-- |
-- Module      :  Data.HMAC
-- Copyright   :  (c) Greg Heartsfield 2007
-- License     :  BSD-style (see the file ReadMe.tex)
--
-- Implements HMAC (hashed message authentication code)
-- as defined in FIPS 198
-- &amp;lt;http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf&amp;gt;.
--
----------------------------------------------------------------------

module Data.HMAC(
   -- * Function Types
   hmac, hmac_sha1, hmac_md5,    sha1_hm,key_from_user,
   -- * Data Types
   HashMethod(HashMethod, digest, input_blocksize),
   ) where

import Data.Digest.SHA1 as SHA1
import Data.Digest.MD5 as MD5
import Data.Word(Word32)
import Data.Bits (shiftR, xor, bitSize, Bits)
import Codec.Utils (Octet)
import Debug.Trace

-- | HMAC works over any hash function, which is represented by
--   HashMethod.  A hash function, and input block size must
--   be specified.

data HashMethod =
    HashMethod { -- | An arbitrary hash function
                 digest :: [Octet] -&amp;gt; [Octet],
                -- | Bit size of an input block to the hash function
                 input_blocksize :: Int}

--Some useful digest functions for use with HMAC.

sha1_hm = HashMethod (w160_to_w8s . SHA1.hash) 512
md5_hm = HashMethod MD5.hash 512

-- | Compute an HMAC using SHA-1 as the underlying hash function.

hmac_sha1 :: [Octet] -- ^ Secret key
          -&amp;gt; [Octet] -- ^ Message text
          -&amp;gt; [Octet] -- ^ Resulting HMAC-SHA1 value
hmac_sha1 = hmac sha1_hm

-- | Compute an HMAC using MD5 as the underlying hash function.

hmac_md5 :: [Octet] -- ^ Secret key
         -&amp;gt; [Octet] -- ^ Message text
         -&amp;gt; [Octet] -- ^ Resulting HMAC-SHA1 value
hmac_md5 = hmac md5_hm

w160_to_w8s :: Word160 -&amp;gt; [Octet]
w160_to_w8s w = concat $ map w32_to_w8s (w160_to_w32s w)

w160_to_w32s :: Word160 -&amp;gt; [Word32]
w160_to_w32s (Word160 a b c d e) = a : b : c : d : e : []

w32_to_w8s :: Word32 -&amp;gt; [Octet]
w32_to_w8s a = (fromIntegral (shiftR a 24)) :
               (fromIntegral (shiftR a 16)) :
               (fromIntegral (shiftR a 8)) :
               (fromIntegral a) : []

-- | Generalized function for creating HMACs on a specified
--   hash function.

hmac :: HashMethod -- ^ Hash function and associated block size
        -&amp;gt; [Octet] -- ^ Secret key
        -&amp;gt; [Octet] -- ^ Message text
        -&amp;gt; [Octet] -- ^ Resulting HMAC value
hmac h uk m = hash (opad ++ (hash (ipad ++ m)))
    where hash = digest h
          (opad, ipad) = process_pads key
                           (make_start_pad bs opad_pattern)
                           (make_start_pad bs ipad_pattern)
          bs = input_blocksize h
          key = key_from_user h uk

-- Create a key of the proper size from the user-supplied key.
-- Keys greater than blocksize get hashed and returned.
-- Keys same as blocksize are used as is.
-- Keys shorter than blocksize are padding with zeros.

key_from_user :: HashMethod -&amp;gt; [Octet] -&amp;gt; [Octet]
key_from_user h uk =
    case (compare (bitcount uk) (input_blocksize h)) of
      GT -&amp;gt; fill_key ((digest h) uk)
      LT -&amp;gt; fill_key uk
      EQ -&amp;gt; uk
    where fill_key kd =
              kd ++ (take (((input_blocksize h) - (bitcount kd)) `div` 8)
                     (repeat 0x0))

-- Create the inner/outer pad values by XOR'ing with the key.

process_pads :: [Octet] -- Key
             -&amp;gt; [Octet] -- opad
             -&amp;gt; [Octet] -- ipad
             -&amp;gt; ([Octet], [Octet]) -- new opad, new ipad
process_pads ks os is =
    unzip $ zipWith3 (\k o i -&amp;gt; (k `xor` o, k `xor` i)) ks os is

-- Create padding values for a hash of a given bit size.

make_start_pad :: Int -&amp;gt; Octet -&amp;gt; [Octet]
make_start_pad size pad = take (size `div` (bitSize pad)) $ repeat pad

-- Padding constants, per the spec.

opad_pattern = 0x5c :: Octet
ipad_pattern = 0x36 :: Octet

-- Bit count of byte array.

bitcount :: [Octet] -&amp;gt; Int
bitcount k = (length k) * (bitSize (head k))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;edited to fix bugs and incorporate suggestions from comments&lt;/i&gt;&lt;br /&gt;
And&amp;#8230; &lt;a href=&quot;http://gregheartsfield.com/code/HMACTest.hs&quot;&gt;test cases&lt;/a&gt;&lt;br /&gt;
This is now incorporated into the latest version of the &lt;a href=&quot;http://www.haskell.org/crypto/&quot;&gt;Haskell crypto library&lt;/a&gt; (later than 4.0.3).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Show and Destroy Whitespace</title>
   <link href="http://gregheartsfield.com/2007/08/22/show-and-destroy-trailing-whitespace.html"/>
   <updated>2007-08-22T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/08/22/show-and-destroy-trailing-whitespace</id>
   <content type="html">&lt;h1&gt;Show and Destroy Whitespace&lt;/h1&gt;
&lt;p&gt;Unnecessary whitespace at the end of a line is my pet peeve, especially since darcs makes it so obvious at record time.  This is a pretty straightforward way to highlight these characters in any emacs buffer containing haskell code (use a hook like &lt;code&gt;first-change-hook&lt;/code&gt; if you want it to show up everywhere). Add the following to &lt;code&gt;.emacs&lt;/code&gt; and enjoy:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;(add-hook 'haskell-mode-hook
          '(lambda ()
             (setq show-trailing-whitespace t)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And don&amp;#8217;t forget &lt;code&gt;M-x delete-trailing-whitespace&lt;/code&gt; for automatic cleanup.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Uncurry</title>
   <link href="http://gregheartsfield.com/2007/07/05/uncurry.html"/>
   <updated>2007-07-05T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/07/05/uncurry</id>
   <content type="html">&lt;h1&gt;Uncurry&lt;/h1&gt;
&lt;p&gt;My &lt;a href=&quot;/2007/07/05/currying-versus-partial-application/&quot;&gt;last post&lt;/a&gt; had me thinking of some of the ways the function &lt;code&gt;uncurry&lt;/code&gt; is useful in Haskell. Let&amp;#8217;s first look at the type:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uncurry :: (a -&amp;gt; b -&amp;gt; c) -&amp;gt; (a, b) -&amp;gt; c&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Usually the results are obvious, transforming a given function by simply taking a couple of its arguments and replacing them with a single tuple argument.  But sometimes we can get something a bit more interesting:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;code&gt;uncurry (flip (,)) :: (a, b) -&amp;gt; (b, a)&lt;/code&gt; &amp;#8212; swap tuple elements around.&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;uncurry (flip (,)) (&quot;hello&quot;, &quot;world&quot;) ==&amp;gt; (&quot;world&quot;, &quot;hello&quot;)&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;uncurry ($) :: (a -&amp;gt; b, a) -&amp;gt; b&lt;/code&gt; &amp;#8212; function application within a tuple; second element applied to the first.&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;uncurry ($) ((+3), 4) ==&amp;gt; 7&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;uncurry const :: (a, b) -&amp;gt; a&lt;/code&gt; &amp;#8212; oops! we&amp;#8217;ve un-optimized ourselves back to the equivalent of &lt;code&gt;fst&lt;/code&gt;.&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;uncurry (flip const) :: (a, b) -&amp;gt; b&lt;/code&gt; &amp;#8212; and we can duplicate &lt;code&gt;snd&lt;/code&gt; by adding a flip.&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;map . uncurry :: (a -&amp;gt; b -&amp;gt; c) -&amp;gt; [(a, b)] -&amp;gt; [c]&lt;/code&gt; &amp;#8212; apply function over a list, getting arguments from within tuples in that list.&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;(map . uncurry) (+) [(1,1), (2,3), (5,10)] ==&amp;gt; [2,5,15]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So basically, &lt;code&gt;uncurry&lt;/code&gt; is a heavy-duty higher-order tool for working with tuples.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Currying Versus Partial Application</title>
   <link href="http://gregheartsfield.com/2007/07/05/currying-versus-partial-application.html"/>
   <updated>2007-07-05T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/07/05/currying-versus-partial-application</id>
   <content type="html">&lt;h1&gt;Currying Versus Partial Application&lt;/h1&gt;
&lt;p&gt;I&amp;#8217;ve noticed what seems to be a growing &lt;a href=&quot;http://www.python.org/dev/peps/pep-0309/#feedback-from-comp-lang-python-and-python-dev&quot;&gt;inclination&lt;/a&gt; to &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.cafe/24880/&quot;&gt;confuse&lt;/a&gt; currying functions, with partial function application.  The recently updated HaskellWiki pages (&lt;a href=&quot;http://haskell.org/haskellwiki/Currying&quot;&gt;currying&lt;/a&gt;, &lt;a href=&quot;http://haskell.org/haskellwiki/Partial_application&quot;&gt;partial application&lt;/a&gt;) are helpful in understanding the difference.&lt;/p&gt;
&lt;p&gt;I think the reason for this confusion is that functions in Haskell are curried by default, making it rare for someone learning Haskell to need to explicitly curry a function.  Explicit partial application is pervasive though, so the error is almost always using &amp;#8220;curry&amp;#8221; when you mean &amp;#8220;partial application.&amp;#8221;&lt;/p&gt;
&lt;p&gt;Quoting &lt;a href=&quot;http://haskell.org/haskellwiki/Currying&quot;&gt;HaskellWiki&lt;/a&gt;: &amp;#8220;Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.&amp;#8221;&lt;/p&gt;
&lt;p&gt;Since Haskell functions are already curried, we simulate standard non-curried functions by using tuple arguments.  A non-curried addition function would look like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;add :: (Int, Int) -&amp;gt; Int
add i = (fst i) + (snd i)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can&amp;#8217;t immediately partially apply a single argument of &amp;#8220;1&amp;#8221; to this, in order to get an &amp;#8220;increment&amp;#8221; function.  Using the &lt;code&gt;curry&lt;/code&gt; function from Standard Prelude, we can transform this (poorly written) function, and immediately take advantage of partial application by giving it an argument (1):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;increment :: Int -&amp;gt; Int
increment = curry add 1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The HaskellWiki Currying page has a couple quick puzzles at the end, which I&amp;#8217;ve solved in a ghci transcript below:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Simplify &lt;code&gt;curry id&lt;/code&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;Prelude&amp;gt; :t curry
curry :: ((a, b) -&amp;gt; c) -&amp;gt; a -&amp;gt; b -&amp;gt; c
Prelude&amp;gt; :t curry id
curry id :: a -&amp;gt; b -&amp;gt; (a, b)
Prelude&amp;gt; :t (,)
(,) :: a -&amp;gt; b -&amp;gt; (a, b)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
	&lt;li&gt;Write the function &lt;code&gt;\(x,y) -&amp;gt; (y,x)&lt;/code&gt; without lambda and with only Prelude functions&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;Prelude&amp;gt; :t \\(x,y) -&amp;gt; (y,x)
\(x,y) -&amp;gt; (y,x) :: (t, t1) -&amp;gt; (t1, t)
Prelude&amp;gt; :t flip
flip :: (a -&amp;gt; b -&amp;gt; c) -&amp;gt; b -&amp;gt; a -&amp;gt; c
Prelude&amp;gt; :t uncurry
uncurry :: (a -&amp;gt; b -&amp;gt; c) -&amp;gt; (a, b) -&amp;gt; c
Prelude&amp;gt; let switch = uncurry (flip (,))
Prelude&amp;gt; switch (1,2)
(2,1)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>Grad School</title>
   <link href="http://gregheartsfield.com/2007/06/21/grad-school.html"/>
   <updated>2007-06-21T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/06/21/grad-school</id>
   <content type="html">&lt;h1&gt;Grad School&lt;/h1&gt;
&lt;p&gt;Some exciting news for me, I&amp;#8217;ve been accepted into the Software Engineering Master&amp;#8217;s program at UT Austin.  I&amp;#8217;ll be starting in August.&lt;/p&gt;
&lt;p&gt;&amp;#8220;There is plenty of interesting work to be done in applying software methodologies to functional languages, and it is disappointing that there is virtually no effort in this area.&amp;#8221;&lt;br /&gt;
&amp;#8212;Philip Wadler (&lt;a href=&quot;http://portal.acm.org/citation.cfm?id=286387&amp;amp;coll=ACM&amp;amp;dl=ACM&amp;amp;CFID=26297794&amp;amp;CFTOKEN=30769124&quot;&gt;Why no one uses functional languages&lt;/a&gt;)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Fractal-hs</title>
   <link href="http://gregheartsfield.com/2007/04/21/fractal-hs.html"/>
   <updated>2007-04-21T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2007/04/21/fractal-hs</id>
   <content type="html">&lt;h1&gt;Fractal-hs&lt;/h1&gt;
&lt;p&gt;I was reminded last week by &lt;a href=&quot;http://adam.pirkle.org/&quot;&gt;Adam&lt;/a&gt; about a project I had worked on with him in calculus, creating fractal images using &lt;a href=&quot;http://en.wikipedia.org/wiki/Newton's_method&quot;&gt;Newton&amp;#8217;s method&lt;/a&gt; in C++, and picked that as my weekend haskell project.  Although this time starting with the Mandelbrot set for variety.  My real motivation was to possibly start work on an image library, but &lt;a href=&quot;http://hackage.haskell.org/cgi-bin/hackage-scripts/package/gd-3000.0.0&quot;&gt;GD bindings&lt;/a&gt; had already been created, so that was unnecessary.  I had to expose a function to set individual pixels to the bindings, &lt;del&gt;so if you actually want to run the code below, you&amp;#8217;ll need to either pull haskell-gd from &lt;a href=&quot;http://www.scsibug.com/haskell-gd/&quot;&gt;my copy&lt;/a&gt;, or just apply &lt;a href=&quot;http://www.scsibug.com/gd_api_additions.patch&quot;&gt;these patches&lt;/a&gt; to the official release from hackage.&lt;/del&gt; &lt;em&gt;(GD now includes my patches)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/fractal-hs/&quot;&gt;Full source here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s fortunate I enjoy mundane programming tasks, because the &amp;#8220;real&amp;#8221; work of calculating the Mandelbrot set took only a few minutes.  Adding the necessary setPixel function to GD, and even transforming coordinates from an image to the complex plane took far more time.  It was much easier to translate the actual mathematical definition of the set, rather than to try and convert from optimized code that used mutable variables.&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;
-- recursive mandelbrot
mandelbrot :: Complex Double -- Coordinate to test
           -&amp;gt; Complex Double -- Iterating Z value
           -&amp;gt; Int -- Current iteration count
           -&amp;gt; Int -- Iterations before diverging
mandelbrot c z iter
    | iter &amp;gt; maxIter = 0
    | otherwise = let z' = z^2 + c in
                  if magnitude(z') &amp;gt; 2
                  then iter
                  else mandelbrot c z' (iter+1)
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;It needs to be reworked with a high-precision floating point library to make more interesting images, but Doubles are good enough for the following (Mandelbrot, Julia Set, Newton&amp;#8217;s Method, Burning Ship):&lt;/p&gt;
&lt;div class=&quot;imagelist&quot;&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/fractal-hs/mandelbrot_600.png&quot; title=&quot;mandelbrot fractal generated with haskell&quot;&gt;&lt;img alt=&quot;Mandelbrot fractal&quot; src=&quot;http://gregheartsfield.com/fractal-hs/mandelbrot_150.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/fractal-hs/juliaset_600.png&quot; title=&quot;Julia set fractal generated with haskell&quot;&gt;&lt;img alt=&quot;julia set fractal&quot; src=&quot;http://gregheartsfield.com/fractal-hs/juliaset_150.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/fractal-hs/juliaset2_600.png&quot; title=&quot;Julia set fractal generated with haskell&quot;&gt;&lt;img alt=&quot;julia set fractal&quot; src=&quot;http://gregheartsfield.com/fractal-hs/juliaset2_150.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/fractal-hs/newtonm_600.png&quot; title=&quot;Newton's method fractal generated with haskell&quot;&gt;&lt;img alt=&quot;Newton's method fractal&quot; src=&quot;http://gregheartsfield.com/fractal-hs/newtonm_150.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/fractal-hs/burning_ship_600.png&quot; title=&quot;Burning ship fractal generated with haskell&quot;&gt;&lt;img alt=&quot;Burning ship fractal&quot; src=&quot;http://gregheartsfield.com/fractal-hs/burning_ship_150.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;</content>
 </entry>
 
 <entry>
   <title>A Simple Game with StateT</title>
   <link href="http://gregheartsfield.com/2006/11/28/a-simple-game-with-statet.html"/>
   <updated>2006-11-28T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2006/11/28/a-simple-game-with-statet</id>
   <content type="html">&lt;h1&gt;A Simple Game with StateT&lt;/h1&gt;
&lt;p&gt;I think I first &amp;#8220;got&amp;#8221; monad transformers after reading the wonderful article  &lt;a href=&quot;http://sigfpe.blogspot.com/2006/05/grok-haskell-monad-transformers.html&quot;&gt;Grok Haskell Monad Transformers&lt;/a&gt; by Dan Piponi.  But it wasn&amp;#8217;t without some frustration, and I think that was due to the use of arbitrary functions (test1, test2, go1, go2&amp;#8230;).  I found it hard to wrap my mind around the bigger issue of the transformer mechanics when I didn&amp;#8217;t have a firm grasp on what the purpose of these individual functions were.&lt;/p&gt;
&lt;p&gt;So, I wrote the ubiquitous &amp;#8220;Guess a number&amp;#8221; program, that many probably first wrote in &lt;span class=&quot;caps&quot;&gt;BASIC&lt;/span&gt;, using Haskell and a State Transformer instead.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/code/guess_a_number.hs&quot;&gt;guess_a_number.hs&lt;/a&gt;, or &lt;a href=&quot;http://gregheartsfield.com/code/guess_a_number_error.hs&quot;&gt;improved version&lt;/a&gt; with error handling (submitted by tphyahoo@gmail.com).&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;
module Main where
import System.Random
import Control.Monad.State

main = do answer &amp;lt;- getStdRandom (randomR (1,100)) -- think of a number
          putStrLn &quot;I'm thinking of a number between 1 and 100, can you guess it?&quot;
          guesses &amp;lt;- execStateT (guessingSession answer) 0 
          putStrLn $ &quot;Success in &quot; ++ (show guesses) ++ &quot; tries.&quot;

guessSession :: Int -&amp;gt; StateT Int IO ()
guessSession answer = 
    do gs &amp;lt;- lift getLine    -- get guess from user
       let g = read gs       -- convert to number
       modify (+1)           -- increment number of guesses
       case compare g answer of
              LT -&amp;gt; do lift $ putStrLn &quot;Too low&quot;
                       guessSession answer
              GT -&amp;gt; do lift $ putStrLn &quot;Too high&quot;
                       guessSession answer
              EQ -&amp;gt; lift $ putStrLn &quot;Got it!&quot;
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;It illustrates a few things rather nicely I think, especially given it is only 20 lines long.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Moving a function argument into the State monad (It is easy to see how guessSession might take two arguments, the answer, and number of guesses made).&lt;/li&gt;
	&lt;li&gt;Incorporating state and IO into the same function, by use of StateT.&lt;/li&gt;
	&lt;li&gt;Recursive StateT/IO &amp;#8220;Just Works&amp;#8221;&lt;/li&gt;
	&lt;li&gt;Highly imperative, state changing, I/O code can be written in Haskell in much the same way as other languages.  That is to say, it&amp;#8217;s not the pain you might expect given Haskell&amp;#8217;s goal of purity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Much thanks to &lt;a href=&quot;http://www.cse.unsw.edu.au/~dons/&quot;&gt;Don&lt;/a&gt; and Cale from &lt;a href=&quot;http://www.haskell.org/haskellwiki/IRC_channel&quot;&gt;#haskell&lt;/a&gt; for showing me how to write a proper case statement!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Calando</title>
   <link href="http://gregheartsfield.com/2006/10/08/calando.html"/>
   <updated>2006-10-08T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2006/10/08/calando</id>
   <content type="html">&lt;h1&gt;Calando&lt;/h1&gt;
&lt;p&gt;I often fall asleep listening to music, but don&amp;#8217;t want it playing full volume all night.  So, Calando is a small OS X application I wrote to gradually fade my volume down to some preset level.  Universal Binary, Requires OS X 10.4.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://gregheartsfield.com/Calando/Calando_v1.1.1.dmg&quot;&gt;Download Calando 1.1.1&lt;/a&gt;. (&lt;a href=&quot;http://gregheartsfield.com/Calando/Calando_v1.1.1.dmg.asc&quot;&gt;&lt;span class=&quot;caps&quot;&gt;GPG&lt;/span&gt; Signature&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://gregheartsfield.com/Calando/Calando-screen.png&quot; alt=&quot;Calando interface screenshot&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Related Software:&lt;/strong&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.embraceware.com/software/awaken/&quot;&gt;Awaken&lt;/a&gt; is an alarm clock application that can do volume fading as well.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>V-22 Touch-and-Go</title>
   <link href="http://gregheartsfield.com/2006/02/25/v22-touch-and-go.html"/>
   <updated>2006-02-25T00:00:00+00:00</updated>
   <id>http://gregheartsfield.com/2006/02/25/v22-touch-and-go</id>
   <content type="html">&lt;h1&gt;V-22 Touch-and-Go&lt;/h1&gt;
&lt;p&gt;3 Marine V-22&amp;#8217;s flew into Alliance Airport Friday evening, and spent Saturday doing flight tests.  Bell employees and their families were allowed to come out in the chilly, drizzly weather to observe.  &lt;a href=&quot;http://gregheartsfield.com/galleries/touchandgo/&quot;&gt;More photos.&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;imagelist&quot;&gt;&lt;a title=&quot;Engine inspection&quot; href=&quot;http://gregheartsfield.com/galleries/touchandgo/pictures/picture-12.jpg&quot;&gt;&lt;img alt=&quot;Engine inspection&quot; src=&quot;http://gregheartsfield.com/galleries/touchandgo/thumbnails/thumb-12.jpg&quot; /&gt;&lt;/a&gt; &lt;a title=&quot;V-22 takeoff&quot; href=&quot;http://gregheartsfield.com/galleries/touchandgo/pictures/picture-21.jpg&quot;&gt;&lt;img alt=&quot;V-22 takeoff&quot; src=&quot;http://gregheartsfield.com/galleries/touchandgo/thumbnails/thumb-21.jpg&quot; /&gt;&lt;/a&gt;  &lt;a title=&quot;V-22 takeoff&quot; href=&quot;http://gregheartsfield.com/galleries/touchandgo/pictures/picture-24.jpg&quot;&gt;&lt;img alt=&quot;V-22 takeoff&quot; src=&quot;http://gregheartsfield.com/galleries/touchandgo/thumbnails/thumb-24.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;</content>
 </entry>
 
 
</feed>
