1 / 34

Modeling with Haskell

Modeling with Haskell. Scientific Seminar 03/04 Gerhard Navratil. Contents. Functional Programming Haskell Modeling Example. Functional Programming. “Everything is a function” Parameters to result Different from imperative programming – no side effects!. Result. Parameters. Function.

aileen
Télécharger la présentation

Modeling with Haskell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil

  2. Contents • Functional Programming • Haskell • Modeling • Example

  3. Functional Programming • “Everything is a function” • Parameters to result • Different from imperative programming – no side effects! Result Parameters Function

  4. Key Elements • Recursion replaces loops • No side effects • No change of value (x = x+1) •  ‘Easy’ to read ‘Easy’ to test Mathematically clean

  5. Constants and Test Data … are functions, too • Constants are constant functionse.g. pi = 3.1415927… • Test data are modeled as constants

  6. Testing the Model • Imperative programming: Debugger • No Debugger necessary for functional programming! • Each function tested separately • No side effects  the function will always react like that • Never forget: Special cases (Div by Zero)

  7. Haskell

  8. The Syntax • Predefined typesBool, Int, Integer, Float, Double, Rational, Char, String, Pairs, Lists Maybe • Type of a functionname :: T1 -> T2 -> … Tn -> Tr • Implementation of a functionname v1 v2 … vn = res

  9. Indentation matters! Examples • increment :: Int -> Intincrement x = x + 1 • Quadratic polynom:root :: (Float,Float,Float) -> (Float,Float) • root (a,b,c) = (x1,x2) where x1 = e + sqrt d / ( 2 * a ) x2 = e – sqrt d / ( 2 * a ) d = b * b – 4 * a * c e = - b / ( 2 * a )

  10. Lists • List = Structure that holds any number of elements of the same type • List of Integers, Stings, Booleans, … • Strings are lists of characters • e.g. [1,2,3], [‘a’, ‘b’], [] • Building: 1 : [2,3]  [1,2,3]

  11. Working with Lists: Recursion • sumList :: [Int] -> IntsumList [] = 0sumList (x:xs) = x + sumList xs • Usually hidden inside other functions: • head, tail, take • sumList x = head x + sumList (tail s) • length, reverse, ++ • 2nd order: map, filter, fold, zip

  12. Example map Coding of texts: coding :: String -> [Int] coding s = map ord s coding “Navratil” [78,97,118,114,97,116,105,108]

  13. Example filter Filtering numbers: filter_even :: [Int] -> [Int] filter_even ns = filter even ns filter_even [1,2,3,4,5] [2,4]

  14. Example fold Summing up all numbers in a list: addAll :: [Int] -> Int addAll ns = foldl (+) 0 ns addAll [1,3,5] 9

  15. Example zip Combining neighboring elements of a list: neighbors :: [Int] -> [(Int,Int)] neighbors x = zip (take (length x - 1) x) (tail x) neighbors [1,2,3] [(1,2),(2,3)]

  16. Representation of Data • Type-synonymstype Name = Stringtype Year = Inttype Month = Inttype Dat = Int • User-defined data typesdata DayOfWeek = Mon | Tue | …data Birthday = B Year Month Daydata Employee = E Name Birthday

  17. Using Data Types • n :: Namen = “Gerhard Navratil” • e :: Employeee = E n (B 1969 8 25) • hire :: Employee -> [Employee] -> [employee]hire e []

  18. Parametrized Data Types • data List a = L a (List a) | Empty • li1, li2, li3 :: List Integerli1 = Emptyli2 = L 1 li1li3 = L 5 li2 • li3L 5 (L 1 Empty)

  19. Polymorphism • Roots of the quadratic equationroots :: (Floating a) => (a,a,a) -> (a,a) • Length of a listlength :: [a] -> Intlength [] = 0length (_:xs) = 1 + length xs

  20. Function Composition • Math: f (g (x)) = (g  f) (x) • Haskell: (g . f) xResult type of f = Parameter type of g • take (length x - 1) x(reverse . tail . reverse) x

  21. Guards • Sign function: • sign x | x < 0 = -1 | x == 0 = 0 | x > 0 = 1

  22. List Comprehension • [x | x <- [0 .. 100], odd x] • f xs ys = [x*y | x<-xs, y<-ys] • map f xs = [f x | x <- xs] • filter p xs = [x | x <- xs, p x]

  23. Modeling

  24. Algebraic Modeling • Set of sorts S • Set of Operators O • Set of Axioms for the operators

  25. Example 2D Vector • sorts: Vec, Float • operations make: Float Float -> Vec getX: Vec -> Float getY: Vec -> Float add: Vec Vec -> Vec sub: Vec Vec -> Vec • axioms add a,b = make (getX a + getX b) (getY a + getY b)sub a,b = make (getX a – getX b) (getY a – getY b)

  26. Example 2D Vector in Haskell class Vector vec where make ::Float -> Float -> vec getX, getY :: vec -> Float add, sub :: vec -> vec -> vec add a b = make (getX a + getX b) (getY a + getY b) sub a b = make (getX a – getX b) (getY a – getY b)

  27. Instances • Connection between classes and data types • type V2 = (Float, Float)instance Vector V2 where make x y = (x,y) getX (x,y) = x getY (x,y) = y • Same procedure fordata V2_2 = V Float Float

  28. Modeling is … • Defining elements • Combining elements • Until the model is as complete as we want it

  29. Example Database

  30. Database Elements • Have an identifier • Have attributes type Attrib = (String, String) class Objects o where object :: ID -> [Attrib] -> o getID :: o -> ID getAtts :: o -> [Attrib] getAttN :: String -> o -> String getAttN n o = (snd.head.filter((n==).fst).getAtts) o

  31. Database • Put objects in the DB • Read objects from the DB • Change objects in the DB class (Objects o) => Databases d owhere empty :: d o getLastID :: d o -> ID insert :: [Attrib] -> [d o] -> [d o] select :: ID -> d o -> o …

  32. Cadaster class (Databases c d, ListFuncs d) => Cadasters c d where inscribe :: c d -> d -> c d data2Doc :: c d -> d -> d formalTest :: c d -> d -> Bool register :: c d -> d -> c d test :: c d -> d -> Bool dbChanges :: c d -> d -> c d inscribe c d = if (formalTest c d) && (test rc nd) then dbChanges rc nd else c where rc = register c a nd = data2Doc rc a

  33. Cadastral Requests class Cadasters c d => Requests c d where owner :: c d -> ID -> d documents :: c d -> ID -> [d]

  34. Further Reading • Books: • Simon Peyton Jones: “Haskell 98 Language and Libraries”, Cambridge University Press, 2003, 272 pages. • Simon Thompson: “Haskell: The Craft of Functional Programming”, 2nd Edition, Addison-Wesley, 1999, 507 pages. • Richard Bird: “Introduction to Functional Programming using Haskell”, 2nd edition, Prentice Hall Press, 1998, 460 pages. • Tutorial texts are available on: www.haskell.org

More Related