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

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CSE341: Programming Languages Lecture 12 Equivalence Dan Grossman Spring 2013.
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Ulf Norell.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Functional Programming Professor Yihjia Tsai Tamkang University.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Lazy Functional Programming in Haskell
Tuples and Lists Lecture 3, Programmeringsteknik del A.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 1 Functional Programming Symbolic AI is based.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
HASKELL!. I NTRODUCTION W HAT IS H ASKELL ? Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy,
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Going Functional Primož Gabrijelčič. Functional programming.
Compiling Functional Programs Mooly Sagiv Chapter 7
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
COMP313A Functional Programming (1)
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
First Order Haskell Neil Mitchell York University λ.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Advanced Functional Programming 2010
Functional Programming
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
CS 326 Programming Languages, Concepts and Implementation
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Functional Programming
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Important Concepts from Clojure
Important Concepts from Clojure
FP Foundations, Scheme In Text: Chapter 14.
Type & Typeclass Syntax in function
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
CSE 3302 Programming Languages
Important Concepts from Clojure
Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 29-May-19.
Laziness and Its Consequences
PROGRAMMING IN HASKELL
Presentation transcript:

Haskell and HaskellVV

What is Haskell? “Haskell is a polymorphically typed, lazy, pure functional language.” – So what does this mean?

Haskell is…a 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

…a pure functional language No side effects!

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!

Types in Haskell (add 1) :: Int → Int (add 1) x = 1 + x inc = add 1 “currying”

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)]])]

…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 (>) –sortBy (customOrdering)

…is polymorphically typed sortBy :: (a → a → Bool) → [a] → [a] a can be any type: –sortBy :: (Int → Int → Bool) → [Int] → [Int] –sortBy :: (Char→Char→Bool) → String →String –sortBy :: ((Int, Int) → (Int, Int) → Bool) → [(Int, Int)] → [(Int, Int)] sortBy (<) :: [a] → [a] (more or less)

Example: quicksort qsort :: [a] → [a] qsort [] = [] qsort (x:xs) = (qsort lt) ++ [x] ++ (qsort gt) wherelt = filter (<x) xs gt = filter (>x) xs We could also write qsortBy, replacing (<x) with (f x)

Haskell is lazy eg. head (qsort list) –Only the first element is needed, so the lists gt are never computed! qsort (x:xs)= (qsort lt) ++ … = (qsort (l:ls) ++ …) ++ … … = ((…(qsort [] ++ [y] ++ …)… qsort (x:xs)= y

One more important function map :: (a → b) → [a] → [b] Applies a function f to every element in a list (or, more generally, any data structure) eg. map (*2) [1,2,3] = [2,4,6]

HaskellVV mesh, vtxLabel, vtxData ← polymorphic No global state → labels particular to mesh Query functions like –prevTo :: mesh → vtxLabel → vtxLabel → vtxLabel –lookupData :: mesh → vtxLabel → vtxData Update functions like –setNB :: mesh → vtxLabel → [vtxLabel] → mesh –lookupData :: mesh → (vtxLabel → vtxData → vtxData) → mesh

HaskellVV Long operations are clumsy insertVertex m p q x = replaceWith ( replaceWith ( setNB m x [p,q] ) p q x) q p x)

HaskellVV There are ways around this: insertVertex m p q x = let m1 = setNB m x [p,q] in let m2 = replaceWith m1 p q x in let m3 = replaceWith m2 q p x in … The problem remains: we want sequential operations.

Monads Mathematical structures offering operations which satisfy certain rules … Imperative operations are monads!

Monads provide a way to incorporate ‘side effects’ eg. I/O operations –putStr :: String → IO () –getStr :: IO String (getStr >>= \str → putStr str) :: IO () (do { str ← getStr; putStr str; }) :: IO ()

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”), …]

Monads and HaskellVV use a monad MeshOp: prevToOp :: vtxLabel→vtxLabel→MeshOp vtxLabel setNBOp :: vtxLabel → [vtxLabel] → MeshOp () MeshOp is just an operation: executeMeshOp :: MeshOp () → mesh → mesh

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

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 Hmmm…maybe Haskell can help here?

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))

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

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!

Conclusion What’s 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, …

Conclusion What’s bad about Haskell (and HaskellVV?) –Haskell is functional and lazy Expressions are stored until evaluated (or garbage collected) If you’re not careful, the heap can fill up with unevaluated expressions –Current implementation of vv is slow

What have I left out? Type classes –define what functions may be applied to a type –for instance, (<) :: a → a → Bool is only defined for a in class Ord: ( a → a → Bool –HaskellVV defines classes Mesh mesh vtxLabel vtxData MeshOp op mesh vtxLabel vtxData VectorSpace field vector and others

More information –Haskell homepage, lots of information, plus compilers, interpreters, etc. –Adding my code, will archive this presentation, etc.