The Trouble with Types

Similar documents
The DOT Calculus. (Dependent Object Types) Nada Amin. June 18, Scala Days

The DOT Calculus. (Dependent Object Types) Nada Amin. May 12, flatmap(oslo)

Scala in Martin Odersky

Scala Where It Came From Where It is Going

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

More Lambda Calculus and Intro to Type Systems

Type Checking and Type Equality

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Functional Programming Principles in Scala. Martin Odersky

Consistent Subtyping for All

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Compilers and computer architecture: Semantic analysis

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

More Lambda Calculus and Intro to Type Systems

Simply-Typed Lambda Calculus

Stop coding Pascal. Saturday, April 6, 13

Recursive Types and Subtyping

CMSC 330: Organization of Programming Languages

Programming Languages

Overview. Elements of Programming Languages. Evaluation order. Evaluation order

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

Static type safety guarantees for the operators of a relational database querying system. Cédric Lavanchy

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

More Lambda Calculus and Intro to Type Systems

Dependent Object Types - A foundation for Scala s type system


Exercise 8 Parametric polymorphism November 18, 2016

Static Typing or Dynamic Typing: A Real Issue or a Simple Case of Tribalism

First-Class Type Classes

Lecture 8: Summary of Haskell course + Type Level Programming

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

Dependent types and program equivalence. Stephanie Weirich, University of Pennsylvania with Limin Jia, Jianzhou Zhao, and Vilhelm Sjöberg

Programming Systems in Artificial Intelligence Functional Programming

Copyright 2018 Tendril, Inc. All rights reserved.

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Type Theory meets Effects. Greg Morrisett

CS558 Programming Languages

Integrating verification in programming languages

Types, Type Inference and Unification

Improving Scala's Safe Type-Level Abstraction

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Featherweight Scala. Week 14

A Practical Optional Type System for Clojure. Ambrose Bonnaire-Sergeant

CSCE 314 Programming Languages. Type System

Implementing Method Type Specialisation In Dotty

Self Type Constructors. Atsushi Igarashi Kyoto University Joint work with Chieri Saito

Topic 9: Type Checking

Topic 9: Type Checking

Concepts of programming languages

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Adam Chlipala University of California, Berkeley ICFP 2006

Introduction to Programming Using Java (98-388)

22c:111 Programming Language Concepts. Fall Types I

Types and Type Inference

Tracing Ambiguity in GADT Type Inference

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Classical Themes of Computer Science

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

A Spectrum of Type Soundness and Performance. Ben Greenman & Matthias Felleisen Northeastern University

Type Systems, Type Inference, and Polymorphism

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

Modules Matter Most. Robert Harper Carnegie Mellon University. MacQueen Fest Chicago May 2012

Scala : an LLVM-targeted Scala compiler

Experimental New Directions for JavaScript

Part III. Chapter 15: Subtyping

G Programming Languages - Fall 2012

The Typed Racket Guide

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

h0ps://

Haskell in the corporate environment. Jeff Polakow October 17, 2008

CS558 Programming Languages

Recursive Types and Subtyping

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Derek Dreyer. WG2.8 Meeting, Iceland July 16-20, 2007

Types and Type Inference

Typed Racket: Racket with Static Types

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CSE373: Data Structure & Algorithms Lecture 23: Programming Languages. Aaron Bauer Winter 2014

Subtyping. Lecture 13 CS 565 3/27/06

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Software II: Principles of Programming Languages. Why Expressions?

JSJS - Project Proposal

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The Hack programming language:

CSCI-GA Scripting Languages

Programming in Scala Second Edition

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Metaprogramming assignment 3

CSE 505: Programming Languages. Lecture 10 Types. Zach Tatlock Winter 2015

Transcription:

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 C Java OCaml Haskell C# Scala Typescript Dart dynamic Assembly JS Ruby Python, Clojure weak strong

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

Static Type Systems Agda Idris Coq detailed Cutting corners Eiffel Typescript Dart Haskell Scala OCaml F# C# Java 5+ Type it to the max coarse C Java 4 Go Pascal My way or the highway weak strong

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

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 Typescript Dart Haskell Scala OCaml F# C# Java 5+ coarse C Java 4 Go weak strong

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 } <: Object] with scala.collection.mutable.builder[(mapreducejob.this.datasource, scala.collection.mutable.set[test.taggedmapper[_, _, _]]) with test.taggedmapper[_$1,_$2,_$3] forsome { type _$1; type _$2; type _$3 },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 } <: Object] with scala.collection.mutable.builder[(mapreducejob.this.datasource, scala.collection.mutable.set[test.taggedmapper[_, _, _]]) with test.taggedmapper[_$1,_$2,_$3] forsome { type _$1; type _$2; type _$3 },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 } <: Object] with scala.collection.mutable.builder[(mapreducejob.this.datasource, scala.collection.mutable.set[test.taggedmapper[_, _, _]]) with test.taggedmapper[_$1,_$2,_$3] forsome { type _$1; type _$2; type _$3 },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 } <: 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 Typescript Dart Haskell Scala OCaml F# C# Java 5+ coarse C Java 4 Go weak strong

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 Functional Named T {... } T with U Modular

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

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

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

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

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 Channel & Logged Named Type 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: Now: List[_] List[T] forsome { type T } List (Types can have abstract members)

Expressing Higher- Kinded What is the type of List constructors? Previously: Now: List 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.

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

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 small 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.