cs5205haskell1 cs5205: foundations in programming languages overview lecturer : chin wei ngan email...

37
CS5205 Haskell 1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Email : [email protected] Office : S15 06-01 “Language Foundation, Applications and Reasoning

Upload: charla-walters

Post on 13-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

CS5205 Haskell 1

CS5205: Foundations in Programming Languages

Overview

Lecturer : Chin Wei Ngan

Email : [email protected]

Office : S15 06-01

“Language Foundation,Applications and Reasoning

CS5205 Haskell 2

Administrative Matters Administrative Matters

- mainly via Web-page + IVLE

- Reading Materials (mostly online): www.haskell.orgRobert Harper : Foundations of Practical Programming Languages.

Free PL books : http://www.cs.uu.nl/~franka/ref

- Lectures + Assignments + Test + Exam- Assignment (30%)- Test (20%)- Exam (50%)

CS5205 Haskell 3

Course Objectives Course Objectives

- graduate-level course to help build a good foundation

- languages as tool for programming

- reasoning about programs

- explore various applications enabled by languages

CS5205 Haskell 4

Course Outline Course Outline

• Lecture Topics (12 weeks + 1 week )• Advanced Language (Haskell) (4 weeks)

http://www.haskell.org• Type System (3 weeks)

http://www.cs.cmu.edu/~rwh/plbook/book.pdf• Applications enabled by Language (3 weeks)

• hoogle/darcs• user interface• parallelism/cloud computing

• Formal Reasoning & Theorem Proving (2 weeks)

CS5205 Haskell 5

Advanced Language - Haskell Advanced Language - Haskell

• Strongly-typed with polymorphism• Higher-order functions• Pure Lazy Language. • Algebraic data types + records• Exceptions• Type classes, Monads, Arrows, etc• Advantages : concise, abstract, reusable

• Why use Haskell ? programmerproductivity

CS5205 Haskell 6

Type SystemType System

• Abstract description of code + genericity

• Compile-time analysis that is tractable

• Guarantees absence of some bad behaviors

• Issues – expressivity, soundness, completeness, inference?

• How to use, design and prove type system.

• Why? detect bugs early

CS5205 Haskell 7

Some ApplicationsSome Applications

• Hoogle – search in code library

• Darcs – distributed version control

• Programming user interface with arrows..

• How to exploit multicore and parallelism?

CS5205 Haskell 8

Program Reasoning and Proving Program Reasoning and Proving

• Is sorting algorithm correct?• Any memory leaks?• Any null pointer dereference?• Any array bound violation?

• What is the your specification/contract?

• How to verify program correctness?

• Why? software reliability

CS5205 Haskell 9

CS5205: Foundations in Programming Languages

Haskell

A pure lazy functional language that embodies many innovative ideas in language design.

CS5205 Haskell 10

Haskell Haskell

• Advanced programming language – reusable, abstract, efficient, feature-rich.

• Higher-order functions• Values, Types and Lazy evaluation• Polymorphic Types and Inference• Products, Records and Algebraic Types• Type-classes, Monads etc• Enriched with syntactic sugar and helpful

shorthands.

Reference --- A Gentle Introduction to Haskell 98 http://www.haskell.org/tutorial

CS5205 Haskell 11

Values and TypesValues and Types

• As a purely functional language, all computations are done via evaluating expressions (syntactic sugar) to yield values (normal forms as answers).

• Each expression has a type which denotes the set of possible outcomes.

• e :: t can be read as expression e has type t.• Examples of typings, associating each expression with

its corresponding type.

5 :: Integer'a' :: Char[1,2,3] :: [Integer]('b', 4) :: (Char, Integer)“cs5” :: String (same as [Char])

CS5205 Haskell 12

Functions and its TypeFunctions and its Type

• Functions can be defined by equations with parameters:

inc x = x+1

• Or through lambda expression (anonymous functions)

(\ x -> x+1)

• They can also be given suitable function typing:

inc :: Integer -> Integer(\x -> x+1) :: Integer -> Integer

• Types can be user-supplied or inferred.

CS5205 Haskell 13

Expression EvaluationExpression Evaluation

• Expressions can be evaluated, and reduced to result form. This can be informally represented using:

e1 ) e2

• A concrete example of this is:

inc (inc 3) ) 5

CS5205 Haskell 14

Expression-OrientedExpression-Oriented

• Conditional expressions are of the form;if e1 then e2 else e3

• An example function:

• Pattern-matching also possible

fact :: Integer -> Integerfact n = if n=0 then 1

else n * fact (n-1)

fact 0 = 1 fact n = n * fact (n-1)

CS5205 Haskell 15

Polymorphic TypesPolymorphic Types

• Support tpes that are universally quantified in some way over all types.

• 8 c. [c] denotes a family of types, for every type c, the type of lists of c.

• Covers [Integer], [Char], [Integer->Integer], etc.

• Polymorphism help support reusable code, e.g

length :: [a] -> Integerlength [] = 0length (x:xs) = 1 + length xs

