SlideShare a Scribd company logo
1 of 23
Download to read offline
Compositional Programming
Weixin Zhang1,2, Yaozhu Sun2, and Bruno C. d. S. Oliveira2


1.University of Bristol 2.The University of Hong Kong


ECOOP 2021
1
▸ Conventional object-oriented programming and functional programming suffer from the
Expression Problem [Wadler 1998]


▸ Dealing with dependencies modularly poses extra challenges


▸ Existing design patterns partly address these problems


▸ E.g. Object Algebras [Oliveira & Cook 2012], Polymorphic Embedding [Hofer et al. 2008], Cake pattern [Odersky &
Zenger 2005], Finally Tagless [Carette et al. 2009], Datatypes a la carte [Swierstra 2008]


▸ Lack of proper mechanisms for modular dependencies and compositions


▸ Heavily parameterized and boilerplate code
Compositional Programming
Motivation
class Mul(e1: Exp, e2: Exp) extends Exp {


def eval = e1.eval * e2.eval


}
print :: Exp -> String


print (Lit n) = show n


print (Add e1 e2) =


if eval e2 == 0 -- dependency on eval


then print e1


else "(" ++ print e1 ++ "+" ++ print e2 ")
abstract class Exp {


def eval: Int


}


class Lit(n: Int) extends Exp {


def eval = n


}


class Add(e1: Exp, e2: Exp) extends Exp {


def eval = e1.eval + e2.eval


}
OOP
data Exp where


Lit :: Int -> Exp


Add :: Exp -> Exp -> Exp


eval :: Exp -> Int


eval (Lit n) = n


eval (Add e1 e2) = eval e1 + eval e
FP
2
Compositional Programming
Contributions
▸ Compositional Programming: A new statically-typed modular programming style


▸ Solving the Expression Problem and dealing with modular programs with complex dependencies


▸ CP: A language design for Compositional Programming


▸ Elaborated to Fi
+ [Bi et al., 2019], a recent calculus that supports disjoint intersection types [Oliveira et
al. 2016], disjoint polymorphism [Alpuim et al. 2017] and nested composition [Bi et al. 2018]


▸ We proved that the elaboration is type-safe and coherent


▸ Attribute Grammars in CP


▸ Inspired by Rendel et al. [2014]’s encoding but without explicit de
fi
nitions of composition operators


▸ Polymorphic contexts


▸ Allowing for modular contexts in modular components


▸ Implementation, case studies, and examples
3
Compositional Programming
Solving the Expression Problem: Operation Extensions
4
Compositional interfaces
First-class traits
Method patterns
Nested trait composition
Self-type annotations
Compositional Programming
Solving the Expression Problem: Variant Extensions
5
Inheritance
Overriding
Compositional Programming
Dependencies and S-attributed Grammars
6
▸ CP can deal with programs with complex dependencies modularly


▸ Child dependencies: attributes depend on other synthesized attributes of the
children














Strong dependency
Weak dependency
Compositional Programming
Dependencies and S-attributed Grammars
7
▸ Self dependencies: attributes depend on other synthesized attributes
of the self-reference






▸ Mutual dependencies: two attributes are inter-de
fi
ned








Compositional Programming
Context Evolution
8
▸ Highly non-modular: existing code has to be modi
fi
ed when a new context is needed


▸ Not encapsulating contexts: contexts are fully exposed even if not directly used
▸ Problem: different modular components may require different contexts
Compositional Programming
Polymorphic Contexts
9
▸ Allowing modular & encapsulated contexts














Disjoint polymorphism
▸ Composing the components with different contexts modularly














Compositional Programming
Polymorphic Contexts
10
Compositional Programming
Formal Syntax
11
CP program
Source
Fi+ expression
Target
Elaboration


Declarations, Sorts & Trait-related constructs
▸ The elaboration builds on ideas from generalized Object Algebras [Oliveira
et al. 2013] and the denotational model of inheritance [Cook and Palsberg, 1989]

















Compositional Programming
Elaborating Compositional Interfaces and Sorts
12
Compositional Programming
Elaborating Traits
13
Compositional Programming
Elaborating Child Dependencies
14
Compositional Programming
Elaborating Self-type Annotations
15
Compositional Programming
Elaborating Inheritance and Overriding
16
Compositional Programming
Elaboration Overview
17
▸ We have proved that the elaboration of CP into Fi
+ is type-
safe and coherent


