Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.

Similar presentations


Presentation on theme: "Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil."— 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! Parameters Function Result

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 functions e.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 types Bool, Int, Integer, Float, Double, Rational, Char, String, Pairs, Lists Maybe  Type of a function name :: T1 -> T2 -> … Tn -> Tr  Implementation of a function name v1 v2 … vn = res

9 Examples  increment :: Int -> Int increment 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 ) Indentation matters!

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] -> Int sumList [] = 0 sumList (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-synonyms type Name = String type Year = Int type Month = Int type Dat = Int  User-defined data types data DayOfWeek = Mon | Tue | … data Birthday = B Year Month Day data Employee = E Name Birthday

17 Using Data Types  n :: Name n = “Gerhard Navratil”  e :: Employee e = 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 Integer li1 = Empty li2 = L 1 li1 li3 = L 5 li2  li3 L 5 (L 1 Empty)

19 Polymorphism  Roots of the quadratic equation roots :: (Floating a) => (a,a,a) -> (a,a)  Length of a list length :: [a] -> Int length [] = 0 length (_:xs) = 1 + length xs

20 Function Composition  Math: f (g (x)) = (g  f) (x)  Haskell: (g. f) x Result 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

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  operationsmake: 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 for data 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 o where 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 www.haskell.org


Download ppt "Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil."

Similar presentations


Ads by Google