CMSC 330: Organization of Programming Languages Operational Semantics.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
ISBN Chapter 3 Describing Syntax and Semantics.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 355 – Programming Languages
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Chapter 2 A Simple Compiler
Describing Syntax and Semantics
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 363 Comparative Programming Languages Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
CMSC 330: Organization of Programming Languages
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Albert Gatt LIN3021 Formal Semantics Lecture 4. In this lecture Compositionality in Natural Langauge revisited: The role of types The typed lambda calculus.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Syntax and Semantics CIS 331 Syntax: the form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions,
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
Semantics (1).
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
Operational Semantics of Scheme
Programming Languages and Compilers (CS 421)
Mini Language Interpreter Programming Languages (CS 550)
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
ADTs, Grammars, Parsing, Tree traversals
FP Foundations, Scheme In Text: Chapter 14.
6.001 SICP Variations on a Scheme
Presentation transcript:

CMSC 330: Organization of Programming Languages Operational Semantics

CMSC 3302 Review of PL Syntax –Regular expressions –Finite automata –Context-free grammars Semantics –Operational semantics –Lambda calculus Implementation –Concurrency –Generics –Argument passing –Scoping –Exceptions

CMSC 3303 Roadmap: Compilation of program Grammar: S  A A  id = E E  T+E | T T  P * T | P P  id | n | (E)‏ Program: X = Compilation: Push X Push 2 Push 3 Add top 2 Assign top to second Postorder: postfix X = (Rec Des) Parsing: ast = + 32 X Program semantics: What does program mean ? Is this correct? What is program supposed to do?

CMSC 3304 Operational Semantics There are several different kinds of semantics –Denotational: A program is a mathematical function –Axiomatic: Develop a logical proof of a program Give predicates that hold when a program (or part) is executed We will briefly look at operational semantics –A program is defined by how you execute it on a mathematical model of a machine We will look at a subset of OCaml as an example

CMSC 3305 Roadmap: Semantics of a program Grammar: S  A A  id = E E  T+E | T T  P * T | P P  id | n | (E)‏ Program: X = Compilation: Push X Push 2 Push 3 Add top 2 Assign top to second Postorder: postfix X = (Rec Des) Parsing: ast = + 32 X value Program semantics: = + 32 X

CMSC 3306 Evaluation We’re going to define a relation E → v –This means “expression E evaluates to v” We’ll use grammars to describe each of these –One to describe abstract syntax trees E –One to describe OCaml values v

CMSC 3307 OCaml Programs E ::= x | n | true | false | [] | if E then E else E | fun x = E | E E –x stands for any identifier –n stands for any integer –true and false stand for the two boolean values –[] is the empty list –Using = in fun instead of -> to avoid some confusion later

CMSC 3308 Values v ::= n | true | false | [] | v::v –n is an integer (not a string corresp. to an integer)‏ Same idea for true, false, [] –v1::v2 is the pair with v1 and v2 This will be used to build up lists Notice: nothing yet requires v2 to be a list –Important: Be sure to understand the difference between program text S and mathematical objects v. E.g., the text 3 evaluates to the mathematical number 3 –To help, we’ll use different colors and italics This is usually not done, and it’s up to the reader to remember which is which

CMSC 3309 Grammars for Trees We’re just using grammars to describe trees E ::= x | n | true | false | [] | if E then E else E | fun x = E | E E v ::= n | true | false | [] | v::v type value = Val_Num of int | Val_Bool of bool | Val_Nil | Val_Pair of value * value type ast = Id of string | Num of int | Bool of bool | Nil | If of ast * ast * ast | Fun of string * ast | App of ast * ast Given a program, we saw last time how to convert it to an ast (e.g., recursive descent parsing)‏ Goal: For any ast, we want an operational rule to obtain a value that represents the execution of ast

CMSC Operational Semantics Rules Each basic entity evaluates to the corresponding value n → n true → true false → false [] → []

CMSC Operational Semantics Rules (cont’d)‏ How about built-in functions? –We’re applying the + function Ignore currying for the moment, and pretend we have multi- argument functions –On the right-hand side, we’re computing the mathematical sum; the left-hand side is source code –But what about + (+ 3 4) 5 ? We need recursion + n m → n + m

CMSC Rules with Hypotheses To evaluate + E 1 E 2, we need to evaluate E 1, then evaluate E 2, then add the results –This is call-by-value –This is a “natural deduction” style rule –It says that if the hypotheses above the line hold, then the conclusion below the line holds i.e., if E 1 executes to value n and if E 2 executes to value m, then + E 1 E 2 executes to value n+m + E 1 E 2 → n + m E 1 → nE 2 → m

CMSC Error Cases Because we wrote n, m in the hypothesis, we mean that they must be integers But what if E 1 and E 2 aren’t integers? –E.g., what if we write + false true ? –It can be parsed, but we can’t execute it We will have no rule that covers such a case –Runtime error! + E 1 E 2 → n + m E 1 → nE 2 → m

