Type inference and modern type systems Stephanie Weirich University of Pennsylvania.

Slides:



Advertisements
Similar presentations
Transposing F to C Transposing F to C Andrew Kennedy & Don Syme Microsoft Research Cambridge, U.K.
Advertisements

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Functional Programming Lecture 10 - type checking.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Let should not be generalized Dimitrios Vytiniotis, Simon Peyton Jones Microsoft Research, Cambridge TLDI’1 0, Madrid, January 2010 Tom Schrijvers K.U.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Type Analysis and Typed Compilation Stephanie Weirich Cornell University.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
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.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05.
Intensional Polymorphism in Type-Erasure Semantics Karl Crary, Stephanie Weirich, Greg Morrisett Presentation by Nate Waisbrot.
Good Advice for Type-directed Programming Aspect-oriented Programming and Extensible Generic Functions Geoffrey Washburn [ ] Joint.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Generative type abstraction and type-level computation (Wrestling with System FC) Stephanie Weirich, Steve Zdancewic University of Pennsylvania Dimitrios.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Closure and Environment Compiler Baojian Hua
Functional Programming Professor Yihjia Tsai Tamkang University.
Type Inference II David Walker CS 510. ML-style Polymorphism 2 characteristics: prenex (let) polymorphism let f : all [a,b,c].t =... in e polymorphic.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Generic Programming with Dependent Types Stephanie Weirich University of Pennsylvania.
Tuples and Lists Lecture 3, Programmeringsteknik del A.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
CCC: An Aspect-Oriented Intermediate Language on.Net Platform Yingfei Xiong and Feng Wan University of Electronic Science and Technology of China, China.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
Applying Translucid Contracts for Modular Reasoning about Aspect and Object Oriented Events Mehdi Bagherzadeh Gary T. Leavens Robert Dyer Foundations of.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
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.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Aspectual Caml an Aspect-Oriented Functional Language Hideaki Tatsuzawa Hidehiko Masuhara Akinori Yonezawa University of Tokyo.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Simon Peyton Jones, Stephanie Weirich, Richard Eisenberg, Dimitrios Vytiniotis Microsoft Research University of Pennsylvania April 2016.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Programming Languages Dan Grossman 2013
Operational Semantics of Scheme
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
What is a Functional Language?
ML: a quasi-functional language with strong typing
Tim Sheard Oregon Graduate Institute
Advanced Functional Programming
Programming Languages and Compilers (CS 421)
Microsoft Research University of Pennsylvania
Advanced Functional Programming
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Type inference and modern type systems Stephanie Weirich University of Pennsylvania

Type inference What is the role of type inference in the design of programming languages? Old answer (for many FP languages): Part of the language specification –Defines what valid programs –Disabling technology for advanced type systems

A different philosophy Type inference as an afterthought: –Expressive (but wordy) language is the standard –Inference is a tool to help programmers produce programs, but no more –Other means of program generation may use different tools Allows more sophisticated type systems

Studying the tool But that doesn’t mean we shouldn’t study type inference Need a specification of the tool Opportunities for research into type system design –This talk: examples from AspectML, Haskell –My goal this week: more examples from type systems for program generation

Specific examples AspectML, aspect-oriented functional programming language –polymorphic aspects and first-class pointcuts –run-time type analysis Haskell –GADTs –Higher-rank/impredicative polymorphism

Trade off Unifying theme –All of these languages use typing annotations to augment HM type system –Each type system distinguishes between “known” and “guessed” type information, with more or less precision. Trade-off between simple specification and pithy language This talk: some details about AspectML, highlights of rest

AspectML Aspects = advice + pointcuts Pointcuts –“where” advice happens –Currently: sets of function names (other joinpoints possible) Advice –“what” happens before/after/around joinpoints

