38
A category-theoretic view of model-driven Meinte Boersma DSL Consultancy April 10th, 2014 A CT view of MD DSL Consultancy

A category-theoretic view of model-driven

Embed Size (px)

DESCRIPTION

The slides for my presentation during the 2014 edition of http://codegeneration.net/cg2014/programme/

Citation preview

Page 1: A category-theoretic view of model-driven

A category-theoretic view of model-driven

Meinte Boersma

DSL Consultancy

April 10th, 2014

A CT view of MD DSL Consultancy

Page 2: A category-theoretic view of model-driven

About me

Owner/founder of DSL Consultancy

Creator of

Experienced in Xtend; Xtext, guerilla warfare...

...still learning category theory!

A CT view of MD DSL Consultancy

Page 3: A category-theoretic view of model-driven

Agenda

I What is Category Theory?

I General introduction to CT

I Why category theory for MD*?

I Finding the category of models

I Discussion and conclusion

A CT view of MD DSL Consultancy

Page 4: A category-theoretic view of model-driven

What is category theory

Category theory is an abstraction of mathematics itselfthat allows many intricate and subtle mathematicalresults [...] to be stated, and proved, in a much simplerway than without the use of categories.(Wikipedia)

=⇒ Raising the level of abstraction!

A CT view of MD DSL Consultancy

Page 5: A category-theoretic view of model-driven

Characteristics of category theory

1. capture essential complexity and separate it from theincidental one

2. leave out unnecessary/technical details until you need them

3. lots of diagrams and ”chasing” those

A CT view of MD DSL Consultancy

Page 6: A category-theoretic view of model-driven

General introduction

Definition A category C consists of:

I objects, usually denoted with capitals

I arrows, each having a source and a target object: Sf−→ T

I a composition operation ◦ on arrows such that for arrows f , g :

1. (identity) for every object X , an identity arrow idX exists

such that for every arrow Sf−→ T : idT ◦ f = f ◦ idS

2. (associativity) if Af−→ B, B

g−→ C and Ch−→ D, then

h ◦ (g ◦ f ) = (h ◦ g) ◦ f

A CT view of MD DSL Consultancy

Page 7: A category-theoretic view of model-driven

A prototypical diagram

A CT view of MD DSL Consultancy

Page 8: A category-theoretic view of model-driven

Another diagram

Commutativity:

fg

g ° f

A CT view of MD DSL Consultancy

Page 9: A category-theoretic view of model-driven

Examples of categories - from mathematics: Set

Description of Set:

I objects: sets

I arrows: Sf−→ T are functions: f : S → T

I composition: composition of functions

I identity arrows: idX (x) = x ∀x ∈ X

A CT view of MD DSL Consultancy

Page 10: A category-theoretic view of model-driven

Examples of categories - from mathematics: Rel

Description of Rel:

I objects: sets

I arrows: Sf−→ T are binary relations: f ⊆ S × T

I composition of Af−→ B and B

g−→ C :

(a, c) ∈ g ◦ f ⇐⇒ (a, b) ∈ f and (b, c) ∈ g for some b ∈ B

I identity arrows: idX = {(x , x) | x ∈ X}

Pub exercise: verify category laws

A CT view of MD DSL Consultancy

Page 11: A category-theoretic view of model-driven

Examples of categories - from mathematics: Graph

A graph consists of

I a set of nodes

I a set of edges

I functions (σ, τ) mapping edges to its source resp. its targetnode

An arrow in Graph is a mapping of nodes and edges that respectssource and target nodes of edges.

Note: this sounds oddly familiar...

A CT view of MD DSL Consultancy

Page 12: A category-theoretic view of model-driven

Examples of categories - from programming: Html

Description of Html:

I objects: instances of HTMLElements

I arrows: invocations of JavaScript functions that take andreturn HTMLElements

I composition: function composition

I identity:

A CT view of MD DSL Consultancy

Page 13: A category-theoretic view of model-driven

Examples of categories - from programming: Jqry

Description of Jqry:

I objects: values of $(...)

I arrows: invocations of members of $.fn

I composition: function composition

I identity:

Note: Html and Jqry look rather alike...

A CT view of MD DSL Consultancy

Page 14: A category-theoretic view of model-driven

Examples of categories - summary

objects arrows composition

Set sets functions from source to target sets composition of functions

Rel sets binary relations between two sets ”connecting transitively”

Graph graphs mappings from source to target graph, re-specting source and target nodes of edges

”following the mapping”

Html HTMLElements invocations of appropriate JS functions functional composition

Jqry $(..) invocations of members of $.fn invocation chaining

Hask Haskell types Haskell functions the . operator

A CT view of MD DSL Consultancy

Page 15: A category-theoretic view of model-driven

Observations

arrows

functions

composition

functionalcomp.

“free”associativity

A CT view of MD DSL Consultancy

Page 16: A category-theoretic view of model-driven

Relations between categories

”Compare” categories with each other using . . .

Definition A functorF : C −→ D

maps {objects | arrows} of C to {objects | arrows} of D such that

I (identity) F (idS) = idF (S) with S an object of CI (composition) F (f ◦ g) = F (f ) ◦ F (g) for arrows f , g of C

A CT view of MD DSL Consultancy

Page 17: A category-theoretic view of model-driven

Examples of functors

I the power set functor P : Set −→ Set

I the ”graphing” functor Cat −→ Graph

