Archive for the ‘Haskell’ Category

BarCamp!

Thursday, August 27th, 2009

So my friend John convinced me and a mutual friend of ours (Joyce a geek in training) to go to BarCamp this weekend. We had a blast!

Barcamp is free, very open ad-hoc technical conference.
Registration is free, but everyone is encouraged to present something.
Time is allocated into 30 minute blocks and you basically just pick an open time-slot/room and write in what you are wanting to present, and then nerds show up at that time to listen to you (if it sounds interesting). I did a presentation covering a brief overview of Haskell (bad slides here) and got a chance to listen to the presentations of several other fellow nerds. It was a lot of fun actually :)

One of the coolest things about BarCamp was all the awesome free stuff! (sponsored by local companies):

  • Free Tee-shirts
  • Free pizza for dinner of Friday.
  • Later Friday evening we all headed over to the Grand Rapids Brewing Company (nice place) for free beer (free beer!) and free appetizers!
  • Free breakfast Saturday morning
  • Free Subway subs for lunch

And did I mention that this was all free!

Anyway, I had a great time. Will definitly be going again next year! :D

Why Haskell is awesome (one of the many reasons)

Saturday, April 11th, 2009

Code without sophisticated error handling in a large and complex component:


– lots of code …

buildParser factor resultToExpr name parseData m = checkParseData $ execState (m >> buildLexer) parseData
where
checkParseData (newParseData@(ParserData lexer opTable literals pegTable)) = expr
where
expr = buildExpressionParser exprOpTable (foldr (<|>) (factor newParseData) literalParsers)

more code


lots more code

Code with sophisticated error handling in a large and complex component:


– lots of code …

buildParser factor resultToExpr name parseData m = checkParseData $ execStateT (m >> buildLexer) parseData
where
checkParseData (Left errorMsg) = fail errorMsg
checkParseData (Right (newParseData@(ParserData lexer opTable literals pegTable)) ) = expr
where
expr = buildExpressionParser exprOpTable (foldr (<|>) (factor newParseData) literalParsers)

more code


lots more code

Did you catch it? can you see the difference?
Now, without any additional modifications I can use ‘lift $ throwError’ anyware in ‘m’ (which is big) or ‘buildLexer’, and the error will propagate all the way down here to checkParseData, which will then drop the error right into the Parsec parser monad.
Doesn’t it just make you happy? :D

Haskell Type Hackery

Saturday, April 11th, 2009
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TypeSynonymInstances #-}

{-
Here is a rough draft (I’ll clean this post up later, but I need to get to bed here…)
of my recent attempts at some fancy type hackery in haskell.

I’ll cover three things (the first two require no extensions):
How to make a function that converts a type to a value
How to make a function that can change it’s output value depending on the type you are requesting of it.
How to make a fancier function that converts a type to a value

-}

– just a random type to play with
data Color = Red | Green | Blue deriving Show

– so we want a function that converts from a type to a value

– here is how you do it

– First make a type class with the conversion function in it.
class TypeToInt a where
– the conversion function takes a type ‘a’ that is the type class parameter
– and returns a value of some other type
typeToInt :: a -> Int

– add instance definitions for each type that you want to convert
instance TypeToInt Color where
typeToInt _ = 0

instance TypeToInt Int where
typeToInt _ = 1

– And whola! values from types.
–   note that we don’t care what value you pass into typeToInt, just the type of the value
numberOfColor = typeToInt Red
numberOfInt = typeToInt (42::Int)
– numberOfColor = 0
– numberOfInt = 1

{-
Now for something a little more tricky, changing the output of a function depending on the type that you
are expecting. Note that there are probably other ways to do this that might work better depending on what
you are trying to do.

-}
– First you make some sort of phantom container type. Note the extra type variable that
– doesn’t show up in the constructor. This allows you to attach type information (any type
– want ) to the container.
data PhantomContainter a b = Container a deriving Show

– These are the types that I want to use to select which value that I want.
– These types don’t have any usable constructors, but you could just as easily use any
– type you want.
newtype Rock = Rock ()
newtype Paper = Paper ()
newtype Scissors = Scissors ()

– make a type class, with the “phantom” variable (the one that is not in the constructor)
–  as the parameter
class TypeSelectableValue b where
– make a function in the class that returns a PhantomContainer
selectValue :: PhantomContainter String b

– make instances for all the types you want to select with
instance TypeSelectableValue Rock where
selectValue = Container “123 Rock”

instance TypeSelectableValue Paper where
selectValue = Container “123 Paper”

instance TypeSelectableValue Scissors where
selectValue = Container “123 Scissors”

– Then you can select a value by specifing a type signature on the return value
myPaper = selectValue :: PhantomContainter String Paper
– myPaper = Continer “Paper”
– myPaper :: PhantomContainter Paper
–or
myRock :: PhantomContainter String Rock
myRock = selectValue
– myRock = Continer “Rock”

– you can then make an unwrapper that take x out of the container, note however that you have
–   to use a type signature to select the value _before_ you unwrap it, because otherwise you
–   strip off all the type information an there is no way to infer the types.
– So this really only works where you want to select the value at compile time. But it can come in handy.
unwrapContainer (Container x) = x

{-

now lets say you have TypeToInt, but what you really want is a TypeToAnything,
you want to be able to do something like this
typeToAnything (someColor) = 5
typeToAnything (someIntger) = “Integer”
typeToAnything (someString) = Blue
well is so happens that you can!
but it will require some multiparameter type classes but before you panic,
it will actually fairly intuitive and look quite similar to TypeToInt.
-}

– you will have to use the switch -XMultiParamTypeClasses to tell the compiler to allow
– the multiparameter type classes or you can put {-# LANGUAGE MultiParamTypeClasses #-}
– at the top of your source file

– so we basically want to do the same thing as we did with TypeToInt, but we are going to replace
– Int with a type variable

– The one extra thing we need to add is the functional dependencie a -> b to the typeclass.
– This essentially says that we only have one output type for a particular input type.
– you’ll need to add {-# LANGUAGE FunctionalDependencies #-} to  the top of the source to tell the compiler
– to allow it.

– You’ll also need to add {-# LANGUAGE TypeSynonymInstances#-} to allow the use of the type synonym String
– in the instance declarations

-}
class TypeToAnything a b | a -> b where
typeToAnything :: a -> b

–then for our instance declarations we have to include the extra type parameter
instance TypeToAnything Color Int where
typeToAnything _ = 42

instance TypeToAnything Int String where
typeToAnything _ = “Integer”

instance TypeToAnything String Color where
typeToAnything a = Blue

somethingFromColor = typeToAnything Red
somethingFromInt = typeToAnything (42::Int)
somethingFromString = typeToAnything “bla bla bla”
– somethingFromColor = 42
– somethingFromInt = “Integer”
– somethingFromString = Blue