The Expression Problem solved, finally!

Post on 04-Jun-2015

818 views 10 download

Tags:

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.

Transcript of The Expression Problem solved, finally!

The Expression Problem solved,Finally!

FPSyd

Disclaimer

I had this problem before

Result

Failure Success Pending

Console Html

Screenshot?

Pdf?

The Expression Problem

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

The Expression Problem

The Expression Problem

Solved in Java?!?

Not a new problem

Once and for all

Encode

anything

Represent

datatypes

Once and for all

Typeclass

Mutually

recursive

Once and for all

Generic

function

Type constructors?

Encod

e

Count

Type constructors?

Typeclass

es

2 for 1!

Data types encodings

Recursive type

Natural

s

Church

encoding

Data types encodings

Fold-

Algebra

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

F-Algebra

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

Data types encodings

Church

encoding

Extensibility

Extensibility

Encoder

Extensibility

Generic

functions for

lists

Better

encode

Troubl

eSolutio

n

Extensibility

Generic,

specialized

functions

Functions

Data Dispatchers

Main points

Data types as Folds (!= F-

Algebras)

Encoded as type-classes

Extensible and modular

“Finally tagless”

Finally tagless

Embedded DSLs“Initial

”“Final”

Extensibility “Initial”

Extensibility “Final”

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

The tagging problem

Higher-order

typed language

Does not

typecheck!

One approach

“tagged”

type

Partial

With GADTs

The “Symantics” approach

Synta

x

Semantic

s

Expressio

n

Pushing the negation down

“Initial”

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

Pushing the negation down “Final”

Bijection between and “Final”“Initial”

Advantages

Expressions are well-typed

Easily extensible

Less demanding of the type system

With OO

The Visitor solution

Data type

Visitor

Object algebra

Interface

Factory

Expressio

n

Pretty printer

Retroactive

Pretty printer

Direct

Extensions

For

booleans

Extensions

Booleans

Pretty-prin

ting

Multi-type

statement

s

Combining data

compositio

n

Combining operations

Banana-split!

Object algebras

Work with OO languages having generics

No visitor and “accept” method

Generic combinator classes?

Fold algebras

Conclusion

Easier on the type system

Extensibility

Shallow / Deep Embeddings – ICFP14

Fold algebras