Advanced Functional Programming - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Advanced Functional Programming

Description:

Due to the visit of Robert Giegerich, Thursday's lecture will be a guest lecture. ... Taming the duplication. fib2 :: Integer - Integer. fib2 z = f z ... – PowerPoint PPT presentation

Number of Views:169
Avg rating:3.0/5.0
Slides: 36
Provided by: david509
Category:

less

Transcript and Presenter's Notes

Title: Advanced Functional Programming


1
Advanced Functional Programming
  • Tim Sheard
  • Oregon Graduate Institute of Science Technology
  • Lecture 14 Dynamic Programming and Parsers
  • Dynamic programming
  • Memoization and lazy arrays
  • Parsing String based
  • Parsing Array based

2
Thursdays Lecture
  • Due to the visit of Robert Giegerich, Thursdays
    lecture will be a guest lecture.
  • Thursday, February 27, 2003
  • at 1100 am
  • AB401
  • Pair Algebras A ()-Lecture on Dynamic
    Programming
  • Note change in time and place from regular
    lecture!

3
Reminder Final Projects
  • This is due Thursday Feb. 27, 2003
  • Hand it to me at the Giegerich lecture, or put it
    in my mailbox by the end of the day
  • A project is a small programming exercise of your
    choice which utilizes some advanced feature of
    Haskell.
  • You must define your own project a 1 page
    description of what you will do.
  • Good project descriptions outline the task, the
    procedure used, perhaps even some of the data
    structures.
  • Good projects can lead to papers and publications.

4
Dynamic Programming
  • Consider the function
  • fib Integer -gt Integer
  • fib 0 1
  • fib 1 1
  • fib n fib (n-1) fib (n-2)
  • Maingt fib 25
  • 121393
  • (4334712 reductions, 7091332 cells, 30 garbage
    collections)
  • takes about 4 seconds on my machine!

5
Why does it take so long
fib 6
  • Consider (fib 6)

fib 5
6
Recursion does the trick
  • fix f f (fix f)
  • g fib 0 1
  • g fib 1 1
  • g fib n fib (n-1) fib (n-2)
  • fib1 fix g

7
Taming the duplication
  • fib2 Integer -gt Integer
  • fib2 z f z
  • where table array (0,z) (i, f i) i lt-
    range (0,z)
  • f 0 1
  • f 1 1
  • f n (table ! (n-1)) (table ! (n-2))
  • Maingt fib2 25
  • 121393
  • (3299 reductions, 4603 cells)
  • Result is instantaeous on my machine

8
Generalizing
  • memo Ix a gt (a,a) -gt ((a -gt b) -gt a -gt b) -gt
    a -gt b
  • memo bounds g f
  • where arrayF array bounds (n, g f n) n lt-
    range bounds
  • f x arrayF ! x
  • fib3 n memo (0,n) g n
  • fact memo (0,100) g
  • where g fact n
  • if n0 then 1 else n fact (n-1)

9
Type of a Parser
  • data Parser a Parser (String -gt (a,String))
  • A function inside a data definition.
  • The output can is a list of successful parses.
  • This type can be made into a monad
  • Also be made into a Monad with zero and () or
    plus.

10
Defining the Monad
  • instance Monad Parser where
  • return v Parser (\inp -gt (v,inp))
  • p gtgt f
  • Parser (\inp -gt concat
  • applyP (f v) out
  • (v,out) lt- applyP p inp)
  • instance MonadPlus Parser where
  • mzero Parser (\inp -gt )
  • mplus (Parser p) (Parser q)
  • Parser(\inp -gt p inp q inp)
  • instance Functor Parser where . . .
  • where applyP undoes the constructor
  • applyP (Parser f) x f x

Note the comprehension syntax
11
Typical Parser
  • Because the parser is a monad we can use the Do
    syntax .
  • do x1 lt- p1
  • x2 lt- p2
  • ...
  • xn lt- pn
  • f x1 x2 ... Xn

12
Running the Parser
  • Running Parsers
  • papply Parser a -gt String -gt (a,String)
  • papply p applyP (do junk p)
  • junk skips over white space and comments. We'll
    see how to define it later

13
Simple Primitives
  • applyP Parser a -gt String -gt (a,String)
  • applyP (Parser p) p
  • item Parser Char
  • item Parser (\inp -gt case inp of
  • "" -gt
  • (xxs) -gt (x,xs))
  • sat (Char -gt Bool) -gt Parser Char
  • sat p do x lt- item
  • if p x then return x else mzero
  • ? papply item "abc"
  • ('a',"bc")

14
Examples
  • ? papply item "abc"
  • ('a',"bc")
  • ? papply (sat isDigit) "123"
  • ('1',"23")
  • ? parse (sat isDigit) "abc"

15
Useful Parsers
  • char Char -gt Parser Char
  • char x sat (x )
  • digit Parser Int
  • digit do x lt- sat isDigit
  • return (ord x - ord '0')
  • lower Parser Char
  • lower sat isLower
  • upper Parser Char
  • upper sat isUpper

16
Examples
  • char x sat (x )
  • ? papply (char 'z') "abc"
  • ? papply (char 'a') "abc"
  • ('a',"bc")
  • ? papply digit "123"
  • (1,"23")
  • ? papply upper "ABC"
  • ('A',"BC")
  • ? papply lower "ABC"

17
More Useful Parsers
  • letter Parser Char
  • letter sat isAlpha
  • Can even use recursion
  • string String -gt Parser String
  • string "" return ""
  • string (xxs)
  • do char x string xs return (xxs)
  • Helps define even more useful parsers
  • identifier Parser String
  • identifier do x lt- lower
  • xs lt- many alphanum
  • return (xxs)
  • What do you think many does?

18
Examples
  • ? papply (string "tim") "tim is red"
  • ("tim"," is red")
  • ? papply identifier "tim is blue"
  • ("tim"," is blue")
  • ? papply identifier "x5W3 12"
  • ("x5W3"," 12")

19
Choice -- 1 parser or another
  • Note that the operator (from MonadPlus) gives
    non-deterministic choice.
  • instance MonadPlus Parser where
  • (Parser p) (Parser q)
  • Parser(\inp -gt p inp q inp)
  • Sometimes wed like to prefer one choice over
    another, and take the second only if the first
    fails
  • We dont we need an explicit sequencing operator
    because the monad sequencing plays that role.

20
Efficiency
  • force Parser a -gt Parser a
  • force p
  • Parser (\ inp -gt
  • let x applyP p inp
  • in (fst (head x), snd (head x))
  • (tail x) )
  • Deterministic Choice
  • () Parser a -gt Parser a -gt Parser a
  • p q
  • Parser(\inp -gt
  • case applyP (p mplus q) inp of
  • -gt
  • (xxs) -gt x)

21
Example
  • ? papply (string "x" string "b") "abc"
  • ? papply (string "x" string "b") "bcd"
  • ("b","cd")

22
Sequences(more recursion)
  • many Parser a -gt Parser a
  • many p force (many1 p return )
  • many1 Parser a -gt Parser a
  • many1 p do x lt- p
  • xs lt- many p
  • return (xxs)
  • sepby Parser a -gt Parser b -gt Parser a
  • p sepby sep (p sepby1 sep) return
  • sepby1 Parser a -gt Parser b -gt Parser a
  • p sepby1 sep
  • do x lt- p
  • xs lt- many (do sep p)
  • return (xxs)

23
Example
  • ? papply (many (char 'z')) "zzz234"
  • ("zzz","234")
  • ? papply (sepby (char 'z') spaceP) "z z z 34"
  • ("zzz"," 34")

24
Sequences separated by operators
  • chainl Parser a -gt Parser (a -gt a -gt a) -gt a
    -gt Parser a
  • chainl p op v (p chainl1 op) return v
  • chainl1 Parser a -gt Parser (a -gt a -gt a) -gt
    Parser a
  • p chainl1 op do x lt- p rest x
  • where rest x
  • do f lt- op y lt- p rest (f x y) return
    x
  • ? papply (chainl int (return ()) 0) "1 3 4 abc"
  • (8,"abc")

25
Tokens and Lexical Issues
  • spaceP Parser ()
  • spaceP do many1 (sat isSpace) return ()
  • comment Parser ()
  • comment dostring "--" many (sat p) return
    ()
  • where p x x / '\n'
  • junk Parser ()
  • junk do many (spaceP comment) return ()
  • A Token is any parser followed by white space or
    a comment
  • token Parser a -gt Parser a
  • token p do v lt- p junk return v

26
Using Tokens
  • symb String -gt Parser String
  • symb xs token (string xs)
  • ident String -gt Parser String
  • ident ks
  • do x lt- token identifier
  • if (not (elem x ks))
  • then return x else zero
  • nat Parser Int
  • nat token natural
  • natural Parser Int
  • natural digit chainl1 return (\m n -gt 10m
    n)

27
Example
  • ? papply (token (char 'z')) "z 123"
  • ('z',"123")
  • ? papply (symb "tim") "tim is cold"
  • ("tim","is cold")
  • ? papply natural "123 abc"
  • (123," abc")
  • ? papply (many identifier) "x d3 23"
  • ("x"," d3 23")
  • ? papply (many (token identifier)) "x d3 23"
  • ("x", "d3","23")

28
More Parsers
  • int Parser Int
  • int token integer
  • integer Parser Int
  • integer (do char '-
  • n lt- natural
  • return (-n))
  • nat

29
Example Parsing Expressions
  • data Term Add Term Term
  • Sub Term Term
  • Mult Term Term
  • Div Term Term
  • Const Int
  • addop Parser(Term -gt Term -gt Term)
  • addop do symb "" return Add
  • do symb "-" return Sub
  • mulop Parser(Term -gt Term -gt Term)
  • mulop do symb ""return Mult
  • do symb "/" return Div

30
Constructing a Parse tree
  • expr Parser Term
  • addop Parser (Term -gt Term -gt Term)
  • mulop Parser (Term -gt Term -gt Term)
  • expr term chainl1 addop
  • term factor chainl1 mulop
  • factor (do n lt- token digit
  • return (Const n))
  • (do symb "( n lt- expr
  • symb ") return n)
  • ? papply expr "5 abc"
  • (Const 5,"abc")
  • ? papply expr "4 5 - 2"
  • (Sub (Add (Const 4) (Const 5))(Const 2),)

31
Array Based Parsers
  • type Subword (Int,Int)
  • newtype P a P (Array Int Char -gt Subword -gt
    a)
  • unP (P z) z
  • emptyP P ()
  • emptyP P f
  • where f z (i,j) () i j
  • notchar Char -gt P Char
  • notchar s P f
  • where f z (i,j) z!j i1 j, z!j / s
  • charP Char -gt P Char
  • charP c P f
  • where f z (i,j) c i1 j, z!j c

32
  • anychar P Char
  • anychar P f
  • where f z (i,j) z!j i1 j
  • anystring P(Int,Int)
  • anystring P f
  • where f z (i,j) (i,j) i lt j
  • symbol String -gt P (Int,Int)
  • symbol s P f
  • where f z (i,j)
  • if j-i length s
  • then (i,j) and z!(ik) s!!(k-1)
  • k lt-1..(j-i)
  • else

33
Combinators
  • infixr 6
  • () P b -gt P b -gt P b
  • () (P r) (P q) P f
  • where f z (i,j) r z (i,j) q z (i,j)
  • infix 8 ltltlt
  • (ltltlt) (b -gt c) -gt P b -gt P c
  • (ltltlt) f (P q) P h
  • where h z (i,j) map f (q z (i,j))
  • infixl 7
  • () P(b -gt c) -gt P b -gt P c
  • () (P r) (P q) P f
  • where f z (i,j)
  • f y k lt- i..j, f lt- r z (i,k), y lt-
    q z (k,j)

34
  • run String -gt P b -gt b
  • run s (P ax) ax (s2a s) (0,length s)
  • s2a s (array bounds (zip 1.. s))
  • where bounds (1,length s)
  • instance Monad P where
  • return x P(\ z (i,j) -gt if ij then x else
    )
  • (gtgt) (P f) g P h
  • where h z (i,j)
  • concat unP (g a) z (k,j)
  • k lt- i..j , a lt- f z (i,k)

35
Examples
  • p1 do symbol "tim" c lt- anychar
  • symbol "tom" return c
  • ex4 run "tim5tom" p1
  • ex5 run "timtom" p1
  • Maingt ex4
  • "5"
  • (1808 reductions, 2646 cells)
  • Maingt ex5
  • ""
  • (1288 reductions, 1864 cells)
Write a Comment
User Comments (0)
About PowerShow.com