Subtyping

1 / 26

Subtyping - PowerPoint PPT Presentation

Subtyping. 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 {x:A | P[x}}  A A  A//R {1,2,3}  Z  Z 6  Z 2. Subtyping (cont.).

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about 'Subtyping' - marja

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Subtyping

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

{x:A|P[x}}A

AA//R

{1,2,3}ZZ6Z2

Subtyping (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 <n,m> can be equal to ) nor to s(k) for any k. (For N and NN we know that a=b implies a~b.)

A+B

AB

A’B

{x:A|P(x)}

AB

AB

A

A

A

A’+B’

A’B’

AB’

A

A

B

A//E

AB

Top

Subtyping relations

If A⊑A’ and B⊑B’ then:

Intersection types

If A is a type and for each xA, B(x) is a type, then  x:A.B(x) is a type.

bx:A.B(x)

iff

bB(a) for every aA

Intersection types (cont.)

bB(a) for every aA

b=b in  x:A.B(x)

iff

b=b in B(a)for every aA

Intersection

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

Union

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

⊢a=bA ⊢a=bB[a] ,x:A⊢B[x]Type

⊢a=b(x:AB)

,x:A,y:B[x],x=yB[x],∆[x]⊢C[x]

,u:(x:AB[x]),∆[u]⊢C[u]

Dependent intersection

x:AB[x]

is a set of all elements x, such that xA and xB[x].

Set type as dependent intersection

{x:A|P[x]} == x:A[P[x]]

Axioms:

Records and variant types

RECORDS

{x:A}=={x}A

{x:A;y:B;z:C}=={x:A}{y:B}{z:C}

VARIANT TYPES

{x of A}=={x}A

(xofA|yofB|zofC)==(xofA)(yofB)(zofC)

Records naïvely

There are many ways to capture the concept of a record. For example

{x:A; y:B; z:C}

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)))

Naïve record extension

We can provide for record extension by adding Top as a last component of any record

Records == T:UiTop

We build up the previous record as follows:

R1== ATop

R2== A(BTop)

R3== A(B(CTop))

Naïve record subtyping

Notice that:

R2⊑R1

R3⊑R2

R2Recordssince AUi,(BTop)Top

since

since

A⊑A,

A⊑A,

BTop⊑Top

B(CTop)⊑BTop

Records using labels

Another approach to records is to take labels, L, as indexes into components.

{x:A;y:B;z:C}

L={x,y,z}, L⊑Atom

Sig:LUi byif j=x then Aelse if j=y then B else C

Given

take

Define

Define the record type as x:LSig(x).

Records as functions

We now take

{x:A;y:B;z:C} == x:LSig(x).

r{x:A;y:B;z:C},

r.i == r(i)

r.xA, r.yB, r.zC

For

let

so

Records extension using labels

Consider {x:A;y:B;z:C;w:D}. Is this a subrecord of {x:A;y:B;z:C}?To examine this, let L’={x,y,z,w}.Notice L ⊑ L’.

Define Sig’(i)=if i=w then D else Sig(i).Notice x:L’Sig’(x)⊑x:LSig(x)becauseL⊑L’ and Sig’(x)⊑Sig(x) for xL.

Record extension depends on function polymorphism

x:L’Sig’(x)⊑x:LSig(x)

because any function r’in x:L’Sig’(x)is a function in x:L Sig(x).

Given inputs from L,x and y, r’(x)Sig’(x) and Sig’(x)=Sig(x), r’(y) Sig’(y)=Sig(y).

Record subtyping depends on polymorphic functions

Let

Note

If

R3 ={x1:A1; x2:A2; x3:A3}

R4 ={x1:A1;;x4:A4}

R4⊑ R3

r  R4 then r(xi) is defined for

xi{x1, , x4}

hence it is defined for xi{x1, , x3}

Dependent records

{x1: A1; x2: A2(x1); . . . ; xm: Am (x1, . . . , x m-1)}

Can we define these dependent records as dependent functions?

Dependent records

{A:U1;p:AAA}

Example

... as very dependent functions

{f|x:LablesRx,f}

where Rx,f=(

if x=A then U1 else

if x=p then (fA)(fA)(fA)

else Top)

... as a dependent intersection

r:{A:U1}{p:r.Ar.Ar.A}

Algebraic structures – naïvely

Example: a monoid over a type M is a pair

<op, id> where op MxM 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.

Groups and monoids example

A group over M is <op,id,inv> where <op,id> is a monoid andinvMM is an inverse.

A group signature is

GrpSig == {op:M2M; id:M; inv:MM}

with these field names we have

GrpSig ⊑MonSig

Algebraic structure more generally

In order to account for extensions in a uniform way, we define in advance the type of labels.

a family is defined (also called a declaration)

Fam:LabelType

a signature or structure is denoted {Fam} defined as

i:LabelFam(i)

Group example more generally continued

GrpFam:LabelTypeGrpFam(I)=Top for I not in {op,id,inv,ax}GrpSig={GrpFam}

groupGrpSig

Note {op, id, inv, ax} ⊑ Label.

{} is an operation (LabelType)Type

Full 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}

Extending algebraic structures

Let Com(G,op) ==  x, y: G (xopy = yopx 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.

Extending 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," theintersection corresponds to record extension.

f: (i : Label A(f, i ))g: ( j:Label B(g, j))

f: (i : Label  A(f, i )  B(f, i ))