Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.

Slides:



Advertisements
Similar presentations
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Advertisements

F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Cse536 Functional Programming 1 6/28/2015 Lecture #3, Oct 4, 2004 Reading Assignments –Finish chapter 2 and begin Reading chapter 3 of the Text Today’s.
Chapter 5 Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
Lazy Functional Programming in Haskell
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
© M. Winter COSC 4P41 – Functional Programming COSC 4P41 Functional Programming Instructor: Michael Winter –Office J323 –Office Hours: Tue & Wed.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Overview of the Haskell 98 Programming Language
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Functional Programming Lecture 5 - Tuples. Packaging several values together Sometimes you need to package up several values into a single object –A function.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Chapter SevenModern Programming Languages1 A Second Look At ML.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Lecture 14: Advanced Topic: Functional Programming
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Functional Programming
Recursion.
Theory of Computation Lecture 4: Programs and Computable Functions II
Functions and patterns
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

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! Parameters Function Result

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

Constants and Test Data … are functions, too  Constants are constant functions e.g. pi = …  Test data are modeled as constants

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)

Haskell

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

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!

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]

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

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

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

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

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)]

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

Using Data Types  n :: Name n = “Gerhard Navratil”  e :: Employee e = E n (B )  hire :: Employee -> [Employee] -> [employee] hire e []

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)

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

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

Guards  Sign function:  sign x| x 0= 1

List Comprehension  [x | x <- [ ], 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]

Modeling

Algebraic Modeling  Set of sorts S  Set of Operators O  Set of Axioms for the operators

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)

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)

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

Modeling is …  Defining elements  Combining elements  Until the model is as complete as we want it

Example Database

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

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 …

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

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

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: