Homework 3 COSE212, Fall 2018

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Homework 5 COSE212, Fall 2018

Functional Programming. Pure Functional Programming

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

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

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Functional Programming

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

Lecture 3: Expressions

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Konzepte von Programmiersprachen

Problem Set CVO 103, Spring 2018

Cornell University 12 Oct Solutions. (a) [9 pts] For each of the 3 functions below, pick an appropriate type for it from the list below.

Mini-ML. CS 502 Lecture 2 8/28/08

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CMSC 330: Organization of Programming Languages

Chapter 3 Linear Structures: Lists

SML A F unctional Functional Language Language Lecture 19

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Chapter 3 Linear Structures: Lists

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Denotational Semantics. Domain Theory

CSE 130, Fall 2006: Final Examination

Exercise 1: Graph coloring (10 points)

Type Checking and Type Inference

Organization of Programming Languages CS3200/5200N. Lecture 11

Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem

Principles of Programming Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

02157 Functional Programming Interpreters for two simple languages including exercises

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

CSE-321 Programming Languages 2012 Midterm

Programming Languages Third Edition

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

L3 Programming September 19, OCaml Cheatsheet

Begin at the beginning

COMP 382: Reasoning about algorithms

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

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

Programming Languages

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Graphical Untyped Lambda Calculus Interactive Interpreter

Handout 2 August 25, 2008

Programming Languages

Pierce Ch. 3, 8, 11, 15. Type Systems

A Brief Introduction to Standard ML

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

An Introduction to Scheme

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

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CSCC24 Functional Programming Scheme Part 2

Programming Languages and Techniques (CIS120)

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

1 Introduction. 3 Syntax

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

Lecture 2: Big-Step Semantics

Logic - CM0845 Introduction to Haskell

CSE 130, Fall 2005: Final Examination

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

CSE 341 Section 5. Winter 2018

Combining Programming with Theorem Proving

Programming Languages Assignment #7

Data Types The ML Type System

Which of the following expressions has the type int list?

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

CS422 - Programming Language Design

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

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Programming Languages and Compilers (CS 421)

CS 360 Programming Languages Interpreters

Flang typechecker Due: February 27, 2015

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

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Note that pcall can be implemented using futures. That is, instead of. we can use

Programming Language Concepts, cs2104 Lecture 04 ( )

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Programming Languages: Application and Interpretation

Datatype declarations

CSE-321: Assignment 8 (100 points)

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

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

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Functional Programming. Functional Programming

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Functional Programming

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

Metaprogramming assignment 3

SMURF Language Reference Manual Serial MUsic Represented as Functions

COMP 105 Homework: Type Systems

Transcription:

Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports built-in lists and (mutually) recursive procedures. Language Design The syntax of ML is defined inductively as follows: P E E () unit true false booleans n integers x variables E + E E - E E * E E / E arithmetic E = E E < E comparison not E negation nil empty list E :: E list cons E @ E list append head E list head tail E list tail isnil E checking empty list if E then E else E if let x = E in E let letrec f(x) = E in E recursion letrec f(x 1 ) = E 1 and g(x 2 ) = E 2 in E mutual recursion proc x E function definition E E function application print E print E; E sequence The semantics of the language is similar to that of OCaml. The set of values the language manipulate includes unit ( ), integers (Z), booleans (Bool), lists (List), non-recursive procedures (Procedure), recursive procedures (RecProcedure), 1

and mutually recursive procedures (MRecProcedure): v Val = { } + Z + Bool + List + Procedure + RecProcedure + MRecProcedure n Z = {..., 2, 1, 0, 1, 2,...} b Bool = {true, false} s List = Val Procedure = Var E Env RecProcedure = Var Var E Env MRecProcedure = complete this for yourself Notations for list values need explanation. We write Val for the set of ordered sequences of values. We write [] for the empty sequence. Given a value v and a sequence s, v :: s denotes the sequence that is obtained by inserting v into the front of s. Given two sequences s 1 and s 2, we write s 1 @s 2 for the concatenation of s 1 and s 2. Environments (Env) map program variables (Var) to values. ρ Env = Var Val The semantics rules are defined inductively as inference rules. Rules for constant expressions: ρ () ρ true true ρ false false ρ n n The value of a variable can be found from the current environment: ρ x ρ(x) Arithmetic operations produce integers: ρ E 1 + E 2 n 1 + n 2 ρ E 1 - E 2 n 1 n 2 ρ E 1 * E 2 n 1 n 2 ρ E 1 / E 2 n 1 /n 2 n 2 0 Note that the semantics is defined only when E 1 and E 2 evaluate to integers and that E 1 / E 2 is undefined when the value of E 2 is 0 (division-by-zero). Comparison operators and negation produce boolean values: ρ E 1 = E 2 true n 1 = n 2 ρ E 1 = E 2 false n 1 n 2 ρ E 1 b 1 ρ E 2 b 2 ρ E 1 = E 2 true b 1 = b 2 ρ E 1 b 1 ρ E 2 b 2 ρ E 1 = E 2 false b 1 b 2 ρ E 1 < E 2 true n 1 < n 2 ρ E 1 < E 2 false n 1 n 2 2

