42
Functional Relational Programming Waging War on Complexity SPA 2006 Ben Moseley [email protected] Peter Marks public@indigomail .net

FRP Presentation Web

Embed Size (px)

Citation preview

Page 1: FRP Presentation Web

Functional Relational ProgrammingWaging War on Complexity

SPA 2006

Ben [email protected]

Peter [email protected]

Page 2: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

OverviewPart 1: Complexity

Types of complexity

Sources of complexity

Part 2: Functional Relational Programming

Functions & Relations

High-level architecture

Worked Example

Page 3: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

ConclusionsState is the root of all evils

Much complexity in our software is non-essential

Constraining our systems to only explicitly possessing essential state offers great promise in helping to control complexity

Separating non-essential control from essential logic reduces system complexity

FRP looks interesting and worth finding out more about

most

Page 4: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

“The complexity of software is an essential property”

Fred Brooks, “No silver bullet”, 1987

Page 5: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Complexity curve

Development

Com

ple

xit

y

Page 6: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Accidents and Essence

Essential Complexity is inherent in, and the essence of, the problem

Accidental Complexity is associated with building a system to solve the problem

Page 7: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

What was wrong with 4GLs?

They weren’t very good languages

Did not make the paradigm shift

Hardware wasn’t fast enough

Degenerated to bad 3GLs

… but RDBMSs and Spreadsheets exploded

Page 8: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Manifestations of Complexity

State

Control

Code Volume

Expression

Page 9: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Example: Quick Sort

qsort( int a[], int lo, int hi ) {

int h, l, p, t;

if (lo < hi) {

l = lo;

h = hi;

p = a[hi];

do {

while ((l < h) && (a[l] <= p))

l = l+1;

while ((h > l) && (a[h] >= p))

h = h-1;

if (l < h) {

t = a[l];

a[l] = a[h];

a[h] = t;

}

} while (l < h);

t = a[l];

a[l] = a[hi];

a[hi] = t;

qsort( a, lo, l-1 );

qsort( a, l+1, hi );

}

}

Duplication

Arbitrary order

Transient state

Missing abstraction

Optimisation to sort in place

Poor naming

Arbitrary order

X

Unclear: should not include hi

Specific algorithm

Page 10: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Activity: Wood and Trees

Working in groups of four

Examine a non-trivial software system

Distill its essence then focus on what remains

Attribute the accidents…

Page 11: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Accidental Complexity

StateWhat can be derived?What is purely transient?ControlWhen is order unimportant?ExpressionDuplication or redundancyInconsistency (particularly of idiom)Missing abstractionUnnecessary abstraction

Page 12: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Activity: Thinning

Classify accidental complexity instances

Platform

Language

Effluent – unwanted byproduct

Page 13: FRP Presentation Web

End of Part 1

Page 14: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Part 2 - Overview

Review of Complexity

Functional Relational Programming

Functions & Relations

High-level architecture

Worked Example

Exercise & Discussion

14

Page 15: FRP Presentation Web

Accidental Essential

Complexity Review

Accidental Complexity Essential Complexity

??

Page 16: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

InfrastructureInfrastructureInfrastructureInfrastructure

Desired Architecture

Accidental

Essential

Page 17: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

oobjbjoobjbj

oobjbjoobjbj

oobjbjoobjbj

oobjbjoobjbj

oobjbjoobjbj

oobjbjoobjbj

oobjbjoobjbj

oobjbjoobjbj

...NO!!

Objects...?

Page 18: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Functional Programming

Pure FP avoids state completely

Better to think of FP as a useful technique rather than a group of languages

Immutability

Passing and returning Functions

Advantages: Reasoning, Expressivity, Speed

Page 19: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Relational ModelFamiliar from database world ...

...but... Relational Model ≠ SQL !!!

Powerful and flexible

Sound mathematical foundation

name groupgroup dob

ben 7 21/05/74sam 4 03/05/81jon 3 17/06/92

mark 4 12/11/89

Page 20: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

dob groupgroup name

21/05/74 7 ben

03/05/81 4 sam

17/06/92 3 jon

12/11/89 4 mark

dob groupgroup name

