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

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

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

Haskell Overview IV (4A) Young Won Lim 10/13/16

Haskell Overview IV (4A) Young Won Lim 10/24/16

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

GHCi: Getting started (1A) Young Won Lim 5/26/17

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

Haskell-Tutorial. Damir Medak Gerhard Navratil. Institute for Geoinformation Technical University Vienna. February 2003

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

Monad Background (3A) Young Won Lim 11/20/17

GHCi: Getting started (1A) Young Won Lim 6/3/17

Lecture 19: Functions, Types and Data Structures in Haskell

GHCi: Getting started (1A) Young Won Lim 5/31/17

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

Libraries (1A) Young Won Lim 6/5/17

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

Monad Background (3A) Young Won Lim 11/8/17

Advanced features of Functional Programming (Haskell)

Monad Background (3A) Young Won Lim 10/5/17

Monad Background (3A) Young Won Lim 11/18/17

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

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

A Sudoku Solver Pruning (3A)

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

State Monad (3D) Young Won Lim 9/25/17

User-Defined Algebraic Data Types

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

Set Haskell Exercises

Background Constructors (1A) Young Won Lim 7/9/18

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

CSC324 Principles of Programming Languages

Background Constructors (1A) Young Won Lim 1/22/18

Overloading, Type Classes, and Algebraic Datatypes

Background Constructors (1A) Young Won Lim 7/5/18

Programming Languages Fall 2013

CS 320: Concepts of Programming Languages

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

Tree Equality: The Problem

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

Background Operators (1E) Young Won Lim 7/7/18

Type Processing by Constraint Reasoning

Background Constructors (1A) Young Won Lim 6/30/18

CSc 372 Comparative Programming Languages

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

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Side Effects (3A) Young Won Lim 1/13/18

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

CS 440: Programming Languages and Translators, Spring 2019 Mon

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

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

CS 320: Concepts of Programming Languages

Advances in Programming Languages

Course year Typeclasses and their instances

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

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

Side Effects (3B) Young Won Lim 11/20/17

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Side Effects (3B) Young Won Lim 11/23/17

Side Effects (3B) Young Won Lim 11/27/17

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Haskell An Introduction

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Typed Racket: Racket with Static Types

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Accessibility (1A) Young Won Lim 8/22/13

Background Expressions (1D) Young Won Lim 7/7/18

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

A general introduction to Functional Programming using Haskell

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Haskell through HUGS THE BASICS

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

State Monad Example (3H) Young Won Lim 2/16/18

Haskell Making Our Own Types and Typeclasses

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

Programming Paradigms, Fall 06

CS 360: Programming Languages Lecture 12: More Haskell

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Pointers (1A) Young Won Lim 11/1/17

An introduction introduction to functional functional programming programming using usin Haskell

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

Haskell Types COMP360

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Methods (2A) Young Won Lim 10/14/14

State Monad (3D) Young Won Lim 8/31/17

Functions (4A) Young Won Lim 5/8/17

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

The Typed Racket Guide

Exercise 1 ( = 24 points)

CSE 3302 Programming Languages Lecture 8: Functional Programming

Methods (2A) Young Won Lim 12/10/14

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Applications of Pointers (1A) Young Won Lim 12/26/17

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

Introduction to Haskell

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Transcription:

(3A)

Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

Based on Haskell Tutorial, Medak & Navratil ftp://ftp.geoinfo.tuwien.ac.at/navratil/haskelltutorial.pdf Yet Another Haskell Tutorial, Daume https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf 3

Type Inference Prelude> 7 :: Int 7 Prelude> 7 :: Double 7.0 usually don't have to declare types (type inference) to declare types, use :: to do it. https://wiki.haskell.org/learn_haskell_in_10_minutes 4

Type Information Prelude> :t False False :: Bool Prelude> :t 'A' 'A' :: Char Prelude> :t "Hello, world" "Hello, world" :: [Char] Print type information https://wiki.haskell.org/learn_haskell_in_10_minutes 5