ρ E true ρ not E false ρ E false ρ not E true Note that equality (E 1 = E 2 ) is undefined for list and function values. We deliberately choose to disallow list values; this is our choice for simplicity of the language semantics. However, comparing functional values is an undecidable problem and cannot be implemented in programming languages. Lists can be constructed in three ways: ρ nil [] ρ E 1 v ρ E 2 s ρ E 1 :: E 2 v :: s ρ E 1 s 1 ρ E 2 s 2 ρ E 1 @ E 2 s 1 @s 2 where v and s denote an arbitrary value and a list value, respectively. Other list operations are defined as follows: ρ E v :: s ρ head E v ρ E v :: s ρ tail E s ρ E [] ρ isnil E true ρ E v :: s ρ isnil E false We defined the semantics of conditional, let, letrec, proc, and call expressions in class as follows: ρ E 1 true ρ E 2 v ρ if E 1 then E 2 else E 3 v ρ E 1 false ρ E 3 v ρ if E 1 then E 2 else E 3 v ρ E 1 v 1 [x v 1 ]ρ E 2 v ρ let x = E 1 in E 2 v [f (f, x, E 1, ρ)]ρ E 2 v ρ letrec f(x) = E 1 in E 2 v ρ proc x E (x, E, ρ) ρ E 1 (x, E, ρ ) ρ E 2 v [x v]ρ E v ρ E 1 E 2 v ρ E 1 (f, x, E, ρ ) ρ E 2 v [x v, f (f, x, E, ρ )]ρ E v ρ E 1 E 2 v Complete this definition ρ letrec f(x) = E 1 and g(y) = E 2 in E 3 Complete this definition (when E 1 evaluates to mutually recursive functions) ρ E 1 E 2 The expression print E prints the value of E and then produces a unit value: ρ print E 3

The sequence expression E 1 ; E 2 evaluates E 1 and E 2 while ignoring the value of E 1 : ρ E 1 v 1 ρ E 2 v 2 ρ E 1 ; E 2 v 2 Language Implementation Now, let us implement ML. In OCaml, the syntax is defined as datatype as follows: type program = exp and exp = UNIT TRUE FALSE CONST of int VAR of var ADD of exp * exp SUB of exp * exp MUL of exp * exp DIV of exp * exp EQUAL of exp * exp LESS of exp * exp NOT of exp NIL CONS of exp * exp APPEND of exp * exp HEAD of exp TAIL of exp ISNIL of exp IF of exp * exp * exp LET of var * exp * exp LETREC of var * var * exp * exp LETMREC of (var * var * exp) * (var * var * exp) * exp PROC of var * exp CALL of exp * exp PRINT of exp SEQ of exp * exp and var = string The type of values and environments are defined as follows: type value = Unit Int of int Bool of bool List of value list Procedure of var * exp * env RecProcedure of var * var * exp * env 4

