Asymptotic Improvement of Computations over Free Monads Janis - - PowerPoint PPT Presentation

asymptotic improvement of computations over free monads
SMART_READER_LITE
LIVE PREVIEW

Asymptotic Improvement of Computations over Free Monads Janis - - PowerPoint PPT Presentation

Asymptotic Improvement of Computations over Free Monads Janis Voigtl ander Technische Universit at Dresden MPC08 Monads for IO in Haskell Program: echo :: IO () echo = do c getChar when ( c = ) $ do putChar c echo


slide-1
SLIDE 1

Asymptotic Improvement of Computations over Free Monads

Janis Voigtl¨ ander

Technische Universit¨ at Dresden

MPC’08

slide-2
SLIDE 2

Monads for IO in Haskell

Program: echo :: IO () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo

2

slide-3
SLIDE 3

Monads for IO in Haskell

Program: echo :: IO () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Behaviour: stdin : stdout :

2

slide-4
SLIDE 4

Monads for IO in Haskell

Program: echo :: IO () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Behaviour: stdin : a stdout : a

2

slide-5
SLIDE 5

Monads for IO in Haskell

Program: echo :: IO () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Behaviour: stdin : a b stdout : a b

2

slide-6
SLIDE 6

Monads for IO in Haskell

Program: echo :: IO () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Behaviour: stdin : a b c stdout : a b c

2

slide-7
SLIDE 7

Monads for IO in Haskell

Program: echo :: IO () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Behaviour: stdin : a b c ∗ stdout : a b c

2

slide-8
SLIDE 8

Monads for IO in Haskell

Program: echo :: IO () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Behaviour: stdin : a b c ∗ stdout : a b c

2

slide-9
SLIDE 9

Testing IO Programs: IOSpec [Swierstra & Altenkirch, 07]

Program: echo :: IOSpec Teletype () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo

3

slide-10
SLIDE 10

Testing IO Programs: IOSpec [Swierstra & Altenkirch, 07]

Program: echo :: IOSpec Teletype () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Testing: > run (evalIOSpec echo singleThreaded) “abc∗”

3

slide-11
SLIDE 11

Testing IO Programs: IOSpec [Swierstra & Altenkirch, 07]

Program: echo :: IOSpec Teletype () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Testing: > run (evalIOSpec echo singleThreaded) “abc∗” Read (Print ‘a’ (Read (Print ‘b’ (Read (Print ‘c’ (Read (Finish ())))))))

3

slide-12
SLIDE 12

Testing IO Programs: IOSpec [Swierstra & Altenkirch, 07]

Program: echo :: IOSpec Teletype () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Testing: prop cs = run (evalIOSpec echo singleThreaded) (cs + + “∗”) ≡ copy cs where copy (c : cs) = Read (Print c (copy cs)) copy [ ] = Read (Finish ())

3

slide-13
SLIDE 13

Testing IO Programs: IOSpec [Swierstra & Altenkirch, 07]

Program: echo :: IOSpec Teletype () echo = do c ← getChar when (c = ‘∗’) $ do putChar c echo Testing: prop cs = run (evalIOSpec echo singleThreaded) (cs + + “∗”) ≡ copy cs where copy (c : cs) = Read (Print c (copy cs)) copy [ ] = Read (Finish ()) > quickCheck prop OK, passed 100 tests.

3

slide-14
SLIDE 14

A Slight Variation of the Example

Program: revEcho :: IO () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c

4

slide-15
SLIDE 15

A Slight Variation of the Example

Program: revEcho :: IO () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Behaviour: stdin : stdout :

4

slide-16
SLIDE 16

A Slight Variation of the Example

Program: revEcho :: IO () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Behaviour: stdin : a stdout :

4

slide-17
SLIDE 17

A Slight Variation of the Example

Program: revEcho :: IO () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Behaviour: stdin : a b stdout :

4

slide-18
SLIDE 18

A Slight Variation of the Example

Program: revEcho :: IO () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Behaviour: stdin : a b c stdout :

4

slide-19
SLIDE 19

A Slight Variation of the Example

Program: revEcho :: IO () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Behaviour: stdin : a b c ∗ stdout : c b a

4

slide-20
SLIDE 20

A Slight Variation of the Example

Program: revEcho :: IO () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Behaviour: stdin : a b c ∗ stdout : c b a

4

slide-21
SLIDE 21

A Slight Variation of the Example

Program: revEcho :: IOSpec Teletype () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Testing: > run (evalIOSpec revEcho singleThreaded) “abc∗”

4

