1 Haskell Kevin Atkinson John Simmons. 2 Contents Introduction Type System Variables Functions Module System I/O Conclusions.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Grab Bag of Interesting Stuff. Topics Higher kinded types Files and handles IOError Arrays.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
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.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Chapter 5 Polymorphic and Higher-Order Functions.
Functional Programming Professor Yihjia Tsai Tamkang University.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
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.
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
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.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Overview of the Haskell 98 Programming Language
© 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.
COMP313A Functional Programming (1)
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Office : S “Language.
Com Functional Programming Lazy Evaluation Marian Gheorghe Lecture 13 Module homepage Mole & ©University of Sheffieldcom2010.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
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,
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
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.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Functional Programming
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
A lightening tour in 45 minutes
More Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 30-Nov-18.
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
Functions and patterns
Programming Languages
Functions and patterns
Haskell Dealing with impurity 29-May-19.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 28-Jun-19.
Presentation transcript:

1 Haskell Kevin Atkinson John Simmons

2 Contents Introduction Type System Variables Functions Module System I/O Conclusions

3 History of Haskell Named for Haskell Curry Created by committee from FPCA conference, 1987 –Needed a “standard” non-strict, purely functional language Latest version: Haskell 98

4 Goals of the Haskell design Suitability for teaching, research, and applications Complete description via the publication of a formal syntax and semantics Free availability Basis of ideas that enjoy a wide consensus Reduction of unnecessary diversity in functional programming languages

5 Interpreters and Compilers GHC nhc98 HBC / HBI Hugs

6 Distinguishing features Lazy evaluation Stateless No exception system - errors are considered equivalent to _|_

7 Variants and extensions Parallel variants Object support Pattern guards Views

8 The Haskell type system value :: Type Type inference Pre-defined types –Integer, Real, String, etc. <= case sensitive! –Not special Polymorphic types User-defined types

9 Keywords type and data Enumerated types, renamed types, tuples (record types), lists Type classes –ex. Equality types –Inheritance ex. Equality => Ordered Kinds of types - verify type constructors –* means a value type

10 Thunks and ! Lazy evaluation: the computation needed to compute a field value is stored until needed in a thunk ! Will force immediate evaluation –Can help constrain types –Use with caution

11 Variables No keywords to define Lists (constructor is : ) Arrays –must open the Array module Infinite data structures

12 Basic Stuff Due to layout rules Haskell syntax is rather elegant and generally easy to understand Functions similar to ML Pattern and Wildcards like ML –but also more powerful Pattern Gards –Case clause to pattern match inside of functions Let like ML to define bindings –but also where as part of function and case clause syntax to declare binding after the expression

13 More on Lists [ f x | x <- xs, x < 10 ] –Reads "the list of all f x such that x is drawn from xs where x < 10" –x <- xs is a generator, x < 10 is a guard More than one generator can be specifed: [ (x,y) | x <- xs, y <- ys ] –Which forms the cartesian product of the two lists xs and ys [1.. 10] => [1,2,3,4,5,6,7,8,9,10] [1,3.. 10] => [1, 3, 5, 7, 9] ints = [1, 2..] –take 10 ints => [1,2,3,4,5,6,7,8,9,10] –squares = map (^2) ints –take 3 squares => [1, 3, 9] numsFrom n = n : numsFrom (n+1) ones = 1 : ones

Fibonacci fib = 1 : 1 : [ a+b | (a,b) <- zip fib (tail fib) ] –Where zip returns the pairwise interleaving of its two list arguments: zip (x:xs) (y:ys) = (x,y) : zip xs ys zip xs ys = [] –An infinite list, defined in terms of itself, as if it were "chasing its tail."

15 Lazy Evaluation No Fixed Order of Evaluation let f x y = a where a = 20 * b b = x + 5 * c c = y * y As you already see infinite lists are not possible Proving program correctness is nothing more than a mathematical proof. Not so in ML because one still has to worry about order of evaluation Many things that were inefficient with strict evaluation or now possible in Haskell. –Can perform complicated operations by function composition. And Finally … No side effects or State.

