Extensible parsing via types

The fact that c’s type-system is so sloppy has been greatly annoying me lately. Sure, you can give gcc flags such as -Wsign-conversion, but if i create a typedef of int32_t named apples and another called oranges, i’ll be at liberty to do any math operator on them all while gcc keeps a straight face.

On the other side of the spectrum is caml where use have to use different syntax for addition depending on if you adding two integers or two floats.

While in reality i much prefer a good pie to most of the cakes you may offer; if cake is what’s on the menu, i’d like to go ahead and eat it.

Haskell uses a concept of type classes to allow the same addition operator to be used for multiple different types, but in a type safe fashion. Gulp.

Lisp’s parser is extensible through what it calls reader macros; basically you can specify your own parser which will be called whenever your syntax is spotted. Using this you can define new sub-languages with completely different syntaxes.

This got me thinking, what interesting things could we do if Lisp had a static type system. One idea is type based parsing: when you define a typeclass you can specify a parser to be used for it. For example, if i create a price typeclass, i might drop in a parser that will let me write “$123,456.78”. That’s sugar, the substance is that the language could avoid potholes other languages have such as “123UL” in c or auto-typecasting or auto-promotion used widely.


~ by pulotka on 2009/05/17.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: