A Recursive Type System with Type Abbreviations and Abstract Types - - PowerPoint PPT Presentation

a recursive type system with type abbreviations and
SMART_READER_LITE
LIVE PREVIEW

A Recursive Type System with Type Abbreviations and Abstract Types - - PowerPoint PPT Presentation

A Recursive Type System with Type Abbreviations and Abstract Types Keiko Nakata Institute of Cybernetics, Tallinn Joint work with Hyeonseung Im and Sungwoo Park 18 May 2014, Narva-J oesuu The ML module system The ML module system supports


slide-1
SLIDE 1

A Recursive Type System with Type Abbreviations and Abstract Types

Keiko Nakata Institute of Cybernetics, Tallinn Joint work with Hyeonseung Im and Sungwoo Park 18 May 2014, Narva-J˜

  • esuu
slide-2
SLIDE 2

The ML module system

The ML module system supports program structuring, code reuse and representation independence (implementation hiding) with

  • nested structures,
  • functoros, and
  • signatures (with abstract types).
slide-3
SLIDE 3

Make interval functor

module type Comparable = sig type t val compare : t → t → int end module Make interval(Endpoint : Comparable) = struct module E = Endpoint type t = Interval of E.t * E.t | Empty let create low high = ... let is empty = function Empty → true | Interval → false let contains t x = match t with | Empty → false | Interval (l,h) → E.compare x l ≥ 0 and E.compare x h ≤ 0 let intersect t1 t2 = ... end

slide-4
SLIDE 4

Instantiating Make interval

module Int interval = Make interval(struct type t = int let compare = Int.compare end) let i1 = Int interval.create 3 8 module Rev int interval = Make interval(struct type t = int let compare x y = Int.compare y x end) (* Int interval.t = Rev int interval.t *) let rev interval = Rev int interval.create 4 3 Int interval.contains rev interval 3

slide-5
SLIDE 5

Constraining the result type of functors

module type Interval intf = sig type t type endpoint val create : endpoint → endpoint → t val is empty : t → bool val contains : t → endpoint → bool val intersect : t → t → t end module Make interval(Endpoint : Comparable) : (Interval intf with type endpoint = Endpoint.t) = struct module E = Endpoint type endpoint = E.t type t = Interval of E.t * E.t | Empty ... end

slide-6
SLIDE 6

Polymorphic recursive types

are equi-recursive types:

  • µX.τ is equal to its one-step unfolding {X → µX.τ}τ.
  • Equivalence of equi-recursive types is structural.

Principle type infernece is available. Code reuse is archived with structural polymorphism. The module machinery can be combined with polymorphic recursive types, e.g., private types.

slide-7
SLIDE 7

Contractiveness

A type variable X is contractive in τ, if X occurs in τ only under a type constructor. A recursive type is contractive if every recursive variable is contractive in its scope. In a simple type language, contractiveness can be enforced syntactically τ, σ ::= X | τ → σ | µX.(τ → σ) Contractiveness guarantees the unique solution of recursive equations introduced by equi-recursive types.

slide-8
SLIDE 8

Contractiveness in OCaml

In an advanced type system, such as in OCaml... Syntactic contractiveness is not sufficient: type ’a t = [‘A of ’a | ‘B];; type s = s t;; We may not be able to know (without breaking type abstraction). module rec M : sig type t end = struct type t = N.t end and N : sig type t end = struct type t = M.t end

slide-9
SLIDE 9

Our work

A equi-recursive type system with type abbreviations and abstract types. We allow non-contractive types in the implementation, but disallow them in the signature. The type system is proved sound, formalized in Coq.

slide-10
SLIDE 10

Non-contractive types in the signature

module M : S = struct module type S = sig type ’a t = ’a type ’a t type u = int and v = bool type u = u t and v = v t let f x = x val f : int → u let g x = x val g : v → bool end end let h x = M.g (M.f x) let y = h 3 (* run-time error *) We found the bug together with Jacques Garrigue, which has been fixed in the latest release OCaml 4.00.1.

slide-11
SLIDE 11

Type language

type constructor s, t, u type name type τ, σ ::= unit base type | α | β | γ type variable | τ → σ function type | τ1 ∗ τ2 product type | τ t type application type abbreviation D ::= type α t = τ type equation abbreviation context ∆ ::= · | ∆, D type variable set Σ ::= · | {α}

slide-12
SLIDE 12

Expression language

value v ::= () | λa : τ.e | (v1, v2) term e ::= () | a | x | λa : τ.e | e1 e2 | (e1, e2) | l | fst e | snd e | fix a : τ.e value context Γ ::= · | Γ, x : τ