▸ Type-safety theorem


▸


▸ Coherence theorem


▸ Each well-typed CP program has a unique elaboration
Compositional Programming
Metatheory
18
▸ A DSL for parallel pre
fi
x circuits [Hinze 2004]









▸ Interpretations: width, depth, wellSized and layout (depending on width)


▸ Variant extension: RStretch


▸ Most compact and modular w.r.t existing implementations





Compositional Programming
Case Studies: Scans
19
Language Haskell [Gibbons & Wu, 2014] Scala [Zhang & Oliveira, 2019] Fi
+ [Bi et al., 2019] CP
SLOC 87 129 72 70
Compositional Programming
Case Studies: Mini Interpreter
20
▸ A mini interpreter for an expression language (~700 SLOC)


▸ Including numeric and boolean literals, arithmetic expressions, logical
expressions, comparisons, branches, variable bindings, function closures …


▸ Sublanguages are separately de
fi
ned as features that can be arbitrarily
combined to form a product line of interpreters


▸ Examine the ability to model non-trivial dependencies and multi-
sorted languages







Compositional Programming
Case Studies: C0 Compiler
21
▸ An educational one-pass compiler


▸ A subset of C compiled to Java bytecode


▸ Originally written in Java with semantics hardcoded in the parser, thus is non-
modular


▸ Rendel et al. [2014] modularized C0 using generalized Object Algebras


▸ Comparison








Compositional Programming
Future Work
▸ There is a lot of room for making CP more expressive and
practical


▸ Recursive types and type constructors


▸ Mutable states


▸ Type inference
22
Compositional Programming
Conclusion
23
▸ We have presented key concepts of Compositional
Programming and a language design called CP


▸ Offering an alternative style to FP and OOP


▸ Allowing programs with non-trivial dependencies to be modularized
in a natural way


▸ Applicability demonstrated by various examples and case studies


▸ Artifact is available at
Thank you!
https://github.com/wxzh/CP

More Related Content

Similar to Compositional Programming

PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesSchwannden Kuo
 
A Survey of Object Oriented Programming LanguagesMaya Hris.docx
A Survey of Object Oriented Programming LanguagesMaya Hris.docxA Survey of Object Oriented Programming LanguagesMaya Hris.docx
A Survey of Object Oriented Programming LanguagesMaya Hris.docxdaniahendric
 
Java-centered Translator-based Multi-paradigm Software Development Environment
Java-centered Translator-based Multi-paradigm Software Development EnvironmentJava-centered Translator-based Multi-paradigm Software Development Environment
Java-centered Translator-based Multi-paradigm Software Development EnvironmentWaqas Tariq
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture OneAngelo Corsaro
 
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELINGPYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELINGijseajournal
 
c++session 1.pptx
c++session 1.pptxc++session 1.pptx
c++session 1.pptxPadmaN24
 
Open issue in oop
Open issue in oopOpen issue in oop
Open issue in oopAnas Ahmed
 
ML Tutorial Introduction
ML Tutorial IntroductionML Tutorial Introduction
ML Tutorial Introductionelbop
 
Exact implementation of design patterns in
Exact implementation of design patterns inExact implementation of design patterns in
Exact implementation of design patterns inijait
 
The Concept Of Abstract Data Types
The Concept Of Abstract Data TypesThe Concept Of Abstract Data Types
The Concept Of Abstract Data TypesKaty Allen
 
Design patterns in_c_sharp
Design patterns in_c_sharpDesign patterns in_c_sharp
Design patterns in_c_sharpCao Tuan
 
Principles of Programming Languages - Lecture Notes
Principles of Programming Languages -  Lecture NotesPrinciples of Programming Languages -  Lecture Notes
Principles of Programming Languages - Lecture Notessuthi
 
Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Krishna Sai
 
Scales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduates
Scales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduatesScales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduates
Scales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduatesHans Ecke
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
 
Ppt about programming in methodology
Ppt about programming in methodology Ppt about programming in methodology
Ppt about programming in methodology Vaishnavirakshe2
 

Similar to Compositional Programming (20)

PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminaries
 
A Survey of Object Oriented Programming LanguagesMaya Hris.docx
A Survey of Object Oriented Programming LanguagesMaya Hris.docxA Survey of Object Oriented Programming LanguagesMaya Hris.docx
A Survey of Object Oriented Programming LanguagesMaya Hris.docx
 
