Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.

Slides:



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

Substitution & Evaluation Order cos 441 David Walker.
Semantics Static semantics Dynamic semantics attribute grammars
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
1 Chapter 2: Simplification, Optimization and Implication Where we learn more fun things we can do with constraints.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Introduction to Computability Theory
1 Basic abstract interpretation theory. 2 The general idea §a semantics l any definition style, from a denotational definition to a detailed interpreter.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Axiomatic Semantics Dr. M Al-Mulhem ICS
Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Midterm & Concept Review CS 510, Fall 2002 David Walker.
1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Introduction to CS Theory Lecture 3 – Regular Languages Piotr Faliszewski
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
A Theory of Hygienic Macros David Herman, Mitchell Wand Northeastern University.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter 3 Part II Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CMSC 330: Organization of Programming Languages Operational Semantics.
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
1 Interactive Computer Theorem Proving CS294-9 October 19, 2006 Adam Chlipala UC Berkeley Lecture 9: Beyond Primitive Recursion.
Operational Semantics of Scheme
Chapter 2: Lambda Calculus
CS 550 Programming Languages Jeremy Johnson
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Tim Sheard Oregon Graduate Institute
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Programming Languages and Compilers (CS 421)
Continuations and Compilation
The Metacircular Evaluator
Semantics In Text: Chapter 3.
CSE S. Tanimoto Lambda Calculus
Program correctness Axiomatic semantics
Programming Languages and Compilers (CS 421)
Presentation transcript:

Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005

2Cs583 FSC Winter 2005 Assignments Projects. By now you should have a project in mind. If you haven’t already done so please write down a 1 page description and send it to me. Projects are Tentatively due Thursday March 17, 2005 Projects should include well commented code A number of example runs, with results A short writeup – several pages – describing the problem, your approach, your code, and any drawbacks you see in your solution. Homework. There will be no more homeworks. Work on your project instead.

3Cs583 FSC Winter 2005 Acknowledgements This set of slides is an adaption of Chapter 6 from Walid Taha’s thesis Multi-stage Programming, Its Theory and Applications.

4Cs583 FSC Winter 2005 Map for todays lecture Reduction semantics for lambda calculus Rewrite rules Confluence Beta-v Extensions for meta ml Rules for code Object level reductions Intensional analysis

5Cs583 FSC Winter 2005 Reduction Semantics Rules for reducing expressions in a language. Not all expressions are reducible. Usually expressed as an (un ordered) set of rewrite rules. Good for reasoning about terms in the language when the context of the term is vague or unknown (local reasoning).

6Cs583 FSC Winter 2005 Expressions and Values Expressions denote Commands Computations Language constructs that have “work left to be done” Values denote Answers Acceptable results Expressions that require “no further evaluation” Values are a subset of Expressions Syntactic view of the world

7Cs583 FSC Winter 2005 Example -  calculus Terms t ::= x | t t | (t,t) |  x. t | i | #i t Values v ::= x |  x. t | (v,v) | i Need rules for eliminating applications and variables In extensions need rules for performing primitive operations, like (+) (so called  -rules) Projecting from tuples, (#1, #2) etc. Eta reductions

8Cs583 FSC Winter 2005 Rewrite rules Call by Name  rule Call by Value  rule Projection rules

9Cs583 FSC Winter 2005 Contexts Expressions with a single “hole” Denoted C[e] C is the context e is the hole Example: If C[_] = f x + _ Then C[e] = f x + e

10Cs583 FSC Winter 2005 Desired Property of reduction semantics For all contexts If e1 rewrites by R to e2 Then C[e1] rewrites by R to C[e2] Allows “local” rewrites, and “local” reasoning, regardless of the wider enclosing context C.

11Cs583 FSC Winter 2005 Coherence & Confluence Term rewriting systems are non-deterministic. Any rule that applies can be used at any time. Applying the rules could get different results. Coherence – any sequence of rewrites that leads to a ground value leads to the same ground value. Confluence – Applicable rules can be applied in any order, and this does not affect the set of possible results. I.e. one never goes down a “dead-end” Confluence implies Coherence

12Cs583 FSC Winter 2005 Example run (power 1 ) run * ~ > run run 22 * 1 * esc twice run  2 

