Archive for the ‘Haskell’ Category


Haskell + lhs2TeX + LaTeX beamer

May 16, 2008

For a presentation I’m currently building I want to walk the public through a bit of Haskell code. Because I want to make sure my code is runnable too, I decided to write it as Literal Haskell and then use lhs2TeX to convert it to very pretty LaTeX which I use to make a presention PDF with LaTeX Beamer.

This is pretty easy for the common use case (a (research) paper) about Haskell), but I had some more requirements.

  • The code is split over several files
  • I want to mix code fragments of those files
  • I want effects, like uncovering code line by line

It took me a couple of hours to figure out a sensible approach, which I describe here. I’m sure there are more ways to do it, and I’d love to hear them!

Since our lhs2TeX activities will be spread over multiple files, we’ll want to make a simple formatting file to share. Let’s call it ‘talk.fmt’

%include polycode.fmt

% put formatting rules here

Then we set up our main LaTeX file. Because we might want to use some code examples in there that are not in the files, let’s make it an lhs-file: ‘talk.lhs’. In it’s preamble we define a couple of new LaTeX commands that are going to help us with making slides but referencing them in an arbitrary order.


%include talk.fmt








First, we have ‘\ignore’ to hide stuff from LaTeX that is only useful in the code and we don’t want to have in our presentation.

The ‘\defslide’ command takes two arguments, a name which is used as a label to reference to and the contents for the slide. The ‘<hidden>’ makes sure that these slides will not be rendered. In this case ‘hidden’ is not a special argument, just a non-existing ‘mode’.

To use a defined slide, we use ‘\slide’, which uses ‘\againslide’ to draw the slide, with all possible subframes (‘<1->’).

In the document body we include all the files which contain slides. Note that this should be .tex file, generated from our .lhs file. For example ‘Code.lhs’ could look like this.

%include talk.fmt

module Code where

>                     example :: IO ()
> {-"\uncover<2->{"-} example = putStrLn "Hello!" {-"}"-}

\frametitle{Just a normal frame}
main :: IO ()
main = undefined

I don’t uncourage anybody to mix styles like this, it’s just an a example, okay? ūüėČ What you also see here is the way to uncover a piece of code line by line. If you want to learn more about LaTeX Beamer I suggest you consult the fine manual.

For easy use, we can make a Makefile to tie everything up. We also use latexmk, a simple tool that automagically processes the LaTeX file as many times as necessary, which saves a ton of time.

default: talk.pdf

talk.pdf : Code.tex

%.pdf : %.tex
    latexmk -pdf $<

%.tex : %.lhs talk.fmt
    lhs2TeX $< > $@

    latexmk -CA talk

If you want to try it for yourself just copy the above code to the mentioned filenames and run ‘make’ to get the pdf. (Provided you have all the necessary requirements.)

(I think I massaged all the code so that it gets through WordPress’ botching, but you’ll still need to replace the indenting spaces with tabs in the Makefile yourself.)

While in this example only one file with code is used, it easy to see that this way as many files as necessary can be used. As shown, the slides can be in arbitrary order and we can use nice LaTeX Beamer effects like uncovering. Hooray!


Beautiful timetables

November 11, 2007

Because I wanted a clear and compact way to display the timetable of the tram nearby, I created a small project. My goal was to have something more ‘visual’ than a list with numbers. I am interested in new ways to display information and what’s nice about it and why.

Preview of output

I think the result is something that has a bit of a learning curve and is therefore less suitable for ‘one-time’ usage. On the other hand, because it’s more of a picture and not a bunch of numbers I think it’s much easier to learn the timetable by heart for regular users.

For specification I wanted a small and simple language and preferably not start with writing a parser, so I decided to hitch a ride with Haskell first.

I’d love to hear your opinion, both a first reaction and after experimenting with it.

¬Ľ Download it

(view readme or view haskell module)


Summing it up

February 24, 2007

Today, I had a list of integers, and I wanted to sum it up. I guessed that I should write a script in Ruby, to make sure it would get done within 2 minutes. So I fired up my editor, and wrote the following script:

sum = 0 ;
$stdin.each do |line|
puts sum

Pretty straightforward. I’d rather had written the script in Haskell, but I figured I needed to get things done. This evening I tried the same thing in Haskell, but to I was surprised: it was even quicker and more straightforward than the Ruby version! Here it is:

main = interact $ show . sum . map read . lines

If you’d like more of these tiny tools, see Haskell Unix Tools.


My Evolution as a Haskell Programmer: Factorial with Arrows

February 19, 2007

A couple of years ago Fritz Ruehr wrote a great article about The Evolution of a Haskell Programmer. It was written in 2001, when I was still in high school, some years away from being introduced to the beautiful Haskell programming language.

Currently, I’m following a course on Advanced Functional Programming at the Utrecht University. One of the advanced topics tought in that course is about Arrows. Arrows are an abstraction of computations, just like Monads, but even more general and pretty nice to define stream functions with (useful for defining logical circuits, for example). I’m still trying to get my head fully around it, but so far I understand it enough to add a new part to the evolution (a missing link!): the factorial function defined with arrows.

I will present the code below and sparsely comment on it. Most of the code is actually from the Advanced Functional Programming lecture.

-- We need this library 
import Control.Arrow        

-- Define a type for stream functions, from one list to another 
newtype SF a b = SF { runSF :: [a] -> [b] }      

