Title: SEUJP in Foundations of Computer Science Advanced Functional Programming
1SEUJP in Foundations of Computer
ScienceAdvanced Functional Programming
- 30-31 October 2001
- Tony Daniels Keith Hanna Stefan Kahrs
- Claus Reinke Simon Thompson
2Introduction
- 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?
3The 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.
4Functional programming
- Computation evaluation of expressions.
- Programming defining your own functions.
- Function
5Modern 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
6Why 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.
7Haskell 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/
8Examples, examples, examples.
- To familiarise you with Haskell syntax.
- To introduce the case studies.
9Definitions in Haskell
- name Type
- name expression
-
- size Int
- size 1213
- square Int -gt Int
- square n nn
10Evaluation in Haskell
6 66
3 23
square (double 3)
11How many pieces with n cuts?
12How 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
-
13Functions over pictures
- A function to flip a picture in a vertical mirror
flipV
14Functions over pictures
- A function to invert the colours in a picture
15Functions over pictures
- A function to put one picture above another
above
16A naïve implementation
- type Picture String
- type String Char
- A Picture is a list of Strings.
- A String is a list of Char (acters).
.......... ......... ......... .........
. ......... ....... ........ .......
... .......... .......... .......... ......
....
17How 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.
18How are they implemented?
- flipH reverse
- flipV map reverse
- rotate flipV . flipH
- above
- sideBySide zipWith ()
- invertColour map (map invertChar)
- superimpose zipWith (zipWith combine)
19Reversing 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.
20The 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.
21Implementing 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
22Partial 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
23Another 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
24Highly 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.
25Semantic tableaux an example
?((A?C)?((A?B)?C))
26Semantic 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)
27Sets
- 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.
28Expanding 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)
29Algorithm
- A single step expands one formula in each branch,
if it's possible - and removes contradictory branches.
- Iterate until reach a fixed point.
30Practical session
- Getting started with Hugs.
- Picture and tableau exercises of varying
difficulty - more than enough for this session.