2012 05-08-lambda-draft

119
Implementing Untyped λ-Calculus Implementing Untyped λ-Calculus in Haskell slide: Jaiyalas Lin Jen-Shin (godfat) 1 / 94

Upload: lin-jen-shin

Post on 12-May-2015

637 views

Category:

Spiritual


0 download

TRANSCRIPT

Page 1: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Implementing Untyped λ-Calculusin Haskell

slide: JaiyalasLin Jen-Shin (godfat) 1 / 94

Page 2: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

godfat.org/slide/2012-05-08-lambda-draft.pdf

2 / 94

Page 3: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Who Am I?

Who Am I?

3 / 94

Page 4: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Who Am I?

What I learned?

▶ 2007∼present: (learning) Haskell▶ 2006∼present: Ruby▶ 2005∼2008: C++▶ 2001∼2004: C

4 / 94

Page 5: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Who Am I?

What I worked?

▶ roodo.com▶ cardinalblue.com

5 / 94

Page 6: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Who Am I?

Where you can find me

▶ github.com/godfat▶ twitter.com/godfat▶ profiles.google.com/godfat

6 / 94

Page 7: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Who Am I?

How I started to learn Haskell

▶ PLT at ssh://[email protected]

▶ IIS at Sinica▶ FLOLAC

7 / 94

Page 8: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Who Am I?

How I started to learn Haskell

▶ PLT at ssh://[email protected]▶ IIS at Sinica

▶ FLOLAC

7 / 94

Page 9: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Who Am I?

How I started to learn Haskell

▶ PLT at ssh://[email protected]▶ IIS at Sinica▶ FLOLAC

7 / 94

Page 10: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Table of Contents

▶ What can Haskell do?▶ What is λ-Calculus?▶ Why Implement λ-Calculus?▶ Let's Implement λ-Calculus▶ Questions?▶ References

8 / 94

Page 11: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

dummy

▶ What can Haskell do?▶ What is λ-Calculus?▶ Why Implement λ-Calculus?▶ Let's Implement λ-Calculus▶ Questions?▶ References

9 / 94

Page 12: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Defined in 1990

10 / 94

Page 13: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Successor ofMiranda from

1985

11 / 94

Page 14: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Haskell 98

12 / 94

Page 15: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Haskell 2010

13 / 94

Page 16: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

GHC (Glasgow Haskell Compiler)

▶ STM (Software Transactional Memory)

▶ Template Haskell▶ GADT (Generalized Algebraic Data Type)

14 / 94

Page 17: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

GHC (Glasgow Haskell Compiler)

▶ STM (Software Transactional Memory)▶ Template Haskell

▶ GADT (Generalized Algebraic Data Type)

14 / 94

Page 18: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

GHC (Glasgow Haskell Compiler)

▶ STM (Software Transactional Memory)▶ Template Haskell▶ GADT (Generalized Algebraic Data Type)

14 / 94

Page 19: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Notable Projects

▶ Audrey Tang's (唐鳳) Pugs

▶ xmonad▶ Darcs

15 / 94

Page 20: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Notable Projects

▶ Audrey Tang's (唐鳳) Pugs▶ xmonad

▶ Darcs

15 / 94

Page 21: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Notable Projects

▶ Audrey Tang's (唐鳳) Pugs▶ xmonad▶ Darcs

15 / 94

Page 22: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Parallelism vs Concurrency?

▶ par-tutorial

▶ Parallelism ̸= Concurrency▶ Parallelism is not concurrency

16 / 94

Page 23: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Parallelism vs Concurrency?

▶ par-tutorial▶ Parallelism ̸= Concurrency

▶ Parallelism is not concurrency

16 / 94

Page 24: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What can Haskell do?

Parallelism vs Concurrency?

▶ par-tutorial▶ Parallelism ̸= Concurrency▶ Parallelism is not concurrency

16 / 94

Page 25: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

dummy

▶ What can Haskell do?▶ What is λ-Calculus?▶ Why Implement λ-Calculus?▶ Let's Implement λ-Calculus▶ Questions?▶ References

17 / 94

Page 26: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

What is λ-Calculus?

An important formal systemfor functional programming

18 / 94

Page 27: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Machine

19 / 94

Page 28: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

by Alan Turingin 1936

20 / 94

Page 29: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

21 / 94

Page 30: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Machine

▶ Infinite tape which stores symbols (memory)

▶ Finite action table which represents(state, symbol) → action (program)

▶ Robotic arm (CPU with a register which storescurrent state)

