CSC324 Principles of Programming Languages

Similar documents
User-Defined Algebraic Data Types

CSC324 Principles of Programming Languages

CS 320: Concepts of Programming Languages

Programming with Math and Logic

Background Type Classes (1B) Young Won Lim 6/14/18

Advances in Programming Languages

Type-indexed functions in Generic Haskell

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

Type system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited)

Background Type Classes (1B) Young Won Lim 6/28/18

Tree Equality: The Problem

Advanced features of Functional Programming (Haskell)

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

Lecture 4: Higher Order Functions

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Haskell Overview III (3A) Young Won Lim 10/4/16

Structural polymorphism in Generic Haskell

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Type Processing by Constraint Reasoning

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS 457/557: Functional Languages

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Topic 9: Type Checking

Topic 9: Type Checking

Monad (1A) Young Won Lim 6/26/17

Topic 7: Algebraic Data Types

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Overloading, Type Classes, and Algebraic Datatypes

INTRODUCTION TO HASKELL

Haskell Type Constraints Unleashed

Programming Languages Fall 2013

Introduction to Functional Programming in Haskell 1 / 56

The Algebra of Programming in Haskell

Programming Language Concepts, CS2104 Lecture 7

CSCE 314 Programming Languages

CS 360: Programming Languages Lecture 12: More Haskell

Polymorphism Overview (1A) Young Won Lim 2/20/18

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Homework 1: Functional Programming, Haskell

Logic - CM0845 Introduction to Haskell

Programming Paradigms

Functional Logic Programming Language Curry

Standard prelude. Appendix A. A.1 Classes

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Monad (3A) Young Won Lim 8/9/17

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

Haskell An Introduction

Giving Haskell a Promotion

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Haskell Overview II (2A) Young Won Lim 8/9/16

All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

A general introduction to Functional Programming using Haskell

College Functors, Applicatives

Course year Typeclasses and their instances

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Lecture 19: Functions, Types and Data Structures in Haskell

CS558 Programming Languages

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic?

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)

Lecture 2: List algorithms using recursion and list comprehensions

Comonads, Applicative Functors, Monads

Currying fun f x y = expression;

Algebraic Types. Chapter 14 of Thompson

Introduction to Programming Using Java (98-388)

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Type Conversion. and. Statements

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CSC Java Programming, Fall Java Data Types and Control Constructs

Haskell Making Our Own Types and Typeclasses

Haskell Monads CSC 131. Kim Bruce

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Programming in Haskell Aug-Nov 2015

COM2001 Advanced Programming Techniques. Mike Stannett Department of Computer Science The University of Sheffield

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records

This exam is open book. Each question is worth 3 points.

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

JAVA MOCK TEST JAVA MOCK TEST II

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Box-and-arrow Diagrams

Programming in C and C++

Haskell Types COMP360

CS558 Programming Languages

Maybe Monad (3B) Young Won Lim 12/21/17

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Constructors.

CIS 194: Homework 5. Due Friday, October 3, Rings. No template file is provided for this homework. Download the

Maybe Monad (3B) Young Won Lim 1/3/18

CSci 450: Org. of Programming Languages Overloading and Type Classes

Transcription:

CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 21, 2018

Last Class Types terminology Haskell s type system Currying Defining types Value constructors Algebraic data types Polymorphic types, type variables Type constructors (e.g. Maybe, []) Generic polymorphism and constraints

Today Type classes Ad hoc polymorphism

Type Classes A type class defines one or more functions A type can be made a member of the type class by implementing the functions for that type Think about a type class as similar to a Java interface; types implement the interface by implementing the required functions

The Show Type Class > 5 5 > "hi" "hi" > data Point = Point Float Float > Point 3 4 error: No instance for (Show Point) Point is not a member of the type class Show!

Automatically Deriving Take 1 > data Point = Point Float Float deriving (Show) > Point 3 4 Point 3.0 4.0 It works, but using deriving doesn t give us control over how our values are shown.

The Show Type Class: Definition class Show a where show :: a -> String So, if we implement function show for a type, then that type is a member of type class Show.

Using instance Take 2 instance Show Point where show (Point x y) = "(" ++ (show x) ++ ", " ++ (show y) ++ ")"

The show Function show is interesting because it has multiple implementations, one for each instance of Show! > :t show show :: Show a => a -> String The Show a is a type class constraint It means that a is required to be a type that is an instance of Show a is called a constrained type variable

Ad Hoc Polymorphism A function is ad hoc polymorphic if its behaviour depends on the type of its parameters e.g. show is ad hoc polymorphic: what it does depends on the type of its parameter

Ad Hoc Polymorphism in Java Method overloading public int f(int n) { return n + 1; } public void f(double n) { // different return type System.out.println(n); } public String f(int n, int m) { // two parameters return String.format("%d %d", n, m); }

More Type Classes -- Eq supports == and /= > :t (==) (==) :: Eq a => a -> a -> Bool -- Ord supports <, <=, >, >= > :t (<) (<) :: Ord a => a -> a -> Bool -- Read supports read, which converts from strings > :t read read :: Read a => String -> a > read "5" :: Int 5

Numbers > length [1, 2, 3, 4] * 2 8 > length [1, 2, 3, 4] / 2???

Numbers... > a = 5 > b = 10.0 > a + b 15.0 > c = 5 :: Int > d = 10.0 > c + d???

Numbers... > a = 5 > b = 10.0 > a + b 15.0 > c = 5 :: Int > d = 10.0 > c + d??? What s going on with these examples?

Numeric Type Classes The main numeric class is called Num It has an Integral subclass for integers It has a Fractional subclass for non-integral numbers > :t (*) -- works with any numeric type class (*) :: Num a => a -> a -> a > :t (/) -- works only with Fractional type classes (/) :: Fractional a => a -> a -> a

Numeric Literals > :t 1 1 :: Num a => a > :t 1.0 1.0 :: Fractional p => p These are polymorphic constants! They take on the type required by the type context e.g. in 1 + 2.3, 1 will be taken as a Fractional

Higher-Order Type Classes map :: (a -> b) -> [a] -> [b] There is a function fmap that maps over many different types, not just lists > fmap (+ 1) (Just 4) Just 5 > fmap (+ 1) Nothing Nothing > fmap even [1, 2, 3] [False,True,False]

Functors A functor is a type class that supports mapping class Functor f where -- f is [], Maybe, etc. fmap :: (a -> b) -> f a -> f b The a -> b function does the mapping f a is the functor type constructor applied to type a e.g. Maybe Int f b is the functor type constructor applied to type b e.g. Maybe Int again, or Maybe String, etc.

Example: BTree as Functor data BTree a = Null Node a (BTree a) (BTree a) deriving (Show) instance Functor BTree where -- fmap :: (a -> b) -> BTree a -> BTree b fmap f Null = Null fmap f (Node item left right) = Node (f item) (fmap f left) (fmap f right)

Exercise: Point as Functor data Point a = Point a a Make Point an instance of Functor Mapping a function over Point p should map the function over each of its two coordinates