Niti in Sanskrit means policy, strategy or vision. This blog hosts my views and commentary on political and economic matters.

June 2007

June 09, 2007

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:

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.

June 04, 2007

Haskell is an amazing language. One can easily embed a DSL (domain specific language) using monad. Let's take an concrete example to illustrate the power of DSL and Monads in Haskell.

I was always fascinated by a very useful, and nifty program, called remind. It is a very sophisticated calendering program that allows one to set reminders. For example, "REM 4 July MSG It's Independence Day!", will trigger an reminder on every July 4th. However, this kind of simple reminders can be set using any calendering application. 'Remind' provides a very flexible date specification for setting reminders. The date spec consists of 'day month year weekday'. If you omit the date spec, the reminder occurs every day. If you specify only the day part, then the reminder is triggered on the specified day of every month. If only month is specified, then the reminder is triggered every day of the specified month. And so on... You can take a look at the man page of the remind for more details about the date spec rules.

Remembering the above rules can be tricky. Instead, let's see if Haskell can help us here. I used an idea proposed by Ketil Malde on Haskell-Cafe mailing list on how to handle calendar dates in Haskell. Here is the basic idea:

data Year = Y Int

data MonthEnum = January | February | March | April | May | June | July | August | September | October | November | December deriving (Show, Eq, Enum, Bounded, Ord)

Notice how leap years are handled in producing days in a given month and year.

The above code was taken from the Ketil Malde's message at Haskell-Cafe and converted to add Enums.I added some boilerplate code to display the months properly by implementing an instance of the type class, Show:

instance Show Month where show (M m t) = show m ++ " " ++ show t

instance Show Year where show (Y y) = " " ++ show y

instance Show Day where show (Dm d t) = " " ++ show d ++ " " ++ show t

With the above addition, and using the fact that List in Haskell is a monad, we can easily represent the following,

-- all days of all months of all years (only the first 3 items are shown)*DateStream> take 3 (years >>= months >>= days)[ 1 January 2007, 2 January 2007, 3 January 2007]

-- first day of every month of every year*DateStream> take 3 (years >>= months >>= return.(Dm 1))[ 1 January 2007, 1 February 2007, 1 March 2007]

-- all days of january of every year*DateStream> take 3 (years >>= return.(M January) >>= days)[ 1 January 2007, 2 January 2007, 3 January 2007]

Notice, how easy it is to express the list of days that you are interested in. You can even filter the days by using the 'filter' function provided in Haskell for lists:

-- all mondays in June of any year starting from 2007*DateStream> take 6 (filter (isDayOfWeek Monday) (years >>= return.(M June) >>=days))[ 4 June 2007, 11 June 2007, 18 June 2007, 25 June 2007, 2 June 2008, 9 June 2008]

'isDayOfWeek' function requires calculating the 'day of week'.

All this functionality in just 43 lines of code (including comments). This already implements all combinations of Remind's date spec that does not involve day of week. It shouldn't be hard to implement the same for day of week.

Most importantly, the user only needs to use simple keywords, such as, years, months, days, january and combine them using the monadic bind operator, '>>='. There, you have a small DSL for calendar apps. You can browse the file here.

Feedback, corrections and suggestions are most welcome.

Update: As some readers pointed out the leap year calculation were wrong. Thanks for the feedback. I have corrected the code to use the gregorianMonthLength function from the Data.Time.Calendar module.