Understanding Monads in Haskell

Update: I started reading up on monads again recently, and here is the best explanation I’ve found.  Also, the update caused my Beckman links to vanish unfortunately…

I’ve been digging through the Haskell resources I posted earlier (and some other ones ), and I must say that treatment of  monads has been pretty poor.  There are all kinds of cognitive metaphors out there, the least clear and most ridiculous of which has to be the space station analogy.  However, I think after seeing a few key resources the picture is starting to come together.

First is a great pair of videos from Microsoft’s Brian Beckman.  It’s geared toward concurrency, but he talks about Haskell’s State monad as a way of automatically mutating an explicit (vs implicit) state.  Doing this non-monadically would mean passing in the current state into each function, but with monads, you can construct larger operations by composing the monads together, and the monads will handle all the work of passing around and updating the state.

The videos are pretty long, but quite interesting and really help to introduce the way a particular monad (the State monad) handles compositing.

The next resource is Monads as a computation. This page generalizes the concepts introduced in the videos. Essentially, monads a way of describing how computations are chained together to form more complex computations. The logic of what happens between computations (such as pre- and post-processing) before moving on to the next step is built into the monad (specifically the return and >>= functions).

Lastly, Learn You a Haskell (which, incidentally, I’m beginning to like more than RWH as the concepts get more sophisticated) has a chapter on monads, which goes in to how Maybe and List monads handle their own processing between steps. For instance, if at any point in a Maybe monad chain Nothing is the result, then that Nothing is propagated to the end of the chain (canceling later computations on the way).

From my understanding of monads (so far), they are a convenient way to automatically handle any book keeping that needs to be done between successive operations (Writer monad, which can be used to log all operations performed) or for controlling the flow of operations in a standardized way (aborting operations with Maybe, handling list flattening with List).

Resources for learning about Haskell

As my last post alluded, I’ve started to dive into the world of functional languages.  I chose to go with Haskell over Scala since Haskell is allegedly the most “pure” of the functional languages in use, and I am of the mindset that I should go big or go home.

I’ve started to read a lot about Haskell lately, and do a few toy examples to try to solidify the concepts more.  In my reading, I have come across some good resources.

The first and I think best resource I have found is Real World Haskell.  It goes through Haskell concept quickly with an appropriate amount of dumbing down.

Similar to RWH is Learn You a Haskell for Great Good.  If you find the style of RWH a little too formal, then Learn You a Haskell is for you.  I thought Learn You a Haskell’s style a little charming at first but then rather annoying, so I recommend RWH.

I am still learning about monads, but I think this resource is decent What the hell are monads?.

And then, if you want to know more of the pros and cons of using functional languages (Haskell in particular) I would direct you to a slashdot discussion on the topic as well as this rather recent blog post.

Component Architectures vs Class Hierarchies

After putting Sprite Clipper into a holding pattern, I started looking for a new nerd hobby.  I figured I would naturally progress into using slick2d, the Java game architecture that first set me off to make SC.  I didn’t have much of an idea of what kind of game I wanted to make, so I thought I would investigate different architectures to get some inspiration.

That’s when I ran across component video game architectures, which totally eschew object-oriented programming concepts.  This was completely new to me.  The gospel of OOP had been preached to me in nearly every context I came across it.  But, after reading some articles about the component paradigm, I began to see the drawbacks of class hierarchies.

The main goal of component architectures is to reduce coupling between various parts of the program as much as possible.  Let’s say you have an extensive class hierarchy, but you want to add a new feature to only certain classes in the inheritance tree (call them classes C) but not in some other classes (call them classes NC).  If the youngest common ancestor of all classes of C (call it superclass S) is also an ancestor of any class in NC, then you’re kind of SOL if you want to use inheritance.  You can’t give superclass S the new feature without also giving it to some class in NC.  Essentially, the class hierarchy couples different classes together, reducing your ability to selectively update parts of the tree in a robust manner. (I say robust because you could always just copy and paste the feature into each class in C, but the CompSci gods would strike you down for your heresy.)

In component architectures, inheritance is avoided.  In fact, most classes are avoided altogether.  Instead, all objects are simply containers of components, which are self-contained contained behaviors.  Want an object to respond to user input?  Add a UserInput component to the object.  Want an object to be destructable?  Add the Destructable component.  Using components, the object can be completely customized without affecting other classes or other objects.

Since component architectures are used to manage the behavior of  individual objects, they present a different problem from class hierarchies – how do you manage all the objects in your program?  Well, one strategy is to define a scripting language that will emulate inheritance, kind of like the one described in this Powerpoint presentation.  The presentation gives a good overview of the advantages and disadvantages of component architectures, and was probably the most useful resource I came across while investigating this topic.

So are component architectures next on my list of things to try out?  Probably not, actually.  I came across another intriguing new concept – functional languages – that I think I’ll explore first.  Functional languages are even stranger to me than component architectures, and I’m just a sucker for a new experience.

Sprite Clipper v0.9 released

The first version of Sprite Clipper, one of the primary reasons I started this site, has been released!  I tried to break the program as best as I could, and if I found something wrong I would fix it.  Since I haven’t been able to break this version, I’m putting it out there for people to use and try out. No guarantees, of course!

Check it out here.

I’ve learned a great deal pursuing this project and this site.  Here’s a bit:

  • Java
  • Netbeans, Ant, Freemarker
  • git and Github
  • Setting up a domain and using WordPress
  • Blob detection algorithms
  • Packing algorithms
  • Slick2d library (for testing)

In fact, the reason I started Sprite Clipper was that I started playing with Slick2d, but found getting sprite assets (even ones just for testing) a real pain in the butt.  I’m one of those people who will expend huge amounts of effort to avoid doing tedious tasks.  Tediously cropping sprites is a pain.  Building entire programs to avoid doing that is actually a lot of fun.

Automatically including a file in your Java jar using Netbeans and Ant

First, a quick note of exposition.  If you’re eager just skip down to the header.

I started this site primarily because I started developing a Java program called Sprite Clipper that I wanted to share with other people.  I also knew it would be a learning experience (in more ways than I anticipated!), and I think maintaining a web presence outside of Facebook is desirable in its own right.

To track my program’s progress, I started to use git in conjunction with GitHub.  GitHub prominently displays a README file at the root of the project repository, which can be used for licensing info (BSD 3 clause in my case).  What I wanted to do was have this README file be automatically generated when I build the jar for my program using the same template text that I use for my new java classes, and also have the README included in the built jar.

Well, I fell pretty short of that goal.  I couldn’t find any information on how to invoke NetBeans’s templating engine, FreeMarker, from Ant without creating a stand-alone installation.  Since I had been working on this for a couple hours at this point, I decided to scale back my ambitions to simply copying a README file I made manually to the jar archive.  While it’s not a slick, sexy solution, it does accomplish what I wanted, albeit with less automation than I would have liked.

Copying a file into JAR archive

In your Netbeans project, go the file view on the left hand side and open up build.xml.  It should be at the top level of your directory.  Here you’ll see an XML file that has a lot of comments and only a little markup.  This is your Ant build script.  It’s kind of a like a makefile that’s specified in XML.

At the bottom of the file, insert the following just before the </project> tag at the end.

 <target name="-post-jar">
  <!-- Add the readme file to the jar archive -->
  <jar jarfile="${dist.jar}" update="true">
   <fileset dir="${basedir}" includes="README"/>
  </jar>
 </target>

What this does is add the README file located in ${basedir} (the project’s root) to the jar file right after it’s made.  You’ll need to modify the fileset dir and includes properties to reflect the location of your own file that you want to copy into the jar.