Sunday, December 21, 2008

To Read - Haskell Type Goodness


Read http://okmij.org/ftp/Haskell/types.html to gain insight into a host of different ways in which the powerful Haskell type system can be (ab)used to get, for instance -

polyvariadic functions (which take a variable number of arguments with potentially different types)

functions with genuine keyword arguments,

static array bounds checking,

automatically reifying a type to yield a function with the desired properties (using only the haskell typechecker. No external tools/extensions are needed because Haskell already has extensive reflexive properties),

strongly typed heterogenous collections,

and a whole bunch of other stuff that will blow your mind!

The following code causes Haskell 98 to give a Segmentation Fault!!!

module C where
     
     import Data.Typeable
     import Data.Maybe
     
     newtype W a = W{unW :: a}
     instance Typeable (W a) where typeOf _ = typeOf ()
     
     bad_cast x = unW . fromJust . cast $ W x
     -- inferred type: bad_cast :: a -> b
     
     test1 = bad_cast True ++ ""

Sad but true...

No comments: