163
Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen Coalgebra: applications in automata theory and programming language design Alexandra Silva Radboud Universiteit Nijmegen and CWI Chocola meeting, 16 May 2013 A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming langua 1 / 71

École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Coalgebra: applications in automata theoryand programming language design

Alexandra Silva

Radboud Universiteit Nijmegen and CWI

Chocola meeting, 16 May 2013

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design1 / 71

Page 2: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Coalgebra

Specify and reason about systems.

Syntax Axiomatization state-machinesRE, CCS, . . . KA,. . . e.g. DFA, LTS, PA, . . .

Can we do all of this uniformly in a single framework?

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design2 / 71

Page 3: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Coalgebra

Specify and reason about systems.

Syntax Axiomatization

state-machines

RE, CCS, . . . KA,. . .

e.g. DFA, LTS, PA, . . .

Can we do all of this uniformly in a single framework?

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design2 / 71

Page 4: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Coalgebra

Specify and reason about systems.

Syntax

Axiomatization

state-machinesRE, CCS, . . .

KA,. . .

e.g. DFA, LTS, PA, . . .

Can we do all of this uniformly in a single framework?

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design2 / 71

Page 5: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Coalgebra

Specify and reason about systems.

Syntax Axiomatization state-machinesRE, CCS, . . . KA,. . . e.g. DFA, LTS, PA, . . .

Can we do all of this uniformly in a single framework?

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design2 / 71

Page 6: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Coalgebra

Specify and reason about systems.

Syntax Axiomatization state-machinesRE, CCS, . . . KA,. . . e.g. DFA, LTS, PA, . . .

Can we do all of this uniformly in a single framework?

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design2 / 71

Page 7: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What do this things have in common?(S, t : S → 2× SA)

(S, t : S → PSA)

(S, t : S → PDω(S)A)

(S, t : S → Dω(S) + (A× S) + 1)

(S, t : S → P(Dω(PS)A))

(S, t : S → TS) T -coalgebras

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design3 / 71

Page 8: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What do this things have in common?(S, t : S → 2× SA)

(S, t : S → PSA)

(S, t : S → PDω(S)A)

(S, t : S → Dω(S) + (A× S) + 1)

(S, t : S → P(Dω(PS)A))

(S, t : S → TS) T -coalgebras

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design3 / 71

Page 9: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What do this things have in common?(S, t : S → 2× SA)

(S, t : S → PSA)

(S, t : S → PDω(S)A)

(S, t : S → Dω(S) + (A× S) + 1)

(S, t : S → P(Dω(PS)A))

(S, t : S → TS) T -coalgebras

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design3 / 71

Page 10: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What do this things have in common?(S, t : S → 2× SA)

(S, t : S → PSA)

(S, t : S → PDω(S)A)

(S, t : S → Dω(S) + (A× S) + 1)

(S, t : S → P(Dω(PS)A))

(S, t : S → TS) T -coalgebras

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design3 / 71

Page 11: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What do this things have in common?(S, t : S → 2× SA)

(S, t : S → PSA)

(S, t : S → PDω(S)A)

(S, t : S → Dω(S) + (A× S) + 1)

(S, t : S → P(Dω(PS)A))

(S, t : S → TS) T -coalgebras

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design3 / 71

Page 12: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What do this things have in common?(S, t : S → 2× SA)

(S, t : S → PSA)

(S, t : S → PDω(S)A)

(S, t : S → Dω(S) + (A× S) + 1)

(S, t : S → P(Dω(PS)A))

(S, t : S → TS)

T -coalgebras

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design3 / 71

Page 13: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What do this things have in common?(S, t : S → 2× SA)

(S, t : S → PSA)

(S, t : S → PDω(S)A)

(S, t : S → Dω(S) + (A× S) + 1)

(S, t : S → P(Dω(PS)A))

(S, t : S → TS) T -coalgebrasA. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design3 / 71

Page 14: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The power of T

(S, t : S → TS)

The functor T determines:

1 notion of observational equivalence (coalg. bisimulation)E.g. T = 2× (−)A: language equivalence

2 behaviour (final coalgebra)E.g. T = 2× (−)A: languages over A – 2A∗

3 set of expressions describing finite systems4 axioms to prove bisimulation equivalence of expressions

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design4 / 71

Page 15: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The power of T

(S, t : S → TS)

The functor T determines:

1 notion of observational equivalence (coalg. bisimulation)E.g. T = 2× (−)A: language equivalence

2 behaviour (final coalgebra)E.g. T = 2× (−)A: languages over A – 2A∗

3 set of expressions describing finite systems4 axioms to prove bisimulation equivalence of expressions

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design4 / 71

Page 16: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The power of T

(S, t : S → TS)

The functor T determines:

1 notion of observational equivalence (coalg. bisimulation)E.g. T = 2× (−)A: language equivalence

2 behaviour (final coalgebra)E.g. T = 2× (−)A: languages over A – 2A∗

3 set of expressions describing finite systems4 axioms to prove bisimulation equivalence of expressions

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design4 / 71

Page 17: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The power of T

(S, t : S → TS)

The functor T determines:

1 notion of observational equivalence (coalg. bisimulation)E.g. T = 2× (−)A: language equivalence

2 behaviour (final coalgebra)E.g. T = 2× (−)A: languages over A – 2A∗

3 set of expressions describing finite systems4 axioms to prove bisimulation equivalence of expressions

1 + 2 are classic coalgebra; 3 + 4 are recent work.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design4 / 71

Page 18: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

How about algorithms?

• Coalgebra has found its place in the semantic side of theworld: operational/denotational semantics, logics, . . .

• Are there also opportunities for contributions in algorithms?

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design5 / 71

Page 19: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

How about algorithms?

• Coalgebra has found its place in the semantic side of theworld: operational/denotational semantics, logics, . . .

• Are there also opportunities for contributions in algorithms?

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design5 / 71

Page 20: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Brzozowski’s algorithm (co)algebraically

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design6 / 71

