10/20/2012

The Power of Haskell's Typesystem

The title of this post could be also "Haskell's unknown record system", but it is not about records, it's about the typesystem, which can do things, you would'nt expect. In the example below a program is written which defines records of different types, one record with two attributes, another one with three. Then a polymorphic function is defined, which prints one attribute. The nice thing: you can call this funtion on a record and request a label, which this record does not have. What happens? See yourself. This is the essence of "Haskell is a strictly typed language".
-- The below small example show how mighty Haskell's Typesystem
-- actually is. 

-- For this demonstration, it uses Oleg Kiselyov, Ralf
-- Laemmel and Keean Schupke heterogeneous collections.
-- see: http://homepages.cwi.nl/~ralf/HList
 

module Main where

import Data.HList
import Data.HList.Label2
import Data.HList.Record
import Data.HList.GhcSyntax

-- this is a polymorphic function, which selects an attribute from 
-- a record and prints it, as you can easily see, the function does
-- not carry any information on the attributes, it selects.

printAttribute record attribute = do
 print $ record # attribute
 return ()

-- this is the main function, applying printAttribute

main = do

 -- first some record attributes are defined
 let attribOne = firstLabel "MyDomain" "attribOne description"
 let attribTwo = nextLabel attribOne "attribTwo description"
 let attribThree = nextLabel attribTwo "attribThree description"

 -- now, let's define a record which has content for attributes one and two
 let recordOneTwo = attribOne .=. 1 .*. attribTwo .=. "this is content for attrib 2" .*. emptyRecord
 
 -- and another record which is an exension and also has content for label three
 let recordOneTwoThree = hAppend recordOneTwo (attribThree .=. "does it print?" .*. emptyRecord)

 -- now, lets print some content from the records, works all fine
 printAttribute recordOneTwo attribOne -- prints "1"
 printAttribute recordOneTwo attribTwo -- prints "this is content for attrib 2"
 printAttribute recordOneTwoThree attribThree -- prints "does it print?"
 
 -- the next line cannot work, it selects attribThree from a record
 -- which is only containing attribOne and attribTwo.
 printAttribute recordOneTwo attribThree

-- program does not compile, but compiles with above line commented!
-- The interesting point here is, the compiler detects this, although the
-- type of function printAttribute is polymorphic with regard to the different
-- records. This is Haskell!

9/21/2012

blog haskell syntax highlighter

To make these postings work, I needed to use a Haskell syntax highlighter. This was quite easy using the google code prettyfier. I added the following lines before the "head" tag in the blog template HTML file:
<link href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js">  </script>
<script type="text/javascript" src="http://google-code-prettify.googlecode.com/svn/trunk/src/lang-hs.js">  </script>
and also modified the "body" tag like this:
<body onload='prettyPrint()'>
and voila, it's possible, to get syntax highlighting by using the "pre" tag with the attribute "prettyprint lang-html" or "prettyprint lang-hs" or ... !

Note: you may need to clean the HTML parenthesis with this tool: Simplecode.pl

{-# LANGUAGE GADTs, FlexibleInstances #-}
module StackMachineMonad where

-- Representation
type Program instr    = [instr]
type StackProgram     = Program StackInstruction
data StackInstruction = Push Int | Pop

-- Concatenation and interface
empty = []

pop  :: StackProgram
push :: Int -> StackProgram
pop    = Pop : []
push n = Push n : []

-- Interpreter
type Stack a = [a]

interpret :: StackProgram -> (Stack Int -> Stack Int)
interpret (Push a : is) stack = interpret is (a : stack)
interpret (Pop    : is) stack = interpret is (tail stack)
interpret []            stack = stack

-- Examples, try for instance
    --      > interpret example []
    --      > interpret (replace 5) [3] 

example      = Push 5 : Push 42 : Pop : []
exampleTwice = example ++ example
replace a    = pop ++ push a