PROGRAMMING IN HASKELL - PowerPoint PPT Presentation

slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
PROGRAMMING IN HASKELL PowerPoint Presentation
Download Presentation

play fullscreen
1 / 47
Download Presentation
Download Presentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes

  2. batch program inputs outputs Introduction To date, we have seen how Haskell can be used to write batch programs that take all their inputs at the start and give all their outputs at the end.

  3. keyboard interactive program inputs outputs screen However, we would also like to use Haskell to write interactive programs that read from the keyboard and write to the screen, as they are running.

  4. The Problem Haskell programs are pure mathematical functions: • Haskell programs have no side effects. However, reading from the keyboard and writing to the screen are side effects: • Interactive programs have side effects.

  5. The Solution Interactive programs can be written in Haskell by using types to distinguish pure expressions from impure actions that may involve side effects. IO a The type of actions that return a value of type a.

  6. For example: The type of actions that return a character. IO Char The type of purely side effecting actions that return no result value. IO () Note: • () is the type of tuples with no components.

  7. Basic Actions The standard library provides a number of actions, including the following three primitives: • The action getChar reads a character from the keyboard, echoes it to the screen, and returns the character as its result value: getChar :: IO Char

  8. The action putChar c writes the character c to the screen, and returns no result value: putChar :: Char  IO () • The action return v simply returns the value v, without performing any interaction: return :: a  IO a

  9. Sequencing A sequence of actions can be combined as a single composite action using the keyword do. For example: a :: IO (Char,Char) a = do x  getChar getChar y  getChar return (x,y)

  10. Derived Primitives • Reading a string from the keyboard: getLine :: IO String getLine = do x  getChar if x == '\n' then return [] else do xs  getLine return (x:xs)

  11. Writing a string to the screen: putStr :: String  IO () putStr [] = return () putStr (x:xs) = do putChar x putStr xs • Writing a string and moving to a new line: putStrLn :: String  IO () putStrLn xs = do putStr xs putChar '\n'

  12. Example We can now define an action that prompts for a string to be entered and displays its length: strlen :: IO () strlen = do putStr "Enter a string: " xs  getLine putStr "The string has " putStr (show (length xs)) putStrLn " characters"

  13. For example: > strlen Enter a string: abcde The string has 5 characters Note: • Evaluating an action executes its side effects, with the final result value being discarded.

  14. Hangman Consider the following version of hangman: • One player secretly types in a word. • The other player tries to deduce the word, by entering a sequence of guesses. • For each guess, the computer indicates which letters in the secret word occur in the guess.

  15. The game ends when the guess is correct. We adopt a top down approach to implementing hangman in Haskell, starting as follows: hangman :: IO () hangman = do putStrLn "Think of a word: " word  sgetLine putStrLn "Try to guess it:" guess word

  16. The action sgetLine reads a line of text from the keyboard, echoing each character as a dash: sgetLine :: IO String sgetLine = do x  getCh if x == '\n' then do putChar x return [] else do putChar '-' xs  sgetLine return (x:xs)

  17. Note: • The action getCh reads a character from the keyboard, without echoing it to the screen. • This useful action is not part of the standard library, but is a special Hugs primitive that can be imported into a script as follows: primitive getCh :: IO Char

  18. The function guess is the main loop, which requests and processes guesses until the game ends. guess :: String  IO () guess word = do putStr "> " xs  getLine if xs == word then putStrLn "You got it!" else do putStrLn (diff word xs) guess word

  19. The function diff indicates which characters in one string occur in a second string: diff :: String  String  String diff xs ys = [if elem x ys then x else '-' | x  xs] For example: > diff "haskell" "pascal" "-as--ll"

  20. Exercise Implement the game of nim in Haskell, where the rules of the game are as follows: • The board comprises five rows of stars: 1: * * * * * 2: * * * * 3: * * * 4: * * 5: *

  21. Two players take it turn about to remove one or more stars from the end of a single row. • The winner is the player who removes the last star or stars from the board. Hint: Represent the board as a list of five integers that give the number of stars remaining on each row. For example, the initial board is [5,4,3,2,1].

  22. Type Declarations In Haskell, a new name for an existing type can be defined using a type declaration. type String = [Char] String is a synonym for the type [Char].

  23. Type declarations can be used to make other types easier to read. For example, given type Pos = (Int,Int) we can define: origin :: Pos origin = (0,0) left :: Pos  Pos left (x,y) = (x-1,y)

  24. Like function definitions, type declarations can also have parameters. For example, given type Pair a = (a,a) we can define: mult :: Pair Int  Int mult (m,n) = m*n copy :: a  Pair a copy x = (x,x)

  25. Type declarations can be nested: type Pos = (Int,Int) type Trans = Pos  Pos However, they cannot be recursive: type Tree = (Int,[Tree])

  26. Data Declarations A completely new type can be defined by specifying its values using a data declaration. data Bool = False | True Bool is a new type, with two new values False and True.

  27. Note: • The two values False and True are called the constructors for the type Bool. • Type and constructor names must begin with an upper-case letter. • Data declarations are similar to context free grammars. The former specifies the values of a type, the latter the sentences of a language.

  28. Values of new types can be used in the same ways as those of built in types. For example, given data Answer = Yes | No | Unknown we can define: answers :: [Answer] answers = [Yes,No,Unknown] flip :: Answer  Answer flip Yes = No flip No = Yes flip Unknown = Unknown

  29. The constructors in a data declaration can also have parameters. For example, given data Shape = Circle Float | Rect Float Float we can define: square :: Float  Shape square n = Rect n n area :: Shape  Float area (Circle r) = pi * r^2 area (Rect x y) = x * y

  30. Note: • Shape has values of the form Circle r where r is a float, and Rect x y where x and y are floats. • Circle and Rect can be viewed as functions that construct values of type Shape: Circle :: Float  Shape Rect :: Float  Float  Shape

  31. Not surprisingly, data declarations themselves can also have parameters. For example, given data Maybe a = Nothing | Just a we can define: safediv :: Int  Int  Maybe Int safediv _ 0 = Nothing safediv m n = Just (m `div` n) safehead :: [a]  Maybe a safehead [] = Nothing safehead xs = Just (head xs)

  32. Recursive Types In Haskell, new types can be declared in terms of themselves. That is, types can be recursive. data Nat = Zero | Succ Nat Nat is a new type, with constructors Zero :: Nat and Succ :: Nat  Nat.

  33.   Note: • A value of type Nat is either Zero, or of the form Succ n where n :: Nat. That is, Nat contains the following infinite sequence of values: Zero Succ Zero Succ (Succ Zero)

  34. = 1 + (1 + (1 + 0)) 3 • We can think of values of type Nat as natural numbers, where Zero represents 0, and Succ represents the successor function 1+. • For example, the value Succ (Succ (Succ Zero)) represents the natural number

  35. Using recursion, it is easy to define functions that convert between values of type Nat and Int: nat2int :: Nat  Int nat2int Zero = 0 nat2int (Succ n) = 1 + nat2int n int2nat :: Int  Nat int2nat 0 = Zero int2nat (n+1) = Succ (int2nat n)

  36. Two naturals can be added by converting them to integers, adding, and then converting back: add :: Nat  Nat  Nat add m n = int2nat (nat2int m + nat2int n) However, using recursion the function add can be defined without the need for conversions: add Zero n = n add (Succ m) n = Succ (add m n)

  37. = Succ (add (Succ Zero) (Succ Zero)) = Succ (Succ (add Zero (Succ Zero)) = Succ (Succ (Succ Zero)) Note: • The recursive definition for add corresponds to the laws 0+n = n and (1+m)+n = 1+(m+n). For example: add (Succ (Succ Zero)) (Succ Zero)

  38. + 1  2 3 Arithmetic Expressions Consider a simple form of expressions built up from integers using addition and multiplication.

  39. Using recursion, a suitable new type to represent such expressions can be declared by: data Expr = Val Int | Add Expr Expr | Mul Expr Expr For example, the expression on the previous slide would be represented as follows: Add (Val 1) (Mul (Val 2) (Val 3))

  40. Using recursion, it is now easy to define functions that process expressions. For example: size :: Expr  Int size (Val n) = 1 size (Add x y) = size x + size y size (Mul x y) = size x + size y eval :: Expr  Int eval (Val n) = n eval (Add x y) = eval x + eval y eval (Mul x y) = eval x * eval y

  41. Note: • The three constructors have types: Val :: Int  Expr Add :: Expr  Expr  Expr Mul :: Expr  Expr  Expr • Many functions on expressions can be defined by replacing the constructors by other functions using a suitable fold function. For example: eval = fold id (+) (*)

  42. 5 7 3 6 9 1 4 Binary Trees In computing, it is often useful to store data in a two-way branching structure or binary tree.

  43. Using recursion, a suitable new type to represent such binary trees can be declared by: data Tree = Leaf Int | Node Tree Int Tree For example, the tree on the previous slide would be represented as follows: Node (Node (Leaf 1) 3 (Leaf 4)) 5 (Node (Leaf 6) 7 (Leaf 9))

  44. We can now define a function that decides if a given integer occurs in a binary tree: occurs :: Int  Tree  Bool occurs m (Leaf n) = m==n occurs m (Node l n r) = m==n || occurs m l || occurs m r But… in the worst case, when the integer does not occur, this function traverses the entire tree.

  45. Now consider the function flatten that returns the list of all the integers contained in a tree: flatten :: Tree  [Int] flatten (Leaf n) = [n] flatten (Node l n r) = flatten l ++ [n] ++ flatten r A tree is a search tree if it flattens to a list that is ordered. Our example tree is a search tree, as it flattens to the ordered list [1,3,4,5,6,7,9].

  46. Search trees have the important property that when trying to find a value in a tree we can always decide which of the two sub-trees it may occur in: occurs m (Leaf n) = m==n occurs m (Node l n r) | m==n = True | m<n = occurs m l | m>n = occurs m r This new definition is more efficient, because it only traverses one path down the tree.

  47. (1) Using recursion and the function add, define a function that multiplies two natural numbers. (2) Define a suitable function fold for expressions, and give a few examples of its use. (3) A binary tree is complete if the two sub-trees of every node are of equal size. Define a function that decides if a binary tree is complete. Exercises