Page 21: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivation

• duality between reachability and observability (Arbib andManes 1975): beautiful, not very well-known.

• combined use of algebra and coalgebra.

• our understanding of automata is still very limited;cf. recent research: universal automata, àtomata, weightedautomata (Sakarovitch, Brzozowski, . . . )

• joint work with Bonchi, Bonsangue, Rutten (Dexter’sfestschrift 2012)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design7 / 71

Page 22: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivation

• duality between reachability and observability (Arbib andManes 1975): beautiful, not very well-known.

• combined use of algebra and coalgebra.

• our understanding of automata is still very limited;cf. recent research: universal automata, àtomata, weightedautomata (Sakarovitch, Brzozowski, . . . )

• joint work with Bonchi, Bonsangue, Rutten (Dexter’sfestschrift 2012)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design7 / 71

Page 23: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Brzozowski algorithm (by example)

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

• initial state: x • final states: y and z

• L(x) = {a,b}∗ a

• X is reachable but not minimal: L(y) = ε + {a,b}∗ a = L(z)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design8 / 71

Page 24: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Brzozowski algorithm (by example)

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

• initial state: x • final states: y and z

• L(x) = {a,b}∗ a

• X is reachable but not minimal: L(y) = ε + {a,b}∗ a = L(z)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design8 / 71

Page 25: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the automaton: rev(X )

X =

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

rev(X ) =

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

• transitions are reversed

• initial states ⇔ final states

• rev(X ) is non-deterministic

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design9 / 71

Page 26: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the automaton: rev(X )

X =

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

rev(X ) =

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

• transitions are reversed

• initial states ⇔ final states

• rev(X ) is non-deterministic

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design9 / 71

Page 27: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the automaton: rev(X )

X =

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

rev(X ) =

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

• transitions are reversed

• initial states ⇔ final states

• rev(X ) is non-deterministic

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design9 / 71

Page 28: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the automaton: rev(X )

X =

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

rev(X ) =

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

• transitions are reversed

• initial states ⇔ final states

• rev(X ) is non-deterministicA. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design9 / 71

Page 29: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Making it deterministic again: det(rev(X ))

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• new state space: 2X = {V | V ⊆ {x , y , z} }

• V a //W W = {w | v a // w , v ∈ V}• initial state:{y , z} • final states: all V with x ∈ V

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design10 / 71

Page 30: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Making it deterministic again: det(rev(X ))

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• new state space: 2X = {V | V ⊆ {x , y , z} }

• V a //W W = {w | v a // w , v ∈ V}• initial state:{y , z} • final states: all V with x ∈ V

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design10 / 71

Page 31: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Making it deterministic again: det(rev(X ))

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• new state space: 2X = {V | V ⊆ {x , y , z} }

• V a //W W = {w | v a // w , v ∈ V}• initial state:{y , z} • final states: all V with x ∈ V

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design10 / 71

Page 32: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Making it deterministic again: det(rev(X ))

x

b��

b$$

b��

zaoo oo

y

a

JJ

a

==

oo

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• new state space: 2X = {V | V ⊆ {x , y , z} }

• V a //W W = {w | v a // w , v ∈ V}• initial state:{y , z} • final states: all V with x ∈ V

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design10 / 71

Page 33: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton det(rev(X )) . . .

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• . . . accepts the reverse of the language accepted by X :

L(det(rev(X )) ) = a {a,b}∗ = reverse(L(X ) )

• . . . and is observable!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design11 / 71

Page 34: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton det(rev(X )) . . .

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• . . . accepts the reverse of the language accepted by X :

L(det(rev(X )) ) = a {a,b}∗ = reverse(L(X ) )

• . . . and is observable!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design11 / 71

Page 35: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton det(rev(X )) . . .

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• . . . accepts the reverse of the language accepted by X :

L(det(rev(X )) ) = a {a,b}∗ = reverse(L(X ) )

• . . . and is observable!A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design11 / 71

Page 36: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Today’s Theorem

If: a deterministic automaton X is reachable and accepts L(X )

then: det(rev(X )) is minimal and

L(det(rev(X )) ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design12 / 71

Page 37: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Today’s Theorem

If: a deterministic automaton X is reachable and accepts L(X )

then: det(rev(X )) is minimal and

L(det(rev(X )) ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design12 / 71

Page 38: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Taking the reachable part of det(rev(X ))

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

x , y , z

a,b

// y , zb$$

a

::

a,b

JJ

• reach(det(rev(X )))

is reachable (by construction)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design13 / 71

Page 39: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Taking the reachable part of det(rev(X ))

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

x , y , z

a,b

// y , zb$$

a

::

a,b

JJ

• reach(det(rev(X )))

is reachable (by construction)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design13 / 71

Page 40: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Taking the reachable part of det(rev(X ))

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

x , y , z

a,b

// y , zb$$

a

::

a,b

JJ

• reach(det(rev(X ))) is reachable (by construction)A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design13 / 71

Page 41: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Repeating everything, now for reach(det(rev(X )))

x , y , z

a,b

// y , zb%%

a

::

a,b

JJ

// s

b�� a // t

bzz

a

JJ

• . . . gives us reach(det(rev(reach(det(rev(X ))))))

• which is (reachable and) minimal and accepts {a,b}∗ a.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design14 / 71

Page 42: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Repeating everything, now for reach(det(rev(X )))

x , y , z

a,b

// y , zb%%

a

::

a,b

JJ

// s

b�� a // t

bzz

a

JJ

• . . . gives us reach(det(rev(reach(det(rev(X ))))))

• which is (reachable and) minimal and accepts {a,b}∗ a.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design14 / 71

Page 43: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Repeating everything, now for reach(det(rev(X )))

x , y , z

a,b

// y , zb%%

a

::

a,b

JJ

// s

b�� a // t

bzz

a

JJ

• . . . gives us reach(det(rev(reach(det(rev(X ))))))

• which is (reachable and) minimal and accepts {a,b}∗ a.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design14 / 71

Page 44: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Repeating everything, now for reach(det(rev(X )))

x , y , z

a,b

// y , zb%%

a

::

a,b

JJ

// s

b�� a // t

bzz

a

JJ

• . . . gives us reach(det(rev(reach(det(rev(X ))))))

• which is (reachable and) minimal and accepts {a,b}∗ a.A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design14 / 71

Page 45: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

All in all: Brzozowski’s algorithm

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

// s

b�� a // t

bzz

a

JJ

• X is reachable and accepts {a,b}∗ a

• reach(det(rev(reach(det(rev(X )))))) also accepts {a,b}∗ a

• . . . and is minimal!!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design15 / 71

Page 46: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

All in all: Brzozowski’s algorithm

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

// s

b�� a // t

bzz

a

JJ

• X is reachable and accepts {a,b}∗ a

• reach(det(rev(reach(det(rev(X )))))) also accepts {a,b}∗ a

• . . . and is minimal!!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design15 / 71

Page 47: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

All in all: Brzozowski’s algorithm

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

// s

b�� a // t

bzz

a

JJ

• X is reachable and accepts {a,b}∗ a

• reach(det(rev(reach(det(rev(X )))))) also accepts {a,b}∗ a

• . . . and is minimal!!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design15 / 71

Page 48: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

All in all: Brzozowski’s algorithm

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

// s

b�� a // t

bzz

a

JJ

• X is reachable and accepts {a,b}∗ a

• reach(det(rev(reach(det(rev(X )))))) also accepts {a,b}∗ a

• . . . and is minimal!!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design15 / 71

Page 49: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

All in all: Brzozowski’s algorithm

// x

b��

a// z

bzz

a}}

