introduction to programming and algorithms...

36
4 Abstract Types Uwe R. Zimmer - The Australian National University Introduction to Programming and Algorithms 2015 Abstract Types Uwe R. Zimmer - The Australian National University

Upload: letruc

Post on 21-May-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

4Abstract Types

Uwe R. Zimmer - The Australian National University

Introduction to Programming and Algorithms 2015

Abstract Types

Uwe R. Zimmer - The Australian National University

Page 2: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 335 of 794 (chapter 4: “Abstract Types” up to page 369)

References

[ Thompson2011 ] Thompson, Simon Haskell - The craft of functional programming Addison Wesley, third edition 2011

Page 3: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 336 of 794 (chapter 4: “Abstract Types” up to page 369)

Defi nition

An abstract type is defi ned by

1. A type name… the type structure can be partly revealed or stay completely hidden

and can be as simple as a character or as complicated as a database.

2. Functions / Operations which work on this type… and have to follow defi ned rules, like an e.g. arithmetic laws or a specifi c algebra.

Page 4: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 337 of 794 (chapter 4: “Abstract Types” up to page 369)

Defi nition

An abstract type is defi ned by

1. A type name… the type structure can be partly revealed or stay completely hidden

and can be as simple as a character or as complicated as a database.

2. Functions / Operations which work on this type… and have to follow defi ned rules, like an e.g. arithmetic laws or a specifi c algebra.

What’s “abstract”?

We don’t necessarily know how values are stored in memory or how the operations are implemented.

… we can for instance use a fl oating point number without knowing which bit goes where, or how the sine function is implemented

Page 5: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 338 of 794 (chapter 4: “Abstract Types” up to page 369)

Defi nition

An abstract type is defi ned by

1. A type name… the type structure can be partly revealed or stay completely hidden

and can be as simple as a character or as complicated as a database.

2. Functions / Operations which work on this type… and have to follow defi ned rules, like an e.g. arithmetic laws or a specifi c algebra.

What’s “abstract”?

We don’t necessarily know how values are stored in memory or how the operations are implemented.

… we can for instance use a fl oating point number without knowing which bit goes where, or how the sine function is implemented

h h b h

All good programming interfaces are designed on

a “need to know” basis.

Page 6: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 339 of 794 (chapter 4: “Abstract Types” up to page 369)

Basic types

Basic, scalar types overviewEnumerations:

Boolean (Bool), Characters (Char)

Numbers:

Cardinal (-), Integer (Integer), Integer range (Ix), Rational (Rational), Real (Float, Double), Complex (Complex)

Machine related types:

Bits (Bits), Words: Integer range 0 2 1Nf - (Word, WordN),

Two’s complements: Integer range 2 2 1N N1 1f- -- - (Int, IntN)with , , ,N 8 16 32 64! 6 @

Page 7: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 340 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Constructors:

data Bool = False | True

Base functions: “and” (&&), “or”(||), “not” (not)

(&&), (||) :: Bool -> Bool -> Boolnot :: Bool -> Bool

Page 8: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 341 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Constructors:

data Bool = False | True

Base functions: “and” (&&), “or”(||), “not” (not)

(&&), (||) :: Bool -> Bool -> Boolnot :: Bool -> Bool

Some related functions:

(==), (/=) :: Eq a => a -> a -> Bool – (<), (<=), (>=), (>) :: Ord a => a -> a -> Bool

isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE :: RealFloat a => a -> Bool

Page 9: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 342 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Constructors:

data Bool = False | True

Base functions: “and” (&&), “or”(||), “not” (not)

(&&), (||) :: Bool -> Bool -> Boolnot :: Bool -> Bool

Some related functions:

(==), (/=) :: Eq a => a -> a -> Bool – (<), (<=), (>=), (>) :: Ord a => a -> a -> Bool

isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE :: RealFloat a => a -> Bool

Example functions:

threshold_reached :: Integer -> Boolthreshold_reached number = number > 100

and_3 :: Bool -> Bool -> Bool -> Booland_3 x y z = x && y && z

Page 10: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 343 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Example function “Exclusive or”:

ex_or1 :: Bool -> Bool -> Boolex_or1 x y = case (x, y) of (False, False) -> False (False, True ) -> True (True , False) -> True (True , True ) -> False

Page 11: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 344 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Example function “Exclusive or”:

ex_or1 :: Bool -> Bool -> Boolex_or1 x y = case (x, y) of (False, False) -> False (False, True ) -> True (True , False) -> True (True , True ) -> False

ex_or2 :: Bool -> Bool -> Boolex_or2 x y = case x of False -> y True -> not y

Page 12: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 345 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Example function “Exclusive or”:

ex_or1 :: Bool -> Bool -> Boolex_or1 x y = case (x, y) of (False, False) -> False (False, True ) -> True (True , False) -> True (True , True ) -> False

ex_or2 :: Bool -> Bool -> Boolex_or2 x y = case x of False -> y True -> not y

ex_or3 :: Bool -> Bool -> Boolex_or3 x y = (x && not y) || (not x && y)

Page 13: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 346 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Example function “Exclusive or”:

ex_or1 :: Bool -> Bool -> Boolex_or1 x y = case (x, y) of (False, False) -> False (False, True ) -> True (True , False) -> True (True , True ) -> False

ex_or2 :: Bool -> Bool -> Boolex_or2 x y = case x of False -> y True -> not y

ex_or3 :: Bool -> Bool -> Boolex_or3 x y = (x && not y) || (not x && y)

ex_or4 :: Bool -> Bool -> Boolex_or4 x y = x /= y

Page 14: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 347 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Testing example implementations:

import Test.QuickCheck

test_ex_ors :: Bool -> Bool -> Booltest_ex_ors x y = (ex_or1 x y) == (ex_or2 x y) && (ex_or2 x y) == (ex_or3 x y) && (ex_or3 x y) == (ex_or4 x y)

> quickCheck test_ex_ors+++ OK, passed 100 tests.

Page 15: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 348 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Boolean algebra:

(a && b) && c == a && (b && c) (a || b) || c == a || (b || c) (associative)

a && b == b && a a || b == b || a (commutative)

a && (a || b) == a a || (a && b) == a (absorption)

a && (b || c) == (a && b) || (a && c) a || (b && c) == (a || b) && (a || c) (distribution)

a && not a == False a || not a == True (complement)

Those basic laws (which are one way to axiomatize the boolean algebra) may help to simplify logic expressions.

Compilers might use them as well to provide warnings like “This guard expression will always be False, hence the following code is unreachable”.

Page 16: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 349 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Constructors:

data Bool = False | True

Base functions: “and” (&&), “or”(||), “not” (not)

(&&), (||) :: Bool -> Bool -> Boolnot :: Bool -> Bool

Algebra:

(a && b) && c == a && (b && c) (a || b) || c == a || (b || c) (associative)

a && b == b && a a || b == b || a (commutative)

a && (a || b) == a a || (a && b) == a (absorption)

a && (b || c) == (a && b) || (a && c) a || (b && c) == (a || b) && (a || c) (distribution)

a && not a == False a || not a == True (complement)

Page 17: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 350 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Boolean (Bool)Constructors:

data Bool = False | True

Base functions: “and” (&&), “or”(||), “not” (not)

(&&), (||) :: Bool -> Bool -> Boolnot :: Bool -> Bool

Algebra:

(a && b) && c == a && (b && c) (a || b) || c == a || (b || c) (associative)

a && b == b && a a || b == b || a (commutative)

a && (a || b) == a a || (a && b) == a (absorption)

a && (b || c) == (a && b) || (a && c) a || (b && c) == (a || b) && (a || c) (distribution)

a && not a == False a || not a == True (complement)

An abstract type is defi ned by its name plus operations

(functions) which have to follow certain rules (algebra).

|| (b || )

Page 18: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 351 of 794 (chapter 4: “Abstract Types” up to page 369)

Enumerations

Character (Char)Defi nition:

data Char = Enumeration of Unicode Characters (ISO/IEC 10646) in UTF-32 – fi rst 128 characters are identical to ASCII,– fi rst 256 characters are identical to ISO 8859-1 (Latin-1).

Base functions:

isControl, isSpace, isLower, isUpper, isAlpha, isAlphaNum, isPrint, isDigit, isOctDigit, isHexDigit, isLetter, isMark, isNumber, isPunctuation, isSymbol, is-Separator, isAscii, isLatin1, isAsciiUpper, isAsciiLower :: Char -> Bool

generalCategory :: Char -> GeneralCategory (Emumeration of Unicode categories)