-- Define the Arrow functions for the SF type.  Once you've 
-- defined these three, you'll get a lot of other functions for 
-- free. 
instance Arrow SF where 
  arr f = SF (map f) 
  SF f >>> SF g = SF (f >>> g) 
  first (SF f) = SF (unzip >>> first f >>> uncurry zip)      

-- arr   lifts a function to the arrow domain 
-- >>>   combines (connects) two arrow functions 
-- first only applies the function to the first part from a 
--       tuple      

-- See the for more 
-- information and some nice graphics, which will help 
-- understanding.

Okay, so far, so good. By defining the ‘first’ and ‘>>>’ functions, we get an ‘&&&’ combinator for free, which we will use in our final definition. For example: ‘f &&& g’ can be understood as accepting and input x, then applying both f and g to it, resulting in the output of a tuple of the results. In short, the type of ‘&&&’ is ‘Arrow arr => arr a b -> arr a c -> arr a (b, c)’.

-- The type of loop is (ArrowLoop arr) => arr (a, c) (b, c) -> 
-- arr a b.  A loop makes a function from 'a' to 'b', while 
-- 'looping' a 'c'. 
instance ArrowLoop SF where 
  loop (SF f) = SF $ \\as -> 
    let (bs,cs) = unzip (f (zip as (stream cs))) 
    in bs      

-- Note in the definition that cs is defined recursive(!)  This 
-- is really where the usefulness of Haskell's lazyness kicks 
-- in.  Although, because zip and unzip are both strict, we 
-- have to help it a little, by defining a helper function 
-- stream.      

stream :: [a] -> [a] 
stream ~(x:xs) = x:stream xs      

-- The ~ in the pattern makes the pattern matching irrefutable, 
-- which means that the pattern will not be interpreted but 
-- assumed to be correct.

Well, with that out of the way we can get to the good stuff. We just need two simple helpers to modify the stream.

-- 'Delays' a stream by prepending an element.  This can be 
-- used to initialize a stream _before_ the input is read. 
-- We're going to need this to use that nasty loop above. 
-- Remember, the c was defined recursive, without 
-- initialization. 
delay :: a -> SF a a 
delay x = SF (x:)      

-- Takes an input and muliplies it.  It 'simply' lifts an 
-- uncurried (*) to the arrow domain. 
-- Example (try it yourself): mul (6,7) = 42 
mul :: Arrow arr => arr (Integer, Integer) Integer 
mul = arr (uncurry (*))

Okay, now, the basic idea is to supply a stream function with the list of integers and then all we have to do is multiply the result of the first item with the second and multiply that result with the third, etc. Aha, that’s easy, we can just use the loop for that. The only thing we need is in initialization value to start the multiplying with. For this we use the delay. Schematicly, our plan looks like the following picture.

Factorial Arrows

You can clearly see how the loop works while the input and the output of the whole is still a single stream. The dot in the image where the output of ‘mul’ splits is actually the ‘&&&’ combinator. The code then looks like this.

facSF :: SF Integer Integer 
facSF = loop (mul >>> (arr id &&& delay 1))

Pretty clean, eh? Only thing left is a nice wrapper to run this thing.

-- Using Integers so that for large numbers (and factorials 
-- tend to get really large fast) it still produces nice 
-- output.  Try 'fac 420' for example. 
fac :: Integer -> Integer 
fac x = runSF facSF [1..x] !! fromInteger (x - 1)

This concludes our fun with Arrows. Viva la evolucion!


Haskell is fun!

January 8, 2007

Lately, I’ve been doing a lot of Haskell. I must warn you, I always have a Programming Language Of The Month, but now I feel as if it’s different. Haskell is a programming language where you get the feeling that a lot of things are just perfect.

First off, the way the type-system works, many, many, many errors that you’d normally make in your imperative language are simply impossible to make. It’s very strict, and although ruby is me second language of choice, this is also a really good thing. For example, I recently read that it’s virtually impossible to generate invalid XML. And that’s not because the author is some crazy god-like person, it’s just an intrinsic feature of the data-types in Haskell.

Furthermore, Haskell gives you a really good intellectual challenge. You really spend more time thinking than typing. The information density in your code is quite high compared to a lot of other languages I work in.

Also, having no side-effects is a Good Thing. The first time that I worked in Haskell, I was really disappointed that IO was so hard to do, with all that incomprehensible Monad-stuff and more. As it turned out, it’s really not that hard, and debugging will get so much easier. You just don’t need logger statements all around your code.

Ruby is my second language of choice, and that’s because you don’t have to type that much.¬†But¬†one¬†of¬†the¬†powers¬†of¬†ruby¬†is¬†the¬†meta-programming,¬†which¬†is¬†a¬†bit¬†of¬†a¬†hack. But less typing means you’ll get stuff done sooner and it’s harder to make errors. In Java, you’ll have to type a lot (even with Eclipse), because of the type-system (pun intended). In Haskell, the type-system is really smart, and you don’t have to type a lot of trivial things. The compiler will automatically deduce the types of functions for you. So you’ll end up having the advantages of a static typed language, but better, and having the advantages of dynamic languages, less code.

Finally, there are a lot of really smart people in the Haskell community. There’s lots of really advanced stuff being done, but most people are also very good writers. Just google for “Simon Peyton Jones“, and read some of his papers. Most are explaining beautiful, advanced things, and he has a really clear writing style. Maybe he’s one of the best, but there’s lots of other people that write excellent programs, articles and other stuff.

So, if you feel at least the tiniest bit excitement, I can recommend that you download a haskell compiler, do some tutorials, don’t give up and get just as excited as I am! And don’t forget: the best way to get to know a language is to program in it!