CMSC Trees of Semantic Rules When we apply rules to an expression, we actually get a tree –Corresponds to the recursive evaluation procedure For example: + (+ 3 4 ) 5 + ( + 3 4) 5 → (+ 3 4) →5 → 3 →4 →

CMSC Rules for If Examples –if false then 3 else 4 → 4 –if true then 3 else 4 → 3 Notice that only one branch is evaluated if E 1 then E 2 else E 3 → E 1 → trueE 2 → v v if E 1 then E 2 else E 3 → E 1 → falseE 3 → v v

CMSC Rule for :: So :: allocates a pair in memory Are there any conditions on E 1 and E 2 ? –No! We will allow E 2 to be anything –OCaml’s type system will disallow non-lists :: E 1 E 2 → E 1 → v 1 E 2 → v 2 v 1 ::v 2

CMSC Rules for Identifiers Let’s assume for now that the only identifiers are parameter names –Ex. (fun x = + x 3) 4 –When we see x in the body, we need to look it up –So we need to keep some sort of environment This will be a map from identifiers to values

CMSC Semantics with Environments Extend rules to the form A ⊢ E → v –Means in environment A, the program text E evaluates to v Notation: –We write ∅ for the empty environment –We write A(x) for the value that x maps to in A –We write A, x ↦ v for the same environment as A, except x is now v might be a new (replacement) mapping for x –We write A, A' for the environment with the bindings of A' added to and overriding the bindings of A

CMSC Rules for Identifiers and Application To evaluate a user-defined function applied to an argument: –Evaluate the argument (call-by-value)‏ –Evaluate the function body in an environment in which the formal parameter is bound to the actual argument –Return the result A ⊢ x → A(x)‏ A ⊢ ( (fun x = E 1 ) E 2 ) → v' A ⊢ E 2 → vA, x ↦ v ⊢ E 1 → v' no hypothesis means “in all cases”

CMSC Operational Semantics Rules A ⊢ (fun x = e 1 ) e 2 → v' A ⊢ e 2 → vA, x ↦ v ⊢ e 1 → v' A ⊢ x → v‏ A ⊢ + e 1 e 2 → n + m A ⊢ e 1 → nA ⊢ e 2 → m A ⊢ n → n SInt SAdd SFun SVar x ↦ v‏ ∈ A‏

CMSC Example: (fun x = + x 3) 4 → ? ∅ ⊢ (fun x = + x 3) 4 → ∅ ⊢ 4 →x ↦ 4 ⊢ + x 3 → 4 x ↦ 4 ⊢ x → 4 x ↦ 4 ⊢ 3 → SFun SAdd SInt SVar

CMSC Nested Functions This works for cases of nested functions –...as long as they are fully applied But what about the true higher-order cases? –Passing functions as arguments, and returning functions as results –We need closures to handle this case –...and a closure was just a function and an environment –We already have notation around for writing both parts

CMSC Exercises (fun x = (fun y = + x y)) 5 7

CMSC Closures Formally, we add closures (A, λx.E) to values –A is the environment in which the closure was created –x is the parameter name –E is the source code for the body λx will be discussed next time. Means a binding of x in E. v ::= n | true | false | [] | v::v | (A, λx.E)‏

CMSC Revised Rule for Lambda To evaluate a function definition, create a closure when the function is created –Notice that we don’t look inside the function body A ⊢ fun x = E → (A, λx.E)‏

CMSC Revised Rule for Application To apply something to an argument: –Evaluate it to produce a closure –Evaluate the argument (call-by-value)‏ –Evaluate the body of the closure, in The current environment, extended with the closure’s environment, extended with the binding for the parameter A ⊢ (E 1 E 2 ) → A ⊢ E 1 → (A', λx.E)‏ A, A', x ↦ v ⊢ E → v' A ⊢ E 2 → v v'

CMSC Example Let = (fun x = (fun y = + x y)) 3 ∅ ⊢ (fun x = (fun y = + x y)) 3 → ∅ ⊢ (fun x = (fun y = + x y)) → (x ↦ 3, λy.(+ x y))‏ ∅ ⊢ 3 → 3 ( ∅, λx.(fun y = + x y))‏ x ↦ 3 ⊢ (fun y = + x y) → (x ↦ 3, λy.(+ x y))‏

CMSC Example (cont’d)‏ ∅ ⊢ ( 4 ) → ∅ ⊢ →(x ↦ 3, λy.(+ x y))‏ ∅ ⊢ 4 → 4 x ↦ 3,y ↦ 4 ⊢ (+ x y) → 7 7

CMSC Why Did We Do This? (cont’d)‏ Operational semantics are useful for –Describing languages Not just OCaml! It’s pretty hard to describe a big language like C or Java, but we can at least describe the core components of the language –Giving a precise specification of how they work Look in any language standard – they tend to be vague in many places and leave things undefined –Reasoning about programs We can actually prove that programs do something or don’t do something, because we have a precise definition of how they work