Created
April 23, 2015 19:59
-
-
Save louissalin/410e84076ffa7e158038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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