CS321 Functional Programming 2 © JAS 2005 3-1 Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.

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.
Haskell user defined types data Temp = Cold|Hot|Warm deriving (Show,Eq, Ord, Enum) -- to enable printing to screen -- comparing for equality -- comparison.
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.
10 October 2006 Foundations of Logic and Constraint Programming 1 Unification ­An overview Need for Unification Ranked alfabeths and terms. Substitutions.
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 Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Standard Logical Equivalences
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 =
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 11 - Part IIK. Louden, Programming Languages1 Exercises [’a’,’b’,’c’] (’a’,’b’,’c’) [(False,’0’),(True,’1’)] ([False,True],[’0’,’1’]) [tail,init,reverse]
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
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,
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
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.
1 COMP313A Programming Languages Functional Programming (7)
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Overview of the Haskell 98 Programming Language
Language: Set of Strings
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
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.
Web Science & Technologies University of Koblenz ▪ Landau, Germany Procedural Semantics Soundness of SLD-Resolution.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
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,
3/20/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
© 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.
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
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}
Functional Programming
Programming Languages
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Advanced Functional Programming
Programming Languages
PROGRAMMING IN HASKELL
CSE 341 Section 5 Winter 2018.
Advanced Functional Programming
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Advanced Functional Programming
Presentation transcript:

CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This is different to the philosophy of languages like Pascal where all typing is explicit. The aim in this part of the course is to examine in more detail how the type of any object (function, or expression) can be derived.

CS321 Functional Programming 2 © JAS The basis for a type checking algorithm was worked out by Robin Milner (who used to be here at Swansea) in the mid 1970s. Much of the theoretical background to the type checking algorithm cames from work on the typed λ-calculus, in particular work done by Roger Hindley (who is still here at Swansea). People refer to the type checking algorithm as the Milner or Hindley-Milner type checking algorithm.

CS321 Functional Programming 2 © JAS Consider the following function in Haskell:- len lis = if lis == [] then 0 else 1 + len (tail lis) if:: Bool -> a -> a -> a []:: [b] 0, 1:: Int tail:: [c] -> [c] +:: Int -> Int -> Int lis:: d (==):: e -> e -> Bool len:: f

CS321 Functional Programming 2 © JAS len lis = if (==) lis [] e->e->Boold[b] Bool then 0 Int else (+) 1 len (tail lis) [c]->[c] d f [c] Int->Int->Int Int g Int Bool->Int->Int->Int d Int len::[c]->Int with e=d=[b];d=[c];f=[c]->g;g=Int

CS321 Functional Programming 2 © JAS Consider another example:- oops fn lis = if lis == [] then [] else (fn(head lis)):fn (tail lis) if:: bool->a->a->a (==):: b->b->bool [] 1 :: [c] [] 2 :: [d] (:) :: e->[e]->[e] head :: [f]->f tail :: [g]->[g] fn :: h lis :: i

CS321 Functional Programming 2 © JAS oops fn lis = if (==) lis [] b->b->Bool i [c] then [] [d] else (:) (fn(head lis)) (fn (tail lis)) [f]->f i h f [g]->[g] i h [g] b=i=[c];i=[f];i=[g];h=[g]->kh=f->j; f=[g]=i=[f]

CS321 Functional Programming 2 © JAS A Type Checking Algorithm We have derived type equivalences. This is essentially an example of term unification. This idea can be defined without reference to types. Let us assume that we are given a collection Ops of operators and a collection Vars of variables. The family Terms of terms is then defined:- Terms ::=Vars | (Ops, [Terms,...., Terms]) Thus an "unsugared'' version of the expression:- 1 + x * y is:- (+,[(1,[]),(*,[x,y])])

CS321 Functional Programming 2 © JAS Any function that assigns terms to variables is called a substitution. Every such function may be extended to act upon terms. If σ is a substitution then Σ is a function Terms -> Terms defined:- Σ(v) = σ(v) forall v є Vars Σ(o,[t 1,..t n ]) = (o,[Σ(t 1 ),..Σ(t n )]) forall є Ops Suppose σ assigns " x+y '' to " x '' and " 0 '' to " y '' then Σ(1+x*y) = 1+(x+y)*0

