Download additional notes - School of Computing Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Stream processing wikipedia , lookup

C Sharp syntax wikipedia , lookup

C syntax wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Functional programming wikipedia , lookup

C++ wikipedia , lookup

Programming language wikipedia , lookup

Least squares wikipedia , lookup

Reactive programming wikipedia , lookup

Simplex algorithm wikipedia , lookup

Structured programming wikipedia , lookup

Linear algebra wikipedia , lookup

Corecursion wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Go (programming language) wikipedia , lookup

Transcript
Types and Programming Languages
Lecture 12a
Simon Gay
Department of Computing Science
University of Glasgow
2006/07
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.
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
2
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:
  e: T   f :U
,   (e, f ) : T  U
Linear record types can be considered similarly, using a split
instead of field projection.
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
3
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.
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
4
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:
  e: T   f :U
  e, f  : T & U
Note the same environment  everywhere.
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
5
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.
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
6
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.
int u
int l
T l U
T u U
T l U
T u U
( T & U doesn’t fit into this pattern.)
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
7
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:T
 + x:T = 
 + x:T
if x  
if T is unlimited and x:T  
is undefined otherwise
+ is defined by adding the entries in  one at a time to .
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
8
Combining Linear and Non-Linear Data
Typing rules are defined in the following way:
  e: T   f :U
    (e, f ) : T  U
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.
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
9
Combining Linear and Non-Linear Data
For example, the rule for constructing an unlimited function is
un lim ited( ) , x : T  e : U
  x : T.e : T u U
The rules for constants and variables allow unlimited entries in
the environment to be discarded:
un lim ited( )
  true : bool
un lim ited(,  )
, x : T,   x : T
For full details, read Walker’s chapter.
2005/06
Types and Programming Languages Lecture 16 - Simon Gay
10