y

b

OO

a

JJ

// s

b�� a // t

bzz

a

JJ

• X is reachable and accepts {a,b}∗ a

• reach(det(rev(reach(det(rev(X )))))) also accepts {a,b}∗ a

• . . . and is minimal!!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design15 / 71

Page 50: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Goal of the day

• Correctness of Brzozowski’s algorithm (co)algebraically• Generalizations to other types of automata

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design16 / 71

Page 51: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

(Co)algebra

algebras:

F (X )

f��

X

coalgebras:

X

f��

F (X )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design17 / 71

Page 52: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Examples of algebras

N× N

+

��

N

1 + N

[0,S]

��

N

1

0��

N

S��

N

1

0��

N

S��

N

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design18 / 71

Page 53: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Examples of algebras

N× N

+

��

N

1 + N

[0,S]

��

N

1

0��

N

S��

N

1

0��

N

S��

N

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design18 / 71

Page 54: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Examples of coalgebras

X

t��

P(A× X )

x a // y ↔ 〈a, y〉 ∈ t(x)

X

〈Left , label ,Right〉��

X × A× X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design19 / 71

Page 55: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Examples of coalgebras

X

t��

P(A× X )

x a // y ↔ 〈a, y〉 ∈ t(x)

X

〈Left , label ,Right〉��

X × A× X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design19 / 71

Page 56: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Examples of coalgebras

〈head, tail 〉��

2× 2ω

head��

tail��

2 2ω

2

2ωhead

CC

tail��

head((b0,b1,b2, . . .)) = b0

tail((b0,b1,b2, . . .)) = (b1,b2,b3 . . .)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design20 / 71

Page 57: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Homomorphisms

F (X )

f��

F (h)// F (Y )

g��

Xh

// Y

X

f��

h // Y

g��

F (X )F (h)

// F (Y )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design21 / 71

Page 58: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Homomorphisms

F (X )

f��

F (h)// F (Y )

g��

Xh

// Y

X

f��

h // Y

g��

F (X )F (h)

// F (Y )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design21 / 71

Page 59: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Initiality, finality

F (A)

α

��

F (h)// F (X )

f��

A∃ !h

// X

X

f��

∃ !h // Z

β

��

F (X )F (h)

// F (Z )

• initial algebras↔ induction

• final coalgebras↔ coinduction

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design22 / 71

Page 60: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Initiality, finality

F (A)

α

��

F (h)// F (X )

f��

A∃ !h

// X

X

f��

∃ !h // Z

β

��

F (X )F (h)

// F (Z )

• initial algebras↔ induction

• final coalgebras↔ coinduction

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design22 / 71

Page 61: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Initiality, finality

F (A)

α

��

F (h)// F (X )

f��

A∃ !h

// X

X

f��

∃ !h // Z

β

��

F (X )F (h)

// F (Z )

• initial algebras↔ induction

• final coalgebras↔ coinduction

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design22 / 71

Page 62: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Automata, (co)algebraically

• Automata are complicated structures:

part of them is algebra - part of them is coalgebra

• ( . . . in two different ways . . . )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design23 / 71

Page 63: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Automata, (co)algebraically

• Automata are complicated structures:

part of them is algebra - part of them is coalgebra

• ( . . . in two different ways . . . )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design23 / 71

Page 64: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

A deterministic automaton

1i""

2

X

f<<

t��

X A

where

1 = {0} 2 = {0,1} X A = {g | g : A→ X }

x a // y ↔ t(x)(a) = y

i(0) ∈ X is the initial state

x is final (or accepting) ↔ f (x) = 1

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design24 / 71

Page 65: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

A deterministic automaton

1i""

2

X

f<<

t��

X A

where

1 = {0} 2 = {0,1} X A = {g | g : A→ X }

x a // y ↔ t(x)(a) = y

i(0) ∈ X is the initial state

x is final (or accepting) ↔ f (x) = 1

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design24 / 71

Page 66: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Automata: algebra or coalgebra?

• initial state: algebraic – final states: coalgebraic

1i!!

2

X

f==

• transition function: both algebraic and coalgebraic

X t // X A

X // (A // X )

X × A t // X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design25 / 71

Page 67: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Automata: algebra or coalgebra?

• initial state: algebraic – final states: coalgebraic

