221707 Introduction to Functional Programming Lambda Calculus

download 221707 Introduction to Functional Programming Lambda Calculus

of 104

Transcript of 221707 Introduction to Functional Programming Lambda Calculus

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    1/104

    T i t l e : I n t r o d u c t i o n t o F u n c t i o n a l P r o g r a m m i n g

    L e c t u r e r : M i k e G o r d o n

    ( h t t p : / / w w w . c l . c a m . a c . u k / u s e r s / m j c g / )

    C l a s s : C o m p u t e r S c i e n c e T r i p o s , P a r t I I ( G e n e r a l ) & D i p l o m a

    T e r m : L e n t t e r m 1 9 9 6

    F i r s t L e c t u r e : F r i d a y J a n u a r y 1 9 1 9 9 6 a t 1 2 a m

    L o c a t i o n : H e y c o c k L e c t u r e R o o m

    D u r a t i o n : T w e l v e l e c t u r e s ( M . W . F . 1 2 )

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    2/104

    P r e f a c e

    T h i s c o u r s e a i m s t o t e a c h b o t h t h e t h e o r y a n d p r a c t i c e o f f u n c t i o n a l p r o g r a m m i n g .

    T h e t h e o r y c o n s i s t s o f t h e - c a l c u l u s a n d t h e p r a c t i c e w i l l b e i l l u s t r a t e d u s i n g t h e

    p r o g r a m m i n g l a n g u a g e S t a n d a r d M L .

    T h e e l d o f F u n c t i o n a l P r o g r a m m i n g s p l i t s i n t o t h o s e w h o p r e f e r ` l a z y ' l a n g u a g e s

    l i k e H a s k e l l a n d t h o s e w h o p r e f e r ` s t r i c t ' l a n g u a g e s l i k e M L . T h e p r a c t i c a l p a r t s

    o f t h i s c o u r s e a l m o s t e x c l u s i v e l y e m p h a s i s e t h e l a t t e r , b u t t h e m a t e r i a l o n t h e -

    c a l c u l u s u n d e r l i e s b o t h a p p r o a c h e s .

    T h e c h a p t e r s o n t h e - c a l c u l u s h a v e b e e n l a r g e l y c o n d e n s e d f r o m P a r t I I o f t h e

    b o o k :

    M . J . C . G o r d o n , P r o g r a m m i n g L a n g u a g e T h e o r y a n d i t s I m p l e m e n t a -

    t i o n , P r e n t i c e H a l l I n t e r n a t i o n a l S e r i e s i n C o m p u t e r S c i e n c e , 1 9 8 8 ( c u r -

    r e n t l y o u t o f p r i n t ) .

    T h e i n t r o d u c t i o n t o M L i n C h a p t e r 4 s t a r t e d l i f e a s p a r t o f :

    G o r d o n , M . J . C . , M i l n e r , A . J . R . G . a n d W a d s w o r t h , C . P . , E d i n b u r g h

    L C F : a m e c h a n i z e d l o g i c o f c o m p u t a t i o n , S p r i n g e r L e c t u r e N o t e s i n

    C o m p u t e r S c i e n c e , S p r i n g e r - V e r l a g , 1 9 7 9 .

    T h e M L p a r t s o f t h i s w e r e u p d a t e d s u b s t a n t i a l l y i n t h e t e c h n i c a l r e p o r t :

    G . C o u s i n e a u , M . G o r d o n , G . H u e t , R . M i l n e r , L . P a u l s o n , a n d

    C . W a d s w o r t h , T h e M L h a n d b o o k , I N R I A ( 1 9 8 6 ) .

    I t r a n s l a t e d t h e i n t r o d u c t i o n o f t h i s r e p o r t i n t o S t a n d a r d M L a n d a d d e d s o m e n e w

    m a t e r i a l t o g e t C h a p t e r 4 . T h e c a s e s t u d i e s w e r e w r i t t e n b y m e a t g r e a t s p e e d ,

    a n d s o a r e b o u n d t o c o n t a i n n u m e r o u s m i s t a k e s ! T h e y a i m t o s h o w h o w M L - b a s e d

    f u n c t i o n a l p r o g r a m m i n g c a n b e u s e d i n p r a c t i c e .

    T h e f o l l o w i n g p e o p l e h a v e c o n t r i b u t e d i n v a r i o u s w a y s t o t h e m a t e r i a l c i t e d a b o v e

    o r t o t h e s e n o t e s : G r a h a m B i r t w i s t l e , S h i u K a i C h i n , A v r a C o h n , J a n v a n E i j c k ,

    M i k e F o u r m a n , E l s a G u n t e r , P e t e r H a n c o c k , M a r t i n H y l a n d , T o m M e l h a m , A l l a n

    C . M i l n e , N i c h o l a s O u r u s o , D a v i d S h e p h e r d a n d R o g e r S t o k e s .

    i

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    3/104

    i i P r e f a c e

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    4/104

    C o n t e n t s

    P r e f a c e i

    1 I n t r o d u c t i o n t o t h e - c a l c u l u s 1

    1 . 1 S y n t a x a n d s e m a n t i c s o f t h e - c a l c u l u s : : : : : : : : : : : : : : : : : 1

    1 . 2 N o t a t i o n a l c o n v e n t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : 3

    1 . 3 F r e e a n d b o u n d v a r i a b l e s : : : : : : : : : : : : : : : : : : : : : : : : 3

    1 . 4 C o n v e r s i o n r u l e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4

    1 . 4 . 1 - c o n v e r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : 5

    1 . 4 . 2 - c o n v e r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : 5

    1 . 4 . 3 - c o n v e r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6

    1 . 4 . 4 G e n e r a l i z e d c o n v e r s i o n s : : : : : : : : : : : : : : : : : : : : : 6

    1 . 5 E q u a l i t y o f - e x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : 7

    1 . 6 T h e ; ! r e l a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9

    1 . 7 E x t e n s i o n a l i t y : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0

    1 . 8 S u b s t i t u t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0

    2 R e p r e s e n t i n g T h i n g s i n t h e - c a l c u l u s 1 3

    2 . 1 T r u t h - v a l u e s a n d t h e c o n d i t i o n a l : : : : : : : : : : : : : : : : : : : : 1 3

    2 . 2 P a i r s a n d t u p l e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5

    2 . 3 N u m b e r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6

    2 . 4 D e n i t i o n b y r e c u r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0

    2 . 5 F u n c t i o n s w i t h s e v e r a l a r g u m e n t s : : : : : : : : : : : : : : : : : : : : 2 2

    2 . 6 M u t u a l r e c u r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5

    2 . 7 R e p r e s e n t i n g t h e r e c u r s i v e f u n c t i o n s : : : : : : : : : : : : : : : : : : 2 6

    2 . 7 . 1 T h e p r i m i t i v e r e c u r s i v e f u n c t i o n s : : : : : : : : : : : : : : : : 2 6

    2 . 7 . 2 T h e r e c u r s i v e f u n c t i o n s : : : : : : : : : : : : : : : : : : : : : 2 7

    2 . 7 . 3 T h e p a r t i a l r e c u r s i v e f u n c t i o n s : : : : : : : : : : : : : : : : : 2 9

    2 . 8 E x t e n d i n g t h e - c a l c u l u s : : : : : : : : : : : : : : : : : : : : : : : : : 2 9

    2 . 9 T h e o r e m s a b o u t t h e - c a l c u l u s : : : : : : : : : : : : : : : : : : : : : 3 0

    2 . 1 0 C a l l - b y - v a l u e a n d Y : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 3

    3 C o m b i n a t o r s 3 5

    3 . 1 C o m b i n a t o r r e d u c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : 3 6

    3 . 2 F u n c t i o n a l c o m p l e t e n e s s : : : : : : : : : : : : : : : : : : : : : : : : : 3 6

    3 . 3 R e d u c t i o n m a c h i n e s : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 9

    3 . 4 I m p r o v e d t r a n s l a t i o n t o c o m b i n a t o r s : : : : : : : : : : : : : : : : : : 4 1

    3 . 5 M o r e c o m b i n a t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 2

    3 . 6 C u r r y ' s a l g o r i t h m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 3

    3 . 7 T u r n e r ' s a l g o r i t h m : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 4

    i i i

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    5/104

    i v C o n t e n t s

    4 A Q u i c k O v e r v i e w o f M L 4 7

    4 . 1 I n t e r a c t i n g w i t h M L : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 7

    4 . 2 E x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 7

    4 . 3 D e c l a r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 8

    4 . 4 C o m m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 9

    4 . 5 F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 9

    4 . 6 T y p e a b b r e v i a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 1

    4 . 7 O p e r a t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 2

    4 . 8 L i s t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 3

    4 . 9 S t r i n g s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 3

    4 . 1 0 R e c o r d s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 4

    4 . 1 1 P o l y m o r p h i s m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 4

    4 . 1 2 f n - e x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 5

    4 . 1 3 C o n d i t i o n a l s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 6

    4 . 1 4 R e c u r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 6

    4 . 1 5 E q u a l i t y t y p e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 7

    4 . 1 6 P a t t e r n m a t c h i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 8

    4 . 1 7 T h e c a s e c o n s t r u c t : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 0

    4 . 1 8 E x c e p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 1

    4 . 1 9 D a t a t y p e d e c l a r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : 6 3

    4 . 2 0 A b s t r a c t t y p e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 5

    4 . 2 1 T y p e c o n s t r u c t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 6

    4 . 2 2 R e f e r e n c e s a n d a s s i g n m e n t : : : : : : : : : : : : : : : : : : : : : : : : 6 7

    4 . 2 3 I t e r a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 7

    4 . 2 4 P r o g r a m m i n g i n t h e l a r g e : : : : : : : : : : : : : : : : : : : : : : : : 6 8

    5 C a s e s t u d y 1 : p a r s i n g 6 9

    5 . 1 L e x i c a l a n a l y s i s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 9

    5 . 2 S i m p l e s p e c i a l c a s e s o f p a r s i n g : : : : : : : : : : : : : : : : : : : : : 7 3

    5 . 2 . 1 A p p l i c a t i v e e x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : 7 3

    5 . 2 . 2 P r e c e d e n c e p a r s i n g o f i n x e s : : : : : : : : : : : : : : : : : : 7 7

    5 . 3 A g e n e r a l t o p - d o w n p r e c e d e n c e p a r s e r : : : : : : : : : : : : : : : : : 8 2

    6 C a s e s t u d y 2 : t h e - c a l c u l u s 8 9

    6 . 1 A - c a l c u l u s p a r s e r : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 0

    6 . 2 I m p l e m e n t i n g s u b s t i t u t i o n : : : : : : : : : : : : : : : : : : : : : : : : 9 2

    6 . 3 T h e S E C D m a c h i n e : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 4

    B i b l i o g r a p h y 9 7

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    6/104

    C h a p t e r 1

    I n t r o d u c t i o n t o t h e - c a l c u l u s

    T h e - c a l c u l u s ( o r l a m b d a - c a l c u l u s ) i s a t h e o r y o f f u n c t i o n s t h a t w a s o r i g i n a l l y

    d e v e l o p e d b y t h e l o g i c i a n A l o n z o C h u r c h a s a f o u n d a t i o n f o r m a t h e m a t i c s . T h i s

    w o r k w a s d o n e i n t h e 1 9 3 0 s , s e v e r a l y e a r s b e f o r e d i g i t a l c o m p u t e r s w e r e i n v e n t e d . A

    l i t t l e e a r l i e r ( i n t h e 1 9 2 0 s ) M o s e s S c h o n n k e l d e v e l o p e d a n o t h e r t h e o r y o f f u n c t i o n s

    b a s e d o n w h a t a r e n o w c a l l e d ` c o m b i n a t o r s ' . I n t h e 1 9 3 0 s , H a s k e l l C u r r y r e d i s -

    c o v e r e d a n d e x t e n d e d S c h o n n k e l ' s t h e o r y a n d s h o w e d t h a t i t w a s e q u i v a l e n t t o

    t h e - c a l c u l u s . A b o u t t h i s t i m e K l e e n e s h o w e d t h a t t h e - c a l c u l u s w a s a u n i v e r s a l

    c o m p u t i n g s y s t e m i t w a s o n e o f t h e r s t s u c h s y s t e m s t o b e r i g o r o u s l y a n a l y s e d .

    I n t h e 1 9 5 0 s J o h n M c C a r t h y w a s i n s p i r e d b y t h e - c a l c u l u s t o i n v e n t t h e p r o g r a m -

    m i n g l a n g u a g e L I S P . I n t h e e a r l y 1 9 6 0 s P e t e r L a n d i n s h o w e d h o w t h e m e a n i n g o f

    i m p e r a t i v e p r o g r a m m i n g l a n g u a g e s c o u l d b e s p e c i e d b y t r a n s l a t i n g t h e m i n t o t h e

    - c a l c u l u s . H e a l s o i n v e n t e d a n i n u e n t i a l p r o t o t y p e p r o g r a m m i n g l a n g u a g e c a l l e d

    I S W I M 2 4 ] . T h i s i n t r o d u c e d t h e m a i n n o t a t i o n s o f f u n c t i o n a l p r o g r a m m i n g a n d

    i n u e n c e d t h e d e s i g n o f b o t h f u n c t i o n a l a n d i m p e r a t i v e l a n g u a g e s . B u i l d i n g o n t h i s

    w o r k , C h r i s t o p h e r S t r a c h e y l a i d t h e f o u n d a t i o n s f o r t h e i m p o r t a n t a r e a o f d e n o t a -

    t i o n a l s e m a n t i c s 1 3 , 3 3 ] . T e c h n i c a l q u e s t i o n s c o n c e r n i n g S t r a c h e y ' s w o r k i n s p i r e d

    t h e m a t h e m a t i c a l l o g i c i a n D a n a S c o t t t o i n v e n t t h e t h e o r y o f d o m a i n s , w h i c h i s n o w

    o n e o f t h e m o s t i m p o r t a n t p a r t s o f t h e o r e t i c a l c o m p u t e r s c i e n c e . D u r i n g t h e 1 9 7 0 s

    P e t e r H e n d e r s o n a n d J i m M o r r i s t o o k u p L a n d i n ' s w o r k a n d w r o t e a n u m b e r o f

    i n u e n t i a l p a p e r s a r g u i n g t h a t f u n c t i o n a l p r o g r a m m i n g h a d i m p o r t a n t a d v a n t a g e s

    f o r s o f t w a r e e n g i n e e r i n g 1 7 , 1 6 ] . A t a b o u t t h e s a m e t i m e D a v i d T u r n e r p r o p o s e d

    t h a t S c h o n n k e l a n d C u r r y ' s c o m b i n a t o r s c o u l d b e u s e d a s t h e m a c h i n e c o d e o f

    c o m p u t e r s f o r e x e c u t i n g f u n c t i o n a l p r o g r a m m i n g l a n g u a g e s . S u c h c o m p u t e r s c o u l d

    e x p l o i t m a t h e m a t i c a l p r o p e r t i e s o f t h e - c a l c u l u s f o r t h e p a r a l l e l e v a l u a t i o n o f p r o -

    g r a m s . D u r i n g t h e 1 9 8 0 s s e v e r a l r e s e a r c h g r o u p s t o o k u p H e n d e r s o n ' s a n d T u r n e r ' s

    i d e a s a n d s t a r t e d w o r k i n g o n m a k i n g f u n c t i o n a l p r o g r a m m i n g p r a c t i c a l b y d e s i g n i n g

    s p e c i a l a r c h i t e c t u r e s t o s u p p o r t i t , s o m e o f t h e m w i t h m a n y p r o c e s s o r s .

    W e t h u s s e e t h a t a n o b s c u r e b r a n c h o f m a t h e m a t i c a l l o g i c u n d e r l i e s i m p o r t a n t

    d e v e l o p m e n t s i n p r o g r a m m i n g l a n g u a g e t h e o r y , s u c h a s :

    ( i ) T h e s t u d y o f f u n d a m e n t a l q u e s t i o n s o f c o m p u t a t i o n .

    ( i i ) T h e d e s i g n o f p r o g r a m m i n g l a n g u a g e s .

    ( i i i ) T h e s e m a n t i c s o f p r o g r a m m i n g l a n g u a g e s .

    ( i v ) T h e a r c h i t e c t u r e o f c o m p u t e r s .

    1 . 1 S y n t a x a n d s e m a n t i c s o f t h e - c a l c u l u s

    T h e - c a l c u l u s i s a n o t a t i o n f o r d e n i n g f u n c t i o n s . T h e e x p r e s s i o n s o f t h e n o t a t i o n

    a r e c a l l e d - e x p r e s s i o n s a n d e a c h s u c h e x p r e s s i o n d e n o t e s a f u n c t i o n . I t w i l l b e

    s e e n l a t e r h o w f u n c t i o n s c a n b e u s e d t o r e p r e s e n t a w i d e v a r i e t y o f d a t a a n d d a t a -

    s t r u c t u r e s i n c l u d i n g n u m b e r s , p a i r s , l i s t s e t c . F o r e x a m p l e , i t w i l l b e d e m o n s t r a t e d

    1

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    7/104

    2 C h a p t e r 1 . I n t r o d u c t i o n t o t h e - c a l c u l u s

    h o w a n a r b i t r a r y p a i r o f n u m b e r s ( x y ) c a n b e r e p r e s e n t e d a s a - e x p r e s s i o n . A s

    a n o t a t i o n a l c o n v e n t i o n , m n e m o n i c n a m e s a r e a s s i g n e d i n b o l d o r u n d e r l i n e d t o

    p a r t i c u l a r - e x p r e s s i o n s f o r e x a m p l e 1 i s t h e - e x p r e s s i o n ( d e n e d i n S e c t i o n 2 . 3 )

    w h i c h i s u s e d t o r e p r e s e n t t h e n u m b e r o n e .

    T h e r e a r e j u s t t h r e e k i n d s o f - e x p r e s s i o n s :

    ( i ) V a r i a b l e s : x , y , z e t c . T h e f u n c t i o n s d e n o t e d b y v a r i a b l e s a r e d e t e r m i n e d

    b y w h a t t h e v a r i a b l e s a r e b o u n d t o i n t h e e n v i r o n m e n t . B i n d i n g i s d o n e b y

    a b s t r a c t i o n s ( s e e 3 b e l o w ) . W e u s e V , V

    1

    , V

    2

    e t c . f o r a r b i t r a r y v a r i a b l e s .

    ( i i ) F u n c t i o n a p p l i c a t i o n s o r c o m b i n a t i o n s : i f E

    1

    a n d E

    2

    a r e - e x p r e s s i o n s ,

    t h e n s o i s ( E

    1

    E

    2

    ) i t d e n o t e s t h e r e s u l t o f a p p l y i n g t h e f u n c t i o n d e n o t e d b y

    E

    1

    t o t h e f u n c t i o n d e n o t e d b y E

    2

    . E

    1

    i s c a l l e d t h e r a t o r ( f r o m ` o p e r a t o r ' )

    a n d E

    2

    i s c a l l e d t h e r a n d ( f r o m ` o p e r a n d ' ) . F o r e x a m p l e , i f ( m n ) d e n o t e s

    a f u n c t i o n r e p r e s e n t i n g t h e p a i r o f n u m b e r s m a n d n ( s e e S e c t i o n 2 . 2 ) a n d

    s u m d e n o t e s t h e a d d i t i o n f u n c t i o n

    1

    - c a l c u l u s ( s e e S e c t i o n 2 . 5 ) , t h e n t h e

    a p p l i c a t i o n ( s u m ( m n ) ) d e n o t e s m + n .

    ( i i i ) A b s t r a c t i o n s : i f V i s a v a r i a b l e a n d E i s a - e x p r e s s i o n , t h e n V : E i s a n

    a b s t r a c t i o n w i t h b o u n d v a r i a b l e V a n d b o d y E . S u c h a n a b s t r a c t i o n d e n o t e s

    t h e f u n c t i o n t h a t t a k e s a n a r g u m e n t a a n d r e t u r n s a s r e s u l t t h e f u n c t i o n

    d e n o t e d b y E i n a n e n v i r o n m e n t i n w h i c h t h e b o u n d v a r i a b l e V d e n o t e s a .

    M o r e s p e c i c a l l y , t h e a b s t r a c t i o n V : E d e n o t e s a f u n c t i o n w h i c h t a k e s a n

    a r g u m e n t E

    0

    a n d t r a n s f o r m s i t i n t o t h e t h i n g d e n o t e d b y E E

    0

    = V ] ( t h e r e s u l t

    o f s u b s t i t u t i n g E

    0

    f o r V i n E , s e e S e c t i o n 1 . 8 ) . F o r e x a m p l e , x : s u m ( x 1 )

    d e n o t e s t h e a d d - o n e f u n c t i o n .

    U s i n g B N F , t h e s y n t a x o f - e x p r e s s i o n s i s j u s t :

    < - e x p r e s s i o n > : : =

    j ( < - e x p r e s s i o n > < - e x p r e s s i o n > )

    j ( : < - e x p r e s s i o n > )

    I f V r a n g e s o v e r t h e s y n t a x c l a s s a n d E , E

    1

    , E

    2

    , : : : e t c . r a n g e o v e r

    t h e s y n t a x c l a s s < - e x p r e s s i o n > , t h e n t h e B N F s i m p l i e s t o :

    E : : =

    V

    v a r i a b l e s

    6

    j ( E

    1

    E

    2

    )

    | { z }

    a p p l i c a t i o n s

    ( c o m b i n a t i o n s )

    6

    j V : E

    | { z }

    a b s t r a c t i o n s

    6

    T h e d e s c r i p t i o n o f t h e m e a n i n g o f - e x p r e s s i o n s j u s t g i v e n a b o v e i s v a g u e a n d

    i n t u i t i v e . I t t o o k a b o u t 4 0 y e a r s f o r l o g i c i a n s ( D a n a S c o t t , i n f a c t 3 2 ] ) t o m a k e i t

    r i g o r o u s i n a u s e f u l w a y . W e s h a l l n o t b e g o i n g i n t o d e t a i l s o f t h i s .

    E x a m p l e : ( x : x ) d e n o t e s t h e ` i d e n t i t y f u n c t i o n ' : ( ( x : x ) E ) = E . 2

    E x a m p l e : ( x : ( f : ( f x ) ) ) d e n o t e s t h e f u n c t i o n w h i c h w h e n a p p l i e d t o E y i e l d s

    ( f : ( f x ) ) E = x ] , i . e . ( f : ( f E ) ) . T h i s i s t h e f u n c t i o n w h i c h w h e n a p p l i e d t o E

    0

    y i e l d s ( f E ) E

    0

    = f ] i . e . ( E

    0

    E ) . T h u s

    ( ( x : ( f : ( f x ) ) ) E ) = ( f : ( f E ) )

    a n d

    ( ( f : ( f E ) ) E

    0

    ) = ( E

    0

    E )

    2

    1

    N o t e t h a t s u m i s a - e x p r e s s i o n , w h e r e a s + i s a m a t h e m a t i c a l s y m b o l i n t h e ` m e t a l a n g u a g e '

    ( i . e . E n g l i s h ) t h a t w e a r e u s i n g f o r t a l k i n g a b o u t t h e - c a l c u l u s .

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    8/104

    1 . 2 . N o t a t i o n a l c o n v e n t i o n s 3

    E x e r c i s e 1

    D e s c r i b e t h e f u n c t i o n d e n o t e d b y ( x : ( y : y ) ) . 2

    E x a m p l e : S e c t i o n 2 . 3 d e s c r i b e s h o w n u m b e r s c a n b e r e p r e s e n t e d b y - e x p r e s s i o n s .

    A s s u m e t h a t t h i s h a s b e e n d o n e a n d t h a t 0 , 1 , 2 , : : : a r e - e x p r e s s i o n s w h i c h r e p -

    r e s e n t 0 , 1 , 2 , : : : , r e s p e c t i v e l y . A s s u m e a l s o t h a t a d d i s a - e x p r e s s i o n d e n o t i n g a

    f u n c t i o n s a t i s f y i n g :

    ( ( a d d m ) n ) = m + n :

    T h e n ( x : ( ( a d d 1 ) x ) ) i s a - e x p r e s s i o n d e n o t i n g t h e f u n c t i o n t h a t t r a n s f o r m s

    n t o 1 + n , a n d ( x : ( y : ( ( a d d x ) y ) ) ) i s a - e x p r e s s i o n d e n o t i n g t h e f u n c -

    t i o n t h a t t r a n s f o r m s m t o t h e f u n c t i o n w h i c h w h e n a p p l i e d t o n y i e l d s m + n ,

    n a m e l y y : ( ( a d d m ) y ) ) . 2

    T h e r e l a t i o n s h i p b e t w e e n t h e f u n c t i o n s u m i n ( i i ) a t t h e b e g i n n i n g o f t h i s s e c t i o n

    ( p a g e 2 ) a n d t h e f u n c t i o n a d d i n t h e p r e v i o u s e x a m p l e i s e x p l a i n e d i n S e c t i o n 2 . 5 .

    1 . 2 N o t a t i o n a l c o n v e n t i o n s

    T h e f o l l o w i n g c o n v e n t i o n s h e l p m i n i m i z e t h e n u m b e r o f b r a c k e t s o n e h a s t o w r i t e .

    1 . F u n c t i o n a p p l i c a t i o n a s s o c i a t e s t o t h e l e f t , i . e . E

    1

    E

    2

    E

    n

    m e a n s

    ( ( ( E

    1

    E

    2

    ) ) E

    n

    ) . F o r e x a m p l e :

    E

    1

    E

    2

    m e a n s ( E

    1

    E

    2

    )

    E

    1

    E

    2

    E

    3

    m e a n s ( ( E

    1

    E

    2

    ) E

    3

    )

    E

    1

    E

    2

    E

    3

    E

    4

    m e a n s ( ( ( E

    1

    E

    2

    ) E

    3

    ) E

    4

    )

    2 . V : E

    1

    E

    2

    : : : E

    n

    m e a n s ( V : ( E

    1

    E

    2

    : : : E

    n

    ) ) . T h u s t h e s c o p e o f ` V '

    e x t e n d s a s f a r t o t h e r i g h t a s p o s s i b l e .

    3 . V

    1

    V

    n

    : E m e a n s ( V

    1

    : ( : ( V

    n

    : E ) ) ) . F o r e x a m p l e :

    x y : E m e a n s ( x : ( y : E ) )

    x y z : E m e a n s ( x : ( y : ( z : E ) ) )

    x y z w : E m e a n s ( x : ( y : ( z : ( w : E ) ) ) )

    E x a m p l e : x y : a d d y x m e a n s ( x : ( y : ( ( a d d y ) x ) ) ) . 2

    1 . 3 F r e e a n d b o u n d v a r i a b l e s

    A n o c c u r r e n c e o f a v a r i a b l e V i n a - e x p r e s s i o n i s f r e e i f i t i s n o t w i t h i n t h e s c o p e

    o f a ` V ' , o t h e r w i s e i t i s b o u n d . F o r e x a m p l e

    ( x : y x ) ( y : x y )

    f r e e

    6

    b o u n d

    6

    f r e e

    6

    b o u n d

    6

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    9/104

    4 C h a p t e r 1 . I n t r o d u c t i o n t o t h e - c a l c u l u s

    1 . 4 C o n v e r s i o n r u l e s

    I n C h a p t e r 2 i t i s e x p l a i n e d h o w - e x p r e s s i o n s c a n b e u s e d t o r e p r e s e n t d a t a o b j e c t s

    l i k e n u m b e r s , s t r i n g s e t c . F o r e x a m p l e , a n a r i t h m e t i c e x p r e s s i o n l i k e ( 2 + 3 ) 5

    c a n b e r e p r e s e n t e d a s a - e x p r e s s i o n a n d i t s ` v a l u e ' 2 5 c a n a l s o b e r e p r e s e n t e d a s a

    - e x p r e s s i o n . T h e p r o c e s s o f ` s i m p l i f y i n g ' ( 2 + 3 ) 5 t o 2 5 w i l l b e r e p r e s e n t e d b y a

    p r o c e s s c a l l e d c o n v e r s i o n ( o r r e d u c t i o n ) . T h e r u l e s o f - c o n v e r s i o n d e s c r i b e d b e l o w

    a r e v e r y g e n e r a l , y e t w h e n t h e y a r e a p p l i e d t o - e x p r e s s i o n s r e p r e s e n t i n g a r i t h m e t i c

    e x p r e s s i o n s t h e y s i m u l a t e a r i t h m e t i c a l e v a l u a t i o n .

    T h e r e a r e t h r e e k i n d s o f - c o n v e r s i o n c a l l e d - c o n v e r s i o n , - c o n v e r s i o n a n d -

    c o n v e r s i o n ( t h e o r i g i n a l m o t i v a t i o n f o r t h e s e n a m e s i s n o t c l e a r ) . I n s t a t i n g t h e

    c o n v e r s i o n r u l e s t h e n o t a t i o n E E

    0

    = V ] i s u s e d t o m e a n t h e r e s u l t o f s u b s t i t u t i n g

    E

    0

    f o r e a c h f r e e o c c u r r e n c e o f V i n E . T h e s u b s t i t u t i o n i s c a l l e d v a l i d i f a n d o n l y

    i f n o f r e e v a r i a b l e i n E

    0

    b e c o m e s b o u n d i n E E

    0

    = V ] . S u b s t i t u t i o n i s d e s c r i b e d i n

    m o r e d e t a i l i n S e c t i o n 1 . 8 .

    T h e r u l e s o f - c o n v e r s i o n

    - c o n v e r s i o n .

    A n y a b s t r a c t i o n o f t h e f o r m V : E c a n b e c o n v e r t e d t o

    V

    0

    : E V

    0

    = V ] p r o v i d e d t h e s u b s t i t u t i o n o f V

    0

    f o r V i n E i s

    v a l i d .

    - c o n v e r s i o n .

    A n y a p p l i c a t i o n o f t h e f o r m ( V : E

    1

    ) E

    2

    c a n b e c o n v e r t e d t o

    E

    1

    E

    2

    = V ] , p r o v i d e d t h e s u b s t i t u t i o n o f E

    2

    f o r V i n E

    1

    i s v a l i d .

    - c o n v e r s i o n .

    A n y a b s t r a c t i o n o f t h e f o r m V : ( E V ) i n w h i c h V h a s n o f r e e

    o c c u r r e n c e i n E c a n b e r e d u c e d t o E .

    T h e f o l l o w i n g n o t a t i o n w i l l b e u s e d :

    E

    1

    ; !

    E

    2

    m e a n s E

    1

    - c o n v e r t s t o E

    2

    .

    E

    1

    ; !

    E

    2

    m e a n s E

    1

    - c o n v e r t s t o E

    2

    .

    E

    1

    ; !

    E

    2

    m e a n s E

    1

    - c o n v e r t s t o E

    2

    .

    I n S e c t i o n 1 . 4 . 4 b e l o w t h i s n o t a t i o n i s e x t e n d e d .

    T h e m o s t i m p o r t a n t k i n d o f c o n v e r s i o n i s - c o n v e r s i o n i t i s t h e o n e t h a t c a n b e

    u s e d t o s i m u l a t e a r b i t r a r y e v a l u a t i o n m e c h a n i s m s . - c o n v e r s i o n i s t o d o w i t h t h e

    t e c h n i c a l m a n i p u l a t i o n o f b o u n d v a r i a b l e s a n d - c o n v e r s i o n e x p r e s s e s t h e f a c t t h a t

    t w o f u n c t i o n s t h a t a l w a y s g i v e t h e s a m e r e s u l t s o n t h e s a m e a r g u m e n t s a r e e q u a l ( s e e

    S e c t i o n 1 . 7 ) . T h e n e x t t h r e e s u b s e c t i o n s g i v e f u r t h e r e x p l a n a t i o n a n d e x a m p l e s o f

    t h e t h r e e k i n d s o f c o n v e r s i o n ( n o t e t h a t ` c o n v e r s i o n ' a n d ` r e d u c t i o n ' a r e u s e d b e l o w

    a s s y n o n y m s ) .

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    10/104

    1 . 4 . C o n v e r s i o n r u l e s 5

    1 . 4 . 1 - c o n v e r s i o n

    A - e x p r e s s i o n ( n e c e s s a r i l y a n a b s t r a c t i o n ) t o w h i c h - r e d u c t i o n c a n b e a p p l i e d i s

    c a l l e d a n - r e d e x . T h e t e r m ` r e d e x ' a b b r e v i a t e s ` r e d u c i b l e e x p r e s s i o n ' . T h e r u l e

    o f - c o n v e r s i o n j u s t s a y s t h a t b o u n d v a r i a b l e s c a n b e r e n a m e d p r o v i d e d n o ` n a m e -

    c l a s h e s ' o c c u r .

    E x a m p l e s

    x : x ; !

    y : y

    x : f x ; !

    y : f y

    I t i s n o t t h e c a s e t h a t

    x : y : a d d x y ; !

    y : y : a d d y y

    b e c a u s e t h e s u b s t i t u t i o n ( y : a d d x y ) y = x ] i s n o t v a l i d s i n c e t h e y t h a t r e p l a c e s

    x b e c o m e s b o u n d . 2

    1 . 4 . 2 - c o n v e r s i o n

    A - e x p r e s s i o n ( n e c e s s a r i l y a n a p p l i c a t i o n ) t o w h i c h - r e d u c t i o n c a n b e a p p l i e d i s

    c a l l e d a - r e d e x . T h e r u l e o f - c o n v e r s i o n i s l i k e t h e e v a l u a t i o n o f a f u n c t i o n c a l l

    i n a p r o g r a m m i n g l a n g u a g e : t h e b o d y E

    1

    o f t h e f u n c t i o n V : E

    1

    i s e v a l u a t e d i n a n

    e n v i r o n m e n t i n w h i c h t h e ` f o r m a l p a r a m e t e r ' V i s b o u n d t o t h e ` a c t u a l p a r a m e t e r '

    E

    2

    .

    E x a m p l e s

    ( x : f x ) E ; !

    f E

    ( x : ( y : a d d x y ) ) 3 ; !

    y : a d d 3 y

    ( y : a d d 3 y ) 4 ; !

    a d d 3 4

    I t i s n o t t h e c a s e t h a t

    ( x : ( y : a d d x y ) ) ( s q u a r e y ) ; !

    y : a d d ( s q u a r e y ) y

    b e c a u s e t h e s u b s t i t i o n ( y : a d d x y ) ( s q u a r e y ) = x ] i s n o t v a l i d , s i n c e y i s f r e e i n

    ( s q u a r e y ) b u t b e c o m e s b o u n d a f t e r s u b s t i t u t i o n f o r x i n ( y : a d d x y ) . 2

    I t t a k e s s o m e p r a c t i c e t o p a r s e - e x p r e s s i o n s a c c o r d i n g t o t h e c o n v e n t i o n s o f S e c -

    t i o n 1 . 2 s o a s t o i d e n t i f y t h e - r e d e x e s . F o r e x a m p l e , c o n s i d e r t h e a p p l i c a t i o n :

    ( x : y : a d d x y ) 3 4 :

    P u t t i n g i n b r a c k e t s a c c o r d i n g t o t h e c o n v e n t i o n s e x p a n d s t h i s t o :

    ( ( ( x : ( y : ( ( a d d x ) y ) ) ) 3 ) 4 )

    w h i c h h a s t h e f o r m :

    ( ( x : E ) 3 ) 4

    w h e r e

    E = ( y : a d d x y )

    ( x : E ) 3 i s a - r e d e x a n d c o u l d b e r e d u c e d t o E 3 = x ] .

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    11/104

    6 C h a p t e r 1 . I n t r o d u c t i o n t o t h e - c a l c u l u s

    1 . 4 . 3 - c o n v e r s i o n

    A - e x p r e s s i o n ( n e c e s s a r i l y a n a b s t r a c t i o n ) t o w h i c h - r e d u c t i o n c a n b e a p p l i e d i s

    c a l l e d a n - r e d e x . T h e r u l e o f - c o n v e r s i o n e x p r e s s e s t h e p r o p e r t y t h a t t w o f u n c t i o n s

    a r e e q u a l i f t h e y g i v e t h e s a m e r e s u l t s w h e n a p p l i e d t o t h e s a m e a r g u m e n t s . T h i s

    p r o p e r t y i s c a l l e d e x t e n s i o n a l i t y a n d i s d i s c u s s e d f u r t h e r i n S e c t i o n 1 . 7 . F o r e x a m p l e ,

    - c o n v e r s i o n e n s u r e s t h a t x : ( s i n x ) a n d s i n d e n o t e t h e s a m e f u n c t i o n . M o r e

    g e n e r a l l y , V : ( E V ) d e n o t e s t h e f u n c t i o n w h i c h w h e n a p p l i e d t o a n a r g u m e n t E

    0

    r e t u r n s ( E V ) E

    0

    = V ] . I f V d o e s n o t o c c u r f r e e i n E t h e n ( E V ) E

    0

    = V ] = ( E E

    0

    ) .

    T h u s V : E V a n d E b o t h y i e l d t h e s a m e r e s u l t , n a m e l y E E

    0

    , w h e n a p p l i e d t o t h e

    s a m e a r g u m e n t s a n d h e n c e t h e y d e n o t e t h e s a m e f u n c t i o n .

    E x a m p l e s

    x : a d d x ; !

    a d d

    y : a d d x y ; !

    a d d x

    I t i s n o t t h e c a s e t h a t

    x : a d d x x ; !

    a d d x

    b e c a u s e x i s f r e e i n a d d x . 2

    1 . 4 . 4 G e n e r a l i z e d c o n v e r s i o n s

    T h e d e n i t i o n s o f ; !

    , ; !

    a n d ; !

    c a n b e g e n e r a l i z e d a s f o l l o w s :

    E

    1

    ; !

    E

    2

    i f E

    2

    c a n b e g o t f r o m E

    1

    b y - c o n v e r t i n g a n y s u b t e r m .

    E

    1

    ; !

    E

    2

    i f E

    2

    c a n b e g o t f r o m E

    1

    b y - c o n v e r t i n g a n y s u b t e r m .

    E

    1

    ; !

    E

    2

    i f E

    2

    c a n b e g o t f r o m E

    1

    b y - c o n v e r t i n g a n y s u b t e r m .

    E x a m p l e s

    ( ( x : y : a d d x y ) 3 ) 4 ; !

    ( y : a d d 3 y ) 4

    ( y : a d d 3 y ) 4 ; !

    a d d 3 4

    2

    T h e r s t o f t h e s e i s a - c o n v e r s i o n i n t h e g e n e r a l i z e d s e n s e b e c a u s e ( y : a d d 3 y ) 4

    i s o b t a i n e d f r o m ( ( x : y : a d d x y ) 3 ) 4 ( w h i c h i s n o t i t s e l f a - r e d e x ) b y r e d u c i n g

    t h e s u b e x p r e s s i o n ( x : y : a d d x y ) 3 . W e w i l l s o m e t i m e s w r i t e a s e q u e n c e o f

    c o n v e r s i o n s l i k e t h e t w o a b o v e a s :

    ( ( x : y : a d d x y ) 3 ) 4 ; !

    ( y : a d d 3 y ) 4 ; !

    a d d 3 4

    E x e r c i s e 2

    W h i c h o f t h e t h r e e - r e d u c t i o n s b e l o w a r e g e n e r a l i z e d c o n v e r s i o n s ( i . e . r e d u c t i o n s

    o f s u b e x p r e s s i o n s ) a n d w h i c h a r e c o n v e r s i o n s i n t h e s e n s e d e n e d o n p a g e 4 ? 2

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    12/104

    1 . 5 . E q u a l i t y o f - e x p r e s s i o n s 7

    ( i ) ( x : x ) 1 ; !

    1

    ( i i ) ( y : y ) ( ( x : x ) 1 ) ; !

    ( y : y ) 1 ; !

    1

    ( i i i ) ( y : y ) ( ( x : x ) 1 ) ; !

    ( x : x ) 1 ; !

    1

    I n r e d u c t i o n s ( i i ) a n d ( i i i ) i n t h e e x e r c i s e a b o v e o n e s t a r t s w i t h t h e s a m e -

    e x p r e s s i o n , b u t r e d u c e r e d e x e s i n d i e r e n t o r d e r s .

    A n i m p o r t a n t p r o p e r t y o f - r e d u c t i o n s i s t h a t n o m a t t e r i n w h i c h o r d e r o n e d o e s

    t h e m , o n e a l w a y s e n d s u p w i t h e q u i v a l e n t r e s u l t s . I f t h e r e a r e s e v e r a l d i s j o i n t

    r e d e x e s i n a n e x p r e s s i o n , o n e c a n r e d u c e t h e m i n p a r a l l e l . N o t e , h o w e v e r , t h a t s o m e

    r e d u c t i o n s e q u e n c e s m a y n e v e r t e r m i n a t e . T h i s i s d i s c u s s e d f u r t h e r i n c o n n e c t i o n

    w i t h t h e n o r m a l i z a t i o n t h e o r e m o f C h a p t e r 2 . 9 . I t i s a c u r r e n t h o t r e s e a r c h t o p i c i n

    ` f t h - g e n e r a t i o n c o m p u t i n g ' t o d e s i g n p r o c e s s o r s w h i c h e x p l o i t p a r a l l e l e v a l u a t i o n

    t o s p e e d u p t h e e x e c u t i o n o f f u n c t i o n a l p r o g r a m s .

    1 . 5 E q u a l i t y o f - e x p r e s s i o n s

    T h e t h r e e c o n v e r s i o n r u l e s p r e s e r v e t h e m e a n i n g o f - e x p r e s s i o n s , i . e . i f E

    1

    c a n

    b e c o n v e r t e d t o E

    2

    t h e n E

    1

    a n d E

    2

    d e n o t e t h e s a m e f u n c t i o n . T h i s p r o p e r t y o f

    c o n v e r s i o n s h o u l d b e i n t u i t i v e l y c l e a r . I t i s p o s s i b l e t o g i v e a m a t h e m a t i c a l d e n i t i o n

    o f t h e f u n c t i o n d e n o t e d b y a - e x p r e s s i o n a n d t h e n t o p r o v e t h a t t h i s f u n c t i o n i s

    u n c h a n g e d b y - , - o r - c o n v e r s i o n . D o i n g t h i s i s s u r p r i s i n g l y d i c u l t 3 3 ] a n d i s

    b e y o n d t h e s c o p e o f t h i s b o o k .

    W e w i l l s i m p l y d e n e t w o - e x p r e s s i o n s t o b e e q u a l i f t h e y c a n b e t r a n s f o r m e d i n t o

    e a c h o t h e r b y a s e q u e n c e o f ( f o r w a r d s o r b a c k w a r d s ) - c o n v e r s i o n s . I t i s i m p o r t a n t

    t o b e c l e a r a b o u t t h e d i e r e n c e b e t w e e n e q u a l i t y a n d i d e n t i t y . T w o - e x p r e s s i o n s

    a r e i d e n t i c a l i f t h e y c o n s i s t o f e x a c t l y t h e s a m e s e q u e n c e o f c h a r a c t e r s t h e y a r e

    e q u a l i f o n e c a n b e c o n v e r t e d t o t h e o t h e r . F o r e x a m p l e , x : x i s e q u a l t o y : y ,

    b u t n o t i d e n t i c a l t o i t . T h e f o l l o w i n g n o t a t i o n i s u s e d :

    E

    1

    E

    2

    m e a n s E

    1

    a n d E

    2

    a r e i d e n t i c a l .

    E

    1

    = E

    2

    m e a n s E

    1

    a n d E

    2

    a r e e q u a l .

    E q u a l i t y ( = ) i s d e n e d i n t e r m s o f i d e n t i t y ( ) a n d c o n v e r s i o n ( ; !

    , ; !

    a n d ; !

    )

    a s f o l l o w s .

    E q u a l i t y o f - e x p r e s s i o n s

    I f E a n d E

    0

    a r e - e x p r e s s i o n s t h e n E = E

    0

    i f E E

    0

    o r t h e r e e x i s t e x p r e s s i o n s

    E

    1

    , E

    2

    , . . . , E

    n

    s u c h t h a t :

    1 . E E

    1

    2 . E

    0

    E

    n

    3 . F o r e a c h i e i t h e r

    ( a ) E

    i

    ; !

    E

    i + 1

    o r E

    i

    ; !

    E

    i + 1

    o r E

    i

    ; !

    E

    i + 1

    o r

    ( b ) E

    i + 1

    ; !

    E

    i

    o r E

    i + 1

    ; !

    E

    i

    o r E

    i + 1

    ; !

    E

    i

    .

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    13/104

    8 C h a p t e r 1 . I n t r o d u c t i o n t o t h e - c a l c u l u s

    E x a m p l e s

    ( x : x ) 1 = 1

    ( x : x ) ( ( y : y ) 1 ) = 1

    ( x : y : a d d x y ) 3 4 = a d d 3 4

    2

    F r o m t h e d e n i t i o n o f = i t f o l l o w s t h a t :

    ( i ) F o r a n y E i t i s t h e c a s e t h a t E = E ( e q u a l i t y i s r e e x i v e ) .

    ( i i ) I f E = E

    0

    , t h e n E

    0

    = E ( e q u a l i t y i s s y m m e t r i c ) .

    ( i i i ) I f E = E

    0

    a n d E

    0

    = E

    0 0

    , t h e n E = E

    0 0

    ( e q u a l i t y i s t r a n s i t i v e ) .

    I f a r e l a t i o n i s r e e x i v e , s y m m e t r i c a n d t r a n s i t i v e t h e n i t i s c a l l e d a n e q u i v a l e n c e

    r e l a t i o n . T h u s = i s a n e q u i v a l e n c e r e l a t i o n .

    A n o t h e r i m p o r t a n t p r o p e r t y o f = i s t h a t i f E

    1

    = E

    2

    a n d i f E

    0

    1

    a n d E

    0

    2

    a r e t w o

    - e x p r e s s i o n s t h a t o n l y d i e r i n t h a t w h e r e o n e c o n t a i n s E

    1

    t h e o t h e r c o n t a i n s E

    2

    ,

    t h e n E

    0

    1

    = E

    0

    2

    . T h i s p r o p e r t y i s c a l l e d L e i b n i t z ' s l a w . I t h o l d s b e c a u s e t h e s a m e

    s e q u e n c e o f r e d u c t i o n f o r g e t t i n g f r o m E

    1

    t o E

    2

    c a n b e u s e d f o r g e t t i n g f r o m E

    0

    1

    t o

    E

    0

    2

    . F o r e x a m p l e , i f E

    1

    = E

    2

    , t h e n b y L e i b n i t z ' s l a w V : E

    1

    = V : E

    2

    .

    I t i s e s s e n t i a l f o r t h e s u b s t i t u t i o n s i n t h e - a n d - r e d u c t i o n s t o b e v a l i d . T h e v a -

    l i d i t y r e q u i r e m e n t d i s a l l o w s , f o r e x a m p l e , x : ( y : x ) b e i n g - r e d u c e d t o y : ( y : y )

    ( s i n c e y b e c o m e s b o u n d a f t e r s u b s t i t u t i o n f o r x i n y : x ) . I f t h i s i n v a l i d s u b s t i t u t i o n

    w e r e p e r m i t t e d , t h e n i t w o u l d f o l l o w b y t h e d e n i t i o n o f = t h a t :

    x : y : x = y : y : y

    B u t t h e n s i n c e :

    ( x : ( y : x ) ) 1 2 ; !

    ( y : 1 ) 2 ; !

    1

    a n d

    ( y : ( y : y ) ) 1 2 ; !

    ( y : y ) 2 ; !

    2

    o n e w o u l d b e f o r c e d t o c o n c l u d e t h a t 1 = 2 . M o r e g e n e r a l l y b y r e p l a c i n g 1 a n d 2

    b y a n y t w o e x p r e s s i o n s , i t c o u l d b e s h o w n t h a t a n y t w o e x p r e s s i o n s a r e e q u a l !

    E x e r c i s e 3

    F i n d a n e x a m p l e w h i c h s h o w s t h a t i f s u b s t i t u t i o n s i n - r e d u c t i o n s a r e a l l o w e d t o

    b e i n v a l i d , t h e n i t f o l l o w s t h a t a n y t w o - e x p r e s s i o n s a r e e q u a l . 2

    E x a m p l e : I f V

    1

    , V

    2

    , . . . , V

    n

    a r e a l l d i s t i n c t a n d n o n e o f t h e m o c c u r f r e e i n a n y o f

    E

    1

    , E

    2

    , . . . , E

    n

    , t h e n

    ( V

    1

    V

    2

    V

    n

    : E ) E

    1

    E

    2

    E

    n

    = ( ( V

    1

    : ( V

    2

    V

    n

    : E ) ) E

    1

    ) E

    2

    E

    n

    ; !

    ( ( V

    2

    V

    n

    : E ) E

    1

    = V

    1

    ] ) E

    2

    E

    n

    = ( V

    2

    : : : V

    n

    : E E

    1

    = V

    1

    ] ) E

    2

    E

    n

    .

    .

    .

    = E E

    1

    = V

    1

    ] E

    2

    = V

    2

    ] E

    n

    = V

    n

    ]

    2

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    14/104

    1 . 6 . T h e ; ! r e l a t i o n 9

    E x e r c i s e 4

    I n t h e l a s t e x a m p l e , w h e r e w a s t h e a s s u m p t i o n u s e d t h a t V

    1

    , V

    2

    , . . . , V

    n

    a r e a l l

    d i s t i n c t a n d t h a t n o n e o f t h e m o c c u r f r e e i n a n y o f E

    1

    , E

    2

    , . . . , E

    n

    ? 2

    E x e r c i s e 5

    F i n d a n e x a m p l e t o s h o w t h a t i f V

    1

    = V

    2

    , t h e n e v e n i f V

    2

    i s n o t f r e e i n E

    1

    , i t i s n o t

    n e c e s s a r i l y t h e c a s e t h a t :

    ( V

    1

    V

    2

    : E ) E

    1

    E

    2

    = E E

    1

    = V

    1

    ] E

    2

    = V

    2

    ]

    2

    E x e r c i s e 6

    F i n d a n e x a m p l e t o s h o w t h a t i f V

    1

    6= V

    2

    , b u t V

    2

    o c c u r s f r e e i n E

    1

    , t h e n i t i s n o t

    n e c e s s a r i l y t h e c a s e t h a t :

    ( V

    1

    V

    2

    : E ) E

    1

    E

    2

    = E E

    1

    = V

    1

    ] E

    2

    = V

    2

    ]

    2

    1 . 6 T h e ; ! r e l a t i o n

    I n t h e p r e v i o u s s e c t i o n E

    1

    = E

    2

    w a s d e n e d t o m e a n t h a t E

    2

    c o u l d b e o b t a i n e d

    f r o m E

    1

    b y a s e q u e n c e o f f o r w a r d s o r b a c k w a r d s c o n v e r s i o n s . A s p e c i a l c a s e o f

    t h i s i s w h e n E

    2

    i s g o t f r o m E

    1

    u s i n g o n l y f o r w a r d s c o n v e r s i o n s . T h i s i s w r i t t e n

    E

    1

    ; ! E

    2

    .

    D e n i t i o n o f ; !

    I f E a n d E

    0

    a r e - e x p r e s s i o n s , t h e n E ; ! E

    0

    i f E E

    0

    o r t h e r e e x i s t e x p r e s s i o n s

    E

    1

    , E

    2

    , . . . , E

    n

    s u c h t h a t :

    1 . E E

    1

    2 . E

    0

    E

    n

    3 . F o r e a c h i e i t h e r E

    i

    ; !

    E

    i + 1

    o r E

    i

    ; !

    E

    i + 1

    o r E

    i

    ; !

    E

    i + 1

    .

    N o t i c e t h a t t h e d e n i t i o n o f ; ! i s j u s t l i k e t h e d e n i t i o n o f = o n p a g e 7 e x c e p t

    t h a t p a r t ( b ) o f 3 i s m i s s i n g .

    E x e r c i s e 7

    F i n d E , E

    0

    s u c h t h a t E = E

    0

    b u t i t i s n o t t h e c a s e t h a t E ; ! E

    0

    . 2

    E x e r c i s e 8

    v e r y h a r d ! ] S h o w t h a t i f E

    1

    = E

    2

    , t h e n t h e r e e x i s t s E s u c h t h a t E

    1

    ; ! E

    a n d E

    2

    ; ! E . ( T h i s p r o p e r t y i s c a l l e d t h e C h u r c h - R o s s e r t h e o r e m . S o m e o f i t s

    c o n s e q u e n c e s a r e d i s c u s s e d i n C h a p t e r 2 . 9 . ) 2

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    15/104

    1 0 C h a p t e r 1 . I n t r o d u c t i o n t o t h e - c a l c u l u s

    1 . 7 E x t e n s i o n a l i t y

    S u p p o s e V d o e s n o t o c c u r f r e e i n E

    1

    o r E

    2

    a n d

    E

    1

    V = E

    2

    V

    T h e n b y L e i b n i t z ' s l a w ( s e e p a g e 8 )

    V : E

    1

    V = V : E

    2

    V

    s o b y - r e d u c t i o n a p p l i e d t o b o t h s i d e s

    E

    1

    = E

    2

    I t i s o f t e n c o n v e n i e n t t o p r o v e t h a t t w o - e x p r e s s i o n s a r e e q u a l u s i n g t h i s p r o p e r t y ,

    i . e . t o p r o v e E

    1

    = E

    2

    b y p r o v i n g E

    1

    V = E

    2

    V f o r s o m e V n o t o c c u r i n g f r e e i n E

    1

    o r E

    2

    . W e w i l l r e f e r t o s u c h p r o o f s a s b e i n g b y e x t e n s i o n a l i t y .

    E x e r c i s e 9

    S h o w t h a t

    ( f g x : f x ( g x ) ) ( x y : x ) ( x y : x ) = x : x

    2

    1 . 8 S u b s t i t u t i o n

    A t t h e b e g i n n i n g o f S e c t i o n 1 . 4 E E

    0

    = V ] w a s d e n e d t o m e a n t h e r e s u l t o f s u b s t i -

    t u t i n g E

    0

    f o r e a c h f r e e o c c u r r e n c e o f V i n E . T h e s u b s t i t u t i o n w a s s a i d t o b e v a l i d

    i f n o f r e e v a r i a b l e i n E

    0

    b e c a m e b o u n d i n E E

    0

    = V ] . I n t h e d e n i t i o n s o f - a n d

    - c o n v e r s i o n , i t w a s s t i p u l a t e d t h a t t h e s u b s t i t u t i o n s i n v o l v e d m u s t b e v a l i d . T h u s ,

    f o r e x a m p l e , i t w a s o n l y t h e c a s e t h a t

    ( V : E

    1

    ) E

    2

    ; !

    E

    1

    E

    2

    = V ]

    a s l o n g a s t h e s u b s t i t u t i o n E

    1

    E

    2

    = V ] w a s v a l i d .

    I t i s v e r y c o n v e n i e n t t o e x t e n d t h e m e a n i n g o f E E

    0

    = V ] s o t h a t w e d o n ' t h a v e

    t o w o r r y a b o u t v a l i d i t y . T h i s i s a c h i e v e d b y t h e d e n i t i o n b e l o w w h i c h h a s t h e

    p r o p e r t y t h a t f o r a l l e x p r e s s i o n s E , E

    1

    a n d E

    2

    a n d a l l v a r i a b l e s V a n d V

    0

    :

    ( V : E

    1

    ) E

    2

    ; ! E

    1

    E

    2

    = V ] a n d V : E ; ! V

    0

    : E V

    0

    = V ]

    T o e n s u r e t h i s p r o p e r t y h o l d s , E E

    0

    = V ] i s d e n e d r e c u r s i v e l y o n t h e s t r u c t u r e o f

    E a s f o l l o w s :

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    16/104

    1 . 8 . S u b s t i t u t i o n 1 1

    E E E

    0

    = V ]

    V E

    0

    V

    0

    ( w h e r e V 6= V

    0

    ) V

    0

    E

    1

    E

    2

    E

    1

    E

    0

    = V ] E

    2

    E

    0

    = V ]

    V : E

    1

    V : E

    1

    V

    0

    : E

    1

    ( w h e r e V 6= V

    0

    a n d V

    0

    : E

    1

    E

    0

    = V ]

    V

    0

    i s n o t f r e e i n E

    0

    )

    V

    0

    : E

    1

    ( w h e r e V 6= V

    0

    a n d V

    0 0

    : E

    1

    V

    0 0

    = V

    0

    ] E

    0

    = V ]

    V

    0

    i s f r e e i n E

    0

    ) w h e r e V

    0 0

    i s a v a r i a b l e

    n o t f r e e i n E

    0

    o r E

    1

    T h i s p a r t i c u l a r d e n i t i o n o f E E

    0

    = V ] i s b a s e d o n ( b u t n o t i d e n t i c a l t o ) t h e o n e i n

    A p p e n d i x C o f 2 ] .

    T o i l l u s t r a t e h o w t h i s w o r k s c o n s i d e r ( y : y x ) y = x ] . S i n c e y i s f r e e i n y , t h e l a s t

    c a s e o f t h e t a b l e a b o v e a p p l i e s . S i n c e z d o e s n o t o c c u r i n y x o r y ,

    ( y : y x ) y = x ] z : ( y x ) z = y ] y = x ] z : ( z x ) y = x ] z : z y

    I n t h e l a s t l i n e o f t h e t a b l e a b o v e , t h e p a r t i c u l a r c h o i c e o f V

    0 0

    i s n o t s p e c i e d . A n y

    v a r i a b l e n o t o c c u r r i n g i n E

    0

    o r E

    1

    w i l l d o .

    A g o o d d i s c u s s i o n o f s u b s t i t u t i o n c a n b e f o u n d i n t h e b o o k b y H i n d l e y a n d S e l d i n

    1 9 ] w h e r e v a r i o u s t e c h n i c a l p r o p e r t i e s a r e s t a t e d a n d p r o v e d . T h e f o l l o w i n g e x e r c i s e

    i s t a k e n f r o m t h a t b o o k .

    E x e r c i s e 1 0

    U s e t h e t a b l e a b o v e t o w o r k o u t

    ( i ) ( y : x ( x : x ) ) ( y : y x ) = x ] .

    ( i i ) ( y ( z : x z ) ) ( y : z y ) = x ] .

    2

    I t i s s t r a i g h t f o r w a r d , b u t r a t h e r t e d i o u s , t o p r o v e f r o m t h e d e n i t i o n o f E E

    0

    = V ]

    j u s t g i v e n t h a t i n d e e d

    ( V : E

    1

    ) E

    2

    ; ! E

    1

    E

    2

    = V ] a n d V : E ; ! V

    0

    : E V

    0

    = V ]

    f o r a l l e x p r e s s i o n s E , E

    1

    a n d E

    2

    a n d a l l v a r i a b l e s V a n d V

    0

    .

    I n C h a p t e r 3 i t w i l l b e s h o w n h o w t h e t h e o r y o f c o m b i n a t o r s c a n b e u s e d t o d e c o m -

    p o s e t h e c o m p l e x i t i e s o f s u b s t i t u t i o n i n t o s i m p l e r o p e r a t i o n s . I n s t e a d o f c o m b i n a t o r s

    i t i s p o s s i b l e t o u s e t h e s o - c a l l e d n a m e l e s s t e r m s o f D e B r u i j n 6 ] . D e B r u i j n ' s i d e a

    i s t h a t v a r i a b l e s c a n b e t h o u g h t o f a s ` p o i n t e r s ' t o t h e s t h a t b i n d t h e m . I n s t e a d o f

    ` l a b e l l i n g ' s w i t h n a m e s ( i . e . b o u n d v a r i a b l e s ) a n d t h e n p o i n t i n g t o t h e m v i a t h e s e

    n a m e s , o n e c a n p o i n t t o t h e a p p r o p r i a t e b y g i v i n g t h e n u m b e r o f l e v e l s ` u p w a r d s '

    n e e d e d t o r e a c h i t . F o r e x a m p l e , x : y : x y w o u l d b e r e p r e s e n t e d b y 2 1 . A s a

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    17/104

    1 2 C h a p t e r 1 . I n t r o d u c t i o n t o t h e - c a l c u l u s

    m o r e c o m p l i c a t e d e x a m p l e , c o n s i d e r t h e e x p r e s s i o n b e l o w i n w h i c h w e i n d i c a t e t h e

    n u m b e r o f l e v e l s s e p a r a t i n g a v a r i a b l e f r o m t h e t h a t b i n d s i t .

    3

    z } | {

    2

    z } | {

    x : y : x y ( y : x y y )

    | { z }

    1

    | { z }

    1

    I n D e B r u i j n ' s n o t a t i o n t h i s i s 2 1 3 1 1 .

    A f r e e v a r i a b l e i n a n e x p r e s s i o n i s r e p r e s e n t e d b y a n u m b e r b i g g e r t h a n t h e d e p t h o f

    s a b o v e i t d i e r e n t f r e e v a r i a b l e s b e i n g a s s i g n e d d i e r e n t n u m b e r s . F o r e x a m p l e ,

    x : ( y : y x z ) x y w

    w o u l d b e r e p r e s e n t e d b y

    ( 1 2 3 ) 1 2 4

    S i n c e t h e r e a r e o n l y t w o s a b o v e t h e o c c u r r e n c e o f 3 , t h i s n u m b e r m u s t d e n o t e a

    f r e e v a r i a b l e s i m i l a r l y t h e r e i s o n l y o n e a b o v e t h e s e c o n d o c c u r r e n c e o f 2 a n d t h e

    o c c u r r e n c e o f 4 , s o t h e s e t o o m u s t b e f r e e v a r i a b l e s . N o t e t h a t 2 c o u l d n o t b e u s e d

    t o r e p r e s e n t w s i n c e t h i s h a d a l r e a d y b e e n u s e d t o r e p r e s e n t t h e f r e e y w e t h u s

    c h o s e t h e r s t a v a i l a b l e n u m b e r b i g g e r t h a n 2 ( 3 w a s a l r e a d y i n u s e r e p r e s e n t i n g z ) .

    C a r e m u s t b e t a k e n t o a s s i g n b i g e n o u g h n u m b e r s t o f r e e v a r i a b l e s . F o r e x a m p l e ,

    t h e r s t o c c u r r e n c e o f z i n x : z ( y : z ) c o u l d b e r e p r e s e n t e d b y 2 , b u t t h e s e c o n d

    o c c u r r e n c e r e q u i r e s 3 s i n c e t h e y a r e t h e s a m e v a r i a b l e w e m u s t u s e 3 .

    E x a m p l e : W i t h D e B r u i j n ' s s c h e m e x : x ( y : x y y ) w o u l d b e r e p r e s e n t e d b y

    1 ( 2 1 1 ) . 2

    E x e r c i s e 1 1

    W h a t - e x p r e s s i o n i s r e p r e s e n t e d b y 2 ( 2 ) ? 2

    E x e r c i s e 1 2

    D e s c r i b e a n a l g o r i t h m f o r c o m p u t a t i n g t h e D e B r u i j n r e p r e s e n t a t i o n o f t h e e x p r e s -

    s i o n E E

    0

    = V ] f r o m t h e r e p r e s e n t a t i o n s o f E a n d E

    0

    . 2

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    18/104

    C h a p t e r 2

    R e p r e s e n t i n g T h i n g s i n t h e

    - c a l c u l u s

    T h e - c a l c u l u s a p p e a r s a t r s t s i g h t t o b e a v e r y p r i m i t i v e l a n g u a g e . H o w e v e r ,

    i t c a n b e u s e d t o r e p r e s e n t m o s t o f t h e o b j e c t s a n d s t r u c t u r e s n e e d e d f o r m o d e r n

    p r o g r a m m i n g . T h e i d e a i s t o c o d e t h e s e o b j e c t s a n d s t r u c t u r e s i n s u c h a w a y t h a t

    t h e y h a v e t h e r e q u i r e d p r o p e r t i e s . F o r e x a m p l e , t o r e p r e s e n t t h e t r u t h v a l u e s t r u e

    a n d f a l s e a n d t h e B o o l e a n f u n c t i o n : ( ` n o t ' ) , - e x p r e s s i o n s t r u e , f a l s e a n d n o t a r e

    d e v i s e d w i t h t h e p r o p e r t i e s t h a t :

    n o t t r u e = f a l s e

    n o t f a l s e = t r u e

    T o r e p r e s e n t t h e B o o l e a n f u n c t i o n ( ` a n d ' ) a - e x p r e s s i o n a n d i s d e v i s e d s u c h

    t h a t :

    a n d t r u e t r u e = t r u e

    a n d t r u e f a l s e = f a l s e

    a n d f a l s e t r u e = f a l s e

    a n d f a l s e f a l s e = f a l s e

    a n d t o r e p r e s e n t _ ( ` o r ' ) a n e x p r e s s i o n o r s u c h t h a t :

    o r t r u e t r u e = t r u e

    o r t r u e f a l s e = t r u e

    o r f a l s e t r u e = t r u e

    o r f a l s e f a l s e = f a l s e

    T h e - e x p r e s s i o n s u s e d t o r e p r e s e n t t h i n g s m a y a p p e a r c o m p l e t e l y u n m o t i v a t e d a t

    r s t . H o w e v e r , t h e d e n i t i o n s a r e c h o s e n s o t h a t t h e y w o r k t o g e t h e r i n u n i s o n .

    W e w i l l w r i t e

    L E T = - e x p r e s s i o n

    t o i n t r o d u c e a s a n e w n o t a t i o n . U s u a l l y w i l l j u s t b e a n a m e s u c h a s t r u e

    o r a n d . S u c h n a m e s a r e w r i t t e n i n b o l d f a c e , o r u n d e r l i n e d , t o d i s t i n g u i s h t h e m

    f r o m v a r i a b l e s . T h u s , f o r e x a m p l e , t r u e i s a v a r i a b l e b u t t r u e i s t h e - e x p r e s s i o n

    x : y : x ( s e e S e c t i o n 2 . 1 b e l o w ) a n d 2 i s a n u m b e r b u t 2 i s t h e - e x p r e s s i o n

    f x : f ( f x ) ( s e e S e c t i o n 2 . 3 ) .

    S o m e t i m e s w i l l b e a m o r e c o m p l i c a t e d f o r m l i k e t h e c o n d i t i o n a l n o t a t i o n ( E !

    E

    1

    j E

    2

    ) .

    2 . 1 T r u t h - v a l u e s a n d t h e c o n d i t i o n a l

    T h i s s e c t i o n d e n e s - e x p r e s s i o n s t r u e , f a l s e , n o t a n d ( E ! E

    1

    j E

    2

    ) w i t h t h e

    f o l l o w i n g p r o p e r t i e s :

    1 3

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    19/104

    1 4 C h a p t e r 2 . R e p r e s e n t i n g T h i n g s i n t h e - c a l c u l u s

    n o t t r u e = f a l s e

    n o t f a l s e = t r u e

    ( t r u e ! E

    1

    j E

    2

    ) = E

    1

    ( f a l s e ! E

    1

    j E

    2

    ) = E

    2

    T h e - e x p r e s s i o n s t r u e a n d f a l s e r e p r e s e n t t h e t r u t h - v a l u e s t r u e a n d f a l s e , n o t

    r e p r e s e n t s t h e n e g a t i o n f u n c t i o n : a n d ( E ! E

    1

    j E

    2

    ) r e p r e s e n t s t h e c o n d i t i o n a l ` i f

    E t h e n E

    1

    e l s e E

    2

    ' .

    T h e r e a r e i n n i t e l y m a n y d i e r e n t w a y s o f r e p r e s e n t i n g t h e t r u t h - v a l u e s a n d n e g a -

    t i o n t h a t w o r k t h e o n e s u s e d h e r e a r e t r a d i t i o n a l a n d h a v e b e e n d e v e l o p e d o v e r t h e

    y e a r s b y l o g i c i a n s .

    L E T t r u e = x : y : x

    L E T f a l s e = x : y : y

    L E T n o t = t : t f a l s e t r u e

    I t i s e a s y t o u s e t h e r u l e s o f - c o n v e r s i o n t o s h o w t h a t t h e s e d e n i t i o n s h a v e t h e

    d e s i r e d p r o p e r t i e s . F o r e x a m p l e :

    n o t t r u e = ( t : t f a l s e t r u e ) t r u e ( d e n i t i o n o f n o t )

    = t r u e f a l s e t r u e ( - c o n v e r s i o n )

    = ( x : y : x ) f a l s e t r u e ( d e n i t i o n o f t r u e )

    = ( y : f a l s e ) t r u e ( - c o n v e r s i o n )

    = f a l s e ( - c o n v e r s i o n )

    S i m i l a r l y n o t f a l s e = t r u e .

    C o n d i t i o n a l e x p r e s s i o n s ( E ! E

    1

    j E

    2

    ) c a n b e d e n e d a s f o l l o w s :

    L E T ( E ! E

    1

    j E

    2

    ) = ( E E

    1

    E

    2

    )

    T h i s m e a n s t h a t f o r a n y - e x p r e s s i o n s E , E

    1

    a n d E

    2

    , ( E ! E

    1

    j E

    2

    ) s t a n d s f o r

    ( E E

    1

    E

    2

    ) .

    T h e c o n d i t i o n a l n o t a t i o n b e h a v e s a s i t s h o u l d :

    ( t r u e ! E

    1

    j E

    2

    ) = t r u e E

    1

    E

    2

    = ( x y : x ) E

    1

    E

    2

    = E

    1

    a n d

    ( f a l s e ! E

    1

    j E

    2

    ) = f a l s e E

    1

    E

    2

    = ( x y : y ) E

    1

    E

    2

    = E

    2

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    20/104

    2 . 2 . P a i r s a n d t u p l e s 1 5

    E x e r c i s e 1 3

    L e t a n d b e t h e - e x p r e s s i o n x y : ( x ! y j f a l s e ) . S h o w t h a t :

    a n d t r u e t r u e = t r u e

    a n d t r u e f a l s e = f a l s e

    a n d f a l s e t r u e = f a l s e

    a n d f a l s e f a l s e = f a l s e

    2

    E x e r c i s e 1 4

    D e v i s e a - e x p r e s s i o n o r s u c h t h a t :

    o r t r u e t r u e = t r u e

    o r t r u e f a l s e = t r u e

    o r f a l s e t r u e = t r u e

    o r f a l s e f a l s e = f a l s e

    2

    2 . 2 P a i r s a n d t u p l e s

    T h e f o l l o w i n g a b b r e v i a t i o n s r e p r e s e n t p a i r s a n d n - t u p l e s i n t h e - c a l c u l u s .

    L E T f s t = p : p t r u e

    L E T s n d = p : p f a l s e

    L E T ( E

    1

    E

    2

    ) = f : f E

    1

    E

    2

    ( E

    1

    E

    2

    ) i s a - e x p r e s s i o n r e p r e s e n t i n g a n o r d e r e d p a i r w h o s e r s t c o m p o n e n t

    ( i . e . E

    1

    ) i s a c c e s s e d w i t h t h e f u n c t i o n f s t a n d w h o s e s e c o n d c o m p o n e n t ( i . e . E

    2

    )

    i s a c c e s s e d w i t h s n d . T h e f o l l o w i n g c a l c u l a t i o n s h o w s h o w t h e v a r i o u s d e n i t i o n s

    c o - o p e r a t e t o g e t h e r t o g i v e t h e r i g h t r e s u l t s .

    f s t ( E

    1

    E

    2

    ) = ( p : p t r u e ) ( E

    1

    E

    2

    )

    = ( E

    1

    E

    2

    ) t r u e

    = ( f : f E

    1

    E

    2

    ) t r u e

    = t r u e E

    1

    E

    2

    = ( x y : x ) E

    1

    E

    2

    = E

    1

    E x e r c i s e 1 5

    S h o w t h a t s n d ( E

    1

    E

    2

    ) = E

    2

    .

    2

    A p a i r i s a d a t a - s t r u c t u r e w i t h t w o c o m p o n e n t s . T h e g e n e r a l i z a t i o n t o n c o m p o n e n t s

    i s c a l l e d a n n - t u p l e a n d i s e a s i l y d e n e d i n t e r m s o f p a i r s .

    L E T ( E

    1

    E

    2

    : : : E

    n

    ) = ( E

    1

    ( E

    2

    ( ( E

    n ; 1

    E

    n

    ) ) ) )

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    21/104

    1 6 C h a p t e r 2 . R e p r e s e n t i n g T h i n g s i n t h e - c a l c u l u s

    ( E

    1

    : : : E

    n

    ) i s a n n - t u p l e w i t h c o m p o n e n t s E

    1

    , : : : , E

    n

    a n d l e n g t h n . P a i r s a r e 2 -

    t u p l e s . T h e a b b r e v i a t i o n s d e n e d n e x t p r o v i d e a w a y o f e x t r a c t i n g t h e c o m p o n e n t s

    o f n - t u p l e s .

    L E T E

    n

    # 1 = f s t E

    L E T E

    n

    # 2 = f s t ( s n d E )

    .

    .

    .

    L E T E

    n

    # i = f s t ( s n d ( s n d ( ( s n d

    | { z }

    i ; 1 s n d s

    E ) ) ) ) ( i f i < n )

    .

    .

    .

    L E T E

    n

    # n = s n d ( s n d ( : : : ( s n d

    | { z }

    n ; 1 s n d s

    E ) : : : ) ) )

    I t i s e a s y t o s e e t h a t t h e s e d e n i t i o n s w o r k , f o r e x a m p l e :

    ( E

    1

    E

    2

    : : : E

    n

    )

    n

    # 1 = ( E

    1

    ( E

    2

    ( : : : ) ) )

    n

    # 1

    = f s t ( E

    1

    ( E

    2

    ( : : : ) ) )

    = E

    1

    ( E

    1

    E

    2

    : : : E

    n

    )

    n

    # 2 = ( E

    1

    ( E

    2

    ( : : : ) ) )

    n

    # 2

    = f s t ( s n d ( E

    1

    ( E

    2

    ( : : : ) ) ) )

    = f s t ( E

    2

    ( : : : ) )

    = E

    2

    I n g e n e r a l ( E

    1

    E

    2

    : : : E

    n

    )

    n

    # i = E

    i

    f o r a l l i s u c h t h a t 1 i n .

    C o n v e n t i o n

    W e w i l l u s u a l l y j u s t w r i t e E # i i n s t e a d o f E

    n

    # i w h e n i t i s c l e a r f r o m t h e c o n t e x t

    w h a t n s h o u l d b e . F o r e x a m p l e ,

    ( E

    1

    : : : E

    n

    ) # i = E

    i

    ( w h e r e 1 i n )

    2 . 3 N u m b e r s

    T h e r e a r e m a n y w a y s t o r e p r e s e n t n u m b e r s b y - e x p r e s s i o n s , e a c h w i t h t h e i r o w n

    a d v a n t a g e s a n d d i s a d v a n t a g e s 3 8 , 2 2 ] . T h e g o a l i s t o d e n e f o r e a c h n u m b e r n a -

    e x p r e s s i o n n t h a t r e p r e s e n t s i t . W e a l s o w a n t t o d e n e - e x p r e s s i o n s t o r e p r e s e n t t h e

    p r i m i t i v e a r i t h m e t i c a l o p e r a t i o n s . F o r e x a m p l e , w e w i l l n e e d - e x p r e s s i o n s s u c , p r e ,

    a d d a n d i s z e r o r e p r e s e n t i n g t h e s u c c e s s o r f u n c t i o n ( n 7! n + 1 ) , t h e p r e d e c e s s o r

    f u n c t i o n ( n 7! n ; 1 ) , a d d i t i o n a n d a t e s t f o r z e r o , r e s p e c t i v e l y . T h e s e - e x p r e s s i o n s

    w i l l r e p r e s e n t t h e n u m b e r s c o r r e c t l y i f t h e y h a v e t h e f o l l o w i n g p r o p e r t i e s :

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    22/104

    2 . 3 . N u m b e r s 1 7

    s u c n = n + 1 ( f o r a l l n u m b e r s n )

    p r e n = n ; 1 ( f o r a l l n u m b e r s n )

    a d d m n = m + n ( f o r a l l n u m b e r s m a n d n )

    i s z e r o 0 = t r u e

    i s z e r o ( s u c n ) = f a l s e

    T h e r e p r e s e n t a t i o n o f n u m b e r s d e s c r i b e d h e r e i s t h e o r i g i n a l o n e d u e t o C h u r c h . I n

    o r d e r t o e x p l a i n t h i s i t i s c o n v e n i e n t t o d e n e f

    n

    x t o m e a n n a p p l i c a t i o n s o f f t o

    x . F o r e x a m p l e ,

    f

    5

    x = f ( f ( f ( f ( f x ) ) ) )

    B y c o n v e n t i o n f

    0

    x i s d e n e d t o m e a n x . M o r e g e n e r a l l y :

    L E T E

    0

    E

    0

    = E

    0

    L E T E

    n

    E

    0

    = E ( E ( ( E

    | { z }

    n E s

    E

    0

    ) ) )

    N o t e t h a t E

    n

    ( E E

    0

    ) = E

    n + 1

    E

    0

    = E ( E

    n

    E

    0

    ) w e w i l l u s e t h e f a c t l a t e r .

    E x a m p l e :

    f

    4

    x = f ( f ( f ( f x ) ) ) = f ( f

    3

    x ) = f

    3

    ( f x )

    2

    U s i n g t h e n o t a t i o n j u s t i n t r o d u c e d w e c a n n o w d e n e C h u r c h ' s n u m e r a l s . N o t i c e

    h o w t h e d e n i t i o n o f t h e - e x p r e s s i o n n b e l o w e n c o d e s a u n a r y r e p r e s e n t a t i o n o f n .

    L E T 0 = f x : x

    L E T 1 = f x : f x

    L E T 2 = f x : f ( f x )

    .

    .

    .

    L E T n = f x : f

    n

    x

    .

    .

    .

    T h e r e p r e s e n t a t i o n s o f s u c , a d d a n d i s z e r o a r e n o w m a g i c a l l y p u l l e d o u t o f a h a t .

    T h e b e s t w a y t o s e e h o w t h e y w o r k i s t o t h i n k o f t h e m a s o p e r a t i n g o n u n a r y

    r e p r e s e n t a t i o n s o f n u m b e r s . T h e e x e r c i s e s t h a t f o l l o w s h o u l d h e l p .

    L E T s u c = n f x : n f ( f x )

    L E T a d d = m n f x : m f ( n f x )

    L E T i s z e r o = n : n ( x : f a l s e ) t r u e

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    23/104

    1 8 C h a p t e r 2 . R e p r e s e n t i n g T h i n g s i n t h e - c a l c u l u s

    E x e r c i s e 1 6

    S h o w :

    ( i ) s u c 0 = 1

    ( i i ) s u c 5 = 6

    ( i i i ) i s z e r o 0 = t r u e

    ( i v ) i s z e r o 5 = f a l s e

    ( v ) a d d 0 1 = 1

    ( v i ) a d d 2 3 = 5

    2

    E x e r c i s e 1 7

    S h o w f o r a l l n u m b e r s m a n d n :

    ( i ) s u c n = n + 1

    ( i i ) i s z e r o ( s u c n ) = f a l s e

    ( i i i ) a d d 0 n = n

    ( i v ) a d d m 0 = m

    ( v ) a d d m n = m + n

    2

    T h e p r e d e c e s o r f u n c t i o n i s h a r d e r t o d e n e t h a n t h e o t h e r p r i m i t i v e f u n c t i o n s .

    T h e i d e a i s t h a t t h e p r e d e c e s s o r o f n i s d e n e d b y u s i n g f x : f

    n

    x ( i . e . n ) t o

    o b t a i n a f u n c t i o n t h a t a p p l i e s f o n l y n ; 1 t i m e s . T h e t r i c k i s t o ` t h r o w a w a y ' t h e

    r s t a p p l i c a t i o n o f f i n f

    n

    . T o a c h i e v e t h i s , w e r s t d e n e a f u n c t i o n p r e f n t h a t

    o p e r a t e s o n p a i r s a n d h a s t h e p r o p e r t y t h a t :

    ( i ) p r e f n f ( t r u e x ) = ( f a l s e x )

    ( i i ) p r e f n f ( f a l s e x ) = ( f a l s e f x )

    F r o m t h i s i t f o l l o w s t h a t :

    ( i i i ) ( p r e f n f )

    n

    ( f a l s e x ) = ( f a l s e f

    n

    x )

    ( i v ) ( p r e f n f )

    n

    ( t r u e x ) = ( f a l s e f

    n ; 1

    x ) ( i f n > 0 )

    T h u s n a p p l i c a t i o n s o f p r e f n t o ( t r u e x ) r e s u l t i n n ; 1 a p p l i c a t i o n s o f f t o x . W i t h

    t h i s i d e a , t h e d e n i t i o n o f t h e p r e d e c e s s o r f u n c t i o n p r e i s s t r a i g h t f o r w a r d . B e f o r e

    g i v i n g i t , h e r e i s t h e d e n i t i o n o f p r e f n :

    L E T p r e f n = f p : ( f a l s e ( f s t p ! s n d p j ( f ( s n d p ) ) ) )

    E x e r c i s e 1 8

    S h o w p r e f n f ( b x ) = ( f a l s e ( b ! x j f x ) ) a n d h e n c e :

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    24/104

    2 . 3 . N u m b e r s 1 9

    ( i ) p r e f n f ( t r u e x ) = ( f a l s e x )

    ( i i ) p r e f n f ( f a l s e x ) = ( f a l s e f x )

    ( i i i ) ( p r e f n f )

    n

    ( f a l s e x ) = ( f a l s e f

    n

    x )

    ( i v ) ( p r e f n f )

    n

    ( t r u e x ) = ( f a l s e f

    n ; 1

    x ) ( i f n > 0 )

    2

    T h e p r e d e c e s s o r f u n c t i o n p r e c a n n o w b e d e n e d .

    L E T p r e = n f x : s n d ( n ( p r e f n f ) ( t r u e x ) )

    I t f o l l o w s t h a t i f n > 0

    p r e n f x = s n d ( n ( p r e f n f ) ( t r u e x ) ) ( d e n i t i o n o f p r e )

    = s n d ( ( p r e f n f )

    n

    ( t r u e x ) ) ( d e n i t i o n o f n )

    = s n d ( f a l s e f

    n ; 1

    x ) ( b y ( v ) a b o v e )

    = f

    n ; 1

    x

    h e n c e b y e x t e n s i o n a l i t y ( S e c t i o n 1 . 7 o n p a g e 1 0 )

    p r e n = f x : f

    n ; 1

    x

    = n ; 1 ( d e n i t i o n o f n ; 1 )

    E x e r c i s e 1 9

    U s i n g t h e r e s u l t s o f t h e p r e v i o u s e x e r c i s e ( o r o t h e r w i s e ) s h o w t h a t

    ( i ) p r e ( s u c n ) = n

    ( i i ) p r e 0 = 0

    2

    T h e n u m e r a l s y s t e m i n t h e n e x t e x e r c i s e i s t h e o n e u s e d i n 2 ] a n d h a s s o m e a d v a n -

    t a g e s o v e r C h u r c h ' s ( e . g . t h e p r e d e c e s s o r f u n c t i o n i s e a s i e r t o d e n e ) .

    E x e r c i s e 2 0

    L E T

    b

    0 = x : x

    L E T

    b

    1 = ( f a l s e

    b

    0 )

    L E T

    b

    2 = ( f a l s e

    b

    1 )

    .

    .

    .

    L E T

    d

    n + 1 = ( f a l s e bn )

    .

    .

    .

    D e v i s e - e x p r e s s i o n s ds u c ,

    d

    i s z e r o , dp r e s u c h t h a t f o r a l l n :

    ( i )

    ds u c bn =

    d

    n + 1

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    25/104

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    26/104

    2 . 4 . D e n i t i o n b y r e c u r s i o n 2 1

    L E T Y = f : ( x : f ( x x ) ) ( x : f ( x x ) )

    I t i s s t r a i g h t f o r w a r d t o s h o w t h a t Y i s i n d e e d a x e d - p o i n t o p e r a t o r :

    Y E = ( f : ( x : f ( x x ) ) ( x : f ( x x ) ) ) E ( d e n i t i o n o f Y )

    = ( x : E ( x x ) ) ( x : E ( x x ) ) ( - c o n v e r s i o n )

    = E ( ( x : E ( x x ) ) ( x : E ( x x ) ) ) ( - c o n v e r s i o n )

    = E ( Y E ) ( t h e l i n e b e f o r e l a s t )

    T h i s c a l c u l a t i o n s h o w s t h a t e v e r y - e x p r e s s i o n E h a s a x e d p o i n t .

    A r m e d w i t h Y , w e c a n n o w r e t u r n t o t h e p r o b l e m o f s o l v i n g t h e e q u a t i o n f o r m u l t .

    S u p p o s e m u l t f n i s d e n e d b y

    L E T m u l t f n = f

    6

    m n : ( i s z e r o m ! 0 j a d d n ( f

    6

    ( p r e m ) n ) )

    a n d t h e n d e n e m u l t b y :

    L E T m u l t = Y m u l t f n

    T h e n :

    m u l t m n = ( Y m u l t f n ) m n ( d e n i t i o n o f m u l t )

    = m u l t f n ( Y m u l t f n ) m n ( x e d - p o i n t p r o p e r t y o f Y )

    = m u l t f n m u l t m n ( d e n i t i o n o f m u l t )

    = ( f m n : ( i s z e r o m ! 0 j a d d n ( f ( p r e m ) n ) ) ) m u l t m n

    ( d e n i t i o n o f m u l t f n )

    = ( i s z e r o m ! 0 j a d d n ( m u l t ( p r e m ) n ) ) ( - c o n v e r s i o n )

    A n e q u a t i o n o f t h e f o r m f x

    1

    x

    n

    = E i s c a l l e d r e c u r s i v e i f f o c c u r s f r e e i n E .

    Y p r o v i d e s a g e n e r a l w a y o f s o l v i n g s u c h e q u a t i o n s . S t a r t w i t h a n e q u a t i o n o f t h e

    f o r m :

    f x

    1

    : : : x

    n

    =

    g

    f

    g

    w h e r e

    g

    f

    g

    i s s o m e - e x p r e s s i o n c o n t a i n i n g f . T o o b t a i n a n f s o t h a t t h i s

    e q u a t i o n h o l d s d e n e :

    L E T f = Y ( f x

    1

    : : : x

    n

    :

    g

    f

    g

    )

    T h e f a c t t h a t t h e e q u a t i o n i s s a t i s e d c a n b e s h o w n a s f o l l o w s :

    f x

    1

    : : : x

    n

    = Y ( f x

    1

    : : : x

    n

    :

    g

    f

    g

    ) x

    1

    : : : x

    n

    ( d e n i t i o n o f f )

    = ( f x

    1

    : : : x

    n

    :

    g

    f

    g

    ) ( Y ( f x

    1

    : : : x

    n

    :

    g

    f

    g

    ) ) x

    1

    : : : x

    n

    ( x e d - p o i n t p r o p e r t y )

    = ( f x

    1

    : : : x

    n

    :

    g

    f

    g

    ) f x

    1

    : : : x

    n

    ( d e n i t i o n o f f )

    =

    g

    f

    g

    ( - c o n v e r s i o n )

    E x e r c i s e 2 1

    C o n s t r u c t a - e x p r e s s i o n e q s u c h t h a t

    e q m n = ( i s z e r o m ! i s z e r o n j

    ( i s z e r o n ! f a l s e j e q ( p r e m ) ( p r e n ) ) )

    2

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    27/104

    2 2 C h a p t e r 2 . R e p r e s e n t i n g T h i n g s i n t h e - c a l c u l u s

    E x e r c i s e 2 2

    S h o w t h a t i f Y

    1

    i s d e n e d b y :

    L E T Y

    1

    = Y ( y f : f ( y f ) )

    t h e n Y

    1

    i s a x e d - p o i n t o p e r a t o r , i . e . f o r a n y E :

    Y

    1

    E = E ( Y

    1

    E )

    2

    T h e x e d - p o i n t o p e r a t o r i n t h e n e x t e x e r c i s e i s d u e t o T u r i n g ( B a r e n d r e g t 2 ] , p a g e

    1 3 2 ) .

    E x e r c i s e 2 3

    S h o w t h a t ( x y : y ( x x y ) ) ( x y : y ( x x y ) ) i s a x e d - p o i n t o p e r a t o r . 2

    T h e n e x t e x e r c i s e a l s o c o m e s f r o m B a r e n d r e g t ' s b o o k , w h e r e i t i s a t t r i b u t e d t o K l o p .

    E x e r c i s e 2 4

    S h o w t h a t Y

    2

    i s a x e d - p o i n t o p e r a t o r , w h e r e :

    L E T $ = a b c d e f g h i j k l m n o p q s t u v w x y z r :

    r ( t h i s i s a f i x e d p o i n t c o m b i n a t o r )

    L E T Y

    2

    = $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

    2

    E x e r c i s e 2 5

    I s i t t h e c a s e t h a t Y f ; ! f ( Y f ) ? I f s o p r o v e i t i f n o t n d a - e x p r e s s i o n

    b

    Y

    s u c h t h a t

    b

    Y f ; ! f (

    b

    Y f ) . 2

    I n t h e p u r e - c a l c u l u s a s d e n e d o n p a g e 1 , - e x p r e s s i o n s c o u l d o n l y b e a p p l i e d t o

    a s i n g l e a r g u m e n t h o w e v e r , t h i s a r g u m e n t c o u l d b e a t u p l e ( s e e p a g e 1 6 ) . T h u s o n e

    c a n w r i t e :

    E ( E

    1

    : : : E

    n

    )

    w h i c h a c t u a l l y a b b r e v i a t e s :

    E ( E

    1

    ( E

    2

    ( ( E

    n ; 1

    E

    n

    ) ) ) )

    F o r e x a m p l e , E ( E

    1

    E

    2

    ) a b b r e v i a t e s E ( f : f E

    1

    E

    2

    ) .

    2 . 5 F u n c t i o n s w i t h s e v e r a l a r g u m e n t s

    I n c o n v e n t i o n a l m a t h e m a t i c a l u s a g e , t h e a p p l i c a t i o n o f a n n - a r g u m e n t f u n c t i o n f

    t o a r g u m e n t s x

    1

    , : : : , x

    n

    w o u l d b e w r i t t e n a s f ( x

    1

    : : : x

    n

    ) . T h e r e a r e t w o w a y s o f

    r e p r e s e n t i n g s u c h a p p l i c a t i o n s i n t h e - c a l c u l u s :

    ( i ) a s ( f x

    1

    : : : x

    n

    ) , o r

    ( i i ) a s t h e a p p l i c a t i o n o f f t o a n n - t u p l e ( x

    1

    : : : x

    n

    ) .

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    28/104

    2 . 5 . F u n c t i o n s w i t h s e v e r a l a r g u m e n t s 2 3

    I n c a s e ( i ) , f e x p e c t s i t s a r g u m e n t s ` o n e a t a t i m e ' a n d i s s a i d t o b e c u r r i e d a f t e r a

    l o g i c i a n c a l l e d C u r r y ( t h e i d e a o f c u r r y i n g w a s a c t u a l l y i n v e n t e d b y S c h o n n k e l 3 1 ] ) .

    T h e f u n c t i o n s a n d , o r a n d a d d d e n e d e a r l i e r w e r e a l l c u r r i e d . O n e a d v a n t a g e o f

    c u r r i e d f u n c t i o n s i s t h a t t h e y c a n b e ` p a r t i a l l y a p p l i e d ' f o r e x a m p l e , a d d 1 i s t h e

    r e s u l t o f p a r t i a l l y a p p l y i n g a d d t o 1 a n d d e n o t e s t h e f u n c t i o n n 7! n + 1 .

    A l t h o u g h i t i s o f t e n c o n v e n i e n t t o r e p r e s e n t n - a r g u m e n t f u n c t i o n s a s c u r r i e d , i t i s

    a l s o u s e f u l t o b e a b l e t o r e p r e s e n t t h e m , a s i n c a s e ( i i ) a b o v e , b y - e x p r e s s i o n s

    e x p e c t i n g a s i n g l e n - t u p l e a r g u m e n t . F o r e x a m p l e , i n s t e a d o f r e p r e s e n t i n g + a n d

    b y - e x p r e s s i o n s a d d a n d m u l t s u c h t h a t

    a d d m n = m + n

    m u l t m n = m n

    i t m i g h t b e m o r e c o n v e n i e n t t o r e p r e s e n t t h e m b y f u n c t i o n s , s u m a n d p r o d s a y ,

    s u c h t h a t

    s u m ( m n ) = m + n

    p r o d ( m n ) = m n

    T h i s i s n e a r e r t o c o n v e n t i o n a l m a t h e m a t i c a l u s a g e a n d h a s a p p l i c a t i o n s t h a t w i l l

    b e e n c o u n t e r e d l a t e r . O n e m i g h t s a y t h a t s u m a n d p r o d a r e u n c u r r i e d v e r s i o n s o f

    a d d a n d m u l t r e s p e c t i v e l y .

    D e n e :

    L E T c u r r y = f x

    1

    x

    2

    : f ( x

    1

    x

    2

    )

    L E T u n c u r r y = f p : f ( f s t p ) ( s n d p )

    t h e n d e n i n g

    s u m = u n c u r r y a d d

    p r o d = u n c u r r y m u l t

    r e s u l t s i n s u m a n d p r o d h a v i n g t h e d e s i r e d p r o p e r t i e s f o r e x a m p l e :

    s u m ( m n ) = u n c u r r y a d d ( m n )

    = ( f p : f ( f s t p ) ( s n d p ) ) a d d ( m n )

    = a d d ( f s t ( m n ) ) ( s n d ( m n ) )

    = a d d m n

    = m + n

    E x e r c i s e 2 6

    S h o w t h a t f o r a n y E :

    c u r r y ( u n c u r r y E ) = E

    u n c u r r y ( c u r r y E ) = E

    h e n c e s h o w t h a t :

    a d d = c u r r y s u m

    m u l t = c u r r y p r o d

    2

    W e c a n d e n e n - a r y f u n c t i o n s f o r c u r r y i n g a n d u n c u r r y i n g . F o r n > 0 d e n e :

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    29/104

    2 4 C h a p t e r 2 . R e p r e s e n t i n g T h i n g s i n t h e - c a l c u l u s

    L E T c u r r y

    n

    = f x

    1

    x

    n

    : f ( x

    1

    : : : x

    n

    )

    L E T u n c u r r y

    n

    = f p : f ( p

    n

    # 1 ) ( p

    n

    # n )

    I f E r e p r e s e n t s a f u n c t i o n e x p e c t i n g a n n - t u p l e a r g u m e n t , t h e n c u r r y

    n

    E r e p r e s e n t s

    t h e c u r r i e d f u n c t i o n w h i c h t a k e s i t s a r g u m e n t s o n e a t a t i m e . I f E r e p r e s e n t s a

    c u r r i e d f u n c t i o n o f n a r g u m e n t s , t h e n u n c u r r y

    n

    E r e p r e s e n t s t h e u n c u r r i e d v e r s i o n

    w h i c h e x p e c t s a s i n g l e n - t u p l e a s a r g u m e n t .

    E x e r c i s e 2 7

    S h o w t h a t :

    ( i ) c u r r y

    n

    ( u n c u r r y

    n

    E ) = E

    ( i i ) u n c u r r y

    n

    ( c u r r y

    n

    E ) = E

    2

    E x e r c i s e 2 8

    D e v i s e - e x p r e s s i o n s E

    n

    1

    a n d E

    n

    2

    b u i l t o u t o f c u r r y a n d u n c u r r y s u c h t h a t

    c u r r y

    n

    = E

    n

    1

    a n d u n c u r r y

    n

    = E

    n

    2

    . 2

    T h e f o l l o w i n g n o t a t i o n p r o v i d e s a c o n v e n i e n t w a y t o w r i t e - e x p r e s s i o n s w h i c h

    e x p e c t t u p l e s a s a r g u m e n t s .

    G e n e r a l i z e d - a b s t r a c t i o n s

    L E T ( V

    1

    : : : V

    n

    ) : E = u n c u r r y

    n

    ( V

    1

    : : : V

    n

    : E )

    E x a m p l e : ( x y ) : m u l t x y a b b r e v i a t e s :

    u n c u r r y

    2

    ( x y : m u l t x y ) = ( f p : f ( p

    2

    # 1 ) ( p

    2

    # 2 ) ) ( x y : m u l t x y )

    = ( f p : f ( f s t p ) ( s n d p ) ) ( x y : m u l t x y )

    = p : m u l t ( f s t p ) ( s n d p )

    T h u s :

    ( ( x y ) : m u l t x y ) ( E

    1

    E

    2

    ) = ( p : m u l t ( f s t p ) ( s n d p ) ) ( E

    1

    E

    2

    )

    = m u l t ( f s t ( E

    1

    E

    2

    ) ) ( s n d ( E

    1

    E

    2

    ) )

    = m u l t E

    1

    E

    2

    2

    T h i s e x a m p l e i l l u s t r a t e s t h e r u l e o f g e n e r a l i z e d - c o n v e r s i o n i n t h e b o x b e l o w . T h i s

    r u l e c a n b e d e r i v e d f r o m o r d i n a r y - c o n v e r s i o n a n d t h e d e n i t i o n s o f t u p l e s a n d

    g e n e r a l i z e d - a b s t r a c t i o n s . T h e i d e a i s t h a t a t u p l e o f a r g u m e n t s i s p a s s e d t o e a c h

    a r g u m e n t p o s i t i o n i n t h e b o d y o f t h e g e n e r a l i z e d a b s t r a c t i o n t h e n e a c h i n d i v i d u a l

    a r g u m e n t c a n b e e x t r a c t e d f r o m t h e t u p l e w i t h o u t a e c t i n g t h e o t h e r s .

  • 8/8/2019 221707 Introduction to Functional Programming Lambda Calculus

    30/104

    2 . 6 . M u t u a l r e c u r s i o n 2 5

    G e n e r a l i z e d - c o n v e r s i o n

    ( ( V

    1

    : : : V

    n

    ) : E ) ( E

    1

    : : : E

    n

    ) = E E

    1

    : : : E

    n

    = V

    1

    : : : V

    n

    ]

    w h e r e E E

    1

    : : : E

    n

    = V

    1

    : : : V

    n

    ] i s t h e s i m u l t a n e o u s s u b s t i t u t i o n o f E

    1

    : : : E

    n

    f o r V

    1

    : : : V

    n

    r e s p e c t i v e l y a n d n o n e o f t h e s e v a r i a b l e s o c c u r f r e e i n a n y o f

    E

    1

    : : : E

    n

    .

    I t i s c o n v e n i e n t t o e x t e n d t h e n o t a t i o n V

    1

    V

    2

    : : : V

    n

    : E d e s c r i b e d o n p a g e 3 s o

    t h a t e a c h V

    i

    c a n e i t h e r b e a n i d e n t i e r o r a t u p l e o f i d e n t i e r s . T h e m e a n i n g o f

    V

    1

    V

    2

    : : : V

    n

    : E i s s t i l l V

    1

    : ( V

    2

    : ( ( V

    n

    : E ) ) ) , b u t n o w i f V

    i

    i s a t u p l e o f

    i d e n t i e r s t h e n t h e e x p r e s s i o n i s a g e n e r a l i z e d a b s t r a c t i o n .

    E x a m p l e : f ( x y ) : f x y m e a n s f : ( ( x y ) : f x y ) w h i c h i n t u r n m e a n s

    f : u n c u r r y ( x y : f x y ) w h i c h e q u a l s f : ( p : f ( f s t p ) ( s n d p ) ) . 2

    E x e r c i s e 2 9

    S h o w t h a t i f t h e o n l y f r e e v a r i a b l e s i n E a r e x

    1