Semantics for MinML COS 441 Princeton University Fall 2004.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Chapter 6 Intermediate Code Generation
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
Type checking © Marcelo d’Amorim 2010.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Dynamic Typing COS 441 Princeton University Fall 2004.
ISBN Chapter 3 Describing Syntax and Semantics.
Type Checking.
Compiler Construction
S EMINAL : Searching for ML Type-Error Messages Benjamin Lerner, Dan Grossman, Craig Chambers University of Washington.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
Parametric Polymorphism COS 441 Princeton University Fall 2004.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
MinML: an idealized programming language CS 510 David Walker.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Aggregate Data Structures COS 441 Princeton University Fall 2004.
HOT Programming in Java COS 441 Princeton University Fall 2004.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Type Checking  Legality checks  Operator determination  Overload resolution.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Describing Syntax and Semantics
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
Imperative Programming
C H A P T E R S E V E N Expressions and Assignment Statements.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Chapter 9: Functional Programming in a Typed Language.
Types in programming languages1 What are types, and why do we need them?
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Object Oriented Programming
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
More on Type Checking. Conversion and Coercion Int C; A = C;
Types and Programming Languages
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Dr. Philip Cannata 1 Programming Languages Chapters 24 – 29 Types, Type Judgments, Type Systems and Type Safety - Chapters Explicit Polymorphism.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Introduction to Parsing
Functional Programming
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
CSE-321 Programming Languages Simply Typed -Calculus
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
ML: a quasi-functional language with strong typing
Parsing & Context-Free Grammars
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
CSE401 Introduction to Compiler Construction
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Programming Languages and Compilers (CS 421)
Drew Wyborski Programming Languages
Abstract Types Defined as Classes of Variables
Presentation transcript:

Semantics for MinML COS 441 Princeton University Fall 2004

MinML Concrete Syntax

Fib in MiniML fun fib(n:int):int is if <(n,2) then 1 else +(apply(fib,-(n,1)), apply(fib,-(n,2)))

MinML Abstract Syntax Syntax fun f ( x :  1 ):   is e represented as ABT fun(  1,  2, (f,x.e) ) f and x both bound in e

ABT for Fib in MiniML fun(int,int, (fib,n. if(<( n,2),1, +(apply( fib,-( n,1)), apply( fib,-( n,2)))) ) )

Type Environment Type environment (  ) maps names to type [x:  ] extends environment defined as Typing rule for names is

Integers

Booleans

must match

Functions

Inversion Theorems

Substitution and Weakening

Transition Semantics Set of states are closed expressions Set of final states (F) are values (v) v ::= true | false | n | fun(  1,  2,(f,x.e) )

Primitive “Instructions”

Search Rules

MinML Summary Hopefully what you saw was completely unsurprising and boring –It will get less boring when you have to implement the semantics for homework in SML Now that we have a very simple base language we can explore extensions to the language

Extending MinML We will consider extensions to the MinML –Adding real numbers –Adding lists –Adding lists and real numbers We will examine the impact on the static and dynamic semantics as well as discuss design alternatives and issues for each extension

MinML with real This is easy right? Just add new operators or overload existing operators Automatic or explicit conversion between real and int ? Examine simplest design first than consider more complicated ones

A Simple Design for Reals Just add new operators and constants Approach OCaml (ML Variant) uses Real Num’s rn::= 0.0 | 0.5 | | … Real Op’s ro::= +. | -. | *. | =. | <. Types  ::= … | real Expr’s e::= … | i2r( e ) | r2i( e )

Typing for Simple Reals  ` +.( e 1,e 2 ) : real  ` e 1 : real  ` e 2 : real  ` =.( e 1,e 2 ) : bool  ` e 1 : real  ` e 2 : real  ` i2r( e ) : real  ` e : int  ` r2i( e ) : int  ` e : real   ` rn : real

Step Function for Simple Reals +.( rn 1,rn 2 )  rn 1 + real rn 2 i2r( n )  n.0 r2i( n. m )  n 

A Simple Design for Reals Straightforward no complications –Type checking syntax directed –Extensions to dynamic semantics Arguably clumsy from a user perspective –New operators for every type –Possibly O(n 2 ) coercions with more numeric types

Reals with Overloading Overload the existing operators Automatically promote integers to real but not the reverse Basically the Java design Real Num’s rn::= 0.0 | 0.5 | | … Types  ::= … | real Expr’s e::= … | trunc( e )

Typing for Overloaded Reals  ` +( e 1,e 2 ) : real  ` e 1 : real  ` e 2 : real  ` =( e 1,e 2 ) : bool  ` e 1 : real  ` e 2 : real  ` e : real  ` e : int  ` trunc( e ) : int  ` e : real   ` rn : real

