� -calculus Motiv ation: primitiv es of and pro cedural programming com binatory logic Lam b da calculus: Jan Ma luszy nski � � A formalism that captures the mec hanism E++ 1�o or ro om 210 of Phone: 1483 { making expressions to pro cedures, Mail: janma@ida.liu.se { pro cedure calls � A general mo del of computation. � Reference concepts for studying pro cedu- ral languages. Pla y ed imp ortan t role in dev elopmen t of pro- gramming languages, ALGOL, LISP ,... � Motiv ation Com binatory logic: � Computation as rewriting � A related formalism � Lam b da calculus � Imp ortan t concept of c ombinator � Com binatory logic � In�uenced implemen tation tec hniques � Relating them Computation seen as Rewriting 1 2 An Example: Simpli�cation of Simpli�cation of P olynomials: a P olynomials Summary Rules: W e ha v e: � a domain: the p olynomials 0 + x ! x 0 � x ! 0 � a binary rewrite relation on the domain: x ( y + z ) ! xy + xz sp eci�ed b y rules ( x + y ) z ! xz + y z Desirable prop erties: Applied to an expression: � an y sequence of reductions is �nite 2 (0 + a )( a + b ) ! a ( a + b ) ! a + ab ( termination ). � for eac h expression the outcome is unique. An alternativ e reduction: (0 + a )( a + b ) ! (0 + a ) a + (0 + a ) b ! 2 2 (0 � a + a ) + (0 + a ) b ! (0 + a ) + (0 + 2 2 a ) b ! a + (0 + a ) b ! a + (0 � b + ab ) ! 2 2 a + (0 + ab ) ! a + ab 3 4
Abstract Reduction Systems T uring Mac hines An ars is a pair ( A; ! ), where : : : : : : 0 1 1 0 � A is a set, 6 � ! is a binary relation on A : a r ewrite r elation . Read/write head Notation S tap e alphab et; Q states; � a ! b : � : Q � S ) Q � S � f left ; right g . " b is a r e duct of a " s : : : s q s : : : s � ! ! : the re�exiv e and transitiv e closure of 1 i � 1 i n ! represen ts a con�guration with � : the in v erse of ! � the tap e s : : : s s : : : s 1 i � 1 i n � : the in v erse of ! ! � the state q � � =: ( ! ! [ ) . � the p osition of the head at s i c omputation , r ewriting: 0 If � ( q ; s ) = ( q ; t; right ), then i 0 s : : : s q s : : : s ` s : : : s t q s : : : s 1 i � 1 i n 1 i � 1 i +1 n 5 6 The con v ertibilit y relation Normalizing ars Giv en an ars ( A; ! ): � a is a normal form : a and b are c onvertible ( a = b ) if a can b e there is no b suc h that a ! b . rewritten in to b using the ! relation forw ards � b is a normal form of a : or bac kw ards a �nite n um b er of times. a ! ! b and b is a normal form. � ! is we akly normalizing if ev ery a 2 A q � @ has a normal form. � @ R q q � @ � ! is str ongly normalizing (or terminat- � R @ q q b a @ � @ � ing , if there is no in�nite sequence a ! 0 R @ � � @ R � � q q q a ! a ! : : : 1 2 @ � R @ � � q q : : : a 0 � @ � � R @ a b 1 0 � @ � � @ R a 2 b 1 � @ � � @ R a 3 b 2 � @ � � . R @ . . 7 8
Con�uen t ars Con�uen t ars 's sp ecify functions Giv en an ars ( A; ! ): � ! is Chur ch-R osser (CR) (or c on�uent ) Giv en a con�uen t ars ( A; ! ): if for all a; b; c 2 A suc h that a ! ! b and � ev ery a 2 A has at most one normal form a ! ! c , there is a d 2 A suc h that b ! ! d and c ! ! d . � ! de�nes a partial function f on A : � ! is we akly Chur ch-R osser (WCR) (or 8 > > < we akly c on�uent ) if for all a; b; c 2 A b; if b 2 NF ( A ) and a ! ! b f ( a ) = > > : suc h that a ! b and a ! c , there is a unde�ned else d 2 A suc h that b ! ! d and c ! ! d . � if ! is w eakly normalizing, then f is a a a � � @ @ � @ total function. � � @ @ � @ � � @ @ � @ � R @ � R @ � � @ R � if ! is strongly normalizing, then f can c c CR W CR b b @ � @ � b e computed with an y reduction strategy . @ � @ � @ @ � � @ @ � � @ R � @ R � � @ R � R @ � � d d � � ? � - a c b d 6 � � 9 10 The ars b elo w is W CR but not CR. Lam b da calculus It is also non-terminating. � � ? � - Lam b da calculus is an ars a c b d 6 � � � Domain: lamb da terms � Rewrite relation: r e duction rules Newman's lemma: If ! is w eakly con- �uen t and terminating, then ! is con�uen t. 11 12
Lam b da terms Notational con v en tions for lam b da terms In tuition � Making an expression to a function, e.g. � Left-asso ciativit y of application fun twice x = 2*x; (( xy ) z ) ) xy z � Using the function, e.g. � Skipping m ultiple � s twice 2; ( �x: ( �y : ( M ))) ) �xy :M F ormal de�nition: � a v ariable ( x; y ; z ; ::: ) is a lam b da term, Example: write � if M is a lam b da term, then ( �x:M ) is a lam b da term ( functional abstr action ) �xy :xy ( xy ) � if M and N are lam b da terms then ( M N ) instead of is a lam b da term ( functional applic ation ) ( �x: ( �y : (( xy )( xy )))) 13 14 In tuition of rewriting The replacemen t op eration � �x:M is a function with parameter x : [ x 7! N ] M x is b ound in M , (i) [ x 7! N ] x is N , non-b ound v ariables are fr e e . (ii) [ x 7! N ] y is y for an y v ariable y di�eren t � ( �x:M ) N is a \call": from x , r eplac e x in M b y N . (iii) [ x 7! N ]( P Q ) is ([ x 7! N ] P [ x 7! N ] Q ), ( �x:x ) y ! y (iv) [ x 7! N ] �x:P is �x:P , (v) [ x 7! N ] �y :P is �y : [ x 7! N ] P if y is di�eren t from x and has no free o ccurrence ( �x:xx )( �x:x ) y ! ( �x:x )( �x:x ) y ! ( �x:x ) y ! y in N , or if x has no free o ccurrence in P , But ho w to handle: (vi) [ x 7! N ] �y :P is �z [ x 7! N ]([ y 7! z ] P ) ( �xy :xy ) y if y is di�eren t from x and has a free o c- currence in N , and x has a free o ccurrence Replacemen t m ust not bind free v ariables. in P . z is a v ariable that do es not o ccur free in N nor in P . ( �xy :xy ) y ! [ x 7! y ]( �y :xy ) = �z [ x 7! y ]([ y 7! z ] �y :xy ) = �z [ x 7! y ]( �z :xz ) = ( �z :y z ) 15 16
( � ) If y is not free in M , then �x:M ! The � -reduction rule � �y : [ x 7! y ] M . ( � ) ( �x:M ) N ! [ x 7! N ] M � � In tuition of ! : function call. � � Computation b y � -reduction ma y or ma y not terminate, e.g. � In tuition of ! : renaming; � ( �y :y y )( �y :y y ) ! ( �y :y y )( �y :y y ) �x:xy ! �z :z y � � Ch urc h-Rosser theorem: if P ! ! M and � � Restriction on ! : \fresh" v ariables are � P ! ! N then there exist � -congruen t � to b e used: terms T and T suc h that M ! ! T and 1 2 � 1 N ! ! T . �x:xy 6! �y :y y � � 2 � Hence, ev ery lam b da term has a unique � ! ! is symmetric, e.g.: � (up to � -con v ersion) normal form, or no �xy :xy ! ! �z w :z w � normal form at all. �z w :z w ! ! �xy :xy � Hence the � rule is called � - c onversion rule . If s ! ! t then s and t are called � � -c ongruent . 17 18 Some rewrite strategies The rewrite strategies and termination � The normal-or der r e duction (lazy ev aluation): � If the normal form exists, the normal-order reduce the left-most redex,e.g.: reduction will compute it. ( �x:xx )(( �y :y )( �y :y )) ! � The applicativ e order reduction ma y not (( �y :y )( �y :y ))(( �y :y )( �y :y )) ! terminate ev en if there is a normal form, ( �y :y )(( �y :y )( �y :y )) ! e.g.: (( �y :y )( �y :y )) ! ( �xz :z )(( �y :y y )( �y :y y )) ! �y :y ( �xz :z )(( �y :y y )( �y :y y )) � The applic ative-or der r e duction using the applicativ e-order reduction. But: (call b y v alue): reduce the left-most innermost redex, e.g.: ( �xz :z )(( �y :y y )( �y :y y )) ! ( �z :z ) ( �x:xx )(( �y :y )( �y :y )) ! using the normal-order reduction. ( �x:xx )( �y :y ) ! (( �y :y )( �y :y )) ! �y :y 19 20
Recommend
More recommend