Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –

Slides:



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

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Situation Calculus for Action Descriptions We talked about STRIPS representations for actions. Another common representation is called the Situation Calculus.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Type checking © Marcelo d’Amorim 2010.
CSE-321 Programming Languages Predicative Polymorphic -Calculus POSTECH May 23, 2007 박성우.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Winter 2004/5Pls – expresssion-lang – Catriel Beeri22 Informal Analysis Goal: prove the semantics is deterministic Stages: Values --- we know they have.
CSE-321 Programming Languages Subtyping POSTECH May 14, 2007 박성우.
Type Checking.
Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
Catriel Beeri Pls/Winter 2004/5 types 1 Types Three chapters:  General introduction to types  A simple static monomorphic type system (with type checking.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Winter 2004/5Pls – inductive – Catriel Beeri1 Inductive Definitions (our meta-language for specifications)  Examples  Syntax  Semantics  Proof Trees.
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Catriel Beeri Pls/Winter 2004/5 inductive-revisited 1 Inductive definitions revisited  Generated and Freely generated sets oPattern match, unification.
Catriel Beeri Pls/Winter 2004/5 interpreters 1 Language processors for EL  An interpreter transEval from  An interpreter natEval from An important theme:
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Catriel Beeri Pls/Winter 2004/5 environment1 1 The Environment Model  Introduction and overview  A look at the execution model  Dynamic scoping  Static.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
Introduction and Syntax. Course objectives Discuss features of programming languages. Discuss how the features are implemented in a simple computer architecture.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Guide To UNIX Using Linux Third Edition
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Catriel Beeri Pls/Winter 2004/5 environment 19 II. Frames and frame structures Frame – set of bindings generated together in a binding generation event.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Subtyping Chapter 15 Benjamin Pierce Types and Programming Languages.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Formal Verification Lecture 9. Formal Verification Formal verification relies on Descriptions of the properties or requirements Descriptions of systems.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Subtying Kangwon National University 임현승 Programming Languages These slides were originally created by Prof. Sungwoo Park at POSTECH.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Towards a Semantic Model for Java Wildcards Sophia Drossopoulou Mariangiola Dezani-Ciancaglini Imperial College London Università di Torino Italy Nicholas.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
CSE341: Programming Languages Lecture 11 Type Inference
Principles of programming languages 12: Functional programming
Stateful Manifest Contracts
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Adapted from slides by Nicholas Shahan and Dan Grossman
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
CSE341: Programming Languages Lecture 11 Type Inference
Assignments and Procs w/Params
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice – executes “fast” (seems linear time) But, some bad cases exist

Catriel Beeri Pls/Winter 2004/5 last 56 consider: let f1 = fun x  (x,x);; let f2 = fun y  f1(f1 y);; let f3 = fun y  f2(f2 y);; ….. …. fn …. fn (some expression that uses fn) How do the types of these functions look like?

Catriel Beeri Pls/Winter 2004/5 last 57 let f1 = fun x  (x,x);; ‘a  ‘a * ‘a (1  2) let f2 = fun y  f1(f1 y);; ‘a  (‘a * ‘a) * (‘a * ‘a) (1  4) let f3 = fun y  f2(f2 y);; ‘a  ( [(‘a * ‘a)*(‘a * ‘a)]*[(‘a * ‘a)*(‘a * ‘a)] )* ( [(‘a * ‘a)*(‘a * ‘a)]*[(‘a * ‘a)*(‘a * ‘a)] ) (1  16) let fn = … (double exponential)

Catriel Beeri Pls/Winter 2004/5 last 58 One can save a lot of space by representing types as graphs, instead of trees (common sub expression elimination) Double exponential  exponential

Catriel Beeri Pls/Winter 2004/5 last 59 Theorem: Type reconstruction for core ML is exptime-complete This means that worst-case complexity is bad, but in practice it is sufficiently efficient Note: extending type reconstruction to the full calculus with universal types is impossible --- type reconstruction for this calculus is undecidable

Catriel Beeri Pls/Winter 2004/5 last 60 II. polymorphic references are problematic: let c = ref (lambda x.x);; here, the type for c is c:= lambda x. x+5;; type-checker allows the assignment, type is unit (!c) true;; type checker accepts but, at run-time we apply a function of type int  int to true – a run-time error

Catriel Beeri Pls/Winter 2004/5 last 61 One possible solution: lazy evaluation of let : let x = e // create binding x  e …. … x // substitute e for x, and continue evaluation In the example: let c = ref (lambda x. x) // bind c to the expression c:= lambda x. x+5 // substitute binding for x  (ref lambda x.x) := lambda x.x+5 // one cell created) (!c) true // substitute binding for x  (!(ref lambda x. x)) true // another cell created

Catriel Beeri Pls/Winter 2004/5 last 62 But Nobody really knows how to specify or implement lazy evaluation for languages with imperative features (side-effects) – how to order the side- effects? The examples shows this leads to a semantics that is not very useful

Catriel Beeri Pls/Winter 2004/5 last 63 The ML solution: In let x = e in … Allow to generalize the type for x only if e is a syntactic value is is not Statistics collected on systems w/o this restriction (a more liberal but complex solution)  there are almost no programs where this restriction hurts.

Catriel Beeri Pls/Winter 2004/5 last 64 Object-oriented languages – some concepts A well known feature of OO pl’s is sub-type polymorphism We concentrate on this subject

Catriel Beeri Pls/Winter 2004/5 last 65 What is sub-type ? Two possible answers: A type t is (denotes) a set of values With the second, int<: float holds; Compiler inserts the coercion during type-checking (so a bit more complexity of type-checking is expected) We use the first (simpler intuition)

Catriel Beeri Pls/Winter 2004/5 last 66 The basic intuition of sub-typing: List to the type-checker level : an expression of a sub-type can be safely used in a context where an expression of a type is expected use is defined by the operations available on the two types  sub-typing is not a new independent feature, it interacts with the other components of a type system

Catriel Beeri Pls/Winter 2004/5 last 67 Sub-typing with records and cells Objects are similar to records  convenient to introduce sub-typing in the context of a language with : base types, records, functions, ref cells We assume some (possibly none) sub-type axioms are given for the base types

Catriel Beeri Pls/Winter 2004/5 last 68 From the basic intuition : The interaction of sub-typing with the type-checker : the subsumption rule: Wherever the type-checker expects a type, it allows a sub-type Note: algorithmically, this rule is problematic

Catriel Beeri Pls/Winter 2004/5 last 69 Rules independent of the given type system: From the basic intuition, sub-typing is reflexive and transitive Second rule also looks a bit problematic (algorithmically)

Catriel Beeri Pls/Winter 2004/5 last 70 Rules for records : Example : let f = lambda x : {a:int}. x.a;; seems reasonable to apply f also to {a=4, b=“john”}, since f uses only x.a But, also make sense to allow a sub-type in a field

Catriel Beeri Pls/Winter 2004/5 last 71 Using these two rules, we can prove: Using reflexivity, we can refine a single field, rather than all

Catriel Beeri Pls/Winter 2004/5 last 72 By combining the two record rules with transitivity, we can change both the number of fields and their types

Catriel Beeri Pls/Winter 2004/5 last 73 Can combine to one comprehensive record rule: Note: we assume that in a record, order of fields is irrelevant, so in all the rules, the label-type pairs are assumed to be a set. This can be emphasized by a rule that allows to change position of fields  Can “add” fields anywhere in a record

Catriel Beeri Pls/Winter 2004/5 last 74 Rules for functions : These can be applied, be passed as arguments/return values If a context requires a function that for an argument of type return a value of type, then a function that returns a value of a sub-type is ok

Catriel Beeri Pls/Winter 2004/5 last 75 But, for the input type: In a context that expects a function of this type, you also accept a function that has this guarantee for a larger set

Catriel Beeri Pls/Winter 2004/5 last 76 The two rules are typically combined : Contra-variance for the input type is difficult to swallow; convince yourself that There are also applications where a restriction of the in-type in a co-variant fashion seems desirable Some languages (e.g., Eiffel) also co-variant change on in- type, and leave a hole in the type system