Modular Implementation of Programming Languages and a Partial Order - - PowerPoint PPT Presentation

modular implementation of programming languages and a
SMART_READER_LITE
LIVE PREVIEW

Modular Implementation of Programming Languages and a Partial Order - - PowerPoint PPT Presentation

Modular Implementation of Programming Languages and a Partial Order Approach to Infinitary Rewriting Patrick Bahr paba@diku.dk University of Copenhagen Department of Computer Science PhD Defence 30 November 2012 two The Big Pictures


slide-1
SLIDE 1

Modular Implementation of Programming Languages and a Partial Order Approach to Infinitary Rewriting

Patrick Bahr paba@diku.dk

University of Copenhagen Department of Computer Science

PhD Defence 30 November 2012

slide-2
SLIDE 2

The Big Pictures two

Modular Implementation of Programming Languages Partial Order Approach to Infinitary Rewriting

f a f g a f g g a f g g g a f g g g g

2

slide-3
SLIDE 3

Modular Implementation of Programming Languages

3

slide-4
SLIDE 4

Motivation

Implementation of a DSL-Based ERP System

Enterprise resource planning systems integrate several software components that are essential for managing a business. ERP systems integrate Financial Management Supply Chain Management Manufacturing Resource Planning Human Resource Management Customer Relationship Management . . .

4

slide-5
SLIDE 5

What do ERP systems look like under the hood?

5

slide-6
SLIDE 6

An Alternative Approach

POETS [Henglein et al. 2009]

ERP Runtime System

Report Language Contract Language Rule Language UI Language Ontology Language ... ...

The abstract picture We have a number of domain-specific languages. Each pair of DSLs shares some common sublanguage. All of them share a common language of values. We have the same situation on the type level!

How do we implement this system without duplicating code?!

6

slide-7
SLIDE 7

Piecing Together DSLs – Syntax

Library of language features

F1

basic data structures

F2

reading and aggregating data from the database

F3

arithmetic operations

F4

contract clauses

F5

type definitions

F6

inference rules

7

slide-8
SLIDE 8

Piecing Together DSLs – Syntax

Library of language features

F1 F2 F3 F4 F5 F6

Constructing the DSLs Report Language =

F1 F2 F3

Contract Language =

F1 F4 F3

Ontology Language =

F1 F5

Rule Language =

F1 F6 F3

8

slide-9
SLIDE 9

Piecing Together Functions

Example: Pretty Printing

Goal: functions of type ProgramL − → String for each language L “functions” for each feature pp1 :

F1

String pp2 :

F2

String pp3 :

F3

String pp4 :

F4

String pp5 :

F5

String pp6 :

F6

String Combine functions pp1 + pp2 + pp3 :

F1 F2 F3

String Other combinations pp1 + pp5 + pp6 :

F1 F5 F6

String . . .

9

slide-10
SLIDE 10

How does it work?

Based on: Wouter Swierstra. Data types ` a la carte

10

slide-11
SLIDE 11

How does it work?

data Exp = Lit Int | Add Exp Exp | Mult Exp Exp

decompose

data Fix s = In (s (Fix s)) data Sig e = Lit Int | Add e e | Mult e e s i g n a t u r e r e c u r s i

  • n

type Exp = Fix Sig

combine

data Lit e = Lit Int data Ops e = Add e e | Mult e e Lit :+: Ops

11

slide-12
SLIDE 12

Combining Functions

Non-recursive function pp1 :: Lit String → String pp1 (Lit i) = show i pp2 :: Ops String → String pp2 (Add e1 e2) = "(" + + e1 + + " + " + + e2 + + ")" pp2 (Mult e1 e2) = "(" + + e1 + + " * " + + e2 + + ")" Fold fold :: Functor f ⇒ (f a → a) → Fix f → a fold f (In t) = f (fmap (fold f ) t) Applying Fold pp :: Fix (Lit :+: Ops) → String pp = fold (pp1 :+: pp2)

12

slide-13
SLIDE 13

Our Contributions Make compositional data types more useful in practise.

Extend the class of definable types mutually recursive types, GADTs abstract syntax trees with variable binders “Algebras with more structure” algebras with effects tree homomorphisms, tree automata, tree transducers

◮ sequential composition program optimisation (deforestation) ◮ tupling additional modularity Skip details 13

slide-14
SLIDE 14

Compositionality

We may compose tree automata along 3 different dimensions. input signature: the type of the AST A1 : µS1 → R A2 : µS2 → R = ⇒ A1 + A2 : µ(S1 + S2) → R sequential composition: a.k.a. deforestation µS1 µS2 µS3 A1 A2 A1 ◦ A2

  • utput type: tupling / product automaton construction

A1 : µS → R1 A2 : µS → R2 = ⇒ A1 × A2 : µF → R1 × R2

14

slide-15
SLIDE 15

Contextuality

tupling / product automaton construction A1 : µS → R1 A2 : µS → R2 = ⇒ A1 × A2 : µ(S) → R1 × R2 mutumorphisms / dependent product automata A1 : R2 ⇒ S → R1 A2 : R1 ⇒ S → R2 = ⇒ A1 × A2 : µS → R1 × R2

15

slide-16
SLIDE 16

Discussion

Advantages it’s just a Haskell library uses well-known concepts (algebras, tree automata, functors etc.) high degree of modularity facilitates reuse Drawbacks it’s just a Haskell library error messages are sometimes rather cryptic learning curve typical drawbacks of higher-order abstract syntax Future work reasoning about modular implementations (Meta-Theory ` a la Carte [Delaware et al. 2013]) describing interactions between modules how well does modularity scale?

