Quick viewing(Text Mode)

Type Systems

Type Systems

execution er- rors

Handbook of and Engineering, Chapter 103. CRC Press, 1997. 1 Copyright 1996 by Digital Equipment Corporation. Revised Friday, March 28, 1997, 4:45 pm.

typetyping polymor- phismdata abstraction type inference

nil

xBoolean xBooleannotx

2

safe

bad behaviorill behaved

3

all

4

weakly typed

5

Economy of execution nil Economy of small-scale development Economy of compilationinterfaces Economy of large-scale development Economy of language features n n

6

decidably verifiable transparent enforceable

Formal type systems type soundness well-typed programs are well behaved

7

terms scoping static lexical scoping dynamic scoping free variables substitution has-typeMA MA subtype-ofAB equal-typeAB static typing environ- ments MA MA∫MA MA has- value results

8

equal type typeBool typeBool structural equivalence by-name equivalence branding

9

∫ entailsstatic typing environment xAxnAn xxn domassertion typing judgment MA M ∫MA MA  ∫trueBool trueBool xNat∫xNat x NatxNat ∫ Q valid∫true Boolinvalid ∫true Nat

 ∫ Q

10

∫n∫n ∫

premisei∫i conclusion∫ Nat MN MN MNMNMN

nn ∫Q ∫MNat∫NNat ∫nNat ∫MNNat

∫Q

(formal) formal proof systems

11 ∫Nat

 ∫ Q   ∫ Q  ∫Natn ∫Nat n ∫Nat

M A∫MAM n inferred Nat ∫Q∫trueBool true truetruenot typeable ∫MNat∫NBool ∫MNNattrue true

12

. ∫MA[M]Ï [A]M A ∫MAMM’∫M’A

xM xM xAMxAM xMxAM ABAB Basic ÏBasic

13 BoolNat xxy xxyysyntactically identical

AB ÏBasic AB M,N x xAM MN

∫A A ∫A

∫ Q ∫A A ∫MA M A

 axiom x xAA

14 ∫A ∫A∫Q x xA∫M B xdom

 x ∫AxÌdom ∫ Q xA∫ Q

∫QÏBasic ∫A∫B ∫ ∫AB

x ’xA∫ Q xA∫M B ∫M AB∫N A ’xA∫xA ∫xAMAB ∫MNB

x ’xA xA ABB A A

∫ Q  ∫ Q  ∫ Q  ∫ Q  ∫ ∫ ∫ ∫ ∫ ∫ y∫ Q x y∫ Q x y∫ y∫ yz ∫ Q x yz ∫ Q x yz∫y x yz∫z x yz∫yz y∫zyz

15 Unit unitBooltruefalse Nat Unit VoidNullUnit Unitunit Unit ∫Q ∫Q ∫Unit ∫unit Unit

Bool A NNA NN ifA AN NA ifA ∫Q ∫Q ∫Q ∫Bool ∫trueBool ∫false Bool

∫M Bool ∫N A ∫N A

∫ifA M then N else N A

16 Natsucc NatNatpred isero ∫Q ∫Q ∫MNat ∫Nat ∫Nat ∫succM Nat

∫MNat ∫MNat ∫predM Nat ∫iseroM Bool

product typesAA AA firstsecond with MxxNwith with typesA...An with

∫A∫A ∫M A∫M A

∫AA ∫ÜMMáAA

∫M AA ∫M AA

∫firstMA ∫second MA

∫M AA xAxA∫N B

∫with xAxA M do N B

AA AleftinLeftA rightinRightisLeftisRight

17 asLeftasRightasLeft asLeft A A isLeftisRight asLeftasRight Boolcase MMxx NN

∫A∫A ∫M A∫A ∫A∫M A

∫AA ∫inLeftAMAA ∫inRightAMAA

∫MAA ∫MAA ∫isLeftM Bool ∫isRightM Bool

∫MAA ∫MAA

∫asLeftM A ∫asRightM A

∫M AA xA∫N B xA∫N B

∫caseB M of xA then NxA then N B

Bool Unit Unitcase IntNat NatNat errorA asRightinLeftAunitA labeled record types variant types

18 with Mllnx xnNwith AARecordfirstAsecondA2

li li

∫A∫An ∫M A∫Mn An

∫RecordlAlnAn ∫recordlMlnMnRecordlAlnAn

∫M RecordlAlnAnjÏn

∫MljAj

∫M RecordlAlnAn xAxnAn∫N B

∫with lxAlnxnAn M do N B

is lisLeftis- Rightas lasLeftasRight case AAVariantleftArightA2 redgreenblueVariantredUnitgreenUnitblueUnit

li li

∫A∫An ∫A∫An∫M AjÏn

∫VariantlAlnAn ∫variantlAlnAnlMVariantlAlnAn

