The Hack programming language:

Similar documents
CSCI-GA Scripting Languages

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

A Deep Dive into the Void

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

Outline. More optimizations for our interpreter. Types for objects

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

COS 320. Compiling Techniques

Typed Racket: Racket with Static Types

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

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

CSCI-GA Final Exam

CS558 Programming Languages

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Swift, functional programming, and does it matter? Alexis

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

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Part III Chapter 15: Subtyping

CSCE 314 Programming Languages. Type System

Topic 9: Type Checking

Topic 9: Type Checking

Exercise 8 Parametric polymorphism November 18, 2016

Types and Type Inference

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Subsumption. Principle of safe substitution

CMSC 330: Organization of Programming Languages

18-642: Code Style for Compilers

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Part III. Chapter 15: Subtyping

The Kotlin Programming Language. Andrey Breslav

Principles of Programming Languages

18-642: Code Style for Compilers

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

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Adding GADTs to OCaml the direct approach

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

An introduction introduction to functional functional programming programming using usin Haskell

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

Lecture 12: Data Types (and Some Leftover ML)

301AA - Advanced Programming [AP-2017]

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

Types and Programming Languages. Lecture 5. Extensions of simple types

Lecture 13: Subtyping

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

Protection Levels and Constructors The 'const' Keyword

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

The Typed Racket Guide

Advances in Programming Languages

The Haskell HOP: Higher-order Programming

Introduce C# as Object Oriented programming language. Explain, tokens,

Announcements. Written Assignment 2 Due Monday at 5:00PM. Midterm next Wednesday in class, 11:00 1:00. Midterm review session next Monday in class.

Exercise 12 Initialization December 16, 2016

CSE 431S Type Checking. Washington University Spring 2013

Types, Type Inference and Unification

Incremental Reasoning for Multiple Inheritance

Structural Typing for Structured Products

Rust. A safe, concurrent, practical language. Graydon Hoare October 2012

Overloading, Type Classes, and Algebraic Datatypes

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

CS 11 Haskell track: lecture 1

Introducing Scala-like function types into Java-TX

22c:111 Programming Language Concepts. Fall Types I

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

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

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Programming Languages and Techniques (CIS120)

Types. What is a type?

Topic 7: Algebraic Data Types

Semantic Analysis and Type Checking

301AA - Advanced Programming [AP-2017]

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into

Compilers and computer architecture: Semantic analysis

A Practical Approach to Programming With Assertions

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

CS558 Programming Languages

Pierce Ch. 3, 8, 11, 15. Type Systems

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

The role of semantic analysis in a compiler

The Kotlin Programming Language. Andrey Breslav Dmitry Jemerov

Proof Carrying Code(PCC)

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Algebraic data types. The case against null

Exercise 12 Initialization December 15, 2017

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Runtime Checking for Program Verification Systems

On Orthogonality. by remembering only m + n things, we get m * n capabilities.

Symmetry in Type Theory

Objects and Iterators

Javarifier: inference of reference immutability

Transcription:

The Hack programming language: Types for PHP Andrew Kennedy Facebook

Facebook s PHP Codebase 350,000 files >10,000,000 LoC (www.facebook.com & internally) 1000s of commits per day, 2 releases per day Anecdotally, good engineers are really productive in PHP And yet

1 + 2 3

How far does this go? 1ne + 2wo 3

How far does this go? 15 + 0xF 15

How far does this go? 15 == 0xF true

What if it s not a numeric string? hagfish + 2 2

OK, it treats non-numeric strings as zero. hagfish 9000000 yqwvysx

It gets worse. $n = hagfish ; $n++; hagfisi

It gets even worse. It never ends. $n = z ; $n++; aa

What to do? Types! * *And quite a lot of other things: remove features (e.g. variable variables ), add async, typed XML syntax,

Some history First focus for Facebook was performance of PHP 2009: HipHop: translation from PHP to C++ 2013: HHVM: highly performant runtime used by FB, Wikipedia, Dynamic checking of type hints => static type system => Hack (Julien Verlaguet). Really two projects A programming language project (this talk) A systems project (make it scale to 10M lines, with parallelism, background incremental type checking, etc.) Also see Flow (Akiv Chaudhuri), a similar effort for Javascript Types matter at Facebook!

Hack: types for PHP Object-oriented type system with generics in the style of Java, C# or Scala Some structural subtyping (tuples, shapes, functions) ML-like type inference based on unification Flow-sensitive typing for locals Type refinement for isnull/type tests Internal use of union types and recursive types ML-style abstract types Gradual typing for mixed code (PHP, Hack)