Java-centered Translator-based Multi-paradigm Software Development Environment
Java-centered Translator-based Multi-paradigm Software Development EnvironmentJava-centered Translator-based Multi-paradigm Software Development Environment
Java-centered Translator-based Multi-paradigm Software Development Environment
 
Modest Formalization of Software Design Patterns
Modest Formalization of Software Design PatternsModest Formalization of Software Design Patterns
Modest Formalization of Software Design Patterns
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture One
 
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELINGPYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
 
c++session 1.pptx
c++session 1.pptxc++session 1.pptx
c++session 1.pptx
 
Open issue in oop
Open issue in oopOpen issue in oop
Open issue in oop
 
ML Tutorial Introduction
ML Tutorial IntroductionML Tutorial Introduction
ML Tutorial Introduction
 
A Primer on High-Quality Identifier Naming [ASE 2022]
A Primer on High-Quality Identifier Naming [ASE 2022]A Primer on High-Quality Identifier Naming [ASE 2022]
A Primer on High-Quality Identifier Naming [ASE 2022]
 
Exact implementation of design patterns in
Exact implementation of design patterns inExact implementation of design patterns in
Exact implementation of design patterns in
 
The Concept Of Abstract Data Types
The Concept Of Abstract Data TypesThe Concept Of Abstract Data Types
The Concept Of Abstract Data Types
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
C++ programing lanuage
C++ programing lanuageC++ programing lanuage
C++ programing lanuage
 
Design patterns in_c_sharp
Design patterns in_c_sharpDesign patterns in_c_sharp
Design patterns in_c_sharp
 
Principles of Programming Languages - Lecture Notes
Principles of Programming Languages -  Lecture NotesPrinciples of Programming Languages -  Lecture Notes
Principles of Programming Languages - Lecture Notes
 
Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-
 
Scales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduates
Scales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduatesScales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduates
Scales02WhatProgrammingLanguagesShouldWeTeachOurUndergraduates
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
Ppt about programming in methodology
Ppt about programming in methodology Ppt about programming in methodology
Ppt about programming in methodology
 

Recently uploaded

TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...ssifa0344
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...Sérgio Sacani
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...RohitNehra6
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfSumit Kumar yadav
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Cultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxCultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxpradhanghanshyam7136
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )aarthirajkumar25
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptMAESTRELLAMesa2
 

Recently uploaded (20)

TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdf
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Cultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxCultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptx
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.ppt
 