13Cs583 FSC Winter 2005 Results We want coherence Its often easier to show confluence Confluence implies coherence Coherence says if we apply rules and we get to a value, then we’ll always get the same value. Importance for a deterministic language Allows local reasoning to be valid

14Cs583 FSC Winter 2005 Extending to MetaML Terms e ::= x | e e |  x. e | i | | ~e | run e Values v ::= x |  x. e | i | Can we add the following rules to  v ?

15Cs583 FSC Winter 2005 What goes wrong? Beta screws up levels Every escape is attached to some bracket Escape can only appear inside bracket. But consider: ~x) ~ > >

16Cs583 FSC Winter 2005 What goes wrong 2? Beta conflicts with intensional analysis I.e. if we allow programmers to pattern match against code. And if we allow beta under brackets Then we lose coherence fun isbeta = true | isbeta _ = false isbeta x) (fn y => y) >

17Cs583 FSC Winter 2005 Fixing things up To fix screwing up levels make the bracket and escape rule like  v, I.e. force the rule only to apply when the thing in brackets is a value. Question - What is an appropriate notion of value?

18Cs583 FSC Winter 2005 Fixing things up 2 When beta screws up intensional analysis Fix 1. Don’t allow intensional analysis, such as pattern matching against code Fix 2. Don’t allow beta inside brackets, such as the code optimizations: safe-beta, safe-eta, and let- normalization. To be sound, we must make one of these choices. MetaML makes neither. MetaML is unsound. The “feature” function allows the programmer to decide which way this should work.

19Cs583 FSC Winter 2005 Expression families e 0 ::= v | x | e 0 e 0 | | run e 0 Terms at level 0 e n+ ::= i | x | e n+ e n+ | x.e n+ | | ~ e n | run e n+ Terms inside n brackets v ::= i | x.e 0 | values

20Cs583 FSC Winter 2005 Rules

21Cs583 FSC Winter 2005 Applying the rules fun pow1 n x = if n=0 then 1 else times x (pow1 (n-1) x); fun pow2 n x = if n=0 then else ; Prove by induction on n that : run (pow2 n ) = pow1 n x

22Cs583 FSC Winter 2005 N=0 run (pow2 n ) = pow1 n x run (if n=0 then else ) = run = 1 = pow1 0 x

23Cs583 FSC Winter 2005 N <> 0 run (pow2 n ) = pow1 n x run (if n=0 then else ~(pow2 (n-1) ) >) = run( ~(pow2 (n-1) x) >) = Can’t use run to erase brackets because of escapes inside. times (run ) (run(pow2 (n-1) )) = times x (pow1 (n-1) x) = pow1 n x

24Cs583 FSC Winter 2005 Are the rules correct? How do we know the rules are correct? That requires answering what are the semantics of staged programs. Two approaches Syntactic approach Denotational approach Syntactic approach can be given by an operational, or big-step semantics. Would like the reduction semantics to be sound with the big-step semantics

25Cs583 FSC Winter 2005 Big-step semantics

26Cs583 FSC Winter 2005

27Cs583 FSC Winter 2005

28Cs583 FSC Winter 2005 Notes Big-step semantics is based upon capture free substitution e[x := v] Two sets of rules At level 0 At level n+1 Except for escape (at 1 and n+2) Collapses to normal bigstep semantics for lambda calculus when remove rules for brackest, escape, and run

29Cs583 FSC Winter 2005 Contributions Two Semantics Operational  good when thinking about implementation Axiomatic semantics  good when reasoning Soundness (adequacy) w.r.t Operational Semantics  They mean the same thing Axiomatic semantics is confluent the order in which you apply the rules doesn't matter Static Type checking Throws away Faulty terms Formal Type system  how to implement it Subject Reduction  proof that it works

30Cs583 FSC Winter 2005 Limitations This type system rejects some programs that do not reduce to faulty terms x.run (( y. )x) or x.run (run (( y. ) )) x.run x  Not unreasonable to reject x.run x should not have type:  t ( f. )>) ( x.run x)  * Axiomatic Semantics Limitations Substitution is defined only on values !  different kind of semantics necessary for call by name or lazy languages. It depends on levels