16

slide-17
SLIDE 17

And now it’s time for something completely different.

slide-18
SLIDE 18

Partial Order Approach to Infinitary Rewriting

f a f g a f g g a f g g g a f g g g g

18

slide-19
SLIDE 19

Rewriting Systems

What are (term) rewriting systems? generalisation of (first-order) functional programs consist of directed symbolic equations of the form l → r semantics: any instance of a left-hand side may be replaced by the corresponding instance of the right-hand side Example (Term rewriting system defining addition and multiplication) R+∗ = x + 0 → x x ∗ 0 → 0 x + s(y) → s(x + y) x ∗ s(y) → x + (x ∗ y) s(s(0)) ∗ s(s(0)) →7 s(s(s(s(0)))) R+∗ is terminating!

19

slide-20
SLIDE 20

Non-Terminating Rewriting Systems

Termination: repeated rewriting eventually reaches a normal form. Non-terminating systems can be meaningful modelling reactive systems, e.g. by process calculi approximation algorithms which enhance the accuracy of the approximation with each iteration, e.g. computing π specification of infinite data structures, e.g. streams Example (Infinite lists) Rnats =

  • from(x) → x : from(s(x))

from(0) →6 0 : 1 : 2 : 3 : 4 : 5 : from(6) → . . . intuitively this converges to the infinite list 0 : 1 : 2 : 3 : 4 : 5 : . . . .

20

slide-21
SLIDE 21

Infinitary Term Rewriting – The Metric Approach

When does a rewrite sequence converge? Rewrite rules are applied at increasingly deeply nested subterms. What is the result of a converging rewrite sequence? A converging rewrite sequence approximates a uniquely determined term t arbitrary well. t0 → t1 → t2 → . . . t For each depth d ∈ N there is some n ∈ N, such that t0 → t1 → . . . → tn → tn+1 → . . . t

  • do not differ up to depth d

21

slide-22
SLIDE 22

Example: Convergence of a Reduction

R = {a → g(a)} f a f g a f g g a f g g g a f g g g g

22

slide-23
SLIDE 23

Example: Non-Convergence of a Reduction

R =

  • a → g(a)

h(x) → h(g(x)) f a h b f a h g b f g a h g b f g a h g g b f g g a h g g b . . .

23

slide-24
SLIDE 24

Issues of the Metric Approach

Notion of convergence is too restrictive (no notion of local convergence) May still not reach a normal form Orthogonal TRSs are not infinitarily confluent Infinitary confluence t t1 t2 t′ For every t, t1, t2 ∈ T ∞(Σ, V) with t1 և t ։ t2 there is a t′ ∈ T ∞(Σ, V) with t1 ։ t′ և t2

24

slide-25
SLIDE 25

Partial Order Approach to Infinitary Term Rewriting

Partial order on terms partial terms: terms with additional constant ⊥ (read as “undefined”) partial order ≤⊥ reads as: “is less defined than” ≤⊥ is a complete semilattice (= bounded complete cpo) Convergence formalised by the limit inferior: lim inf

ι→α tι =

  • β<α
  • β≤ι<α

tι intuition: eventual persistence of nodes of the terms convergence: limit inferior of the contexts of the reduction

25

