Ads




Profiles

Blogroll



  • Ashish's Niti - Old Blog

Your email address:


Powered by FeedBlitz

More on Haskell DSL

In an earlier post, partial make replacement, I developed a small Haskell based DSL to express GNU make like rules. In this post, I take the idea further to support multiple dependencies per target.

testO = (return ["test.c", "test.h"]) ->> ccompile >>- "test.o"

testExe = testO ->> link >>- "test.exe"

main = testExe

The above example, expresses in a simple way, that test.o depends on test.c and test.h and can be "made" using ccompile (C compilation). Similarly, test.exe depends on test.o and can be generated by linking test.o. Checkout the Hmake module (30 lines, including the definition of ccompile and link) and try it out yourself. It now properly checks if a target is out of date with its dependency before trying to build to the target.

Compare the above with the elaborate topological sort based ermake implementation (232 lines) using erlang.

With the Haskell module above, one can even build multiple targets in parallel by using forkIO (part of the Concurrent Haskell).

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.

June 04, 2007

Haskell, DSL and Monad

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)

data DayEnum = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday   deriving (Show, Eq, Enum, Bounded)

data Month = M MonthEnum Year
data Day   = Dm Int Month

Haskell uses lazy evaluation by default. This makes it easier to represent an infinite stream of years starting from 2007 as:

years = [Y y | y <- [2007..]]

Using similar list comprehension notation, list of months in a given year and list of days in a given months can be represented as,

months (Y y) = [M m (Y y) | m <- [January .. December]]

days (M m yy@(Y y))
    | m `elem` [January,March,May,July,August,October,December] = [Dm d (M m yy) | d <- [1..31]]
    | m == February = [Dm d (M m yy) | d <- [1..if y `mod` 4 == 0 then 29 else 28]]
    | otherwise = [Dm d (M m yy) | d <- [1..30]]

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 Jun
e  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.

March 16, 2007

What would Gandhi Really Do?

WHAT WOULD GANDHI DO? Fred Thompson thinks Code Pink's sanctimonious question is actually reprehensible.

During World War II, Gandhi penned an open letter to the British people, urging them to surrender to the Nazis. Later, when the extent of the holocaust was known, he criticized Jews who had tried to escape or fight for their lives as they did in Warsaw and Treblinka. “The Jews should have offered themselves to the butcher’s knife,” he said. “They should have thrown themselves into the sea from cliffs.” “Collective suicide,” he told his biographer, “would have been heroism.”

Source: Instapundit.com -

But then, there is this!

At the beginning of the Second World War he demanded independence as India's price for helping Britain during the war.

Source: BBC News | World | The life and death of Mahatma Gandhi

So, what if British had agreed to Gandhi's price? I bet when he meant India will help Britain during the war, he was not merely talking about moral support.

Wouldn't his actions (or offer of action) be more relevant here? After all, the question was not what Gandhi would have said but what he would have done!

March 10, 2007

Learning Haskell

I stumbled onto Haskell around 2 months back while searching for resources on spreadsheet programming.

I wanted to do Buy vs Rent calculations before deciding to buy a house. Microsoft Excel is pretty good. However, after a while it simply did not scale up. You cannot define your own functions and there are no local variables available. It has been my experience that learning in the context of real problem is much faster and easier. This time, it launched my exploration of functional programming. Enter Haskell.

