Haskell - A Perspective Presented by Gábor Lipták April 2011.

Slides:



Advertisements
Similar presentations
Parallel Haskell Tutorial: The Par Monad Simon Marlow Ryan Newton Simon Peyton Jones.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Software Transactional Memory Steve Severance – Alpha Heavy Industries.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Ulf Norell.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Intel Concurrent Collections (for Haskell) Ryan Newton, Chih-Ping Chen, Simon Marlow Software and Services Group Jul 27, 2010.
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.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
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.
Dan Johnson.  Functional language that started development in  Committee designed language  Pure and Lazy  Compiled or Interpreted  Named after.
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)
Advanced Functional Programming Tim Sheard 1 Lecture 14 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Advanced Programming Andrew Black and Tim Sheard Lecture 11 Parsing Combinators.
Chapter Fifteen: Functional Programming Languages Lesson 12.
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.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
COMP313A Functional Programming (1)
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
1 Haskell Kevin Atkinson John Simmons. 2 Contents Introduction Type System Variables Functions Module System I/O Conclusions.
QIAN XI COS597C 10/28/2010 Explicit Concurrent Programming in Haskell.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
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.
Introduction to Functional Programming Part 1 – The Basic Concepts Winter Young
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Patrik Jansson.
Advanced Functional Programming 2010
Polymorphic Functions
Functional Programming
Laziness and Infinite Datastructures
Recursion.
dr Robert Kowalczyk WMiI UŁ
Types CSCE 314 Spring 2016.
Parallelism and Concurrency
Types for Programs and Proofs
Theory of Computation Lecture 4: Programs and Computable Functions II
A lightening tour in 45 minutes
Functional Programming
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
CSE 341 Section 5 Winter 2018.
Type & Typeclass Syntax in function
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Records and Type Classes
Programming Languages
Functional Programming
Functions and patterns
Functional Programming and Haskell
PROGRAMMING IN HASKELL
Functional Programming and Haskell
Records and Type Classes
Presentation transcript:

Haskell - A Perspective Presented by Gábor Lipták April 2011

Topics Why? Functional Haskell highlights Development Concurrency approaches Q&A