slide-26
SLIDE 26

An Example

f a h b f a h g b f g a h g b f g a h g g b f g g a h g g b . . . f g g a h g g b f g g ⊥ p-converges to

26

slide-27
SLIDE 27

Properties of the Partial Order Approach

Benefits reduction sequences always converge (but result may contain ⊥s) more fine-grained than the metric approach subsumes metric approach, i.e. both approaches agree on total reductions Theorem (total p-convergence = m-convergence) For every reduction S in a TRS, we have S : s ։

p t is total

⇐ ⇒ S : s ։

m t.

Theorem (confluence, normalisation) Every orthogonal TRS is normalising and confluent w.r.t. p-convergent reductions, i.e. every term has a unique normal form.

27

slide-28
SLIDE 28

Sharing – From Terms to Term Graphs

Lazy evaluation and infinitary rewriting

Skip term graphs

Lazy evaluation consists of two things: non-strict evaluation sharing avoids duplication Example from(x) → x : from(s(x)) from x : x from s x

28

slide-29
SLIDE 29

Example

from : from s : : s from s : : s : s

29

slide-30
SLIDE 30

Properties of Infinitary Term Graph Rewriting

Theorem (total p-convergence = m-convergence) For every reduction S in a GRS, we have S : g ։

p h is total

⇐ ⇒ S : g ։

m h.

Theorem (soundness) For every left-linear, left-finite GRS R we have g h

m p

R s U (·) U (R) t

m

U (·)

30

slide-31
SLIDE 31

Completeness

Theorem (Completeness) p-convergence in an orthogonal, left-finite GRS R is complete: s t

p

g U (·) U (R) t′ h

p

U (·)

p

R Does not hold for metric convergence! Completeness of m-convergence for normalising reductions s t ∈ NF

m

g U (·) h

m

U (·) U (R) R

31

slide-32
SLIDE 32

Discussion

Contributions novel approach to infinitary term rewriting first formalisation of infinitary term graph rewriting Note: B¨

  • hm reduction for TRSs

s ։

p R t

⇐ ⇒ s ։

m B t

B adds to R rules of the form t → ⊥ for each root-active term t. Future work: Infinitary term graph rewriting Are orthogonal systems infinitarily confluent? higher-order systems (e.g. lambda calculus with letrec)

32

slide-33
SLIDE 33

Publications

[1] Patrick Bahr. Modes of Convergence for Term Graph Rewriting. Logical Methods in Computer Science 8(2), pp. 1-60, 2012. [2] Patrick Bahr. Modular Tree Automata. Mathematics of Program Construction, pp. 263-299, 2012. [3] Patrick Bahr. Infinitary Term Graph Rewriting is Simple, Sound and Complete. 23rd International Conference on Rewriting Techniques and Applications (RTA’12) , pp. 69-84, 2012. [4] Patrick Bahr. Modes of Convergence for Term Graph Rewriting. 22nd International Conference on Rewriting Techniques and Applications (RTA’11), pp. 139-154, 2011. [5] Patrick Bahr. Partial Order Infinitary Term Rewriting and B¨

  • hm Trees. Proceedings of the 21st International Conference on

Rewriting Techniques and Applications, pp. 67-84, 2010. [6] Patrick Bahr. Abstract Models of Transfinite Reductions. Proceedings of the 21st International Conference on Rewriting Techniques and Applications, pp. 49-66, 2010. [7] Patrick Bahr, Tom Hvitved. Parametric Compositional Data Types. Proceedings Fourth Workshop on Mathematically Structured Functional Programming, pp. 3-24, 2012. [8] Patrick Bahr, Tom Hvitved. Compositional data types. Proceedings of the seventh ACM SIGPLAN workshop on Generic programming, pp. 83-94, 2011. [9] Patrick Bahr. Evaluation ` a la Carte: Non-Strict Evaluation via Compositional Data Types. Proceedings of the 23rd Nordic Workshop on Programming Theory, pp. 38-40, 2011. [10] Patrick Bahr. A Functional Language for Specifying Business Reports. Proceedings of the 23rd Nordic Workshop on Programming Theory, pp. 24-26, 2011. [11] Patrick Bahr. Convergence in Infinitary Term Graph Rewriting Systems is Simple. Submitted to Math. Structures Comput. Sci. [12] Patrick Bahr. Partial Order Infinitary Term Rewriting and B¨

  • hm Trees. Submitted to Log. Methods Comput. Sci.

33