CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Similar documents
CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Homework 3 COSE212, Fall 2018

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CS 360 Programming Languages Interpreters

CS 320: Concepts of Programming Languages

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Syntax and Grammars 1 / 21

CIS 194: Homework 5. Due Friday, October 3, Rings. No template file is provided for this homework. Download the

L3 Programming September 19, OCaml Cheatsheet

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS558 Programming Languages

CS 360: Programming Languages Lecture 12: More Haskell

CSCI 3155: Homework Assignment 4

Introduction to Haskell

Principles of Programming Languages

Semantics of programming languages

FOFL and FOBS: First-Order Functions

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

CSE 12 Abstract Syntax Trees

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

Using Scala in CS241

SCHEME AND CALCULATOR 5b

CS558 Programming Languages

Writing Evaluators MIF08. Laure Gonnord

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Monad Background (3A) Young Won Lim 11/8/17

G Programming Languages - Fall 2012

Information Science 1

CIS 194: Homework 6. Due Friday, October 17, Preface. Setup. Generics. No template file is provided for this homework.

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions


Monad Background (3A) Young Won Lim 11/18/17

G Programming Languages - Fall 2012

CS457/557 Functional Languages

CIS 194: Homework 8. Due Wednesday, 8 April. Propositional Logic. Implication

Principles of Programming Languages

Types and Static Type Checking (Introducing Micro-Haskell)

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CIS192: Python Programming

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

LECTURE 16. Functional Programming

Language Reference Manual

These are notes for the third lecture; if statements and loops.

Semantics of programming languages

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

COMP 105 Homework: Type Systems

CSE341 Spring 2017, Final Examination June 8, 2017

Programming in Haskell Aug Nov 2015

Katja Laboratory Abstract Syntax Trees in Java with Katja

Denotational Semantics. Domain Theory

CS 11 Haskell track: lecture 1

CS 320: Concepts of Programming Languages

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

CMSC 330: Organization of Programming Languages

Functional Programming. Pure Functional Programming

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Applicative, traversable, foldable

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Lists. Michael P. Fourman. February 2, 2010

CIS 194: Homework 5. Due Monday, 18 February. Expressions. (2 + 3) 4 would be represented by the value

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Lecture 12: Conditional Expressions and Local Binding

Topic 7: Algebraic Data Types

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Haskell Syntax in Functions

In Our Last Exciting Episode

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Lecture 1 August 9, 2017

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Program Representations

CSCC24 Functional Programming Scheme Part 2

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSC324 Principles of Programming Languages

CS 11 Ocaml track: lecture 6

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Introduction to OCaml

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Mutable References. Chapter 1

Semantics of programming languages

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Types and Static Type Checking (Introducing Micro-Haskell)

Principles of Programming Languages 2017W, Functional Programming

Side Effects (3B) Young Won Lim 11/20/17

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

MP 3 A Lexer for MiniJava

Types and Programming Languages. Lecture 5. Extensions of simple types

Information Science 1

Transcription:

CIS 194: Homework 3 Due Wednesday, February 11, 2015 Interpreters An interpreter is a program that takes another program as an input and evaluates it. Many modern languages such as Java 1, Javascript, Python, and PHP are interpreted. Although interpreted languages are generally slower than compiled ones such as Haskell, OCaml, and C, they offer many advantages such as platform independence. Haskell s type system makes it an excellent metalanguage. A metalanguage is a language that is used to reason about a different language. In this assignment, you will implement an interpreter for a simple imperative language in Haskell. 1 Java is first compiled to bytecode which is then interpreted by the Java Virtual Machine Meet SImPL SImPL is the Simple Imperative Programming Language that you will be interpreting. This language is comprised of seven kinds of statements: assignments, increments, if statements, while loops, for loops, sequences, and skips. The only kinds of values that exists in SImPL are integers. Integers can be represented in expressions as literals (ie -1, 0, 5), variables, or the result of a computation involving two sub-expressions. An example SImPL program that calculates the value of B to the power of E is:

