Functional Programming Lecture 10 - type checking.

Slides:



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

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.
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Type checking © Marcelo d’Amorim 2010.
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.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
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.
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,
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.
1 COMP313A Programming Languages Functional Programming (7)
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Operators, Functions and Modules1 Pattern Matching & Recursion.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
Overview of the Haskell 98 Programming 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.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
0 INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
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.
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 Languages
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
Haskell Chapter 2.
A lightening tour in 45 minutes
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Programming Languages
ML’s Type Inference and Polymorphism
PROGRAMMING IN HASKELL
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
Records and Type Classes
PROGRAMMING IN HASKELL
ML’s Type Inference and Polymorphism
PROGRAMMING IN HASKELL
ML’s Type Inference and Polymorphism
PROGRAMMING IN HASKELL
Records and Type Classes
Presentation transcript:

Functional Programming Lecture 10 - type checking

Strong Typing Haskell is a strongly typed programming language. This means that every value has a defined type, and we can check that type before evaluating the value. Benefits - many errors are caught before run-time - use types to categorise and lookup functions A type may be monomorphic, polymorphic, or an instance of a type class. Note: we can always make a monomorphic instance of a polymorphic type. E.g. length :: [a] -> Int (polymorphic) length Int :: [Int] -> Int (monomorphic)

Monomorphic Type Checking Expression:a literal, a variable, a constant, a list, a tuple, or a function application. Inference rules for type checking a list: x:: t xs :: [t] _________ x:xs :: [t] [] :: [t] In words, If x has type t and xs is a list of t x:xs then result has type list of t Examples: Show [True, False] :: [Bool] Show [3,True] is not well-typed.

Inference rule for type checking a tuple: x 1 :: t 1 x n :: t n (x 1, …., x n ) :: (t 1, …., t n ) In words, if each component x i has type t i (x 1, …., x n ) then result has type tuple (t 1, …., t n ) Examples: Show (True,3) :: (Bool,Int)

Inference rule for type checking a function application: f :: s -> t e :: s f e :: t In words, f has type s -> t e has type s f e result has type t Examples: Show 2 + Int 3 :: Int Show (ord c) + Int 3 :: Int Show (ord c) + Int True is not well-typed.

Type Checking Function Definitions To check a function f :: t 1 -> t 2 -> … -> t k -> t when there are several cases, i.e. f p 1 p 2 … pk | g 1 = e 1 | g 2 = e 2 … | g l = e l We need to check that - each of the guards g i is of type Bool - the value of each e i is of type t - each pattern p j matches some element of type t j. E.g. pattern p:q matches an element of type [t] when p matches an element of t and q matches an element of [t].

Type Checking Function Definitions Example f :: Int -> [Int] -> Int f a (x:xs) | (a>x) = a + x | (a ==x) = x Note: | (x >xs) = a type error Can we deduce type of f? Let (x:xs):: [b], then x::b. a has same type as x, so a::b. b must be numeric, assume b=Int. f:: t1 -> t2 -> t3 p 1 =a, so p 1 matches an element of Int p 2 =x:xs so p 2 matches an element of [b] = [Int] e 1 =a + x, a has type Int So f :: Int -> [Int] -> Int

Polymorphic Type Checking Type checking a polymorphic type is the same as solving type constraints. Some examples: Consider f (x,y) = (x,[a.. y]) a pair a character - the argument to f is a pair - x is completely unconstrained - y is an expression in an enumerated type, starting with a Char, so y must have type Char - [a.. y] has type [Char] - the rhs is a pair. Therefore, f :: (a,Char) -> (a, [Char])

Consider g (m,zs) = m + length zs a pair adds values [b] -> Int - m must have a numeric type - zs must be a list type, call it [b] - length zs :: Int, so this forces m to have type Int - since m has type Int, the rhs must have type Int - we dont know anything else about zs. Therefore, g :: (Int,[b]) -> Int.

Function Composition Two functions can be composed in Haskell, just as in Maths. Consider composition of f1 and f2. f2.f1 a a f1 b f2 c c f1 a = b, f2 b = c. f2.f1 takes in a and outputs c. Definition (f2. f 1) x = f2 (f1 x) -- the output of f1 becomes the input of f2 The type of _._ is _._ :: (b -> c) -> (a -> b) -> (a -> c) type of f2 type of f1 type of f2.f1 -

Function Composition h = f2. f1 (b -> c) (a -> b) the input the output result is function from input to output h :: a -> c

Now consider the type of h = g. f g :: (Int,[d]) -> Int -- rename type var b as d g (m,zs) = m + length zs f :: (e,Char) -> (e, [Char]) -- rename type var a as e f (x,y) = (x,[a.. y]) The type of _._ is _._ :: (b -> c) -> (a -> b) -> (a -> c) g f Therefore the type of g. f is b=(Int, [d]) a=(e,Char) c=Int b=(e,[Char]) We have to unify the two definitions of b: (Int,[d]) = (e,[Char]) Solution: e=Int, d=Char Therefore, h :: a -> c h :: (Int,Char) -> Int -

Unification Unification is the process of matching two terms, that is making substitutions to each term. How do you describe the unification of (e,[Char]) and (Int,[d])? (Bool, [Char]) (Int,[Int]) (Char,[Char]) (Int, [Char]) (Int,[Bool]) (a->a, [Char]) (Int, [[c]]) … … (e, [Char]) (Int,[d]) The unification of two expressions is the intersection of the sets of types of those two expressions. This intersection is given by most general common instance of the two expressions. The unifier is the subsitution: e = Int, d = Char.

Unification Not all expressions can be unified. For example, consider [Int] -> [Int] and a -> [a]. These constraints are inconsistent, the two expressions cannot be unified. [Int] -> [Int] no solution a -> [a] a=Int,a=[Int]

Type checking polymorphic function application Assume f :: s -> t e :: u f e has type has type s -> t u unify s and u by s s -> t s

Type checking polymorphic function application Example: map :: (a -> b) -> [a] -> [b] ord :: Char -> Int What is type of map ord? Must unify a->b and Char -> Int. So, a=Char, b=Int. map ord :: [a] -> [b] map ord (a ->b) -> [a]->[b] Char -> Int s -> t u (Char -> Int) ->[Char] ->[Int] s -> t Therefore map ord :: [a] -> [b] map ord :: [Char]-> [Int] Note: the result type may still contain type variables.

Resolving Type Ambiguity A final note on types. Assume you have defined: f :: Show a => [a] -> String -- f converts lists of a to string format, e.g. sets f [] = {} f (x:xs) = { ++ ( g (x:xs)) ++ } where g [x] = show a g x:y:xs = (show a) ++, ++ …. Then f [] will be ambiguous, that is, it wont know which type a to check as a Show type! > f [] ERROR: Unresolved overloading *** type : Show a => [Char] *** expression : print1 [] Solution: > f ([]::String) {} NB. You could force [] to have any list of Show type.