SEUJP in Foundations of Computer Science Advanced Functional Programming - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

SEUJP in Foundations of Computer Science Advanced Functional Programming

Description:

Haskell and Hugs. Haskell Brooks Curry: mathematician and ... Various implementations: Hugs (interpreter for Windows, Mac, Unix); GHC, NHC, HBC (compilers) ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 31
Provided by: thom214
Category:

less

Transcript and Presenter's Notes

Title: SEUJP in Foundations of Computer Science Advanced Functional Programming


1
SEUJP in Foundations of Computer
ScienceAdvanced Functional Programming
  • 30-31 October 2001
  • Tony Daniels Keith Hanna Stefan Kahrs
  • Claus Reinke Simon Thompson

2
Introduction
  • Welcome
  • Why SEUJP?
  • Introductions
  • Arrangements
  • SE14 and the multimedia lab
  • Coffee machine water
  • We'll take you to lunch
  • and we'll got somewhere for dinner curry?
  • Anything else?

3
The course
  • Introduction Practical Simon Team
  • The lambda calculus Stefan Tony
  • Types Keith Stefan
  • Domain Specific Embedded Languages Claus
  • Verification Simon
  • Wrap-up Team
  • You have the detailed timetable.

4
Functional programming
  • Computation evaluation of expressions.
  • Programming defining your own functions.
  • Function

5
Modern FP Haskell, ML, ...
  • Functions as data
  • higher-order programming.
  • Advanced type systems
  • polymorphism, overloading, generics, dependent
    types,
  • Pure or pureish
  • flexibility in implementation
  • verification
  • Interoperability
  • controlled side-effects

6
Why FP for SEUJP?
  • A perspective on the fundamentals of programming.
  • Consequences in related fields
  • OO types, process algebra, domains,
  • Implementation language of choice for many
    theorists.

7
Haskell and Hugs
  • Haskell Brooks Curry mathematician and logician
    inventor of the ?-calculus.
  • Haskell 98 standard.
  • Various implementations Hugs (interpreter for
    Windows, Mac, Unix) GHC, NHC, HBC
    (compilers).
  • http//www.haskell.org/

8
Examples, examples, examples.
  • To familiarise you with Haskell syntax.
  • To introduce the case studies.

9
Definitions in Haskell
  • name Type
  • name expression
  • size Int
  • size 1213
  • square Int -gt Int
  • square n nn

10
Evaluation in Haskell
  • square n nn
  • double n 2n

6 66
3 23
square (double 3)
11
How many pieces with n cuts?
12
How many pieces with n cuts?
  • No cuts 1 piece.
  • With the nth cut, you get n more pieces
  • cuts Int -gt Int
  • cuts n
  • n0 1
  • ngt0 cuts (n-1) n
  • otherwise 0

13
Functions over pictures
  • A function to flip a picture in a vertical mirror

flipV
14
Functions over pictures
  • A function to invert the colours in a picture

15
Functions over pictures
  • A function to put one picture above another

above
16
A naïve implementation
  • type Picture String
  • type String Char
  • A Picture is a list of Strings.
  • A String is a list of Char (acters).

.......... ......... ......... .........
. ......... ....... ........ .......
... .......... .......... .......... ......
....
17
How are they implemented?
  • flipH Reverse the list of strings.
  • flipV Reverse each string.
  • rotate flipH then flipV (or v.versa).
  • above Join the two lists of strings.
  • sideBySide Join corresponding lines.
  • invertColour Change each Char and each line.
  • superimpose Join each Char join each line.

18
How are they implemented?
  • flipH reverse
  • flipV map reverse
  • rotate flipV . flipH
  • above
  • sideBySide zipWith ()
  • invertColour map (map invertChar)
  • superimpose zipWith (zipWith combine)

19
Reversing a list
  • A list is either empty, , or is built by adding
    an element to an existing list, (xxs). is
    CONS.
  • Convention variables xs, ys, over lists.
  • reverse
  • reverse (xxs) reverse xs x
  • reverse 2,4,1

reverse 4,1 2
joins two lists.
20
The type of reverse
  • Haskell is strongly typed detect all type errors
    before evaluation.
  • reverse
  • reverse (xxs) reverse xs x
  • reverse a -gt a
  • a is a type variable reverse works over any list
    type, returning a list of the same type.

21
Implementing the mapping pattern
  • map (a -gt b) -gt a -gt b
  • map f
  • map f (xxs) f x map f xs
  • Examples over pictures
  • flipV pic map reverse pic
  • invertColour pic map invertLine pic

22
Partial application functions as results
  • map isEven 1,2 False, True
  • map isEven ??
  • It is a partial application, which gives a
    function
  • give it a Int and it will give you back a
    Bool
  • map isEven Int -gt Bool

23
Another pattern zipping together
zipWith f xs ys zipWith f (xxs) (yys)
f x y zipWith f xs ys
zipWith (a-gtb-gtc) -gt a -gt b -gt c
24
Highly generic functions
  • Not only apply to a whole family of types, as in
  • concat a -gt a
  • but also apply to any methods over those types.
  • zipWith (a-gtb-gtc) -gt a -gt b -gt c
  • Supports reuse in many (unforeseen) contexts.
  • When you define a new type, ask what are the HOT
    (higher-order, polymorphic type) functions.

25
Semantic tableaux an example
?((A?C)?((A?B)?C))
26
Semantic tableaux implementation
  • data Formula A B C D E F G H
  • Not Formula
  • Or Formula Formula
  • And Formula Formula
  • Implies Formula Formula
  • Iff Formula Formula
  • deriving (Show, Eq, Ord)
  • type Tableau Set (Set Formula)

27
Sets
  • An abstract data type, using ordered lists
    without repetitions as the representation.
  • empty Set a
  • sing a -gt Set a
  • memSet Ord a gt Set a -gt a -gt Bool
  • union,inter, Ord a gt Set a -gt Set a -gt Set a
  • eqSet Eq a gt Set a -gt Set a -gt Bool
  • makeSet Ord a gt a -gt Set a
  • mapSet Ord b gt (a -gt b) -gt Set a -gt
    Set b
  • etc.

28
Expanding formulas
  • expandFormula Formula -gt Set (Set Formula)
  • expandFormula (Or a b)
  • makeSet sing a, sing b
  • expandFormula (And a b)
  • sing (makeSet a,b)
  • expandFormula (Not (Or a b))
  • sing (makeSet Not a, Not b)
  • expandFormula (Not (And a b))
  • makeSet sing (Not a), sing (Not b)

29
Algorithm
  • A single step expands one formula in each branch,
    if it's possible
  • and removes contradictory branches.
  • Iterate until reach a fixed point.

30
Practical session
  • Getting started with Hugs.
  • Picture and tableau exercises of varying
    difficulty
  • more than enough for this session.
Write a Comment
User Comments (0)
About PowerShow.com