cis 194: homework 3 2 Out := 1; for (I := 0; I < E; I++) { } Out := Out * B At the end of the program s execution, the value of B to the power E is stored in the variable Out. This SImPL program is nicely readable by humans, however is not so easy for Haskell to work with. In Haskell, we will represent SImPL programs using Abstract Syntax Trees or ASTs. An AST is just a Haskell datatype that represents the structure of a piece of code. The datatypes that we will be working with in this assignment are defined in HW03.hs. The SImPL program above could be represented in Haskell as follows: Sequence (Assign "Out" (Val 1)) (For (Assign "I" (Val 0)) (Op (Var "I") Lt (Var "E")) (Incr "I") (Assign (Var "Out") (Op (Var "Out") Times (Var "B")))) Before you continue, make sure you understand the correspondence between the SImPL program and its representation in Haskell. All of this assignment will be based on working with and manipulating the AST. Exercise 1 Before we can start evaluating Expressions and Statements we need some way to store and look up the state of a variable. We define a State to be a function of type String -> Int. This makes it very easy to look up the value of a variable; to look up the value of "A" in state, we simply call state "A". Whenever we assign a variable, we want to update the program State. Implement the following function: extend :: State -> String -> Int -> State Hint: You can use the input State as a black box for variables other than the one you are assigning. let st = extend st "A" 5 in st "A" == 5 In addition to the ability to extend States, we need to have an empty State that we can use to evaluate programs where no variables are assigned. In the empty State, it might make sense to throw

cis 194: homework 3 3 an exception when you try to access a variable that has not been defined. Unfortunately, SImPL has no notion of exceptions 2. For this reason, we will define the empty State to set all variables to 0. Define the empty State: 2 SImPL has no type errors either; everything is an integer, so typechecking isn t necessary! empty :: State empty "A" == 0 Exercise 2 We are now ready to evaluate expressions! This can be implemented as a fairly straightforward recursive function. The value that an Expression evaluates to depends on the state of the variables that appear in the Expression. For this reason, a State must be provided along with the Expression that is being evaluated. Implement the function: evale :: State -> Expression -> Int Note: some of the binary operators (Bops) in SImPL are Int -> Int -> Int functions, but others have type Int -> Int -> Bool. Since SImPL only has integer types, we need to modify these functions to return Ints. We will do so by returning 0 in place of False and 1 in place of True. evale empty (Val 5) == 5 evale empty (Op (Val 1) Eql (Val 2)) == 0 Exercise 3 It turns out that SImPL isn t so simple after all. The syntax of SImPL contains some repetition. For example, Incrs are just special cases of Assigns. Syntax like this makes it easier for programmers to reason about code, however it makes the internal representation of the language more complicated. This is called Syntactic Sugar 3. SImPL is a little too sweet for my taste, so before we evaluate SImPL Statements we are going to desugar them. A DietStatement is a new datatype that we will use to represent desugared SImPL Statements. You may notice that DietStatements are very similar to Statements. In fact, the DietStatement type is the same as the Statement type with two constructors removed: Incr and 3 Haskell has plenty of Syntactic Sugar. We will learn much more about this when we learn about Monads in a few weeks. Before Haskell gets compiled to native machine code, it goes through an intense desugaring process. The result is a language called GHC Core.

cis 194: homework 3 4 For. This is because Incr is Syntactic Sugar for an assignment and For is Syntactic Sugar for a while loop. For Loop in SImPL for (A := 0; A < N; A++) {...} For Loop represented as an AST For (Assign "A" (Val 0)) (Op (Var "A") Lt (Var "N")) (Incr "A") (...) Explanation Initialization: Executed once at the very beginning Loop Condition: Expression dictating whether or not the body of the loop should be executed. Update: Executed every iteration after the body of the loop Now, implement the following function: desugar :: Statement -> DietStatement desugar (Incr "A") == DAssign "A" (Op (Var "A") Plus (Val 1)) Exercise 4 In this exercise, you will write a function that evaluates desugared Statements. Unlike Expressions, Statements do not evaluate to a single value. Instead, they perform actions that mutate the initial State. Since we cannot actually mutate the State in Haskell, we will return a new State that is the result of evaluating the program. Implement the function: evalsimple :: State -> DietStatement -> State Note: If statements and While loops both represent conditionals as an Expression. Since Expressions in SImPL are integers, we will consider 0 to be False and every other value to be True 4. 4 This is the standard convention in C. There is no boolean type in C either. let s = evalsimple empty (DAssign "A" (Val 10)) in s "A" == 10 We also want to be able to run programs that have not already been desugared. Implement the function: run :: State -> Statement -> State Note: run should be defined in terms of desugar and evalsimple. It should not be implemented from scratch.

cis 194: homework 3 5 Running Programs Congratulations! You have written an interpreter for a simple imperative language. But what good is an interpreter without any programs to test it on? We have provided three programs to test your code on: factorial, square root, and fibonacci. You can use these programs to test your interpreter. In each case, the input is assumed to be assigned to the variable "In" and the output is assign to "Out". So, for example, if you wanted to run the factorial program with input 4, you should run the program with a State that binds the variable "In" to 4. let s = run (extend empty "In" 4) factorial in s "Out" == 24