17/06/92 3 jon

12/11/89 4 mark

21/05/74 7 ben

03/05/81 4 sam

name groupgroup dob

ben 7 21/05/74

sam 4 03/05/81

jon 3 17/06/92

mark 4 12/11/89

Structural component is based on Sets

So, no duplicates & ordering irrelevant

name groupgroup dob

jon 3 17/06/92

mark 4 12/11/89

ben 7 21/05/74

sam 4 03/05/81

Relational Model

name groupgroup dob

ben 7 21/05/74

sam 4 03/05/81

jon 3 17/06/92

mark 4 12/11/89

Page 21: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Relational Model

Structure

Manipulation (Relational Algebra - not SQL!)

Integrity

Data Independence

USERS

name groupgroup dob

ben 7 21/05/74sam 4 03/05/81jon 3 17/06/92

mark 4 12/11/89

Page 22: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Functional Relational Programming

What is FRP?

A new (as far as we know), unproven way of building large systems

Based on some old ideas

Aims for SIMPLICITY above all else

An (internal) Architecture not Language...

Page 23: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

InfrastructureInfrastructureInfrastructureInfrastructure

FRP Architecture

Accidental

Essential

Page 24: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

InfrastructureInfrastructureInfrastructureInfrastructure

FRP Architecture

Accidental

State & Control

EssentialLogic

EssentialState

Page 25: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

FRP ArchitectureInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

RelationsRelations

FunctionsFunctions

Interfaces

Page 26: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

FRP Running ExampleInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

Editorial system for a Journal

People submit Papers, Chief Editor posts them for Review (on private editorial website), they are Rejected / Accepted and Published

Main Entities: Paper, Journal

Each Paper conceptually has a lifecycle...

Page 27: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

PaperLifecycle

InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

ProposedProposedProposedProposed Under Under ReviewReviewUnder Under ReviewReview

Awaiting Awaiting CorrectionCorrection

ss

Awaiting Awaiting CorrectionCorrection

ss

RejectedRejectedRejectedRejected

PublishePublishedd

PublishePublishedd

AcceptedAcceptedAcceptedAccepted

(“State” Transition Diagram)(“State” Transition Diagram)(“State” Transition Diagram)(“State” Transition Diagram)

Page 28: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential StateInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

This consists solely of definitions of Relations representing the user’s inputs to the system

Basically just a schema for the inputs

The whole system will be driven by users updating these input relations at runtime

No Derived values here (since not “essential” they be stored rather than rederived)

Page 29: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

PAPER

paper authorauthor submitted

DB Theory ian 21/05/06Pi Calculus sam 03/05/06

OODBs tim 17/06/06Ruby On Rails bob 12/11/06

POSTING

paper posted

DB Theory 23/05/06Pi Calculus 05/05/06

Ruby On Rails 15/11/06

OPINION

paper reviewerreviewer opinion

DB Theory adam GOODDB Theory fred BADPi Calculus adam GOODPi Calculus fred GOODPi Calculus simon GOOD

Ruby On Rails adam GOODRuby On Rails fred GOODRuby On Rails simon GOOD

JOURNAL

issueissue paper

17 Pi Calculus18 Ruby On Rails

Essential State Example InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

Page 30: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential LogicInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

Definitions of Derived Relations

i.e. intermediate and output relations

Defined in terms of Essential State relations

Uses relational algebra (not SQL!)

Uses (pure) user-defined functions

Essentially similar to View defns in an RDBMS

Page 31: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential LogicInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

Integrity Constraints

Not concerned with State at all (eg provides no facilities for manipulating State)

Page 32: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential Logic Example InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

OPINION

paper reviewerreviewer opinion

DB Theory adam GOODDB Theory fred BADPi Calculus adam GOODPi Calculus fred GOODPi Calculus simon GOOD

Ruby On Rails adam GOODRuby On Rails fred GOODRuby On Rails simon GOOD

OVERVIEW = project(OPINION, paper opinion)

OVERVIEW

paper opinion

DB Theory GOODDB Theory BADPi Calculus GOOD

Ruby On Rails GOOD

Page 33: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential Logic Example InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

