Automatic Refunctionalization To a Language with Copattern Matching - - PowerPoint PPT Presentation

automatic refunctionalization
SMART_READER_LITE
LIVE PREVIEW

Automatic Refunctionalization To a Language with Copattern Matching - - PowerPoint PPT Presentation

Automatic Refunctionalization To a Language with Copattern Matching With Applications to the Expression Problem Tillmann Rendel Julia Trieflinger Klaus Ostermann University of Tbingen, Germany Two Core Aspects of FP 2/15 Two Core


slide-1
SLIDE 1

Automatic Refunctionalization

To a Language with Copattern Matching

With Applications to the Expression Problem

Tillmann Rendel Julia Trieflinger Klaus Ostermann University of Tübingen, Germany

slide-2
SLIDE 2

Two Core Aspects of FP

2/15

slide-3
SLIDE 3

Two Core Aspects of FP

programming with first-class functions

infinite behavior black box to use, apply to values

? ? ?

2/15

slide-4
SLIDE 4

Two Core Aspects of FP

programming with first-class functions

infinite behavior black box to use, apply to values

? ? ?

? ? ?

2/15

slide-5
SLIDE 5

Two Core Aspects of FP

programming with first-class functions

infinite behavior black box to use, apply to values

? ? ?

? ? ?

2/15

slide-6
SLIDE 6

Two Core Aspects of FP

programming with first-class functions

infinite behavior black box to use, apply to values

programming with algebraic data types

finite information known structure to use, traverse structure

? ? ?

? ? ?

2/15

slide-7
SLIDE 7

Defunctionalization

programs with data types

[Reynolds, 1972]

defunctionalize programs with first-class functions 3/15

slide-8
SLIDE 8

Defunctionalization

image of defunct. programs with data types

[Reynolds, 1972]

defunctionalize programs with first-class functions 3/15

slide-9
SLIDE 9

Refunctionalization

programs with data types programs with first-class functions

[Danvy and Milliken, 2009]

refunctionalize image of defunct. 4/15

slide-10
SLIDE 10

programs with codata types

Wanted: Symmetric Languages

programs with data types programs with first-class functions image of defunct. 5/15

slide-11
SLIDE 11

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-12
SLIDE 12

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-13
SLIDE 13

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-14
SLIDE 14

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-15
SLIDE 15

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-16
SLIDE 16

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-17
SLIDE 17

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-18
SLIDE 18

Language with Data Types

  • data type declarations

data Nat where zero() : Nat succ(Nat) : Nat

  • first-order functions matching on first argument

function add(Nat, Nat) : Nat where

add(zero(), m) = m add(succ(n), m) = succ(add(n, m))

6/15

slide-19
SLIDE 19

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-20
SLIDE 20

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-21
SLIDE 21

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-22
SLIDE 22

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-23
SLIDE 23

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-24
SLIDE 24

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-25
SLIDE 25

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-26
SLIDE 26

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-27
SLIDE 27

Language with Codata Types

  • codata type declarations

codata Stream where Stream.head() : Nat Stream.tail() : Stream

  • first-order functions with copattern matching

function repeat(Nat) : Stream where

repeat(n).head() = n repeat(n).tail() = repeat(n)

7/15

slide-28
SLIDE 28

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-29
SLIDE 29

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-30
SLIDE 30

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-31
SLIDE 31

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-32
SLIDE 32

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-33
SLIDE 33

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-34
SLIDE 34

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-35
SLIDE 35

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-36
SLIDE 36

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-37
SLIDE 37

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-38
SLIDE 38

Encoding First-Class Functions

  • destructors with arguments

codata Fun where Fun.apply(Nat) : Nat

  • example: mapping over a stream

function map(Stream, Fun) : Stream where map(s, f).head() = f.apply(s.head()) map(s, f).tail() = map(s.tail(), f)

8/15

slide-39
SLIDE 39

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize 9/15

