The Trouble with Types Martin Odersky EPFL and Typesafe.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Generic programming in Java
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Searching for Type-Error Messages Benjamin Lerner, Matthew Flower, Dan Grossman, Craig Chambers University of Washington.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
6/10/2015Martin Odersky, LAMP, EPFL1 Programming Language Abstractions for Semi-Structured Data Martin Odersky Sebastian Maneth Burak Emir EPFL.
Reasons to study concepts of PL
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
Semantics for MinML COS 441 Princeton University Fall 2004.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
F# Shiva Srivastava David He Peter Bingel. Overview F# (pronounced "F sharp") is a functional and object oriented programming language for the Microsoft.NET.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor John Altidor Taming the Wildcards: Combining Definition- and Use-Site Variance.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Foundations of Programming Languages – Course Overview Xinyu Feng Acknowledgments: some slides taken or adapted from lecture notes of Stanford CS242
Language Evaluation Criteria
Topic 3 The Stack ADT.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Effective Java: Generics Last Updated: Spring 2009.
09/17/08Andrew Frank1 Time and Process: The challenge for GIS and what ontology can contribute Andrew U. Frank Geoinformation TU Vienna
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Self Type Constructors Atsushi Igarashi Kyoto University Joint work with Chieri Saito 1.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Evolving the ML Module System Derek Dreyer Toyota Technological Institute at Chicago April 15, 2004.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Types in programming languages1 What are types, and why do we need them?
Chapter 3 - Language Design Principles
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Object Oriented Programming
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
CSE 341 Section 10 Subtyping, Review, and The Future.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Language Paradigms CS655.
Type Checking and Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
Types CSCE 314 Spring 2016.
Programming Languages Dan Grossman 2013
Types for Programs and Proofs
Semantic Analysis Type Checking
Type Systems Terms to learn about types: Related concepts: Type
Foundations of Programming Languages – Course Overview
Foundations of Programming Languages – Course Overview
Adriaan Moors Martin Odersky
CSE341: Programming Languages Lecture 11 Type Inference
Generic programming in Java
CSE341: Programming Languages Lecture 11 Type Inference
Haskell Types, Classes, and Functions, Currying, and Polymorphism
CSE341: Programming Languages Lecture 11 Type Inference
Type Systems Terms to learn about types: Related concepts: Type
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

The Trouble with Types Martin Odersky EPFL and Typesafe

Types Everyone has an opinion on them Industry: – Used to be the norm (C/C++, Java). – Today split about evenly with dynamic. Academia: – Static types are more common.

Static: Points in Favor More efficient Better tooling Fewer tests needed Better documentation Safety net for maintenance

Dynamic: Points in Favor Simpler languages Fewer puzzling compiler errors No boilerplate Easier for exploration No type-imposed limits to expressiveness

What is Good Design? − Clear − Correct − Minimal − The opposite of “random” Great designs are often discovered, not invented.

Elements Of Great Designs: Patterns & Constraints

Example: Bach Fugues

What Is A Good Language for Design? One that helps discovering great designs.

What Is A Good Language for Design? One that helps discovering great designs. Patterns  Abstractions Constraints  Types

Example – Functional Collections – And their generalizations, e.g. monads, applicatives Powerful patterns made safe by types.

But... Type systems are hairy. Otherwise there would not be so many different ones. I'm not against types, but I don't know of any type systems that aren't a complete pain, so I still like dynamic typing [Alan Kay]

Type Systems Landscape static dynamic strongweak COCaml Haskell Scala Python, Clojure JS Java C# Ruby Assembly Typescript Dart

Static Type Systems detailed coarse strongweak C Haskell OCaml Scala Java 5+ C# Typescript Dart Java 4 Go F# Eiffel Pascal “Cutting corners” “My way or the highway” “Type it to the max”

(1) My Way or the Highway detailed coarse strongweak C Haskell OCaml Scala Java 5+ C# Typescript Dart Java 4 Go F#

My Way or The Highway Simple type systems No generics Not that extensible by users  Simpler tooling  Highly normative

(3) Type it to the Max detailed coarse strongweak C Haskell OCaml Scala Java 5+ C# Typescript Dart Java 4 Go F#

Type it to the Max Rich* language to write types Type combination forms, including generics. Type systems often inspired by logic. * Often, turing complete

Type it to the Max Where dynamic languages had the upper hand: – No type-imposed limits to expressiveness  Rich type system + escape hatches such as casts – No boilerplate  Type Inference – Easier for exploration  Bottom type Nothing, ???

Making Good Use of Nothing def f(x: Int) = ???

Making Good Use of Nothing def f(x: Int): Nothing = ??? if (x < 0) ??? else f(x)

Other Strengths of Dynamic Simpler languages − Rich types add complexity Fewer puzzling compiler errors