toUpper, toLower, toTitle :: Char -> Char

digitToInt :: Char -> IntintToDigit :: Int -> Char (works for hexadecimal characters / values too)

ord :: Char -> Intchr :: Int -> Char

Page 19: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 352 of 794 (chapter 4: “Abstract Types” up to page 369)

Numbers

Integer numbers (Integer)Defi nition:

Integer numbers – negative, zero, positive – no bounds!

Base functions:

max, min :: Ord a => a -> a -> a

(+), (-), (*), (^) :: Num a => a -> a -> aquot, rem, div, mod :: Integral a => a -> a -> aquotRem, divMod :: Integral a => a -> a -> (a, a)

negate, abs, signum :: Num a => a -> a

fromInteger :: Num a => Integer -> atoInteger :: Integral a => a -> Integer

Page 20: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 353 of 794 (chapter 4: “Abstract Types” up to page 369)

Numbers

Integer numbers (Integer)Defi nition:

Integer numbers – negative, zero, positive – no bounds!

Base functions:

max, min :: Ord a => a -> a -> a

(+), (-), (*), (^) :: Num a => a -> a -> aquot, rem, div, mod :: Integral a => a -> a -> aquotRem, divMod :: Integral a => a -> a -> (a, a)

negate, abs, signum :: Num a => a -> a

fromInteger :: Num a => Integer -> atoInteger :: Integral a => a -> Integer

‘a’ stands for multiple types here –

including Integer

‘quot’, ‘rem’ truncate towards zero

‘div’, ‘mod’ truncate towards negative infi nity

Page 21: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 354 of 794 (chapter 4: “Abstract Types” up to page 369)

Numbers

Ranges and Modulo Types (Ix)Defi nition:

Sub-ranges of discrete types (Integer or enumerations), either with an “out of range detection” or a modulo arithmetic semantic.

Not directly available in Haskell. (yet e.g. the Ix class implements part of the functionality.)

Base functions:

range :: Ord a => (a, a) -> [a]

index :: Ord a => (a, a) -> a -> Int

inRange :: Ord a => (a, a) -> a -> Bool

rangeSize :: Ord a => (a, a) -> Int

Example expressions:

> range (‘a’, ‘z’)“abcdefghijklmnopqrstuvwxyz”

> inRange (‘a’, ‘z’) ‘b’True

> index (‘a’, ‘z’) ‘b’1

> range (1, 12)[1,2,3,4,5,6,7,8,9,10,11,12]

‘a’ stands for any type on which an order is defi ned

– including Integer

Indices start at 0

Page 22: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 355 of 794 (chapter 4: “Abstract Types” up to page 369)

Numbers

Ranges and Modulo Types (Ix)Defi nition:

Sub-ranges of discrete types (Integer or enumerations), either with an “out of range detection” or a modulo arithmetic semantic.

Not directly available in Haskell. (yet e.g. the Ix class implements part of the functionality.)

Range and modulo type examples:Natural = subtype Integer range 0 ..

Positive = subtype Integer range 1 ..

Index = subtype Positive range 1..10

Base_Colour = subtype Color range Red .. Blue

Circular_Index = modulo 10

(none of which are natively available in Haskell)

Page 23: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 356 of 794 (chapter 4: “Abstract Types” up to page 369)

Numbers

Rational numbers (Rational)Defi nition:

data Integral a => Ratio atype Rational = Ratio Integer

Any number defi ned by the ratio of two Integers – no bounds or precision limits!

Base functions:

(%) :: Integer -> Integer -> Rational

numerator :: Rational -> Integer

denominator :: Rational -> Integer

approxRational :: RealFrac a => a -> a -> Rational

Example expressions:

> 10 * (3 % 4)15 % 2

> approxRational pi 0.0122 % 7

fromInteger (numerator api) / fromInteger (denominator api) where api = approxRational pi 0.01

3.142857Real number which needs converting

Precision of conversion

h

Page 24: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 357 of 794 (chapter 4: “Abstract Types” up to page 369)

Numbers

Real numbers (Float, Double)Defi nition:

Limited precision real numbers in fl oating point notation (IEEE 754).Float is stored in 32 bits (about 7 signifi cant digits and approximate range: 10 1038 38! f- ).Double is stored in 64 bits (about 16 signifi cant digits and approx. range: 10 10308 308! f- ).

