Ads




Profiles

Blogroll



  • Ashish's Niti - Old Blog

Your email address:


Powered by FeedBlitz

« Haskell, DSL and Monad | Main | More on Haskell DSL »

June 09, 2007

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.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d8341e003353ef00e5507254898833

Listed below are links to weblogs that reference Another DSL embedded in Haskell - a partial make replacement in less than 10 lines:

Comments

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

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".

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.

Subscribe




  • Subscribe to my RSS feed

  • Enter your Email


    Preview |

Tip Jar



  • Amazon Honor System Click Here to Pay Learn More

Ashish's shared items in Google Reader

Search


  • Google

  • Search Now:
    Amazon Logo

BookShelf - Recently Read

April 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Blog powered by TypePad