Louden Chpters 6,9 Types Dt Types nd Abstrct Dt Types 1 Arrys s functons f: U -> V (f U s ordnl type) f() rry C rrys types cn be wthout szes rry vrbles must hve fxed sze rry_mx( [], sze) // prmeters re essentlly poers { temp, ; ssert(sze > 0); temp = [0]; for (=1; < sze; ++) { f ([] > temp) temp = []; return temp; 2 Jv rrys Jv rrys re hep llocted nd the sze cn be specfed completely dynmclly (but once specfed cn not chnge unless rellocted) clss ArryTest { sttc rry_mx([] ) { temp; temp = [0]; for ( = 1; <.length; ++) { f ([] > temp) temp = []; return temp; u = Integer.prseInt(n.redLne()); [] x = new [u]; Multdmensonl Arrys (row mjor, column mjor form) x[10][20]; /* c code */ rry_mx( [][20], sze); [] [] x = new [10][20]; /* Jv code */ 3 4
Poers nd Arrys n C Recursve Dttypes n C [] = {1,2,3,4,5; * p = ; prf( %d\n, *p); prf( %d\n, *(p+2)); prf( %d\n, *(+2)); prf( %d\n, 2[]); /* ll of the bove pr 3 :-) */ [2] s shorthnd for *(+2); n ML: dttype ChrLst = EmptyChrLst ChrLstNode of chr * ChrLst; C requres tht ech dt type hs fxed mxmum sze determned t trnslton tme Ech node hs fxed sze nd they cn be strung together to form lst of rbtrry sze struct ChrLstNode { chr dt; struct ChrLstNode* next; ; typedef struct ChrLstNode* ChrLst; 5 6 Usng recursve poer types ChrLst cl = (ChrLst) mlloc(szeof(struct ChrLstNode)); (*cl).dt = ''; (*cl).next = 0; /* ths s so common tht there s short-hnd notton */ cl->dt = ''; cl->next = 0; (*cl).next = (ChrLst) mlloc(szeof(struct ChrLstNode)); (*(*cl).next).dt = 'b'; /* cl->next->dt='b'; */ (*(*cl).next).next = 0; /* cl->next->next=0; */ Type equvlence Structurl equvlence two dttypes re the sme f they hve the sme structure (trcker thn t sounds) Nme equvlence two types re the sme only f they hve the sme nme 7 8
C ML Type equvlence n C, ML nme equvlence for structs structure equvlence for everythng else type Age ; (structurl equvlence) dttype NewAge = NewAge ; (nme equvlence) Type checkng C, C++ : wek type checkng Scheme : strong dynmc type checkng Ad : strong type checkng no type nference (very verbose) ML : strong type checkng + type nference flexblty + protecton from errors 9 10 Polymorphc Type Checkng An exmple Hndley-Mlner type checkng (1969,1978) Mjor feture of ML nd Hskell The mchnery for utomtclly determnng the types of rguments nd results of functons wthout the progrmmer hvng to specfy them [] + + [] rry of Type checker flls n the types of ech node nd check f everythng s ok 11 12
An exmple Cn we do wthout type declrtons? [] + + [] Type checker flls n the types of ech node nd check f everythng s ok + type vrbles: + (only subscrpts) + rry of [] [] [] rry of rry of 13 14 Hndley-Mlner type checkng Instntton: once type vrble s replced by n ctul type, the ALL nstnces of tht vrble must be updted Unfcton: ny type vrble unfes wth ny type expresson type constnts unfy f they re the sme two type constructons unfy f they re the sme ll components lso (recursvely) unfy Unfcton exmples ' unfes to b' rry unfes to ' lst unfes wth 'b lst 2 cn not unfy wth rry of rry of cnnot unfy wth rry of chr 15 16
Polymorphc type checkng 'b rry must be the sme s ' rry therefore 'b nd ' re the sme but we don't know wht they re REALLY COOL Implct Prmetrc Polymorphsm (compler does t for you) Explct Prmetrc Polymorphsm Ad-hoc polymorphsm (overlodng) There s more to t Red pges 240-244 of your book nd usng your experence wth the ML compler you should hve no problem understndng them 17 18 Code trnslton for polymorphc types Wthout knowng the types trnsltor cnnot determne the sze of vlues Two solutons Expnson = exmne ll uses generte seperte code for ech use Boxng nd tggng = Fx sze for sclr types + sze feld for the rest (flg bt to dsmbgute) (lso used for grbge collecton) Explct Polymorphsm In ML (explct works ncely wth HM type checkng): dttype ' Stck = EmptyStck Stck of ' * (' Stck); In C++ (templtes re explct polymorphsm) Stck<> s; The most recent ddton to Jv re re Genercs whch re smlr. templte <typenme T> struct StckNode { T dt; StckNode<T> *next; ; templte <typenme T> struct Stck { StckNode<T>* thestck; ; 19 20