Base functions (in addition to previously introduced numerical functions):

pi :: Floating a => a

exp, log, sqrt, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh :: Floating a => a -> a

(**), logBase, atan2 :: Floating a => a -> a -> a

(^^) :: (Fractional a, Integral b) => a -> b -> a

isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE :: RealFloat a => a -> Bool

truncate, round, ceiling, floor :: Integral b => a -> b

Page 25: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 358 of 794 (chapter 4: “Abstract Types” up to page 369)

Numbers

Complex numbers (Complex)Defi nition:

data RealFloat a => Complex a = a :* a

Limited precision complex numbers based on the type Float or Double for its components.

Base functions (in addition to previously introduced numerical functions):

realPart, imagPart, magnitude, phase :: Complex a -> a

conjugate :: Complex a -> Complex a

polar :: Complex a -> (a, a)

mkPolar :: a -> a -> Complex a

cis :: a -> Complex a

Page 26: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 359 of 794 (chapter 4: “Abstract Types” up to page 369)

Machine oriented scalars

Words as Cardinals/Naturals (WordN)Defi nition:

Word, Word8, Word16, Word32, Word64 are representations for words in memoryinterpreted as natural numbers between 0 and 2 1N - for WordN. Word stands for a word of default size for the local machine (size can be checked at runtime).

Base functions:

Functions are identical to the functions for the Integers.

Warning note:

Words have limited precision and all arithmetic is performed in mod 2N.

-4 :: Word16 65532(65500 :: Word16) + (40 :: Word16) 4(65500 :: Integer) + (40 :: Integer) 65540(65540 :: Integer) `mod` (2 ^ 16) 4

No error or warning is raised in any of

those expressions in Haskell, C or Java!

540

Page 27: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 360 of 794 (chapter 4: “Abstract Types” up to page 369)

No error or warning is raised in any of

those expressions in Haskell, C or Java!

Machine oriented scalars

Words as Integers (IntN)Defi nition:

Int, Int8, Int16, Int32, Int64 are representations for words in memoryinterpreted as integer numbers between 2N 1- - and 2 1N 1 -- for IntN (2’s comple-ment representation). Int has the same size as Word (size can be checked at runtime).

Base functions:

Functions are identical with functions for Integer.

Warning note:

Integers have limited precision and all arithmetic is performed in “warp-around” mode:

(127 :: Int8) + 1 -128(127 :: Int8) + 1 + 255 127(127 :: Integer) + 1 128(((127 :: Integer) + 1 + 2^7) `mod` 2^8) - 2^7 -128

Page 28: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 361 of 794 (chapter 4: “Abstract Types” up to page 369)

Machine oriented scalars

Bits (Bits)Defi nition:

The type Bits is commonly used to manipulate Word or Int types on bit level.

Base functions:

(.&.), (.|.), xor :: a -> a -> a

complement :: a -> a

bit :: Int -> a

shift, rotate, shiftL, shiftR, rotateL, rotateR, setBit, clearBit, complementBit :: a -> Int -> a

testBit :: a -> Int -> BoolisSigned :: a -> Bool

bitSize :: a -> Int

Example:

shift (8 :: Word8) 2 32

Typical usages:

Interface programming

Effi cient implementation of “fl ag-arrays” or “bit-vectors”

Page 29: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 362 of 794 (chapter 4: “Abstract Types” up to page 369)

Machine oriented scalars

Bits (Bits)Defi nition:

The type Bits is commonly used to manipulate Word or Int types on bit level.

Base functions:

(.&.), (.|.), xor :: a -> a -> a

complement :: a -> a

bit :: Int -> a

shift, rotate, shiftL, shiftR, rotateL, rotateR, setBit, clearBit, complementBit :: a -> Int -> a

testBit :: a -> Int -> BoolisSigned :: a -> Bool

bitSize :: a -> Int

Example:

shift (8 :: Word8) 2 32

Typical usages:

Interface programming

Effi cient implementation of “fl ag-arrays” or “bit-vectors”

page 362

Bits type is outside the scope of this course – only mentioned for

completeness.

Page 30: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 363 of 794 (chapter 4: “Abstract Types” up to page 369)

Basic scalar types

Choosing the best type

In case of boolean and character types: no choice.

In case of numerical types: …

