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

Similar documents
Homework 3 COSE212, Fall 2018

CMSC 330: Organization of Programming Languages

COSE212: Programming Languages. Lecture 0 Course Overview

Begin at the beginning

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

Functional Programming. Pure Functional Programming

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Homework 5 COSE212, Fall 2018

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

Programming Languages

Programming Languages

Problem Set CVO 103, Spring 2018

Programming Languages

CMSC 330, Fall 2013, Practice Problems 3

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

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

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

Recap: Functions as first-class values

Programming Languages

Metaprogramming assignment 3

Implemen'ng a Func'onal Language. A Basic Func2onal Language. Goal: illustrate and understand the features of the run-2me support

Variables and Bindings

Type Checking and Type Inference

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

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

The Substitution Model. Nate Foster Spring 2018

Fall Lecture 3 September 4. Stephen Brookes

CSCI-GA Scripting Languages

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

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

The Substitution Model

CMSC 330, Fall 2013, Practice Problem 3 Solutions

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

Lecture 12: Conditional Expressions and Local Binding

Programming Languages

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

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Programming Languages

Denotational Semantics. Domain Theory

Exercises on ML. Programming Languages. Chanseok Oh

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

CMSC 330: Organization of Programming Languages. Operational Semantics

CSE341, Spring 2013, Final Examination June 13, 2013

CSE 341 Section 5. Winter 2018

Lecture 3: Expressions

CSE505, Fall 2012, Midterm Examination October 30, 2012

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

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

Programming Languages

Programming Languages and Compilers (CS 421)

Lecture 15 CIS 341: COMPILERS

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

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

CITS3211 FUNCTIONAL PROGRAMMING. 13. Interpretation

CS 314 Principles of Programming Languages

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Introduction to OCaml

Programming Languages Fall 2014

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

L3 Programming September 19, OCaml Cheatsheet

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Principles of Programming Languages 2017W, Functional Programming

CS421 Spring 2014 Midterm 1

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

CS153: Compilers Lecture 14: Type Checking

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

Programming Languages

CS558 Programming Languages. Winter 2013 Lecture 3

So what does studying PL buy me?

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

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

Lecture 2: Big-Step Semantics

Programming Languages and Compilers (CS 421)

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

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

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Last class. CS Principles of Programming Languages. Introduction. Outline

Mutable References. Chapter 1

Comp 311: Sample Midterm Examination

CS558 Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Lecture 13 CIS 341: COMPILERS

COMP 105 Homework: Type Systems

A Brief Introduction to Standard ML

CS 11 Ocaml track: lecture 7

SML A F unctional Functional Language Language Lecture 19

Lecture08: Scope and Lexical Address

Example Scheme Function: equal

Abstract Interpretation

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

Transcription:

CVO103: Programming Languages Lecture 5 Design and Implementation of PLs (1) Expressions Hakjoo Oh 2018 Spring Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 1 / 23

Plan Part 1 (Preliminaries): inductive definition, basics of functional programming, recursive and higher-order programming Part 2 (Basic concepts): syntax, semantics, naming, binding, scoping, environment, interpreters, states, side-effects, store, reference, mutable variables, parameter passing Part 3 (Advanced concepts): type system, typing rules, type checking, soundness/completeness, automatic type inference, polymorphic type system, lambda calculus, program synthesis Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 2 / 23

Goal We will learn essential concepts of programming languages by designing and implementing a programming language, called ML--: Expressions Procedures States Types Design decisions of programming languages Expression/statement-oriented Static/dynamic scoping Eager/lazy evaluation Explicit/implicit reference Static/dynamic type system Sound/unsound type system Manual/automatic type inference... Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 3 / 23

Designing a Programming Language We need to specify syntax and semantics of the language: Syntax: how to write programs Semantics: the meaning of the programs Both are formally specified by inductive definitions. Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 4 / 23

Let: Our First Language Syntax P E E n x E + E E E iszero E if E then E else E let x = E in E read Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 5 / 23

Examples 1, 2, x, y 1+(2+3), x+1, x+(y-2) iszero 1, iszero (2-2), iszero (iszero 3) if iszero 1 then 2 else 3, if 1 then 2 else 3 let x = read in x + 1 let x = read in let y = 2 in if zero x then y else x Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 6 / 23

Values and Environments To define the semantics, we need to define values and environments. The set of values that the language manipulates: 1+(2+3) iszero 1, iszero (2-2) if zero 1 then 2 else 3 Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 7 / 23

Values and Environments To define the semantics, we need to define values and environments. The set of values that the language manipulates: 1+(2+3) iszero 1, iszero (2-2) if zero 1 then 2 else 3 An environment is a variable-value mapping, which is needed to evaluate expressions with variables: x, y x+1, x+(y-2) let x = read in let y = 2 in if zero x then y else x Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 7 / 23