5862.scala:36: error: type mismatch; found : scala.collection.mutable.Iterable[_ >: (MapReduceJob.this.DataSource, scala.collection.mutable.Set[test.TaggedMapper[_, _, _]]) with test.TaggedMapper[_$1,_$2,_$3] forSome { type _$1; type _$2; type _$3 } : (MapReduceJob.this.DataSource, scala.collection.mutable.Set[test.TaggedMapper[_, _, _]]) with test.TaggedMapper[_$1,_$2,_$3] forSome { type _$1; type _$2; type _$3 } : (MapReduceJob.this.DataSource, scala.collection.mutable.Set[test.TaggedMapper[_, _, _]]) with test.TaggedMapper[_$1,_$2,_$3] forSome { type _$1; type _$2; type _$3 } : (MapReduceJob.this.DataSource, scala.collection.mutable.Set[test.TaggedMapper[_, _, _]]) with test.TaggedMapper[_$1,_$2,_$3] forSome { type _$1; type _$2; type _$3 } <: Object] with scala.collection.mutable.Builder[(MapReduceJob.this.DataSource, scala.collection.mutable.Set[test.TaggedMapper[_, _, _]]) and so on for another 200 lines

(3) Cutting Corners detailed coarse strongweak C Haskell OCaml Scala Java 5+ C# Typescript Dart Java 4 Go F#

Cutting Corners Appeal to user’s intuitions (covariant generics). – Employee are Persons – So functions from Employees to Employers are also functions from Persons to Employers, right? Embrace unsoundness. Easy, and superficially simple. But, fundamentally, a hack. Can we build great designs on false theories?

Precision Soundness Simplicity Take Any Two?

Abstractions Two fundamental forms – Parameters (positional, functional) – Abstract Members (name-based, object-oriented)

Abstractions Two fundamental forms – Parameters (positional, functional) – Abstract Members (name-based, modular)

Types in Scala scala.collection.BitSet Named Type Channel with Logged Compound Type Channel { def close(): Unit } Refined Type List[String]Parameterized List[T] forSome { type T } Existential Type List Higher-Kinded

Orthogonal Design T {... }Named T with U Modular Functional

Non-Orthogonal Design T {... }Named T with UT[U] Modular Functional More Features Fewer combinations

Too Many Combinations? T {... }Named T with U Modular Functional ?

Projections Reduce Dimensionality T {... }Named T with U Modular Functional

Projections Help Remove Features T {... }Named T with U Modular Functional

Dot and Dotty DOT: Calculus for Dependent Object Types Dotty: A Scala-Like Language with DOT as its core

[FOOL 2012]

Types in Dotty scala.collection.BitSet Named Type Channel & Logged Intersection Type Channel { def close(): Unit } Refined Type ( List[String]Parameterized ) List[T] forSome { tpe T } Existential Type List Higher-Kinded

Modelling Generics class Set[T] {... }  class Set { type $T } Set[String]  Set { type $T = String } class List[+T] {... }  class List { type $T }List[String]  List { type $T <: String } Parameters  Abstract members Arguments  Refinements

Making Parameters Public class Set[type Elem] {...} class Set { type Elem...} Set[String] Set { type Elem = String } class List[type +Elem] {...} class List { type Elem...}List[String] List { type Elem <: String } Analogous to “val” parameters: class C(val fld: Int) class C { val fld: Int }

Expressing Existentials What is the type of Lists with arbitrary element type? Previously: List[_] List[T] forSome { type T } Now: List (Types can have abstract members)

Expressing Higher-Kinded What is the type of List constructors? Previously: List Now: List Can always instantiate later: type X = List X { type T = String } X[String]

In a Nutshell In this system, Existential = Higher-kinded In fact, both are just types with abstract members. We do not distinguish between types and type constructors.

Native Meets and Joins The horrible type error message came from a computed join of two types. Problem: In Scala, the least upper bound of two types can be infinitely large. Adding native & and | types fixes that.

Will this Be Scala? Hopefully. Depends on how compatible we can make it. Note: SIP 18 already forces you to flag usages of existentials and higher-kinded types in Scala. This should give you a some indication how much effort would be needed to convert.

The Essence of Scala Harness the power of naming A simple language struggling to get out

Types Are Trouble – Tooling – Error messages – Conceptual complexity – Scope for misuse But I believe they are worth it, because they can lead to great designs.

Supplementary Slides

DOT expr.member Type = path.TypeName | Type { Defs } |... Def = val x: Type = Expr | def f(y: Type): Type = Expr | type T <: Type >: = extends

Subtyping Fundamental relation: T1 <: T2 T1 is a subtype of T2. Comes in many guises: Implementation matches Interface Type class extension Signature ascription