CSE 505: Concepts of Programming Languages

Similar documents
CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

CSE505, Fall 2012, Midterm Examination October 30, 2012

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

CSE 505: Concepts of Programming Languages

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

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.

Programming Languages

Fall Recursion and induction. Stephen Brookes. Lecture 4

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

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

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

The Substitution Model

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

CSCI-GA Scripting Languages

Lecture 3: Recursion; Structural Induction

Introduction to SML Getting Started

Part VI. Imperative Functional Programming

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

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

Chapter 3 (part 3) Describing Syntax and Semantics

The Substitution Model. Nate Foster Spring 2018

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

CSE 341: Programming Languages

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

Lecture Notes on Program Equivalence

Operational Semantics. One-Slide Summary. Lecture Outline

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

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

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

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

Introduction to Axiomatic Semantics

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

1 Introduction. 3 Syntax

CSC 7101: Programming Language Structures 1. Operational Semantics. Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6. Operational vs.

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Lectures 20, 21: Axiomatic Semantics

An Annotated Language

Semantics of programming languages

Semantics of programming languages

Semantics of programming languages

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

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

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Lecture 6: Sequential Sorting

In Our Last Exciting Episode

COS 320. Compiling Techniques

Chapter 3. Describing Syntax and Semantics ISBN

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

COMP 382: Reasoning about algorithms

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

Supplementary Notes on Abstract Syntax

Warmup Problem. Translate the following sentence from English into Propositional Logic. I want to eat ice cream even though I am on a diet.

Agenda. We ve done Growth of functions Asymptotic Notations (O, o,,!, ) Now Binary search as an example Recurrence relations, solving them

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

Operational Semantics of Cool

CMSC330 Fall 2014 Midterm 2 Solutions

Programming Languages

At build time, not application time. Types serve as statically checkable invariants.

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Lecture 2: Big-Step Semantics

What does my program mean?

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

Chapter 3. Describing Syntax and Semantics

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

Semantics and Concurrence Module on Semantic of Programming Languages

CSE 505: Programming Languages. Lecture 10 Types. Zach Tatlock Winter 2015

Checks and Balances - Constraint Solving without Surprises in Object-Constraint Programming Languages: Full Formal Development

Lecture Notes on Induction and Recursion

Semantics and Concurrence Module on Semantic of Programming Languages

MPRI course 2-4 Functional programming languages Exercises

Supplementary Notes on Exceptions

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

A CRASH COURSE IN SEMANTICS

Fall Lecture 3 September 4. Stephen Brookes

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Introduction to OCaml

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Modules, Structs, Hashes, and Operational Semantics

Programming Languages Third Edition

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

Lecture 2: SML Basics

Fundamental Concepts. Chapter 1

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

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

3.7 Denotational Semantics

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

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

CMSC 330: Organization of Programming Languages

Lecture 3.4: Recursive Algorithms

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

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

Syntax vs. semantics. Detour: Natural deduction. Syntax vs. semantics (cont d) Syntax = grammatical structure Semantics = underlying meaning

Lecture 15 CIS 341: COMPILERS

Metaprogramming assignment 3

Transcription:

CSE 505: Concepts of Programming Languages Dan Grossman Fall 2009 Lecture 4 Proofs about Operational Semantics; Pseudo-Denotational Semantics Dan Grossman CSE505 Fall 2009, Lecture 4 1

This lecture Continue last lecture, including detailed proofs (and some wrong approaches) of two theorems How to prove them Why these theorems are interesting The idea behind denotational semantics via translation to ML Dan Grossman CSE505 Fall 2009, Lecture 4 2

Proofs Write out proofs by hand for: while 1 skip diverges Key point: Must get induction hypothesis just right not too strong (false) or too weak (proof doesn t go through) No negatives is preserved by evaluation Can define a program property via judgments and prove it holds after every step Inverting assumed derivations gives you necessary facts for smaller expressions/statements (e.g., the while case) Dan Grossman CSE505 Fall 2009, Lecture 4 3

Motivation of non-negatives While no negatives preserved boils to down to properties of blue-+ and blue-*, writing out the whole proof ensures our language has no mistakes or bad interactions. The theorem is false if we have: Overly flexible rules, e.g.: H ; c c An unsafe language like C: H(x) = {c 0,..., c n 1 } H ; e c c n H ; x[e] := e H ; s Dan Grossman CSE505 Fall 2009, Lecture 4 4