slide-40
SLIDE 40

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-41
SLIDE 41

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-42
SLIDE 42

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-43
SLIDE 43

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-44
SLIDE 44

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-45
SLIDE 45

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-46
SLIDE 46

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-47
SLIDE 47

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), m) = m add(succ(n), m) = succ(add(n, m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(n).add(m) = succ(n.add(m))

slide-48
SLIDE 48

(De|Re)functionalization

codata types destructors functions with copatterns

  • data types
  • functions with patterns
  • constructors

defunctionalize refunctionalize 9/15

slide-49
SLIDE 49

Automatic Refunctionalization

  • Codata with multiple observations that take

arguments

– to support the full data language

  • Top-level, first-order functions, copatterns

– to avoid lambda lifting and name mangling

10/15

slide-50
SLIDE 50

Case Study

11/15

  • Based on Reynold's metacircular interpreter
slide-51
SLIDE 51
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val

slide-52
SLIDE 52
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val

slide-53
SLIDE 53
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val

slide-54
SLIDE 54
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val

slide-55
SLIDE 55
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val

slide-56
SLIDE 56
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val

slide-57
SLIDE 57
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val

slide-58
SLIDE 58
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val

slide-59
SLIDE 59
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val

slide-60
SLIDE 60
  • Based on Reynold's metacircular interpreter

Case Study

11/15 codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val Val.reify(…) : Val fun clo(Exp, Env) : Val fun rvar(…) : Val fun rapp(Val, Val) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val codata Val where Val.app(Val) : Val fun clo(Exp, Env) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val fun app(Val, Val) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val data Val where clo(Exp, Env) : Val rvar(…) : Val rapp(Val, Val) : Val fun clo(Exp, Env) : Val fun reify(Val, …) : Val

slide-61
SLIDE 61

Case Study

11/15

  • Based on Reynold's metacircular interpreter
  • Extension to Normalization by Evaluation
  • Different extensions are modular

in data vs. codata fragment

  • Expression Problem?
slide-62
SLIDE 62

Programs as Matrices

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), n) = n add(succ(m), n) = succ(add(m, n)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(m).add(n) = succ(m.add(n))

12/15

slide-63
SLIDE 63

Programs as Matrices

data Nat where zero() : Nat succ(Nat) : Nat fun add(Nat, Nat) : Nat where add(zero(), n) = n add(succ(m), n) = succ(add(m, n)) codata Nat where Nat.add(Nat) : Nat fun zero() : Nat where zero().add(n) = n fun succ(Nat) : Nat where succ(m).add(n) = succ(m.add(n))

12/15

slide-64
SLIDE 64

Programs as Matrices

data N where zero() : N succ(N) : N add(N, N) : N x1 succ(add(x1, x2)) codata N where N.add(N) : N zero() : N x1 succ(N) : N succ(x1.add(x2))

12/15

slide-65
SLIDE 65

Programs as Matrices

data N where zero() : N succ(N) : N add(N, N) : N x1 succ(add(x1, x2)) codata N where N.add(N) : N zero() : N x1 succ(N) : N succ(x1.add(x2))

12/15

slide-66
SLIDE 66

Programs as Matrices

data N where zero() : N succ(N) : N add(N, N) : N x1 succ(add(x1, x2)) codata N where N.add(N) : N zero() : N x1 succ(N) : N succ(x1.add(x2))

12/15

slide-67
SLIDE 67

Programs as Matrices

  • Programs can be written as matrices
  • (De|Re)functionalization is

matrix transposition

  • Modular extension by rows, not by columns
  • (De|Re)functionalization changes the dimension
  • f modularly supported extensibility

12/15

slide-68
SLIDE 68

In The Paper

  • More examples.
  • Formalization of

language fragments and transformations.

  • Source code of the

case study.

13/15

slide-69
SLIDE 69

Relation to OO

14/15

  • OO

– codata + implicit self recursion + mutable state + …

  • FP (traditional)

– data + first-class functions

  • FP (symmetric)

– first-order functions + data + codata

slide-70
SLIDE 70

Conclusions

We generalize from first-class functions to codata types with multiple observations with arguments ...

  • for automatic defunctionalization and

refunctionalization

  • to formulate (de|re)functionalization as matrix

transposition that switches the dimension of extensibility

  • to study one aspect of OO in an FP setting

15/15

slide-71
SLIDE 71

Conclusions

We generalize from first-class functions to codata types with multiple observations with arguments ...

  • for automatic defunctionalization and

refunctionalization

  • to formulate (de|re)functionalization as matrix

transposition that switches the dimension of extensibility

  • to study one aspect of OO in an FP setting

Thank you!