Using Haskell

59
Using Haskell Dan Vasicek 2010 – 02 – 21

description

Using Haskell. Dan Vasicek 2010 – 02 – 21. What is Haskell? (Theoretical Aspects). Haskell is a computer programming language In particular, it is a polymorphically statically typed , lazy , purely functional language, quite different from most other programming languages - PowerPoint PPT Presentation

Transcript of Using Haskell

Page 1: Using Haskell

Using Haskell

Dan Vasicek2010 – 02 – 21

Page 2: Using Haskell

What is Haskell? (Theoretical Aspects)

• Haskell is a computer programming language • In particular, it is a polymorphically

statically typed, lazy, purely functional language, quite different from most other programming languages

• The language is named for Haskell Brooks Curry, whose work in mathematical logic serves as a foundation for functional languages

• Haskell is based on the lambda calculus, hence the symbol, λ, is used as part of the Haskell logo

Page 3: Using Haskell

What is Haskell? (Practical Aspects)

• Two commands for your command line environment• ghc – Glasgow Haskell Compiler: produces executable

code from Haskell source• ghci – Glasgow Haskell Command line Interpreter:

– An environment for the execution of Haskell commands– Special commands for manipulating, & querying the

environment– Debugging commands– Foreign language (e.g. C++) interface

Page 4: Using Haskell

Why Consider Haskell?• Produces concise, provably correct code• Short code development time• Interactive and compilable• Built-in Concurrency and Parallelism• A high level modern programming language that

is a result of 20 years of development• Supported by a large library of packaged modules• Supplied with debuggers and profilers• Freely available (Open source, $0, complete

development package)

Page 5: Using Haskell

Installation of Glasgow Haskell Platform

• Download most recent version– Stable versions from:

http://hackage.haskell.org/platform • Windows version is 54 Megabytes (Feb 1, 2010)• Mac OS X is 106 Megabytes• Source tarball is 1.28 Megabytes

– Linux X86 & X86-64 ~ 72 Megabytes• http://haskell.org/ghc/download_ghc_6_10_4.html#x86linux

• Other Haskell Compilers - e.g. http://repetae.net/computer/jhc/ (Efficient)

Page 6: Using Haskell

Example GHCi Session - Strings

Page 7: Using Haskell

Glasgow Haskell Platform• GHC 6.10.4 - The state-of-the-art optimzing compiler for Haskell.• GHCi 6.10.4 - A bytecode interpreter and interactive environment for

Haskell• The GHCi debugger - An interactive, imperative-style debugger for Haskell• The GHC parallel runtime - a multicore language runtime, featuring

lightweight threads, thread sparks, affinity control, and a parallel garbage collector

• The Happy parser generator - Happy 1.18.4, a yacc-like parser generator for Haskell

• The Alex lexer generator - Alex 2.3.1, a lex-like lexer generator for Haskell• The Haddock Documentation tool - generator Haddock 2.4.2• The Cabal package distribution tool - cabal-install 0.6.2, download and

install new Haskell packages from Hackage• The hsc2hs foreign language binding tool - a preprocessor for binding

Haskell to C - (e.g. Access to HDF5, FITS, … data)• Unicode built-in

Page 8: Using Haskell

Editor Syntax highlighting and formatting support

• Emacs Haskell Mode• VIM “Superior Haskell Interaction Mode”

(SHIM)

Page 9: Using Haskell

List of Available Packages

• http://hackage.haskell.org/packages/hackage.html

• Currently there are 1800 Haskell Packages available

• Compare to 9033 Python packages available• See the appendix of these slides for a listing of

some Haskell Packages

Page 10: Using Haskell

Haskell Syntax – Some References

• http://www.haskell.org/onlinereport/syntax-iso.html - general syntax description

• http://www.haskell.org/onlinereport/standard-prelude.html - standard functions

• http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/ListDoc - Standard list functions

Page 11: Using Haskell

Simple Syntax

• Strongly Typed• Simple arithmetic• Arbitrary precision integers• String manipulation• List manipulation such as sorting

Page 12: Using Haskell

Positionally Sensitive Notation• The next character following any of the keywords

where, let, or of is what determines the starting column for the declarations in the where, let, or case expression being written

• Be sure that the starting column is further to the right than the starting column associated with the immediately surrounding clause (otherwise it would be ambiguous). The "termination" of a declaration happens when something appears at or to the left of the starting column associated with that binding form. (Haskell observes the convention that tabs count as 8 blanks; thus care must be taken when using an editor which may observe some other convention.)

Page 13: Using Haskell

Type Signatures (::)

• 5 :: Integer • 'a' :: Char• ”abc” :: [Char] (is a list of characters)• (+1) :: Integer -> Integer {e.g. (+1) 1 =2)}• [1,2,3] :: [Integer] - List of Integers • ('b',4) :: (Char,Integer) - Pair

