44
Mathematical Foundations of Bidirectional Transformations Michael Johnson Optus Macquarie Cyber Security Hub Macquarie University Sydney BXSS, Oxford, July 25-29, 2016

Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Mathematical Foundations of BidirectionalTransformations

Michael Johnson

Optus Macquarie Cyber Security HubMacquarie University Sydney

BXSS, Oxford, July 25-29, 2016

Page 2: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986

Page 3: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

Page 4: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

Page 5: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

Page 6: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996

Page 7: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

Page 8: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001

Page 9: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

Page 10: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006

Page 11: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

Page 12: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

oo //

Page 13: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

oo //

A bidirectional transformation

Page 14: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

X Yoo //X Y

A bidirectional transformation

(1) A consistency relation R between the possible states X and thepossible states Y

Page 15: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

X Yoo //X Y

A bidirectional transformation

(1) A consistency relation R between the possible states X and thepossible states Y , so R ⊂ X × Y

Page 16: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

X Yoo //X Y

A bidirectional transformation

(1) A consistency relation R between the possible states X and thepossible states Y , together with (2) consistency restorers.

Page 17: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

X Yoo //X Y

A bidirectional transformation

A consistency relation R between the possible states X and thepossible states Y , together with consistency restorers

/ : X × Y // X and . : X × Y // Y

Meertens

Page 18: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011

Page 19: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011

..

Page 20: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011

Page 21: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011

||yyyyyyyyyy

""EEEEEEE

Page 22: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011

W''OOOOOOOOO

Wwwoooooooo

Page 23: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011

||yyyyyyyyyy

""EEEEEEE

W''OOOOOOOOO

Wwwoooooooo

Page 24: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011 — The great big system in the sky

oo //||yyyyyyyyyy

""EEEEEEE

Page 25: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

History

1986 — Just communications and just computations

1996 — Web: Ordinary people’s computers communicate

2001 — Convergence: Phones manage data too

2006 — Synchronization

2011 — The great big system in the sky

V

SS

V

g

%%KKKKKKKKKK

Page 26: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Lens

(Asymmetric, set-based: S and V are sets, g and p are functions,and s, s ′ ∈ S and v , v ′ ∈ V .)

S

V

g

��V × S

S

p

OO

gs v� //

s

gs����s s ′s ′

v

Satisfying axiomsPutGet: g(p(v , s)) = vGetPut: p(g(s), s)) = s andPutPut: p(v ′, p(v , s)) = p(v ′, s).

