Meanings of syntax. Norman Ramsey Geoffrey Mainland. COMP 105 Programming Languages Tufts University. January 26, 2015

Similar documents
Review: Concrete syntax for Impcore

CS 152: Programming Languages

Programming Languages: the ultimate user interface. Welcome to CS301. Why programming languages? Some language features

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

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

Denotational Semantics. Domain Theory

The Substitution Model

CMSC 330: Organization of Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

The Substitution Model. Nate Foster Spring 2018

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

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

Operational semantics questions and answers

COMP 105 Homework: Type Systems

Principles of Programming Languages

Functional Programming. Pure Functional Programming

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

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

Recap: Functions as first-class values

Flang typechecker Due: February 27, 2015

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

CS558 Programming Languages. Winter 2013 Lecture 3

Case Study: Undefined Variables

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

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

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Abstracting Definitional Interpreters

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

Chapter 1. Fundamentals of Higher Order Programming

Lecture 2: SML Basics

Begin at the beginning

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

CS 314 Principles of Programming Languages

Variables and Bindings

Discussion 12 The MCE (solutions)

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

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

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

Environments

Static Checking and Type Systems

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Datatype declarations

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

CMSC 330: Organization of Programming Languages. Operational Semantics

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

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

CS 360 Programming Languages Interpreters

1 Introduction. 3 Syntax

Mutable References. Chapter 1

CPSC 311, 2010W1 Midterm Exam #2

From Syntactic Sugar to the Syntactic Meth Lab:

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.

Application: Programming Language Semantics

Semantic Analysis. Lecture 9. February 7, 2018

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

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

CSCC24 Functional Programming Scheme Part 2

Comp 311: Sample Midterm Examination

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

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

Lecture 12: Conditional Expressions and Local Binding

Operational Semantics 1 / 13

Programming Languages

Programming Language Concepts, cs2104 Lecture 04 ( )

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages

Variables. Substitution

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

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

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

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Programs as data first-order functional language type checking

Compilation 2013 Semantic Analysis

CS558 Programming Languages

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

Racket: Modules, Contracts, Languages

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

Lecture #23: Conversion and Type Inference

A Small Interpreted Language

Homework 3 COSE212, Fall 2018

Compiler construction

Formal Syntax and Semantics of Programming Languages

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Functional Programming. Pure Functional Languages

Calculating Correct Compilers

Programming Languages and Compilers (CS 421)

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

Lecture 3: Recursion; Structural Induction

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

C311 Lab #3 Representation Independence: Representation Independent Interpreters

An Explicit-Continuation Metacircular Evaluator

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Logic-Flow Analysis of Higher-Order Programs

An Introduction to Scheme

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

Formal Semantics of Programming Languages

CS4215 Programming Language Implementation. Martin Henz

Transcription:

Meanings of syntax Norman Ramsey Geoffrey Mainland COMP 105 Programming Languages Tufts University January 26, 2015 (COMP 105) Meanings of syntax January 26, 2015 1 / 33

What is the meaning of a while loop? (COMP 105) Meanings of syntax January 26, 2015 2 / 33

How do we represent a while loop? Code that has a condition and a body. The condition can be any expression. The body can be any expression, which is executed for its side effects. (COMP 105) Meanings of syntax January 26, 2015 3 / 33

Meanings, part I: Names Environment associates each variable with one value Written ρ = {x 1 n 1,...x k n k }, associates variable x i with value n i. Environment is finite map, aka partial function x domρ x is defined in environment ρ ρ(x) the value of x in environment ρ ρ{x v} extends/modifies environment ρ to map x to v (COMP 105) Meanings of syntax January 26, 2015 4 / 33

Environments in C, abstractly An abstract type: typedef struct Valenv *Valenv; Valenv mkvalenv(namelist vars, Valuelist vals); int isvalbound(name name, Valenv env); Value fetchval(name name, Valenv env); void bindval(name name, Value val, Valenv env); (COMP 105) Meanings of syntax January 26, 2015 5 / 33

Implementing environments Uses pair of lists. Example: after (val x 1) (val y 2) (val z 3) globals vars z y x global environment: vals 3 2 1 Environment costs can drive language design (e.g., Exercise 22). (COMP 105) Meanings of syntax January 26, 2015 6 / 33

Concrete syntax for Impcore Definitions and expressions, as strings def ::= (val x exp) exp (define f (formals) e) exp ::= integer-literal variable-name (set x exp) (if exp1 exp2 exp3) (while exp1 exp2) (begin exp1... expn) (op exp1... expn) op ::= function-name primitive-name (COMP 105) Meanings of syntax January 26, 2015 7 / 33