OPINION

paper reviewerreviewer opinion

DB Theory adam GOODDB Theory fred BADPi Calculus adam GOODPi Calculus fred GOODPi Calculus simon GOOD

Ruby On Rails adam GOODRuby On Rails fred GOODRuby On Rails simon GOOD

BAD_OPINIONS = restrict(OPINION | opinion == BAD)

BAD_OPINIONS

paper reviewer opinion

DB Theory fred BAD

Page 34: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential Logic Example InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

REJECTED = project(BAD_OPINIONS, paper)

BAD_OPINIONS

paper reviewer opinion

DB Theory fred BAD

REJECTED

paper

DB Theory

Page 35: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential Logic Example InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

OPINION

paper reviewerreviewer opinion

DB Theory adam GOODDB Theory fred BADPi Calculus adam GOODPi Calculus fred GOODPi Calculus simon GOOD

Ruby On Rails adam GOODRuby On Rails fred GOODRuby On Rails simon GOOD

GOOD_OPINIONS = restrict(OPINION | opinion == GOOD)

GOOD_OPINIONS

paper reviewerreviewer opinion

DB Theory adam GOODPi Calculus adam GOODPi Calculus fred GOODPi Calculus simon GOOD

Ruby On Rails adam GOODRuby On Rails fred GOODRuby On Rails simon GOOD

Page 36: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

ACCEPTED = minus(project(GOOD_OPINIONS, paper), REJECTED)

ACCEPTED = minus(project(GOOD_OPINIONS, paper), REJECTED)

ACCEPTED = minus(project(GOOD_OPINIONS, paper), REJECTED)

ACCEPTED = minus(project(GOOD_OPINIONS, paper), REJECTED)

Essential Logic Example InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

GOOD_OPINIONS

paper reviewerreviewer opinion

DB Theory adam GOODPi Calculus adam GOODPi Calculus fred GOODPi Calculus simon GOOD

Ruby On Rails adam GOODRuby On Rails fred GOODRuby On Rails simon GOOD

REJECTED

paper

DB Theory

ACCEPTED...

paper

DB Theory

Pi CalculusRuby On Rails

ACCEPTED

paper

Pi CalculusRuby On Rails

Page 37: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential LogicInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

SCORE

paper reviewerreviewer score

DB Theory adam 98%DB Theory fred 48%Pi Calculus adam 79%Pi Calculus fred 73%Pi Calculus simon 74%

Ruby On Rails adam 83%Ruby On Rails fred 89%Ruby On Rails simon 77%

OPINION

paper reviewerreviewer score opinion

DB Theory adam 98% GOODDB Theory fred 48% BADPi Calculus adam 79% GOODPi Calculus fred 73% GOODPi Calculus simon 74% GOOD

Ruby On Rails adam 83% GOODRuby On Rails fred 89% GOODRuby On Rails simon 77% GOOD

opinion=opinionFromScore(score))

OPINION =extend(SCORE,

Page 38: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Essential LogicInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

Comprised of:

Derived Relations

User-defined functions

Constraints

No State, no Control

Page 39: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Accidental State & Control (ie Performance)

InfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

Basically this is just a configuration file:

Declarative specification of where State should be used

Declarative specification of what Control should be used (eg which bits should be eager, which lazy)

Has NO affect whatsoever on the logic (i.e. correctness) of the system

Page 40: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

InfrastructureInfrastructureInfrastructureInfrastructureInfrastructure

Accidental

State & Control

EssentialLogic

EssentialState

Completely independent of all the rest of the system - totally “system-agnostic”

Can be written fairly easily in any language (eg 2000 LOC Java, 1500 LOC Scheme)

Determines what performance settings are available to the Accidental component

May provide type-checking, basic functions etc

Page 41: FRP Presentation Web

Ben Moseley & Peter Marks SPA 2006

Activity - DVD Rentals

System to manage DVD Store:

Give info about Stock

Give info about Rentals

Assume the existence of any useful function

Don’t worry about Syntax!

Page 42: FRP Presentation Web

Functional Relational Programming

SPA 2006

Ben [email protected]

Peter [email protected]

et