Weixin Zhang and Bruno C. d. S. Oliveira <Programming> 2019 - - PowerPoint PPT Presentation

weixin zhang and bruno c d s oliveira
SMART_READER_LITE
LIVE PREVIEW

Weixin Zhang and Bruno C. d. S. Oliveira <Programming> 2019 - - PowerPoint PPT Presentation

1 Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality Weixin Zhang and Bruno C. d. S. Oliveira <Programming> 2019 April 3, 2019 Shallow EDSLs and Object-Oriented Programming: Beyond Simple


slide-1
SLIDE 1

Shallow EDSLs and Object-Oriented Programming:
 Beyond Simple Compositionality

Weixin Zhang and Bruno C. d. S. Oliveira

<Programming> 2019

April 3, 2019

1

slide-2
SLIDE 2

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Background

2

programming languages general-purpose domain-specific embedded (internal) (external) standalone deep shallow

slide-3
SLIDE 3

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Shallow vs. deep embeddings

▸ Shallow embeddings

▸ Semantics first ▸ Compositional ▸ No AST ▸ Easy to add new language

constructs

▸ Hard to add new interpretations

3

▸ Deep embeddings

▸ Syntax first ▸ Non-compositional ▸ Have an AST ▸ Easy to add new interpretations ▸ Hard to add new language

constructs

slide-4
SLIDE 4

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Contribution

▸ Shallow embeddings and OOP are closely related

▸ Both essence is procedural abstraction [Reynolds,1978]



 


▸ OOP mechanisms, subtyping, inheritance and type-

refinement increase the modularity of shallow EDSLs

▸ Enable multiple (possibly dependent) interpretations



 


4

shallow embeddings procedural abstraction

Gibbons & Wu, 2015

OOP

Cook, 2009

slide-5
SLIDE 5

▸ Grammar:



 
 
 
 
 
 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

SCANS: a DSL for parallel prefix circuits

5

id 3 fan 3 id 3 beside fan 3 id 3 above fan 3 stretch 3 2 3 fan 3 (fan 2 beside fan 2) above (id 1 beside fan 2 beside id 1) (stretch 2 2 fan 2) above

slide-6
SLIDE 6

▸ A shallow implementation should conform to the following signatures



 
 
 


▸ E.g. an interpretation calculating the width



 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Embedding SCANS in Haskell

6

procedural abstraction semantic domain

slide-7
SLIDE 7

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Towards OOP

▸ An isomorphic encoding of width

7

slide-8
SLIDE 8

▸ It is easy to port the definition into an OOP language like Scala



 
 
 
 
 
 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Embedding SCANS in OOP

8

slide-9
SLIDE 9

▸ Smart constructors are needed for building a circuit object conveniently



 
 
 


▸ Constructing the example circuit again 



 
 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Smart constructors

9

slide-10
SLIDE 10

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Multiple interpretations in Haskell

10

▸ Often claimed as a limitation of shallow embedding ▸ Typical workaround is to use tuples

▸ e.g. additionally supporting depth for SCANS



 
 
 
 
 
 


▸ However, this implementation is non-modular

slide-11
SLIDE 11

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Multiple interpretations in Scala

11

Inheritance Type-refinement

▸ Multiple interpretations can be modular with Scala

Subtyping

slide-12
SLIDE 12

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Dependent interpretations in Haskell

12

▸ An interpretation depends not only on itself but also on

  • ther interpretations

▸ E.g. wellSized, which depends on width

slide-13
SLIDE 13

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Dependent interpretations in Scala

13

▸ Again, modular dependent interpretations are unproblematic in Scala



 
 
 
 
 
 
 
 
 
 
 


slide-14
SLIDE 14

▸ An interpretation relies on some context

▸ e.g. layout

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Context-sensitive interpretations in Haskell

14

accumulating parameter

slide-15
SLIDE 15

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Context-sensitive interpretations in Scala

15

slide-16
SLIDE 16

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

An alternative encoding of modular interpretations

16

▸ Allow non-linear extensions and loose dependencies

▸ e.g. wellSized



 
 
 
 
 


▸ Require an extra step for combining wellSized and width



 
 


slide-17
SLIDE 17

▸ Extend SCANS with right stretches

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Adding language constructs

17

slide-18
SLIDE 18

▸ Object Algebras [Oliveira & Cook, 2012] come to the rescue



 
 
 
 
 
 
 
 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Modular terms

18

slide-19
SLIDE 19

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Modular terms, extended

19

slide-20
SLIDE 20

▸ We refactored an external SQL query processor [Rompf & Amin,

2015] to make it more modular, shallow, and embedded



 
 
 
 
 
 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Case study

20

talks.csv

slide-21
SLIDE 21

▸ Under the surface syntax, a relational algebra expression is constructed



 


▸ Each relational algebra operator implements the following interface



 
 
 
 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

A relational algebra interpreter

21

Project Scan Print Join Filter

slide-22
SLIDE 22

▸ The interpreter is simple but slow ▸ Turning a slow interpreter into a fast compiler while

keeping the simplicity — staging (LMS [Rompf & Odersky, 2010])

▸ Actions on records are delayed to the generated code



 
 
 


▸ Two backends are supported (Scala and C), modularly

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

From interpreter to compiler

22

slide-23
SLIDE 23

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Syntax extensions

23

▸ Add aggregations (group by) and hash joins



 
 
 
 
 
 
 
 
 


slide-24
SLIDE 24

▸ The same code is generated, thus performance is similar ▸ The modularity comes with a few more lines of code



 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Evaluation

24

slide-25
SLIDE 25

▸ An OOP inspired Haskell encoding of

modular (dependent) interpretations
 
 
 
 
 
 
 
 


Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

More in the paper

25

slide-26
SLIDE 26

Shallow EDSLs and Object-Oriented Programming: Beyond Simple Compositionality

Conclusion

26

▸ OOP and shallow embeddings are closely related

▸ The essence of both is procedural abstraction

▸ OOP abstractions bring extra modularity to shallow embeddings

▸ Subtyping, inheritance and type-refinement

▸ Combine extensible interpreters with Object Algebras for greater good

▸ Modular multiple (possibly dependent) interpretations and terms

▸ Shallow embeddings can be performant with staging ▸ The motivation to employ deep embeddings becomes weaker

▸ Mostly reduced to the need for AST transformations


https://github.com/wxzh/shallow-dsl

Thank you!