■ Read the symbol on the tape at current position■ Write a symbol on the tape at current position■ Move the tape left or right

22 / 94

Page 31: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Machine

▶ Infinite tape which stores symbols (memory)▶ Finite action table which represents(state, symbol) → action (program)

▶ Robotic arm (CPU with a register which storescurrent state)

■ Read the symbol on the tape at current position■ Write a symbol on the tape at current position■ Move the tape left or right

22 / 94

Page 32: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Machine

▶ Infinite tape which stores symbols (memory)▶ Finite action table which represents(state, symbol) → action (program)

▶ Robotic arm (CPU with a register which storescurrent state)

■ Read the symbol on the tape at current position■ Write a symbol on the tape at current position■ Move the tape left or right

22 / 94

Page 33: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Machine

▶ Infinite tape which stores symbols (memory)▶ Finite action table which represents(state, symbol) → action (program)

▶ Robotic arm (CPU with a register which storescurrent state)

■ Read the symbol on the tape at current position

■ Write a symbol on the tape at current position■ Move the tape left or right

22 / 94

Page 34: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Machine

▶ Infinite tape which stores symbols (memory)▶ Finite action table which represents(state, symbol) → action (program)

▶ Robotic arm (CPU with a register which storescurrent state)

■ Read the symbol on the tape at current position■ Write a symbol on the tape at current position

■ Move the tape left or right

22 / 94

Page 35: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Machine

▶ Infinite tape which stores symbols (memory)▶ Finite action table which represents(state, symbol) → action (program)

▶ Robotic arm (CPU with a register which storescurrent state)

■ Read the symbol on the tape at current position■ Write a symbol on the tape at current position■ Move the tape left or right

22 / 94

Page 36: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Turing Machine

Turing Complete

23 / 94

Page 37: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

So what isλ-calculus?

24 / 94

Page 38: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Alligator Eggs!

http://worrydream.com/AlligatorEggs/

25 / 94

Page 39: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

by Alonzo Churchin 1930s

26 / 94

Page 40: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Also Turingcomplete

27 / 94

Page 41: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

but why?

28 / 94

Page 42: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

Defineλ-Complete

29 / 94

Page 43: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

Whateverλ-calculus can do

30 / 94

Page 44: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

Implement Turing machine in λ-calculus

31 / 94

Page 45: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

λ-Complete

▶ So λ-calculus can do anything TM can do

▶ Plus what λ-calculus can do without a TM▶ Thus λ-calculus is Turing complete

32 / 94

Page 46: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

λ-Complete

▶ So λ-calculus can do anything TM can do▶ Plus what λ-calculus can do without a TM

▶ Thus λ-calculus is Turing complete

32 / 94

Page 47: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

λ-Complete

▶ So λ-calculus can do anything TM can do▶ Plus what λ-calculus can do without a TM▶ Thus λ-calculus is Turing complete

32 / 94

Page 48: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

On the otherhand...

33 / 94

Page 49: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

Implement λ-calculus in Turing machine

34 / 94

Page 50: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

So Turingmachine is alsoλ-complete

35 / 94

Page 51: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

λ-Complete

They have the same computability

36 / 94

Page 52: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

What exactly is λ-calculus?

▶ (λx. x+1)

▶ (λx. x+1) 1▶ = (1+1)▶ = 2

37 / 94

Page 53: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

What exactly is λ-calculus?

▶ (λx. x+1)▶ (λx. x+1) 1

▶ = (1+1)▶ = 2

37 / 94

Page 54: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

What exactly is λ-calculus?

▶ (λx. x+1)▶ (λx. x+1) 1▶ = (1+1)

▶ = 2

37 / 94

Page 55: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

What exactly is λ-calculus?

▶ (λx. x+1)▶ (λx. x+1) 1▶ = (1+1)▶ = 2

37 / 94

Page 56: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Does it look like Lisp?

(λf.(λx.f (x x)) (λx.f (x x)))

38 / 94

Page 57: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Church Encoding

Church Encoding

39 / 94

Page 58: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Church Encoding

Natural Numbers

▶ 0 ≡ λf.λx. x▶ 1 ≡ λf.λx. f x▶ ...▶ n ≡ λf.λx. fn x

40 / 94

Page 59: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Church Encoding

Computation with Natural Numbers

▶ succ ≡ λn.λf.λx. f (n f x)▶ plus ≡ λm.λn.λf.λx. m f (n f x)

41 / 94

Page 60: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Church Encoding

Booleans

▶ true ≡ λa.λb. a▶ false ≡ λa.λb. b

42 / 94