Haskell is a pure functional programming language. Transition from imperative programming to functional programming (and too a pure one) was very difficult. But slowly, I got the hang of it. I re-did Buy vs Rent calculations in Haskell (it's not complete yet).

To explore Haskell further, I decided to implement a small subset of GNU Make features in Haskell. Within a few days (1 day of "thinking" and reading, followed by a day of coding), I was able to get a working system (see make.hs). To use it, simply call 'make' function, passing it a list of rules and a target:

make [(makeCCRule "test")] "test.o"

Simple, C compilation and linking rules are included. But you can define your own rules (a tuple of dependencies, target and list of commands to run when the target is out of date with its dependencies). Haskell allows you to focus on your application logic without causing undue distractions of memory management, maintaining state or even declaring types (it provides type inferencing). It is amazing how much you can do in less than 90 lines of Haskell code (including comments and empty lines). 

I haven't tested it thoroughly and there is no error handling. The system is easy to extend with your own "definition" of what it means to have a target out of date with its dependencies. With a little bit of refactoring it won't be that hard to support pattern like rules (.c to .o, etc.). Please send me feedback on how to improve this (I would love more modularity using type classes, for example).

March 08, 2007

Minsky and Housing Bubble

Dr. Hyman Minsky's explanation of boom and boost in a capitalist economy is based on structure (or rather change of) of financial relationships. It is highly relevant today as it describes what is happening with the housing sector. The practice of ARMs and various option mortgages are nothing but Ponzi finance schemes.

“In order to understand why our economy has behaved differently since the middle of 1960s than it has earlier in the post-World War II epoch we have to appreciate how the broad contours of the financial structure have changed. The changes in the broad contours of demand have changed the reaction of aggregate profits to a change in investment and therefore have changed the cyclical behavior of the ability of business to validate its debts. The changes in the financial structure have increased the proportion of speculative and Ponzi finance in the total financial structure and therefore increased the vulnerability of the financial system to refinancing and debt validating crises.”

“A thorough research study should examine the changing composition of the assets and liabilities of the various sectors and the implications of this changing structure, as well as changes in financing terms, for the cash flows of the various sectors of the economy. The cash flow structure due to liabilities need then be integrated with the cash flow from assets and the various cash flows due to income production. In particular the changing relations between cash receipts and payment obligations and between payment obligations and the margin of safety need be understood.” (page 49)

The combined effects of big government as a demander of goods and services, as a generator – through its deficits – of business profits and as a provider to financial markets of high-grade default-free liabilities when there is a reversion from private debt means that big government is a three way stabilizer in our economy and that the very process of stabilizing the economy sets the stage for a subsequent bout of accelerating inflation.” (page 56)

“Innovations in financial practices are a feature of our economy, especially when things go well… But each new instrument and expanded use of old instruments increases the amount of financing that is available and which can be used for financing activity and taking positions in inherited assets. Increased availability of finance bids up the prices of assets relative to the prices of current output, and this leads to increases in investment… In our economy it is useful to distinguish between hedge and speculative finance. Hedge finance takes place when the cash flows from operations are expected to be large enough to meet the payment commitments on debts. Speculative finance takes place when the cash flows from operations are not expected to be large enough to meet payment commitments, even though the present value of expected cash receipts is greater than the present value of payment commitments.” (page 66)

“During a period of successful functioning of the economy, private debts and speculative practices are validated.  However, whereas units that engage in hedge finance depend only upon the normal functioning of factor and product markets, unit which engage in speculative finance also depend upon the normal functioning of financial markets. In particular, speculative units must continuously refinance their positions. Higher interest rates will raise their costs of money even as the returns on assets may not increase…

In addition to hedge and speculative finance there is Ponzi finance – a situation in which cash payments commitments on debt are met by increasing the amount of debt outstandingPonzi financing units cannot carry on too longFeedbacks from revealed financial weakness of some units affect the willingness of bankers and business to debt finance a wide variety of organizations… Quite suddenly a panic can develop as pressure to lower debt ratios increases.” (page 67)

Source: Credit Bubble Bulletin, by Doug Noland of Prudent Bear.

February 21, 2007

Virtual Gold Rush

 Via the Division of Labour, This BussinessWeek article, Innovation in the Age of Mass Collaboration, describes how a conservative mining company discovered gold by opening up its proprietary database on the Internet and offering a prize:

The contestants identified 110 targets on the Red Lake property, more than 80% of which yielded substantial quantities of gold. In fact, since the challenge was initiated, an astounding 8 million ounces of gold have been found—worth well over $3 billion. Not a bad return on a half million dollar investment.

Steve Jobs blames teacher unions

Apple Inc. CEO Steve Jobs lambasted teacher unions Friday, claiming no amount of technology in the classroom would improve public schools until principals could fire bad teachers.

Source: Cafe Hayek

February 20, 2007

Does change in exchange rates affect international trade?

If yesterday one dollar bought 100 yen, and today one dollar buys 120 yen, each dollar today buys 20 percent more Japanese goods than it bought yesterday.

Source: Cafe Hayek: Hooray for a Strong Dollar

Really! So, presumably, this 20% more japanese products came out of nowhere in a day?

Or did he meant to say that purchasing power has shifted from holders of Japanese Yen to holders of US dollars. But how can we conclude that merely from the fact that exchange ratio between dollars and Yen has changed.

If say, Japanese govt. has printed more Yen. It will simply inflate the prices of all products in Yen. US dollars will also appreciate compared to Yen but (may) not appreciate compared to Japanese products.

So, why would US dollars be able to buy more Japanese products?

If amount of US dollars and Japanese products in the market haven't changed in a day, why would their ratio (prices) change?

Bay Area and House Prices

Burbed.com, is my favorite blog when it comes to housing market. It is witty, sarcastic and informative. This post is just too good to pass!.

If you ask ask most people in the Bay Area “Why did homes get so expensive in the last few years?” you’ll probably here these reasons:

  1. It’s not a bubble: They’re not making any more land. It’s special here. (Who knew that land suddenly decreased?)
  2. It’s not a bubble: Weather. It’s special here. (Who knew that the weather suddenly got so much better?)
  3. It’s not a bubble: Everyone wants to live here. It’s special here. (But why so much so in the last few years?)
  4. It’s not a bubble: Web 2.0, Google, The Valley is BACK! Oh, and it’s special here.

The last one is distinctly possible. Google IPOed in 2004, instantly creating thousands of millionaires/billionaires. The job market has picked back up. Traffic is back with a vengeance.

Ok, so maybe that explains these two graphs:

Source: How the Bay Area caused home prices to go up nationally… -- Burbed.com: Your Silicon Valley Home and Mortgage Insanity Blog

February 04, 2007

Festival of Stocks - February 5, 2007

Welcome to the February 5, 2007 edition of festival of stocks.

Sean Hackbarth says AirTran is Trying to Get Midwest for Cheap.

Paul Smith analyzes "the effect of Magazine tips when used in combination with Fundamental Analysis."

makingourway tackles the challenge of all bad choices in selecting 401k options with Merrill Lynch.

Larry Russell says Most individuals are poor investment portfolio managers

For all the time and effort that many individuals put into stock picking, you would hope that their economic wages would be very high for this activity. Sadly, the opposite seems to be true for most individual investors.

TJP analyzes the investment potential of Crocs (CROX), the hottest footwear company in the U.S in, Crocs: Own A Pair and Buy Some Shares.

wcsinvestor explains, in Reasoning about Two Sub-prime Lenders, how Sub-Prime Lenders can be a good investment.

"Not all sub-prime lenders are doomed. I go through a reasoning exercise to explain why Nicholas Financial and CompuCredit are good investments."

Paul Smith presents Quick Analysis of 0 -10c stocks with PE under 10.

Matthew Paulson presents The Ultimate Guide to Choosing Mutual Funds.

Scott Lee presents Become a Rock Star of Business.

TJP presents 3 Australian Uranium Stocks You Should Know.

H.S. Ayoub presents biotech stocks, news, commentary - BioHealth Investor.

H.S. Ayoub presents Government Contract Could Set Hollis-Eden Stock Soaring.

Travis Johnson presents Doubly Pinky Promise from Blackboard (BBBB):

"Blackboard (BBBB) angered a lot of its customers by trying to enforce a surprisingly broad patent ... now its promising to let open source competition move forward. What does that mean for this near-monopoly software provider?"

Bryan C. Fleming presents Million Dollar Savings Club Update: Week 5.

Brian Schumacher presents Charts of the Week - CLRT and AZL.

George presents Sally Beauty Holdings Question:

"Some thoughts on whether Sally Beauty will be added to a major index and whether the stock has already been dumped by the Big money."

That concludes this edition. Submit your blog article to the next edition of festival of stocks using our carnival submission form. Past posts and future hosts can be found on our blog carnival index page.

Technorati tags: , .

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

September 2007

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