Docsity

Haskell: A Modern Functional Programming Language, Slides of Computers and Information technologies

Partial preview of the text

Download Haskell: A Modern Functional Programming Language and more Slides Computers and Information technologies in PDF only on Docsity!

Haskell

Ian Holyer Ian Holyer

Haskell

  • Haskell was born in about 1992, which is newish, butHaskell was born in about 1992, which is newish, but before Java (1995) before Java (1995)
  • It is now the standard functional language, building onIt is now the standard functional language, building on a lot of previous experience a lot of previous experience
  • The Glasgow Haskell Compiler (ghc and ghci) is bestThe Glasgow Haskell Compiler (ghc and ghci) is best
  • A lot of libraries have been developed, includingA lot of libraries have been developed, including graphics, but to nothing like the same extent as Java graphics, but to nothing like the same extent as Java
  • As an academic language, it is regarded as very stableAs an academic language, it is regarded as very stable and well developed and well developed
  • As a commercial language, it has no big-nameAs a commercial language, it has no big-name compilers, and it has only a small minority following compilers, and it has only a small minority following

What is Haskell Good For?

  • YouYou cancan now do without it, so its main importance isnow do without it, so its main importance is as a source of ideas as a source of ideas
  • First, it is still a good intro to the declarative ideas usedFirst, it is still a good intro to the declarative ideas used in Maple, Prolog, Python, Ruby, HTML, XML, XSL, in Maple, Prolog, Python, Ruby, HTML, XML, XSL, SQL, BNF, JSP, Java beans, H/W description langs SQL, BNF, JSP, Java beans, H/W description langs
  • Second, its features are making their way into otherSecond, its features are making their way into other languages, e.g. generics were copied into Java, and languages, e.g. generics were copied into Java, and higher order functions into Python/Ruby so higher order functions into Python/Ruby so understanding Haskell first gives you a big head start understanding Haskell first gives you a big head start
  • Third, Haskell has a different way of making softwareThird, Haskell has a different way of making software flexible, which complements the OO way, and it is flexible, which complements the OO way, and it is worth learning to use these tricks in other languages worth learning to use these tricks in other languages

What Shall We Study?

  • TheThe lastlast thing we want to do is to learn about extrathing we want to do is to learn about extra obscure features of Haskell obscure features of Haskell
  • One of the main points of Haskell is that you don'tOne of the main points of Haskell is that you don't actually actually needneed very many language featuresvery many language features
  • We will look at two thingsWe will look at two things
  • One is the relationship between OO and FPOne is the relationship between OO and FP
  • The other is the way in which Haskell promotesThe other is the way in which Haskell promotes flexibility in software, and how those ideas can be flexibility in software, and how those ideas can be translated into other languages translated into other languages

Functional Decomposition

  • Before OO, this was an important design strategyBefore OO, this was an important design strategy
  • It means breaking down the overall problem intoIt means breaking down the overall problem into pieces, then breaking the pieces down further etc. pieces, then breaking the pieces down further etc.
  • This tends to lead to a hierarchy of functionsThis tends to lead to a hierarchy of functions
  • This is generally regarded as NONO now, because itThis is generally regarded as NONO now, because it leads to tight coupling between the components leads to tight coupling between the components
  • However, it is still the best design for some problemsHowever, it is still the best design for some problems (e.g. thinking of a compiler as a sequence of phases) (e.g. thinking of a compiler as a sequence of phases) and is unreasonably difficult in an OO language and is unreasonably difficult in an OO language (needing something like the Visitor Design Pattern) (needing something like the Visitor Design Pattern)
  • Also note that Haskell programs are not too tightlyAlso note that Haskell programs are not too tightly coupled because there is coupled because there is nono (mutable) global data(mutable) global data

Strong Types

  • Haskell has strong types (like Java etc)Haskell has strong types (like Java etc)
  • Strong types are ones where you can't cheat, like in CStrong types are ones where you can't cheat, like in C
  • They are essential for serious programming, and are theThey are essential for serious programming, and are the main scaling up problem for scripting languages main scaling up problem for scripting languages
  • The idea is that types make a lot of attempted programsThe idea is that types make a lot of attempted programs illegal (including some which would illegal (including some which would workwork ok)ok)
  • In return, the compiler gives you compile-time errorsIn return, the compiler gives you compile-time errors and warnings about possible bugs and warnings about possible bugs
  • These are a million times better than run-time errors,These are a million times better than run-time errors, which can't usually point to a line number or cause which can't usually point to a line number or cause
  • On the other hand, strong types can restrict flexibilityOn the other hand, strong types can restrict flexibility