Values and Environments In Let, the set of values includes integers and booleans: v Val = Z + Bool and an environment is a function from variables to values: ρ Env = Var Val Notations: []: the empty environment. [x v]ρ (or ρ[x v]): the extension of ρ where x is bound to v: { v if x = y ([x v]ρ)(y) = ρ(y) otherwise For simplicity, we write [x 1 v 1, x 2 v 2 ]ρ for the extension of ρ where x 1 is bound to v 1, x 2 to v 2 : [x 1 v 1, x 2 v 2 ]ρ = [x 1 v 1 ]([x 2 v 2 ]ρ) Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 8 / 23

Evaluation of Expressions Given an environment ρ, an expression e evaluates to a value v: ρ e v or does not evaluate to any value (i.e. e does not have semantics w.r.t ρ). [] 1 1 [x 1] x+1 2 [] read 3, [x 1] read 5 [x 0] let y = 2 in if iszero x then y else x 2 iszero (iszero 3) if 1 then 2 else 3 Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 9 / 23

Evaluation Rules ρ e v ρ n n ρ x ρ(x) ρ E 1 n 1 ρ E 2 n 2 ρ E 1 + E 2 n 1 + n 2 ρ E 1 n 1 ρ E 2 n 2 ρ E 1 E 2 n 1 n 2 ρ read n ρ E 0 ρ iszero E true ρ E n ρ iszero E false n 0 ρ 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 Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 10 / 23

Evaluation Rules More precise interpretation of the evaluation rules: The inference rules define a set S of triples (ρ, e, v). For readability, the triple was written by ρ e v in the rules. We say an expression e has semantics w.r.t. ρ iff there is a triple (ρ, e, v) S for some value v. That is, we say an expression e has semantics w.r.t. ρ iff we can derive ρ e v for some value v by applying the inference rules. We say an initial program e has semantics if [] e v for some v. Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 11 / 23

Examples: Arithmetic Expressions ρ n n ρ x ρ(x) ρ E 1 n 1 ρ E 2 n 2 ρ E 1 + E 2 n 1 + n 2 ρ E 1 n 1 ρ E 2 n 2 ρ E 1 E 2 n 1 n 2 When ρ = [i 1, v 5, x 10], ρ x 10 ρ 3 3 ρ v 5 ρ i 1 ρ x 3 7 ρ v i 4 ρ (x 3) (v i) 3 Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 12 / 23

Examples: Arithmetic Expressions Expression y 3 does not have semantics w.r.t the same ρ because ρ y 3 v cannot be derived for any value v. Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 13 / 23

Examples: Arithmetic Expressions Expression y 3 does not have semantics w.r.t the same ρ because ρ y 3 v cannot be derived for any value v. In ρ = [x true], the semantics of x + 1 is not defined because ρ x + 1 v cannot be derived for any v. Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 13 / 23

Examples: Conditional Expressions ρ E 0 ρ iszero E true ρ E 1 true ρ E 2 v ρ if E 1 then E 2 else E 3 v ρ E n ρ iszero E false n 0 ρ E 1 false ρ E 3 v ρ if E 1 then E 2 else E 3 v When ρ = [x 33, y 22], ρ x 33 ρ 11 11 ρ x 11 22 ρ y 22 ρ 4 4 ρ iszero (x 11) false ρ y 4 18 ρ if iszero (x 11) then y 2 else y 4 18 Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 14 / 23

Examples: Let Expression A let expression creates a new variable binding in the environment: ρ E 1 v 1 [x v 1 ]ρ E 2 v ρ let x = E 1 in E 2 v Example: [] 5 5 [x 5] x 5 [x 5] 3 3 [x 5] x 3 2 [] let x = 5 in x 3 2 Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 15 / 23

Let Expression Let expressions can be nested: let z = 5 in let x = 3 in let y = x - 1 in let x = 4 in z - (x-y) let x = 7 in let y = 2 in let y = let x = x - 1 in x - y in (x-8)-y Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 16 / 23

Implementation of the Language Syntax definition in OCaml: type program = exp and exp = CONST of int VAR of var ADD of exp * exp SUB of exp * exp READ ISZERO of exp IF of exp * exp * exp LET of var * exp * exp and var = string Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 17 / 23

Example let x = 7 in let y = 2 in let y = let x = x - 1 in x - y in (x-8)-y LET ("x", CONST 7, LET ("y", CONST 2, LET ("y", LET ("x", SUB(VAR "x", CONST 1), SUB (VAR "x", VAR "y")), SUB (SUB (VAR "x", CONST 8), VAR "y")))) Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 18 / 23

Values and Environments Values: type value = Int of int Bool of bool Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 19 / 23

Values and Environments Values: type value = Int of int Bool of bool Environments: type env = (var * value) list let empty_env = [] let extend_env (x,v) e = (x,v)::e let rec apply_env x e = match e with [] -> raise (Failure ("variable " ^ x ^ " not found")) (y,v)::tl -> if x = y then v else apply_env x tl Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 19 / 23

Evaluation Rules let rec eval : exp -> env -> value =fun exp env -> match exp with CONST n -> Int n VAR x -> apply_env env x ADD (e1,e2) -> let v1 = eval e1 env in let v2 = eval e2 env in (match v1,v2 with Int n1, Int n2 -> Int (n1 + n2) _ -> raise (Failure "Type Error: non-numeric values")) SUB (e1,e2) -> let v1 = eval e1 env in let v2 = eval e2 env in (match v1,v2 with Int n1, Int n2 -> Int (n1 - n2) _ -> raise (Failure "Type Error: non-numeric values"))... Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 20 / 23

Implementation: Semantics let rec eval : exp -> env -> value =fun exp env ->... READ -> Int (read_int()) ISZERO e -> (match eval e env with Int n when n = 0 -> Bool true _ -> Bool false) IF (e1,e2,e3) -> (match eval e1 env with Bool true -> eval e2 env Bool false -> eval e3 env _ -> raise (Failure "Type Error: condition must be Bool type") LET (x,e1,e2) -> let v1 = eval e1 env in eval e2 (extend_env (x,v1) env) Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 21 / 23

Interpreter let run : program -> value =fun pgm -> eval pgm empty_env Examples: # let e1 = LET ("x", CONST 1, ADD (VAR "x", CONST 2));; val e1 : exp = LET ("x", CONST 1, ADD (VAR "x", CONST 2)) # run e1;; - : value = Int 3 Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 22 / 23

Summary We have designed and implemented our first programming language: P E E n x E + E E E iszero E if E then E else E let x = E in E key concepts: syntax, semantics, interpreter Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 23 / 23