Step Fun. for Overloaded Reals +( rn 1,rn 2 )  rn 1 + real rn 2 n  n.0 trunc( n. m )  n 

Reals with Overloading Many issues with our extensions Type checking not syntax directed –But can choose unambiguous order or rule applications to get “sensible” system –Bottom up type checking algorithm Dynamic semantics non-deterministic –Bigger problem here semantics is not clear when ints are promoted to reals – +(1,2)  * 3.0 and +(1,2)  * 3

Reals with Overloading (Fixed) +( n 1,rn 2 )  n real rn 2 trunc( n. m )  n  +( rn 1,n 2 )  rn 1 + real n 2.0

Fixing Dynamic Semantics Rewrite semantics so that ints are promoted to reals only when used in context expecting a real Mirrors intuition of type-checking rules Suggests we must distinguish between reals and integers at runtime –Not a problem for an interpret but bigger problem for compiler –Messy number for rules!

Translation Semantics Avoid complicated dynamic semantics by using original simpler semantics of and type-directed translation of program Best of both worlds –User gets overloading –Compiler and proof hacker’s get simpler systems

Type Directed Translation  ` (e ) i2r( e’ ) ) : real  ` (e ) e’) : int   ` (rn ) rn) : real  ` (n ) n) : int  ` ( +( e 1,e 2 ) ) r+( e’ 1,e’ 2 ) ): real  ` (e 1 ) e’ 1 ) : real  ` (e 2 ) e’ 2 ) : real  ` ( +( e 1,e 2 ) ) i+( e’ 1,e’ 2 ) ): int  ` (e 1 ) e’ 1 ) : int  ` (e 2 ) e’ 2 ) : int

MinML with (  )list Simple ML generic list with hd/tl Types  ::= … | (  )list Expr’s e::=… | nil | cons( e 1, e 2 ) | hd( e ) | tl( e ) | isNil( e )

Typing MinML with (  )list  ` cons( e 1,e 2 ) : (  )list  ` e 1 :   ` e 2 : (  )list   ` nil : (  )list  ` hd( e ) :   ` e : (  )list  ` tl( e ) : (  )list  ` e : (  )list  ` isNil( e ) : bool  ` e : (  )list

Step Fun. for MinML with (  )list hd(cons( v hd, v tl ))  v hd tl(cons( v hd, v tl ))  v tl  What should the behavior of hd(nil) or tl(nil) be?

MinML with (  )list and casel Simple ML generic list with casel Types  ::= … | (  )list Expr’s e::=… | nil | cons( e 1, e 2 ) | casel( e ) of nil => e 1 or cons(x: ,y)=> e 2 end

MinML with (  )list Type checking not syntax directed –nil has many types –Can use weaker form of type inference to assign type to nil or raise error if it is not determinable casel version is less error prone and avoids messing up the dynamic semantics with corner cases

MinML with real and (  )list Lists hd/tl Lists casel Reals Separate Ops Reals Overloaed Ops

MinML with real and (  )list Lists hd/tl Lists casel Reals Separate Ops No unpleasant interactions Reals Overloaed Ops

MinML with real and (  )list Lists hd/tl Lists casel Reals Separate Ops No unpleasant interactions Reals Overloaed Ops How do we type check? +(hd(nil),1) No unpleasant interactions with a little thinking

What’s Going On? In the real type system all the primitive leaf rules/axioms for expressions provided us one unique type Bottom up reading of the expression could resolve overloading ambiguities Type inference for “ nil ” violates that assumption breaking overloading

What can we do about it? Give up on either overloading or type-inference! Complain when our algorithm gets too confused –Choose sane defaults (e.g. SML + is by default (int * int) -> int) –Allow optional annotations to help checker and override defaults –Pragmatic engineering and design issues Adopt Haskell type classes which allow for polymorphic runtime overloading!

Lessons Learned We can easily specify languages at a high- level with formal techniques We can reason about the design and interaction of systems incrementally The formalizes are just an aid to force you to think about all the details that come up when trying to do good language design