Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Office : S15 06-01 “Language.

Similar presentations


Presentation on theme: "CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Office : S15 06-01 “Language."— Presentation transcript:

1 CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Email : chinwn@comp.nus.edu.sg Office : S15 06-01 “Language Foundation, Applications and Reasoning

2 CS5205Haskell2 Administrative Matters Administrative Matters - mainly via Web-page + IVLE - Reading Materials (mostly online): www.haskell.org Robert 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%)

3 CS5205Haskell3 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

4 CS5205Haskell4 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)

5 CS5205Haskell5 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 ? programmer productivity

6 CS5205Haskell6 Type System Type 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

7 CS5205Haskell7 Some Applications Some Applications Hoogle – search in code library Darcs – distributed version control Programming user interface with arrows.. How to exploit multicore and parallelism?

8 CS5205Haskell8 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

9 CS5205Haskell9 CS5205: Foundations in Programming Languages Haskell A pure lazy functional language that embodies many innovative ideas in language design.

10 CS5205Haskell10 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

11 CS5205Haskell11 Values and Types Values 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])

12 CS5205Haskell12 Functions and its Type Functions 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.

13 CS5205Haskell13 Expression Evaluation Expression Evaluation Expressions can be evaluated, and reduced to result form. This can be informally represented using: e 1 ) e 2 A concrete example of this is: inc (inc 3) ) 5

14 CS5205Haskell14 Expression-Oriented Expression-Oriented Conditional expressions are of the form; if e1 then e2 else e3 An example function: Pattern-matching also possible fact :: Integer -> Integer fact n=if n=0 then 1 else n * fact (n-1) fact 0 = 1 fact n= n * fact (n-1)

15 CS5205Haskell15 Polymorphic Types Polymorphic 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] -> Integer length [] = 0 length (x:xs)= 1 + length xs

16 CS5205Haskell16 Polymorphic Types Polymorphic Types More examples : length [1,2,3] ) 2 length [‘a’, ’b’, ‘c’] ) 3 length [[1],[],[3]] ) 3 This polymorphic function can be used on list of any type.. head :: [a] -> a head (x:xs)= x tail :: [a] -> [a] tail (x:xs)= xs Note that head/tail are partial functions, while length is a total function?

17 CS5205Haskell17 Principal Types Principal 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:

18 CS5205Haskell18 User-Defined Algebraic Types User-Defined Algebraic Types Can also describe a tuple data Bool = False | True data Color= Red | Green | Blue | Violet Can describe enumerations: data Pair = P2 Integer Integer data Point a = Pt a a Pt is a data constructor with type a -> a -> Point a Pt 2.0 3.1 :: Point Float Pt ‘a’ ‘b’:: Point Char Pt True False:: Point Bool

19 CS5205Haskell19 Recursive Types Recursive Types Two data constructors: data Tree a = Leaf a | Branch (Tree a) (Tree a) Some types may be recursive: Leaf :: a -> Tree a Branch:: 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)

20 CS5205Haskell20 Type Synonyms Type 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 = String data 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.

21 CS5205Haskell21 Built-In Types Built-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 : []))

22 CS5205Haskell22 List Comprehension List 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 =x]

23 CS5205Haskell23 Arithmetic Sequences and Strings Arithmetic 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”

24 CS5205Haskell24 Functions Functions The first version allows a function to be returned as result after applying a single argument. add2 :: (Integer,Integer) -> Integer add2(x,y) = x+y Functions can be written in two main ways: add :: Integer -> Integer -> Integer add x y = x+y inc :: Integer -> Integer inc = add 1 The second version needs all arguments. Same effect requires a lambda abstraction: inc = \ x -> add2(x,1)

25 CS5205Haskell25 Functions Functions 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+y add= \ x y -> x+y map (add 1) [1, 2, 3] ) [2, 3, 4] map add [1, 2, 3] ) [add 1, add 2, add 3]

26 CS5205Haskell26 Infix Operator Infix 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’ bx ‘elem’ xs

27 CS5205Haskell27 Sections Sections 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=(+)

28 CS5205Haskell28 Lazy (non-strict) Functions Lazy (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 -> b second 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.

29 CS5205Haskell29 Infinite Data Structures Infinite Data Structures They can be accessed in a finite way using functions, such as take, head, etc. ones= 1 : ones numsFrom 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!)

30 CS5205Haskell30 Infinite Data Structures Infinite 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.

31 CS5205Haskell31 Error Function Error 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)= x head []= error “argument cannot be []” This can be viewed as an uncaught exception, and is type consistent due to polymorphism.

32 CS5205Haskell32 Pattern-Matching Pattern-Matching Formal parameters are also patterns though irrefutable. head (x : _)= x tail (_ : 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

33 CS5205Haskell33 Pattern-Matching Semantics Pattern-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

34 CS5205Haskell34 Case Expression Case 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.

35 CS5205Haskell35 Lazy Patterns Lazy 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]

36 CS5205Haskell36 Lexical Scoping Lexical Scoping For scope bindings over guarded expressions, we require a where construct instead: let y = a+b f 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

37 CS5205Haskell37 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+b f x = (x+y)/y in f c + f d let { y = a+b ; f x = (x+y)/y } in f c + f d


Download ppt "CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Office : S15 06-01 “Language."

Similar presentations


Ads by Google