Presentation is loading. Please wait.

Presentation is loading. Please wait.

Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07.

Similar presentations


Presentation on theme: "Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

1 Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07

2 2005/06Types and Programming Languages Lecture 16 - Simon Gay2 Data Structures with Linear Types It makes sense to consider data structures in a linear type system. Of course we must be careful with the typing rules. Pairs The natural concept is a pair of values, both of which will be used exactly once. The type of linear pairs is traditionally written T  U (pronounced “tensor”). If we have e : T  U then both components must be used. This makes it tricky to work with fst and snd because they discard the other component.

3 2005/06Types and Programming Languages Lecture 16 - Simon Gay3 Linear Pairs:  The easiest way to formulate extraction of components of  is to use a split construct: split e as (x,y) in e’ When you have worked out the typing rules for split in the non-linear case (for the assessed exercise), you will be able to see how it works in the linear case. The typing rule for construction of linear pairs is: Linear record types can be considered similarly, using a split instead of field projection.

4 2005/06Types and Programming Languages Lecture 16 - Simon Gay4 Linear Pairs and Currying Now we have a dual view of linear functions of two parameters, analogous to the familiar curried/uncurried view in functional programming. A  B –o C is the type of a function which is given a pair of values and uses them both to produce a result. A –o B –o C is the type of a curried function which is given two values separately and uses them both to produce a result.

5 2005/06Types and Programming Languages Lecture 16 - Simon Gay5 Linear Pairs: & There is a second kind of linear pair. T & U (pronounced “with”) is a type of pairs such that one or other of the components must be used, but not both. For this kind of pair, projections make sense: fst : A & B –o A snd : A & B –o B The typing rule for pair formation is: Note the same environment  everywhere.

6 2005/06Types and Programming Languages Lecture 16 - Simon Gay6 Linear Pairs: & The type constructor & is what’s needed to convert the conditional into a function (we would also want call-by-name): cond : bool  (T & T) –o T Exercise: extend the proof of linear type safety to both kinds of linear pair.

7 2005/06Types and Programming Languages Lecture 16 - Simon Gay7 Combining Linear and Non-Linear Data In a practical programming language we would usually want to combine linear and standard (non-linear) constructs. A general way of doing this is described by David Walker in Chapter 1 of “Advanced Topics in Types and Programming Languages” (edited by Benjamin Pierce). Every type constructor can be defined in linear and unlimited versions. ( T & U doesn’t fit into this pattern.)

8 2005/06Types and Programming Languages Lecture 16 - Simon Gay8 Combining Linear and Non-Linear Data The typing rules can be generalized so that they handle both linear and non-linear parts of the environment. A nice way of doing this is to define a partial operation + on environments:  + x:T = , x:Tif x    + x:T =  if T is unlimited and x:T    + x:Tis undefined otherwise  +  is defined by adding the entries in  one at a time to .

9 2005/06Types and Programming Languages Lecture 16 - Simon Gay9 Combining Linear and Non-Linear Data Typing rules are defined in the following way: with the implicit hypothesis that  +  must be defined. This covers both the non-linear (must be the same) and linear (put them together) entries in the environment. Furthermore, we must not construct an unlimited data structure containing linear data.

10 2005/06Types and Programming Languages Lecture 16 - Simon Gay10 Combining Linear and Non-Linear Data For example, the rule for constructing an unlimited function is The rules for constants and variables allow unlimited entries in the environment to be discarded: For full details, read Walker’s chapter.


Download ppt "Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07."

Similar presentations


Ads by Google