Compositional Programming

  • 1. Compositional Programming Weixin Zhang1,2, Yaozhu Sun2, and Bruno C. d. S. Oliveira2 1.University of Bristol 2.The University of Hong Kong ECOOP 2021 1
  • 2. ▸ Conventional object-oriented programming and functional programming suffer from the Expression Problem [Wadler 1998] ▸ Dealing with dependencies modularly poses extra challenges ▸ Existing design patterns partly address these problems ▸ E.g. Object Algebras [Oliveira & Cook 2012], Polymorphic Embedding [Hofer et al. 2008], Cake pattern [Odersky & Zenger 2005], Finally Tagless [Carette et al. 2009], Datatypes a la carte [Swierstra 2008] ▸ Lack of proper mechanisms for modular dependencies and compositions ▸ Heavily parameterized and boilerplate code Compositional Programming Motivation class Mul(e1: Exp, e2: Exp) extends Exp { def eval = e1.eval * e2.eval } print :: Exp -> String print (Lit n) = show n print (Add e1 e2) = if eval e2 == 0 -- dependency on eval then print e1 else "(" ++ print e1 ++ "+" ++ print e2 ") abstract class Exp { def eval: Int } class Lit(n: Int) extends Exp { def eval = n } class Add(e1: Exp, e2: Exp) extends Exp { def eval = e1.eval + e2.eval } OOP data Exp where Lit :: Int -> Exp Add :: Exp -> Exp -> Exp eval :: Exp -> Int eval (Lit n) = n eval (Add e1 e2) = eval e1 + eval e FP 2
  • 3. Compositional Programming Contributions ▸ Compositional Programming: A new statically-typed modular programming style ▸ Solving the Expression Problem and dealing with modular programs with complex dependencies ▸ CP: A language design for Compositional Programming ▸ Elaborated to Fi + [Bi et al., 2019], a recent calculus that supports disjoint intersection types [Oliveira et al. 2016], disjoint polymorphism [Alpuim et al. 2017] and nested composition [Bi et al. 2018] ▸ We proved that the elaboration is type-safe and coherent ▸ Attribute Grammars in CP ▸ Inspired by Rendel et al. [2014]’s encoding but without explicit de fi nitions of composition operators ▸ Polymorphic contexts ▸ Allowing for modular contexts in modular components ▸ Implementation, case studies, and examples 3
  • 4. Compositional Programming Solving the Expression Problem: Operation Extensions 4 Compositional interfaces First-class traits Method patterns Nested trait composition Self-type annotations
  • 5. Compositional Programming Solving the Expression Problem: Variant Extensions 5 Inheritance Overriding
  • 6. Compositional Programming Dependencies and S-attributed Grammars 6 ▸ CP can deal with programs with complex dependencies modularly ▸ Child dependencies: attributes depend on other synthesized attributes of the children 
 










 Strong dependency Weak dependency
  • 7. Compositional Programming Dependencies and S-attributed Grammars 7 ▸ Self dependencies: attributes depend on other synthesized attributes of the self-reference 
 


 ▸ Mutual dependencies: two attributes are inter-de fi ned 
 





  • 8. Compositional Programming Context Evolution 8 ▸ Highly non-modular: existing code has to be modi fi ed when a new context is needed ▸ Not encapsulating contexts: contexts are fully exposed even if not directly used ▸ Problem: different modular components may require different contexts
  • 9. Compositional Programming Polymorphic Contexts 9 ▸ Allowing modular & encapsulated contexts 
 










 Disjoint polymorphism
  • 10. ▸ Composing the components with different contexts modularly 
 










 Compositional Programming Polymorphic Contexts 10
  • 11. Compositional Programming Formal Syntax 11 CP program Source Fi+ expression Target Elaboration Declarations, Sorts & Trait-related constructs
  • 12. ▸ The elaboration builds on ideas from generalized Object Algebras [Oliveira et al. 2013] and the denotational model of inheritance [Cook and Palsberg, 1989] 
 













 Compositional Programming Elaborating Compositional Interfaces and Sorts 12
  • 18. ▸ We have proved that the elaboration of CP into Fi + is type- safe and coherent ▸ Type-safety theorem ▸ ▸ Coherence theorem ▸ Each well-typed CP program has a unique elaboration Compositional Programming Metatheory 18
  • 19. ▸ A DSL for parallel pre fi x circuits [Hinze 2004] 
 





 ▸ Interpretations: width, depth, wellSized and layout (depending on width) ▸ Variant extension: RStretch ▸ Most compact and modular w.r.t existing implementations 
 

 Compositional Programming Case Studies: Scans 19 Language Haskell [Gibbons & Wu, 2014] Scala [Zhang & Oliveira, 2019] Fi + [Bi et al., 2019] CP SLOC 87 129 72 70
  • 20. Compositional Programming Case Studies: Mini Interpreter 20 ▸ A mini interpreter for an expression language (~700 SLOC) ▸ Including numeric and boolean literals, arithmetic expressions, logical expressions, comparisons, branches, variable bindings, function closures … ▸ Sublanguages are separately de fi ned as features that can be arbitrarily combined to form a product line of interpreters ▸ Examine the ability to model non-trivial dependencies and multi- sorted languages 
 




  • 21. Compositional Programming Case Studies: C0 Compiler 21 ▸ An educational one-pass compiler ▸ A subset of C compiled to Java bytecode ▸ Originally written in Java with semantics hardcoded in the parser, thus is non- modular ▸ Rendel et al. [2014] modularized C0 using generalized Object Algebras ▸ Comparison 
 





  • 22. Compositional Programming Future Work ▸ There is a lot of room for making CP more expressive and practical ▸ Recursive types and type constructors ▸ Mutable states ▸ Type inference 22
  • 23. Compositional Programming Conclusion 23 ▸ We have presented key concepts of Compositional Programming and a language design called CP ▸ Offering an alternative style to FP and OOP ▸ Allowing programs with non-trivial dependencies to be modularized in a natural way ▸ Applicability demonstrated by various examples and case studies ▸ Artifact is available at Thank you! https://github.com/wxzh/CP