CS 11 Haskell track: lecture 6This week:ModulesArraysMore MonadsMonadPlusWrapping upModulesHaskell modules much more conservative than ocaml's module systemMuch of the work of e.g. functors done by type classesConsequently, modules are rather simpleModule examplemodule Tree ( Tree (Leaf, Branch), fringe ) wheredata Tree a = Leaf a | Branch (Tree a) (Tree a) fringe :: Tree a -> [a]fringe (Leaf x) = [x]fringe (Branch left right) = fringe left ++ fringe rightModule examplemodule Tree ( Tree(Leaf, Branch), fringe ) where...This means that this module explicitly exportsthe Tree datatypethe fringe functionnothing elseIf written as:module Tree where ...then everything in module is exportedImporting into modulesmodule Main whereimport Tree ( Tree(Leaf, Branch), fringe )main = print (fringe (Branch (Leaf 1) (Leaf 2)))If the second line was justimport Treethen everything exported from Tree module would be importedAvoiding name clashes (1)By default, imported names dumped into local namespaceWhat if two modules are used which share names?Can explicitly qualify names during importAvoiding name clashes (2)module Main whereimport Tree ( Tree(Leaf, Branch), fringe )import qualified Fringe ( fringe ) Module Fringe contains a function fringe which has same name as Tree module's fringe functionQualifying means refer to second fringe as Fringe.fringeimport qualified ... as ...Can rename the qualifier of a module by using the as syntaximport qualified VeryLongModuleName as VWatch out for this:import Foobar as F Brings in all names from Foobar with and without qualification (why would you want this?)hiding declarationsCan selectively hide some names upon import with a hiding declaration:Assume module A exports x and yimport A -- x and y importedimport A hiding y -- x onlyimport qualified A hiding y -- A.x onlyModules and instancesInstance declarations not explicitly imported/exportedmodules export all instance declarationsArraysHaskell arrays are functionalno in-place update in standard Arraysthough some mutable array types in ghc libraries (not covered here)Arrays require an Ix (indexing) type to represent indices (usually just Int)Array indicesclass (Ord a) => Ix a where range :: (a, a) -> [a] index :: (a, a) -> a -> Int inRange :: (a, a) -> a -> Bool range (0,4) => [0,1,2,3,4] range ((0,0), (1,2)) => [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]Array indicesclass (Ord a) => Ix a where range :: (a, a) -> [a] index :: (a, a) -> a -> Int inRange :: (a, a) -> a -> Bool index (1,9) 2 => 1index ((0,0), (1,2)) (1,1) => 4Creating arraysarray :: (Ix a) => (a,a) -> [(a,b)] -> Array a b squares = array (1,100) [(i, i*i) | i <- [1..100]]Accessing array elementssquares ! 8 => 64 bounds squares => (1,100)Examplefibs :: Int -> Array Int Int fibs n = a where a = array (0, n) ([(0, 1), (1, 1)] ++ [(i, a!(i-2) + a!(i-1)) | i <- [2..n]])Q: why do we need the where clause?"Modifying" array elements(//) :: (Ix a) => Array a b -> [(a,b)] -> Array a bsquares_bad = squares // [(8, 63)]squares_bad ! 8 => 63Creates a new array, not modifying in placeOther ways to actually modify in placebut need to be in e.g. IO monadMonadPlusMany Monads have a notion of a "zero" elementsome kind of "addition" of monadic objectsThis is captured in the MonadPlus classclass Monad m => MonadPlus m where mzero :: m a mplus :: m a -> m a -> m aMonadPlus instancesinstance MonadPlus Maybe wheremzero = NothingNothing `mplus` ys = ysxs `mplus` ys = xsinstance MonadPlus [] wheremzero = []mplus = (++)Where to now? (1)Lots of information on the webwww.haskell.orgwww.haskell.org/ghcHaskell mailing lists:www.haskell.org/haskellwiki/Mailing_Listshaskell mailing listhaskell-cafe mailing listWhere to now? (2)Lots of interesting paper collectionsI particularly recommend Phil Wadler's papers:http://homepages.inf.ed.ac.uk/wadler/ Good examples:"Imperative Functional Programming""Monads for Functional Programming""Comprehending
View Full Document