Even more general proofs to come We defined the semantics. Given our semantics, we established properties of programs and sets of programs. More interesting is having multiple semantics for what program states are they equivalent? (For what notion of equivalence?) Or having a more abstract semantics (e.g., a type system) and asking if it is preserved under evaluation. (If e has type τ and e becomes e, does e have type τ?) But first a one-lecture detour to denotational semantics. Dan Grossman CSE505 Fall 2009, Lecture 4 5

A different approach Operational semantics defines an interpreter, from abstract syntax to abstract syntax. Metalanguage is inference rules (slides) or Caml (interp.ml). Denotational semantics defines a compiler (translater), from abstract syntax to a different language with known semantics. Target language is math, but we ll make it Caml for now. Metalanguage is math or Caml (we ll show both). Dan Grossman CSE505 Fall 2009, Lecture 4 6

The basic idea A heap is a math/ml function from strings to integers: string int An expression denotes a math/ml function from heaps to integers. den(e) : (string int) int A statement denotes a math/ml function from heaps to heaps. den(s) : (string int) (string int) Now just define den in our metalanguage (math or ML), inductively over the source language. Dan Grossman CSE505 Fall 2009, Lecture 4 7

Expressions den(e) : (string int) int den(c) = fun h -> c den(x) = fun h -> h x den(e 1 + e 2 ) = fun h -> (den(e 1 ) h) + (den(e 2 ) h) den(e 1 e 2 ) = fun h -> (den(e 1 ) h) * (den(e 2 ) h) In plus (and times) case, two ambiguities : + from source language or target language? Translate abstract + to Caml +, ignoring overflow (!) when do we denote e 1 and e 2? Not a focus of the metalanguage. At compile time. Dan Grossman CSE505 Fall 2009, Lecture 4 8

Switching metalanguage With Caml as our metalanguage, ambiguities go away. But it s harder to distinguish mentally between target and meta. If denote in function body, then source is around at run time. After translation, should be able to remove the definition of the abstract syntax. ML doesn t have such a feature, but the point is we no longer need the abstract syntax. (See denote.ml.) Dan Grossman CSE505 Fall 2009, Lecture 4 9

Statements, w/o while den(s) : (string int) (string int) den(skip) = fun h -> h den(x := e) = fun h -> (fun v -> if x=v then den(e) h else h v) den(s 1 ; s 2 ) = fun h -> den(s 2 ) (den(s 1 ) h) den(if e s 1 s 2 ) = fun h -> if den(e) h > 0 then den(s 1 ) h else den(s 2 ) h Same ambiguities; same answers. See denote.ml. Dan Grossman CSE505 Fall 2009, Lecture 4 10

While den(while e s) = While(e,s) -> let rec f h = let d1=denote_exp e in if (den(e) h)>0 let d2=denote_stmt s in then f (den(s) h) let rec f h = else h in if (d1 h)>0 f then f (d2 h) else h in f The function denoting a while statement is inherently recursive! Good thing our target language has recursive functions! Dan Grossman CSE505 Fall 2009, Lecture 4 11

Finishing the story let denote_prog s = let d = denote_stmt s in fun () -> (d (fun x -> 0)) "ans" Compile-time: let x = denote_prog (parse file). Run-time: print_int (x ()). In-between: We have a Caml program using only functions, variables, ifs, constants, +, *, >, etc. Dan Grossman CSE505 Fall 2009, Lecture 4 12

The real story For real denotational semantics, target language is math (And we write [[s]] instead of den(s)) Example: [[x := e]][[h]] = [[H]][x [[e]][[h]]] There are two major problems, both due to while: 1. Math functions do not diverge, so no function denotes while 1 skip. 2. The denotation of loops cannot be circular. Dan Grossman CSE505 Fall 2009, Lecture 4 13

The elevator version; ignorable for 505 For (1), we lift the semantic domains to include a special. den(s) : (string int) ((string int) ). Have to change meaning of den(s 2 ) den(s1) appropriately. For (2), we use while e s to define a (meta)function f that given a lifted heap-transformer X produces a lifted heap-transformer X : If den(e)(den(h)) = 0, then den(h). Else den(s) X. Now let den(while e s) be the least fixed-point of f. An hour of math to prove the least fixed-point exists. Another hour to prove it s the limit of starting with and applying f over and over (i.e., any number of loop iterations). Keywords: monotonic functions, complete partial orders, Knaster-Tarski theorem Dan Grossman CSE505 Fall 2009, Lecture 4 14

Where we are Have seen operational and denotational semantics Connection to interpreters and compilers Useful for rigorous definitions and proving properties Next: Equivalence of semantics Crucial for compiler writers Crucial for code maintainers Then: Leave IMP behind and consider functions But first: Will any of this help write an O/S? Dan Grossman CSE505 Fall 2009, Lecture 4 15