∫MVariantlAlnAnjÏn

∫M is ljBool

∫MVariantlAlnAnjÏn

∫M as ljAj

19

∫M VariantlAlnAn xA∫N B xnAn∫Nn B

∫caseB M of lxA then NlnxnAn then Nn B

RefA A unit ∫A ∫M A ∫RefA ∫refMRefA

∫M RefA ∫M RefA∫N A ∫derefMA ∫M N Unit

Ref Ref ArrayA@ Nat NatRefA

arrayANM@ NM let cellRefArefMandandcellNRefArefM inÜNxNat ifxthencellelse ifelse ifxNthencellN else errorRefAá boundM@ firstM

MNA@ if N < first M then derefsecondMN

else errorA MNP@ if N < first M then secondMNP

else errorUnit

20 ArrayAA let ∫A ∫ArrayA

∫NNat ∫MA ∫MArrayA ∫arrayNMArrayA ∫bound M : Nat

∫NNat ∫MArrayA ∫NNat ∫MArrayA ∫PA ∫MN A ∫MNPUnit

A AAunfold foldA AABABA un- foldfoldMMfoldunfoldM’M’ ∫ QÌdom ’∫Q ∫A ∫ Q ’∫ ∫A

21 ∫M AA ∫M A

∫foldAMA ∫unfoldAMAA

ListA Anil listCase

ListA@ UnitA

nilAListA@ foldinLeft unit

consAAListAListA@ hdAtlListAfoldinRight Ühdtlá

listCaseABListABAListABB@ lListA nB AListAB case unfold lof unitUnit then n pAListA then c p

casewith A®AíA

®AA@ xBunfoldB x x foldBxBunfoldB x x

íAAAA@ fAAxB f unfoldB x x foldBxB f unfoldB x x B7 AA

V@ äxã@ x ä-ãV

äxMã@ foldV xVäMã äM Nã@ unfoldV äMãäNã

22 foldunfold

Unit A Bool Nat Proc I C I A E C IE

CC DC I EC

23 E I N

EE EE

∫C∫E A∫MA CE∫D a S signatureSdeclarationD x NatxNat ∫ Q ∫A A ∫C C ∫E A E A ∫D a S D S

 I ∫AIÌdom ∫ Q IA∫ Q

∫Q ∫Q ∫Q ∫Bool ∫Nat ∫Proc

∫C ∫E A AÏBoolNat ∫ ICa IProc ∫I AEa IA

∫IA∫E A ∫C∫C

∫IE ∫CC

24 ∫D a IAIA∫C ∫IProc ∫EBool∫C ∫DC ∫I ∫EC

IA ∫ Q ∫Q

IA ∫ IA ∫N Nat

∫ENat∫ENat ∫ENat∫ENat

∫EENat ∫EEBool

type parametersM, M AxAx Aid@ xx Atype instantiationidA xAx Muniversally quantified MÓAforallM AMA idÓ

25

AB AB ÓA M,N x xAM MN M M A

ÓAAMM

∫ Q ∫A A ∫MA M A

 x ∫AxÌdom ∫QÌdom ∫ Q xA∫ Q ∫ Q

’∫Q ∫A∫B ∫A ’∫ ∫AB ∫ÓA

x ’xA∫ Q xA∫M B ∫M AB∫N A ’xA∫xA ∫xAMAB ∫MNB

26 ∫M A ∫M ÓA∫B ∫MÓA ∫MB BA

ÓA A BAB AidÓA idAA7 AA idÓid existentially quantified ∫A ∫BM BA

∫ÔA ∫packÔA B with M ÔA

∫M ÔA xA∫N B ∫B

∫openB M as xA in N B

UnitUnit truefalsecond BoolInterface @ ÔBoolRecordtrueBoolfalseBoolcondÓBool Bool truefalsecond BoolUnitUnit pack

27 boolModuleBoolInterface @

packBoolInterface BoolUnitUnit with record true inLeftunit false inRightunit

cond xBoolyy

case x of xUnit then yxUnit then y BoolboolOp inif bool- Optrue thenelse

openNat boolModule as BoolboolOpRecordtrueBoolfalseBoolcondÓBool inboolOpcondNatboolOptrue boolOptrueopen Bool open

∫AB ABAB AB Top

28 ∫ Q ∫A A ∫AB AB ∫MA M A

AAB B Top ABA’B’A’ ABB’ MABA A’AMB B’BM ABA’B’ A’B’AB A’B’ UnitUnitUnit UnitUnit

∫ A ∫AB∫BC ∫aA∫AB ∫AA ∫AC ∫aB

∫ Q ∫A ∫A’A∫BB’ ∫Top ∫ATop ∫ABA’B’

NatInt