Page 14: Using Haskell

Simple Arithmetic Using GHCi

• Prelude> 3+7• 10• Prelude> 2*8• 16• Prelude> 100/25• 4.0

Page 15: Using Haskell

Arbitrary Precision Integers in GHCi

• Prelude> 2^200• 1606938044258990275541962092341162602522202993782792835301376

• 61 digits (Same result as Mathematica)• Prelude> 1/3• 0.3333333333333333• Sixteen significant figures• log(10^300)/log(10) = 299.9999999999994

Page 16: Using Haskell

List Operations• “abc” is [‘a’, ‘b’, ‘c’]• “abc”!!0 is ‘a’ list item • “abc”!!2 is ‘c’ • “ab” ++ “cd” is “abcd” Concatination• head “abc” is ‘a’ car• tail “abc” is “bc” cdr• take 2 “abc” is “ab”• take 1 “abc” is “a” is [‘a’]• drop 1 “abc” is “bc”• reverse(“abcdefg”) is “gfedcba”• length(“abc”) is 3

Page 17: Using Haskell

Shorthand for lists of integers

• [1..4] [1,2,3,4]• [1,3..10] [1,3,5,7,9]• [1,3..] [1,3,5,7,9,11, … infinite sequence

Page 18: Using Haskell

More List Operations

• map (/2) [1, 2, 3] is [.5, 1.0, 1.5]• map sin [1, 2, 3] is [.84, .91, .14]• foldr (+) 0 [1, 2, 3, 4] is 10 (Σn=1

n=4n)• foldr (*) 0 [1..4] is 0• foldr (*) 1 [1,2,3,4] is 24 (4!)• filter (4<) [1, 2, 8, 7] is [8,7]• partition (<5) [1..8] returns• ([1,2,3,4],[5,6,7,8])

Page 19: Using Haskell

Define a simple function

• let sq(x) = x*x• sq(9)• 81• sq 5• 25• sq is a function that is no more special than *

or + (more on the next slide)

Page 20: Using Haskell

Redefine basic operations

• let x + y = x*y• Then 4+5=20

Page 21: Using Haskell

Type and definition of “map”using patterns

• map :: (a->b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs

• map f :: [a] [b]

Page 22: Using Haskell

GHCi Commands

• The “:” operator tells GHCi to expect a command• :? Or : Gives a summary of : commands• :browse – will list the current environment• :browse <module name> – will list the contents of a

module• :show – will display the syntax of the show command• :show bindings – will display current bindings• :! Cmd – pass Cmd to the shell• E.g. :!dir – will display the current directory (in DOS)

Page 23: Using Haskell

Example of the “:show” command

• :show bindings• sq :: (Num a) = > aa = _• Which says that sq is a function of numbers

that takes a number, returns a number, and prints the value of the number

Page 24: Using Haskell

Creating a Haskell Script

• :set editor "C:\Program Files\Vim\vim72\gvim.exe“

– Sets the editor to be gvim• :edit fac.hs• Creates a file called fac.hs containing

– fac(0) = 1– fac(n) = n*fac(n-1)

• :load fac • Compiles and loads fac.hs into the

environment

Page 25: Using Haskell

Executing “fac” script

• fac(3)• 6• fac(10)• 2628800• The function “fac” is called “primitive

recursive”

Page 26: Using Haskell

Validation of “fac”

• by induction• fac(0)=1• fac(1) =1• fac(2)=2• Fac(100) is a big number and is probably

correct

Page 27: Using Haskell

Ackermann’s Function

• Is not primitive recursive• Definition ------------------------------------------

– ack(0,n) = n+1– ack(m+1, 0) = ack(m,1)– ack(m,n) = ack(m-1,ack(m,n-1))

• Program Validation-----------------------------– ack(0,0) = 1 √– ack(2,0)= 5 √– ack(3,3) = 61 √

Page 28: Using Haskell

Inquire about the environment

• :show modules• Main ( fac.hs, interpreted)

Page 29: Using Haskell

GHCi Configuration

• When GHCi starts it executes config files in order:1. .ghci file2. appdata/ghc/ghci.conf where appdata is

• C:/Documents and Settings/user/Application Data (on MS Win)

• $HOME/.ghc/ghci.conf (on Unix)

3. $HOME/ghci.conf

Page 30: Using Haskell

Creating a GHCi configuration file

• :edit .ghci• Type a configuration script for example:

– :set editor "C:\Program Files\Vim\vim72\gvim.exe“

• Save this file in the default GHCi directory – To find the default directory see the results of the

• :!dir command on windows• :!pwd on Linux

• Now gvim will be the default editor instead of Notepad

Page 31: Using Haskell

Begin Functional Programming Segment

• Fundamental concepts– Functional Programming– Sessions and scripts– Polymorphic types– Order of evaluation– Patterns– Lazy evaluation– Side Effects

• Simple data types– Numbers– Lists– Trees

• Efficiency– Evaluation order– Lazy Evaluation– Space

• Abstract Data Types• Infinite lists• Monads• Parsing• Examples

Page 32: Using Haskell

Functional Programming

• A functional program is a function that solves a problem

• That function may involve several subsidiary functions and is described in a notation that obeys normal mathematical principles

• The result of the function is the solution of the problem and is disjoint from the input to the function

Page 33: Using Haskell

Fundamental Concepts

• Polymorphic Static types– length(list) – The list can have elements of any type.

So, length is polymorphic. It can be applied to lists of characters, numbers, tuples, lists, …

– length [] = 0– length (x:xs) = 1+ length xs

• Where [] is a pattern that means the empty list• And x:xs is a pattern that means x is the first element of the

input list and xs is the rest of the list– Pattern matching is an important component of

Haskell

Page 34: Using Haskell

Polymorphism

• head :: [a] -> a• head (x:xs) = x

tail :: [a] -> [a]tail (x:xs) = xs

• Both fail if presented with an empty list• Both work for lists of anything, even lists of

empty lists and are Polymorphic• Examples of the Hindley-Milner type system

Page 35: Using Haskell

Order of Evaluation

• Order of evaluation (simplification, or reduction) is not specified in a functional program

• sq(3+4) could be simplified as– sq(7) 7*7 49– (3+4)*(3+4) 7*(3+4) 7*749

• Both orders produce the same result• The independence of the result from the order is

a characteristic feature functional programs

Page 36: Using Haskell

Lazy Evaluation• Let three x = 3• Let infinity = infinity +1• Now simplify the expression

– three infinity– If we choose to simplify infinity first we get

• Three(infinity +1 +1 +1 and so on) • which does not terminate

– If we choose to simplify three first, • three infinity = 3• the expression terminates in one step

• Some simplification orders may terminate while others do not

• In GHCi three infinity =3

Page 37: Using Haskell

Lazy Evaluation

• Guarantees termination whenever termination is possible

Page 38: Using Haskell

Side Effects

• A pure function simply returns a value• A pure function has no internal state• A pure function cannot modify the input data• In GHCi values may be displayed by the

interactive environment• Monadic programming allows functional

programs to mimic imperative programs• Monads provide a way to execute “Commands”

and display values

Page 39: Using Haskell

Basic Data Types

• Bool • Char• Enumerations• Tuples

Page 40: Using Haskell

Unicode in Haskell

• Haskell 98 specification says that Haskell supports Unicode

• http://blog.kfish.org/2007/10/survey-haskell-unicode-support.html

• http://code.haskell.org/utf8-string/

Page 41: Using Haskell

Unicode table

Page 42: Using Haskell

Unicode Experiment

• Create a list of byte codes for some Hebrew characters:

• hebrew = ['\n', '\x05d0', '\x05d1', '\x05d2', '\x05d3', '\x05d4', '\x05d5', '\x05d6', '\x05d7', '\x05d8', '\x05d9','\x5da','\x5db','\x5dc','\x5de','\x5df', '\x05e0', '\x0e1', '\x05e2', '\x05e3', '\x05e4', '\x05e5', '\x05e6', '\x05e7', '\x05e8', '\x05e9' , '\x05ea', '\x05eb', '\x05ec', '\x05ed', '\x05ee', '\x05ef' , '\n','\n‘]

• putStr hebrew• Result on next slide

Page 43: Using Haskell

Result of “putStr hebrew”

Page 44: Using Haskell

Unicode Greek

The letters printed by my program are in the order

αβ Γ Π Σ σμτΦΘΩδ

And this does not agree with the order in the above table.

Page 45: Using Haskell

Encoding Problem

• Hexadecimal ‘\x05d0’ = ‘\1488’ decimal• So, my coding is not the problem

Page 46: Using Haskell

Quick Sort Algorithm

• qsort [] = []• qsort ( x:xs) = qsort (filter (<= x) xs) ++ • qsort (filter ( > x) xs)• Inefficient! Calls filter twice for xs• Can use (length (x:xs))2memory

Page 47: Using Haskell

Begin Appendix

• Details of available modules• Comparison to other languages• List of some Haskell functions

Page 48: Using Haskell

List of Packages

• http://hackage.haskell.org/packages/archive/pkg-list.html

Page 49: Using Haskell

Example: Algorithm package• binary-search library: Binary and exponential searches• Binpack library: Common bin-packing heuristics.• DecisionTree library: A very simple implementation of decision

trees for discrete attributes.• Diff library: O(ND) diff algorithm in haskell.• dom-lt library: The Lengauer-Tarjan graph dominators algorithm.• edit-distance library and programs: Levenshtein and restricted

Damerau-Levenshtein edit distances• funsat library and program: A modern DPLL-style SAT solver• garsia-wachs library: A Functional Implementation of the Garsia-

Wachs Algorithm• Graphalyze library: Graph-Theoretic Analysis library.• GraphSCC library: Tarjan's algorithm for computing the strongly

connected components of a graph.

Page 50: Using Haskell

Default Packages – provided by the downloaded system (283

functions)• ghc-prim • integer - Arbitrary Precision Integer Arithmetic

• base – basic data types and functions–31 data types

• rts

Page 51: Using Haskell

More Algorithms • hgal library: library for computation automorphism group and canonical labelling of a graph• hmm library: Hidden Markov Model algorithms• incremental-sat-solver library: Simple, Incremental SAT Solving as a Library• infinite-search library: Exhaustively searchable infinite sets.• iproute library: IP Routing Table• kmeans library: K-means clustering algorithm• ListTree library: Combinatoric search using ListT• markov-chain library: Markov Chains for generating random sequences with a user definable behaviour.• Munkres library: Munkres' assignment algorithm (hungarian method)• natural-sort library: User-friendly text collation• Numbers library: An assortment of number theoretic functions• NumberSieves library: Number Theoretic Sieves: primes, factorization, and Euler's Totient• palindromes library and program: Finding palindromes in strings• pqueue-mtl library: Fully encapsulated monad transformers with queuelike functionality.• presburger library: Cooper's decision procedure for Presburger arithmetic.• primes library: Efficient, purely functional generation of prime numbers• queuelike library: A library of queuelike data structures, both functional and stateful.• rangemin library: Linear range-min algorithms.• sat programs: CNF SATisfier• sat-micro-hs program: A minimal SAT solver• satchmo library: SAT encoding monad• satchmo-examples programs: examples that show how to use satchmo• satchmo-funsat library: funsat driver as backend for satchmo• teams library: Graphical modeling tools for sequential teams• TrieMap library: Automatic type inference of generalized tries.• union-find library: Efficient union and equivalence testing of sets.

Page 52: Using Haskell

Modules in the Default Packagearray bytestring Cabal containers directory editline filepath haskell98 hpc old-localeold- time

packedstring pretty process random readline syb template-haskell unix Win32

Page 53: Using Haskell

Some Haskell Functions (From the appendix of the book)

• (.) – Functional Composition• (.) :: (β γ) (αβ) (αγ)• (f.g)x =f(g x)

• (++) Concatenation of two lists• (++) :: [α] [α] [α] • [] ++ ys = ys• (x:xs) ++ ys = x: (xs ++ ys)

Page 54: Using Haskell

More Functions• (^) Conjunction• (^) :: Bool Bool Bool• True ^ x = x• False ^ x = False

• (v) Disjunction• (v) :: Bool Bool Bool• True v x = True• False v x = x

Page 55: Using Haskell

More Functions• (!!) List indexing• (!!) :: [a] Int a• []!!n = error “(!!): Index too large”• (x:xs)!!0 = x• (x:xs)!!(n+1) = xs!!n

• and returns the conjunction of a list of booleans• and :: [Bool] Bool• and = foldr (^) True

Page 56: Using Haskell

More Functions

• concat Concatineates a list of lists• concat :: [[a]] [a]• concat = foldr (++) []

• const creates a constant valued function• const :: a b a• const (x,y) = x

Page 57: Using Haskell

More Functions

• cross Applies a pair of functions to the corresponding elements of a pair

• cross :: (a b, c d) (a,c) (b,d)• cross (f,g) = pair(f.fst, g.snd)

• curry converts a non-curried function into a curried one

• curry :: ((a,b)c) (abc)• curry f x y = f(x,y)

Page 58: Using Haskell

List Functions documented at:

• http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/ListDoc/

Page 59: Using Haskell

Comparison to other languages• Haskell separates the definition of a type from the definition of the methods associated with that

type. A class in C++ or Java usually defines both a data structure (the member variables) and the functions associated with the structure (the methods). In Haskell, these definitions are separated.

• The class methods defined by a Haskell class correspond to virtual functions in a C++ class. Each instance of a class provides its own definition for each method; class defaults correspond to default definitions for a virtual function in the base class.

• Haskell classes are roughly similar to a Java interface. Like an interface declaration, a Haskell class declaration defines a protocol for using an object rather than defining an object itself.

• Haskell does not support the C++ overloading style in which functions with different types share a common name.

• The type of a Haskell object cannot be implicitly coerced; there is no universal base class such as Object which values can be projected into or out of.

• C++ and Java attach identifying information (such as a VTable) to the runtime representation of an object. In Haskell, such information is attached logically instead of physically to values, through the type system.

• There is no access control (such as public or private class constituents) built into the Haskell class system. Instead, the module system must be used to hide or reveal components of a class.