The Substitution Model. Nate Foster Spring 2018

Similar documents
The Substitution Model

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

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

CMSC 330: Organization of Programming Languages

The Environment Model. Nate Foster Spring 2018

The Environment Model

CS4215 Programming Language Implementation. Martin Henz

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

CMSC 330: Organization of Programming Languages. Operational Semantics

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

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

Introduction to OCaml

Programming Languages

Programming Languages Fall 2014

Programs as data first-order functional language type checking

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

Functions. Nate Foster Spring 2018

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

Parsing. CS321 Programming Languages Ozyegin University

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Lecture 15 CIS 341: COMPILERS

CS 11 Ocaml track: lecture 7

1 Introduction. 3 Syntax

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Operational Semantics 1 / 13

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

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

CMSC 330: Organization of Programming Languages

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

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

CS 242. Fundamentals. Reading: See last slide

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

CSCI-GA Scripting Languages

Syntax. A. Bellaachia Page: 1

Modules, Structs, Hashes, and Operational Semantics

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

CS558 Programming Languages

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.

Lecture Compiler Middle-End

Denotational Semantics. Domain Theory

Lecture 3: Recursion; Structural Induction

Programming Languages Third Edition

Proofs are Programs. Prof. Clarkson Fall Today s music: Proof by Paul Simon

Type Checking and Type Inference

Lecture 2: Big-Step Semantics

CMSC 330: Organization of Programming Languages. Lambda Calculus

Programming Languages Fall 2013

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

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

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

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

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

Semantic Analysis. Lecture 9. February 7, 2018

Balanced Trees. Nate Foster Spring 2018

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

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

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

CS 6110 S14 Lecture 1 Introduction 24 January 2014

A Functional Evaluation Model

Recap: Functions as first-class values

Behavioral Equivalence

Behavioral Equivalence

CSE505, Fall 2012, Midterm Examination October 30, 2012

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Functional Programming. Introduction To Cool

CMSC 330: Organization of Programming Languages. Context Free Grammars

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

In Our Last Exciting Episode

CMSC 330: Organization of Programming Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Proofs are Programs. Prof. Clarkson Fall Today s music: Two Sides to Every Story by Dyan Cannon and Willie Nelson

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

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

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

CS 360 Programming Languages Interpreters

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CMSC 330: Organization of Programming Languages

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

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

Variables. Substitution

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CPSC W1 University of British Columbia

Review: Concrete syntax for Impcore

CS153: Compilers Lecture 14: Type Checking

Transcription:

The Substitution Model Nate Foster Spring 2018

Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on single steps parser and lexer (in lab) Today: Formal syntax: BNF Formal dynamic semantics: small-step, substitution model Formal static semantics

Substitution (* [subst e v x] is e{v/x}, that is, * [e] with [v] substituted for [x]. *) let rec subst e v x = match e with Var y -> if x=y then v else e Int n -> Int n Add(el,er) -> Add(subst el v x, subst er v x) Let(y,ebind,ebody) -> let ebind' = subst ebind v x in if x=y then Let(y, ebind', ebody) else Let(y, ebind', subst ebody v x)

Step let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1 + n2) Add(Int n1, e2) -> Add (Int n1, step e2) Add(e1, e2) -> Add (step e1, e2) Var _ -> failwith "Unbound variable" Let(x, Int n, e2) -> subst e2 (Int n) x Let(x, e1, e2) -> Let (x, step e1, e2)

FORMAL SYNTAX

Abstract syntax of expression lang. e ::= x i e1+e2 let x = e1 in e2 e, x, i: meta-variables that stand for pieces of syntax e: expressions x: program variables, aka identifiers i: integer constants, aka literals ::= and are meta-syntax: used to describe syntax of language notation is called Backus-Naur Form (BNF) from its use by Backus and Naur in their definition of Algol-60

Backus and Naur John Backus (1924-2007) ACM Turing Award Winner 1977 For profound, influential, and lasting contributions to the design of practical high-level programming systems Peter Naur (1928-2016) ACM Turing Award Winner 2005 For fundamental contributions to programming language design

Abstract syntax of expr. lang. e ::= x i e1+e2 let x = e1 in e2 Note resemblance of BNF to AST type: type expr = Var of string Int of int Add of expr * expr Let of string * expr * expr