Abstract syntax for Impcore Definitions and expressions as data structures Exp = LITERAL (Value) VAR (Name) SET (Name name, Exp exp) IFX (Exp cond, Exp true, Exp false) WHILEX (Exp cond, Exp exp) BEGIN (Explist) APPLY (Name name, Explist actuals) One kind of application for both user-defined and primitive functions. (COMP 105) Meanings of syntax January 26, 2015 8 / 33

Abstract syntax in C typedef struct Exp *Exp; typedef enum { LITERAL, VAR, SET, IFX, WHILEX, BEGIN, APPLY } Expalt; /* which alternative is it? */ struct Exp { // only two fields: alt and u! Expalt alt; union { Value literal; Name var; struct { Name name; Exp exp; } set; struct { Exp cond; Exp true; Exp false; } ifx; struct { Exp cond; Exp exp; } whilex; Explist begin; struct { Name name; Explist actuals; } apply; } u; }; (COMP 105) Meanings of syntax January 26, 2015 9 / 33

Analysis and examples Example AST for (f x (* y 3)) (Example uses Explist) Example Ast for (define abs (x) (if (< x 0) (- 0 x) x)) (Example uses Namelist) (COMP 105) Meanings of syntax January 26, 2015 10 / 33

Syntax and environments combine to produce meaning Trick question: What s the value of (* y 3)? OK, what s its meaning? (COMP 105) Meanings of syntax January 26, 2015 11 / 33

Meanings, part II: expressions Expression evaluation Expressions are evaluated in an environment to produce values. An environment consists of formal, global, and function environments. Heart of the interpreter structural recursion on Exps environment provides meanings of names (COMP 105) Meanings of syntax January 26, 2015 12 / 33

How do we explain evaluation? Answer three questions 1 What are the expressions? 2 What are the values? 3 What are the rules for turning expressions into values? Combined: operational semantics (COMP 105) Meanings of syntax January 26, 2015 13 / 33

Operational semantics Specify executions of programs on an abstract machine Typical uses Very concise and precise language definition Direct guide to implementor Prove things like environments can be kept on a stack (COMP 105) Meanings of syntax January 26, 2015 14 / 33

Operational Semantics Loosely speaking, an interpreter More precisely, formal rules for interpretation Set of expressions, also called terms Set of values Full state of abstract machine (e.g., e,ξ,φ,ρ, expression + 3 environments) Well specified initial state Transition rules for the abstract machine Good programs end in an accepting state Bad programs get stuck ( go wrong ) (COMP 105) Meanings of syntax January 26, 2015 15 / 33

Operational semantics for Impcore You ve seen expressions: ASTs All values are integers. State e,ξ,φ,ρ is e Expression being evaluated ξ Values of global variables φ Definitions of functions ρ Values of formal parameters Rules form a proof system for judgment: e,ξ,φ,ρ v,ξ,φ,ρ (This is a big-step judgment form.) (COMP 105) Meanings of syntax January 26, 2015 16 / 33

Impcore semantics: Literals literal(v),ξ,φ,ρ v,ξ,φ,ρ Literal (COMP 105) Meanings of syntax January 26, 2015 17 / 33

Impcore semantics: Variables Parameters hide global variables. x domρ var(x),ξ,φ,ρ ρ(x),ξ,φ,ρ FormalVar x / domρ x domξ var(x),ξ,φ,ρ ξ(x),ξ,φ,ρ GlobalVar (COMP 105) Meanings of syntax January 26, 2015 18 / 33

Impcore semantics: Assignment x domρ e,ξ,φ,ρ v,ξ,φ,ρ set(x,e),ξ,φ,ρ v,ξ,φ,ρ {x v} FormalAssign x / domρ x domξ e,ξ,φ,ρ v,ξ,φ,ρ set(x,e),ξ,φ,ρ v,ξ {x v},φ,ρ GlobalAssign (COMP 105) Meanings of syntax January 26, 2015 19 / 33

Rules of semantics play two roles Code: Each rule implemented in interpreter Math: Compose rules to make proofs Interpreter succeeds if and only if a proof exists (COMP 105) Meanings of syntax January 26, 2015 20 / 33

Code: Cases to implement evaluation rules VAR find binding for variable and use value SET rebind variable in formals or globals IFX (recursively) evaluate condition, then t or f WHILEX (recursively) evaluate condition, body BEGIN (recursively) evaluate each Exp of body APPLY look up function in functions built-in PRIMITIVE do by cases USERDEF function use arg values to build formals env, recursively evaluate fun body (COMP 105) Meanings of syntax January 26, 2015 21 / 33

Code to implement evaluation Value eval(exp *e, ξ, φ, ρ) { switch(e->alt) { case LITERAL: return e->u.literal; case VAR:... /* look up in ρ and ξ */ case SET:... /* modify ρ or ξ */ case IFX:... case WHILEX:... case BEGIN:... case APPLY: if (!isfunbound(e->u.apply.name, φ)) error("call to undefined function %n", e->u.apply.name); f = fetchfun(e->u.apply.name, φ);... /* user fun or primitive */ } } (COMP 105) Meanings of syntax January 26, 2015 22 / 33

Impcore semantics Variables x domρ var(x),ξ,φ,ρ ρ(x),ξ,φ,ρ FormalVar x / domρ x domξ var(x),ξ,φ,ρ ξ(x),ξ,φ,ρ GlobalVar (COMP 105) Meanings of syntax January 26, 2015 23 / 33

Evaluation Variables To evaluate x, find x in ξ or ρ, get value Conceptually, one environment, composed of formals+globals Composition implemented in eval, not in Env type: case VAR: if (isvalbound(e->u.var, formals)) return fetchval(e->u.var, formals); else if (isvalbound(e->u.var, globals)) return fetchval(e->u.var, globals); else error("unbound variable %n", e->u.var); (COMP 105) Meanings of syntax January 26, 2015 24 / 33

Impcore semantics Assignment x domρ e,ξ,φ,ρ v,ξ,φ,ρ set(x,e),ξ,φ,ρ v,ξ,φ,ρ {x v} FormalAssign x / domρ x domξ e,ξ,φ,ρ v,ξ,φ,ρ set(x,e),ξ,φ,ρ v,ξ {x v},φ,ρ GlobalAssign (COMP 105) Meanings of syntax January 26, 2015 25 / 33

Evaluation Assignment (set x e) means change ρ or ξ, depending on where x is bound. case SET: { Value v = eval(e->u.set.exp,globals,functions,formals); if(isvalbound(e->u.set.name, formals)) bindval(e->u.set.name, v, formals); else if(isvalbound(e->u.set.name, globals)) bindval(e->u.set.name, v, globals); else error("set: unbound variable %n", e->u.set.name); return v; } (COMP 105) Meanings of syntax January 26, 2015 26 / 33

Impcore semantics Application ApplyUser φ(f) = user( x 1,...,x n,e) x 1,...,x n all distinct e 1,ξ 0,φ,ρ 0 v 1,ξ 1,φ,ρ 1 e 2,ξ 1,φ,ρ 1 v 2,ξ 2,φ,ρ 2. e n,ξ n 1,φ,ρ n 1 v n,ξ n,φ,ρ n e,ξ n,φ,{x 1 v 1,...,x n v n } v,ξ,φ,ρ apply(f,e 1,...,e n ),ξ 0,φ,ρ 0 v,ξ,φ,ρ n (COMP 105) Meanings of syntax January 26, 2015 27 / 33

Evaluation Application 1 Find function in old environment f = fetchfun(e->u.apply.name, functions); 2 Evaluate actuals to get list of values (also in old ρ) vl = evallist(e->u.apply.actuals, globals, functions, formals); N.B. actuals evaluated in the current environment 3 Make new env, binding formals to actuals new_formals = mkvalenv(f.u.userdef.formals, vl); 4 Evaluate body in new environment return eval(f.u.userdef.body, globals, functions, new_formals); (COMP 105) Meanings of syntax January 26, 2015 28 / 33

Application binding parameters Actuals evaluated in the current environment Result is Valuelist half of an environment (reason why pair of lists, not list of pairs) Formals are bound to actuals in a new environment mkvalenv builds an environment from two lists (COMP 105) Meanings of syntax January 26, 2015 29 / 33

Return to math Use rules to create syntactic proofs Valid proof is a derivation D Compositionality again: Rule with no premises above the line? A derivation by itself Rule with premises? Build derivations from smaller derivations (COMP 105) Meanings of syntax January 26, 2015 30 / 33

Build derivation from conclusion up, left to right In Impcore, (+ 2 3) evaluates to 5 in an environment where φ(+) = primitive(+). To construct the derivation: 1 Start with apply(+, literal(2), literal(3)), ξ, φ, ρ on left side of bottom 2 Find applicable rule ApplyAdd and work up 3 Construct derivations for literal(2) and literal(3) recursively (Notice that ξ and ρ don t change.) 4 Finish with 5,ξ,φ,ρ on right side of bottom (COMP 105) Meanings of syntax January 26, 2015 31 / 33

Build derivation from conclusion up, left to right In Impcore, (+ 2 3) evaluates to 5 in an environment where φ(+) = primitive(+). Literal ApplyAdd literal(2),ξ,φ,ρ 2,ξ,φ,ρ To construct the derivation: literal(3),ξ,φ,ρ 3,ξ,φ,ρ Literal apply(+,literal(2),literal(3)),ξ,φ,ρ 5,ξ,φ,ρ 1 Start with apply(+, literal(2), literal(3)), ξ, φ, ρ on left side of bottom 2 Find applicable rule ApplyAdd and work up 3 Construct derivations for literal(2) and literal(3) recursively (Notice that ξ and ρ don t change.) 4 Finish with 5,ξ,φ,ρ on right side of bottom (COMP 105) Meanings of syntax January 26, 2015 31 / 33

Build derivation from conclusion up, left to right In Impcore, (+ 2 3) evaluates to 5 in an environment where φ(+) = primitive(+). Literal ApplyAdd literal(2),ξ,φ,ρ 2,ξ,φ,ρ To construct the derivation: literal(3),ξ,φ,ρ 3,ξ,φ,ρ Literal apply(+,literal(2),literal(3)),ξ,φ,ρ 5,ξ,φ,ρ 1 Start with apply(+, literal(2), literal(3)), ξ, φ, ρ on left side of bottom 2 Find applicable rule ApplyAdd and work up 3 Construct derivations for literal(2) and literal(3) recursively (Notice that ξ and ρ don t change.) 4 Finish with 5,ξ,φ,ρ on right side of bottom (COMP 105) Meanings of syntax January 26, 2015 31 / 33

Build derivation from conclusion up, left to right In Impcore, (+ 2 3) evaluates to 5 in an environment where φ(+) = primitive(+). Literal ApplyAdd literal(2),ξ,φ,ρ 2,ξ,φ,ρ To construct the derivation: literal(3),ξ,φ,ρ 3,ξ,φ,ρ Literal apply(+,literal(2),literal(3)),ξ,φ,ρ 5,ξ,φ,ρ 1 Start with apply(+, literal(2), literal(3)), ξ, φ, ρ on left side of bottom 2 Find applicable rule ApplyAdd and work up 3 Construct derivations for literal(2) and literal(3) recursively (Notice that ξ and ρ don t change.) 4 Finish with 5,ξ,φ,ρ on right side of bottom (COMP 105) Meanings of syntax January 26, 2015 31 / 33

Build derivation from conclusion up, left to right In Impcore, (+ 2 3) evaluates to 5 in an environment where φ(+) = primitive(+). Literal ApplyAdd literal(2),ξ,φ,ρ 2,ξ,φ,ρ To construct the derivation: literal(3),ξ,φ,ρ 3,ξ,φ,ρ Literal apply(+,literal(2),literal(3)),ξ,φ,ρ 5,ξ,φ,ρ 1 Start with apply(+, literal(2), literal(3)), ξ, φ, ρ on left side of bottom 2 Find applicable rule ApplyAdd and work up 3 Construct derivations for literal(2) and literal(3) recursively (Notice that ξ and ρ don t change.) 4 Finish with 5,ξ,φ,ρ on right side of bottom (COMP 105) Meanings of syntax January 26, 2015 31 / 33

Build derivation from conclusion up, left to right In Impcore, (+ 2 3) evaluates to 5 in an environment where φ(+) = primitive(+). Literal ApplyAdd literal(2),ξ,φ,ρ 2,ξ,φ,ρ To construct the derivation: literal(3),ξ,φ,ρ 3,ξ,φ,ρ Literal apply(+,literal(2),literal(3)),ξ,φ,ρ 5,ξ,φ,ρ 1 Start with apply(+, literal(2), literal(3)), ξ, φ, ρ on left side of bottom 2 Find applicable rule ApplyAdd and work up 3 Construct derivations for literal(2) and literal(3) recursively (Notice that ξ and ρ don t change.) 4 Finish with 5,ξ,φ,ρ on right side of bottom A syntactic proof (derivation) is a data structure (COMP 105) Meanings of syntax January 26, 2015 31 / 33

Things to notice about Impcore Lots of environments: global variables functions parameters local variables? More environments = more name spaces more complexity Typical of many programming languages. (COMP 105) Meanings of syntax January 26, 2015 32 / 33

Questions to remember Abstract syntax: what are the terms? Values: what do terms evaluate to? Environments: what can names stand for? Evaluation rules: how to evaluate terms? Initial basis (primitives+): what s built in? (COMP 105) Meanings of syntax January 26, 2015 33 / 33