A Functional Evaluation Model

Similar documents
To figure this out we need a more precise understanding of how ML works

Implemen'ng OCaml in OCaml

Lecture 2: SML Basics

Lecture 13 CIS 341: COMPILERS

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

CMSC 330: Organization of Programming Languages

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

The Substitution Model

CMSC 330: Organization of Programming Languages. Lambda Calculus

CA Compiler Construction

Variables. Substitution

Basic concepts. Chapter Toplevel loop

CMSC 330: Organization of Programming Languages. Lambda Calculus

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CSE-321: Assignment 8 (100 points)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

Functional Programming. Pure Functional Programming

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

The Substitution Model. Nate Foster Spring 2018

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

To figure this out we need a more precise understanding of how ML works

CS153: Compilers Lecture 15: Local Optimization

CSE 341 Section 5. Winter 2018

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CS558 Programming Languages

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

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

Formal Systems and their Applications

Lecture 3: Recursion; Structural Induction

Programming Languages and Compilers (CS 421)

CSE 505: Concepts of Programming Languages

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Type Checking and Type Inference

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

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

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Programming Languages and Compilers (CS 421)

Semantic Analysis. Lecture 9. February 7, 2018

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

CS422 - Programming Language Design

Programming Paradigms

(Refer Slide Time: 4:00)

Pure Lambda Calculus. Lecture 17

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

A Func'onal Introduc'on. COS 326 David Walker Princeton University

Intro. Scheme Basics. scm> 5 5. scm>

Parsing II Top-down parsing. Comp 412

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

CS 11 Haskell track: lecture 1

CSE 341: Programming Languages

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

Introduction to the Lambda Calculus

Assignment 4: Evaluation in the E Machine

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

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

CMSC 330: Organization of Programming Languages

Intro to Haskell Notes: Part 5

Supplementary Notes on Abstract Syntax

CS558 Programming Languages. Winter 2013 Lecture 3

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Recap: Functions as first-class values

Programming Language Concepts, cs2104 Lecture 04 ( )

An introduction introduction to functional functional programming programming using usin Haskell

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

CSCI-GA Scripting Languages

Lambda Calculus. Lecture 4 CS /26/10

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

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

COP 3402 Systems Software Syntax Analysis (Parser)

CSE 12 Abstract Syntax Trees

61A Lecture 2. Friday, August 28, 2015

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Lambda Calculus. Variables and Functions. cs3723 1

15 150: Principles of Functional Programming. More about Higher-Order Functions

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Going beyond propositional logic

Lambda Calculus.

SML A F unctional Functional Language Language Lecture 19

LECTURE 3. Compiler Phases

CS 360: Programming Languages Lecture 12: More Haskell

The SPL Programming Language Reference Manual

CMSC 330: Organization of Programming Languages. Operational Semantics

Transcription:

A Functional Evaluation Model COS 326 Andrew W. Appel Princeton University slides copyright 2013-2015 David Walker and Andrew W. Appel

A Functional Evaluation Model In order to be able to write a program, you have to have a solid grasp of how a programming language works. We often call the definition of how a programming language works its semantics. There are many kinds of programming language semantics. In this lecture, we will look at O Caml s call-by-value evaluation: First, informally, giving program rewrite rules by example Second, using code, by specifying an OCaml interpreter in OCaml Third, more formally, using logical inference rules In each case, we are specifying what is known as OCaml's operational semantics 2

O CAML BASICS: CORE EXPRESSION EVALUATION

Evaluation Execution of an OCaml expression produces a value and may have some effect (eg: it may raise an exception, print a string, read a file, or store a value in an array) A lot of OCaml expressions have no effect they are pure they produce a value and do nothing more the pure expressions are the easiest kinds of expressions to reason about We will focus on evaluation of pure expressions

Evaluation of Pure Expressions Given an expression e, we write: e --> v to state that expression e evaluates to value v Note that "e --> v" is not itself a program -- it is some notation that we use talk about how programs work