CS5205 Haskell 16

Polymorphic TypesPolymorphic Types

• More examples :

length [1,2,3] ) 2length [‘a’, ’b’, ‘c’] ) 3length [[1],[],[3]] ) 3

• This polymorphic function can be used on list of any type..

head :: [a] -> ahead (x:xs) = x

tail :: [a] -> [a]tail (x:xs) = xs

• Note that head/tail are partial functions, while length is a total function?

CS5205 Haskell 17

Principal TypesPrincipal Types

• An expression’s principal type is the least general type that contains all instances of the expression.

• For example, the principal type of head function is [a]->a, while [b] -> a, b -> a, a are correct but too general but [Integer] -> Integer is too specific.

• Principal type can help supports software reusability with accurate type information.

[Char] <: 8 a. [a] <: 8 a. a

• Some types are more general than others:

CS5205 Haskell 18

User-Defined Algebraic TypesUser-Defined Algebraic Types

• Can also describe a tuple

data Bool = False | Truedata Color = Red | Green | Blue | Violet

• Can describe enumerations:

data Pair = P2 Integer Integerdata Point a = Pt a a

• Pt is a data constructor with type a -> a -> Point a

Pt 2.0 3.1 :: Point FloatPt ‘a’ ‘b’ :: Point CharPt True False :: Point Bool

CS5205 Haskell 19

Recursive TypesRecursive Types

• Two data constructors:

data Tree a = Leaf a | Branch (Tree a) (Tree a)

• Some types may be recursive:

Leaf :: a -> Tree aBranch :: Tree a -> Tree a -> Tree a

• An example function over recursive types:

fringe :: Tree a -> [a]

fringe (Leaf x) = [x]fringe (Branch left right) = (fringe left) ++

(fringe right)

CS5205 Haskell 20

Type SynonymsType Synonyms

• Type synonyms do not define new types, but simply give new names for existing types.

type String = [Char]type Person = (Name, Address)type Name = Stringdata Address = None | Addr String

• It is possible to provide new names for commonly used types

type AssocList a b = [(a,b)]

• Their use can improve code readibility.

CS5205 Haskell 21

Built-In TypesBuilt-In Types

• Tuples are also built-in.

data Char = ‘a’ | ‘b’ | …data Int = -65532 | … | -1 | 0 | 1 | …. | 65532 data Integer = … | -2 | -1 | 0 | 1 | 2 | …

• They are not special:

data (a,b) = M2(a,b)data (a,b,c) = M3(a,b,c)data (a,b.c.d) = M4(a,b,c,d)

• List type uses an infix operator:

data [a] = [] | a : [a]

[1,2,3] is short hand for 1 : (2 : (3 : []))

CS5205 Haskell 22

List ComprehensionList Comprehension

• Captures a list of all f x where x is drawn from xs. More than one generators allowed:

[f x | x <- xs]

• List comprehension is a useful shorthand for building list data structures. Similar to set comprehension notation.

[(x,y) | x <- xs, y <- ys]

• Guards are also permitted. Example :

quicksort [] = []quicksot (x:xs) = quicksort [y | y<-xs, y<x]

++ [x]++ quicksort [y | y<-xs, y>=x]

CS5205 Haskell 23

Arithmetic Sequences and StringsArithmetic Sequences and Strings

• String is just a shorthand for list of chars.

• For example, the string “hello” is a shorthand for[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]. Advantage : standard polymorphic list functions can be used to operate on strings.

[1 .. 10] ) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10][1, 3 .. 10] ) [1, 3, 5, 7, 9][1, 4 .. ] ) [1, 4, 7, 10, ….. (infinite)

• Special syntax for arithmetic progression sequence:

“hello” ++ “ world” ) “hello world”

CS5205 Haskell 24

FunctionsFunctions

• The first version allows a function to be returned as result after applying a single argument.

add2 :: (Integer,Integer) -> Integeradd2(x,y) = x+y

• Functions can be written in two main ways:

add :: Integer -> Integer -> Integeradd x y = x+y

inc :: Integer -> Integerinc = add 1

• The second version needs all arguments. Same effect requires a lambda abstraction:

inc = \ x -> add2(x,1)

CS5205 Haskell 25

FunctionsFunctions

• Such higher-order function aids code reuse.

• Functions can also be passed as parameters. Example:

map :: (a->b) -> [a] -> [b]map f [] = []map f (x:xs) = (f x) : (map f xs)

• Alternative ways of defining functions:

add = \ x -> \ y -> x+yadd = \ x y -> x+y

map (add 1) [1, 2, 3] ) [2, 3, 4]map add [1, 2, 3] ) [add 1, add 2, add 3]

CS5205 Haskell 26

Infix OperatorInfix Operator

• Another example is function composition.

• Infix operators are distinguished entirely by symbols:

(++) :: [a]-> [a] -> [a][] ++ ys = ys(x:xs) ++ ys = x : (xs ++ ys)