16 Standard Prelude. $ $! flip curry/uncurry id const until head/tail ++ length concat reverse zip zipWith map fold* filter take/drop iterate repeat cycle span any/all elem sum/product maximum/minimum lines words

17 Using The Standard Prelude wordCount = length. words fact n = product (take n [1, 2..]) infixl 9.| (.|) = flip (.) doubleSpace = lines.| map (++ “\n\n”).| concat summary = words.| take 20.| unwords wordlist = words.| filter (\x -> length x > 3).| map (to lower).| group.| filter (\x length x >= 3).| map head.| map (++ “\n”).| concat

18 Error Conditions Often handled using the Maybe type defined in the Standard Prelude –data Maybe a = Just a | Nothing doQuery :: Query -> DB -> Maybe Record r :: Maybe Record r = case doQuery db q1 of Nothing -> Nothing Just r1 -> case do Query db (q2 r1) of Nothing -> Nothing Just r2 -> case doQuery db (q3 r2) of Nothing -> Nothing Just r3 ->... Which can get quite tedious after a while There has to be a better way…

19 And there is... thenMB :: Maybe a -> (a -> Maybe b) -> Maybe b mB `thenMB` f = case mB of Nothing -> Nothing Just a -> f a returnMB a = Just a r = doQuery q1 db `thenMB` \r1 -> doQuery (q2 r1) db `thenMB` \r2 -> doQuery (q3 r2) db `thenMB` \r3 -> returnMB r3 And what we have is the mathematical notion of Monad which is formally defined as: return a `then` f === f a m `then` return === m m `then` (\a -> f a `then` h) === (m `then` f) `then` h Monads

20 Haskell Monads In Haskell a Monad is a class defined something like: class Monad m where >>= :: m a -> (a -> m b) -> m b return :: a -> m a So for our Database example we will have something like instance Monad Maybe where (>>=) = thenMB return = returnMB doQuery q1 db >>= \r1 -> doQuery (q2 r1) db >>= \r2 -> doQuery (q3 r2) db >>= \r3 -> return r3 Which can be rewritten using Haskell’s do syntax do r1 <- doQuery q1 db r2 <- doQuery q2 db r3 <- doQuery q3 db return r3

21 Simulating State To simulate state when there is none simply pass in the state and return a copy of the new state. parm -> SomeState -> (res, SomeState) Functions that do so are known as State Transformers. We can model this notation in Haskell using a type synonym type StateT s a = s -> (a, s) parm -> StateT SomeState res

22 State Example addRec :: Record -> DB -> (Bool,DB) addRec :: Record -> StateT DB Bool newDB :: StateT DB Bool newDB db = let (bool1,db1) = addRec rec1 db (bool2,db2) = addRec rec2 db1 (bool3,db3) = delRec rec3 db2 in (bool1 && bool2 && bool3) As you can see this could get quite tedious However, with Monads we can abstract the state passing details away. The details or a bit complicated, but the end result will look something like this: do boo11 <- addRec rec1 bool2 <- addRec rec2 bool3 <- addRec rec3 return (bool1 && bool2 && bool3)

23 I / O To Perform I/O A State Transformer Must be used, for example to get a character: getChar :: FileHandle -> StateT World Char But we must make sure each World is only passed to one function so a Monad is used But this is open to abuse so an ADT must be used: data IO a = IO (StateT World a) So putChar is now: getChar :: FileHandle -> IO Char The end result is that any function which uses I/O must return the IO type. And the only way IO can be executed is by declaring the function “main :: IO ()”.

24 Finally A Complete Program import System (getArgs) main :: IO () main = do args do fstr putStrLn "usage: wc fname"

25 Hudak, P. et al (2000) “A Gentle Introduction to Haskell Version 98” Jones, S.P. et al (1999) “The Haskell 98 Report” Winstanley, Noel (1999) “What the Hell are Monads?” References