(Notice that the arrow gs // v is not used.)

Page 27: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

The axioms diagramatically

V × S

V

π ��:::::V × S Sp // S

V

g��������S

S1 ��::::::S V × S〈g ,1〉 // V × S

S

p�������PUTGET GETPUT

V × S Sp//

V × V × S

V × S

π0,2��

V × V × S V × SV×p // V × S

S

p��

PUTPUT

Page 28: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Adjoints (Functional Programming)

You’re most likely familiar with

(C × A // B)

(A // BC )

which comes from

C C(−)C

77C CwwC×−

and adjoints come with a unit (boring) and a counit (important)

ε : C × XC // X

Page 29: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Adjoints (Algebra)

Now think of free monoids (free groups, free . . . )

(FA // B)

(A // UB)

which comes from (F free, U underlying)

Alg SetU

55Alg Setuu

F

and adjoints come with a unit

ηA : A // UFA

and a counitεB : FUB // B

Page 30: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Monads (Universal Algebra)

Any adjunction

D CU

77D CvvF

gives rise to an endofunctor T = UF : C // C with units

ηA : A // UFA

amounting to a natural transformation η : 1C // UF .Furthermore, TT reduces to T (terms of terms are terms) by anatural transformation µ : T 2 // T (setting µ = UεF ).

So . . .

Page 31: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Monad (definition)

A monad is one of the three standard ways of presenting(specifying) a universal algebra (a class of algebras).

A monad consists of an endofunctor T : C // C together withnatural transformations

µ : T 2 // T ,

called the multiplication, and

η : 1C // T ,

called the unit, satisfying. . .

Page 32: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Monad axioms

(Remember: T : C // C, µ : T 2 // T and η : 1C // T )

T T 2ηT //T

T

1

##GGGGGGGGGGGGG T 2

T

µ

��

T 2 Too TηT 2

T��

T

T

1

{{wwwwwwwwwwwww

T 2 Tµ//

T 3

T 2

��

T 3 T 2µT // T 2

T

µ

��

called left-ientity, right-identity and associativity, respectively.

Page 33: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Algebras

Monads are just ways of forming and managing terms. What wereally work with (even if you’re a functional programmer) isalgebras.An algebra for the monad T : C // C is an object C of C alongwith a morphism a : TC // C of C, called the action, renderingcommutative

C

C1 ��::::::C TC

ηC // TC

C

a��������

TC Ca//

T 2C

TC

µC��

T 2C TCTa // TC

C

a��

These are (also) called the identity and associativity lawsrespectively (but for the algebra now).

Page 34: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Monadicity

Suppose we have an adjunction F a U.Are the algebras for the monad UF the ones you might expect?Usually, yes. When they are, we say that the right adjoint U ismonadic.

But sometimes, no. For example, Categories are not monadic overthe category of sets. This isn’t really surprising: The data forbuilding a free category shouldn’t be just a set of objects, or a setof arrows, or even a pair of sets of objects and arrows. The basic(non-categorical) structure needs to be pre-specified.Categories are indeed monadic over the category of directed(multi-) graphs.

The moral here is that we need to be thoughtful about what is theappropriate base category for our monads.

Page 35: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Defining categories

Now might be a good time to give a brief formal definition of acategory.

A category is a

I directed graph whose nodes are called objects and edges arecalled arrows, together with

I a specified composition which is associative and has identities.

(A pair of arrows is composable if and only if they line up head totail: A // B // C .)

Page 36: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Time for a short pause

And congratulate yourself: Very few people learn about adjunctions and monadicity before defining categories!

Page 37: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Let’s compareLens axioms

V × S

V

π ��:::::V × S Sp // S

V

g��������S

S1 ��::::::S V × S〈g ,1〉 // V × S

S

p�������PUTGET GETPUT

V × S Sp//

V × V × S

V × S

π0,2��

V × V × S V × SV×p // V × S

S

p��

PUTPUT

Monad algebra axioms

C

C1 ��::::::C TC

ηC // TC

C

a��������

TC Ca//

T 2C

TC

µC��

T 2C TCTa // TC

C

a��

IDENTASSOC

Page 38: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Slice category

Given a category C and an object V in C, the slice category C/Vhas as objects arrows of C with codomain V and as arrows fromf : C // V to g : C ′ // V arrows of C from C // C ′ making thetriangle

C

Vf ��::::::C C ′// C ′

V

g��������

commute.

For example, if set is the category of sets, and V is a set (forexample the set of states of a system) then set/V denotes a slicecategory whose objects one might think of as potential gets forlenses.

Page 39: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Slice category

Given a category C and an object V in C, the slice category C/Vhas as objects arrows of C with codomain V and as arrows fromf : C // V to g : C ′ // V arrows of C from C // C ′ making thetriangle

C

Vf ��::::::C C ′// C ′

V

g��������

commute.

For example, if set is the category of sets, and V is a set (forexample the set of states of a system) then set/V denotes a slicecategory whose objects one might think of as potential gets forlenses.

Page 40: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Lenses are algebras

Note that R = V ×−, fibred by the projection, is a monad onset/V .

TheoremAn algebra for the monad R is a (set-based asymmetric) lenssatisfying the three lens axioms (PutGet, GetPut and PutPut).

More precisely, R = ∆Σ where Σ : set/V // set is the projectionfrom the slice category (“sum up the fibres” to get just a set) and∆ : set // set/V takes X to ∆X = π : V × X // V (called“fibred over V by the projection of the product”).Furthermore Σ a ∆ and, provided V is non-empty, ∆ is monadic.

Page 41: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Lenses are algebras

Note that R = V ×−, fibred by the projection, is a monad onset/V .

TheoremAn algebra for the monad R is a (set-based asymmetric) lenssatisfying the three lens axioms (PutGet, GetPut and PutPut).

More precisely, R = ∆Σ where Σ : set/V // set is the projectionfrom the slice category (“sum up the fibres” to get just a set) and∆ : set // set/V takes X to ∆X = π : V × X // V (called“fibred over V by the projection of the product”).Furthermore Σ a ∆ and, provided V is non-empty, ∆ is monadic.

Page 42: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Constant complement

Corollary

Every set-based asymmetric lens satisfying the three lens axioms isisomorphic to one of the form g = π : V × C // V with p givenby the constant complement updating strategy

p(v , s) = p(v , (v ′, c)) = (v , c).

This is important and valuable (for example, the updating strategyin SQL is entirely based on this), but not very interesting and quiterestricted.For some time people wanting more interesting lenses said “let’sgive up PutPut — it looked overly strong anyway” but life is muchmore interesting than that(!).

Stay tuned. . .

Page 43: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

Constant complement

Corollary

Every set-based asymmetric lens satisfying the three lens axioms isisomorphic to one of the form g = π : V × C // V with p givenby the constant complement updating strategy

p(v , s) = p(v , (v ′, c)) = (v , c).

This is important and valuable (for example, the updating strategyin SQL is entirely based on this), but not very interesting and quiterestricted.For some time people wanting more interesting lenses said “let’sgive up PutPut — it looked overly strong anyway” but life is muchmore interesting than that(!).

Stay tuned. . .

Page 44: Mathematical Foundations of Bidirectional Transformations · woooo oooo. History 1986 | Just communications and just computations 1996 | Web: Ordinary people’s computers communicate

End of Monday lecture

Would you like some lunch?