Evaluation of Pure Expressions Given an expression e, we write: to state that expression e evaluates to value v Some examples: e --> v

Evaluation of Pure Expressions Given an expression e, we write: to state that expression e evaluates to value v Some examples: e --> v 1 + 2

Evaluation of Pure Expressions Given an expression e, we write: to state that expression e evaluates to value v Some examples: e --> v 1 + 2 --> 3

Evaluation of Pure Expressions Given an expression e, we write: to state that expression e evaluates to value v Some examples: e --> v 1 + 2 --> 3 2

Evaluation of Pure Expressions Given an expression e, we write: to state that expression e evaluates to value v Some examples: e --> v 1 + 2 --> 3 2 --> 2 values step to values

Evaluation of Pure Expressions Given an expression e, we write: to state that expression e evaluates to value v Some examples: e --> v 1 + 2 --> 3 2 --> 2 int_to_string 5 --> "5"

Evaluation of Pure Expressions More generally, we say expression e (partly) evaluates to expression e : e --> e

Evaluation of Pure Expressions More generally, we say expression e (partly) evaluates to expression e : e --> e Evaluation is complete when e is a value In general, I ll use the letter v to represent an arbitrary value The letter e represents an arbitrary expression Concrete numbers, strings, characters, etc. are all values, as are: tuples, where the fields are values records, where the fields are values datatype constructors applied to a value functions

Evaluation of Pure Expressions Some expressions (all the interesting ones!) take many steps to evaluate them: (2 * 3) + (7 * 5)

Evaluation of Pure Expressions Some expressions (all the interesting ones!) take many steps to evaluate them: (2 * 3) + (7 * 5) --> 6 + (7 * 5)

Evaluation of Pure Expressions Some expressions (all the interesting ones!) take many steps to evaluate them: (2 * 3) + (7 * 5) --> 6 + (7 * 5) --> 6 + 35

Evaluation of Pure Expressions Some expressions (all the interesting ones!) take many steps to evaluate them: (2 * 3) + (7 * 5) --> 6 + (7 * 5) --> 6 + 35 --> 41

Evaluation of Pure Expressions Some expressions do not compute a value and it is not obvious how to proceed: "hello" + 1 -->???? A strongly typed language rules out a lot of nonsensical expressions that compute no value, like the one above Other expressions compute no value but raise an exception: 7 / 0 --> raise Divide_by_zero Still others simply fail to terminate

Let Expressions: Evaluate using Substitution This must be already a value let x = 30 in let y = 12 in x+y let y = 12 in 30 + y --> 30 + 12 --> 42 19

Let Expressions: Evaluate using Substitution let x = 30 in let y = 12 in x+y let y = 12 in 30 + y --> This must be already a value otherwise, first evaluate inside the bound expression let x = 15*2 in let y = 12 in x+y --> let x = 30 in let y = 12 in x+y 30 + 12 --> 42 20

Informal Evaluation Model To evaluate a function call f a first evaluate f until we get a function value (fun x -> e) then evaluate a until we get an argument value v then substitute v for x in e, the function body then evaluate the resulting expression. this is why we say O Caml is call by value (let f = (fun x -> x + 1) in f) (30+11) --> (fun x -> x + 1) (30 + 11) --> (fun x -> x + 1) 41 --> 41 + 1 --> 42

Another example: Informal Evaluation Model let add x y = x+y in let inc = add 1 in let dec = add (-1) in dec(inc 42) 22

Recall the syntactic sugar: Informal Evaluation Model let add = fun x -> (fun y -> x+y) in let inc = add 1 in let dec = add (-1) in dec(inc 42) 23

Informal Evaluation Model Then we use the let rule we substitute the value for add: let add = fun x -> (fun y -> x+y) in let inc = add 1 in let dec = add (-1) in dec(inc 42) functions are values --> let inc = (fun x -> (fun y -> x+y)) 1 in let dec = (fun x -> (fun y -> x+y)) -1 in dec(inc 42) 24