Unions

  • The way unions work in Haskell is like this:The way unions work in Haskell is like this: data Result = Yes Int | No String report :: Result -> String report (Yes n) = "Found the item, position " ++ show n report (No s) = "Can't find the item because " ++ s data Result = Yes Int | No String report :: Result -> String report (Yes n) = "Found the item, position " ++ show n report (No s) = "Can't find the item because " ++ s
  • An object (or value or record) of the Result type canAn object (or value or record) of the Result type can have two different forms, with different fields have two different forms, with different fields
  • A function can recognise them and extract the fields byA function can recognise them and extract the fields by pattern matching (and build them using Yes and No) pattern matching (and build them using Yes and No)

Unions in Java

  • You can do the same in Java like this:You can do the same in Java like this: interface Result { String report(); } class Yes implements Result { int n; String report() { "Found" ... n ... } } class No implements Result { String s; String report() { "Can't find" ... s ... } } interface Result { String report(); } class Yes implements Result { int n; String report() { "Found" ... n ... } } class No implements Result { String s; String report() { "Can't find" ... s ... } }

Classes

  • Haskell's classes look like this:Haskell's classes look like this: class Show a where show :: a -> String instance Show Person where show p = forename p ++ " " ++ surname p instance Show Point where show p = "(" ++ x p ++ "," ++ y p ++ ")" class Show a where show :: a -> String instance Show Person where show p = forename p ++ " " ++ surname p instance Show Point where show p = "(" ++ x p ++ "," ++ y p ++ ")"
  • The class statement defines a Java-like interfaceThe class statement defines a Java-like interface
  • The instance statements define the function on twoThe instance statements define the function on two different types (just like "Person implements Show") different types (just like "Person implements Show")

Interfaces in Java

  • This time, it matches the Java more or less exactlyThis time, it matches the Java more or less exactly interface Show { String show(); } class Person implements Show { String show() { ... surname ... } } class Point implements Result { String show() { ... x ... y ... } } interface Show { String show(); } class Person implements Show { String show() { ... surname ... } } class Point implements Result { String show() { ... x ... y ... } }

Polymorphism

  • Polymorphism means "many forms" and refers to aPolymorphism means "many forms" and refers to a function working on data of many forms, i.e. types function working on data of many forms, i.e. types
  • The idea is not just to ease up on the type system, asThe idea is not just to ease up on the type system, as with data and class statements, but to make a function with data and class statements, but to make a function truly general, working on a parameterised range of types truly general, working on a parameterised range of types length :: [a] -> Int length [] = 0 length (x:xs) = 1 + length xs length :: [a] -> Int length [] = 0 length (x:xs) = 1 + length xs
  • Because the code doesn't touch elements inside the list,Because the code doesn't touch elements inside the list, the compiler knows that it will work on list-of-anything the compiler knows that it will work on list-of-anything

Generics in Java

  • Java has generics, based on Haskell's polymorphismJava has generics, based on Haskell's polymorphism List xs; int n = xs.size(); List xs; int n = xs.size();
  • AA^ ListList^ "knows" that it contains only strings"knows" that it contains only strings
  • The size() method is polymorphic in Haskell's senseThe size() method is polymorphic in Haskell's sense
  • The scheme works very well for library data structuresThe scheme works very well for library data structures
  • Using it for your own purposes can be subtle (e.g. doUsing it for your own purposes can be subtle (e.g. do you understand covariance?) you understand covariance?)

Laziness in Java

  • Laziness is a very important concept in Java, but lots ofLaziness is a very important concept in Java, but lots of different phrases are used for it: different phrases are used for it: - just in time - on demand - delayed execution - lazy constructors - factories

Laziness in Java

  • The most obvious equivalent in Java is streamsThe most obvious equivalent in Java is streams
  • An interactive streamAn interactive stream^ isis^ a potentially infinite list, and ita potentially infinite list, and it needs needs to be lazy so the code doesn't try to read itemsto be lazy so the code doesn't try to read items before they are available before they are available
  • Java deals with this by providingJava deals with this by providing separateseparate streamstream interfaces as opposed to just using list interfaces interfaces as opposed to just using list interfaces
  • Also, in many places in the Java libraries,Also, in many places in the Java libraries, iteratorsiterators provide a stream-like interface to the items in a data provide a stream-like interface to the items in a data structure, so as not to make any inefficient assumptions structure, so as not to make any inefficient assumptions about how they are stored or accessed about how they are stored or accessed
  • Could we do better in Java? I think we could! ByCould we do better in Java? I think we could! By giving streams and iterators an array-like notation giving streams and iterators an array-like notation