• Infix operator can be coerced to prefix by bracketing, e.g. map (+) [1,2,3]

(.) :: (b->c) -> (a->b) -> (a->c)f . g = \ x -> f (g x)

• Alphanumeric named function can be made infix too, e.g. a ‘add’ b x ‘elem’ xs

CS5205 Haskell 27

SectionsSections

• Other examples:.

• Infix operators can also be partially applied:

(x+) ´ \y -> x+y(+y) ´ \x -> x+y(+) ´ \x y -> x+y

• These are syntactic sugar for programming conveniences.

inc = (+ 1)add = (+)

CS5205 Haskell 28

Lazy (non-strict) FunctionsLazy (non-strict) Functions

• They can be avoided by lazy evaluation, e.g.

bot = bot

• Errors such as 3/0 and non-termination such as bot are ill-defined expressions.

second bot 3 ) 3

second :: a -> b -> bsecond x y = y

• Internally, arguments are converted to thunks (delayed expressions) that are only evaluated on demand. Each thunk is similar to a function with nullary argument.

CS5205 Haskell 29

Infinite Data StructuresInfinite Data Structures

• They can be accessed in a finite way using functions, such as take, head, etc.

ones = 1 : onesnumsFrom n = n : numFrom (n+1)squares = map (^ 2) (numsFrom 0)

• Data constructors are non-strict too which allow us to define infinite data structures, such as :

take 5 squares ) [0, 1, 4, 9, 16]head ones ) 1

• Non-termination can still occur if we try to access all its components.

sum squares ) … (non-terminating!)

CS5205 Haskell 30

Infinite Data StructuresInfinite Data Structures

• Another example of circularity is.

ones = 1 : ones

• Circular structures are space efficient :

fib = 1 : 1 : [a+b | (a,b) <- zip fib (tail fib)]

zip (x:xs) (y:ys) = (x,y) : (zip xs ys)zip xs ys = []

• This circular fibonacci function can be computed very efficiently.

CS5205 Haskell 31

Error FunctionError Function

• This can be used in many places to highlight erroneous computation:

error :: String -> a

• There is a special built-in polymorphic function which returns a value that can be accepted by all types:

head (x:xs) = xhead [] = error “argument cannot be []”

• This can be viewed as an uncaught exception, and is type consistent due to polymorphism.

CS5205 Haskell 32

Pattern-MatchingPattern-Matching

• Formal parameters are also patterns though irrefutable.

head (x : _) = xtail (_ : xs) = xs

• Pattern matching is applicable to constructor of any type whether user-defined or built-in :

• As-pattern provide a convenient way to reuse a pattern.

f (x:xs) = x : (x:xs)

f s@(x:xs) = x : s

• Wild-card _ match a value we care nothing about.

contrived :: ([a],Char,(Int,Float),String, Bool) -> Bool

contrived ([], ‘b’, (1, 2.0), “hi”, True) = False

CS5205 Haskell 33

Pattern-Matching SemanticsPattern-Matching Semantics

• Pattern-matching occurs “top-down, left-to-right”.

sign x | x > 0 = 1| x==0 = 0| x<0 = -1

• Pattern matching either fail, succeed or diverge. A successful match will bind the formal parameters in the pattern.

• Top-level patterns may have boolean guards.

[0,bot] matched against pattern [1,2] fails

[bot,0] matched against pattern [1,2] diverges

CS5205 Haskell 34

Case ExpressionCase Expression

take m ys = case (m,ys) of(0,_) -> [](_,[]) -> [](n,x:xs) -> x : take (n-1) xs

if e1 then e2 else e3

´ case e1 of True -> e2 False -> e3

• Case expression is the basic language construct to implement pattern-matching.

CS5205 Haskell 35

Lazy PatternsLazy Patterns

• Another example is circular code with a lazy as-pattern

client init resps = init : client (next (head resps)) (tail resps)

• Irrefutable patterns allow us to support infinite recursive procedures with no base cases.

client init ~(r:rs) = init : client (next r) rs

fib = 1 : 1 : [a+b | (a,b) <- zip fib (tail fib)]

fib@(1:tfib) = 1 : 1 : [a+b | (a, b) <- zip fib tfib]

CS5205 Haskell 36

Lexical Scoping Lexical Scoping

• For scope bindings over guarded expressions, we require a where construct instead:

let y = a+bf x = (x+y)/y

in f c + f d

• Local variables can be created by let construct to give nested scope for the name space. Example:

f x y | x>z = …| y==z = …| y<z = ….

where z=x*x

CS5205 Haskell 37

Layout Rule Layout Rule

is being parsed as:

• Haskell uses two dimensional syntax that relies on declarations being “lined-up columnwise”

• Rule : Next character after keywords where/let/of determines the starting columns for declarations. Starting after this point continues a declaration, while starting before this point terminates a declaration.

let y = a+bf x = (x+y)/y

in f c + f d

let { y = a+b; f x = (x+y)/y }

in f c + f d