Compiling to a stack machine

6.4.4 Compiling to a stack machine

  In this section we compile expressions to instructions on a stack machine. We can then use this stack machine to evaluate compiled expressions. This section is inspired by an example in [3].

  Imagine a simple computer for evaluating arithmetic expressions. This computer has a ‘stack’ and can execute ‘instructions’ which change the value of the stack. The class of possible instructions is defined by the following datatype.

  data MachInstr v = Push v | Apply (v -> v -> v) type StackMachine v = [MachInstr v]

  An instruction either pushes a value of type v on the stack, or it executes an operator that takes the two top values of the stack, applies the operator, and pushes the result back on the stack. A stack (a value of type Stack v for some value type v) is a list of values, from which you can pop values, on which you can push values, and from which you can take the top value. A module Stack for stacks can be found in appendix A. The effect of executing an instruction of type MachInstr is defined by

  execute

  :: MachInstr v -> Stack v -> Stack v

  execute (Push x) s

  = push x s

  execute (Apply op) s = let a = top s

  t = pop s

  b = top t u = pop t in push (op a b) u

  A sequence of instructions is executed by the function run defined by run

  :: StackMachine v -> Stack v -> Stack v

  run []

  s = s

  run (x:xs) s = run xs (execute x s) It follows that run can be defined as a foldl.

  An expression can be translated (or compiled) into a list of instructions by the function compile, defined by:

  Compositionality

  compileExpr :: Expr ->

  Env Name (StackMachine Value) -> StackMachine Value

  compileExpr = foldExpr (add,min,mul,dvd,num,var,def) where

  f ‘add‘ g

  = \env -> f env ++ g env ++ [Apply (+)]

  f ‘min‘ g

  = \env -> f env ++ g env ++ [Apply (-)]

  f ‘mul‘ g

  = \env -> f env ++ g env ++ [Apply ()]

  f ‘dvd‘ g

  = \env -> f env ++ g env ++ [Apply ()]

  num v

  = \env -> [Push v]

  var x

  = \env -> env ? x

  def x fd fb = \env -> fb ((x,fd env):env) We now have for all expressions e, environments env, and stacks s:

  top (run (compileExpr e env) s) = resultExprGood e env The proof of this equality, which is by induction on expressions, is omitted.

  Exercise 6.5 . Define the following functions as folds on the datatype Expr that contains defi-

  nitions.

  1. isSum, which determines whether or not an expression is a sum.

  2. vars, which returns the list of variables that occur in the expression.

  Exercise 6.6 . This exercise deals with expressions without definitions. The function der is

  defined by

  der :: Expr -> String -> Expr der (e1 ‘Add‘ e2) dx = der e1 dx ‘Add‘ der e2 dx der (e1 ‘Min‘ e2) dx = der e1 dx ‘Min‘ der e2 dx der (e1 ‘Mul‘ e2) dx = e1 ‘Mul‘ (der e2 dx) ‘Add‘ (der e1 dx) ‘Mul‘ e2 der (e1 ‘Dvd‘ e2) dx = (e2 ‘Mul‘ (der e1 dx)

  ‘Min‘ e1 ‘Mul‘ (der e2 dx)) ‘Dvd‘ (e2 ‘Mul‘ e2)

  der (Num f) dx

  = Num 0.0

  der (Var s) dx

  = if s == dx then Num 1.0 else Num 0.0

  1. Give an informal description of the function der.

  2. Why is the function der not compositional ?

  3. Define a datatype Exp to represent expressions consisting of (floating point) constants, variables, addition and substraction. Also, define the type ExpAlgebra and the corresponding foldExp.

  4. Define the function der on Exp and show that this function is compositional.

  6.5 Block structured languages

  Exercise 6.7 . Define the function replace, which given a binary tree and an element m replaces

  the elements at the leaves by m as a fold on the datatype BinTree, see Section 6.2.1. It is easy to write a function with the required functionality if you swap the arguments, but then it is impossible to write replace as a fold. Note that the fold returns a function, which when given m replaces all the leaves by m.

  Exercise 6.8 . Consider the datatype of paths introduced in Exercise 3. A path in a tree leads

  to a unique leaf. Define a compositonal function path2Value which, given a tree and a path in the tree, yields the element at the unique leaf.

Dokumen yang terkait

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 819 7

ANTARA IDEALISME DAN KENYATAAN: KEBIJAKAN PENDIDIKAN TIONGHOA PERANAKAN DI SURABAYA PADA MASA PENDUDUKAN JEPANG TAHUN 1942-1945 Between Idealism and Reality: Education Policy of Chinese in Surabaya in the Japanese Era at 1942-1945)

1 29 9

Improving the Eighth Year Students' Tense Achievement and Active Participation by Giving Positive Reinforcement at SMPN 1 Silo in the 2013/2014 Academic Year

7 202 3

Improving the VIII-B Students' listening comprehension ability through note taking and partial dictation techniques at SMPN 3 Jember in the 2006/2007 Academic Year -

0 63 87

The Correlation between students vocabulary master and reading comprehension

16 145 49

The correlation intelligence quatient (IQ) and studenst achievement in learning english : a correlational study on tenth grade of man 19 jakarta

0 57 61

An analysis of moral values through the rewards and punishments on the script of The chronicles of Narnia : The Lion, the witch, and the wardrobe

1 59 47

Improping student's reading comprehension of descriptive text through textual teaching and learning (CTL)

8 140 133

The correlation between listening skill and pronunciation accuracy : a case study in the firt year of smk vocation higt school pupita bangsa ciputat school year 2005-2006

9 128 37

Transmission of Greek and Arabic Veteri

0 1 22