1i!!

2

X

f==

• transition function: both algebraic and coalgebraic

X t // X A

X // (A // X )

X × A t // X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design25 / 71

Page 68: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Automata: algebra and coalgebra!

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

To take home: this picture!! . . . which we’ll explain next . . .

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design26 / 71

Page 69: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Automata: algebra and coalgebra!

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

To take home: this picture!! . . .

which we’ll explain next . . .

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design26 / 71

Page 70: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Automata: algebra and coalgebra!

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

To take home: this picture!! . . . which we’ll explain next . . .

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design26 / 71

Page 71: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The “automaton” of languages

2

2A∗

�

ε?

OO

(2A∗)A

ε?(L) = 1↔ ε ∈ L

2A∗= {g | g : A∗ → 2} ∼= {L | L ⊆ A∗}

β(L)(a) = La = {w ∈ A∗ | a · w ∈ L}

• We say “automaton”: it does not have an initial state.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design27 / 71

Page 72: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The “automaton” of languages

2

2A∗

�

ε?

OO

(2A∗)A

ε?(L) = 1↔ ε ∈ L

2A∗= {g | g : A∗ → 2} ∼= {L | L ⊆ A∗}

β(L)(a) = La = {w ∈ A∗ | a · w ∈ L}

• We say “automaton”: it does not have an initial state.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design27 / 71

Page 73: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton of languages

• transitions: L a // La where La = {w ∈ A∗ | a · w ∈ L}

• for instance:

{ab}

b""

a // {b}

a��

b // {ε}

a,b}}

a,b

JJ

a∗ b {a,b}∗

b��

a��

{a,b}∗

a,b

MM

• note: every state L accepts . . . . . . the language L !!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design28 / 71

Page 74: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton of languages

• transitions: L a // La where La = {w ∈ A∗ | a · w ∈ L}

• for instance:

{ab}

b""

a // {b}

a��

b // {ε}

a,b}}

a,b

JJ

a∗ b {a,b}∗

b��

a��

{a,b}∗

a,b

MM

• note: every state L accepts . . . . . . the language L !!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design28 / 71

Page 75: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton of languages

• transitions: L a // La where La = {w ∈ A∗ | a · w ∈ L}

• for instance:

{ab}

b""

a // {b}

a��

b // {ε}

a,b}}

a,b

JJ

a∗ b {a,b}∗

b��

a��

{a,b}∗

a,b

MM

• note: every state L accepts . . .

. . . the language L !!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design28 / 71

Page 76: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton of languages

• transitions: L a // La where La = {w ∈ A∗ | a · w ∈ L}

• for instance:

{ab}

b""

a // {b}

a��

b // {ε}

a,b}}

a,b

JJ

a∗ b {a,b}∗

b��

a��

{a,b}∗

a,b

MM

• note: every state L accepts . . . . . . the language L !!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design28 / 71

Page 77: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton of languages is . . . final

2

X

f;;

t��

∃!o// 2A∗

�

ε?

OO

X A

oA// (2A∗

)A

o(x) = {w ∈ A∗ | f (xw ) = 1}

= the language accepted by x

where: xw is the state reached after inputting the word w ,

and: oA(g) = o ◦ g, all g ∈ X A.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design29 / 71

Page 78: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton of languages is . . . final

2

X

f;;

t��

∃!o// 2A∗

�

ε?

OO

X A

oA// (2A∗

)A

o(x) = {w ∈ A∗ | f (xw ) = 1}

= the language accepted by x

where: xw is the state reached after inputting the word w ,

and: oA(g) = o ◦ g, all g ∈ X A.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design29 / 71

Page 79: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Back to today’s picture

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

On the right: final coalgebra

On the left: initial algebra . . .

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design30 / 71

Page 80: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Back to today’s picture

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

On the right: final coalgebra

On the left: initial algebra . . .

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design30 / 71

Page 81: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Back to today’s picture

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

On the right: final coalgebra

On the left: initial algebra . . .

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design30 / 71

Page 82: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The “automaton” of words

1

�

A∗

�

(A∗)A

ε is initial state

α(w)(a) = w · a

that is, transitions: w a // w · a

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design31 / 71

Page 83: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The automaton of words is . . . initial

1

�

i

##A∗

∃! r//

�

X

t��

(A∗)A

rA// X A

i ∈ X = initial state(to be precise: i(0))

r(w) = iw= the state reached from i

after inputting w

• Proof: easy exercise.

• Proof: formally, because A∗ is an initial 1 + A× (−)-algebra!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design32 / 71

Page 84: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Duality

• Reachability and observability are dual:

Arbib and Manes, 1975.

• (here observable = minimal)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design33 / 71

Page 85: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachability and observability

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

r(w) = state reachedon input w

o(x) = languageaccepted by x

•We call X reachable if r is surjective.

•We call X observable (= minimal) if o is injective.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design34 / 71

Page 86: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachability and observability

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

r(w) = state reachedon input w

o(x) = languageaccepted by x

•We call X reachable if r is surjective.

•We call X observable (= minimal) if o is injective.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design34 / 71

Page 87: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachability and observability

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

r(w) = state reachedon input w

o(x) = languageaccepted by x

•We call X reachable if r is surjective.

•We call X observable (= minimal) if o is injective.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design34 / 71

Page 88: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachability and observability

1

�

i

""

2

A∗r

//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

r(w) = state reachedon input w

o(x) = languageaccepted by x

•We call X reachable if r is surjective.

•We call X observable (= minimal) if o is injective.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design34 / 71

Page 89: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the automaton

• Reachability↔ observability

• Being precise about homomorphisms is crucial.

• Forms the basis for proof Brzozowski’s algorithm.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design35 / 71

Page 90: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Powerset construction

V

g��

2V

2(−) : 7→

W 2W

2g

OO

where 2V = {S | S ⊆ V} and, for all S ⊆W ,

2g(S) = g−1(S) (= {v ∈ V | g(v) ∈ S} )

