Abstract Data Types Functional Programming and Reasoning Dr Hans - - PowerPoint PPT Presentation

abstract data types
SMART_READER_LITE
LIVE PREVIEW

Abstract Data Types Functional Programming and Reasoning Dr Hans - - PowerPoint PPT Presentation

Abstract Data Types Functional Programming and Reasoning Dr Hans Georg Schaathun University of Surrey Spring 2010 Week 4 Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 1 / 32 Outline Modules 1 Abstract Data Types


slide-1
SLIDE 1

Abstract Data Types

Functional Programming and Reasoning Dr Hans Georg Schaathun

University of Surrey

Spring 2010 – Week 4

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 1 / 32

slide-2
SLIDE 2

Outline

1

Modules

2

Abstract Data Types

3

Specification

4

Data Structures

5

Closing

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 2 / 32

slide-3
SLIDE 3

This session

After this session, you should

be able to use ADT-s in software design be able to implement simple ADT-s in haskell based on a specification

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 3 / 32

slide-4
SLIDE 4

Modules

Outline

1

Modules

2

Abstract Data Types

3

Specification

4

Data Structures

5

Closing

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 4 / 32

slide-5
SLIDE 5

Modules

Modularisation

Divide large problems into smaller ones

solve each subproblem separately

Functions is one way to split large programs But large programs, with many functions,

would still be hard to structure

Modules allow you to split programs into multiple files

Each file is one module

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 5 / 32

slide-6
SLIDE 6

Modules

Naming modules

module Ant where It is good practice to give your modules a name The filename should match the module name

the Ant module resides in Ant.hs

This is necessary to import the module into another

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 6 / 32

slide-7
SLIDE 7

Modules

Sample module

module Ant where import Insect data Ants = Queen Int Float | Worker Float anteater x = x - 1

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 7 / 32

slide-8
SLIDE 8

Modules

Importing modules

import Insect Whenever you need functions from a module

you import it

Access to all functions in the module

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 8 / 32

slide-9
SLIDE 9

Modules

Import Controls

Limited imports

import Ant ( Ants(..) )

Excluding functions import Ant hiding ( anteater ) import Prelude hiding ( words ) Fully qualified names import qualified Ant Ant.anteater Local name

import Insect as Ant importing Ant, but calling it Insect

All of these techniques may be important to avoid name clashes

but you don’t need them for now

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 9 / 32

slide-10
SLIDE 10

Modules

Program versus module

We have not yet written a program

  • nly modules

The hugs command line allows us to test modules

we can evaluate any expression testing any function in the module

A program needs to know what to evaluate

that is an object main in a module Main

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 10 / 32

slide-11
SLIDE 11

Modules

The Main module

A program needs a top-level module Main

module Main where

Defining an expression main

this expression is evaluated when the program is run

The main expression must be an IO type

we will discuss how to do this later (Week 9?)

Such a program can be run as a script

runhugs main.hs

  • r compiled using the Glasgow Haskell Compiler

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 11 / 32

slide-12
SLIDE 12

Modules

Sample Main module

module Main where data Shape = Circle Float | Rectangle Float Float area :: Shape -> Float area (Circle r) = pi*r^2 area (Rectangle x y) = x*y s :: Shape s = Circle 2.3 main = print (area s)

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 12 / 32

slide-13
SLIDE 13

Modules

Software design

Which modules?

Each module should have

a clear purpose clear scope documentation

Main contains the main routine

  • nly definitions particular to this programme

separate modules for reusable functions

Often, one module for each data type

including functions on the data type

Think about how you split the program into modules

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 13 / 32

slide-14
SLIDE 14

Abstract Data Types

Outline

1

Modules

2

Abstract Data Types

3

Specification

4

Data Structures

5

Closing

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 14 / 32

slide-15
SLIDE 15

Abstract Data Types

Encapsulation

A module often contains many definitions

  • nly a few are docuemented

intended for use in importing modules

many auxiliary definitions and features for internal use

This causes problems

information overload for the programmer using the module name clashes with other modules

Good practice: hide all unnecessary definitions

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 15 / 32

slide-16
SLIDE 16

Abstract Data Types

Example

Inventory of a Store

A data type to hold a library catalogue

Possibly type Library = [(Book,Int)]

using your Book type from the exercises storing the number of copies

Do you need to know how the type is implemented?

  • No. You need to know the functions working on the type.

getStock :: Library -> Book -> Int initial :: Library (empty library) addBook :: Library -> Book -> Library

All functionality available via the functions You do not need to know the implementation

data Library = Lib [ (Book,Int) ], or data Library = Lib (Book -> Int)

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 16 / 32

slide-17
SLIDE 17

Abstract Data Types

Example

Inventory of a Store

A data type to hold a library catalogue

Possibly type Library = [(Book,Int)]

using your Book type from the exercises storing the number of copies

Do you need to know how the type is implemented?

  • No. You need to know the functions working on the type.

getStock :: Library -> Book -> Int initial :: Library (empty library) addBook :: Library -> Book -> Library

All functionality available via the functions You do not need to know the implementation

data Library = Lib [ (Book,Int) ], or data Library = Lib (Book -> Int)

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 16 / 32