Type Classes Prelude> :t 42 42 :: (Num t) => t Prelude> :t 42.0 42.0 :: (Fractional t) => t Prelude> :t gcd 15 20 gcd 15 20 :: (Integral t) => t 42 can be used as any numeric type 42.0 can be any fractional type gcd 15 20can be any integral type class constraint the type t is constrained by the context (Num t), (Fractional t), (Integral t) the types of t must belong to the Num / Factional / Integra type class https://wiki.haskell.org/learn_haskell_in_10_minutes 6

Instances Num instances Integral instances Int an integer with at least 30 bits of precision. Integer an integer with unlimited precision. Float a single precision floating point number. Double a double precision floating point number. Rational a fraction type, with no rounding error. Fractional instances https://wiki.haskell.org/learn_haskell_in_10_minutes 7

Type Class a type class definition: specifying a set of functions or constants, together with their respective types, Like the Interface in Java that must be implemented for every type that is belonged to the type class https://en.wikipedia.org/wiki/type_class 8

Type Class Definition the type class Eq is intended to contain types that have implementations of equality (==), (/=) functions class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool a type a has an instance of the class Eq if there is an (overloaded) operation == and /= defined. a type a belongs to the type class Eq if (==) and (/=) functions are defined https://en.wikipedia.org/wiki/type_class 9

Instance of a Class type class Eq type a class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool a type a can be an instance of the class Eq if there is an (overloaded) operation == and /= defined. The type Integer is an instance of the class Eq, whose method == is defined The type Float is an instance of the class Eq, whose method == is defined https://en.wikipedia.org/wiki/type_class 10

Instance Declaration class Eq a where type class type (==) :: a -> a -> Bool Eq a instance Eq Integer where x == y = x `integereq` y type class Eq Eq instance Integer Float instance Eq Float where x == y = x `floateq` y https://en.wikipedia.org/wiki/type_class 11

Default Method class Eq a where (==), (/=) :: a -> a -> Bool x /= y = not (x == y) If a method is not defined in an instance declaration, then the default implementation defined in the class declaration, if it exists, is used instead. overloaded method definition The default definition can be overloaded in an instance declaration https://en.wikipedia.org/wiki/type_class 12

Class Constraint elem :: a -> [a] -> Bool the function elem has the type a -> [a] -> Bool elem :: (Eq a) => a -> [a] -> Bool the type a is constrained by the context (Eq a) the types of a must belong to the Eq type class => : called as a 'class constraint' https://en.wikipedia.org/wiki/type_class 13

Class Constraint Example elem function definition which determines if an element is in a list elem :: (Eq a) => a -> [a] -> Bool elem y [] = False elem y (x:xs) = (x == y) elem y xs https://en.wikipedia.org/wiki/type_class 14

as Renaming module imports. Like qualified and hiding, as is not a reserved word but may be used as function or variable name. import qualified Data.Map as M main = print (M.empty :: M.Map Int ()) https://wiki.haskell.org/keywords#as 15

Enumerated Data Types Type Constructor Data Constructor data Bool = True False The type being defined here is Bool, and it has exactly two values: True and False. var1 :: Bool var1 = True True :: Bool False:: Bool data Color = Red Green Blue var2 :: Bool var2 = False var3 :: Color var3 = Red var4 :: Color var4 = Green Red Green Blue :: Color :: Color :: Color var5 :: Color var5 = Blue https://www.haskell.org/tutorial/goodies.html 16

Type Names and Constructor Functions A nullary constructor: takes no arguments A multi-constructor data Bool = True False Type Constructor Data Constructor Type name : Bool The name of new data type Usually it appears in the linea concerning type information ( :: ) Constructor function : True, False Usually it appears in the lines concerning application ( = ) https://www.haskell.org/tutorial/goodies.html 17

Parameterized Data Type Definition A unary constructor (one argument a) A single constructor data Point a = Pt a a Type Constructor Data Constructor v1 :: Point Float v1 = Pt 2.0 3.0 Pt 2.0 3.0 Pt 'a' 'b' Pt True False :: Point Float :: Point Char :: Point Bool v2 v2 v3 v3 :: Point Char = Pt a b :: Point Bool = Pt True False Pt :: a -> a -> Point a https://www.haskell.org/tutorial/goodies.html 18

