Title: Subtyping
1Subtyping
- The type A is a subtype of B iff
- Any element of A is an element of B.
- If two elements are equal in A then they are
equal in B.
Examples A ? Top Void ? A xA Px ? A A ?
A//R 1,2,3 ? Z ? Z6 ? Z2
2Subtyping (cont.)
The relation A ? B is treated as a new type as
well as a new proposition.
A type B type A ? B type
It makes sense to negate A ? B. For example, we
know that N ? N ? N is false since no pair ltn,mgt
can be equal to ) nor to s(k) for any k. (For N
and N ? N we know that ab implies ab.)
3Subtyping relations
If A ? A and B ? B then
4Intersection types
If A is a type and for each x ? A, B(x) is a
type, then ? xA.B(x) is a type.
b ? ? xA.B(x) iff b ? B(a) for every a ? A
5Intersection types (cont.)
b ? B(a) for every a ? A b b? in ?
xA.B(x) iff b b? in B(a) for every a ? A
6Intersection
An element x is in the intersection of A and B if
and only if x is in A and in B. And two elements
are equal in A ? B iff they are equal both in A
and in B.
We have 1. A ? B ? A 2. A ? B ? B 3. If C ? A
and C ? B then C ? A ? B
Example Z4 ? Z6 Z12
7Union
An element x is in the union of A and B if and
only if x is in A or in B. The equivalence
relation of A ? B is the transitive closure of
equivalence relations of A and B.
We have 1. A ? A ? B 2. B ? A ? B 3. If A ? C
and B ? C then A ? B ? C
Example Z4 ? Z6 Z2
8Dependent intersection
xA ? Bx
is a set of all elements x, such that x ? A and x
? Bx.
Set type as dependent intersection
xAPx xA ? Px
Axioms
9Records and variant types
RECORDS
xA x ? A xAyBzC xA ? yB ?
zC
VARIANT TYPES
x of A x ? A (x of Ay of Bz of C) (x
of A) ? (y of B) ? (z of C)
10Records naïvely
There are many ways to capture the concept of a
record. For example
xA yB zC
can be defined as A ?(B ? C) and the field
selectors can be defined as functions on tuples,
say
x ?(r.1of(r)) y ?(r.1of(2of(r))) z
?(r.2of(2of(r)))
11Naïve record extension
We can provide for record extension by adding Top
as a last component of any record
Records TUi ? Top
We build up the previous record as follows
R1 A ? Top R2 A ?(B ? Top) R3 A ?(B
?(C ? Top))
12Naïve record subtyping
Notice that
R2 ? R1 R3 ? R2 R2 ? Records since A ? Ui, (B
? Top) ? Top
since since
A ? A, A ? A,
B ? Top ? Top B ?(C ? Top) ? B ? Top
13Records using labels
Another approach to records is to take labels, L,
as indexes into components.
xA yB zC Lx,y,z, L ? Atom SigL ? Ui
byif jx then Aelse if jy then B else C
Given take Define Define the record type as xL
? Sig(x).
14Records as functions
We now take
xA yB zC xL ? Sig(x).
r ? xA yB zC, r.i r(i) r.x ? A, r.y ?
B, r.z ? C
For let so
15Records extension using labels
Consider xA yB zC wD. Is this a
subrecord of xA yB zC?To examine this,
let Lx,y,z,w. Notice L ? L.
Define Sig(i)if iw then D else Sig(i). Notice
xL?Sig(x) ? xL?Sig(x) because L ? L and
Sig(x) ? Sig(x) for x ? L.
16Record extension depends on function polymorphism
xL?Sig(x) ? xL?Sig(x)
because any function r in xL?Sig(x) is a
function in xL ?Sig(x). Given inputs from L, x
and y, r(x)? Sig(x) and Sig(x)Sig(x), r(y)?
Sig(y)Sig(y).
17Record subtyping depends on polymorphic functions
Let Note If
R3 x1A1 x2A2 x3A3 R4
x1A1?x4A4 R4 ? R3 r ? R4 then r(xi) is
defined for xi?x1, ?, x4
hence it is defined for xi?x1, ?, x3
18Dependent records
x1 A1 x2 A2(x1) . . . xm Am (x1, . . .
, x m-1) Can we define these dependent records
as dependent functions?
19Dependent records
AU1pA ? A ? A
Example
... as very dependent functions
fxLables ? Rx,f
where Rx,f( if xA then U1 else if xp then
(fA) ? (fA) ? (fA) else Top)
... as a dependent intersection
rAU1 ? pr.A ? r.A ? r.A
20Algebraic structures naïvely
Example a monoid over a type M is a pair
ltop, idgt where op ? M x M ? M is associative id
? M is an identity
The signature, or type, of such an object is
op M x M ? M id M
call this MonSig.
21Groups and monoids example
A group over M is ltop,id,invgt where ltop,idgt is a
monoid and inv ? M ? M is an inverse. A group
signature is GrpSig opM2 ? M idM invM ?
M with these field names we have GrpSig ? MonSig
22Algebraic structure more generally
In order to account for extensions in a uniform
way, we define in advance the type of
labels. Labelop,id,adop,adid,mulop,mulid, ? a
family is defined (also called a
declaration) FamLabel ? Type a signature or
structure is denoted Fam defined as iLabel ?
Fam(i)
23Group example more generally continued
GrpFamLabel ? TypeGrpFam(I)Top for I not in
op,id,inv,axGrpSigGrpFam group ?
GrpSig Note op, id, inv, ax ? Label. is an
operation (Label ? Type) ? Type
24Full algebraic structure
We can fully characterize a monoid with these
axioms
Assoc(M, op) ?x, y, z M. x op(y op z) (x
op y) op z in M Id(M, op, id) ?x, M. ((x op
id) x in M (id op x) x in M)
If we adopt the propositions-as-types principle,
then we can incorporate the axioms in the
structure.
FullMonsig(M) op M2 ? M id M assoc-ax
Assoc(M, op), id-ax Id(M, op, id
25Extending algebraic structures
Let Com(G, op) ? x, y G (x op y y op
x in G) and let GrpAx be the "group
axioms." We'd like a way to extend
by op G2 ? G, id G inv G ? G ax GrpAx
op G2 ? G com_ax Com(G, op) to get Abelian
Groups.
26Extending algebraic structures (cont.)
If we take op id inv ax com_ax from a fixed
infinite discrete type of labels, Label, and
assign Top to all "unused labels," the
intersection corresponds to record extension. f
(i Label ? A(f, i )) ? g ( jLabel ? B(g,
j)) f (i Label ? A(f, i ) ? B(f, i ))