29 WorkingAge@VariantstudentUnit, adultUnit Age@VariantchildUnit, studentUnit, adultUnit, seniorUnit Worker@RecordnameStringageWorkingAgeprofessionString Person@RecordnameStringageAge WorkingAgeAge WorkerPerson RefARefBAB RefARefB

∫AB∫AB ∫AB∫AB

∫AABB ∫AABB

li

∫AB∫AnBn∫An∫Anm

∫RecordlAlnmAnmRecordlBlnBn

li

∫AB∫AnBn∫Bn∫Bnm

∫VariantlAlnAnVariantlBlnmBnm

bounded variables Top AB AB AB ABAB

30 <: ∫ AÌdom ’A∫Q ’A∫Q A ∫ Q ’A∫ ’A∫A

Top∫A ∫A ∫B Top ∫AB ∫A ∫AB

AM M AM TopMAM ÓAB

AB AB ÓAB M,N x xAM MN AM M A

ÓAB BAAMMA  x x

31  A∫B ∫A’AA’∫BB’ ∫ÓAB ∫ÓABÓA’B’

A∫M B ∫M ÓAB∫A’A ∫AMÓAB ∫MA’ A’B

ÔABpartially abstract type A  A∫B ∫AAA∫BB’ ∫ÔAB ∫ÔABÔA’B’

∫C A∫CM CB

∫packÔAB AC with M ÔAB

∫M ÔAB ∫D AxB∫N D

∫openD M as AxB in N D

type equivalence∫A B ∫A

32 AA∫A A ∫AB AB retyping rule∫MA∫A B∫MB term equivalence ∫MNA ∫Int N NM

typability MM’ MAAM’ M’M

MA ∫M A

M 7 xx ∫ M

M 7 xyx 7 ’y∫M

xBxxB

33 ∫xTopBxxTopBB BxTop

∫xBunfoldBxx BBB7 unfoldBxBB

∫xBxBxBBB7 Ó xBBB TypeMM Mfail xAMxÌ domx

Typex@ if xA Ï for some A then A else fail TypexAM@ ATypexA)M TypeMN@ if TypeM7 TypeNB for some B then B else fail

zyz y, Typey)zyz Typeyz)yz if Typeyz)y7 Typeyz)zB for some B then B else fail

34 if 7 B for some B then B else failB7

GoodA M A

Good@Ï dom GoodAB@GoodAandGoodB GoodÓA@GoodA Typex@ if xA Ï for some A then A else fail TypexAM@ if GoodAthenATypexA)M else fail TypeMN@ if TypeM7 TypeNB for some B then B else fail TypeM@ Ó Type)M TypeMA@ if TypeM7 ÓB for some ,B and GoodA then AB else fail

SubtypeA, BAB Subtype AB AB

Good@Ï dom GoodTop@true GoodAB@GoodAandGoodB GoodÓAB@GoodAandGoodAB

35 SubtypeATop@ true Subtype@ true SubtypeA@ forA=Top ifBÏ for some B then SubtypeBAelsefalse SubtypeABA’B’@ SubtypeA’AandSubtypeBB’ SubtypeÓABÓ’A’.B’@ Subtype, A’, A and Subtype, ’A’, ’B, B’ SubtypeAB@ false otherwise Expose@ ifAÏ for some A then ExposeAelsefail ExposeA@ A otherwise Typex@ if xA Ï for some A then A else fail TypexAM@ if GoodAthenATypexA)M else fail TypeMN@ if ExposeTypeM7 AB for some A,B and SubtypeTypeNA then B else fail TypeAM@ if GoodAthenÓATypeA)M else fail TypeMA@ if ExposeTypeM7 ÓA’B for some ,A’,B and GoodA and SubtypeAA’ then AB else fail

36

37

 AB ABC ACBCC AB ABD DADBD not

38 A BB

39

Proc. ACM Conference on and Computer Architecture .ACM Transactions on Pro- gramming Languages and Systems .Theoretical Computer Science .Science of Theoretical Aspects of Object-Oriented Programming, .ACM Computing Surveys .Mathematical Structures in Computer Science . Academic Press . Proc. OOPSLA’95 . . .Springer Verlag .Journal of Computer and

40 System Sciences Proc. 11th Annual ACM Symposium on Principles of Programming Languages Handbook of Theoretical Computer Science, Proc. 12th Annual ACM Symposium on Principles of Programming Languages Proc. 9th Annual IEEE Symposium on Logic in Computer Science(To appear in Information and Computation.) Proc. 19th Annual ACM Sympo- sium on Principles of Programming Languages Proc. Colloquium sur la programma- tion Information Pro- cessing, . .Information and Computation Proc. 9th Annual IEEE Symposium on Logic in Computer Science .Information and Computation

41

42