Let should not be generalized Dimitrios Vytiniotis, Simon Peyton Jones Microsoft Research, Cambridge TLDI’1 0, Madrid, January 2010 Tom Schrijvers K.U.

Slides:



Advertisements
Similar presentations
Programmed Strategies for Program Verification Richard B. Kieburtz OHSU/OGI School of Science and Engineering and Portland State University.
Advertisements

Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
0 - 0.
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.
Functional Programming Lecture 10 - type checking.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Haskell user defined types data Temp = Cold|Hot|Warm deriving (Show,Eq, Ord, Enum) -- to enable printing to screen -- comparing for equality -- comparison.
1 First order theories (Chapter 1, Sections 1.4 – 1.5)
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Tom Schrijvers K.U.Leuven, Belgium with Manuel Chakravarty, Martin Sulzmann and Simon Peyton Jones.
Modern Programming Languages, 2nd ed.
Bounding Option Prices Using Semidefinite Programming S ACHIN J AYASWAL Department of Management Sciences University of Waterloo, Canada Project work for.
Galit Haim, Ya'akov Gal, Sarit Kraus and Michele J. Gelfand A Cultural Sensitive Agent for Human-Computer Negotiation 1.
3 Logic The Study of What’s True or False or Somewhere in Between.
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Type-based termination analysis with disjunctive invariants Dimitrios Vytiniotis, MSR Cambridge with Byron Cook (MSR Cambridge) and Ranjit Jhala (UCSD)
Type inference in type-based verification Dimitrios Vytiniotis, Microsoft Research May 2010.
Reconciling OO and Haskell-Style Overloading Mark Shields Simon Peyton Jones Microsoft Research Cambridge
1 Decision Procedures An algorithmic point of view Equality Logic and Uninterpreted Functions.
Solve by Substitution: Isolate one variable in an equation
1 Modeling and Simulation: Exploring Dynamic System Behaviour Chapter9 Optimization.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Problems and Their Classes
Module 16: One-sample t-tests and Confidence Intervals
This work was partially funded by the RNTL initiative (LUTIN project) 1 Refactoring to Object-Oriented Design Patterns Mikal Ziane (LIP6 and Université.
Addition 1’s to 20.
Checking  -Calculus Structural Congruence is Graph Isomorphism Complete Victor Khomenko 1 and Roland Meyer 2 1 School of Computing Science, Newcastle.
Solving Addition and Subtraction Inequalities
A SMALL TRUTH TO MAKE LIFE 100%
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Auction Theory Class 5 – single-parameter implementation and risk aversion 1.
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.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
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.
Type inference and modern type systems Stephanie Weirich University of Pennsylvania.
System F with type equality coercions Simon Peyton Jones (Microsoft) Manuel Chakravarty (University of New South Wales) Martin Sulzmann (University of.
Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
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.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Type Inference II David Walker COS 441. Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Generic Programming with Dependent Types Stephanie Weirich University of Pennsylvania.
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
Type Inference II David Walker COS 441. Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate.
Second-Order Functions and Theorems in ACL2 Alessandro Coglio Workshop 2015 Kestrel Institute.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Hoogλe Finding Functions from Types Neil Mitchell haskell.org/hoogle community.haskell.org/~ndm/ [α]  [α]
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
AI Lecture 17 Planning Noémie Elhadad (substituting for Prof. McKeown)
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Simon Peyton Jones, Stephanie Weirich, Richard Eisenberg, Dimitrios Vytiniotis Microsoft Research University of Pennsylvania April 2016.
GADTs meet their match George Karachalias(Ghent University, Belgium) Tom Schrijvers (KU Leuven, Belgium) Dimitrios Vytiniotis(Microsoft Research Cambridge,
Functional Dependencies in System FC
Lesson 10 Type Reconstruction
Type Checking and Type Inference
ML: a quasi-functional language with strong typing
Haskell Chapter 2.
Matching Logic An Alternative to Hoare/Floyd Logic
Microsoft Research University of Pennsylvania
Records and Type Classes
Records and Type Classes
Presentation transcript:

let should not be generalized Dimitrios Vytiniotis, Simon Peyton Jones Microsoft Research, Cambridge TLDI’1 0, Madrid, January 2010 Tom Schrijvers K.U. Leuven

Extending ML type inference with … 1 Advanced types  Generalized Algebraic Datatypes (GADTs)  [ Cheney & Hinze, Xi, Peyton Jones et al., Pottier & Simonet, Pottier & Regis-Gianas,… ]  Open Type Families [ Schrijvers et al., ICFP 2007 ]  … types indexed by some constraint domain [e.g. Kennedy’s types indexed by Units of Measure, ESOP94] Advanced forms of constraints  Type equalities with type families, type class constraints  Implication constraints that arise because of pattern matching  [Pottier & Regis-Gianas, Sulzmann et al.] A question: How should we be generalizing let -bound definitions?

Why is this question relevant? 2  Type system decisions (as let -generalization) affect 1. Implementability of type inference & checking 2. Complexity of implementation 3. Efficiency of implementation 4. Programmability 5. Predictability of type checking 6. Backwards compatibility (lots of Haskell 98 code!)  GOAL:  Support advanced forms of types and constraints mentioned  Perform well in (1 – 6)

Generalized Algebraic Datatypes 3 (a ~ Int) => (Int ~ a) (a ~ Bool) => (Bool ~ a)  GADT data constructors introduce constraints  Pattern matching creates implication constraints  That a solver must discharge data R a where Rint :: (a ~ Int) => R a Rbool :: (a ~ Bool) => R a create :: R a -> a create Rint = 42 create Rbool = False Constraint introduced by Rint Constraint introduced by Rint 42 : Int Expected type: a

GADTs and Generalization 4 Observation:  ( flop 43 ) and ( flop False ) can potentially reach first or second branch No DEAD code in the example * data R a where Rint :: (a ~ Int) => R a Rbool :: (a ~ Bool) => R a mkR :: a -> R a flop x = let g () = not x -- not :: Bool -> Bool in case (mkR x) of Rbool -> g () Rint -> True x : β β ~ Bool mkR x : R β β ~ Bool => … ? … β ~ Int => true

GADTs and Generalization 5  What is the type of g?  If spec does not allow quantification over equalities () -> Bool  If spec does allow quantification over equalities () => Bool or (β ~ Bool) => () -> Bool data R a where Rint :: (a ~ Int) => R a Rbool :: (a ~ Bool) => R a mkR :: a -> R a flop x = let g () = not x -- not :: Bool -> Bool in case (mkR x) of Rbool -> g () Rint -> True x : β β ~ Bool mkR x : R β β ~ Bool => … ? … β ~ Int => true

Quantifying over equalities 6 data R a where Rint :: (a ~ Int) => R a Rbool :: (a ~ Bool) => R a mkR :: a -> R a flop x = let g () = not x -- not :: Bool -> Bool in case (mkR x) of Rbool -> g () Rint -> True Option 1I β = Bool g :: () -> Bool Option 1I β = Bool g :: () -> Bool Option I g :: β ~ Bool => () -> Bool Option I g :: β ~ Bool => () -> Bool g :: () -> Bool Second branch rejected ( Bool =/= Int ) … or … typeable as unreachable g :: () -> Bool Second branch rejected ( Bool =/= Int ) … or … typeable as unreachable

Hence, for GADTs: 7  We want to unify away as much as possible (type of ‘x’)  For simpler types  For support for some eager solving  For shorter constraints  But we can’t unify variables bound in the environment! if the type system allows quantification over equalities then we must defer a lot of silly unifications as constraints

Open type families 8  Programmers declare type-level computations  And give axiom schemes for them forall ( b ).G b Int ~ b  In GHC the axiom scheme definitions are open  If G Bool γ ~ Bool we must not conclude that γ ~ Int [think of another consistent axiom G Bool Char ~ Bool ] type family G a b type instance G b Int = b

Quantifying restricted constraints? 9  Ok, if equalities are problematic quantify over:  Class constraints: Eq α  Type family constraints: F α ~ Int  Problematic: We want to rewrite as much as possible  But we must not rewrite too much. Rather delicate! type family G a b type instance G b Int = b flop x = let test =... in... x :: β Yielding constraint: G β Int ~ Int Yielding type: β -> β x :: β Yielding constraint: G β Int ~ Int Yielding type: β -> β G β Int ~ β & G β Int ~ Int … which gives … β ~ Int G β Int ~ β & G β Int ~ Int … which gives … β ~ Int

Quantifying over only class constraints? 10  Problematic: class constraints may include superclasses:  Even if not, it’s hard to give a complete specification  Left-to-right: REJECT (can’t discharge constraint)  And it’s not right to defer unsolvable ( forall b. F α b ~ Int )  Right-to-left: ACCEPT  Rest of typing problem determines α ~ Int and triggers axiom! Class (a ~ b) => Eq a b type instance F Int b = b let f x = (let h y = … (yielding F α β ~ Int) … in 42, x + 42) x :: α y :: β x :: α y :: β

Let generalization not a new problem really 11  A. Kennedy knew about it when I was 14! [LIX RR/96/09]  Kennedy used a clever domain-specific solution  Constraint equivalent to: v ~ β/u  Τ ype: forall u. Num u -> Num (β/u) div :: forall u v. Num (u * v) -> Num u -> Num v weight :: Num kg time :: Num sec flop x = let y = div x in (y weight, y time) x::β Yielding constraint: β ~ (u * v) Yielding type: u -> v x::β Yielding constraint: β ~ (u * v) Yielding type: u -> v Solving gives β = u * v and g becomes monomorphic! Which IS polymorphic

The proposal 12  The specification and implementation costs for generalizing local let-bound expressions are becoming high: Do NOT Generalize Local Let-Bound Expressions  Top-level ones do not contribute to the problems [No environment to interact with]  Local but annotated let definitions can be polymorphic  Most Haskell 98 programs actually do not use local let polymorphism (though arguably code refactoring tools may). Results performed in Hackage reported in paper

Also in paper 13  Combining Let Should Not Be Generalized with the OutsideIn [ICFP09] strategy for solving implication constraints leads to LHM(X): HM(X) with Local assumptions from pattern matching  Type system parameterized over constraint domain X  Inference algorithm parameterized over X solver  Soundness result provided X solver assumptions  … towards pluggable type systems + type inference

A challenge for type system designers 14  Solvers for type class and family instance constraints are inherently weak (by design) under an open world assumption  Constraint arising: F α ~ Int  Instance declaration: type instance F Int ~ Int  Question: What is “ α ”?  Type system may “guess” α = Int, but algorithm can’t [or shouldn’t] Challenge: Find a declarative specification that rejects ambiguity

A first step: ambiguous constraints 15  Constraint C is unambiguously solvable in a top-level theory T: usolv (T,C) iff T shows θ (C) and T & C shows ( θ )  The constraint must be solvable by a substitution derivable by massaging the constraint: ¬ usolv ( F Int ~ Int, F α ~ Int )  … because: (F Int ~ Int & F α ~ Int) =/=> (α ~ Int)  Similar definition by Sulzmann & Stuckey [TOPLAS2005], also recent related work by Camarao et al.

Conclusions and directions 16 The cost of implicit local generalization is high, we should find alternatives or not generalize implicitly Directions and ongoing work:  A full GHC implementation that supports Haskell type classes, GADTs, type functions, and first-class polymorphism [journal submission soon]  A declarative specification that deals with ambiguity is open

17 Thank you for your attention