Top Banner
Functional Design and Programming Lecture 1: Functional modeling, design and programming
22

Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Functional Design and Programming

Lecture 1:

Functional modeling, design and programming

Page 2: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Literature

Paulson, chap. 1 (excl. 1.7, 1.8) Paulson, chap. 2:

Value declarations (2.1-2.3) Numbers, characters, strings (2.4-2.6) Pairs, tuples, records (2.7-2.10) The evaluation of expressions (2.11-2.16)

Page 3: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Programming paradigms

Imperative programming Procedural Object-oriented

Functional programming (Logic programming)

Page 4: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Imperative Programming

Basic machine model: Machine has a state (memory, state of devices) State is acted upon by commands (e.g. “move a

byte to some location in memory’’) Programs describe sequences of state changes by

providing simple commands (“assign some value to a variable”) and constructs for composing those.

Page 5: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Procedural programming

Central notion: procedure. Purpose: Code factoring, reuse.

Procedure is a named piece of code (sequence of commands), parameterized by formal arguments.

Procedures can be called (invoked) by referring to their name and providing actual arguments (values) that are assigned to the corresponding formal arguments.

Page 6: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Object-oriented programming

Central concept: object. Purpose: Code factoring, abstraction (safety), reuse.

Encapsulation of data and the operations (procedures) that operate on those data.

Page 7: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Functional programming

Basic model: Machine consists of input ports and output ports. A program describes a function which computes

outputs for given inputs.

Central notions: value (incl. function), referential transparency, compositionality.

Page 8: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Referential transparency

Expression: describes a computation and its result (“value”)

Referential transparency: any computation can be replaced by its value in

any context, without affecting the observable results.

Page 9: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Example

function gcd(m,n: integer): integer; var prevm: integer;begin while m <> 0 do begin prevm := m; m := n mod m; n := prevm end; gcd := nend;

fun gcd (m, n) = if m = 0 then n else gcd (n mod m, m);

Page 10: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Functional programming: Characteristics

Powerful ‘bulk’ data types, e.g. lists, trees. Higher-order functions: functions as values (can be

passed as parameters to and returned from other functions, stored in data structures)

Recursion: Recursively defined functions and types. Pattern matching: convenient access to components

of data structures

Page 11: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Functional programming: Characteristics...

Polymorphism: Flexible and safe reuse of functions with many types of arguments

Safe composition: Support for modular composition.

High-level programming: automatic memory management no low-level data types

Page 12: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Functional Programming: Principles

Orthogonality: No ad-hoc restrictions on how language constructs can be combined.

Rigor: Languages are well-defined; support for rigorous reasoning about behavior of programs

Safety: Static and dynamic checking of safety of operations; careful design of basic types and their operations.

Page 13: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Names, functions and types

Values: Values have no identity, no birth date, no

expiration date, and they can’t change (ever hear of a 5 turning into a 6?); they are unchanging and immortal.

E.g., the string “Susanne”, the number denoted by the Arabic numeral 5, the list of natural numbers from 0 to 5: [0,1,2,3,4,5], etc.

Page 14: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Names, functions and types...

Names: Names refer to things (values, people, etc.) Names can be bound to values in constructs called

declarations. In this fashion we can refer to values by their names. (Q: How many names can a value have?)

Some values have predefined names (constants) that cannot be bound to other values; e.g. 60 is the standard name (“numeral”) for a particular natural number.

NB: Names are not values. Susanne as a name is not the same as the string “Susanne”, which is a value.

Page 15: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Identifiers in SML

Identifiers: Those names that can be bound to values in declarations Alphabetic names (length) Symbolic names (@) Long identifiers (String.sub)

Some identifiers are predefined; that is, they are bound to a value at program startup, but can be bound in a declaration to a new value.

Page 16: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Strings and characters

String constants: ”Susanne” Character constants: #”a”

Page 17: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Truth values (“Booleans”) and conditional expressions

Boolean constants: true, false Conditional expressions:if ... then ... else ...

E.g.:fun sign(n) = if n>0 then 1else if n=0 then 0else (* n<0 *) ~1

Page 18: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Pairs, tuples, records

Pairs (2-tuples): e.g.,(5, 8), (“Susanne”, 30)

Tuples (n-tuples): e.g., (5, 8, 9), (“Susanne”, 30, #”a”)

Records (named tuples): e.g., { firstName = “Susanne”, age = 30, empCategory = #”a” }

Page 19: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Pairs, tuples, records...

Record selection:val tup1 = (5, 8);val tup2 = (“Susanne”, 30, #”a”);val empRec = {...};#1 tup1;#3 tup2;#firstName empRec;

Page 20: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Record types

Tuple types: int * int, string * int * char

Record types: {firstName: string, age: int, empCategory: char}

Page 21: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Type declarations

Type declaration: Binding of type (expression) to identifier.

E.g., type empRecType = {firstName: string, age: int, empCategory: char}

Page 22: Functional Design and Programming Lecture 1: Functional modeling, design and programming.

Expression evaluation

General idea: Compute the values of the subexpressions (in SML typically from left-to-right). Then perform the resulting operation on the values.