slide-13
SLIDE 13

Module language

specification D ::= type α t abstract type | type α t = τ type equation | val l : τ value specification definition dτ ::= type α t = τ type definition de ::= let l = e value definition signature S ::= · | S, D structure M ::= (dτ, de) program P ::= (M, S, e) signature sealing | (M, e)

slide-14
SLIDE 14

Type equivalence

The judgment S ⊢ τ ⇀ σ states that type τ unfolds into σ by expanding a type name in τ into its definition under S. ∆ ∋ type α t = σ ∆ ⊢ τ t ⇀ {α → τ}σ unfold

slide-15
SLIDE 15

Type equivalence

Inductive type equivalence ∆; Σ ⊢ τ1

R

= τ2 ∆; Σ ⊢ unit R = unit eq-unit α ∈ Σ ∆; Σ ⊢ α R = α eq-var ∆; Σ ⊢ τi R σi (i = 1, 2) ∆; Σ ⊢ τ1 → τ2

R

= σ1 → σ2 eq-fun ∆; Σ ⊢ τi R σi (i = 1, 2) ∆; Σ ⊢ τ1 ∗ τ2

R

= σ1 ∗ σ2 eq-prod S ∋ type α t S; Σ ⊢ τ R σ S; Σ ⊢ τ t R = σ t eq-abs ∆ ⊢ τ ⇀ τ ′ ∆; Σ ⊢ τ ′ R = σ ∆; Σ ⊢ τ R = σ eq-lunfold ∆ ⊢ σ ⇀ σ′ ∆; Σ ⊢ τ R = σ′ ∆; Σ ⊢ τ R = σ eq-runfold

slide-16
SLIDE 16

Type equivalence

Inductive type equivalence ∆; Σ ⊢ τ1

R

= τ2 ∆; Σ ⊢ unit R = unit eq-unit α ∈ Σ ∆; Σ ⊢ α R = α eq-var ∆; Σ ⊢ τi R σi (i = 1, 2) ∆; Σ ⊢ τ1 → τ2

R

= σ1 → σ2 eq-fun ∆; Σ ⊢ τi R σi (i = 1, 2) ∆; Σ ⊢ τ1 ∗ τ2

R

= σ1 ∗ σ2 eq-prod S ∋ type α t S; Σ ⊢ τ R σ S; Σ ⊢ τ t R = σ t eq-abs ∆ ⊢ τ ⇀ τ ′ ∆; Σ ⊢ τ ′ R = σ ∆; Σ ⊢ τ R = σ eq-lunfold ∆ ⊢ σ ⇀ σ′ ∆; Σ ⊢ τ R = σ′ ∆; Σ ⊢ τ R = σ eq-runfold

slide-17
SLIDE 17

Type equivalence

Coinductive type equivalence ∆; Σ ⊢ τ1 ≡ τ2 ∆; Σ ⊢ τ ≡ = σ ∆; Σ ⊢ τ type ∆; Σ ⊢ σ type ∆; Σ ⊢ τ ≡ σ eq-ind ∆; Σ ⊢ τ type ∆; Σ ⊢ σ type ∆ ⊢ τ ⇀ τ ′ ∆ ⊢ σ ⇀ σ′ ∆; Σ ⊢ τ ′ ≡ σ′ ∆; Σ ⊢ τ ≡ σ eq-coind

slide-18
SLIDE 18

Contractive types and signatures

S ↓⇓ τ S ⇓ τ ctr-coind S ↓C unit ctr-unit S ↓C α ctr-var (S, τ) ∈ C (S, σ) ∈ C S ↓C τ → σ ctr-fun (S, τ1) ∈ C (S, τ2) ∈ C S ↓C τ1 ∗ τ2 ctr-prod S ∋ type α t S ↓C τ S ↓C τ t ctr-abs S ⊢ τ ⇀ σ S ↓C σ S ↓C τ ctr-type BN(S) distinct ∀(type α t = τ) ∈ S, S ⇓ τ S ⇓ ctr-sig

slide-19
SLIDE 19

Type soundness of λrec

abs

The key lemma in the soundness states that a well-formed type is contractive:

Lemma

Suppose S ok, S ⇓, and S; Σ ⊢ τ type. Then S ⇓ τ. which enables us to prove that type equivalence is preserved by signature elimination:

Lemma

If S1 ≦ S2, S2 ⇓, and S2; Σ ⊢ τ ≡ σ, then S1; Σ ⊢ τ ≡ σ.

Theorem

The type system for λrec

abs is sound.

(We prove the progress and preservation properties.)