1 / 26

Subtyping

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

marja
Download Presentation

Subtyping

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  3. 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:

  4. 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

  5. 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

  6. 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

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

  8. ⊢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:

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

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

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

  12. 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

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

  14. 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

  15. 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.

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

  17. 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}

  18. Dependent records {x1: A1; x2: A2(x1); . . . ; xm: Am (x1, . . . , x m-1)} Can we define these dependent records as dependent functions?

  19. 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}

  20. 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.

  21. 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

  22. Algebraic structure more generally In order to account for extensions in a uniform way, we define in advance the type of labels. Label={op,id,adop,adid,mulop,mulid, } a family is defined (also called a declaration) Fam:LabelType a signature or structure is denoted {Fam} defined as i:LabelFam(i)

  23. 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

  24. 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}

  25. 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.

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

More Related