I monads are special kinds of functors on Hask

I the ”wrapping” functor Html −→ Jqry:

A CT view of MD DSL Consultancy

Page 18: A category-theoretic view of model-driven

Types of functors

functor type property

endo- C = D

covariant F (S)F (f )−→ F (T )

contravariant F (S)F (f )←− F (T )

for every arrow Sf−→ T

A CT view of MD DSL Consultancy

Page 19: A category-theoretic view of model-driven

Why category theory for MD*?

Bad reason: because the hipsters are doing it!

”Don’t you see: a monad simply is an endofunctorarising as the composition of two adjoint functors withtwo natural transformations, really!”

A CT view of MD DSL Consultancy

Page 20: A category-theoretic view of model-driven

Why category theory for MD*?

To try and give answers to the following:

1. What is a model?

2. What is a meta model?

3. Model transformations

4. Discussion: semantics, meta model evolution, etc.

A CT view of MD DSL Consultancy

Page 21: A category-theoretic view of model-driven

The category of models

Goal: a suitable category of models which answers our questions.

Challenge: CT assumes little ⇒ CT gives little off-the-shelf. . .

Idea: start withMod = (directed) graphs with (general) mappings; then: retrofit.

Note: Mod 6= Graph.

A CT view of MD DSL Consultancy

Page 22: A category-theoretic view of model-driven

An example model

(Xcore=Ecore model)

A CT view of MD DSL Consultancy

Page 23: A category-theoretic view of model-driven

An example model - ”categorized”

nodes

edges

payloads

⇒ nodes and edges require a payload (names, . . .) ⇐ retrofit!

Arrows induce mapping of payloads through their base point.

A CT view of MD DSL Consultancy

Page 24: A category-theoretic view of model-driven

Types of model arrows

What types of arrows can we think of?

I model transformations

I type systems

I queries

I is-a’s: ”going meta”

A CT view of MD DSL Consultancy

Page 25: A category-theoretic view of model-driven

Arrows between models - caveats

Note:

I not all {source | target} {nodes | edges} need to be mapped

I each arrow is a transformation between a particular sourceand target model

I a model transformation that works on a class of models is a(covariant) functor ⇐= requires some formalization

A CT view of MD DSL Consultancy

Page 26: A category-theoretic view of model-driven

”Going meta” - visualized

model pre-meta model

mappings

Note: mapping can carry extra information

A CT view of MD DSL Consultancy

Page 27: A category-theoretic view of model-driven

”Going meta” - formalized

Definition In Mod, a model M2 is pre-meta for M1 if an ”is-a”arrow f exists that maps all source nodes and edges.

model

pre-meta model

is-a

A CT view of MD DSL Consultancy

Page 28: A category-theoretic view of model-driven

Multiple meta models

model

pre-meta model

is-a

A CT view of MD DSL Consultancy

Page 29: A category-theoretic view of model-driven

Multiple meta models

model

pre-meta model

is-a

also pre-meta

also is-a

A CT view of MD DSL Consultancy

Page 30: A category-theoretic view of model-driven

The ultimate meta model

A CT view of MD DSL Consultancy

Page 31: A category-theoretic view of model-driven

Constraints

Many constraints are not expressible by pre-meta notion.

Just retrofit them to Mod where required!

Definition A constraint in M2 is a predicate function on modelsM1 for which M2 is pre-meta.

A CT view of MD DSL Consultancy

Page 32: A category-theoretic view of model-driven

”Going meta” - with constraints

Simply ”store” the constraints in M2 as part of it!

Definition In Mod, a model M2 is meta for M1 if it is pre-metaand it satisfies all constraints in M2.

A CT view of MD DSL Consultancy

Page 33: A category-theoretic view of model-driven

Model transformations

Now define the sub-category

ModM2 := {M1 in Mod such that M2 is meta for M1}

Definition A model transformation is a functor

T : ModM2 −→ModM′2

that respects the constraints of M ′2.

Post-conference exercise: what about F (f )?

A CT view of MD DSL Consultancy

Page 34: A category-theoretic view of model-driven

Uncharted territory

CT concepts untouched (by me):

I duality

I natural transformations

I adjointness

A CT view of MD DSL Consultancy

Page 35: A category-theoretic view of model-driven

Discussion

Possible topics:

I semantics

I meta model evolution

I model merging

I . . .?

A CT view of MD DSL Consultancy

Page 36: A category-theoretic view of model-driven

Conclusion

I CT provides a nice, tech-independent language for MD

I Comes with its own semantics and ”API”

I It still makes us work (”answers”)

I CT-approach can be taken much further - see articlereferences

A CT view of MD DSL Consultancy

Page 37: A category-theoretic view of model-driven

More information

I Books:

1. Lawvere and Schanuel, Conceptual Mathematics2. Simmons, An Introduction to Category Theory3. Mac Lane, Categories for the Working Mathematician

I Articles:

1. Diskin and Maibaum, Category Theory and Model-DrivenEngineering: From Formal Semantics to Design Patterns andBeyond

2. Diskin and Wolter, A Diagrammatic Logic for Object-OrientedVisual Modeling

I Blogs and video lectures by John Bender

A CT view of MD DSL Consultancy

Page 38: A category-theoretic view of model-driven

End credits

Meinte [email protected]

Software value creator @ DSL Consultancy

A CT view of MD DSL Consultancy