1 COMP313A Programming Languages Functional Programming (7)

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Functional Programming Lecture 10 - type checking.
Formal Models of Computation Part II The Logic Model
Haskell user defined types data Temp = Cold|Hot|Warm deriving (Show,Eq, Ord, Enum) -- to enable printing to screen -- comparing for equality -- comparison.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
1 Functional Programming Lecture 6 - Algebraic Data Types.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Overview of the Haskell 98 Programming Language
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
© M. Winter COSC 4P41 – Functional Programming Modules in Haskell Using modules to structure a large program has a number of advantages: Parts of.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Do-it-yourself dictionaries in Haskell1 Ingmar Brouns & Lukas Spee.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Chapter SevenModern Programming Languages1 A Second Look At ML.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Functional Programming
Conditional Expressions
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Haskell Chapter 2.
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

1 COMP313A Programming Languages Functional Programming (7)

2 Lecture Outline Type checking –type classes Some Haskell tricks

3 Polymorphism versus Operator Overloading Use the same function name with different types Polymorphism – the same function definition Operator overloading – different function for each different type

4 Polymorphism first (x, y) = x list x = [x]

5 Operator Overloading == Defined for Int and …. One for pairs (n,m) == (p,q) = (n==p) && (m==q)

6 Type Classes in Haskell Underpinned by the notion of overloading Mechanism by which overloaded functions can be given types – why? Elem does not make sense for every possible a How can we still have the flexibility of polymorphic types and ensure type safety Use type classes to do this elem :: a -> [a] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elem x ys

7 elemBool :: Bool -> [Bool] -> Bool elem x [] = False elem x (y:ys) = (x == Bool y) || elemBool x ys elemInt :: Int -> [Int] -> Bool elem x [] = False elem x (y:ys) = (x == Int y) || elemInt x ys We could have: YUK!

8 Type Classes in Haskell Operator Overloading Could instead make the equality function a parameter: But this is too general Instead Where type a is restricted to those types which can be used with equality elem :: (a ->a->Bool) ->a -> [a] -> Bool elem :: a -> [a] -> Bool

9 elemBool :: Bool -> [Bool] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elemBool x ys elemInt :: Int -> [Int] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elemInt x ys elem :: Eq a  a -> [a] -> Bool elem x [] = False elem x (y:ys) = (x == y) || elem x ys context

10 Operator Overloading Think of as: –the equality operator has a set of types defined for it. These are the types which can be used with the equality operator Thus we say: –The definition of elem can be used over all types with equality Type classes are used to give a type to functions like elem.

11 Type Checking and Classes member [] y = False member (x:xs) y = x == y || member xs y Apply the function member to an expression e, whose type is Ord b => [[b]] Eg. A list of lists of integers. Integers have an ordering partial application member e Unify the type expressions giving (without contexts) member :: [[b]] -> [b] -> Bool e::[[b]] The type will be? And member e the type [b] -> Bool

12 Type Checking and Classes But also have to unify the contexts (Eq [b], Ord b) i.e are the set of defined for Eq the same as the set of types defined for Ord Now check and simplify the context –the requirements in a context, ([b], b) can only apply to type variables –need to simplify requirements like Eq [b] [3, 5, 6] == [3, 5, 6] 3==3 & 5==5 & 6 == 6 instance Eq a => Eq [a] where…. – (Eq b, Ord b)

13 (Eq b, Ord b) Class Eq a => Ord a where …. tells us how the class is derived but not the instances Any instance of Ord is an instance of Eq but … so…. What should the unified context be…

14 Ord b member e :: Ord b => [b] -> Bool

15 Assignment hints Some Haskell tricks

16 the cons operator x:rest x:y:rest x:y:z:rest head x head y head z head (drop 1 x) [x,y,z]:rest To get the last ement in a list use “last” To get a list with all but the first element use “tail” tail rest head last rest [["auckland", "pokeno", "sh1"],["pokeno", "tauranga", "sh2"], ["tauranga", "opotiki", "sh2"], ["opotiki", "gisborne", "sh2"], ["gisborne", "napier", "sh2"], [ "napier", "woodville", "sh2"]]

17 How would you get the second last element in a list?

18 To make a list x:rest [x] [x,y,z] etc If x y and z are characters then the type of [x,y,z] is String [x,y,z]:rest [x,y,z] == “fred” A function to make a list out of some argument list x = [x]

19 Sometimes it is easier to just create an auxiliary function which accumulates the result as an argument Sometimes it is easier to build a list in reverse order Take the list “the quick brown fox” process_list text = process_list_aux text “” process_list_aux [] result = process_list_aux x:text result = process_list_aux text : versus ++

20