• This construction is contravariant !!

• Note: if g is surjective, then 2g is injective.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design36 / 71

Page 91: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Powerset construction

V

g��

2V

2(−) : 7→

W 2W

2g

OO

where 2V = {S | S ⊆ V} and, for all S ⊆W ,

2g(S) = g−1(S) (= {v ∈ V | g(v) ∈ S} )

• This construction is contravariant !!

• Note: if g is surjective, then 2g is injective.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design36 / 71

Page 92: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Powerset construction

V

g��

2V

2(−) : 7→

W 2W

2g

OO

where 2V = {S | S ⊆ V} and, for all S ⊆W ,

2g(S) = g−1(S) (= {v ∈ V | g(v) ∈ S} )

• This construction is contravariant !!

• Note: if g is surjective, then 2g is injective.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design36 / 71

Page 93: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Powerset construction

V

g��

2V

2(−) : 7→

W 2W

2g

OO

where 2V = {S | S ⊆ V} and, for all S ⊆W ,

2g(S) = g−1(S) (= {v ∈ V | g(v) ∈ S} )

• This construction is contravariant !!

• Note: if g is surjective, then 2g is injective.A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design36 / 71

Page 94: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing transitions

X

t��

X A

X × A

��

X

� 2(−)//

2X×A

2X

OO(2X )A

2X

OO2X

2t��

(2X )A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design37 / 71

Page 95: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing transitions

X

t��

X A

X × A

��

X

� 2(−)//

2X×A

2X

OO(2X )A

2X

OO2X

2t��

(2X )A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design37 / 71

Page 96: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing transitions

X

t��

X A

X × A

��

X

� 2(−)//

2X×A

2X

OO

(2X )A

2X

OO2X

2t��

(2X )A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design37 / 71

Page 97: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing transitions

X

t��

X A

X × A

��

X

� 2(−)//

2X×A

2X

OO(2X )A

2X

OO

2X

2t��

(2X )A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design37 / 71

Page 98: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing transitions

X

t��

X A

X × A

��

X

� 2(−)//

2X×A

2X

OO(2X )A

2X

OO2X

2t��

(2X )A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design37 / 71

Page 99: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Initial↔ final

1i

X

� 2(−)//

2

2X

2i==

2

X

f>>

oo //

1f!!

2X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design38 / 71

Page 100: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Initial↔ final

1i

X � 2(−)//

2

2X

2i==

2

X

f>>

oo //

1f!!

2X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design38 / 71

Page 101: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Initial↔ final

1i

X � 2(−)//

2

2X

2i==

2

X

f>>

oo //

1f!!

2X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design38 / 71

Page 102: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Initial↔ final

1i

X � 2(−)//

2

2X

2i==

2

X

f>>

oo //

1f!!

2X

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design38 / 71

Page 103: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the entire automaton

1i!!

2

X

f==

t��

X A

� 2(−)//

1f##

2

2X

2i;;

2t��

(2X )A

• Initial and final are exchanged . . .

• transitions are reversed . . .

• and the result is again deterministic!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design39 / 71

Page 104: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the entire automaton

1i!!

2

X

f==

t��

X A

� 2(−)//

1f##

2

2X

2i;;

2t��

(2X )A

• Initial and final are exchanged . . .

• transitions are reversed . . .

• and the result is again deterministic!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design39 / 71

Page 105: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the entire automaton

1i!!

2

X

f==

t��

X A

� 2(−)//

1f##

2

2X

2i;;

2t��

(2X )A

• Initial and final are exchanged . . .

• transitions are reversed . . .

• and the result is again deterministic!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design39 / 71

Page 106: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the entire automaton

1i!!

2

X

f==

t��

X A

� 2(−)//

1f##

2

2X

2i;;

2t��

(2X )A

• Initial and final are exchanged . . .

• transitions are reversed . . .

• and the result is again deterministic!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design39 / 71

Page 107: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reversing the entire automaton

1i!!

2

X

f==

t��

X A

� 2(−)//

1f##

2

2X

2i;;

2t��

(2X )A

• Initial and final are exchanged . . .

• transitions are reversed . . .

• and the result is again deterministic!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design39 / 71

Page 108: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Our previous example

X =

// x

b��

a// z

