1/24/2013

The Objectsystem of Haskell - IORef

Each second or third month, there pops up a new discussion on Haskell and Object Oriented Programming (OOP) in the blogs and mail threads. Usually typical questions are: "why does Haskell don't have it?", "what are the differences between the type classes and OO classes?", "how can non mutal state be incorporated in a design and combined with OO?" and similar ones.

After a while this discussion then is decreasing in intensity and most of the time the historic references are also cited like OOHaskell from Oleg Kiselyov, Ralf Lämmel, and Keean Schupke or O'Haskell from Johan Nordlander as another one. Sometimes non-Haskell examples from OCaml, Clojure, Scala and F# are given (you may name additional ones). And in rare cases a discussion spins of about functional reactive programming (FRP) being a paradigm used for more complex simulation situations, a domain, where OOP excells.

But actually, the fact that there is still no in-use OO extension of Haskell and that there is still a big difference between Haskell and other more cross-paradigm languages gives a hint that there might be more deeper reasons behind this missing link. There must be something fundamentally different between Haskell concepts and OO language concepts and this difference may also reveal interesting things about OO and Haskell itself. And actually of course this difference has been pointed to by many others and and is around the handling of state in general.

In this blog post I want to focus on this state handling and connect it with a statement on OOP which might be overdrawn but which is actually a hidden truth behind OOP:

One of the main reasons why OOP was and is successful is simply due to the new possibility of administering big amounts of state in a complex program in a reasonable and efficient manner. This is the core feature of OOP, which made it successful, nothing else, not inheritance not encapsulation nor re-usability.
In OOP this management of state is done by pointers to individual, identified, mutuable data blocks. Therefore one could argue that the OOP features of Haskell are named IORef and fclabels because those are the basic technologies in Haskell that allow programming with the same paradigms as used heavily in OOP: reference semantics instead of value semantics, whereas fclabels serves as an efficient and usable record system to make administration even easier.
The Objectsystem of Haskell is named IORef!
Of course this neglects all the other nice features of OOP like encapsulation, inheritance, re-usability. But let's go one step back: are those features not just enablers of making the basic fundamental use case working and isn't this fundamental use case the typical OOP state handling paradigm? Taking this approach further it becomes clear why OOP is not used in Haskell. This language puts a much more clean focus on state and in cases where complex state systems are needed (as in simulations for example) other solutions are taken by the Haskellers, like Monads, Arrows, FRP, which are keeping the value semantics. An actually could'nt we go without IORef in Haskell and only use a good record system and keep value semantics. Yes, of course but then it's not OOP any more. So to finalize with a last statement.
The discussion if it is OK to use IORef and the discussion if it is good to use OOP are actually identical.

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