I. Programming in the Style of Mathematics
A. Introduction
1. Traditional computing: not the best tool for math
a. traditional finite representations do not respect mathematical accounts of numbers:
i. when integers grow Òtoo largeÓ they can become negative
ii. floating point values represent real numbers inaccurately
\break (see Prof. JanebaÕs Numerical Analysis course)
2. Computing: more than just a tool for {\it doing} math
a. computing can have a better relationship with math:
i. computational style can reflect math sensibilities
ii. computation can be studied from a mathematical perspective
3. What are Òmathematical sensibilitiesÓ?
a.
i. proof, equational reasoning
ii. concise, stylized language
iii. what, not how (i.e., results, not process)
iv. focus on relevance, suppress distractions
v. abstraction and generality
vi. conceptual elegance (e.g., symmetry, duality)
4. Functional programming
a. an alternative style (or ÒparadigmÓ), outside the mainstream, which stresses sound mathematical semantics and encourages formal methods
5. The Haskell programming language
a. a {\it purely} functional language with a math-friendly syntax and a sophisticated, flexible type system
b. \break {\it (other functional languages: Clean, SML, Caml, Scheme, LISP, APL, ...)}
B. A Quick Taste of Haskell (I)
1. An introduction to Haskell through examples
a. we give the flavor of Haskell via examples in the Hugs interactive interpreter
\break {\it (in the talk itself, we hope to run these examples ÒliveÓ)}
2. Simple arithmetic, large integers
a. simple calculation can be done at the interactive prompt
b. \begin{haskell}> 2 + 3 * 5
17
> 37^37
10555134955777783414078330085995832946127396083370199442517\end{haskell}
3. Exact rational arithmetic
a. Haskell provides exact rational numbers (written with an infix "\%") as a safer alternative to floating-point
b. \begin{haskell}> 51 % 3
17 % 1
> (51 % 3) * (2 % 5)
34 % 5\end{haskell}
4. Floating-point arithmetic
a. Traditional IEEE floating-point arithmetic is also available (with the usual caveats)
b. \begin{haskell}> 2.3 ^ 5
64.36343
> $$ * 25.333
1630.51877\end{haskell}
5. Pairs and tuples
a. Unlike most languages, Haskell provides pairs and tuples as independent entities (e.g., to return multiple results)
b. \begin{haskell}> 17 `divMod` 3
(5,2)
> 17 `divMod` 3
(5,2)\end{haskell}
6. Lists of numbers (ellipsis notation)
a. we can easily generate and manipulate lists of numbers
b. \begin{haskell}> [1..12]
[1,2,3,4,5,6,7,8,9,10,11,12]
> sum [1..10]
55
> product [1..10]
3628800\end{haskell}
7. Z-F expressions
a. another notational convenience (due to David Turner) is {\bf list comprehension}, which mimics notation from Zermelo-Fraenkel set theory
b. \begin{haskell}> [ a * b | a<-[1..3], b<-reverse [1..4] ]
[4,3,2,1,8,6,4,2,12,9,6,3]
> [ 2^i | i<-[1..20], odd i]
[2,8,32,128,512,2048,8192,32768,131072,524288]\end{haskell}
8. Strings and list operations
a. \begin{haskell}> replicate 3 "hello"
["hello","hello","hello"]
> words "This is a sample text"
["This","is","a","sample","text"]
> unwords (reverse $$)
"text sample a is This"\end{haskell}
C. A Quick Taste of Haskell (II)
1. Booleans and {\tt if} expressions
a. The usual boolean values are written with initial caps; the conditional construct is an {\it expression}, not a {\it statement}
b. \begin{haskell}> if 3 < 2 then "oops" else "hurray!"
"hurray!"
> (if 3 < 2 then (+) else (*)) 10 20
200\end{haskell}
2. Infinite lists and lazy evaluation
a. Haskell provides {\bf lazy evaluation} for functions and data structures, so we can define infinite lists (it is prudent to avoid evaluation of a whole infinite list!)
b. \begin{haskell}> [1,3..]
[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,{Interrupted!}
> take 20 [1,3..]
[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39]\end{haskell}
3. Higher-order functions
a. Haskell allows functions to take other functions as arguments, or to return functions as results
b. \begin{haskell}> even 3
False
> any even [1,3..11]
False
> any (>10) [1..]
True\end{haskell}
4. Currying
a. Haskell functions of several arguments are actually "curried", i.e., they are higher-order functions of successive arguments
b. \begin{haskell}> 2 + 3
5
> (+) 2 3
5
> :t (+)
(+) :: Num a => a -> a -> a
> :t (2+)
(2 +) :: Num a => a -> a\end{haskell}
5. The map functional
a. The map functional takes a function as its first argument, then applies it to every element of a list
b. \begin{haskell}> map (^2) [1..10]
[1,4,9,16,25,36,49,64,81,100]
> map (`div` 3) [1..20]
[0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6]
> map reverse ["hey", "there", "world"]
["yeh","ereht","dlrow"]\end{haskell}
6. The fold functions
a. The fold functions {\tt foldl} and {\tt foldr} combine elements of a list based on a binary function and an initial value
b. \begin{haskell}> foldr (+) 0 [1..10]
55
> sum [1..10]
55
> foldr (*) 1 [1..5] == 1 * 2 * 3 * 4 * 5 * 1
True
> foldr (++) [] ["a", "bb", "ccc"]
"abbccc"\end{haskell}
7. Strong, static typing
a. expressions in Haskell are only well-formed if they are {\bf well-typed} according to a sophisticated type system
b. \begin{haskell}> :t ['a'..'z']
enumFromTo 'a' 'z' :: [Char]
> :t reverse
reverse :: [a] -> [a]
> :t foldr
foldr :: (a -> b -> b) -> b -> [a] -> b
> :t foldr (++) []
foldr (++) [] :: [[a]] -> [a]\end{haskell}
8. More examples of Haskell in actions
a. here are some links to external files defining Haskell types and functions
i. \xlink{http://www.willamette.edu/~fruehr/haskell/code/short.txt}{short, simple examples}
ii. \xlink{http://www.willamette.edu/~fruehr/haskell/code/MathEgs.txt}{some math-oriented examples from the Hugs distibution}
iii. \xlink{http://www.willamette.edu/~fruehr/haskell/code/Lattice.txt}{type classes used to capture lattices abstractly (from the Hugs distibution)}
iv. \xlink{http://www.willamette.edu/~fruehr/haskell/code/GenSort.txt}{a generic sorting application applied to a simple database}
v. \xlink{http://www.willamette.edu/~fruehr/haskell/code/expr.txt}{a flexible system for parsing and processing arithmetic expressions}
D. The Mathematical Style of Haskell
1. Programming paradigms
a. functional programming is just one of several major {\it programming paradigms}
b. \par \xlink{http://www.willamette.edu/~fruehr/haskell/lectures/paradigms.html}{(see diagram here)}
2. History of functional programming
a. functional proramming properly begins with LISP and APL in the 1950Õs and 1960Õs
\break (but it got a big boost in the late 70Õs with BackusÕ Turing Award)
3. History of Haskell
a. Haskell was a community effort (open source) in reaction to proprietary Miranda
(ÒMirandaÓ is a trademark of Research Software, Ltd.Ó)
4. Haskell fits well with mathematical style and usage
a. many features collaborate to support this fit
b. based on mathematical concepts (functions, algebra)
i. higher-order functions, algebra = structure + operations
c. concise, high-level descriptions
i. whole-structure manipulation
d. referential transparency (allows equational reasoning)
i. no aliasing, no time-bound effects
e. well-behaved numeric types
i. big integers and exact rationals
5. an uncluttered, math-compatible syntax
a. infix operators, ZF-expressions, guards, let declarations
b. \begin{haskell}2+3*4
[ x^2 | x <- [1..10] ]
abs x | (x < 0 ) = -x
| otherwise = x
let x = 3*y+7 in x^2-k\end{haskell}
6. Haskell has been guided by a consistent, {\it persistent} design philosophy
a. the community has held its ground against encroachment by ÒimpureÓ features
b. this discipline has actually led to major innovations (e.g., monads)
E. Technical Aspects of the Haskell Language
1. Programs are sets of equational definitions
a. equational (or conditional-equational) substitution is vital to formal methods
2. Based on lambda calculus and combinators
a. these theories of pure functions inspire higher-order functions and lazy evaluation
3. Data structure definitions are (roughly) initial algebras
a. they also look like context-free grammars, thus encouraging embedded languages
4. Strong, static type system provides security and flexibility
a. Hindley-Milner polymorphism allows convenient inference of types
5. Type classes provide for representation independence and data abstraction
a. we can capture the abstract essence of a component without requiring a specific implementation
6. Types as high-level sketches of structure
a. Haskell increasingly supports type-level features, encouraging high-level Òprogramming-in-the-largeÓ
F. Haskell Applications (I)
1. Fran: Functional Reactive Animation
a. Conal Elliot and Paul Hudak have developed sophisticated graphics, animation and interaction techniques
b. \par \xlink{http://www.conal.net/fran/tutorial.htm}{ElliotÕs FRAN tutorial}
c. \par \xlink{http://haskell.cs.yale.edu/soe/demos.htm}{HudakÕs SOE demo page at Yale}
2. Functional imagery
a. Conal and Jerzy Karczmarczuk have explored images as functions from points to colors
b. \par \xlink{http://conal.net/papers/functional-images/jfp/figures.html}{ConalÕs Functional Images gallery}
c. \par \xlink{http://users.info.unicaen.fr/~karczma/arpap/texturf.pdf}{JerzyÕs texture page}
3. Haskore: based on an algebra of music
a. Paul Hudak at Yale is also a jazz musician: he has developed an abstract language for music based on pitches, tempos, transpositions and sequential and parallel composition
b. \par \xlink{http://haskell.org/haskore/onlinetutorial/haskore.html}{the on-line documentation for Haskore}
4. Haskell in K-12 education
a. a group at Yale led by Hudak and John Peterson is exploring the use of these ideas for mathematics-based K-12 education
b. \par \xlink{http://www.haskell.org/edsl/math.htm}{introduction and sample student work}
G. Haskell Applications (II)
1. Lula: based on an algebra of theatre lighting
a. Michael Sperber at Universitat Tubingen has developed a successful theatre lighting program based on abstract notions of cues, fixtures, intensities, pan/tilt, etc.
b. \par \xlink{http://www-pu.informatik.uni-tuebingen.de/users/sperber/papers/developing-stage-lighting-system.pdf}{a paper describing Lula and its algebra}
2. An algebra of financial contracts
a. Peyton Jones, Eber and Seward have developed an award-winning, abstract approach to specifying financial contracts based on abstract notions of time, choice, obligation and currency
b. \par \xlink{http://research.microsoft.com/~simonpj/papers/Options-ICFP.ppt}{a PowerPoint presentation on the contract system}
3. The Vital visualization tool for Haskell
a. Keith Hammond at the University of Kent at Canterbury has developed a spreadsheet-like interface for visualizing Haskell data
b. \par \xlink{http://www.cs.ukc.ac.uk/people/staff/fkh/Vital/overview/overview.html}{an on-line overview of Vital}
4. The Alfa proof editor
a. a group at Chalmers University in Sweden has developed an interactive proof editor in Haskell
b. \par \xlink{http://www.math.chalmers.se/~hallgren/Alfa/Tutorial/ndstyle.html}{a quick overview of Alfa}
5. Haskell-based hardware simulation (NSW, Ruby, Hawk)
a. a number of different groups around the world have used Haskell and other functional languages to model hardware (chips) and perform logic-based validation
H. Haskell Opportunities at Home and Abroad
1. Functional Programming, CS 454
a. I teach a one-semester course in functional programming (using Haskell) every two years at Willamette
b. \par (offered in Spring of 2003 on MWF 10:20-11:20, lab MW 3:00-4:30)
2. A gateway to the mathematics of programming
a.
ii. study of functional programming opens up the door to a variety of mathematical topics in Computer Science:
ii. alternative foundations for computability (lambda calculus)
ii. denotational semantics (formal meanings for programs via fixed-points)
ii. type theory and intutionistic logic (a Òtyped set theoryÓ with a constructive flavor)
ii. category theory (an abstract approach to algebra treating structure generically)
3. PacSoft Research at the Oregon Graduate Institute
a. a world-renowned research group at OGI focuses on functional programming, formal methods and Haskell
b. \par \xlink{http://www.cse.ogi.edu/PacSoft/}{the PacSoft home page}
c. \par (I sometimes also attend a Tuesday morning research seminar there)
4. Galois Connections, Inc.
a. a faculty-led spin-off company uses Haskell and other formal methods tools to develop secure software solutions for clients such as the National Security Agency
b. \par \xlink{http://galois.com/}{the Galois Connections home page}
5. Opportunities for Graduate Study
a. many top-flight graduate schools have programs and projects based on functional programming and related ideas
b. \par US schools: OGI, OSU, Yale, Indiana, Stanford, Carnegie Mellon, MIT, University of Washington, UC Berkeley, and many others
c. \par international schools: Oxford, Glasgow, Chalmers, Nottingham, Kent, York, Edinburgh, Utrecht, Glasgow, INRIA (France), New South Wales, and others
END