Polymorphism, subtyping and type inference in MLsub

Stephen Dolan and Alan Mycroft September 3, 2015 Computer Laboratory University of Cambridge

Avoiding the hard problems

Type inference with subtyping is traditionally considered difficult.

We have two tricks for getting around the difficulties: • Define types properly • Only use half of them

2

Avoiding the hard problems

Type inference with subtyping is traditionally considered difficult.

We have two tricks for getting around the difficulties: • Define types properly • Only use half of them

2

Standard definition of types and subtyping Types are either ⊥, > or functions: τ = ⊥|>|τ →τ

3

Standard definition of types and subtyping Types are either ⊥, > or functions: τ = ⊥|>|τ →τ ⊥ is the least, > is the greatest, and functions have contravariant arguments and covariant results: ⊥≤τ

τ ≤>

τ10 ≤ τ1 τ2 ≤ τ20 τ1 → τ2 ≤ τ10 → τ20

3

Lattice structure

Any two types have a greatest common subtype (u) and a least common supertype (t).

For instance: (τ1 → τ2 ) t (τ3 → τ4 ) = (τ1 u τ3 ) → (τ2 t τ4 ) Quite a well-behaved structure!

4

Bizzarely difficult questions Is this true, for all α? α→α≤⊥→>

5

Bizzarely difficult questions Is this true, for all α? α→α≤⊥→> How about this? (⊥ → >) → ⊥ ≤ (α → ⊥) t α

5

Bizzarely difficult questions Is this true, for all α? α→α≤⊥→> How about this? (⊥ → >) → ⊥ ≤ (α → ⊥) t α

Yes, it turns out, by case analysis on α.

5

Bizzarely difficult questions Is this true, for all α? α→α≤⊥→> How about this? (⊥ → >) → ⊥ ≤ (α → ⊥) t α

Yes, it turns out, by case analysis on α. And only by case analysis. 5

Extensibility

If a program typechecks, it should still typecheck when more types are added

This is a useful property: • New versions of the language • User-defined types • Seperate compilation 6

Do we have extensibility? (⊥ → >) → ⊥ ≤ (α → ⊥) t α

Let’s add a new type of functions τ1 subtypes of -types. Now, α = (>

⊥)

τ2 , so that →-types are

⊥ is a counterexample!

The previous reasoning relied on the non-existence of

.

7

Do we have extensibility? (⊥ → >) → ⊥ ≤ (α → ⊥) t α

Let’s add a new type of functions τ1 subtypes of -types. Now, α = (>

⊥)

τ2 , so that →-types are

⊥ is a counterexample!

The previous reasoning relied on the non-existence of

.

We need an open-world definition: τ = α|τ tτ |τ uτ |⊥|>|τ →τ 7

Avoiding the hard problems

Type inference with subtyping is traditionally considered difficult.

We have two tricks for getting around the difficulties: • Define types properly • Only use half of them

8

Avoiding the hard problems

Type inference with subtyping is traditionally considered difficult.

We have two tricks for getting around the difficulties: • Define types properly • Only use half of them

8

Input and output types τ t τ 0 : this code produces a value which is a τ or a τ 0 τ u τ 0 : this code requires a value which is a τ and a τ 0 t is only useful for outputs, and u is only useful for inputs.

9

Input and output types τ t τ 0 : this code produces a value which is a τ or a τ 0 τ u τ 0 : this code requires a value which is a τ and a τ 0 t is only useful for outputs, and u is only useful for inputs. Divide types into output types τ + and input types τ − :

τ + ::= α | τ + t τ + | ⊥ | τ − → τ + τ − ::= α | τ − u τ − | > | τ + → τ −

9

Polymorphism

Here’s a function that takes two values and returns one of them: choose : α1 → α2 → α3

10

Polymorphism

Here’s a function that takes two values and returns one of them: choose : α1 → α2 → α3 In ML, you must have α1 = α2 = α3 . With subtyping, you must have α1 ≤ α3 , α2 ≤ α3 . α1 and α2 may be incomparable.

