data abstraction

33
Data Abstraction COS 441 Princeton University Fall 2004

Upload: bena

Post on 21-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Data Abstraction. COS 441 Princeton University Fall 2004. Existential Types. Powerful mechanism for providing data-abstraction Useful for Lists of Heterogeneous Values Typed Closure Conversion Encoding Modules. Heterogeneous Lists Revisited. Consider a heterogonous list of the form - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Abstraction

Data Abstraction

COS 441

Princeton University

Fall 2004

Page 2: Data Abstraction

Existential Types

Powerful mechanism for providing data-abstraction

Useful for

Lists of Heterogeneous Values

Typed Closure Conversion

Encoding Modules

Page 3: Data Abstraction

Heterogeneous Lists Revisited

Consider a heterogonous list of the form

val x : ?? list = [1, true, “hello world”] :

how can we implement a generic print function?

val printList : ?? list ! unit

Page 4: Data Abstraction

Tagged List

Simple answer is to use tagged datatypedatatype tagged = I of int | B of bool | S of

stringval x = [I 1,B true, S “hello world”]fun tagged2s (I i) = Int.toString i | tagged2s (B b) = Bool.toString b

| tagged2s (S s) = s fun printList l = …

Page 5: Data Abstraction

Problems With Tagged Approach

We must know the type of every value we might want to tag.

If a user creates a new type, they have to modify the tagged2s function as well as the tagged datatype

Not a modular approach

Page 6: Data Abstraction

A More Modular Approach

Create a list of “objects” which consist of a value and a function to convert it into a string

type obj = ??

val x : obj list = [(1,Int.toString),

(true,Bool.toString),

(s,(fn x => x))]

fun obj2s (x,f) = f x

Page 7: Data Abstraction

Typing Problems

It doesn’t type check in SML

We can get a similar effect with HOF

type obj = unit ! string

val x : obj list = [(fn () => Int.toString 1),

(fn () => Bool.toString true),

(fn () => s)]

fun obj2s f = f ()

Page 8: Data Abstraction

Extending the Type System

The type obj should capture the pattern

obj = ( * ( ! string))

for some arbitrary 9 t(t * (t ! string))

Provides a modular solution that avoids a fixed non-modular tagging scheme.

Page 9: Data Abstraction

Closure Conversion

Remember functions represented in runtime as a pair “environment” and function body.

Considertype obj = unit ! stringfun IntObj i = (fn () => Int.toString i)let val obj = IntObj 10in obj ()

end

Page 10: Data Abstraction

Closure Representation

Can represent environments as tuplesfun IntObj (_,i) =

(i,fn (i,()) => Int.toString i)let val (fenv,f) = IntObj (glbl_env,10)in f (fenv,())

endSize and type of tuple depends on number

of free vars. in body of function

Page 11: Data Abstraction

Typed Closure Conversion

Closure conversion done by the compiler user doesn’t care what goes on or how the environment is represented!

However, useful to be able check the output of the compiler for debugging and security reasons

Also notice that closures are just like our “object” type i.e. a piece of private data stored with something that knows how to manipulate it

We can us existential to make the result of the compiler type checkable!

Page 12: Data Abstraction

Existential Types

Page 13: Data Abstraction

Example: pack

type obj = 9 t((t * (t ! string))

pack int with (1,Int.toString)

as 9 t((t * (t ! string))

Note: type of (1,Int.toString) =

(int * (int ! string)) [t int](t * (t ! string)

Page 14: Data Abstraction

Example: open

fun obj2s (obj:9 t((t * (t ! string))) =

open obj as t with x:(t * (t ! string)) in

let val (v:t,v2s:(t! string)) = x

in v2s v

end

Page 15: Data Abstraction

Static Semantics

Page 16: Data Abstraction

Dynamic Semantics

Page 17: Data Abstraction

Modules and Existentials

Can be encode with the type

9q((q * ((int * q) ! q) * (q ! (int * q))))

Page 18: Data Abstraction

Representation Independence

A simple reference implementation of a queuepack int list with

(nil,fun insert …,fun remove …)as 9q((q * ((int * q) ! q) * (q ! (int * q))))

Page 19: Data Abstraction

Client of a Module

open QL as q

with <e,i,r> : (q * ((int * q) ! q) * (q ! (int * q)))

in

end

Page 20: Data Abstraction

Representation Independence

• Imagine we have to “equivalent” implementations of a module

• The behave externally the same way but may use different internal representations of data structures

• A client should work correctly assuming the implementations are equivalent

Page 21: Data Abstraction

Example

Consider the slightly more efficient representation of a queue

Page 22: Data Abstraction

Formal Definition

Let q be (q * ((int * q) ! q) * (q ! (int * q)))

Let q be 9q(q)

Let the expression that represents the module QL be vQL:q and the expression that represents the module QFB be vQFB:q

We wish to show for any client code ec :c

open vQL as q with x:q in ec :c

behaves equivalently to

open vQFB as q with x:q in ec:c

Page 23: Data Abstraction

What Definition of Equivalent?

We can use the framework of parametricity to define equivalence at some type

Page 24: Data Abstraction

Definition of Value Equivalence

Define a notion of equivalence of closed values indexed by type

Page 25: Data Abstraction

Definition of Value Equivalence

Interesting case is definition of “all”

Page 26: Data Abstraction

Definition of Value Equivalence

Definition of “some”

Some simulation relation

Page 27: Data Abstraction

Alternative View

` open ei using ec:c

` ec:8t(t ! c) ` c ok

Key observation that relates data-abstraction and parametricity

Page 28: Data Abstraction

Client Equivalence

So to because the client is just a polymorphic function and from the parametricity theorem we can conclude

if we know v1 and v2 are equivalent existential packages

Page 29: Data Abstraction

Equivalence of Modules

Definition of “some”

To show modules equivalent choose some R and show implementations are equivalent under R

Some simulation relation

Page 30: Data Abstraction

vQL val vQFB : q

First we must choose some RLet R be

Then assuming that v1 val v2 : q iff (v1,v2) 2 Rshow

1. QL.empty val QFB.empty : q2. QL.insert val QFB.insert : (int * q) ! q3. QL.remove val QFB.remove: q ! (int * q)

Page 31: Data Abstraction

Note About Proofs

Proofs are tedious

Have you ever seen an interesting proof yet?

Formalisms we present in class are to make you aware that precise formal definitions exist for abstraction and modularity

Page 32: Data Abstraction

What Do I Need To Know?

I had to read through Harper’s notes several times carefully to understand them!

Therefore I won’t assign a homework/test question about formal notions of parametricity/data abstraction!

However, it is educational to understand the high-level structure of the definitions and how they relate to one another

Page 33: Data Abstraction

Summary

Existential type allow you to enforce data-abstraction

Data-abstraction exists because “client” code is polymorphic with respect to the actual type

Can encode modules, closures, and objects with existential types in a type safe way