slide-22
SLIDE 22

A Slight Variation of the Example

Program: revEcho :: IOSpec Teletype () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Testing: > run (evalIOSpec revEcho singleThreaded) “abc∗” Read (Read (Read (Read (Print ‘c’ (Print ‘b’ (Print ‘a’ (Finish ())))))))

4

slide-23
SLIDE 23

A Slight Variation of the Example

Program: revEcho :: IOSpec Teletype () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Testing: prop cs = run (evalIOSpec revEcho singleThreaded) (cs + + “∗”) ≡ mirror cs (Finish ()) where mirror (c : cs) acc = Read (mirror cs (Print c acc)) mirror [ ] acc = Read acc

4

slide-24
SLIDE 24

A Slight Variation of the Example

Program: revEcho :: IOSpec Teletype () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Testing: prop cs = run (evalIOSpec revEcho singleThreaded) (cs + + “∗”) ≡ mirror cs (Finish ()) where mirror (c : cs) acc = Read (mirror cs (Print c acc)) mirror [ ] acc = Read acc > quickCheck prop OK, passed 100 tests.

4

slide-25
SLIDE 25

A Slight Variation of the Example: Ouch!

Program: revEcho :: IOSpec Teletype () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c Testing: prop cs = run (evalIOSpec revEcho singleThreaded) (cs + + “∗”) ≡ mirror cs (Finish ()) where mirror (c : cs) acc = Read (mirror cs (Print c acc)) mirror [ ] acc = Read acc > quickCheck prop OK, passed 100 tests. But each test takes quadratic time!

4

slide-26
SLIDE 26

But Why?

Let’s take a closer look at “IOSpec Teletype”, henceforth “IOtt”.

5

slide-27
SLIDE 27

But Why?

Let’s take a closer look at “IOSpec Teletype”, henceforth “IOtt”. data IOtt α = GetChar (Char → IOtt α) | PutChar Char (IOtt α) | Return α

5

slide-28
SLIDE 28

But Why?

Let’s take a closer look at “IOSpec Teletype”, henceforth “IOtt”. data IOtt α = GetChar (Char → IOtt α) | PutChar Char (IOtt α) | Return α instance Monad IOtt where · · ·

5

slide-29
SLIDE 29

But Why?

Let’s take a closer look at “IOSpec Teletype”, henceforth “IOtt”. data IOtt α = GetChar (Char → IOtt α) | PutChar Char (IOtt α) | Return α instance Monad IOtt where · · · getChar :: IOtt Char getChar = GetChar Return putChar :: Char → IOtt () putChar c = PutChar c (Return ())

5

slide-30
SLIDE 30

But Why?

Let’s take a closer look at “IOSpec Teletype”, henceforth “IOtt”. data IOtt α = GetChar (Char → IOtt α) | PutChar Char (IOtt α) | Return α instance Monad IOtt where · · · getChar :: IOtt Char getChar = GetChar Return putChar :: Char → IOtt () putChar c = PutChar c (Return ()) run :: IOtt α → String → Output α run (GetChar f ) (c : cs) = Read (run (f c) cs) run (PutChar c p) cs = Print c (run p cs) run (Return a) cs = Finish a

5

slide-31
SLIDE 31

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c

6

slide-32
SLIDE 32

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: run revEcho “abc∗” a

6

slide-33
SLIDE 33

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: run revEcho “abc∗” a

6

slide-34
SLIDE 34

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: run GetChar f “abc∗” a

6

slide-35
SLIDE 35

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: run GetChar f “abc∗” a run (GetChar f ) (c : cs) = Read (run (f c) cs)

6

slide-36
SLIDE 36

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: Read run > > “bc∗” revEcho putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs)

6

slide-37
SLIDE 37

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: Read run > > “bc∗” revEcho putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs)

6

slide-38
SLIDE 38

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: Read run > > “bc∗” GetChar f putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs)

6

slide-39
SLIDE 39

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: Read run > > “bc∗” GetChar f putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-40
SLIDE 40

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 Read run GetChar “bc∗” λc > > f c putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-41
SLIDE 41

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 Read run GetChar “bc∗” λc > > f c putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-42
SLIDE 42

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 Read Read run > > “c∗” > > putChar ‘a’ revEcho putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-43
SLIDE 43

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 Read Read run > > “c∗” > > putChar ‘a’ revEcho putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-44
SLIDE 44

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 Read Read run > > “c∗” > > putChar ‘a’ GetChar f putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-45
SLIDE 45

But Why?