b{{

a��

y

bOO

a

JJ

2X =

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• Note that X has been reversed and determinized:

2X = det(rev(X ))

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design40 / 71

Page 109: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Our previous example

X =

// x

b��

a// z

b{{

a��

y

bOO

a

JJ

2X =

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• Note that X has been reversed and determinized:

2X = det(rev(X ))

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design40 / 71

Page 110: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Our previous example

X =

// x

b��

a// z

b{{

a��

y

bOO

a

JJ

2X =

x , y

a��

b // x , y , z

a,b

x , zboo

a��

// y , zb$$

a

::

xb

dd

azz

yb//

aOO

a,b

JJz

boo

aOO

• Note that X has been reversed and determinized:

2X = det(rev(X ))

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design40 / 71

Page 111: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Proving today’s Theorem

If: a deterministic automaton X is reachable and accepts L(X )

then: 2X ( = det(rev(X )) ) is minimal/observable and

L(2X ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design41 / 71

Page 112: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Proving today’s Theorem

If: a deterministic automaton X is reachable and accepts L(X )

then: 2X ( = det(rev(X )) ) is minimal/observable and

L(2X ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design41 / 71

Page 113: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Proof: by reversing A∗ r // X

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

(2X )A // (2A∗)A

• X becomes 2X

• initial automaton A∗ becomes (almost) final automaton 2A∗

• r is surjective ⇒ 2r is injective

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design42 / 71

Page 114: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Proof: by reversing A∗ r // X

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

(2X )A // (2A∗)A

• X becomes 2X

• initial automaton A∗ becomes (almost) final automaton 2A∗

• r is surjective ⇒ 2r is injective

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design42 / 71

Page 115: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Proof: by reversing A∗ r // X

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

(2X )A // (2A∗)A

• X becomes 2X

• initial automaton A∗ becomes (almost) final automaton 2A∗

• r is surjective ⇒ 2r is injective

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design42 / 71

Page 116: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Proof: by reversing A∗ r // X

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

(2X )A // (2A∗)A

• X becomes 2X

• initial automaton A∗ becomes (almost) final automaton 2A∗

• r is surjective ⇒ 2r is injective

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design42 / 71

Page 117: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Proof: by reversing A∗ r // X

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

(2X )A // (2A∗)A

• X becomes 2X

• initial automaton A∗ becomes (almost) final automaton 2A∗

• r is surjective ⇒ 2r is injective

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design42 / 71

Page 118: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachable becomes observable

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

rev// 2A∗

�

ε?ee

(2X )A // (2A∗)A // (2A∗

)A

• If r is surjective then (2r and hence) rev ◦ 2r is injective.

• That is, 2X is observable (= minimal).

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design43 / 71

Page 119: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachable becomes observable

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

rev// 2A∗

�

ε?ee

(2X )A // (2A∗)A // (2A∗

)A

• If r is surjective then (2r and hence) rev ◦ 2r is injective.

• That is, 2X is observable (= minimal).

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design43 / 71

Page 120: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachable becomes observable

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

rev// 2A∗

�

ε?ee

(2X )A // (2A∗)A // (2A∗

)A

• If r is surjective then (2r and hence) rev ◦ 2r is injective.

• That is, 2X is observable (= minimal).

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design43 / 71

Page 121: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Reachable becomes observable

1

�

i##

A∗r//

�

X

t��

(A∗)A // X A

� 2(−)//

2

2X

2i::

2t��

2r// 2A∗

2�

2εOO

rev// 2A∗

�

ε?ee

(2X )A // (2A∗)A // (2A∗

)A

• If r is surjective then (2r and hence) rev ◦ 2r is injective.

• That is, 2X is observable (= minimal).

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design43 / 71

Page 122: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Summarizing

1

�

i##

2

A∗r//

�

X

f>>

t��

(A∗)A // X A

1f""

2

2X

2i::

2t��

rev ◦ 2r// 2A∗

�

ε?

OO

(2X )A // (2A∗)A

• If: X is reachable, i.e., r is surjective

then: rev ◦ 2r is injective, i.e., 2X is observable = minimal.

• And: rev(2r (f )) = rev(o(i)), i.e., L(2X ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design44 / 71

Page 123: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Summarizing

1

�

i##

2

A∗r//

�

X

f>>

t��

(A∗)A // X A

1f""

2

2X

2i::

2t��

rev ◦ 2r// 2A∗

�

ε?

OO

(2X )A // (2A∗)A

• If: X is reachable, i.e., r is surjective

then: rev ◦ 2r is injective, i.e., 2X is observable = minimal.

• And: rev(2r (f )) = rev(o(i)), i.e., L(2X ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design44 / 71

Page 124: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Summarizing

1

�

i##

2

A∗r//

�

X

f>>

t��

(A∗)A // X A

1f""

2

2X

2i::

2t��

rev ◦ 2r// 2A∗

�

ε?

OO

(2X )A // (2A∗)A

• If: X is reachable, i.e., r is surjective

then: rev ◦ 2r is injective, i.e., 2X is observable = minimal.

• And: rev(2r (f )) = rev(o(i)), i.e., L(2X ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design44 / 71

Page 125: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Summarizing

1

�

i##

2

A∗r//

�

X

f>>

t��

(A∗)A // X A

1f""

2

2X

2i::

2t��

rev ◦ 2r// 2A∗

�

ε?

OO

(2X )A // (2A∗)A

• If: X is reachable, i.e., r is surjective

then: rev ◦ 2r is injective, i.e., 2X is observable = minimal.

• And: rev(2r (f )) = rev(o(i)), i.e., L(2X ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design44 / 71

Page 126: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Summarizing

1

�

i##

2

A∗r//

�

X

f>>

t��

(A∗)A // X A

1f""

2

2X

2i::

2t��

rev ◦ 2r// 2A∗

�

ε?

OO

(2X )A // (2A∗)A

• If: X is reachable, i.e., r is surjective

then: rev ◦ 2r is injective, i.e., 2X is observable = minimal.

• And: rev(2r (f )) = rev(o(i)), i.e., L(2X ) = reverse(L(X ) )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design44 / 71

Page 127: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Corollary: Brzozowski’s algorithm

• X becomes 2X , accepting reverse(L(X ))

• take reachable part: Y = reachable(2X )

• Y becomes 2Y , which is minimal and accepts

reverse(reverse(L(X ))) = L(X )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design45 / 71

Page 128: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Corollary: Brzozowski’s algorithm

• X becomes 2X , accepting reverse(L(X ))

• take reachable part: Y = reachable(2X )

• Y becomes 2Y , which is minimal and accepts

reverse(reverse(L(X ))) = L(X )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design45 / 71

Page 129: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Corollary: Brzozowski’s algorithm

• X becomes 2X , accepting reverse(L(X ))

• take reachable part: Y = reachable(2X )

• Y becomes 2Y , which is minimal and accepts

reverse(reverse(L(X ))) = L(X )

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design45 / 71

Page 130: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Generalizations

1

�

i

##

2

A∗r//

�

X

f;;

t��

o// 2A∗

�

ε?

OO

(A∗)A

rA// X A

oA// (2A∗

)A

1

�

i

##

B

A∗r//

�

X

f;;

t��

o// BA∗

��

OO

(A∗)A

rA// X A

oA// (BA∗

)A

• A Brzozowski minimization algorithm for Moore automata.

BX = {ϕ | ϕ : X → B} Bf (ϕ) = ϕ ◦ f

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design46 / 71

Page 131: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Further generalizations

• Moore automata generalization: uniform algorithm fordecorated traces and must testing (joint work with Bonchi,Caltais and Pous);

• Further generalizations to non-deterministic and weightedautomata.

Coalgebra is not only semantics but also algorithms!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design47 / 71

Page 132: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Further generalizations

• Moore automata generalization: uniform algorithm fordecorated traces and must testing (joint work with Bonchi,Caltais and Pous);

• Further generalizations to non-deterministic and weightedautomata.

Coalgebra is not only semantics but also algorithms!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design47 / 71

Page 133: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

CoCaml: Programming with Coinductive Types(joint work with Jean-Baptiste Jeannin and Dexter Kozen)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design48 / 71

Page 134: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Computing with Coalgebraic Data

• Inductive datatypes and functions on those arewell-understood; coinductive datatypes often considereddifficult to handle, not many programming languages offerthe constructs for them.

• OCaml offers the possibility of defining coinductivedatatypes, but the means to define recursive functions onthem are limited.

• Often the obvious definitions do not halt or provide thewrong solution.

• Even so, there are often perfectly good solutions(examples forthcoming!)

• We show how to extend the language to allow it!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design49 / 71

Page 135: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivating example

type list = N | C of int * list

let rec ones = C(1, ones);; 1,1,1,1,...let rec alt = C(1, C(2, alt));; 1,2,1,2,...

Infinite lists but. . . regular:

1 •

2

A simple function:

let set l = match l with| N -> N| C(h, t) -> (insert h (set t));;

We expect set ones = {1} and set alt = {1,2}.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design50 / 71

Page 136: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivating example

type list = N | C of int * list

let rec ones = C(1, ones);; 1,1,1,1,...let rec alt = C(1, C(2, alt));; 1,2,1,2,...

Infinite lists but. . . regular:

1 •

2

A simple function:

let set l = match l with| N -> N| C(h, t) -> (insert h (set t));;

We expect set ones = {1} and set alt = {1,2}.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design50 / 71

Page 137: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivating example

type list = N | C of int * list

let rec ones = C(1, ones);; 1,1,1,1,...let rec alt = C(1, C(2, alt));; 1,2,1,2,...

Infinite lists but. . . regular:

1 •

2

A simple function:

let set l = match l with| N -> N| C(h, t) -> (insert h (set t));;

We expect set ones = {1} and set alt = {1,2}.A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design50 / 71

Page 138: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What is the problem?

• The function definition above will not halt in OCaml. . .• even though it is clear what the answer should be;

• Note that this is not a corecursive definition: we are notasking for a greatest solution or a unique solution in a finalcoalgebra,

• but rather a least solution in a different ordered domainfrom the one provided by the standard semantics ofrecursive functions.

• Standard semantics: least solution in the flat Scott domainwith bottom element ⊥ representing nontermination

• Intended semantics: least solution in a different CPO,namely (P(Z),⊆) with bottom element ∅.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design51 / 71

Page 139: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What is the problem?

• The function definition above will not halt in OCaml. . .• even though it is clear what the answer should be;• Note that this is not a corecursive definition: we are not

asking for a greatest solution or a unique solution in a finalcoalgebra,

• but rather a least solution in a different ordered domainfrom the one provided by the standard semantics ofrecursive functions.

• Standard semantics: least solution in the flat Scott domainwith bottom element ⊥ representing nontermination

• Intended semantics: least solution in a different CPO,namely (P(Z),⊆) with bottom element ∅.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design51 / 71

Page 140: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivating example c’d

We would like to use (almost) the same definition and get theintended solution. . .

let set l = match l with| N -> N| C(h, t) -> (insert h (set t));;

We change it to:

let corec[iterator(N)] set l = match l with| N -> N| C(h, t) -> insert h (set t);;

The construct corec with the parameter iterator(N)specifies to the compiler how to solve equations.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design52 / 71

Page 141: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivating example c’d

We would like to use (almost) the same definition and get theintended solution. . .

let set l = match l with| N -> N| C(h, t) -> (insert h (set t));;

We change it to:

let corec[iterator(N)] set l = match l with| N -> N| C(h, t) -> insert h (set t);;

The construct corec with the parameter iterator(N)specifies to the compiler how to solve equations.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design52 / 71

Page 142: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivating example c’d

For instance, for the infinite list alt:

1 •

2

the compiler will generate two equations:

set(x) = insert 1 (set(y))set(y) = insert 2 (set(x))

then solve them using iterator (least fixed point) which willproduce the intended set {1,2}.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design53 / 71

Page 143: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Motivating example c’d

let map f = match arg with| N -> N| C(h, t) -> C(f(h), map(f,t));;

We would like: map plusOne alt to produce the infinite list2,3,2,3, . . . :

1 •

2

7→ •

2 •

3

This is not a least fixed point computation anymore but rather asolution in the final coalgebra.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design54 / 71

Page 144: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Another Example

Free variables of a λ-term

type term =| Var of string x| App of term *

term(f e)

| Lam of string *term

λx .e

let rec fv = function| Var v -> {v}| App(t1,t2) -> fv t1 ∪ fv t2| Lam(x,t) -> (fv t) - {x}

let rec t = App(Var "x", App(Var "y", t))

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design55 / 71

Page 145: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Another ExampleBut what about infinitary λ-terms (λ-coterms)?

type term =| Var of string x| App of term *

term(f e)

| Lam of string *term

λx .e

let rec fv = function| Var v -> {v}| App(t1,t2) -> fv t1 ∪ fv t2| Lam(x,t) -> (fv t) - {x}

let rec t = App(Var "x", App(Var "y", t))

x •

y

We would like: fv t = {x,y} (again LFP).A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design56 / 71

Page 146: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Substitution

Replace y by •

x xin

x •

y

to get•

x •

x x

.

The usual semantics would infinitely unfold the term on the left,generating instead:

x •

• •

x x •

. . .. . .

x x

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design57 / 71

Page 147: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Probabilistic Protocols

s

H t

T

12

12

12

12

PrH(s) = 12 + 1

8 + 132 + 1

128 + · · · = 23

PrH(t) = 14 + 1

16 + 164 + 1

256 + · · · = 13

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design58 / 71

Page 148: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Probabilistic Protocols

s

H t

T

12

12

12

12

PrH(s) = 12 + 1

2 · PrH(t)

PrH(t) = 12 · PrH(s)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design59 / 71

Page 149: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The Von Neumann Trick

s

u t

TH

p 1− p

p1− p

1− pp

PrH(s) = p · PrH(u) + (1− p) · PrH(t)PrH(u) = (1− p) + p · PrH(s)PrH(t) = (1− p) · PrH(s)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design60 / 71

Page 150: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The Von Neumann Trick

type state =| H| T| Flip of float * state * state

let rec pr heads s = function| H -> 1.| T -> 0.| Flip(p,u,v) ->

p *. (pr heads u) +. (1 -. p) *. (pr heads v)

let rec s = Flip(.345,u,t)and u = Flip(.345,H,s)and t = Flip(.345,T,s)

print p heads s

s

u t

TH

p 1− p

p1− p

1− pp

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design61 / 71

Page 151: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Theoretical Foundations

• Well-founded coalgebras [Taylor 99]• Recursive coalgebras [Adámek, Lücke, Milius 07]• Elgot algebras [Adámek, Milius, Velebil 06]• Corecursive algebras [Capretta, Uustalu, Vene 09]

Ingredients:• Functor F (usually polynomial or power set)• domain: an F -coalgebra (C, γ)• range: an F -algebra (A, α)

C A

FC FA

h

γ

Fh

α

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design62 / 71

Page 152: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What about Non-Well-Founded Coalgebras?The foundations existing so far were for unique solutions; wewant alternative solutions.

C A

FC FA

h

γ

Fh

α

• Even if (C, γ) is not well-founded, the diagram may stillhave a canonical solution, provided (A, α) comes equippedwith a method for solving systems of equations

• The diagram specifies the system to be solved• The variables are the elements of C and h is their

interpretation in A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design63 / 71

Page 153: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

What about Non-Well-Founded Coalgebras?The foundations existing so far were for unique solutions; wewant alternative solutions.

C A

FC FA

h

γ

Fh

α

• Even if (C, γ) is not well-founded, the diagram may stillhave a canonical solution, provided (A, α) comes equippedwith a method for solving systems of equations

• The diagram specifies the system to be solved• The variables are the elements of C and h is their

interpretation in AA. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design63 / 71

Page 154: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The general idea

The programmer specifies the equations as usual with an extraparameter, like in:

let corec[iterator(N)] set l = match l with| N -> N| C(h, t) -> insert h (set t);;

The compiler generates equations and solves them using theextra parameter.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design64 / 71

Page 155: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

The general idea

The programmer specifies the equations as usual with an extraparameter, like in:

let corec[iterator(N)] set l = match l with| N -> N| C(h, t) -> insert h (set t);;

The compiler generates equations and solves them using theextra parameter.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design64 / 71

Page 156: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Free Variables of a λ-Coterm

The free variablesof

•s

xu •t

yv

are {x , y}

fv(s) = fv(u) ∪ fv(t)fv(t) = fv(v) ∪ fv(s)

fv(u) = {x}fv(v) = {y}

The least solution in (P(Var),⊆) is {x , y}

Standard semantics: A ∪ ⊥ = ⊥, whereas here A ∪ ∅ = A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design65 / 71

Page 157: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Free Variables of a λ-Coterm

The free variablesof

x •

y

s

u t

v

are {x , y}

fv(s) = fv(u) ∪ fv(t)fv(t) = fv(v) ∪ fv(s)fv(u) = {x}fv(v) = {y}

The least solution in (P(Var),⊆) is {x , y}

Standard semantics: A ∪ ⊥ = ⊥, whereas here A ∪ ∅ = A

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design66 / 71

Page 158: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Example: Probabilistic Protocolss

H t

T

12

12

12

12

PrH(s) = 12 + 1

2 · PrH(t) PrH(t) = 12 · PrH(s)

• Can calculate expected running times, higher moments,outcome functions similarly

• These are all least solutions in an appropriate ordereddomain—in the above example, ([0,1],≤)

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design67 / 71

Page 159: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Other Non-Well-Founded Examples

• static analysis, abstract interpretation• p-adic arithmetic• automata constructions

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design68 / 71

Page 160: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Implementation

• We implemented corec constructor which takes a solveras a parameter

• We implemented several general solvers: least fixed point,unique solution in a final coalgebra, gaussian elimination,. . .

• Solvers are implemented directly in the interpreter, astransformers from an abstract syntax tree to anotherabstract syntax tree.

• Future: to provide tools to manipulate the abstract syntaxtree allowing programmers to easily specify their solver.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design69 / 71

Page 161: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Implementation

• We implemented corec constructor which takes a solveras a parameter

• We implemented several general solvers: least fixed point,unique solution in a final coalgebra, gaussian elimination,. . .

• Solvers are implemented directly in the interpreter, astransformers from an abstract syntax tree to anotherabstract syntax tree.

• Future: to provide tools to manipulate the abstract syntaxtree allowing programmers to easily specify their solver.

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design69 / 71

Page 162: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

CoCaml

• CoCaml offers new program constructs and functionalitiesto implement functions on coinductive structures.

• Examples illustrate the need for new constructs• New constructs enable allow definitions very much in the

style of standard recursive functions.

http://www.cs.cornell.edu/Projects/CoCaml/

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design70 / 71

Page 163: École normale supérieure de Lyonchocola.ens-lyon.fr/media/talk72/silva-chocola.pdf · Coalgebra Brzozowski’s algorithm CoCaml RU Nijmegen The power of T (S;t : S !TS) The functor

CoalgebraBrzozowski’s algorithm

CoCamlRU Nijmegen

Thanks!

A. Silva (RU Nijmegen) Chocola meeting Coalgebra: applications in automata theory and programming language design71 / 71