Haskell, DSL and Monad
More on Haskell DSL

Another DSL embedded in Haskell - a partial make replacement in less than 10 lines

My earlier post on Haskell described a small DSL for calendar apps. In this post, I will show a very simple DSL (written in just 6 lines of code) to replace (a small portion of) GNU make utility. Earlier, I had attempted the GNU make replacement in Haskell without the use of DSL concepts.

At a very basic level, GNU make allows one to describe how a particular goal (or target) is made (generated using a set of shell commands) from a set of its dependencies. In Haskell, I should be able to describe this relationship like this:

return "test.c" ->> ccompile >>- "test.o" ->> link >>- "test.exe"

In other words, test.exe is made from test.o, which in turn is made from test.c.
The plumbing code can be written in a few of lines like this:

import System.Cmd

dep ->> cmd = \target -> dep >>= (\xdep -> (cmd xdep target) >> return target)

tcmd >>- target = (tcmd target) >> return target

ccompile dep target = system("gcc -c " ++ dep ++ " -o " ++ target)

link dep target = system("gcc  " ++ dep ++ " -o " ++ target)

Notice, how the dataflow is made clear because of the notations used. In addition, the use of IO monad means you can keep on "linking" output of one stage to another. Compare and contrast this with the compiler based DSL using Ocaml described here.
For the sake of simplicity, I haven't added the code to check if the target is out of order with   
respect to its dependency. And only one dependency per target is currently supported. These features however, are not very hard to add. As usual, your feedback is highly welcome and is sincerely appreciated. You can download the code from here.

Comments

Eugene

Hi. My name is Eugene Gershin. I'd like to welcome you to Obadiah Shoher's blog, Samson Blinded: A Machiavellian Perspective on the Middle East Conflict.

Obadiah is a pen name of a politician. He writes extremely controversial articles about Israel, the Middle East politics, and terrorism.

Obadiah advocates political rationalism instead of moralizing. He is economic liberal and political conservative.

Google refused advertising our site and Amazon deleted reviews of Obadiah's book. Nevertheless, Obadiah’s is the largest Jewish personal blog, read by more than 100,000 people monthly. 210,000 people from 81 countries downloaded Obadiah’s book. The blog was voted the best overall in People’s Choice: Jewish and Israeli blogs Awards, received Webby Honoree and other awards.

Please help us spread Obadiah's message, and mention the blog in one of your posts, or link to us. We would greatly appreciate your comments at www.samsonblinded.org/blog

Best wishes,

Eugene Gershin

Jewrusalem.net – Israeli Uncensored News

Conal Elliott

Neat.

I think you're missing a "return" at the very start of your example. Or, define

    given target = return ()
and use
    given >>- "test.c" ->> ccompile >>- ...
or whatever name you like in place of "given".

Neil Mitchell

Very cool, I've wanted a DSL for Make in Haskell for a while. If you turn this into a fully fledged system (say about 60 lines ;) let me know!

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)