Why should you be interested (as a Java,.Net, Ruby developer)? Knowing this different language will help you improve your understanding and skills in your "main" language Research proving ground for features coming to your language some while later (or to languages hosted on your VM, F#, Scala, Clojure) Significant scaling (today) Fun:)

Scalability

Functional? Programming with (mathematical) functions In functional programming, programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed. Functional programming typically avoids using mutable state. Prelude> filter even [1..10] filter :: (a -> Bool) -> [a] -> [a] filter _ [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs

Functional?? Object oriented: object method args Functional: function args Lambdas: add' = (+) test1add' = add' 3 5 test2add' = 3 `add'` 5 add'' = \x -> (\y -> x + y) test1add'' = add'' 3 5

Purely Functional First class/Higher order functions Pure functions (no side effects) o Immutable data o Referential transparency (each call returns the same result) o Lazy evaluation o Purity and effects (monads) Type system/Type inference Tail recursion Compositional/Declarative/Concise Lazy (vs. eager) evaluation

Purity (adapted from Caging the effects monster )Caging the effects monster

Introduction Named after Haskell Brooks Curry, was an American mathematician and logician. Two programming languages named after him.Haskell Brooks Curry Lambda calculusLambda calculus is a formal system for function definition, function application and recursion. Prelude> 2^

Reserved Words case class data deriving do else if import in infix infixl infixr instance let of module newtype then type where

Polymorphically Statically Typed (type inference) Prelude> :t map map :: (a -> b) -> [a] -> [b] data Bool = False | True data Roulette = Black | Red | Zero | DoubleZero deriving (Eq, Ord, Show, Read, Bounded, Enum) type PhoneNumber = String type Name = String type PhoneBook = [(Name,PhoneNumber)] Eliminating easy to make errors during compile time.

Type Classes square :: Num a => a -> a square x = x *x class Num a where (*) :: a -> a -> a instance Num Int where a * b = mulInt a b -- mulInt is a primitive class Increment a where increment :: Num -> Num instance Increment Int where increment n = n + 1

Lazy (thunks) numsFrom n = n : numsFrom (n+1) squares = map (^2) (numsfrom 0) take 5 squares => [0,1,4,9,16] take 3 (sort xs) Thunk represents an unevaluated expression.Storing and evaluating thunks are costly.

Folds foldr (+) 0 (1:2:3:[]) == 1 + foldr (+) 0 (2:3:[]) == 1 + (2 + foldr (+) 0 (3:[]) == 1 + (2 + (3 + foldr (+) 0 [])) == 1 + (2 + (3 + 0)) foldl (+) 0 (1:2:3:[]) == foldl (+) (0 + 1) (2:3:[]) == foldl (+) ((0 + 1) + 2) (3:[]) == foldl (+) (((0 + 1) + 2) + 3) [] == (((0 + 1) + 2) + 3)

Tail recursion (and accumulator) my_sum :: [ Integer ] -> Integer my_sum [] = 0 my_sum (x:xs) = x + my_sum xs main :: IO () main = print (my_sum [ ]) my_sum :: [ Integer ] -> Integer my_sum xs = my_sum' 0 xs where my_sum' acc [] = acc my_sum' acc (x:xs) = my_sum' (acc+x) xs main :: IO () main = print (my_sum [ ])

Pattern Matching and Guards lucky :: (Integral a) => a -> String lucky 3 = "Lucky Number!" lucky x = "Sorry, you're out of luck!" numberDesc :: (Integral) => a -> String numberDesc number | number < 0 = "negative" | number > 0 = "positive" | otherwise = "zero"

Higher order functions, currying map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs mapM :: Monad m => (a -> m b) -> [a] -> m [b] mapM_ :: Monad m => (a -> m b) -> [a] -> m () map (+3) [1,5,3,1,6] map (\(a,b) -> a + b) [(1,2),(3,5),(6,3),(2,6),(2,5)] take5 :: [Char] -> [Char] take5 = take 5

Monads (1) Monad is a computation returning result of type a Computations are pure during construction, and might have side effects when running (lazy)

Monads (2) instance Monad Maybe where return x = Just x Nothing >>= f = Nothing Just x >>= f = f x fail _ = Nothing Prelude> Nothing >> Just 3 Nothing Prelude> Just 3 >> Just 4 Just 4 Prelude> Just 3 >> Nothing Nothing

Monads (3) main :: IO () main = do putStrLn "Hello, what is your name?" name <- getLine putStrLn ("Hey " ++ name ++ "!") More than you care to read (just search for Monad tutorial :) In particular look for parsing examples.Monad tutorial You become a real Haskell programmer only after publishing your own Monad Tutorial :)

Development Use your editor (vim,Emacs), Leksah, Eclipse, VisualStudio to developvimEmacsLeksahEclipseVisualStudio Project structures are detailed at haskell.orghaskell.org Use of types (and type signatures) helps to write correct code Profiling (for space "leakage"...) Listing sparks/concurrency details when running

QuickCheck Testing invariants in the code Lots of "clones" for other languages import Test.QuickCheck take5 :: [Char] -> [Char] take5 = take 5 main = do quickCheck (\s -> length (take5 s) == 5) quickCheck (\s -> length (take5 s) <= 5) *Main> main *** Failed! Falsifiable (after 1 test): "" +++ OK, passed 100 tests.

Other tools Build system: Cabal Package repository: Hackage Code search engine HoogleHoogle Code search engine Hayoo!Hayoo! Haddock documentation tool HUnit (from xUnit series)

Concurrency Approaches Explicit (lightweight) threads and STM (software transactional memory) Semi-implicit (`par`, `pseq`) a "hint" Data parallel

Explicit threads Not dissimilar to threads found in other languages, with same benefits/drawbacks... Non-deterministic by design Monadic: forkIO and STM forkIO :: IO () −> IO ThreadId forkOS :: IO () −> IO ThreadId

Software Transactional Memory atomically :: STM a -> IO a retry :: STM a orElse :: STM a -> STM a -> STM a... newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM () Emphasis on composition Similar to database transactions

Semi-implicit hard to ensure the right granularity Deterministic Pure: par and seq infixr 0 `par` infixr 1 `pseq` par :: a -> b -> b pseq :: a -> b -> b equivalent to par a b = b pseq a b = _|_ if a = _|_ = b otherwise _|_ (read "bottom", non terminating expression).

Example import Control.Parallel cutoff :: Int cutoff = 20 parFib :: Int -> Int parFib n | n < cutoff = fib n parFib n = p `par` q `pseq` (p + q) where p = parFib $ n - 1 q = parFib $ n - 2 fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2) main :: IO () main = print $ parFib 40

Dual core $ time./parfib.exe +RTS -N real 0m1.998s user 0m0.015s sys 0m0.015s $ time./parfib.exe +RTS -N real 0m1.337s user 0m0.015s sys 0m0.015s

Data parallel (used in languages like High Performance Fortran) Deterministic Pure: parallel arrays Shared memory initially; distributed memory eventually; possibly even GPUs mapP :: (a -> b) -> [:a:] -> [:b:] zipWithP :: (a -> b -> c) -> [:a:] -> [:b:] -> [:c:] filterP :: (a -> Bool) -> [:a:] -> [:a:] sumP :: Num a => [:a:] -> a import GHC.PArr

Final comments Very active community (Haskell Cafe and other mailing lists with very good info to noise ratio)Haskell Cafe and other mailing lists Great support for algorithms Lots of libraries (many of them are very specialised) Very wide use in academia, less outside Might be hard to find knowledgeable developers

Further information haskell.org Real World Haskell Yet Another Haskell Tutorial Learn You a Haskell for a Great Good! HEAT (Haskell Educational Advancement Tool) Haskell Cheat Sheet The Monad.ReaderThe Monad.Reader (if you want to bend your mind :) Simon Peyton-Jones (Principal Researcher at Microsoft) Philip Wadler Galois Multicore/Don Stewart Microsoft Channel9Going Deep Lectures Carnegie Mellon curriculum change

Q&A