10

Polymorphism

Here’s a function that takes two values and returns one of them: choose : α1 → α2 → α3 In ML, you must have α1 = α2 = α3 . With subtyping, you must have α1 ≤ α3 , α2 ≤ α3 . α1 and α2 may be incomparable. choose : τ1− → τ2− → τ3+

10

Cases of unification

In HM inference, unification happens in three situations: • Unifying two input types • Unifying two output types • Using the output of one expression as input to another

11

Cases of unification

In HM inference, unification happens in three situations: • Unifying two input types • Unifying two output types • Using the output of one expression as input to another We handle these as: • Introducing u • Introducing t • Decomposing a τ + ≤ τ − constraint

11

Decomposing constraints

We only need to decompose constraints of the form τ + ≤ τ − .

τ1 t τ2 ≤ τ3



τ1 ≤ τ3 , τ2 ≤ τ3

τ1 ≤ τ2 u τ3



τ1 ≤ τ2 , τ1 ≤ τ3

Thanks to the input/output type distinction, the hard cases of τ1 u τ2 ≤ τ3 and τ1 ≤ τ2 t τ3 can never come up.

12

Typechecking OCaml’s List module

Most of the OCaml List module typechecks, with the same types as OCaml (modulo syntax annoyances, and unimplemented features).

13

Questions? http://www.cl.cam.ac.uk/~sd601/mlsub [email protected]

14

15

Defining types extensibly Include variables and lattice operations in the definition of types: τ = α|τ tτ |τ uτ |⊥|>|τ →τ Define some rules about when types are equal: (τ1 → τ2 ) t (τ3 → τ4 ) = (τ1 u τ3 ) → (τ2 t τ4 ) ... This is the standard construction of a free algebra, and variables aren’t presumed to be divided into cases. (⊥ → >) → ⊥ 6≤ (α → ⊥) t α 16

Mutable references References are generally considered “invariant”. Instead, consider ref a two-argument constructor (α, β) ref with operations: make : (α, α) ref get : (⊥, β) ref → β set : (α, >) ref → α → unit

17

Polymorphism, subtyping and type inference in MLsub - ML Family ...

Sep 3, 2015 - Polymorphism, subtyping and type inference in. MLsub. Stephen Dolan and Alan Mycroft ... We have two tricks for getting around the difficulties: • Define types properly. • Only use half of them. 2 ... Any two types have a greatest common subtype (⊓) and a least common supertype (⊔). For instance: (т1 → т2) ...

114KB Sizes 0 Downloads 235 Views

Recommend Documents

Polymorphism, subtyping and type inference in MLsub - ML Family ...
Sep 3, 2015 - Polymorphism, subtyping and type inference in. MLsub. Stephen Dolan and Alan Mycroft ... We have two tricks for getting around the difficulties: • Define types properly. • Only use half of them. 2 ... Any two types have a greatest c

Arduino programing of ML-style in ATS - ML Family Workshop
binaries generated from ATS source are very close (in terms of size) to those generated from the C counterpart. 2. ATS programming language. ATS is a programming language equipped with a highly expressive type system rooted in the framework Applied T

Nullable Type Inference - OCaml
Dec 11, 2002 - Imperative programming languages, such as C or Java deriva- tives, make abundant ... In languages using the ML type discipline, the option type type α option ..... //docs.hhvm.com/manual/en/hack.nullable.php. [3] Facebook ...

Nullable Type Inference - OCaml
Dec 11, 2002 - [1] Apple (2014): Swift, a new programming language for iOS and. OS X. Available at https://developer.apple.com/swift. [2] Facebook (2014): ...

Mergeable Types - ML Family Workshop
systems with the ability to define and compose distributed ML computations around ... library on a single machine, this implementation behaves as expected.