slide-18
SLIDE 18

Abstract Data Types

Example

Inventory of a Store

A data type to hold a library catalogue

Possibly type Library = [(Book,Int)]

using your Book type from the exercises storing the number of copies

Do you need to know how the type is implemented?

  • No. You need to know the functions working on the type.

getStock :: Library -> Book -> Int initial :: Library (empty library) addBook :: Library -> Book -> Library

All functionality available via the functions You do not need to know the implementation

data Library = Lib [ (Book,Int) ], or data Library = Lib (Book -> Int)

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 16 / 32

slide-19
SLIDE 19

Abstract Data Types

Abstract Datatype

An abstract data type (ADT) is a type which can only be accessed or manipulated using a limited set of clearly defined functions. New functions must be defined in terms of the ‘primitive’ functions No direct access to the data

many things will be impossible

This prevents abuse of the data type

and thus prevents many errors

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 17 / 32

slide-20
SLIDE 20

Abstract Data Types

Defining an ADT

module Library( Library, initial, getStock, addBook ) where We list explicitely which names to export The type Library is exported

its constructors are not

Exported functions (initial, getStock, addBook) gives access to the type This Haskell’s ADT mechanism

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 18 / 32

slide-21
SLIDE 21

Abstract Data Types

Export Controls

Export Controls can be used in different ways ADT-s are special in that

the constructors are hidden

module Library( Library(..), initial, getStock, addBook ) where

(..) means that the constructors should be exported as well this is not an ADT

You can choose (almost) freely what to export

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 19 / 32

slide-22
SLIDE 22

Abstract Data Types

Classes and Instances

Defining an ADT, you will often want to overload standard functions

instance Show Library where

Such instances are always exported Inherent properties of the data type

cannot be hidden

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 20 / 32

slide-23
SLIDE 23

Abstract Data Types

Advantages of ADT-s

Interchangeable

different implementations of the same ADT can be interchanged ... as long as the specification is the same

Delegate the implementation

if you agree on the interface (functions) you know it will work with other modules

Updates with backwards compatibility

add features while continuing to support old ones

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 21 / 32

slide-24
SLIDE 24

Specification

Outline

1

Modules

2

Abstract Data Types

3

Specification

4

Data Structures

5

Closing

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 22 / 32

slide-25
SLIDE 25

Specification

Specification

A specification is a careful description of a program or module

formal specification languages (such as Z) natural language

It defines exactly how the module is intended to work

exactly which functions are supported

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 23 / 32

slide-26
SLIDE 26

Specification

Specification example

Library catalogue

A type Library

storing records of books number of copies per book

Required functions

addBook :: Library -> Book -> Library

add a new book to the library if the book exists already, increase number of copies

initial :: Library

return a Library with no books

getStock :: Library -> Book -> Int

given a book return

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 24 / 32

slide-27
SLIDE 27

Specification

Why use specification

Software projects are big

communication and collaboration essential

Agreeing on specifications

allow independent implementation

One person can implement the ADT Others can write modules using the ADT It is important to be able to program strictly to specifications

  • therwise the modules won’t fit together

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 25 / 32

slide-28
SLIDE 28

Data Structures

Outline

1

Modules

2

Abstract Data Types

3

Specification

4

Data Structures

5

Closing

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 26 / 32

slide-29
SLIDE 29

Data Structures

Data structures

ADT-s are common for defining Data Structures

e.g. lists, queues, stacks, heaps, etc.

Holding large quantities of data Different functions for data processing and access

sorting searching last/first element

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 27 / 32

slide-30
SLIDE 30

Data Structures

Example: Queue

First in, first out Insert elements at the end of the queue Retrieve elements from the head of the queue Usually no searching ADT functions

emptyQ (create empty queue) isEmptyQ (is the queue empty?) addQ (add element) remQ (remove/retrieve element)

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 28 / 32

slide-31
SLIDE 31

Data Structures

Different ways to define data types

data NewType = Cons1 T11 T12 ... | Cons2 T21 T22 ... | ...

algebraic data type – as discussed the most flexible construct

newtype NewType = Cons1 T1

special case with only one constructor and one argument more efficient (internally) than data

  • therwise equivalent

type NewType = ...

alias for another data type

  • ften tupples

no type checking between the alias and the original type

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 29 / 32

slide-32
SLIDE 32

Data Structures

Queue Implementation

module Queue(Queue,emptyQ,isEmptyQ,addQ,remQ) where newtype Queue a = Qu [a]

  • - parameterised

emptyQ - Qu [] isEmptyQ (Qu []) = True isEmptyQ _ = False addQ x (Qu xs) = Qu ( xs++[x] ) remQ Qu ([]) = error "Cannot remove from empty queue" remQ Qu (x:xs) = (x,xs)

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 30 / 32

slide-33
SLIDE 33

Closing

Outline

1

Modules

2

Abstract Data Types

3

Specification

4

Data Structures

5

Closing

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 31 / 32

slide-34
SLIDE 34

Closing

Summary

Module allows structuring your program An ADT is a module with a carefully controlled interface The coursework will require an ADT

you are given a front-end module have to implement the ADT strictly to specification the use of ADT-s ensure compatibility

Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 32 / 32