Skip to content

Instantly share code, notes, and snippets.

@louissalin
Created April 23, 2015 19:59
Show Gist options
  • Save louissalin/410e84076ffa7e158038 to your computer and use it in GitHub Desktop.
Save louissalin/410e84076ffa7e158038 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings, GADTs #-}
import Prelude
import Control.Monad.Operational
data StackInstruction a where
Push :: Int -> StackInstruction ()
Pop :: StackInstruction Int
type StackProgram a = Program StackInstruction a
type Stack b = [b]
push = singleton . Push
pop = singleton Pop
interpret :: StackProgram a -> (Stack Int -> a)
interpret = eval . view
where
eval :: ProgramView StackInstruction a -> (Stack Int -> a)
eval (Push a :>>= is) stack = interpret (is ()) (a:stack)
eval (Pop :>>= is) (a:stack) = interpret (is a ) stack
eval (Return a) stack = a
main = print $ interpret prog [7,11]
where prog = do
a <- pop
b <- pop
return (a*b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment