Reasoning with Functional Programs - PowerPoint PPT Presentation

About This Presentation
Title:

Reasoning with Functional Programs

Description:

Title: Efficiency Last modified by: test Created Date: 9/30/1996 6:28:10 PM Document presentation format: On-screen Show (4:3) Other titles: Times New Roman Arial ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 20
Provided by: cecsWrig9
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Reasoning with Functional Programs


1
Reasoning with Functional Programs
  • Optimization by source to source transformation
  • Well-founded induction
  • Streams Infinite lists

2
Linear Speed-up
  • fun sum 0
  • sum (xxs) x sum xs
  • fun double
  • double (xxs) 2x double xs
  • val f sum o double
  • fun g 0
  • g (xxs) 2x g xs

3
Exponential Speed-up
  • fun fib 0 0
  • fib 1 1
  • fib n fib (n-1) fib (n-2)
  • 1 (g n)
  • fun g 0 (0,1)
  • g n let val (u,v) g (n-1)
  • in ( v, u v )
  • end
  • Exponential Fibonacci sequence definition
    converted into linear implementation.
    (Cf. Recursion vs Iteration debate)

4
Fold/Unfold Transformation
  • Meaning preserving rules for generating new
    recursion equations that execute efficiently.
  • Derivation
  • ( Definition (eureka) ) g(n) (fib(n),
    fib(n1))
  • ( Instantiation ) g(0) (fib(0), fib(1))
  • ( Unfolding ) (0,1)
  • g(n1) (fib(n1), fib(n2))
  • ( Unfolding ) (fib(n1),fib(n)fib(n1) )
  • ( Abstraction ) let (u,v) g(n)
  • in (v,uv)
  • end

5
McCarthys 91-function
  • f(x) if x gt 100 then x - 10
  • else f(f(x11))
  • ( Determining the order on N that reflects the
    complexity of the function is non-trivial. )
  • f(0) ? f(f(11) ) ? f(f(f(22))) ?
  • f(89) ? f(f(100) ) ? f(f(f(111))) ?
  • f(90) ? f(f(101) ) ? f(91) ?
  • f(101) ? 91
  • f(102) ? 92

6
Structuring the domain for 91-function
  • ?x ? N x gt 100
  • f(x) x - 10
  • ?x ? N 89 lt x lt 100
  • f(x) f(f(x11))
  • f(x11-10) f(x1)
  • ?x ? N 79 lt x lt 89
  • f(x) f(f(x11))
  • f(91) 91
  • Similarly for ?x ? N 68 lt x lt 79,
    ...

7
Well-Founded Induction
  • A relation ? is well-founded if there
    exists no infinite descending chain.
  • ? x3 ? x2 ? x1
  • Induction Step
  • if ?(x) for all x ? y, then
    ?(y)
  • Required to show equivalences such as
  • f(x) if xgt100 then x-10 else f(f(x11))
  • f(x) if xgt100 then x-10 else 91

8
Motivation for Domain Theory
  • Construction of Domains Continuity
  • A set can never be equi-numerous with its
    function space. However, LISP defines and
    represents functions in the same language.
  • Equivalence of Functions
  • f1(x) if f1(x) 1 then 0 else 1
  • f2(x) f2(x1)
  • f3(x) f3(x) 1

9
Streams Motivation
  • Ref SICP
  • (Abelson and Sussmans
  • Structure and Interpretation of Computer Programs)

10
  • Modeling real-world objects (with state) and
    real-world phenomena
  • Use computational objects with local variables
    and implement time variation of states using
    assignments
  • Alternatively, use sequences to model time
    histories of the states of the objects.
  • Possible Implementations of Sequences
  • Using Lists
  • Using Streams
  • Delayed evaluation (demand-based evaluation)
    useful (necessary) when large (infinite)
    sequences are considered.

11
  • (define stream-car car)
  • (define (stream-cdr s)
  • ( (cdr s) ) )
  • (define (stream-cons x s)
  • (cons x ( lambda ( ) s) ) )
  • (define the-empty-stream ( ))
  • (define stream-null? null?)

12
  • (define (stream-filter p s)
  • (cond ((stream-null? s) the-empty-stream)
  • ((p (stream-car s))
  • (cons-stream (stream-car s)

  • (stream-filter p (stream-cdr s))))
  • (else (stream-filter p (stream-cdr
    s))) )
  • )
  • (define (stream-enum-interval low high)
  • (if (gt low high) the-empty-stream
  • (cons-stream low
  • (stream-enum-interval ( 1 low)
    high))))

13
  • (stream-car
  • (stream-cdr
  • (stream-filter prime?
  • (stream-enum-interval
    100 1000))))
  • (define (fibgen f1 f2)
  • (cons-stream f1 (fibgen f2 ( f1 f2)))
  • (define fibs (fibgen 0 1))

14
Streams in ML
15
Infinite Lists Streams Lazy Lists
  • ML represents the tail of an infinite list by a
    function (thunk), to delay its evaluation.
  • datatype a seq Nil
  • Cons of a (unit -gt a seq)
  • The conventional termination criterion must be
    modified by requiring that a program generate
    each finite part of the result in finite time.
  • Tasks such as list-reversal, computing the
    maximum of a list, etc are no longer computable.

16
  • exception Empty
  • fun hd (Cons (x,xf)) x
  • hd Nil raise Empty
  • fun tl (Cons (x,xf)) xf ()
  • tl Nil raise Empty
  • fun take (xs, 0)
  • take (Nil,n) raise General.Subscript
  • take (Cons (x,xf),n)
  • x take (xf(), n-1)

17
  • fun map f Nil Nil
  • map f (Cons (x,xf))
  • Cons ( f x, fn () gt map f (xf ()) )
  • fun filter p Nil Nil
  • filter p (Cons (x,xf))
  • if p x
  • then Cons (x, fn () gt filter p (xf ()))
  • else filter p (xf ())
  • fun natnum n
  • Cons (n, (fn () gt natnum (n1)) )
  • take ((map (fn igt2i) (natnum 0)), 10)
  • ( val it 0,2,4,6,8,10,12,14,16,18
    int list )

18
Prime Numbers Sieve of Eratosthenes
  • fun sift p
  • filter (fn n gt (n mod p) ltgt 0)
  • ( eliminates numbers divisible by prime
    p)
  • fun sieve (Cons (p,nf))
  • Cons( p, fn()gt sieve (sift p (nf())) )
  • val primes sieve (natnum 2)
  • take (primes, 100)
  • ( val it 2,3,5,7,11,13,17,19,23,29,31,37,.
    .. int list )

19
Prime Numbers Sieve of Eratosthenes with Lazy
Evaluation
  • nums Int -gt Int
  • nums n n nums (n1)
  • primes sieve (nums 2)
  • sieve (xxs)
  • x sieve z zlt-xs, z mod x / 0
  • Hugsgt load I\tkprasad\cs776\primes.hs
  • Maingt take 25 primes
  • 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61
    ,67,71,73,79,83,89,97
Write a Comment
User Comments (0)
About PowerShow.com