Pragmatism We re typing code that already exists! Lots of special casing for common PHP idioms Driven by need to convert millions of lines of code & convert hundreds of reluctant developers. => Language has a materials in the room feel to it. Materials being drawn from many years of p. l. research

Rich object-oriented type system Primitive types, named classes, interfaces, and traits, with static and virtual methods Generic type parameters on types and methods, with variance annotations and lower/upper bounds Maybe/option type Named class abstract class ChunkIterable<Tk, +Tv> { abstract public function getiterator(): AsyncIterator<ResultChunk<Tk, Tv>>; Type parameter Covariant type parameter abstract protected function getiteratorwithcursor(?chunkcursor $from,?chunkcursor $to, bool $iterate_backwards): AsyncIterator<(ChunkCursorMaker<Tk>, ResultChunk<Tk, Tv>)>; Lower bound final public function filter<tu super Tv>( IChunqPredicate<Tu> $predicate): ChunkIterable<Tk, Tu> { return new FilteredChunkIterable($this, $predicate);

More inference than Java, C# or Scala Type annotation on function arguments and results only Types inferred for locals; type parameters inferred for new and generic methods class List<T> { function MakeSingleton<T>(T x): List<T> {... function foo(int $b): void { $y = new List(); $y->add($b); $z = new List(); $z->add($y); $s = MakeSingleton($z); Inferred to be List<List<int>> Type parameter is inferred Note: no type-based overloading! (contrast Java, C#)

Flow-sensitive typing of locals Locals aren t even declared in PHP function f( $b) { if ($b) { What types can we write on $x = b ; parameter and result? bar($x); $x = 12; else { $x = a ; return $x;

Flow-sensitive typing of locals Locals aren t even declared in PHP function f(bool $b): mixed { if ($b) { What types can we write on $x = b ; parameter and result? bar($x); $x = 12; else { $x = a ; return $x;

Flow-sensitive refinement Types in Hack do not contain null by default. Must write?type to include null. At last! Tony Hoare s billion-dollar mistake, rectified Null tests in conditionals refine the type inside the branch Similarly, can test dynamic class using instanceof function foo(?int $xopt):int { if ($xopt == null) { return 42; else { return $xopt; function bar(widget $a):void { if ($a instanceof Button) { $a->click(); else { $a->dosomethinggeneric(); Type of $xopt is now int

Flow-sensitive refinement: expressions Types of some expressions can be refined. But care needed! class C { private?int $f = 0; function settonull(): void { $this->f = null; function get(): int { if ($this->f == null) { return 0; else { return $this->f; Type of $this->f is now int

Flow-sensitive refinement: expressions Types of some expressions can be refined. But care needed! class C { private?int $f = 0; function settonull(): void { $this->f = null; function get(): int { if ($this->f == null) { return 0; else { $this->settonull(); Type is invalidated by return $this->f; function call TYPE ERROR!

Internal types Internally, Hack uses a kind of union type for flow-sensitive typing class A { function Foo():int {... class B { function Foo():string {... function foo(bool $b, A $x, B $y): mixed { if ($b) { $obj = $x; int string is a subtype of mixed else { $obj = $y; $result = $obj->foo(); Hack gives $result the type int string Hack gives $obj the type A B

Structural typing PHP uses arrays a lot. Arrays can be indexed by integer or string; they re extensible; and values are dynamically typed. PHP arrays are often used in idiomatic ways; these are reflected in structural types in Hack: Tuples e.g. tuple(int, string, MyClass) Associative maps e.g. array<string,item> Records a.k.a. shapes e.g. shape('id' => int, 'name' => string) Also: function types, with proper co/contra-variant subtyping

Type abstraction Where they are used, types are surprisingly strong Abstraction is enforced on enumeration types (contrast C#) enum NodeColour : int = { Red = 0; Black = 1; No type compatibility between NodeColour and int Opaque types, with optional supertype newtype UserId = string; Outside file, no compatibility between UserId and string

Gradual typing Hack code is marked as strict, partial or decl Strict code has full type annotations and is fully type checked. It cannot call into legacy PHP code Partial code has optional annotations and is type checked as much as it can. It can call into legacy PHP code Decl code is not checked; but type annotations are processed for use by other files Where types are omitted, Hack assumes an any type that is compatible with all other types Contrast mixed which is the top type w.r.t. subtyping

Type safety The intention is that strict mode code is type safe But no soundness theorem; and what would it say about mixed code? Also, plenty of back doors e.g. invariant construct

Implementation Hack is implemented in Ocaml Core of type checker is purely functional Open sourced on github: see http://hacklang.org

That's all folks. Questions?