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