Solving a list of quadratic equations roots :: (Float, Float, Float) -> (Float, Float) roots (a,b,c) = if d < 0 then error "sorry" else (x1, x2) where x1 = e + sqrt d / (2 * a) x2 = e - sqrt d / (2 * a) d = b * b - 4 * a * c e = - b / (2 * a) real :: (Float, Float, Float) -> Bool real (a,b,c) = (b*b - 4*a*c) >= 0 p1 = (1.0,2.0,1.0) :: (Float, Float, Float) p2 = (1.0,1.0,1.0) :: (Float, Float, Float) ps = [p1,p2] newps = filter real ps rootsofps = map roots newps 19

User defined type example (1) data Polynom = Poly Float Float Float Poly :: Float -> Float -> Float -> Polynom data the keyword Polynom the name of the data type Poly the constructor function (:t Poly) Float the three arguments to the Poly constructor 20

User defined type example (2) data Polynom = Poly Float Float Float roots :: Float Float Float -> (Float, Float) roots a b c = function definition roots2 :: Polynom -> (Float, Float) roots2 (Poly a b c) = function definition (Poly a b c) pattern matching p1, p2 :: Polynom p1 = Poly 1.0, 2.0, 3.0 p2 = Poly 1.0, 3.0, (-5.0) 21

Recursive Data Type Example (1) data Bus = Start Next (Bus) deriving Show mybusa = Start mybusb = Next (Next (Next (Start))) mybusc = Next mybusb plus :: Bus -> Bus -> Bus plus a Start = a plus a (Next b) = Next (plus a b) (Next b) parenthesis for pattern matching testbus :: Bus testbus = plus mybusc mybusb 22

Recursive Data Type Example (2) data Bus = Start Next (Bus) deriving Show mybusa = Start mybusb = Next (Next (Next (Start))) mybusc = Next mybusb = Next ( Next (Next (Next (Start))) ) plus :: Bus -> Bus -> Bus plus a Start = a plus a (Next b) = Next (plus a b) plus mybusc mybusb plus Next (Next (Next (Start))) Next (Next (Next (Next (Start)))) Next (plus Next (Next (Next (Start))) Next (Next (Next (Start))) ) Next (Next (plus Next (Next (Next (Start))) Next (Next (Start)) )) Next (Next (Next (plus Next (Next (Next (Start))) Next (Start) ))) Next (Next (Next (Next (plus Next (Next (Next (Start))) Start )))) Next (Next (Next (Next (Next (Next (Next (Start))) )))) 23

Recursive Data Type Example (3) howfar :: Bus -> Int howfar Start = 0 howfar (Next r) = 1 + howfar r testint :: Int testint = (+) (howfar mybusc) (howfar mybusb) 24

Recursive Data Type Example (4) testint = (+) (howfar mybusc) (howfar mybusb) howfar mybusc howfar Next (Next (Next (Start))) 1 + howfar Next (Next (Start)) 2 + howfar Next (Start) 3 + howfar Start 3 howfar :: Bus -> Int howfar Start = 0 howfar (Next r) = 1 + howfar r howfar mybusb howfar Next (Next (Next (Next (Start)))) 1 + howfar Next (Next (Next (Start))) 2 + howfar Next (Next (Start)) 3 + howfar Next (Start) 4 + howfar Start 5 (+) 3 5 8 (Next r) parens for pattern matching (howfar mybusc) (howfar mybusb) unnecessary parens in function call for readibility 25

Recursive Definition of Lists data [a] = [ ] a : [a] Any type is ok but The type of every element in the list must be the same List = [ ] (a : List) an empty list a list with at least one element [ ] (x:xs) https://www.haskell.org/tutorial/goodies.html 26

List Type Definition : Parameterized & Recursive Parameter data List a = L a (List a ) Empty Bool Int Integer Float Double Char String data List a = L a (List a) Empty Recursive Definition 27

List Type Definition : Constructors Type Constructor Data Constructor with two parameters data List a = L a (List a) Empty Data Constructors Empty L a (List a) L :: a -> List a -> List a Head : Tail : element list 28

