ASSIGNMENT 2 ------------ Announced on 4th March 2007 Submission Deadline: 26 March 2007 Please start early or you will run into problems. This warning This aim of this assignment is to give you a better understanding of (1) Implementation of strict functional languages. (2) I/O in Haskell (3) The Read and Show classes in Haskell. Parts of this assignment will feed into the last assignment, which will be about implementation of lazy functional langauges. _________________________________________________________________________ Background building: 1. Parts of chapters 10, 11, 12 of the textbook The Implmentation of Functional Programming Languages by Simon L Peyton-Jones. Relevant parts of the book will be made available to you. 2. A gentle introduction to Haskell 3. The Haskell Manual. __________________________________________________________________________ Consider the representation of a small higher order language in Haskell: A program is a list of function definition followed by a single expression type Program = ([Fundecl], Exp) data Fundecl = Fun Funname [Var] Exp -- more than one arguments data Funname = F | G | H -- add more names if you want data Var = X | Y | Z -- add more variables if you want data Exp = Con Int | Var Var | Add Exp Exp | Funname Funname | Funapp [Exp] Assume that the language is eager. Write an interpreter for the language. The interpreter will read a program in the above language from a file. The file will contain the Haskell representation of the program in textual form. The interpreter will interpret the program and print the answer on the terminal. You can assume that the entire program produces a non-functional value. Example program 1: f x = x 1 + x 2 g a b c = a + b + c f (g (1+2) (3+4)) Haskell representation of program 1. pgm = ([fd1, fd2], exp) where fd1 = Fun F [X] (Add (Funapp [Var X, Con 1]) (Funapp [Var X, Con 2])) fd2 = Fun G [A, B, C] (Add (Add (Var A) (Var B)) (Var C)) exp = Funapp [(Funname F) (Funapp [(Funname G) (Add (Con 1) (Con 2)) (Add (Con 3) (Con 4))])] Example program 2: l a b = a + b f x = g x x g y z = h y h w = p w w p a b = a 3 f (l (1+2))