46
The Expression Problem solved, Finally!

The Expression Problem solved, finally!

Embed Size (px)

DESCRIPTION

How object algebras, or "fold-algebras" can be used to represent data and how its encoding as typeclasses or interfaces help with the Expression Problem.

Citation preview

Page 1: The Expression Problem solved, finally!

The Expression Problem solved,Finally!

FPSyd

Page 2: The Expression Problem solved, finally!

Disclaimer

Page 3: The Expression Problem solved, finally!

I had this problem before

Result

Failure Success Pending

Console Html

Screenshot?

Pdf?

Page 4: The Expression Problem solved, finally!

The Expression Problem

Add(Lit(1), Lit(2))

Page 5: The Expression Problem solved, finally!

The Expression Problem

Page 6: The Expression Problem solved, finally!

The Expression Problem

Page 7: The Expression Problem solved, finally!

Solved in Java?!?

Page 8: The Expression Problem solved, finally!

Not a new problem

Page 9: The Expression Problem solved, finally!

Once and for all

Encode

anything

Represent

datatypes

Page 10: The Expression Problem solved, finally!

Once and for all

Typeclass

Mutually

recursive

Page 11: The Expression Problem solved, finally!

Once and for all

Generic

function

Page 12: The Expression Problem solved, finally!

Type constructors?

Encod

e

Count

Page 13: The Expression Problem solved, finally!

Type constructors?

Typeclass

es

2 for 1!

Page 14: The Expression Problem solved, finally!

Data types encodings

Recursive type

Natural

s

Church

encoding

Page 15: The Expression Problem solved, finally!

Data types encodings

Fold-

Algebra

(T1 -> A, T2 -> A, …, Tn -> A)

F-Algebra

(T1 + T2 + … + Tn) -> A F[A] -> A

Page 16: The Expression Problem solved, finally!

Data types encodings

Church

encoding

Page 17: The Expression Problem solved, finally!

Extensibility

Page 18: The Expression Problem solved, finally!

Extensibility

Encoder

Page 19: The Expression Problem solved, finally!

Extensibility

Generic

functions for

lists

Better

encode

Troubl

eSolutio

n

Page 20: The Expression Problem solved, finally!

Extensibility

Generic,

specialized

functions

Functions

Data Dispatchers

Page 21: The Expression Problem solved, finally!

Main points

Data types as Folds (!= F-

Algebras)

Encoded as type-classes

Extensible and modular

Page 22: The Expression Problem solved, finally!

“Finally tagless”

Page 23: The Expression Problem solved, finally!

Finally tagless

Embedded DSLs“Initial

”“Final”

Page 24: The Expression Problem solved, finally!

Extensibility “Initial”

Page 25: The Expression Problem solved, finally!

Extensibility “Final”

Page 26: The Expression Problem solved, finally!

Some other problems

• The tagging problem in the interpretation of typed higher-order languages

• The pattern-matching problem• Many others…

“The [deserialization] problem becomes much more frustrating, exhilarating, time

consuming and addictive in the general case of higher-order typed embedded languages”

- Oleg Kiselyov

Page 27: The Expression Problem solved, finally!

The tagging problem

Higher-order

typed language

Does not

typecheck!

Page 28: The Expression Problem solved, finally!

One approach

“tagged”

type

Partial

Page 29: The Expression Problem solved, finally!

With GADTs

Page 30: The Expression Problem solved, finally!

The “Symantics” approach

Synta

x

Semantic

s

Expressio

n

Page 31: The Expression Problem solved, finally!

Pushing the negation down

“Initial”

neg (add 1 2) => add (neg 1) (neg 2)

Page 32: The Expression Problem solved, finally!

Pushing the negation down “Final”

Page 33: The Expression Problem solved, finally!

Bijection between and “Final”“Initial”

Page 34: The Expression Problem solved, finally!

Advantages

Expressions are well-typed

Easily extensible

Less demanding of the type system

Page 35: The Expression Problem solved, finally!

With OO

Page 36: The Expression Problem solved, finally!

The Visitor solution

Data type

Visitor

Page 37: The Expression Problem solved, finally!

Object algebra

Interface

Factory

Expressio

n

Page 38: The Expression Problem solved, finally!

Pretty printer

Retroactive

Page 39: The Expression Problem solved, finally!

Pretty printer

Direct

Page 40: The Expression Problem solved, finally!

Extensions

For

booleans

Page 41: The Expression Problem solved, finally!

Extensions

Booleans

Pretty-prin

ting

Page 42: The Expression Problem solved, finally!

Multi-type

statement

s

Page 43: The Expression Problem solved, finally!

Combining data

compositio

n

Page 44: The Expression Problem solved, finally!

Combining operations

Banana-split!

Page 45: The Expression Problem solved, finally!

Object algebras

Work with OO languages having generics

No visitor and “accept” method

Generic combinator classes?

Fold algebras

Page 46: The Expression Problem solved, finally!

Conclusion

Easier on the type system

Extensibility

Shallow / Deep Embeddings – ICFP14

Fold algebras