First-class pointcuts Code to install advice may be used for many different pointcuts –example: a “debugger” aspect can use a single function to install tracing code Pointcuts must have types –specify interface between joinpoints and advice –advice may be polymorphic pc : pointcut (all a. a → Int) advice before pc [a] (x:a, …) { … }

Polymorphic pointcut typing May assume joinpoint is more polymorphic let pc = { f } : pointcut (Int → Int) advice after pc [a] (x:a,…) { …. } Can’t assume joinpoint is less polymorphic let pc = { g } : pointcut ( a. a → a ) advice before pc (x:int,…) { … }

Pointcut typing Computing pointcut type requires anti-unification f : Int → Int g : a → Int let pc = { f, g } : pointcut (a. a → Int)

Typecase let add_tracing (pc : pointcut (ab. a -> b)) = let print[a] (x:a) = (typecase[unit] a of int => print_int x bool => print_bool x (b,c) => print (fst x); print (snd x) (b->c) => print “ ”) in advice before pc [a] (x:a, f,s) { print x; x }

Type inference problems in Aspect ML Pointcuts –anti-unification/unification –Can’t guess pointcut type - like first-class polymorphism –Specification of HM let-polymorphism is too flexible, functions have multiple types Typecase –Most examples require polymorphic recursion –Can be difficult to determine result type –Pathological examples with no most-general type

Let polymorphism Specification in HM:  ├ e : t Gen( ,t) = s , x:s├ e’ : t’  ├ let x = e in e’ : t’ Allows multiple derivations: ├ x.x : int  int Gen(int  int) = int  int f : int  int ├ f 3 : int ├ let f = x. x in f 3 : int ├ x.x : a  a Gen(a  a) =  a. a  a f :  a. a  a ├ f 3 : int ├ let f = x. x in f 3 : int What is type of {f} ?

Pathological typecase f x = typecase a of int => 2 + x Does f have type –int -> int –forall a. a -> int –forall a. int -> a –forall a. a -> a Most general type is not expressible: –forall a. (a=int) => a -> a

Simple, fairly conservative solution Typing annotations resolve ambiguity –typecase Annotate for polymorphic recursion Annotate return type, variables with “refined” types –pointcuts When created or used When arguments to functions Typing spec distinguishes “known” and “inferred” types –Context distinguishes types of variables Investigating how well this simple spec works

GADTs A small bit of typesafe metaprogramming data Exp a where Lit : a -> Exp a If : Exp Bool -> Exp a -> Exp a App : Exp (a -> b) -> Exp a -> Exp b Plus : Exp (Int -> Int -> Int) eval :: Exp a -> a eval (Lit x) = x eval (If x y z) = if (eval x) then (eval y) else (eval z) eval (App x y) = (eval x) (eval y) eval (Plus) = +

Type inference and GADTs Similar problems as typecase Annotations more burdensome here –typecase always know scrutinee from program text not that common (?) –GADTs may not know type of scrutinee must generalize normal case analysis (and deal with nested case analysis, existentials, etc.)

Wobbly types GADT type system distinguishes between “wobbly” and “rigid” types. –Typing rules push rigid types into the judgment –A type is wobbly if any part of it must be guessed Special rules also propagate “rigidity” through polymorphic function application

Higher-rank / impredicative polymorphism Allows polymorphic values to be stored in data structures and passed to functions Example: polymorphic, reified code –Now : code(tau) –Allows: code(sigma) Is this useful in practice? –polymorphism in meta-language allows polymorphism in object language –forall a. code (a -> a) vs. code (forall a. a -> a)

Boxy types Most precise system: boxes in the type separate “known” and “guessed” type information Essential for specification of impredicative instantiation Annotations propagated throughout the typing judgment

For more information Papers available from my website AspectML –with David Walker, Geoff Washburn, Dan Dantas GADTs - wobbly types –with Simon Peyton Jones, Dimitrios Vytiniotis, Geoff Washburn Impredicativity/Higher rank - boxy types –with Simon Peyton Jones, Dimitrios Vytiniotis