MRecProcedure of (* complete for yourself *) and env = (var * value) list Implement the function runml: runml : program -> value which takes a program, evaluates it, and produces its value. Whenever the semantics is undefined, raise exception UndefinedSemantics. Check your implementation by running the following example pro- Examples grams. 1. Evaluating the program let x = 1 in let f = proc (y) (x + y) in let x = 2 in let g = proc (y) (x + y) in (f 1) + (g 1) LET ("x", CONST 1, LET ("f", PROC ("y", ADD (VAR "x", VAR "y")), LET ("x", CONST 2, LET ("g", PROC ("y", ADD (VAR "x", VAR "y")), ADD (CALL (VAR "f", CONST 1), CALL (VAR "g", CONST 1)))))) should produce the value Int 5. 2. Evaluating the program letrec double(x) = if (x = 0) then 0 else (double (x-1) + 2 in (double 6) LETREC ("double", "x", IF (EQUAL (VAR "x", CONST 0), CONST 0, ADD (CALL (VAR "double", SUB (VAR "x", CONST 1)), CONST 2)), CALL (VAR "double", CONST 6)) should produce Int 12. 3. Evaluating the program 5

letrec even(x) = if (x = 0) then true else odd(x-1) odd(x) = if (x = 0) then false else even(x-1) in (even 13) LETMREC (("even", "x", IF (EQUAL (VAR "x", CONST 0), TRUE, CALL (VAR "odd", SUB (VAR "x", CONST 1)))), ("odd", "x", IF (EQUAL (VAR "x", CONST 0), FALSE, CALL (VAR "even", SUB (VAR "x", CONST 1)))), CALL (VAR "odd", CONST 13)) should produce Bool true. 4. Evaluating the program letrec factorial(x) = if (x = 0) then 1 else factorial(x-1) * x in letrec loop n = if (n = 0) then () else (print (factorial n); loop (n-1)) in (loop 10) LETREC ("factorial", "x", IF (EQUAL (VAR "x", CONST 0), CONST 1, MUL (CALL (VAR "factorial", SUB (VAR "x", CONST 1)), VAR "x")), LETREC ("loop", "n", IF (EQUAL (VAR "n", CONST 0), UNIT, SEQ (PRINT (CALL (VAR "factorial", VAR "n")), CALL (VAR "loop", SUB (VAR "n", CONST 1)))), CALL (VAR "loop", CONST 10))) should produce Unit after printing out the following lines: 3628800 362880 40320 5040 720 120 6

24 6 2 1 5. Evaluating the program letrec range(n) = if (n = 1) then (cons 1 nil) else n::(range (n-1)) in (range 10) LETREC ("range", "n", IF (EQUAL (VAR "n", CONST 1), CONS (CONST 1, NIL), CONS (VAR "n", CALL (VAR "range", SUB (VAR "n", CONST 1)))), CALL (VAR "range", CONST 10)) should produce List [Int 10; Int 9; Int 8; Int 7; Int 6; Int 5; Int 4; Int 3; Int 2; Int 1]. 6. Evaluating the program letrec reverse(l) = if (isnil l) then [] else (reverse (tl l)) @ (cons hd l) in (reverse (cons (1, cons (2, cons (3, nil))))) LETREC ("reverse", "l", IF (ISNIL (VAR "l"), NIL, APPEND (CALL (VAR "reverse", TAIL (VAR "l")), CONS (HEAD (VAR "l"), NIL))), CALL (VAR "reverse", CONS (CONST 1, CONS (CONST 2, CONS (CONST 3, NIL))))) should produce List [Int 3; Int 2; Int 1]. 7. An interesting fact in programming languages is that any recursive function can be defined in terms of non-recursive functions (i.e., letrec is syntactic sugar 1 in ML ). Consider the following function: let fix = proc (f) ((proc (x) f (proc (y) ((x x) y))) (proc (x) f (proc (y) ((x x) y)))) 1 https://en.wikipedia.org/wiki/syntactic_sugar 7

which is called fixed-point-combinator (or Z-combinator). 2 Note that fix is a non-recursive function, although its structure is complex and repetitive. Any recursive function definition of the form: letrec f(x) = <body of f> in... can be defined as follows using fix: let f = fix (proc (f) (proc (x) (<body of f>))) in... For example, the factorial program letrec f(x) = if (x = 0) then 1 else f(x-1) * x in (f 10) can be defined using fix: let fix = proc (f) ((proc (x) f (proc (y) ((x x) y))) (proc (x) f (proc (y) ((x x) y)))) in let f = fix (proc (f) (proc (x) (if (x = 0) then 1 else f(x-1) * x))) in (f 10) which is represented in our implementation as follows: LET ("fix", PROC ("f", CALL (PROC ("x", CALL (VAR "f", PROC ("y", CALL (CALL (VAR "x", VAR "x"), VAR "y")))), PROC ("x", CALL (VAR "f", PROC ("y", CALL (CALL (VAR "x", VAR "x"), VAR "y")))))), LET ("f", CALL (VAR "fix", PROC ("f", PROC ("x", IF (EQUAL (VAR "x", CONST 0), CONST 1, MUL (CALL (VAR "f", SUB (VAR "x", CONST 1)), VAR "x"))))), CALL (VAR "f", CONST 10))) Evaluating this program with your interpreter should produce Int 3628800. For another example, consider the function range defined above: in letrec range(n) = if (n = 1) then (cons 1 nil) else n::(range (n-1)) in (range 10) 2 https://en.wikipedia.org/wiki/fixed-point_combinator 8

We can translate it to a non-recursive version as follows: let fix = proc (f) ((proc (x) f (proc (y) ((x x) y))) (proc (x) f (proc (y) ((x x) y)))) in let f = fix (proc (range) (proc (n) (if (n = 1) then (cons 1 nil) else n::(range (n-1))))) in (f 10) In OCaml: LET ("fix", PROC ("f", CALL (PROC ("x", CALL (VAR "f", PROC ("y", CALL (CALL (VAR "x", VAR "x"), VAR "y")))), PROC ("x", CALL (VAR "f", PROC ("y", CALL (CALL (VAR "x", VAR "x"), VAR "y")))))), LET ("f", CALL (VAR "fix", PROC ("range", PROC ("n", IF (EQUAL (VAR "n", CONST 1), CONS (CONST 1, NIL), CONS (VAR "n", CALL (VAR "range", SUB (VAR "n", CONST 1))))))), CALL (VAR "f", CONST 10))) Evaluating this program should produce List [Int 10; Int 9; Int 8; Int 7; Int 6; Int 5; Int 4; Int 3; Int 2; Int 1]. 9