The expression lemma [Ralph Lmmel, Ondrej Rypacek] Joo Vinagre - - PowerPoint PPT Presentation

the expression lemma
SMART_READER_LITE
LIVE PREVIEW

The expression lemma [Ralph Lmmel, Ondrej Rypacek] Joo Vinagre - - PowerPoint PPT Presentation

The expression lemma [Ralph Lmmel, Ondrej Rypacek] Joo Vinagre MAPi Thematic Seminar, June 2012 Outline 1. Introduction and motivation 2. Programming: Functional vs OO 3. Simple expression lemma 4. Generalized expression lemma 5.


slide-1
SLIDE 1

The expression lemma

João Vinagre MAPi Thematic Seminar, June 2012

[Ralph Lämmel, Ondrej Rypacek]

slide-2
SLIDE 2

2

Outline

1. Introduction and motivation 2. Programming: Functional vs OO 3. Simple expression lemma 4. Generalized expression lemma 5. Related work 6. Conclusions and future work

slide-3
SLIDE 3

3

Introduction

Problem Recursive Program

Functional paradigm OO paradigm

slide-4
SLIDE 4

4

Functional vs OO

We know (by code inspection) that these two are semantically equivalent

But can we prove it? (Mathematically!)

Recursive functions on algebraic data type Recursive methods on

  • bject state
slide-5
SLIDE 5

5

Algebras and coalgebras

  • Functional programs:
  • formalized in algebras of functional folds (catamorphisms)
  • OO programs:
  • formalized in coalgebras of object unfolds (anamorphisms)
  • (Co)algebraic specification example (binary tree with labels):
slide-6
SLIDE 6

6

Algebras and coalgebras

  • Functional programs:
  • formalized in algebras of functional folds (catamorphisms)
  • OO programs:
  • formalized in coalgebras of object unfolds (anamorphisms)
  • (Co)algebraic specification example (binary tree with labels):
slide-7
SLIDE 7

7

The expression lemma

… defines a proper correspondence between anamorphically (and coalgebraically) phrased OO programs and catamorphically phrased functional programs.

slide-8
SLIDE 8

8

Simple expression lemma

Given a distributive law λ : FB → BF, we can define an arrow μF → νB using the derivations:

slide-9
SLIDE 9

9

Generalized expression lemma

  • Same reasoning, but lifts to monads and

comonads

  • Some additional properties need to be met

Given a monad 〈 T,η,μ 〉 , a comonad 〈 D,η,δ 〉 and a distributive law Λ : TD → DT of the monad T

  • ver D:
slide-10
SLIDE 10

10

Related work

  • Functional OO programming languages
  • Moby, .NET (C#/VB + LINQ), F#, Scala, OCaml, …
  • Focus is on extending existing OO languages to support the

functional paradigm (or vice-versa)

  • However they do not try to establish a formal correspondence

between OO and functional programs

  • The expression problem [Wadler]
  • Language code extensibility problem (both term- and
  • peration-wise)
  • Assumes the expression lemma, however with less structure
slide-11
SLIDE 11

11

Related work (continued)

  • Operational semantics [Turi and Plotkin]
  • Denotational and operational semantics corresponds to

functional and OO, respectively.

  • Distributive laws
  • Languages with binders in presheaf category [Fiore,

Plotkin and Turi]

  • Recursive constructs [Klin]
  • Modular constructions on distributive laws [Jacobs]
  • Distributive laws for recursion and corecursion [Pardo,

Uustalu, Vene et al]

slide-12
SLIDE 12

12

Conclusions

  • Given:
  • a recursive functional program expressed in

catamorphisms (folds);

  • a recursive OO program expressed in anamorphisms

(unfolds);

  • we can prove the semantic equivalence between

both

(if they are in fact equivalent)

slide-13
SLIDE 13

13

Limitations / Open issues

  • Not everything is linearly recursive! → a more

general lemma is necessary for many real world problems

  • Now that we can establish semantic

equivalence, we should take advantage of this (e.g. bidirectional code refactoring) → not trivial

slide-14
SLIDE 14

14