Page 31: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 364 of 794 (chapter 4: “Abstract Types” up to page 369)

Basic scalar types

Choosing the best type

• “Mathematical” types: (Natural), (Positive), Integer, Rational: Can express any precision value:more_precise_pi = 314159265358979323846264338327950288419716939937510 % 100000000000000000000000000000000000000000000000000

No loss of precision during calculations.

Behave exactly as the numbers by this name in mathematics.

• Versatile types: Float, Double, Complex:Limited precision, compact representation. Precision loss during calculations.Effi cient calculations incl. exponential and trigonometric functions. Choice of precision.

• Machine oriented types: Word, Int, Bits:Most effi cient calculations. Verifi cation required that all calculations stay with-in limits of their types (manually or by means of automated run-time checks).

Page 32: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 365 of 794 (chapter 4: “Abstract Types” up to page 369)

Defi nition

An abstract type is defi ned by

1. A type name… the type structure can be partly revealed or stay completely hidden

and can be as simple as a character or as complicated as a database.

2. Functions / Operations which work on this type… and have to follow defi ned rules, like an e.g. arithmetic laws or a specifi c algebra.

What’s “abstract”?

We don’t necessarily know how values are stored in memory or how the operations are implemented.

… we can for instance use a fl oating point number without knowing which bit goes where, or how the sine function is implemented

h h b h

All good programming interfaces are designed on

a “need to know” basis.

Repetition:

Page 33: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 366 of 794 (chapter 4: “Abstract Types” up to page 369)

Basic types

Basic, scalar types overviewEnumerations:

Boolean (Bool), Characters (Char)

Numbers:

Cardinal (-), Integer (Integer), Integer range (Ix), Rational (Rational), Real (Float, Double), Complex (Complex)

Machine related types:

Bits (Bits), Words: Integer range 0 2 1Nf - (Word, WordN),

Two’s complements: Integer range 2 2 1N N1 1f- -- - (Int, IntN)with , , ,N 8 16 32 64! 6 @

Page 34: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 367 of 794 (chapter 4: “Abstract Types” up to page 369)

Basic types

Basic, scalar types overviewEnumerations:

Boolean (Bool), Characters (Char)

Numbers:

Cardinal (-), Integer (Integer), Integer range (Ix), Rational (Rational), Real (Float, Double), Complex (Complex)

Machine related types:

Bits (Bits), Words: Integer range 0 2 1Nf - (Word, WordN),

Two’s complements: Integer range 2 2 1N N1 1f- -- - (Int, IntN)with , , ,N 8 16 32 64! 6 @

erview

s (Char)

Can you envision other abstract types?

Page 35: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 368 of 794 (chapter 4: “Abstract Types” up to page 369)

Basic types

Basic, scalar types overviewEnumerations:

Boolean (Bool), Characters (Char)

Numbers:

Cardinal (-), Integer (Integer), Integer range (Ix), Rational (Rational), Real (Float, Double), Complex (Complex)

Machine related types:

Bits (Bits), Words: Integer range 0 2 1Nf - (Word, WordN),

Two’s complements: Integer range 2 2 1N N1 1f- -- - (Int, IntN)with , , ,N 8 16 32 64! 6 @

erview

s (Char)

Can you envision other abstract types?

n

(Bits),2 1N - (Word, WordN),

nge 2 2 1N N2 1N2f- -2 2N (Int, IntN)6 @, , ,8 1, 6 3,, 2 6, 4

There will be many – in fact every program can be expressed

as a set of abstract types –

… we will look into some more complex types next.

Page 36: Introduction to Programming and Algorithms 2015courses.cecs.anu.edu.au/courses/old/COMP1100.2015...Introduction to Programming and Algorithms 2015 Abstract Types © 2015 Uwe R. Zimmer,

Abstract Types

© 2015 Uwe R. Zimmer, The Australian National University page 369 of 794 (chapter 4: “Abstract Types” up to page 369)

Summary

Abstract Types

• Defi nition• What is abstract in an abstract type?

• Examples of abstract type• Boolean (Bool), Characters (Char).

• Numbers: Cardinal (-), Integer (Integer), Integer range (Ix), Rational (Rational), Real (Float, Double), Complex (Complex).

• Machine related types: Bits (Bits), Words: Integer range (Word, WordN), Two’s complements.