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

22
Functional Design and Programming Lecture 1: Functional modeling, design and programming
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    268
  • download

    3

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

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)

Programming paradigms

Imperative programming Procedural Object-oriented

Functional programming (Logic 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.

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.

Object-oriented programming

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

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

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.

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.

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

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

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

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.

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.

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.

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.

Strings and characters

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

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

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” }

Pairs, tuples, records...

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

Record types

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

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

Type declarations

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

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

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.