Informal Evaluation Model let inc = (fun x -> (fun y -> x+y)) 1 in let dec = (fun x -> (fun y -> x+y)) (-1) in dec(inc 42) --> not a value; must reduce before substituting for inc let inc = fun y -> 1+y in let dec = (fun x -> (fun y -> x+y)) (-1) in dec(inc 42) 25

Informal Evaluation Model now a value let inc = fun y -> 1+y in let dec = (fun x -> (fun y -> x+y)) (-1) in dec(inc 42) --> let dec = (fun x -> (fun y -> x+y)) (-1) in dec((fun y -> 1+y) 42) 26

Informal Evaluation Model Next: simplify dec s definition using the function-call rule. let dec = (fun x -> (fun y -> x+y)) (-1) in dec((fun y -> 1+y) 42) --> now a value let dec = fun y -> -1+y in dec((fun y -> 1+y) 42) 27

Informal Evaluation Model And we can use the let-rule now to substitute dec: let dec = fun y -> -1+y in dec((fun y -> 1+y) 42) --> (fun y -> -1+y) ((fun y -> 1+y) 42) 28

Informal Evaluation Model Now we can t yet apply the first function because the argument is not yet a value it s a function call. So we need to use the function-call rule to simplify it to a value: (fun y -> -1+y) ((fun y -> 1+y) 42) --> (fun y -> -1+y) (1+42) --> (fun y -> -1+y) 43 --> -1+43 --> 42 29

Variable Renaming Consider the following OCaml code: let x = 30 in let y = 12 in x+y;; Does this evaluate any differently than the following? let a = 30 in let b = 12 in a+b;; 30

Renaming A basic principle of programs is that systematically changing the names of variables shouldn t cause the program to behave any differently it should evaluate to the same thing. let x = 30 in let y = 12 in x+y;; But we do have to be careful about systematic change. let a = 30 in let a = 12 in a+a;; Systematic change of variable names is called alpha-conversion. 31

Substitution Wait a minute, how do we evaluate this using the letrule? If we substitute 30 for a naively, then we get: let a = 30 in let a = 12 in a+a --> let 30 = 12 in 30+30 Which makes no sense at all! Besides, Ocaml returns 24 not 60. What went wrong with our informal model? 32

Scope and Modularity Lexically scoped (a.k.a. statically scoped) variables have a simple rule: the nearest enclosing let in the code defines the variable. So when we write: let a = 30 in let a = 12 in a+a;; we know that the a+a corresponds to 12+12 as opposed to 30+30 or even weirder 30+12. 33

A Revised Let-Rule: To evaluate let x = e 1 in e 2 : First, evaluate e 1 to a value v. Then substitute v for the corresponding uses of x in e 2. Then evaluate the resulting expression. let a = 30 in let a = 12 in a+a --> let a = 12 in a+a --> 12+12 --> 24 This a doesn t correspond to the uses of a below. So when we substitute 30 for it, it doesn t change anything. 34

Scope and Modularity But what does corresponding uses mean? Consider: let a = 30 in let a = (let a = 3 in a*4) in a+a;; 35

Abstract Syntax Trees We can view a program as a tree the parentheses and precedence rules of the language help determine the structure of the tree. let a = 30 in let a = (let a = 3 in a*4) in a+a;; let a 30 let == a let + (let a = (30) in (let a = (let a = (3) in (a*4)) in (a+a))) a 3 * a 4 a a 36

Binding Occurrences An occurrence of a variable where we are defining it via let is said to be a binding occurrence of the variable. let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 37

Free Occurrences A non-binding occurrence of a variable is said to be a free variable. That is a use of a variable as opposed to a definition. let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 38

Abstract Syntax Trees Given a free variable occurrence, we can find where it is bound by let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 39

Abstract Syntax Trees crawling up the tree to the nearest enclosing let let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 40

Abstract Syntax Trees crawling up the tree to the nearest enclosing let let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 41

Abstract Syntax Trees crawling up the tree to the nearest enclosing let let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 42

Abstract Syntax Trees and see if the let binds the variable if so, we ve found the nearest enclosing definition. If not, we keep going up. let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 43

Abstract Syntax Trees Now we can also systematically rename the variables so that it s not so confusing. Systematic renaming is called alphaconversion let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 44

Abstract Syntax Trees Start with a let, and pick a fresh variable name, say x let let a = 30 in let a = (let a = 3 in a*4) in a+a;; a 30 let a let + a a 3 * a a 4 45

Abstract Syntax Trees Rename the binding occurrence from a to x. let let x = 30 in let a = (let a = 3 in a*4) in a+a;; x 30 let a let + a a 3 * a a 4 46

Abstract Syntax Trees Then rename all of the free occurrences of the variables that this let binds. let let x = 30 in let a = (let a = 3 in a*4) in a+a;; x 30 let a let + a a 3 * a a 4 47

Abstract Syntax Trees There are none in this case! let These a s are bound by this let. let x = 30 in let a = (let a = 3 in a*4) in a+a;; x 30 let a let + a a 3 * a a 4 48

Abstract Syntax Trees There are none in this case! let This a is bound by this let let x = 30 in let a = (let a = 3 in a*4) in a+a;; x 30 let a let + a a 3 * a a 4 49

Abstract Syntax Trees Let s do another let, renaming a to y. let let x = 30 in let a = (let a = 3 in a*4) in a+a;; x 30 let a let + a a 3 * a a 4 50

Abstract Syntax Trees Let s do another let, renaming a to y. let let x = 30 in let y = (let a = 3 in a*4) in y+y;; x 30 let y let + y a 3 * y a 4 51

Abstract Syntax Trees And if we rename the other let to z : let let x = 30 in let y = (let z = 3 in z*4) in y+y;; x 30 let y let + y a 3 * y a 4 52

Abstract Syntax Trees And if we rename the other let to z : let let x = 30 in let y = (let z = 3 in z*4) in y+y;; x 30 let y let + y z 3 * y z 4 53

AN O CAML DEFINITION OF O CAML EVALUATION

Implementing an Interpreter text file containing program as a sequence of characters let x = 3 in x + x Num 6 Parsing data structure representing result of evaluation Pretty Printing data structure representing program Let ( x, Num 3, Binop(Plus, Var x, Var x )) Evaluation 6 the data type and evaluator tell us a lot about program semantics text file/stdout containing with formatted output

Making These Ideas Precise We can define a datatype for simple OCaml expressions: type variable = string ;; type op = Plus Minus Times ;; type exp = Int_e of int Op_e of exp * op * exp Var_e of variable Let_e of variable * exp * exp ;; 56

Making These Ideas Precise We can define a datatype for simple OCaml expressions: type variable = string ;; type op = Plus Minus Times ;; type exp = Int_e of int Op_e of exp * op * exp Var_e of variable Let_e of variable * exp * exp ;; let three = Int_e 3 ;; let three_plus_one = Op_e (Int_e 1, Plus, Int_e 3) ;; 57

Making These Ideas Precise We can represent the OCaml program: let x = 30 in let y = (let z = 3 in z*4) in y+y;; as an exp value: Let_e( x, Int_e 30, Let_e( y, Let_e( z, Int_e 3, Op_e(Var_e z, Times, Int_e 4)), Op_e(Var_e y, Plus, Var_e y ) 58

Making These Ideas Precise Notice how this reflects the tree : Let_e( x,int_e 30, Let_e( y,let_e( z,int_e 3, Op_e(Var_e z, Times, Int_e 4)), Op_e(Var_e y, Plus, Var_e y ) let x 30 let y let + z 3 * y y z 4 59

type exp = Free versus Bound Variables Int_e of int Op_e of exp * op * exp Var_e of variable Let_e of variable * exp * exp This is a free occurrence of a variable 60

type exp = Free versus Bound Variables Int_e of int Op_e of exp * op * exp Var_e of variable Let_e of variable * exp * exp This is a free occurrence of a variable This is a binding occurrence of a variable 61