Download presentation
Presentation is loading. Please wait.
1
Refinement types for ML By Tim Freeman, Frank Pfenning @ CMU Presented by: Andrey Utis
2
The Dilemma Find the last element in a list: fun last (x::nil) = x | last (x::L) = last(L); What is last(nil) ? Solution #1: exceptions –non-exhaustive match failure (SML) Solution #2: refinement types –last: `a non-empty-list -> `a
3
Refinement types A is a refinement of B iff every element of type A also has type B Finding the best refinement type is undecidable in general –function can run M on x Solution: let the programmer specify the refinement types
4
Example Standard list type: datatype `a list = nil | cons of `a * `a list Non-empty list refinement type: rectype `a nonemptylist = cons of `a * `a list Singleton list refinement type: rectype `a singleton = cons(`a, nil)
5
Refinements as lattices We can think of refinements as lattices list singleton OR nil singletonnil ?
6
Decidability Decidable since the lattices have finite depth This is why we needed refinements to be specified by the programmer
7
Abstract Interpretation Perform abstract interpretation using the lattice Need a “constructor” function – e.g. how do we get a singleton from another type? This can be derived from the “rectype” definition
8
Example of cons Singleton cons(`a * nil) -> `a singleton cons(`a * `a singleton) -> `a list cons(`a * `a list) -> `a list
9
Functions & polymorphism How do we deal with functions? –Contravariance a->b refines c->d iff c refines a and b refines d Polymorphism – use bounded quantification – 8 ra::a ra->ra –almost the same as standard type inference
10
The algorithm First infer standard ML types, then refine Additional inference rules: –contravariance –refinement transitivity
11
Inference rules LOOP is used for successive approximations of recursive function types The new inference rules: – |- LOOP(f, y.e, ?, L) – |-LOOP(f, y.e, C 1, L); f:C 1 ::L |- y.e:C 2 ::L => |- LOOP(f, y.e, C 2, L) – |-LOOP(f, y.e, C, L); f:C::L |- y.e:C::L => |- fix f. y.e:C::L
12
Soundness Theorem: for all valid type environments and expressions e, if e evaluates to v and |- e:D::L then |- v:D`::L for some D` D Proof is by induction on the structure of
13
Implementation There is an implementation for Mini-ML Theory vs Practice datatype maybe = true|false|maybe rectype tt=true and ff=false and tf=true|false let f x = fun true->false | false -> x | maybe->false Theory: f:maybe->maybe Practice: f:maybe->ff
14
Conclusion Decidable version of type refinement Efficiency can be a problem Type refinements can be thought of as implicit documentation May be used to make execution faster
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.