-- Abstract machine example from chapter 8 of Programming in Haskell, -- Graham Hutton, Cambridge University Press, 2016. -- Example of a simple toy expression language -- that can evaluate addition operations. -- The idea of this exercise is to built a control stack -- that guarantees the evaluation from left to right -- syntax of this toy language for simple arithmetic expressions data Expr = Val Int | Add Expr Expr deriving Show -- this is just to allow us to print values for this data type -- Example: the expression (1 + 2) + 3 in the syntax of the toy language is -- (Add (Add (Val 1) (Val 2)) (Val 3)) -- semantics of this toy language for simple arithmetic expressions -- value is a function that evaluates the expression to a value -- example -- value (Add (Add (Val 1) (Val 2)) (Val 3)) should return 6 value :: Expr -> Int value (Val n) = n value (Add x y) = value x + value y -- to make the manipulation of arguments explict from left to right -- we build a stack controller as a list of operations accepting evaluation and addition type Cont = [Op] data Op = EVAL Expr | ADD Int -- value' is an alternative implementation of value, -- in value the order of exectution is controlled by Haskell -- the idea is that with value' we are controlling the order of execution -- from left to right using the control stack form above -- example -- value' (Add (Add (Val 1) (Val 2)) (Val 3)) -- value' :: Expr -> Int -- value' e = eval e [] -- value' is implemented with the help of eval and exec -- eval and exec are two mutually recursive funtions that -- are going to be used to evaluate an expression -- eval takes an expression and a stack controller c, to moves operations, -- and return an integer. -- eval :: Expr -> Cont -> Int -- cases: -- the idea for this case is that once we have evaluated as much as possible, -- then we continue executing the rest of the control stack c -- eval (Val n) c = exec c n -- (1) execute c in the context of n -- example -- eval (Val 1) [EVAL (Val 2), EVAL (Val 3)] -- reduces to -- exec [EVAL (Val 2), EVAL (Val 3)] 1 -- the idea of this case is to evaluate first x (as much as possible) and postpone the evaluation of y -- this guarantee that evaluation is from left to right -- eval (Add x y) c = eval x (EVAL y : c) -- (2) eval x and put EVAL y at the head of the stack -- eval (Add (Add (Val 1) (Val 2)) (Val 3)) [] -- reduces to -- eval (Add (Val 1) (Val 2)) [EVAL (Val 3)] -- example: evaluate (1 + 2) + 3 -- eval (Add (Add (Val 1) (Val 2)) (Val 3)) [] -- reduces to -- eval (Add (Val 1) (Val 2)) [EVAL (Val 3)] -- reduces to -- eval (Val 1) [EVAL (Val 2), EVAL (Val 3)] -- reduces to -- exec [EVAL (Val 2), EVAL (Val 3)] 1 -- ... -- a funtion to execute the operations in the control stack from left to right. -- The function exec takes a stact comtroller and integer n and returns an integer -- exec :: Cont -> Int -> Int -- cases: -- exec [] n = -- (1) returns the input integer -- example -- exec [] 5 reduces to 5 -- the idea of this case is to evaluate y first before adding it to n -- exec (EVAL y : c) n = -- (2) evaluates y and put ADD n at head of the stack -- example -- exec [EVAL (Add (Val 2) (Val 3)), EVAL (Val 2)] 1 -- reduces to -- eval (Add (Val 2) (Val 3)) [ADD 1,EVAL (Val 2)] -- at this point we know that both n and m are values -- exec (ADD n : c) m = -- (3) execute the rest of the stack C in the context of m+n -- example -- exec [ADD 1, EVAL (Add (Val 2) (Val 3))] 5 -- reduces to -- exec [EVAL (Add (Val 2) (Val 3))] 6 -- uncomment value' and try -- value' (Add (Add (Val 1) (Val 2)) (Val 3))