the expression problem solved, finally!
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
![Page 1: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/1.jpg)
The Expression Problem solved,Finally!
FPSyd
![Page 2: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/2.jpg)
Disclaimer
![Page 3: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/3.jpg)
I had this problem before
Result
Failure Success Pending
Console Html
Screenshot?
Pdf?
![Page 4: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/4.jpg)
The Expression Problem
Add(Lit(1), Lit(2))
![Page 5: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/5.jpg)
The Expression Problem
![Page 6: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/6.jpg)
The Expression Problem
![Page 7: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/7.jpg)
Solved in Java?!?
![Page 8: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/8.jpg)
Not a new problem
![Page 9: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/9.jpg)
Once and for all
Encode
anything
Represent
datatypes
![Page 10: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/10.jpg)
Once and for all
Typeclass
Mutually
recursive
![Page 11: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/11.jpg)
Once and for all
Generic
function
![Page 12: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/12.jpg)
Type constructors?
Encod
e
Count
![Page 13: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/13.jpg)
Type constructors?
Typeclass
es
2 for 1!
![Page 14: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/14.jpg)
Data types encodings
Recursive type
Natural
s
Church
encoding
![Page 15: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/15.jpg)
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!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/16.jpg)
Data types encodings
Church
encoding
![Page 17: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/17.jpg)
Extensibility
![Page 18: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/18.jpg)
Extensibility
Encoder
![Page 19: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/19.jpg)
Extensibility
Generic
functions for
lists
Better
encode
Troubl
eSolutio
n
![Page 20: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/20.jpg)
Extensibility
Generic,
specialized
functions
Functions
Data Dispatchers
![Page 21: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/21.jpg)
Main points
Data types as Folds (!= F-
Algebras)
Encoded as type-classes
Extensible and modular
![Page 22: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/22.jpg)
“Finally tagless”
![Page 23: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/23.jpg)
Finally tagless
Embedded DSLs“Initial
”“Final”
![Page 24: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/24.jpg)
Extensibility “Initial”
![Page 25: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/25.jpg)
Extensibility “Final”
![Page 26: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/26.jpg)
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!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/27.jpg)
The tagging problem
Higher-order
typed language
Does not
typecheck!
![Page 28: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/28.jpg)
One approach
“tagged”
type
Partial
![Page 29: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/29.jpg)
With GADTs
![Page 30: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/30.jpg)
The “Symantics” approach
Synta
x
Semantic
s
Expressio
n
![Page 31: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/31.jpg)
Pushing the negation down
“Initial”
neg (add 1 2) => add (neg 1) (neg 2)
![Page 32: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/32.jpg)
Pushing the negation down “Final”
![Page 33: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/33.jpg)
Bijection between and “Final”“Initial”
![Page 34: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/34.jpg)
Advantages
Expressions are well-typed
Easily extensible
Less demanding of the type system
![Page 35: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/35.jpg)
With OO
![Page 36: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/36.jpg)
The Visitor solution
Data type
Visitor
![Page 37: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/37.jpg)
Object algebra
Interface
Factory
Expressio
n
![Page 38: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/38.jpg)
Pretty printer
Retroactive
![Page 39: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/39.jpg)
Pretty printer
Direct
![Page 40: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/40.jpg)
Extensions
For
booleans
![Page 41: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/41.jpg)
Extensions
Booleans
Pretty-prin
ting
![Page 42: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/42.jpg)
Multi-type
statement
s
![Page 43: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/43.jpg)
Combining data
compositio
n
![Page 44: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/44.jpg)
Combining operations
Banana-split!
![Page 45: The Expression Problem solved, finally!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/45.jpg)
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!](https://reader036.vdocuments.us/reader036/viewer/2022062319/55700978d8b42ac0178b47b1/html5/thumbnails/46.jpg)
Conclusion
Easier on the type system
Extensibility
Shallow / Deep Embeddings – ICFP14
Fold algebras