Page 61: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Church Encoding

Computation with Booleans

▶ and ≡ λm.λn. m n m▶ or ≡ λm.λn. m m n▶ not ≡ λm.λa.λb. m b a▶ if ≡ λm.λa.λb. m a b

43 / 94

Page 62: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Recursion?No problems

44 / 94

Page 63: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Y combinator

Y combinator

▶ Discovered by Haskell Curry▶ y = (λf.(λx.f (x x)) (λx.f (x x)))

▶ y f = (λf.(λx.f (x x)) (λx.f (x x))) f▶ y f = f (y f)

45 / 94

Page 64: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Y combinator

Y combinator

▶ Discovered by Haskell Curry▶ y = (λf.(λx.f (x x)) (λx.f (x x)))▶ y f = (λf.(λx.f (x x)) (λx.f (x x))) f

▶ y f = f (y f)

45 / 94

Page 65: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Y combinator

Y combinator

▶ Discovered by Haskell Curry▶ y = (λf.(λx.f (x x)) (λx.f (x x)))▶ y f = (λf.(λx.f (x x)) (λx.f (x x))) f▶ y f = f (y f)

45 / 94

Page 66: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Y combinator

Paul Graham,I know what isY combinator!

46 / 94

Page 67: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Fixed Point Combinator

Fixed Point Combinator

▶ Strict languages need some delay▶ Z = (λx.f (λv.((x x) v))) (λx.f (λv.((x x) v)))

▶ Discovered by Alan Turing▶ Θ = (λx.λy. (y (x x y))) (λx.λy. (y (x x y)))

47 / 94

Page 68: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

What is λ-Calculus?

Fixed Point Combinator

Fixed Point Combinator

▶ Constructed by Jan Willem Klop▶ Yk = (L L L L L L L L L L L . . .)

where L = λabcdefghijklmnopqstuvwxyzr. (r (thisisafixedpointcombinator))

48 / 94

Page 69: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ What can Haskell do?▶ What is λ-Calculus?▶ Why Implement λ-Calculus?▶ Let's Implement λ-Calculus▶ Questions?▶ References

49 / 94

Page 70: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ My interest in researchingprogramming languages

▶ Implementing λ-calculus in Haskellcould teach us a lot inprogramming languages

▶ It's my most influential Haskell exercise

50 / 94

Page 71: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ My interest in researchingprogramming languages

▶ Implementing λ-calculus in Haskellcould teach us a lot inprogramming languages

▶ It's my most influential Haskell exercise

50 / 94

Page 72: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ My interest in researchingprogramming languages

▶ Implementing λ-calculus in Haskellcould teach us a lot inprogramming languages

▶ It's my most influential Haskell exercise

50 / 94

Page 73: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

Let's Learn Haskell The Hard WayYou a Haskell For Great Good!

51 / 94

Page 74: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

But before westarted...

52 / 94

Page 75: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ No parsing here

▶ Parse tree (S-expression) interpreter only▶ Haskell is also good at parsing▶ e.g. parser combinator and parsec

53 / 94

Page 76: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ No parsing here▶ Parse tree (S-expression) interpreter only

▶ Haskell is also good at parsing▶ e.g. parser combinator and parsec

53 / 94

Page 77: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ No parsing here▶ Parse tree (S-expression) interpreter only▶ Haskell is also good at parsing

▶ e.g. parser combinator and parsec

53 / 94

Page 78: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Why Implement λ-Calculus?

dummy

▶ No parsing here▶ Parse tree (S-expression) interpreter only▶ Haskell is also good at parsing▶ e.g. parser combinator and parsec

53 / 94

Page 79: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

dummy

▶ What can Haskell do?▶ What is λ-Calculus?▶ Why Implement λ-Calculus?▶ Let's Implement λ-Calculus▶ Questions?▶ References

54 / 94

Page 80: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

