CS302: Paradigms of Programming Functional Programming with Haskell - - PowerPoint PPT Presentation

cs302 paradigms of programming functional programming
SMART_READER_LITE
LIVE PREVIEW

CS302: Paradigms of Programming Functional Programming with Haskell - - PowerPoint PPT Presentation

CS302: Paradigms of Programming Functional Programming with Haskell (Cont.) Manas Thakur Spring 2020 The best way to begin (and even finish!) 2 Types types types What is a type? What is it useful for? What is type checking? Di


slide-1
SLIDE 1

CS302: Paradigms of Programming Functional Programming with Haskell (Cont.)

Manas Thakur

Spring 2020

slide-2
SLIDE 2

The best way to begin (and even finish!)

2

slide-3
SLIDE 3

Types types types

  • What is a type?
  • What is it useful for?
  • What is type checking?
  • Difference between a strongly and a weakly typed language?
  • Why shouldn’t we always use the most general type?
  • Static vs dynamic typing?
  • Untyped languages?
  • Haskell tries to offer the best of all worlds: Type inference!

3

Duck typing!
 If it walks like a duck
 and it quacks like a duck,
 then it must be a duck.

slide-4
SLIDE 4

Abstract Data Types

  • Type with operations
  • What’s abstract about it?
  • Hides the representation details.
  • Recall our Point example from Lab2.
  • Definition of Bool in Haskell:

4

data Bool = False | True

Data declaration Data constructors

slide-5
SLIDE 5

Examples of ADTs

  • What are constructors in OO languages?
  • Check the types of Circle and Rectangle.
  • Now you can use these types in functions:

5

data Shape = Circle Float Float Float | Rectangle Float Float Float Float area :: Shape -> Float area (Circle _ _ r) = pi * r * r area (Rectangle ...

  • Why won’t area :: Circle -> Float work?
  • Same reason why foo :: True -> Int won’t work!

Notice
 pattern matching! Named constructors!

slide-6
SLIDE 6

Type Parameters

  • Maybe is a very useful type for handling failures.

6

data Maybe a = Nothing | Just a

Type parameter

head :: [a] -> a head [] = error “Empty list” head (x:_) = x safeHead :: [a] -> Maybe a safeHead [] = Nothing safeHead (x:_) = Just x safediv :: Int -> Int -> Maybe Int safediv _ 0 = Nothing safediv m n = Just (m `div` n) Can’t return this!

slide-7
SLIDE 7

Type Classes

7

  • Check the type of Haskell’s built-in sort function:

> import Data.List > :t sort > sort :: Ord a => [a] -> [a]

  • It sorts lists containing elements of type a such that a belongs

to the type class Ord.

  • What’s the problem with the following types for +:

(+) :: Int -> Int -> Int (+) :: Float -> Float -> Float (+) :: a -> a -> a

Too specific Too general

  • We would like to define (+) for only number-like types.
slide-8
SLIDE 8

Type Classes (Cont.)

8

  • Check the type of + now:
  • Here is the declaration of the type class Num from Haskell:

> :t (+) > + :: Num a => a -> a -> a class (Eq a, Show a) => Num a where (+),(-),(*) :: a -> a -> a negate :: a -> a abs, signum :: a -> a fromInteger :: Integer -> a

  • Class Num is a subclass of both Eq and Show.
  • +, -, *, negate, … are predefined functions in Num.

a is a type variable

slide-9
SLIDE 9

Recursive Types

  • What does this type describe?

9

data Nat = Zero | Succ Nat

  • Does this remind of something from Lab1?
  • Church numerals!

nat2int :: Nat -> Int nat2int Zero = 0 nat2int (Succ n) = 1 + nat2int n add :: Nat -> Nat -> Nat add m n = int2nat (nat2int m + nat2int n) CQ

slide-10
SLIDE 10

The best way to finish (and even begin!)

10

slide-11
SLIDE 11

On roll for the last week

11

  • 1. Type Inference
  • 2. Proving Program


Properties

  • 3. Monads

T-Week