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
Two Core Aspects of FP 2/15
Two Core Aspects of FP programming with first-class functions ? ? ? infinite behavior black box to use, apply to values 2/15
Two Core Aspects of FP programming with first-class functions ? ? ? ? ? ? infinite behavior black box to use, apply to values 2/15
Two Core Aspects of FP programming with first-class functions ? ? ? ? ? ? infinite behavior black box to use, apply to values 2/15
Two Core Aspects of FP programming with programming with first-class functions algebraic data types ? ? ? ? ? ? infinite behavior finite information black box known structure to use, apply to values to use, traverse structure 2/15
Defunctionalization programs with first-class programs with defunctionalize functions data types [Reynolds, 1972] 3/15
Defunctionalization programs with first-class image of defunctionalize functions defunct. programs with data types [Reynolds, 1972] 3/15
Refunctionalization programs with first-class image of refunctionalize functions defunct. programs with data types [Danvy and Milliken, 2009] 4/15
Wanted: Symmetric Languages programs with first-class image of functions defunct. programs with programs with codata types data types 5/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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
(De|Re)functionalization refunctionalize defunctionalize ● data types codata types ● functions with patterns destructors ● constructors functions with copatterns 9/15
Recommend
More recommend