In tro duction to F unctional Programming: Lecture 3 1 In tro duction to F unctional Programming John Harrison Univ ersit y of Cam bridge Lecture 3 ML's t yp e system T opics co v ered: � Wh y t yp es? � Approac hes to t yping � Basic t yp es. � P olymorphism. � ML t yp ec hec king. � Equalit y t yp es John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 2 Logical reasons for t yp es T yp es help us rule out certain programs that don't seem to mak e sense. Is it reasonable to apply a function to itself, as in f f ? It mak es some sense for functions lik e the iden tit y fn x => x or constan t functions fn x => y . But in general it lo oks v ery suspicious. This sort of self-application can lead to inconsistencies in formal logics designed to pro vide a foundation for mathematics. F or example, Russell's parado x considers f x j x 62 x g , the set of all sets that are not mem b ers of themselv es. T o a v oid this, Russell in tro duced a system of t yp es. T yp e theory is no w seen as an alternative to set theory as a foundation. There are in teresting links b et w een t yp e theory and programming. John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 3 Programming reasons for t yp es T yp es w ere in tro duced in programming for a mixture of reasons. W e can (at least in retrosp ect) see the follo wing adv an tages: � They can help the computer to generate more e�cien t co de, and use space more e�ectiv ely . � They serv e as a kind of `sanit y c hec k' for programs, catc hing a lot of programming errors b efore execution. � They can serv e as do cumen tation for p eople. � They can help with data hiding and mo dularization. A t the same time, some programmers �nd them an irksome restriction. Ho w can w e ac hiev e the b est balance? John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 4 Di�eren t t yping metho ds W e can distinguish b et w een � Strong t yping, as in Mo dula-3, where t yp es m ust matc h up exactly . � W eak t yping, as in C, where greater latitude is allo w ed (e.g. an argumen t of t yp e int to a function exp ecting a float ). and also b et w een � Static t yping, as in F OR TRAN, whic h happ ens during compilation � Dynamic t yping, as in LISP , whic h happ ens during execution. ML is statically and strongly t yp ed. A t the same time, a feature called p olymorphism giv es man y b ene�ts of w eak or dynamic t yping. John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 5 Basic t yp es The primitiv e t yp es in ML include: � T yp e unit , a 1-elemen t t yp e, whose only elemen t is written () . � T yp e bool , a 2-elemen t t yp e whose elemen ts are written true and false . � T yp e int , a subset of the p ositiv e and negativ e in tegers, e.g. 6 and ~11 . � T yp e real , �oating p oin t n um b ers, written e.g. 1.0 and 3.1415926 . � T yp e string , whic h corresp onds to sequences of c haracters, written "like this" . These can b e put together using t yp e constructors, including the function constructor -> and the Cartesian pro duct constructor * . W e will see ho w to de�ne new t yp es and t yp e constructors later. John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 6 P olymorphism Some functions can ha v e v arious di�eren t t yp es | p olymorphism . W e distinguish b et w een: � T rue (`parametric') p olymorphism, where all the p ossible t yp es for an expression are instances of some sc hematic t yp e, and all instances of that sc hema are p ossible t yp es. F or example, fn x => x can ha v e an y t yp e of the form � -> � , e.g. int -> int or bool -> bool but not int -> real . � Ad ho c p olymorphism, or overlo ading , where this isn't so. The addition op eration in fn x => 1 + x and fn x => 1.0 + x has t yp es int * int -> int and real * real -> real resp ectiv ely , but it can't ha v e t yp e bool * bool -> bool . ML has o v erloading, but only for a few sp ecial cases, and w e prefer to ignore it. W e'll concen trate on (parametric) p olymorphism. John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 7 T yp e v ariables In order to express p olymorphism, ML allo ws t yp es to con tain typ e variables . These are written 'a , 'b etc., ASCI I appro ximations to � and � . If an expression has a t yp e in v olving � then it can also b e giv en an y t yp e that results from consisten tly replacing � b y another t yp e (whic h ma y itself in v olv e t yp e v ariables). Let's sa y that a t yp e � is more general than � , and write � � � , when w e can substitute t yp es for t yp e v ariables in � and get � . F or example: � � bool � � � ( � ! � ) � ( int ! int ) ( � ! � ) 6� ( int ! bool ) ( � ! � ) � ( � ! � ) ( � ! � ) 6� � John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 8 Most general t yp es Ev ery expression in ML that has a t yp e has a most general t yp e. This w as �rst pro v ed in a similar con text b y Hindley , and for the exact setup here b y Milner. What's more, there is an algorithm for �nding the most general t yp e of an y expression, ev en if it con tains no t yp e information at all. ML implemen tations use this algorithm. Therefore it is nev er necessary in ML to write do wn a t yp e. All t yping is implicit. Th us, the ML t yp e system is m uc h less irksome than in man y languages lik e Mo dula-3. W e nev er ha v e to sp ecify t yp es explicitly and w e can often re-use the same co de with di�eren t t yp es: the compiler will w ork ev erything out for us. John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 9 ML t yp e inference (1) R oughly sp e aking , here's ho w ML's t yp e inference w orks. Using this metho d y ou can t yp ec hec k ML expressions y ourself, though it's rather lab orious and with some exp erience it b ecomes m uc h easier. W e'll use as an example: fn a => (fn f => fn x => f x) (fn x => x) First, attac h distinct t yp e v ariables to distinct v ariables in the expression, and the appropriate t yp es to previously de�ned constan ts, p erhaps themselv es p olymorphic. Di�eren t t yp e v ariables m ust b e used for distinct instances of p olymorphic constan ts. Note that v ariables lik e x in fn x => ha v e a limited scop e, and outside that scop e instances of x are really separate v ariables. W e get fn (a: � ) => (fn (f: � ) => fn (x: � ) => (f: � ) (x: � )) (fn (x: � ) => (x: � )) . John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
In tro duction to F unctional Programming: Lecture 3 10 ML t yp e inference (2) No w an application of a function to an argumen t f x can only b e w ell-t yp ed if f : � ! � and x : � for some � and � . In this case, ( f x ) : � . An expression fn (x: � ) => E: � has t yp e � ! � . Using these facts, w e can �nd relations among the t yp e v ariables. Essen tially , w e get a series of sim ultaneous equations, and use them to eliminate some unkno wns. The remaining unkno wns, if an y , parametrize the �nal p olymorphic t yp e. If the t yp es can't b e matc hed up, or some t yp e v ariable has to b e equal to some comp osite t yp e con taining itself, then t yp ec hec king fails. Another w a y of lo oking at it is as a case of uni�c ation . John Harrison Univ ersit y of Cam bridge, 20 Jan uary 1998
Recommend
More recommend