Now, revEcho desugared and with some inlining: revEcho :: IOtt () revEcho = GetChar f where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 Read Read run > > “c∗” > > putChar ‘a’ GetChar f putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-46
SLIDE 46

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 1 Read Read run > > “c∗” GetChar putChar ‘a’ λc > > f c putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-47
SLIDE 47

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 1 Read Read run > > “c∗” GetChar putChar ‘a’ λc > > f c putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-48
SLIDE 48

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 2 Read Read run GetChar “c∗” λc > > > > putChar ‘a’ f c putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-49
SLIDE 49

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 2 Read Read run GetChar “c∗” λc > > > > putChar ‘a’ f c putChar ‘b’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-50
SLIDE 50

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 2 Read Read Read run > > “∗” > > putChar ‘a’ > > putChar ‘b’ revEcho putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-51
SLIDE 51

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 2 Read Read Read run > > “∗” > > putChar ‘a’ > > putChar ‘b’ revEcho putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-52
SLIDE 52

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 2 Read Read Read run > > “∗” > > putChar ‘a’ > > putChar ‘b’ GetChar f putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-53
SLIDE 53

But Why?

· · · where f = λc → when (c = ‘∗’) $ revEcho > > putChar c An example evaluation, counting (certain) steps: 1 + 2 Read Read Read run > > “∗” > > putChar ‘a’ > > putChar ‘b’ GetChar f putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-54
SLIDE 54

But Why?

An example evaluation, counting (certain) steps: 1 + 2 + 1 Read Read Read run > > “∗” > > putChar ‘a’ GetChar putChar ‘b’ λc > > f c putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-55
SLIDE 55

But Why?

An example evaluation, counting (certain) steps: 1 + 2 + 1 Read Read Read run > > “∗” > > putChar ‘a’ GetChar putChar ‘b’ λc > > f c putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-56
SLIDE 56

But Why?

An example evaluation, counting (certain) steps: 1 + 2 + 2 Read Read Read run > > “∗” GetChar putChar ‘a’ λc > > > > putChar ‘b’ f c putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-57
SLIDE 57

But Why?

An example evaluation, counting (certain) steps: 1 + 2 + 2 Read Read Read run > > “∗” GetChar putChar ‘a’ λc > > > > putChar ‘b’ f c putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-58
SLIDE 58

But Why?

An example evaluation, counting (certain) steps: 1 + 2 + 3 Read Read Read run GetChar “∗” λc > > > > putChar ‘a’ > > putChar ‘b’ f c putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-59
SLIDE 59

But Why?

An example evaluation, counting (certain) steps: 1 + 2 + 3 Read Read Read run GetChar “∗” λc > > > > putChar ‘a’ > > putChar ‘b’ f c putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-60
SLIDE 60

But Why?

An example evaluation, counting (certain) steps: 1 + 2 + 3 Read Read Read run GetChar “∗” λc > > > > putChar ‘a’ > > putChar ‘b’ f c putChar ‘c’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) (GetChar f ) > > m = GetChar (λc → f c > > m)

6

slide-61
SLIDE 61

What to Do?

Switch type yet again: revEcho :: C () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c

7

slide-62
SLIDE 62

What to Do?

Switch type yet again: revEcho :: C () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c type C α = ∀β. (α → IOtt β) → IOtt β

7

slide-63
SLIDE 63

What to Do?

Switch type yet again: revEcho :: C () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c type C α = ∀β. (α → IOtt β) → IOtt β instance Monad C where · · ·

7

slide-64
SLIDE 64

What to Do?

Switch type yet again: revEcho :: C () revEcho = do c ← getChar when (c = ‘∗’) $ do revEcho putChar c type C α = ∀β. (α → IOtt β) → IOtt β instance Monad C where · · · getChar :: C Char getChar = λh → GetChar (λc → h c) putChar :: Char → C () putChar c = λh → PutChar c (h ())

7

slide-65
SLIDE 65

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h

8

slide-66
SLIDE 66

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: run $ “abc∗” revEcho Return a

8

slide-67
SLIDE 67

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: run $ “abc∗” revEcho Return a run (GetChar f ) (c : cs) = Read (run (f c) cs)

8

slide-68
SLIDE 68

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: Read run $ “bc∗” > > Return revEcho putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs)

8

slide-69
SLIDE 69

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: Read run $ “bc∗” > > Return revEcho putChar ‘a’ a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-70
SLIDE 70

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: Read run $ “bc∗” revEcho λ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-71
SLIDE 71

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: Read run $ “bc∗” revEcho λ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-72
SLIDE 72

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: Read Read run $ “c∗” > > λ revEcho putChar ‘b’ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-73
SLIDE 73

