Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining Classes Modules and ADTs CSCE 314 Spring 2016.

Similar presentations


Presentation on theme: "Defining Classes Modules and ADTs CSCE 314 Spring 2016."— Presentation transcript:

1 Defining Classes Modules and ADTs CSCE 314 Spring 2016

2 CSCE 314 – Programming Studio “deriving” Experimenting with the type definitions we’ve seen will give many errors Data types come with no functionality by default, so you can’t compare for equality or show a value The deriving command lets you declare something as derived from one of the type classes: Eq, Ord, Enum, Read, Show, Bounded Example: Booleans: data Bool = False | True deriving (Eq, Ord, Show, Read)

3 Spring 2016 CSCE 314 – Programming Studio deriving Example: Booleans: data Bool = False | True deriving (Eq, Ord, Show, Read) So, we can do the following, now: > False == False True > False < True Order determined by definition order True > show False “False” > read “False” :: Bool Need :: Bool to resolve type False

4 Spring 2016 CSCE 314 – Programming Studio Defining a new type class New classes can be declared using the class constructor Example: class Eq a where (==), (/=) :: a -> a -> Bool --- Minimal complete definition: (==) and (/=) x /= y = not (x == y) x == y = not (x /= y) -- Optional For a type to be an instance fo the class Eq, it must support equality and inequality operators of the specified types Since we’ve defined /= in terms of ==, we only need to make sure == is specified

5 Spring 2016 CSCE 314 – Programming Studio Instance Declarations Definitions are given in an instance declaration A class can specify default definitions Example: the x /= y = not (x == y) line Example: to make Bool be a member of Eq, we must define ==: instance Eq Bool where (==) False False = True (==) True True = True (==) _ _ = False deriving Eq would generate an equivalent definition, automatically.

6 Spring 2016 CSCE 314 – Programming Studio Class hierarchies Classes can be derived from other classes Example: Ord class derived from Eq class Adds 6 more operators class Eq a => Ord a where ( ), (>=) :: a -> a -> Bool min, max :: a -> a -> a min x y | x <= y = x | otherwise = y max x y | x <= y = y | otherwise = x Defaults for min, max, mean that instances must just specify 4 operators

7 Spring 2016 CSCE 314 – Programming Studio Class hierarchy instance As with class definition, just define the remaining operators: instance Ord Bool where False < True = True _ < _ = False b <= c = (b < c) or (b == c) -- Notice: < and == are known b > c = c < b b >= c = c <= b

8 Spring 2016 CSCE 314 – Programming Studio Instances can specify differently than deriving data Weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving Show > map show [Mon, Tue, Wed] [“Mon”, “Tue”, “Wed”] or data Weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun instance Show Weekday where show Mon = “Monday” show tue = “Tuesday” … > map show [Mon, Tue, Wed] [“Monday”, “Tuesday”, “Wednesday”]

9 Spring 2016 CSCE 314 – Programming Studio Parameterized Instances Can define instances with parameters instance Show a => Show [a] where show [] = “[]” show (x:xs) = “[“ ++ show x ++ showRest xs where showRest [] = “]” showRest (x:xs) = “,” ++ show x ++ showRest xs Now, this works: > show [Mon, Tue, Wed] “[Monday, Tuesday, Wednesday]”

10 Spring 2016 CSCE 314 – Programming Studio Modules A Haskell program consists of a collection of modules. The purpose of a module is to: Control Namespaces Create Abstract Data Types A module contains various declarations First: import declarations Then (in any order): Data and type declarations Class and instance declarations Type signatures Function definitions, etc. One module per file Note: see online tutorial at Haskell.org

11 Spring 2016 CSCE 314 – Programming Studio Module example Module declaration begins with the keyword module Then name, then list of exports The module name may be the same as that of the type it contains Must begin with an uppercase letter Export list contained in parentheses Omitting exports all the entities in the module Same indentation rules as with other declarations apply Example: tree module module Tree ( Tree(Leaf, Branch), fringe ) where data Tree a = Leaf a | Branch (Tree a) (Tree a) fringe :: Tree a -> [a] fringe (Leaf x) = [x] fringe (Branch left right) = fringe left ++ fringe right

12 Spring 2016 CSCE 314 – Programming Studio Using a module Once it’s defined, a module can be imported module Main (main) where import Tree (Tree(Leaf, Branch), fringe) main = print (fringe (Branch (Leaf 1) (Leaf 2))) Omitting an explicit import list imports everything exported by the module Note: the “main” definition in the Main module is a way of defining a default “program”

13 Spring 2016 CSCE 314 – Programming Studio Qualifiers Qualifiers are used to resolve namespace conflicts e.g. to import different functions with same name from different modules Need to explicitly use qualifiers – they are not automatic To use, need to put a ‘. ’ in between Module name and qualified name import Tree (Tree(Leaf, Branch), fringe) import qualified Fringe (fringe) -- We now have two versions of “fringe”: fringe (Branch (Leaf 1) (Leaf 2)) Fringe.fringe (Branch (Leaf 1) (Leaf 2))

14 Spring 2016 CSCE 314 – Programming Studio More module features Can choose to not import certain entities using hiding : import Prelude hiding (length, sum) -- does not import length or sum from Prelude Can rename entities using as Can be used to shorten long names. import AVeryLongModuleName as A myFun n = A.somefunc n Can be used to combine module names, if no name conflicts: import Module1 as M import Module2 as M M.funcFromMod1 M.funcFromMod2

15 Spring 2016 CSCE 314 – Programming Studio Modules for Abstract Data Types ADTs: Hide the information about a specific representation Modules are the only way in Haskell to get this information hiding Example: definition of a Tree module TreeADT (Tree, leaf, branch, cell, left, right, isleaf) where data Tree a = Leaf a | Branch (Tree a) (Tree a) leaf = Leaf branch = Branch cell (Leaf a) = a left (Branch l r) = l right (Branch l r) = r isLeaf (Leaf _ ) = True isLeaf _ = False Notice: Tree is exported, but NOT Leaf or Branch! So, the actual way Tree is implemented is not seen We access it only through its operations: leaf, branch, cell, left, right, isleaf


Download ppt "Defining Classes Modules and ADTs CSCE 314 Spring 2016."

Similar presentations


Ads by Google