260 likes | 275 Vues
Subtyping defines the relationship between types A and B, illustrating containment, equality, and intersection properties. Learn how to apply subtyping in various examples and its implications.
E N D
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: ATop VoidA {x:A|P[x}}A AA//R {1,2,3}ZZ6Z2
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 NN⊑N is false since no pair <n,m> can be equal to ) nor to s(k) for any k. (For N and NN we know that a=b implies a~b.)
A+B AB A’B {x:A|P(x)} AB AB A A A A’+B’ A’B’ AB’ A A B A//E AB Top ⊑ ⊑ ⊑ ⊑ ⊑ ⊑ ⊑ ⊑ ⊑ Subtyping relations If A⊑A’ and B⊑B’ then:
Intersection types If A is a type and for each xA, B(x) is a type, then x:A.B(x) is a type. bx:A.B(x) iff bB(a) for every aA
Intersection types (cont.) bB(a) for every aA b=b in x:A.B(x) iff b=b in B(a)for every aA
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 AB 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=bA ⊢a=bB[a] ,x:A⊢B[x]Type ⊢a=b(x:AB) ,x:A,y:B[x],x=yB[x],∆[x]⊢C[x] ,u:(x:AB[x]),∆[u]⊢C[u] Dependent intersection x:AB[x] is a set of all elements x, such that xA and xB[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:UiTop We build up the previous record as follows: R1== ATop R2== A(BTop) R3== A(B(CTop))
Naïve record subtyping Notice that: R2⊑R1 R3⊑R2 R2Recordssince AUi,(BTop)Top since since A⊑A, A⊑A, BTop⊑Top B(CTop)⊑BTop
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:LUi byif j=x then Aelse if j=y then B else C Given take Define Define the record type as x:LSig(x).
Records as functions We now take {x:A;y:B;z:C} == x:LSig(x). r{x:A;y:B;z:C}, r.i == r(i) r.xA, r.yB, r.zC 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:LSig(x)becauseL⊑L’ and Sig’(x)⊑Sig(x) for xL.
Record extension depends on function polymorphism x:L’Sig’(x)⊑x:LSig(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:AAA} Example ... as very dependent functions {f|x:LablesRx,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.Ar.Ar.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 andinvMM is an inverse. A group signature is GrpSig == {op:M2M; id:M; inv:MM} 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. Label={op,id,adop,adid,mulop,mulid, } a family is defined (also called a declaration) Fam:LabelType a signature or structure is denoted {Fam} defined as i:LabelFam(i)
Group example more generally continued GrpFam:LabelTypeGrpFam(I)=Top for I not in {op,id,inv,ax}GrpSig={GrpFam} groupGrpSig Note {op, id, inv, ax} ⊑ Label. {} is an operation (LabelType)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 ))