CS321 Functional Programming 2 © JAS Two substitutions σ and t may be combined:- τ;σ(v) = (Σ. τ)(v) = Σ(τ(v)) forall v є Vars A unifier for any two terms, t and t', is any substition σ such that Σ(t) = Σ(t'). The most general unifier of two terms is the unifier σ 0 of these terms such that for any other unifier σ 1 there exists a unification τ such that σ 1 = σ 0 ; τ. It can be proved that for any pair of unifiable terms a most general unifier (unique up to renaming of variables) exists. Term unification is the process of finding the most general unifier.

CS321 Functional Programming 2 © JAS How do we find the most general unifier, mgu, of two terms, t and t‘? Need to consider both variables and operators. If t is a variable that does not occur in t' then mgu(t,t') is the substitution [t'/t] of term t' for t. The reverse is true if t' is a variable that does not exist in t. If t is a variable that does occur in t' there is no unifier unless t=t'. For operators then it is clear that for two terms no unifier can exist unless the operators are equal. If the operators are equal then a unifier can exist iff the argument lists are equal in length and the arguments are pairwise unifiable by the same substitution.

CS321 Functional Programming 2 © JAS Consider the two terms:- (o,[t1,....,tn]) (o,[t'1,....,t'n]) and let the following substitutions exist σ 1 = mgu (t 1,t' 1 ) σ 2 = mgu (Σ 1 t 2,Σ 1 t' 2 ) σ 3 = mgu (Σ 2 (Σ 1 t 3 ),Σ 2 (Σ 1 t' 3 )) σ n = mgu (Σ n-1 (Σ 2 (Σ 1 t n )),Σ n-1 (Σ 2 (Σ 1 t' n ))) then mgu( t, t‘ ) = σ 1 ; σ 2 ;......;σ n

CS321 Functional Programming 2 © JAS To write a program to perform the type checking we would need a type to represent a type expression: type Tvname = String data Texp = Tvar Tvname | Tcons String [Texp] arrow :: Texp -> Texp -> Texp arrow t1 t2 = Tcons "arrow" [t1,t2] int :: Texp int = Tcons "int" [] cross :: Texp -> Texp -> Texp cross t1 t2 = Tcons "cross" [t1,t2] list :: Texp -> Texp list t = Tcons "list" [t]

CS321 Functional Programming 2 © JAS A subsitution function maps type variable names to type expressions type Subst = Tvname -> Texp We can extend this to apply to type expressions sub_type :: Subst -> Texp -> Texp sub_type phi (Tvar tvn) = phi tvn sub_type phi (Tcons tcn ts) = Tcons tcn (map (sub_type phi) ts) Composition of substitutions is possible scomp :: Subst -> Subst -> Subst scomp sub2 sub1 tvn = sub_type sub2 (sub1 tvn)

CS321 Functional Programming 2 © JAS We define an identity substitution id_subst :: Subst id_subst tvn = Tvar tvn such that sub_type id_subst t = t A delta substitution is one that affects only one variable and all other substitutions can be built from the identity substitution by composition on the left with delta substitutions. delta :: Tvname -> Texp -> Subst delta tvn t tvn' = if tvn == tvn' then t else Tvar tvn'

CS321 Functional Programming 2 © JAS Assume a data type to allows to return success (a Subst ) or failure data Reply a = OK a | FAILURE A unification function will be applied to pairs of Type expressions unify :: Subst->(Texp,Texp)->Reply Subst unify phi ((Tvar tvn),t)= unify phi ((Tcons tcn ts),(Tvar tvn))= unify phi ((Tcons tcn ts),(Tcons tcn' ts')) =

CS321 Functional Programming 2 © JAS Applying type checking to Haskell Need to extend from simple function abstractions and applications to include (for example) Pattern Matching for each case the type of the pattern(s) must be consistent Guards each guard must be of type Bool Type Classes

CS321 Functional Programming 2 © JAS Type Checking Classes – An Example member :: Eq a => [a] -> a -> Bool member []y = False member (x:xs)y = (x == y) || member xs y e :: Ord b => [[b]] member e [a]->a->Bool [[b]] [b] -> Bool [a]=[[b]]; (Eq[b],Ord b) (Eq b, Ord b) Ord b Recall instance Eq => Eq [a]… class Eq a => Ord a… Ord b => [b] -> Bool

CS321 Functional Programming 2 © JAS member [id] Eq a => [a]->a->Bool [a->a] [a->a] not an instance of Eq