First Expression and evaluate (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression.evaluate :: Expression -> Integerevaluate (Literal i) = ievaluate (Plus expr0 expr1) =evaluate expr0 + evaluate expr1

.test0 = evaluate (Literal 1)test1 = evaluate (Plus (Literal 1) (Literal 2))test2 = evaluate (Plus (Plus (Literal 1)

(Literal 2))(Literal 3))

55 / 94

Page 81: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

First Expression and evaluate (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression.evaluate :: Expression -> Integerevaluate (Literal i) = ievaluate (Plus expr0 expr1) =evaluate expr0 + evaluate expr1

.test0 = evaluate (Literal 1)test1 = evaluate (Plus (Literal 1) (Literal 2))test2 = evaluate (Plus (Plus (Literal 1)

(Literal 2))(Literal 3))

56 / 94

Page 82: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

First Expression and evaluate (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression.evaluate :: Expression -> Integerevaluate (Literal i) = ievaluate (Plus expr0 expr1) =evaluate expr0 + evaluate expr1

.test0 = evaluate (Literal 1)test1 = evaluate (Plus (Literal 1) (Literal 2))test2 = evaluate (Plus (Plus (Literal 1)

(Literal 2))(Literal 3))

57 / 94

Page 83: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

First Expression and evaluate (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression.evaluate :: Expression -> Integerevaluate (Literal i) = ievaluate (Plus expr0 expr1) =evaluate expr0 + evaluate expr1

.test0 = evaluate (Literal 1)test1 = evaluate (Plus (Literal 1) (Literal 2))test2 = evaluate (Plus (Plus (Literal 1)

(Literal 2))(Literal 3))

58 / 94

Page 84: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Algebraic Datatypesmodule Main where.data Expression = Literal Integer

| Plus Expression Expression.evaluate :: Expression -> Integerevaluate (Literal i) = ievaluate (Plus expr0 expr1) =Think of interface and subclasses if you like OOP.test0 = evaluate (Literal 1)test1 = evaluate (Plus (Literal 1) (Literal 2))test2 = evaluate (Plus (Plus (Literal 1)

(Literal 2))(Literal 3))

59 / 94

Page 85: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Pattern Matchingmodule Main where.data Expression = Literal Integer

| Plus Expression Expression.evaluate :: Expression -> Integerevaluate (Literal i) = ievaluate (Plus expr0 expr1) =evaluate expr0 + evaluate expr1

.test0 = evaluate (Literal 1)Think of dynamic cast or instanceof with a switch if you like OOPtest2 = evaluate (Plus (Plus (Literal 1)

(Literal 2))(Literal 3))

60 / 94

Page 86: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

First Expression and evaluate (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression.evaluate :: Expression -> Integerevaluate (Literal i) = ievaluate (Plus expr0 expr1) =evaluate expr0 + evaluate expr1

.test0 = evaluate (Literal 1)test1 = evaluate (Plus (Literal 1) (Literal 2))test2 = evaluate (Plus (Plus (Literal 1)

(Literal 2))(Literal 3))

61 / 94

Page 87: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Variable and Environment (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression| Variable String

.type Environment = [(String, Integer)].evaluate :: Expression -> Environment -> Integerevaluate (Literal i) env = ievaluate (Plus expr0 expr1) env =evaluate expr0 env + evaluate expr1 env

evaluate (Variable name) env =case lookup name env of (Just i) -> i

62 / 94

Page 88: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Variable and Environment (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression| Variable String

.type Environment = [(String, Integer)].evaluate :: Expression -> Environment -> Integerevaluate (Literal i) env = ievaluate (Plus expr0 expr1) env =evaluate expr0 env + evaluate expr1 env

evaluate (Variable name) env =case lookup name env of (Just i) -> i

63 / 94

Page 89: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Variable and Environment (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression| Variable String

.type Environment = [(String, Integer)].evaluate :: Expression -> Environment -> Integerevaluate (Literal i) env = ievaluate (Plus expr0 expr1) env =evaluate expr0 env + evaluate expr1 env

evaluate (Variable name) env =case lookup name env of (Just i) -> i

64 / 94

Page 90: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Variable and Environment (source)module Main where.data Expression = Literal Integer

| Plus Expression Expression| Variable String

.type Environment = [(String, Integer)].evaluate :: Expression -> Environment -> Integerevaluate (Literal i) env = ievaluate (Plus expr0 expr1) env =evaluate expr0 env + evaluate expr1 env

evaluate (Variable name) env =case lookup name env of (Just i) -> i

65 / 94

Page 91: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Variable and Environment (source)

test0 = evaluate (Variable "var") [("var", 1)]test1 = evaluate (Plus (Variable "var") (Literal2)) [("var", 1)]

66 / 94

Page 92: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Type Aliasmodule Main where.data Expression = Literal Integer

| Plus Expression Expression| Variable Name

.type Name = Stringtype Environment = [(Name, Integer)].evaluate :: Expression -> Environment -> Integerevaluate (Literal i) env = ievaluate (Plus expr0 expr1) env =evaluate expr0 env + evaluate expr1 env

evaluate (Variable name) env =case lookup name env of (Just i) -> i

67 / 94

Page 93: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Pair of a and b

("var", 2) :: (String, Integer)(2, "var") :: (Integer, String)

68 / 94

Page 94: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

List of a

[1,2,3] :: [Integer]["a","b","c"] :: [String][("var", 1)] :: [(String, Integer)]

69 / 94

Page 95: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Curried Functions

evaluate :: Expression -> Environment -> Integer

70 / 94

Page 96: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Curried Functions

evaluate :: Expression -> (Environment -> Integer)

71 / 94

Page 97: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Uncurried Functions

evaluate :: (Expression, Environment) -> Integer

72 / 94

Page 98: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Uncurried Functions

threeArguments ::(String, String, String) -> Integer

73 / 94

Page 99: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Curried Function

threeArguments ::String -> String -> String -> Integer

74 / 94

Page 100: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Curried Function

threeArguments ::String -> (String -> String -> Integer)

75 / 94

Page 101: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Curried Function

threeArguments ::String -> (String -> (String -> Integer))

76 / 94

Page 102: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

(->) isright-associative

77 / 94

Page 103: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Partially Applied Function

filter :: (a -> Bool) -> [a] -> [a].above60 :: [a] -> [a]above60 = filter (>=60).above60 [1..65] -- [60,61,62,63,64,65]

78 / 94

Page 104: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Partially Applied Function

map :: (a -> b) -> [a] -> [b].div2 :: [Integer] -> [Integer]div2 = map (`div`2).div2 [1..5] -- [0,1,1,2,2]

79 / 94

Page 105: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Function Composition

(.) :: (b -> c) -> (a -> b) -> a -> c.above60AndDiv2 :: [Integer] -> [Integer]above60AndDiv2 = div2 . above60.above60AndDiv2 [1..65] -- [30,30,31,31,32,32]

80 / 94

Page 106: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Function Composition

compose :: (b -> c) -> (a -> b) -> a -> ccompose g f x = g (f x)

81 / 94

Page 107: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Curried functions make function composition powerful,function composition makes curried function even more useful.

82 / 94

Page 108: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

List of Partially Applied Functions

mapPlus = map (+) -- think of [(+), (+), ...].mapPlus1to5 = mapPlus [1..5]-- think of [(1+), (2+), ...].map ($1) mapPlus1to5 -- [2,3,4,5,6]..-- applicative functor style(+) <$> [1..5] <*> [1] -- [2,3,4,5,6]

83 / 94

Page 109: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Exception Handling

data Expression = Literal Integer| Plus Expression Expression| Variable Name

.type Name = Stringtype Environment = [(Name, Integer)].type Value = Maybe Integer.evaluate :: Expression -> Environment -> Value

84 / 94

Page 110: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Exception Handling

data Expression = Literal Integer| Plus Expression Expression| Variable Name

.type Name = Stringtype Environment = [(Name, Integer)].type Value = Maybe Integer.evaluate :: Expression -> Environment -> Valueevaluate (Literal i) env = Just ievaluate (Variable name) env = lookup name env

85 / 94

Page 111: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Exception Handling

evaluate (Plus expr0 expr1) env =let val0 = evaluate expr0 env

val1 = evaluate expr1 envin

case val0 of(Nothing) -> Nothing(Just i0) -> case val1 of

(Nothing) -> Nothing(Just i1) -> Just (i0 + i1)

86 / 94

Page 112: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Maybe Monad with do notation

evaluate (Plus expr0 expr1) env =do

val0 <- evaluate expr0 envval1 <- evaluate expr1 envreturn (val0 + val1)

87 / 94

Page 113: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Do notation underneath

evaluate (Plus expr0 expr1) env =evaluate expr0 env >>= \val0 ->

evaluate expr1 env >>= \val1 -> return (val0 +val1)

88 / 94

Page 114: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

liftM2

evaluate (Plus expr0 expr1) env =evaluate expr0 env `plus` evaluate expr1 env whereplus = liftM2 (+)

89 / 94

Page 115: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Sorry! To becontinued...

90 / 94

Page 116: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Let's Implement λ-Calculus

Peek the final work

https://github.com/godfat/sandbox/blob/master/haskell/fpug/01/last.hs

91 / 94

Page 117: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

We're hiring

cblue.tw/jobs

92 / 94

Page 118: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

Feel free to ask me questions online

▶ github.com/godfat▶ twitter.com/godfat▶ profiles.google.com/godfat

93 / 94

Page 119: 2012 05-08-lambda-draft

Implementing Untyped λ-Calculus

References

▶ To be listed...▶

94 / 94