List Type Definition : Constructors Type Constructor Data Constructor with two parameters data List a = L a (List a) Empty Head : Tail : element list Data Constructor L L :: a -> List a -> List a data List a = L a (List a) Empty Recursively used 29

List Type Definition : Examples data List a = L a (List a) Empty L1, L2, L3 :: List Integer L1 = Empty L2 = L 1 L1 L3 = L 5 L2 type constructor List L4 = L 1.5 Empty :: List Double type constructor List data constructor L a (List a) 30

Recursive Definition of Tree Data Type data Tree a = Leaf a Branch (Tree a) (Tree a) Recursively used https://www.haskell.org/tutorial/goodies.html 31

Constructor Definition of Tree Data Type data Tree a = Leaf a Branch (Tree a) (Tree a) Type Constructor : Tree a Data Constructor 1: Leaf a Leaf :: a -> Tree a Data Constructor 2: Branch (Tree a) (Tree a) Branch :: Tree a -> Tree a -> Tree a https://www.haskell.org/tutorial/goodies.html 32

Polymorphic Type types that are universally quantified in some way over all types essentially describe families of types (forall a) [a] is the family of types consisting of, for every type a, the type of lists of a. lists of integers (e.g. [1,2,3]) lists of characters (['a','b','c']) lists of lists of integers, etc. [2,'b'] is not a valid example https://www.haskell.org/tutorial/goodies.html 33

Show Class Class Show the instances of Show are those types that can be converted to character strings. (information about the class) The function show show :: (Show a) => a -> String Similar to the tostring() method in Java https://www.haskell.org/tutorial/goodies.html 34

Eq Instance of Tree Type Eq Instance instance (Eq a) => Eq (Tree a) where (Leaf x) == (Leaf y) = x == y (Branch l r) == (Branch l' r') = l == l' && r == r' _ == _ = False ( ) pattern matching type instance instance Eq Integer where x == y = x `integereq` y The type Integer is an instance of the class Eq, whose method == is defined instance Eq Float where x == y = x `floateq` y The type Float is an instance of the class Eq, whose method == is defined https://www.haskell.org/tutorial/stdclasses.html 35

Eq Instance of Tree Type Eq Instance instance (Eq a) => Eq (Tree a) where (Leaf x) == (Leaf y) = x == y (Branch l r) == (Branch l' r') = l == l' && r == r' _ == _ = False ( ) pattern matching type instance type instance Eq a Eq (Tree a) deriving https://www.haskell.org/tutorial/stdclasses.html 36

Derived Instances instance (Eq a) => Eq (Tree a) where (Leaf x) == (Leaf y) = x == y (Branch l r) == (Branch l' r') = l == l' && r == r' _ == _ = False ( ) pattern matching Automatically Derived Eq Instance data Tree a = Leaf a Branch (Tree a) (Tree a) deriving Eq Eq a Eq (Tree a) https://www.haskell.org/tutorial/stdclasses.html 37

Derived Instances instance (Eq a) => Eq (Tree a) where (Leaf x) == (Leaf y) = x == y (Branch l r) == (Branch l' r') = l == l' && r == r' _ == _ = False instance (Ord a) => Ord (Tree a) where (Leaf _) <= (Branch _) = True (Leaf x) == (Leaf y) = x <= y (Branch _) <= (Leaf _) = False (Branch l r) == (Branch l' r') = l == l' && r <= r' l <= l' data Tree a = Leaf a Branch (Tree a) (Tree a) deriving (Eq, Ord) data [a] = [] a : [a] deriving (Eq, Ord) https://www.haskell.org/tutorial/stdclasses.html 38

Subset Polymorphism roots :: (Floating a) => (a, a, a) -> (a, a) https://www.haskell.org/tutorial/goodies.html 39

Parameterized Polymorphism plus :: a -> a -> a, plus :: Int -> Int -> Int, plus :: Rat -> Rat -> Rat, data List a = L a (List a) Empty listlen :: List a -> Int listlen Empty = 0 listlen (L _ list) = 1 + listlen list (L _ list) pattern matching _ : match with any element https://www.haskell.org/tutorial/goodies.html 40