Sundials/ML: interfacing with numerical solvers - ML Family Workshop
Sep 22, 2016 - 4. REFERENCES. [1] T. Bourke and M. Pouzet. Zélus: A synchronous language with ODEs. In HSCC, pages 113–118. ACM. Press, Apr. 2013.

Sundials/ML: interfacing with numerical solvers - ML Family Workshop
Sep 22, 2016 - [email protected]. Jun Inoue. National Institute of Advanced. Industrial Science and. Technology. [email protected]. Marc Pouzet. Univ. Pierre et Marie Curie. École normale supérieure,. PSL Research University. Inria Paris.

Tierless Modules - The ML Family Workshop
Web, client/server, OCaml, ML, Eliom, functional, module. 1 INTRODUCTION. Traditional Web applications are composed of several dis- tinct tiers: Web pages ...

Ambiguous pattern variables - The ML Family Workshop
Jul 29, 2016 - Let us define .... where the Bi,k are binding sets, sets of variables found ... new rows bind to a different position. [Bi,1 ... Bi,l. | K(q1,...,qk) pi,2.

Relational Conversion for OCaml - ML Family Workshop
preters (Programming Pearl) // Proceedings of the 2012 Work- shop on Scheme and Functional Programming (Scheme '12). [5] Henk Barendregt. Lambda ...

Practical Type Inference for the GADT Type System
opportunity to develop my interests in computer science and to pursue graduate ...... algebraic data types that I made in the course of this dissertation research. Here ..... APP This rule types a function application by typing the function (f) and i

Practical Type Inference for the GADT Type System
Portland State University ... opportunity to develop my interests in computer science and to pursue graduate ..... 7.13 Type inference for reified state monad (1).

GADTs and exhaustiveness: looking for the impossible - ML Family ...
... !env expected_ty) expected_ty k else k (mkpat Tpat_any expected_ty). | Ppat_or (sp1, sp2) -> (* or pattern *) if mode = Check then let state = save_state env in try type_pat sp1 expected_ty k with exn ->. 3The code is available through OCaml's Su

GADTs and exhaustiveness: looking for the impossible - ML Family ...
log's SLD resolution, for which counter-example genera- tion (i.e. construction of a witness term) is known to be only semi-decidable. Another way to see it is that ...

Type Inference Algorithms: A Survey
An overview of type inference for a ML-like language can be found ..... this convention early on because it was built into some of the Coq libraries we used in our.

A Type-Safe Embedding of XDuce into ML
We would like to stress again that the ML (application) programmer does not need to know ... of the designer of these data types to provide the necessary rules.

A Type-Safe Embedding of XDuce into ML
However, it is difficult to promote their widespread use due to limited library support and ... We give a type and semantic preserving translation from XDuce to ML.

Compliance and subtyping in timed session types
If p is subtype of p, then all services interacting correctly with p must interact correctly also with p . Formally, let p⊳⊲ = {q | p ⊳⊲ q} p ⊑ p whenever p. ⊳⊲. ⊇ p⊳⊲. Example: p = ?zip.(!weather + !abort). ⊑ p = ?zip.!weather

Relational Conversion for OCaml - The ML Family Workshop
St.Petersburg State University .... Logic in Computer Science (Vol. 2), 1992. [6] William E. ... Indiana University, Bloomington, IN, September 30, 2009. [7] Dmitry ...

Typer: An infix statically typed Lisp - The ML Family Workshop
Oxford, UK, September 2017 (ML'2017), 2 pages. ... the syntax of macro calls is just as exible as that of any other .... Conference on Functional Programming.

VOCAL – A Verified OCAml Library - ML Family Workshop
OCaml is the implementation language of systems used worldwide where stability, safety, and correctness are of ... An overview of JML tools and applications.

VOCAL – A Verified OCAml Library - ML Family Workshop
Libraries are the basic building blocks of any realistic programming project. It is thus of utmost .... verification of object-oriented programs. In 21st International ...