Haskell and HaskellVV. What is Haskell? “Haskell is a polymorphically typed, lazy, pure functional language.” –www.haskell.orgwww.haskell.org So what

Download Haskell and HaskellVV. What is Haskell? “Haskell is a polymorphically typed, lazy, pure functional language.” –www.haskell.orgwww.haskell.org So what

Post on 19-Dec-2015

213 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> Haskell and HaskellVV </li> <li> Slide 2 </li> <li> What is Haskell? Haskell is a polymorphically typed, lazy, pure functional language. www.haskell.orgwww.haskell.org So what does this mean? </li> <li> Slide 3 </li> <li> Haskell isa functional language Imperative (C, C++) Program is a sequence of steps Subroutines are called in a specific order How to calculate Functional (Lisp, ML) Program is a single function evaluation Sub-expressions are evaluated when needed What is calculated </li> <li> Slide 4 </li> <li> a pure functional language No side effects! </li> <li> Slide 5 </li> <li> Types in Haskell num = 2 inc x = 1 + x add x y = x + y num :: Int inc :: Int Int add :: Int Int Int Could be Int Int Int or could be Int (Int Int) a function returning a function! </li> <li> Slide 6 </li> <li> Types in Haskell (add 1) :: Int Int (add 1) x = 1 + x inc = add 1 currying </li> <li> Slide 7 </li> <li> Other types Bool, Char [] are lists [Bool] or [Char] (String = [Char]) (,) (,,) and so on are pairs, triples, n-tuples (Bool, Int) or (Bool, [Char], Int) [(Bool, [Char Int], [[(Char,Bool)]])] </li> <li> Slide 8 </li> <li> is a functional language (2) Functions are first-order objects! Functions can be passed as arguments: sort :: [Int] [Int] sortBy :: (Int Int Bool) [Int] [Int] sortBy () sortBy (customOrdering) </li> <li> Slide 9 </li> <li> is polymorphically typed sortBy :: (a a Bool) [a] [a] a can be any type: sortBy :: (Int Int Bool) [Int] [Int] sortBy :: (CharCharBool) String String sortBy :: ((Int, Int) (Int, Int) Bool) [(Int, Int)] [(Int, Int)] sortBy ( </li> <li> Example: quicksort qsort :: [a] [a] qsort [] = [] qsort (x:xs) = (qsort lt) ++ [x] ++ (qsort gt) wherelt = filter (x) xs We could also write qsortBy, replacing ( </li> <li> Monads provide a way to incorporate side effects eg. I/O operations putStr :: String IO () getStr :: IO String (getStr &gt;&gt;= \str putStr str) :: IO () (do { str getStr; putStr str; }) :: IO () </li> <li> Slide 18 </li> <li> Monads This looks like imperative code, but 1.Side-effects are precisely controlled 2.These are first-order objects! map putStr [one, two, three, ] is actually a list of I/O operations [IO ()], ([IO String], Bool IO ()) sequence [do {str getStr; putStr str; }, (putStr foo), ] </li> <li> Slide 19 </li> <li> Monads and HaskellVV use a monad MeshOp: prevToOp :: vtxLabelvtxLabelMeshOp vtxLabel setNBOp :: vtxLabel [vtxLabel] MeshOp () MeshOp is just an operation: executeMeshOp :: MeshOp () mesh mesh </li> <li> Slide 20 </li> <li> Example: insertVertex insertVertex :: vtxLabel vtxLabel MeshOp vtxLabel insertVertex p q = dolbl newVertexOp setNBOp lbl [p,q] replaceWithOp p q lbl replaceWithOp q p lbl return lbl </li> <li> Slide 21 </li> <li> Monads and HaskellVV Some things are easy (like forall): map someOperation (listVertices mesh) map insertVertex (listNeighbours mesh) However, many vv programs require two (or more) passes through a forall statement Hmmmmaybe Haskell can help here? </li> <li> Slide 22 </li> <li> Delay monads The Delay monad lets you arbitrarily delay an operation: synchronize ( do { delay a; b; }) do { b; a; } A vertex or pair can be dealt with in one go: synchronize (map doSomething (getVertices mesh)) </li> <li> Slide 23 </li> <li> Example: delayedInsertVertex dInsertVertex :: vtxLabel vtxLabel MeshOp vtxLabel dInsertVertex p q = dolbl newVertexOp delay (do setNBOp lbl [p,q] replaceWithOp p q lbl replaceWithOp q p lbl) return lbl </li> <li> Slide 24 </li> <li> Example, continued An operation can then be someOperation = synchronize ( map handleOne (getNeighbours mesh)) where handleOne (p,q) = dolbl delayInsertVertex p q someOtherOperation p q someOtherOperation uses all of the old neighbourhoods! </li> <li> Slide 25 </li> <li> Conclusion Whats good about Haskell (and HaskellVV?) Delayed operations Strict semantics (no side-effects) Easier to understand the code Operations are implementation independent Haskell code can be as fast as C / C++ Windowing support, OpenGL libraries, </li> <li> Slide 26 </li> <li> Conclusion Whats bad about Haskell (and HaskellVV?) Haskell is functional and lazy Expressions are stored until evaluated (or garbage collected) If youre not careful, the heap can fill up with unevaluated expressions Current implementation of vv is slow </li> <li> Slide 27 </li> <li> What have I left out? Type classes define what functions may be applied to a type for instance, (</li></ul>