Extended with Booleans e ::= x i b e1 + e2 e1 && e2 let x = e1 in e2 if e1 then e2 else e3 v ::= i b

FORMAL DYNAMIC SEMANTICS

Dynamic semantics Defined as a binary relation: e --> e' Read as e takes a single step to e' e.g., (5+2)+0 --> 7+0 Expressions continue to step until they reach a value e.g., (5+2)+0 --> 7+0 --> 7 Values are a syntactic subset of expressions: v ::= i b

Dynamic semantics Reflexive transitive closure of --> is written -->* e -->* e' read as e multisteps to e' e.g., (5+2)+0 -->* (5+2)+0 (5+2)+0 -->* 7+0 (5+2)+0 -->* 7 This style of definition is called a small-step semantics: based on taking single small steps

Dynamic semantics of expr. lang. e1 + e2 --> e1' + e2 if e1 --> e1' v1 + e2 --> v1 + e2' if e2 --> e2' v1 + v2 --> i if i is the result of primitive operation v1+v2

Dynamic semantics of expr. lang. let x = e1 in e2 --> let x = e1' in e2 if e1 --> e1' let x = v1 in e2 --> e2{v1/x} recall: read e2{v1/x} as e2 with v1 substituted for x (as we defined last lecture and implemented in subst) so we call this the substitution model of evaluation

Evaluation models Small-step substitution model: Substitute value for variable in body of let expression And in body of function, since let x = e1 in e2 behaves the same as (fun x -> e2) e1 Inefficient implementation: have to do too much substitution at run time Not really what OCaml does Good mental model for evaluation Big-step environment model: Keep a data structure that binds variables to values At the heart of what OCaml really does

Dynamic semantics of expr. lang. if e1 then e2 else e3 --> if e1' then e2 else e3 if e1 --> e1' if true then e2 else e3 --> e2 if false then e2 else e3 --> e3

Dynamic semantics of expr. lang. Values and variables do not single step: v -/-> (values are already done evaluating) x -/-> (should have been substituted away) But they do multistep (because they can take 0 steps): v -->* v x -->* x

Scaling up to OCaml Read notes on website: full dynamic semantics for core OCaml: e ::= x e1 e2 fun x -> e i e1 + e2 (e1, e2) fst e1 snd e2 Left e Right e match e with Left x -> e1 Right y -> e2 let x = e1 in e2 Missing: other built-in types, records, lists, options, declarations, patterns in function arguments and let bindings, if expressions, recursion

FORMAL STATIC SEMANTICS

Static semantics We can have nonsensical expressions: 5 + false if 5 then true else 0 Need static semantics (type checking) to rule those out...

if expressions [from lec 2] Syntax: if e1 then e2 else e3 Type checking: if e1 has type bool and e2 has type t and e3 has type t then if e1 then e2 else e3 has type t

Static semantics Defined as a ternary relation: T - e : t Read as in typing context T, expression e has type t Turnstile - can be read as "proves" or "shows" You're already used to e : t, because utop uses that notation Typing context is a dictionary mapping variable names to types

Static semantics e.g., x:int - x+2 : int x:int,y:int - x<y : bool - 5+2 : int

Static semantics of expr. lang. T - i : int T - b : bool

Static semantics of expr. lang. T - e1 + e2 : int if T - e1 : int and T - e2 : int T - e1 && e2 : bool if T - e1 : bool and T - e2 : bool

Static semantics of expr. lang. T - if e1 then e2 else e3 : t if T - e1 : bool and T - e2 : t and T - e3 : t T - let x:t1 = e1 in e2 : t2 if T - e1 : t1 and T, x:t1 - e2 : t2 T, x:t - x : t To avoid need for type inference, require type annotation here

Purpose of type system Ensure type safety: well-typed programs don't get stuck: haven't reached a value, and unable to evaluate further Lemmas: Progress: if e:t, then either e is a value or e can take a step. Preservation: if e:t, and if e takes a step to e', then e':t. Type safety = progress + preservation Proving type safety is a fun part of CS 4110

Q: Why bother doing proofs about programming languages? They are almost always boring if the definitions are right. A: The definitions are almost always wrong. Anonymous

Interpreter for expr. lang. See interp3.ml in code for this lecture 1. Type-checks expression, then 2. Evaluates expression

Upcoming events [Wednesday] A3 due [Thursday] A4 out