And Then?

Now, revEcho desugared and with some inlining: revEcho :: C () revEcho = λh → GetChar f where f = λc → (when (c = ‘∗’) $ revEcho > > putChar c) $ h An example evaluation: Read Read run $ “c∗” > > λ revEcho putChar ‘b’ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-74
SLIDE 74

And Then?

An example evaluation: Read Read run $ “c∗” revEcho λ $ putChar ‘b’ λ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-75
SLIDE 75

And Then?

An example evaluation: Read Read run $ “c∗” revEcho λ $ putChar ‘b’ λ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-76
SLIDE 76

And Then?

An example evaluation: Read Read Read run $ “∗” > > λ revEcho putChar ‘c’ $ putChar ‘b’ λ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-77
SLIDE 77

And Then?

An example evaluation: Read Read Read run $ “∗” > > λ revEcho putChar ‘c’ $ putChar ‘b’ λ $ putChar ‘a’ Return a run (GetChar f ) (c : cs) = Read (run (f c) cs) p > > m = λh → p $ (λ → m $ h)

8

slide-78
SLIDE 78

And Then?

An example evaluation: Read Read Read run $ “∗” revEcho λ $ putChar ‘c’ λ $ putChar ‘b’ λ $ putChar ‘a’ Return a

8

slide-79
SLIDE 79

And Then?

An example evaluation: Read Read Read run $ “∗” revEcho λ $ putChar ‘c’ λ $ putChar ‘b’ λ $ putChar ‘a’ Return a

8

slide-80
SLIDE 80

And Then?

An example evaluation: Read Read Read run $ “∗” revEcho λ $ putChar ‘c’ λ $ putChar ‘b’ λ $ putChar ‘a’ Return a Overall, linear time!

8

slide-81
SLIDE 81

More General Considerations

◮ Was the transformation semantically correct?

9

slide-82
SLIDE 82

More General Considerations

◮ Was the transformation semantically correct? ◮ Is something similar possible for other data types?

9

slide-83
SLIDE 83

More General Considerations

◮ Was the transformation semantically correct? ◮ Is something similar possible for other data types? ◮ How to provide the transformation to the programmer?

9

slide-84
SLIDE 84

More General Considerations

◮ Was the transformation semantically correct?

⇒ program calculation, monad laws

◮ Is something similar possible for other data types? ◮ How to provide the transformation to the programmer?

9

slide-85
SLIDE 85

More General Considerations

◮ Was the transformation semantically correct?

⇒ program calculation, monad laws

◮ Is something similar possible for other data types?

⇒ generic development for arbitrary free monads

◮ How to provide the transformation to the programmer?

9

slide-86
SLIDE 86

More General Considerations

◮ Was the transformation semantically correct?

⇒ program calculation, monad laws

◮ Is something similar possible for other data types?

⇒ generic development for arbitrary free monads

◮ How to provide the transformation to the programmer?

⇒ type constructor classes, rank-2 types

9

slide-87
SLIDE 87

More General Considerations

◮ Was the transformation semantically correct?

⇒ program calculation, monad laws

◮ Is something similar possible for other data types?

⇒ generic development for arbitrary free monads

◮ How to provide the transformation to the programmer?

⇒ type constructor classes, rank-2 types Next:

◮ even more monads?

9

slide-88
SLIDE 88

More General Considerations

◮ Was the transformation semantically correct?

⇒ program calculation, monad laws

◮ Is something similar possible for other data types?

⇒ generic development for arbitrary free monads

◮ How to provide the transformation to the programmer?

⇒ type constructor classes, rank-2 types Next:

◮ even more monads? ◮ dual concepts?

9

slide-89
SLIDE 89

More General Considerations

◮ Was the transformation semantically correct?

⇒ program calculation, monad laws

◮ Is something similar possible for other data types?

⇒ generic development for arbitrary free monads

◮ How to provide the transformation to the programmer?

⇒ type constructor classes, rank-2 types Next:

◮ even more monads? ◮ dual concepts? ◮ . . . ?

9

slide-90
SLIDE 90

References

  • K. Claessen and R.J.M. Hughes.

QuickCheck: A lightweight tool for random testing of Haskell programs. In International Conference on Functional Programming, Proceedings, pages 268–279. ACM Press, 2000.

  • W. Swierstra and T. Altenkirch.

Beauty in the beast: A functional semantics for the awkward squad. In Haskell Workshop, Proceedings, pages 25–36. ACM Press, 2007.

  • W. Swierstra.

Data types ` a la carte. Journal of Functional Programming, 18(4):423–436, 2008.

10