modular design of domain specific languages using
play

Modular Design of Domain-Specific Languages using Splittings of - PowerPoint PPT Presentation

Modular Design of Domain-Specific Languages using Splittings of Catamorphisms Eric Badouel and Rodrigue Aim e Djeumen Djatcha Inria - Rennes, France and Univ. Douala, Cameroon ICTAC 2018, Stellenbosch, South Africa E. Badouel, R. Djeumen


  1. Modular Design of Domain-Specific Languages using Splittings of Catamorphisms Eric Badouel and Rodrigue Aim´ e Djeumen Djatcha Inria - Rennes, France and Univ. Douala, Cameroon ICTAC 2018, Stellenbosch, South Africa E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 1 / 16

  2. Domain specific languages Domain Specific Languages Language-oriented approach to modularity Design a domain specific language (DSL) by packaging reusable domain knowledge into language features. Develop a specific application by writing a program in this DSL. Domain Specific Embedded Languages (DSEL) P. Hudak The DSEL inherits from its host language features not specific to the domain. It also inherits the tools used as a support to programming (edition, type checking, compilation ...). Several DSELs can communicate through their shared host language. Haskell is a good host language DSL for denotational semantics. Monadic computations. E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 2 / 16

  3. A language of boxes Example: A language of boxes x−coord hb (0,0) vl vr hb vl ht y−coord A box is either an elementary box (with a unit size: its depth and height is one) or is obtained by composing two sub-boxes. Two boxes can be composed either vertically with a left (vl) or a right (vr) alignement or horizontally with a top (ht) or bottom (hb) alignment. E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 3 / 16

  4. A language of boxes The semantic domain is given by attributes We assign a set of attributes to each sort Inherited attribute: position of its origin. Synthesized attributes: size ot the box and the origin of the elementary boxes it contains. Semantic interpretation of a box: data Size = Size { depth , height :: Double } deriving Show data Point = Point { xcoord, ycoord :: Double } deriving Show data Box = Box { list :: Point -> [Point] Size } ,size :: E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 4 / 16

  5. A language of boxes The Interpretation functions are given by semantic rules semantic rules elembox = SBox ( \ pt → [pt])(Size 1 1) hb box1 box2 = SBox list’ size’ where list’ pt = (list box1 (pi1 pt))++(list box2 (pi2 pt)) size’ = Size (d1 + d2)(max h1 h2) pi1 (Point x y) = Point x (y + (max (h2-h1) 0)) pi2 (Point x y) = Point x (y + (max (h1-h2) 0)) Size d1 h1 = size box1 Size d2 h2 = size box2 ... E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 5 / 16

  6. A language of boxes Application Programming Interface (API) of the DSL The description of datatype Box is not exported (abstract data type) Functions that allows to use a box (destructors): size :: Box → Size :: Box → Point → [ Point ] list Functions that allows to build a box (constructors): :: ebox Box hb , ht , vt , vb :: Box → Box → Box For example: size (hb (vl (hb ebox ebox) ebox) (vr ebox (vl ebox (ht ebox ebox)))) = Size { depth =4, height =3 } E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 6 / 16

  7. A language of boxes Extending a Domain Specific Language By adding new destructors and/or constructors in a conservative way: The expression problem (Philip Walder) The goal is to define a data type by cases, where one can add new cases to the data type and new functions over the datatype, without recompiling existing code, and while retaining static type safety. A solution: Wouter Swierstra, data types ` a la carte . But sometimes we need to refactor more drastically the domains of interpretation and one cannot reuse the existing implementation, eg data Box = Image { image :: Point -> Maybe Color, bb :: Size } E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 7 / 16

  8. Algebras and modular decomposition Algebras Multi-sorted signature Σ = ( S , Ω) (syntax): a set of sorts s ∈ S ; a set of operators ω ∈ Ω( s 1 · · · s n , s ) with arities and sort. A Σ-algebra ( A , ϕ ) (interpretation) a domain of interpretation A s for each s ∈ S ; a set of interpretation functions ω ϕ :: A s 1 · · · A s n → A s for each ω ∈ Ω( s 1 · · · s n , s ). Letting A = ζ × α ( ζ for the parameters), an algebra is a morphism ϕ : F ζα → α for the (parametric) polynomial functor F : | Set | p + n → | Set | n where � F ( X ) s = X s 1 × . . . × X s n ω ∈ Ω( s 1 ··· s n , s ) the free algebra (of terms) F † ζ (note: F † is called the type functor � ∼ � F † ζ = F † ζ . associated with F ) satisfies a natural isomorphism F ζ F † ζ is the initial algebra, hence the canonical morphism of algebras ) F ,ζ : F † ζ → α (catamorphism). ( [ ϕ ] E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 8 / 16

  9. Algebras and modular decomposition Continuous Algebras Haskell functions are however interpreted in the category H = DCPO ⊥ of pointed dcpos and continuous functions. Which does not have coproducts The subcategory C = DCPO ⊥ ! of pointed dcpos and strict continuous functions. Finite products are given by the cartesian products Finite coproduct of two dcpos is their coalesced sum A ⊕ B The lifting operator ( − ) ⊥ consists in adding a new least element to a given dcpo: A ⊥ = A ⊎ {⊥} . The sum of pointed dcpos be given by � 1 ≤ i ≤ n A i = ( A 1 ) ⊥ ⊕ · · · ⊕ ( A n ) ⊥ or equivalently by � 1 ≤ i ≤ n A i = ( A 1 ⊎ · · · ⊎ A n ) ⊥ . NOT ASSOCIATIVE. C ( A ⊥ , B ) ∼ = H ( A , B ) hence � A i , B ) = C ( ⊕ 1 ≤ i ≤ n ( A i ) ⊥ , B ) ∼ � C (( A i ) ⊥ , B ) ∼ � C ( = = H ( A i , B ) 1 ≤ i ≤ n 1 ≤ i ≤ n 1 ≤ i ≤ n E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 9 / 16

  10. Algebras and modular decomposition Algebras in Haskell The Haskell data type definition data Tree a = Node a (Forest a) data Forest a = Leaf | Cons (Tree a) (Forest a) is associated with the (parametric) polynomial functor F : C 3 → C 2 given by F ( A , T , F ) = (( A × F ) ⊥ , 1 + ( T × F )) An F -algebra ϕ : F ζα → α is a continuous Σ-algebra = Alg { node :: data Alg a t f a -> f -> t, leaf :: f, t -> f -> t } cons :: E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 10 / 16

  11. Algebras and modular decomposition Polynomial and regular functors Any locally continuous functor F : |C| p + n → |C| n has a (locally continuous) parametric fixed-point F † : |C| p → |C| n which can be computed by elimination (Beki´ c’s Theorem). Regular functors Least class of functors containing the polynomial functors and closed by type functor. Regular (and thus polynomial) functors are locally continuous. E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 11 / 16

  12. Algebras and modular decomposition Module Module: G = ( F , D ) polynomial functor F : C p + n + m → C n associated with a signature Σ = ( S , Ω) where S = S p ⊎ S d ⊎ S i with s ∈ S p is a parameter ( | S p | = p ). s ∈ S d is a defined sort ( | S d | = n ). s ∈ S i is a imported sort ( | S i | = m ). Regular functor D : C p + n → C m represents the imported definitions of the grammar. The functor associated with the modular grammar is the (regular) functor F G = F ◦ � id p + n , D � : C p + n → C n We let F ( G ) = F and D ( G ) = D denote the respective components of module G . E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 12 / 16

  13. Algebras and modular decomposition Modular Decomposition F : C p + n → C n where n = n 1 + n 2 ◦ F : C p + n → C n 2 , Subsystem: F 2 = π ( n 1 , n 2 ) 2 � � : C p + n 1 → C n 1 id p + n 1 , F † Residual system: F / F 2 = F 1 ◦ 2 c’s Theorem: F † = ( F / F 2 ) † ⋊ F † Beki´ 2 The semidirect product (or cascaded composition) of functors H : C p → C n and T : C p + n → C m is given by H ⋊ T = � H , T ◦ � id p , H �� : C p → C n + m E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 13 / 16

  14. Algebras and modular decomposition Splitting of a modular Language Proposition Let G = ( F , D ) be a modular grammar with polynomial functor F : C p + n + m → C n and regular functor D : C p + n → C m . If n = n 1 + n 2 then π ( n 1 , n 2 ) ◦ F G = F G 2 and 2 F G / G 2 = F G / F G 2 where the second projection G 2 = π ( n 1 , n 2 ) ( G ) of modular 2 grammar G is given by C ( p + n 1 )+ n 2 + m → C n 2 F ( G 2 ) = π ( n 1 , n 2 ) ◦ F ( G ) : 2 C ( p + n 1 )+ n 2 → C m D ( G 2 ) = D ( G ) : and the residual operation is defined as C p + n 1 +( n 2 + m ) → C n 1 F ( G / G 2 ) = π ( n 1 , n 2 ) ◦ F ( G ) : 1 C p + n 1 → C n 2 + m D ( G / G 2 ) = F † G 2 ⋊ D ( G ) : � � F † F † ⋊ F † Corollary: G = G / G 2 G 2 E. Badouel, R. Djeumen (Inria & Univ. Douala) Modular Design of DSLs ICTAC 2018, Stellenbosch 14 / 16

Recommend


More recommend