Polymorphic Single-Pushout Graph Transformation Michael Löwe, Harald König, Christoph Schulz Bericht Nr.:
02013/04
Impressum Forschungsberichte der FHDW Hannover – Veröffentlichungen aus dem Bereich Forschung und Entwicklung der FHDW Hannover.
Herausgeber:
Die Professoren der FHDW Hannover Fachhochschule für die Wirtschaft Hannover Freundallee 15 30173 Hannover Kontakt:
[email protected]
ISSN 1863-7043
POLYMORPHIC SINGLE-PUSHOUT GRAPH TRANSFORMATION MICHAEL LÖWE, HARALD KÖNIG, AND CHRISTOPH SCHULZ Abstract. The paper extends single-pushout graph transformation by polymorphism, a key concept in object-oriented design. The notions sub-rule and remainder, well-known in single-pushout rewriting, are applied to model dynamic rule extension and type dependent rule application. This extension mechanism qualifies graph transformation as a modelling technique for extendable frameworks. Therefore, it contributes to the applicability of graph transformation in software engineering. The increase of expressive power gained by the extension is demonstrated by some instructive examples.
1. Introduction and Related Work Algebraic graph transformation has been extended by many object-oriented modelling concepts, for example types and attributes, compare [2]. However, the central structure of object-orientation, namely inheritance with polymorphism, has not been completely integrated yet. We propose a well founded concept for polymorphism in the single pushout approach [12]. It builds on the results we obtained for the double-pushout approach [2] in [14]. Most related theoretical research lines do not admit polymorphism. H. Ehrig et al. [2] introduce inheritance as an additional set of inheritance edges between vertices in the type graph. It is not required that this structure is hierarchical. Cycle-freeness is not necessary, since they do not work with the original type graph. Instead they use a canonically flattened type structure, in which inheritance edges are removed and some of the other edges are copied to the “more special” vertices. By this reduction, they get rid of inheritance and are able to reestablish their theoretical results. E. Guerra and J. de Lara [7] extend this approach to inheritance between vertices and edges. F. Hermann et al. [5] avoid this flattening and define a weak adhesive category based on the original type graph with inheritance structure. The rule morphisms are required to reflect the subtype structure: If an image of a morphism possesses subtypes, all these subtypes have pre-images under the morphism. This feature considerably restricts the applicability to examples like those in Section 4. U. Golas et al. [6] also avoid flattening. They require that the paths along inheritance edges are cycle-free (hierarchy) and that every vertex has at most one abstraction (single-inheritance). For this set-up, they devise an adhesive category comparable to our approach in [14] but restricted to single-inheritance. All the above mentioned related concepts do not address redefinition of rules and “code sharing” by using super rules and polymorphism. One approach working in this direction is the graph transformation model of object-oriented programming by 1
A. P. Lüdtke Ferreira and L. Ribeiro [5], which is based on single-pushout rewriting. They allow vertex and edge specialisations in the type graph and show that suitably restricted situations admit pushouts of partial morphisms. Their framework is shown to be adequate as a model for object-oriented systems. They do not address further categorical properties like hereditary pushout properties. The work in [5] aims at modelling object-oriented concepts like inheritance and polymorphism by single-pushout graph grammars. It does not equip general single-pushout rewriting with polymorphism. There are some practical approaches that allow rule extension. One example is [11] which is based on triple graph grammars. The operational effects are comparable to ours, but the devised mechanisms are described informally only. The paper is organised as follows. Section 2 recapitulates the theory of singlepushout graph transformation1. Especially the concepts sub-rule, remainder, and amalgamated rule are introduced, since they are used to model object-oriented concepts of method redefinition and super calls. Section 3 summarises the results of [14]. It introduces the basic category GT of graphs typed in a type graph T with inheritance hierarchy. The morphisms in GT can map a vertex v to any vertex the type of which is a specialisation of the type of v. In section 4, the sufficient conditions for GT to admit single-pushout rewriting presented in Section 2 are proved. These results are new. Section 4 also introduces the new concept of a polymorphic graph transformation system. This concept allows type dependent behaviour. The increase in the expressive power of graph transformations is demonstrated by some examples. Section 5 discusses topics of future research. 2. Single-Pushout Transformation Framework Single-pushout graph transformation simplifies the classical double-pushout approach [3, 2] by the construction of a span category that models double-pushout l
r
rules (L K ⇢ R) as morphisms. A span base (C, M) consists of a category C and a subclass M of the morphisms of C such that: (1) (2) (3) (4) (5)
M contains all isomorphisms of C. M is closed under composition. M is prefix-closed: q p 2 M =) p 2 M. C has all pullbacks for all pairs of morphisms (p, q) with p 2 M. Pullbacks in C are M-closed: (p⇤ , q ⇤ ) pullback of (p 2 M, q) =) p⇤ 2 M.
Given an object A 2 C, C #M A denotes the restriction of the comma category C # A to M-morphisms2. The conditions 1, 2, and 3 guarantee that C #M A is a category. The conditions 4 and 5 provide a pullback functor h⇤ : C #M B ! C #M A for every morphism h : A ! B in C. A concrete M-span is a pair of C-morphisms (p 2 M, q) such that domain(p) = domain(q). Two M-spans (p1 , q1 ) and (p2 , q2 ) are equivalent and denote the same abstract span if there is an isomorphism i such that p1 i = p2 and q1 i = q2 ; 1
Compare [12, 10, 13, 9]
2The M-morphisms are the objects of C # M A and, due to condition 3, each C #M A-morphism
is a M-morphisms.
2
in this case we write (p1 , q1 ) ⌘ (p2 , q2 ) and [(p, q)]⌘ for the class of spans that are equivalent to (p, q). The category of abstract M-spans M(C) over C has the same objects as C and M(C) equivalence classes of spans wrt. ⌘ as arrows. The identities are defined by idA = [(idA , idA )]⌘ and composition of [(p, q)]⌘ and [(r, s)]⌘ such that codomain(q) = codomain(r) is given by [(r, s)]⌘ M(C) [(p, q)]⌘ = [(p r0 , s q 0 )]⌘ where (r0 , q 0 ) is a pullback of (q, r). Note that there is the natural embedding faithful functor ◆ : C ! M(C) defined by identity on objects and (f : A ! B) 7! [A
idA
a slight abuse of notation, we call a morphism [A [A
d
f
A ! B] on morphisms. By d
f
f
B ! C] total and write
B ! C] 2 C if d is isomorphism. A morphism [A
co-total, From now on, we write A h iff f is gisomorphism. i span A B!C .
f
d
g
f
B ! C] is called
B ! C for the abstract
⌘
f
g
Proposition 1. (Co-Total/Total Decomposition) Every span A B ! C can be decomposed into a co-total and a total span, i. e. (f, g) = (id, g) (f, id). ⇤
Proof. The pullback of two identities is given by these identities.
M(C) is called a category of partial morphisms over C, if M is a subclass of all monomorphisms of C. Proposition 2. (Initial Object) If M(C) is a category of partial morphisms and C has an initial object I, I is initial in M(C). Proof. The initial morphism from I to an arbitrary object C 2 C in M(C) is the pair (idI , i) where i : I ! C is the initial morphism in C. If there is another morphism (d : X ! I, e : X ! C), then there is j with (i) d j = idI , since I is initial in C. This implies (ii) d j d = idI d = d idX . Since d is monic, (ii) implies (iii) j d = idX . By properties (i) and (ii), d and j are isomorphisms. Again, by I being initial, e j = i. ⇤ Proposition 3. (Factorisation) If x c = t y in a category M(C) of partial morphisms , c is co-total, and t is total, then there is a partial morphism z such that z c = y and t z = x. Proof. Consider Figure 1. The squares (1) and (2) are pullbacks and the squares (3) and (4) commute, since (ix , x) (ic , c) = (it , t) (iy , y). Square (3) is also pullback, since all participating morphisms are monic and f is isomorphism. The morphism f is isomorphism, since diagram (1) is pullback and it is isomorphism by the premise that t is total. Since c is co-total, (c, ic ) is total morphism and (c e, y f ) = y (c, ic ) is the required morphism z. We have z
c=y
(i)
(c, ic ) (ic , c) = y
(ic , ic ) = (iy , y) (ic , ic ) = (ic
(ii)
e, y
f) =
(iy f, y f ) = (iy , y) = y. Property (i) holds, since c is isomorphism, property (ii) holds, since f is isomorphism. 3
iy ic
y f
(3)
e
c
it
(1)
g
d
(2)
t
(4)
ix
x
Figure 1. Factorisation We also have t z = t y (c, ic ) = (iy f, t g) (c, ic ) = (ic e, x d) (c, ic ) = (c e, x d) = (ix d, x d) = (ix , x) = x, since the pullback of (ic e, ic ) is (id, e) and d is isomorphism. ⇤ The following property guarantees that pushouts in C are pushouts in a category of partial morphisms M(C). Definition 4. (M-Hereditary Pushout 3.) A pushout (q 0 , p0 ) of (p, q) in C is Mhereditary if for each commutative cube as in figure 2, which has pullback squares (pi , i0 ) and (qi , i0 ) of (i2 , p) resp. (i1 , q) as back faces such that i1 and i2 are in M, the top square (qi0 , p0i ) is pushout of (pi , qi ) if and only if the front faces (p0i , i1 ) and (qi0 , i2 ) are pullbacks of (i3 , p0 ) resp. (i3 , q 0 ) and i3 is in M. ai
pi bi
di
i2 b
i0
qi'
i3
ci
pi' q
a
p q'
qi
p'
i1 c
d
Figure 2. Pushout/pullback cube The following fact reformulates the sufficient criterion of [13] for a category of partial morphisms to possess not only hereditary but all pushouts. Fact 5. (Pushout of Partial Morphisms) A category of partial morphisms M(C) has all pushouts, if (i) C has all pushouts and all small limits, (ii) pushouts in C are M-hereditary, (iii) for every h : A ! B in C, the pullback functor h⇤ : C #M B ! C #M A has a right adjoint h⇤ : C #M A ! C #M B. 3For details on hereditary pushouts see [9, 10] 4
The theory of single-pushout transformation of graph-like structures is built on a category C and a class M of monomorphisms such that M(C) has all pushouts and, due to Proposition 2, all finite co-limits. Assumption 6. (Base Category) In the following, all constructions are performed in a category M(C) of partial morphisms over a base category C that satisfies the criteria (i), (ii), and (iii) of Fact 5. An example is the category of graphs together with all monomorphisms. Definition 7. (Category of Graphs G) A graph G = (V, E, s : E ! V, t : E ! V ) consists of s set of vertices V , a set of edges E, and two mappings s, t : E ! V , which provide a source resp. target vertex for each edge. A graph morphism f : G1 ! G2 from a graph G1 = (V1 , E1 , s1 , t1 ) to a graph G2 = (V2 , E2 , s2 , t2 ) is a pair (fV : V1 ! V2 , fE : E1 ! E2 ) of mappings such that fV s1 = s2 fE and f V t1 = t 2 f E . Further examples are hyper-graphs or graph structure as in [12] with M the class of all monomorphisms. Definition 8. (Rule, Pre-Match, and Substitution) A rule t : L ! R is a morphism in M(C). A pre-match for t in a host graph G is a morphism m : L ! G 2 C. A substitution t@m of a rule t along a pre-match m is given by the M(C)-pushout (t hmi : G ! t@m, m hti : R ! t@m) of (t, m). The object t@m is the substitution result. The partial morphism t hmi is called the trace, the partial morphism m hti the co-match. Note that the co-match need not be total, i. e. the co-match need not be in C. An example is depicted in Figure 3. L
R
t
1 2
(Pushout) t〈m〉= Ø
1/2
2
===>
===> G
m
=====>
m〈t〉= Ø
H
=====>
Figure 3. Partial Co-Match Definition 9. (Conflict- and Confusion-Free Pre-Match) A pre-match m for rule t is conflict-free 4, if its co-match in t@m is in C. It is confusion-free, if it is conflict-free for every prefix of t, i. e. m hpi 2 C for each p 2 M(C) such that x p = t. Obviously, confusion-free pre-matches induce decompositions of substitutions for each decomposition of a rule: Fact 10. (Substitution at Confusion-Free Pre-Match) If t@m is a substitution of rule t at confusion-free pre-match m and t = t2 M(C) t1 is an arbitrary decomposition of the rule, then t hmi = t2 hm ht1 ii t1 hmi and m hti = m ht1 i ht2 i. 4Single-pushout derivations at conflict-free matches coincide with sesqui-pushout rewritings [1] with monic left-hand sides in rules. 5
In [12], conflict- and confusion-freeness have been characterised for G. Fact 11. (Conflict and Confusion in G) A pre-match m for a rule t = L in the category G of graphs is (i) conflict-free, if and only if
tl
tr
Dt ! R
8x, y 2 L : m(x) = m(y) =) x, y 2 tl (Dt ) _ x, y 2 / tl (Dt ),
and it is (ii) confusion-free, if and only if
5
8x, y 2 L : m(x) = m(y) =) x, y 2 tl (Dt ) _ x = y. Confusion-free pre-matches allow to decompose a rule L following atomic actions:
tl
tr
Dt ! R into the
t (1) Addition: tl = idL , monic tr , and |RV | + |RE | = |DVt | + |DE |+1 t (2) Deletion: tr = idR , monic tl , and |LV | + |LE | = |DVt | + |DE |+1
t (3) Identification6: tl = idL , epic tr , and |ker (trV )|+|ker (trE )| = |DVt |+|DE |+2
Obviously, edge deletion has to precede deletion of the source or target vertex. Symmetrically, vertex addition has to precede edge addition if the source and/or target vertex of the new edge is new itself. And vertex identification comes before edge identification as far as the source and/or target vertex is concerned. But besides these restrictions, the decomposition can be done arbitrarily. A possible order is: edge deletion, vertex deletion, vertex identification, edge identification, vertex addition, and, finally, edge addition. We are also free to start with the additions, do the deletions, and finish with the identifications. And even mixtures of all three action types are possible. Due to these positive properties, it is reasonable to allow only confusion-free prematches in direct derivations: Definition 12. (Match and Direct Derivation) The matches for a rule are its confusion-free pre-matches. Direct derivations are substitutions along matches. The compact notion of direct derivation allows for a straightforward and simple theory of single-pushout rewriting7. We repeat some results8 which are used below when we add inheritance. Definition 13. (Parallel Independence) Direct derivations t1 @m1 and t2 @m2 starting from the same host graph are parallel independent if t2 hm2 i m1 is a match for t1 and t1 hm1 i m2 is a match for t2 . The derivations with rules at independent matches lead to the same trace in any order of application: Fact 14. (Parallel Independence) If direct derivations t1 @m1 and t2 @m2 are parallel independent, then t2 ht1 hm1 i m2 i t1 hm1 i = t1 ht2 hm2 i m1 i t2 hm2 i. 5This property is called d-injective in [12]. 6Here, ker(f ) denotes the equivalence that is induced by a map f : A ! B on its domain A.
The specified difference of 2 admits just one symmetrical pair besides the reflexive ones. 7All necessary proofs can be performed just by using well-known general composition and decomposition results for pushouts. 8For details see [12]. 6
Definition 15. (Sub-rule and Remainder) A rule t : L ! R is an (i, j)-sub-rule of another rule t0 : L0 ! R0 , written t ✓i,j t0 , if i : L ! L0 , j : R ! R0 are two total morphisms9 such that (i) j t = t0 i and (ii) i is a match10 for t. The (i, j)remainder of t0 wrt. t is the universal morphism t0 i,j t : t@i ! R0 that satisfies (a) (t0 i,j t) t hii = t0 and (b) (t0 i,j t) i hti = j. Proposition 16. (Composition of Matches) If t : L ! R ✓i,j t0 : L0 ! R0 and m : L0 ! G is match for t0 , then m i is match for t.
Proof. Let u be a prefix of t, i. e. t = x u for some morphism x. We have to show that m i hui is total, if m is a match for t0 . Due to pushout decomposition properties m i hui = m hu hiii i hui. The morphism i hui is total, since i is a match for t. The morphism m hu hiii is total, since m is a match for t0 and u hii is a prefix of t0 , i. e. t0 = z u hii and z i hui = j for the universal morphism z from the pushout object u@i which makes the diagram commute. ⇤ This proposition together with property (a) of Definition 15 immediately provides the following result: Corollary 17. (Sub-Rule) Direct derivations with sub-rule-structured rules can be decomposed into a derivation with the sub-rule followed by a derivation with the remainder, i. e. if t : L ! R ✓i,j t0 : L0 ! R0 and m : L0 ! G is a match for t0 , then t0 hmi = t0 i,j t hm ht hiiii t hm ii and m ht0 i = m ht hiii ht0 i,j ti. Proof. The direct derivation t0 @m can be decomposed into 2 pushouts, i. e. t hii @m and t0 t@m ht hiii. The morphism m ht hiii is a match for t0 t, since m is a match for t0 and t hii is a prefix of t0 , which implies that x t hii is a prefix of t0 for every prefix x for t0 t. The two pushouts t@i and t hii @m can be composed to a pushout t@ (m i). The morphism m i is a match for t due to Proposition 16. ⇤ Definition 18. (Amalgamation) If t0 : L0 ! R0 is a (i1 , j1 )-sub-rule of t1 : L1 ! R1 and a (i2 , j2 )-sub-rule of t2 : L2 ! R2 , the amalgamation of t1 and t2 along11 t0 is the universal morphism t3 : L3 ! R3 from the pushout (i⇤1 : L2 ! L3 , i⇤2 : L1 ! L3 ) of (i1 , i2 ) to the pushout (j1⇤ : R2 ! R3 , j2⇤ : R1 ! R3 ) of (j1 , j2 ) that satisfies t3 i⇤1 = j1⇤ t2 and t3 i⇤2 = j2⇤ t1 . Lemma 19. If m is match for rule t, then it is match13 for j
t, if j is total12.
Proof. Let y be a prefix of j t, i. e. j t = x y. We decompose y in a total morphism yt and a co-total morphism yc , i. e. y = yt yc , compare Proposition 1. Thus, we have j t = (x yt ) yc . Since yc is co-total and j is total, Proposition 3 provides morphism z with (i) j z = (x yt ) and (ii) z yc = t. If m hyc i is total, then m hyi is total, since y = yt yc , m hyt yc i = m hyc i hyt i, yt is total, and pushouts of total morphisms in the category of partial morphisms are hereditary, compare Assumption 6 and Definition 4. But m hyc i is total since yc is a prefix of t, compare (ii) above, and m is match for t. ⇤ 9I. e. i, j 2 C. 10Remember, that, due to Definition 12, all matches are confusion-free! 11More precisely, along (i , j ) and (i , j ). 12I. e. j 2 C
1
1
2
2
7
Proposition 20. (Induced Matches) The morphisms i⇤1 and i⇤2 constructed in Definition 18 are matches13 for t2 and t1 resp. Proof. Consider Figure 5. L0
t0 R0
j2 R2
i2
j1 R1
t2
i1
L2
i1*
j2*
j1*
L1
t1
i2*
L t3
R
Figure 4. Amalgamated Rule Let t2 = x t02 and (t02 hi⇤1 i , i⇤1 ht02 i) be the pushout of (i⇤1 , t02 ). Then (i) t02 i2 is a prefix of j2 t0 , since x t02 i2 = t2 i2 = j2 t0 . And, since pushouts compose, (ii) (t02 hi⇤1 i , i⇤1 ht02 i i⇤2 ) is the pushout of (t02 i2 , i1 ). (iii) The morphism i2 is match for t0 and, by lemma 19, match for j2 t0 . The properties (i)-(iii) provide that i⇤1 ht02 i is total, q.e.d. The proof that i⇤2 is match for t1 is analogue. Let (t00 hi⇤1 i2 i , i⇤1 i2 ht00 i) be the pushout of t00 and i⇤1 i2 for a prefix t00 of t0 , i. e. t0 = y t00 . This pushout can be decomposed into two pushouts, namely t00 @i2 and t00 hi2 i @i⇤1 . Since i2 is match for t0 , i ht0 i is total. Since j2 t0 = j2 y t00 = t2 i2 there is unique z with z t00 hi2 i = t2 and z i2 ht00 i = j2 y. Thus, i2 ht00 i is a prefix of t2 . Since i⇤1 is match for t2 , i⇤1 ht00 hi2 ii is total. Therefore, i⇤1 i2 ht00 i = i⇤1 ht00 hi2 ii i ht0 i is total. ⇤ The derivation with an amalgamated rule results in the same trace as applying the common sub-rule followed by the two remainders in any order14. This is an immediate consequence of the following fact: Proposition 21. (Amalgamation) Let t1 +t0 t2 be the amalgamation of t1 and t2 along t0 , with the embedding (i1 , j1 ) of t0 to t1 and the embedding (i⌦2 , j2 )↵ of t0 to ⇤ R R t2 . If we set i0 = i⇤1 i2 = i⇤2⌦ i1 ,↵ mR t0 ) m R 1 = i2 ht0 hi1 ii, t1 = (t1 1 , m2 = ⇤ R R i1 ht0 hi2 ii, and t2 = (t2 t0 ) m2 , then we obtain the following two properties: ⌦ ↵ ⌦ ↵ t1 +t0 t2 = (t2 t0 ) tR mR (t1 t0 ) mR t0 hi0 i 1 2 1 ⌦R ↵ ⌦ R↵ R t1 +t0 t2 = (t1 t0 ) t2 m1 (t2 t0 ) m2 t0 hi0 i .
Proof. Direct consequence of Propositions 16 and 20, Lemma 19, and the observation that all quadrangles in Figure 5 are pushouts due to general pushout properties: (a) (i⇤1 , i⇤2 ) is pushout of (i1 , i2 ) by Definition 18. (b1 ) (i1 ht0 i , t0 hi1 i) and (b2 ) (i1 ht0 i , t0 hi1 i) are pushouts of (t0 , i1 ) and (t0 , i2 ) resp. by Definition 15. The pair t0 hi⇤2 i1 i , mR i1 ht0 i represents the derivation (pushout) with rule t0 at 1 match i⇤2 i1 = i⇤1 i2 . This means that t0 hi⇤2 i1 i , mR i2 ht0 i is also the pushout 2 of t0 and i⇤1 i2 . Due to general pushout composition and decomposition properties, 13Confusion-free.
14I. e. the remainders are parallel independent. 8
j1 i1 〈 t0 〉
t1 - t0 t0 〈 i1 〉
t0
t1
i1 i2 〈 t0 〉
i2* 〈 t0 〈 i1 〉〉
i2*
i2
= m1R
i1* j2
t0 〈 i2 〉
m1R 〈 t1 – t0 〉
t0 〈 i*2○i1 〉
i1* 〈 t0 〈 i2 〉〉
t1 – t0 〈 m1R 〉 = t1R
= m2
R
t 2 - t0
t2
t2 – t0 〈 m2R 〉
y
= t2
R
m2R 〈 t2 – t0 〉
x
Figure 5. Decomposition of Amalgamated Rule ⇤ ⇤ R (c1 ) t0 hi⇤2 i1 i , mR 1 is pushout of (i2 , t0 hi1 i), (c2 ) t0 hi2 i1 i , m2 is pushout of ⇤ R R R (i1 , t0 hi2 i), and (d) m1 , m2 is pushout of (i1 ht0 i , i2 ht0 i). (e1 ) tR t0 i 1 , m1 ht1 R R R is the direct derivation with rule t1 t0 at match m1 and (e2 ) t2 , m2 ht2 t0 i is the direct derivation with rule t2 t0 at match mR 2 . Finally, construct the pushout R R (f) (x, y) of tR and t . By composition of (d), (e t0 i 1 ), (e2 ), and (f), y m1 ht1 1 2 R R together with x m2 ht2 t0 i is pushout of (j1 , j2 ). Thus, x t2 t0 hi⇤2 i1 i = y tR t0 hi⇤2 i1 i = t1 +t0 t2 , since all three morphisms make the diagram com1 mute. It remains to check that all used matches are confusion-free: (i) i⇤2 i1 = i⇤1 i2 is R confusion-free for t0 by Propositions 16 and 20. (ii) The morphisms mR 1 and m2 are ⇤ ⇤ confusion-free for t1 t0 and t2 t0 resp., since i2 and i1 are confusion-free for t1 and R R t2 resp. due to Proposition 20. (iii) tR mR mR t0 i 2 1 is total since t2 1 , m2 ht2 is pushout of the total pair (j2 , i1 ht0 i). Let v be a prefix of t1 t0 , i. e. w 15 v = t1 t0 . Then ⌦ R v R ↵i1 ht0 iR is total, since j1 = w v i1 ht0 i is total . Now R R t2 m1 hvi , v t2 m1 m2 ht2 t0 i is pushout of the pair (j2 , v i1 ht0 i) of R R R total morphisms. Thus, tR 2 m1 hvi is total. The proof that t1 m2 is analogue. ⇤
3. The Category of Typed Graphs with Inheritance In this section, we recapitulate definitions and results of [14]. Definition 22. (Type Graph) A type graph T = (GT , ) consists of a graph GT = (V, E, s,Wt) and a partial order ✓ V V ⇥ V on the vertices, which has least upper bounds S and greatest lower bounds S for every subset S ✓ V . ⇤ 15If the composition g
f of two partial morphisms is total, the first morphism, i. e. f is total. 9
Note that the vertex set of a type W V graph cannot be empty, since the least element ; and the greatest element ; must be vertices. Therefore, the simplest type graph consists of a single type vertex and no edges. At first sight and from a practical point of view, it seems strange to require all greatest lower bounds and all least upper bounds. But it is not. For V example any discrete graph canWbe turned into a type graph by just adding ;, an abstraction of all types, and ;, a type that specialises all types, with the induced ordering. Any single-inheritance typeV hierarchy H can be turned into a type graph in ourWsense the same way: Add ; = b Anything if H has more than one root and add ; = b Everything as a type for objects of every shape. And for an arbitrary hierarchy H on types, there is the Dedekind/MacNeillecompletion [15], which provides the smallest partial order closed under least upper and greatest lower bounds containing the original H. In the completion, any added element is a missing bound. And, in the finite case, the bounds in the completion coincide with the original bounds if they existed already in H. If we are given an arbitrary type hierarchy H, which does not satisfy the type graph requirements of Definition 22, we calculate the type graph T (H) by the Dedekind/MacNeille-completion. If, in a concrete derivation sequence, added types, i. e. types in T (H) H, occur, they can be interpreted as follows: Everything almost always indicates an error and all other added types indicate “uncertainty” in the sense that the concrete type in H cannot be computed on the basis of the given information. Definition 23. (Typed Graph) Given a type graph T , a graph G becomes a T typed graph by a typing i : G ! T which is a pair (iV : GV ! TV , iE : GE ! TE ) of mappings such that16 (1)
iV
sG sT
iE
(2)
iV
tG tT
iE
Condition (1) means that subtypes inherit all attributes of all their super-types. Condition (2) formalises the fact that associations may appear polymorphic at runtime in the type of their target. Definition 24. (Type-Compatible Morphism) If i : G ! T and j : H ! T are two typings in the same type graph T , a graph morphism m : G ! H is type-compatible, written m : i ! j, if (3)
jV
mV iV
(4)
jE
mE = iE
A morphism is called strong, if in (3) can be replaced by =, i. e. jV mV = iV . The typings in T together with the type-compatible graph morphisms between them constitute the category of T -typed graphs GT . There is the functor ⌧ : GT ! G which forgets the typing, i. e. maps a GT -morphism m : (i : G ! T ) ! (j : H ! T ) to the G-morphism m : G ! H. 16If f, g : X ! G are two mappings into a partially ordered set G = (G, ), we write f g if f (x) g(x) for all x 2 X. 10
A type-compatible morphism can map an “object” of type c to an “object” the type of which is a subtype of c. If a morphism does not take advantage of this flexibility, it is called strong. Fact 25. (Strong Morphism) (a) Isomorphisms are strong. (b) The composition of two strong morphisms is strong. (c) Strongness is prefix-closed, i. e. if f g is strong, then g is strong. To apply the single pushout transformation framework of Section 4, strong monomorphisms are of special interest: Fact 26. (Strong Monomorphism) A monomorphism in GT is strong, if and only if it is extremal. Proposition 27. (Limits and Co-Limits) For every small diagram : D ! GT , there is a limit17 (lo : L ! (o))o2D and co-limit (co : (o) ! C)o2D , such that ⌧ (lo )o2D and ⌧ (co )o2D are the limit and co-limit of the diagram ⌧ W: D ! G resp. The typings l : ⌧ (L) ! T and cV: ⌧ (C) ! T map x 2 ⌧ (L)V to { (o)(y) : y = lo (x), o 2 D} and x 2 ⌧ (C)V to { (o)(y) : x = co (y), o 2 D} resp.
Proof. By definition, (lo : L ! (o))o2D and (co : (o) ! C)o2D are families of type compatible morphisms. Let (xo : (o) ! x)o2D be a family of type compatible morphisms such that xo0 (m) = xo for every morphism m 2 D. Then ⌧ (xo0 ) ⌧ ( (m)) = ⌧ (xo ) for every morphism m 2 D and we obtain u : ⌧ (C) ! ⌧ (x) with (u ⌧ (co ) = ⌧ (xo ))o2D . We show that u is type compatible and a morphism in GT . Condition (4) of Definition 24 is satisfied per se. For Condition (3), we know by (u ⌧ (co ) = ⌧ (xo ))o2D , that u(z) = u ⌧ (co )(y) = ⌧ (xo )(y) for all y 2 (o) and o 2 D such that ⌧ (co )(y) = V z. Thus, the typing x (u (z)) (o)(y) for all such y. Therefore, x (u (z)) { (o)(y) : z = co (y), o 2 D} = c(z). Uniqueness of u in GT follows from uniqueness of u in G. The proof for the limit construction is analogue. ⇤ Proposition 28. Pushouts and pullbacks preserve strong morphisms in GT , i. e. if (p⇤ , q ⇤ ) is pushout or pullback of (p, q) and p is strong, then p⇤ is strong. ⇤
Proof. Compare [14]. 4. Single Pushout Transformation with Polymorphism
In this section, we instantiate the single-pushout framework of section 2 for the category GT of typed graphs presented in the last section. The span base category is (GT , S) where S is the class of all strong monomorphisms. Thus, S(GT ) is a category of (strong) partial morphisms. (GT , S) satisfies the requirements (1) – (5) on page 2 of a span base, compare section 3. Since GT has all small limits and co-limits18, it remains to show the requirements (ii) and (iii) of Fact 5 for the existence of arbitrary pushouts in S(GT ). For the existence of pushouts of total morphisms in S(GT ), we can prove: Proposition 29. Pushouts in GT are S-hereditary.
17The notation o 2 D stands here and in the following 5 occurrences for o 2 Object . D 18Compare Proposition 27. 11
Proof. Consider figure 2 , where (p0 , q 0 ) is pushout of (q, p), (i0 , pi ) is pullback of (i2 , p), (i0 , qi ) is pullback of (i1 , q), and i1 and i2 – and due to Fact 28 i0 – are strong monomorphisms. Let i3 be strong and monic, (p0i , i1 ) be pullback of (i3 , p0 ) and (i2 , qi0 ) pullback of (i3 , q 0 ). Then, due to properties of the untyped category, (⌧ (qi0 ), ⌧ (p0i )) is pushout of (⌧ (pi ), ⌧ (qi )). We have to show that (qi0 , p0i ) is alsoV pushout in GT . This means, we have to show that for all xi 2 dom(di )V , di (xi ) = {ci (zi ) : p0i (zi ) = xi } [V {bi (yi ) : qi0 (yi ) = xi }. We know, di (xi ) = d(i3 (xi )) and, if we let x = i3 (xi ), d(x) = {c(z) : p0 (z) = x} [ {b(y) : q 0 (y) = x}. Since i1 , i2 , and i3 are monomorphisms, for any y with q 0 (y) = x = i3 (xi ), there is yi with qi0 (yi ) = xi and i2 (yi ) = y and for any z with p0 (z) = x = i3 (xi ), there is zi with p0i (zi ) = xi and i1 (zi ) = z. The morphisms i1 and i2 are strong. Thus, di (xi )
= = =
d(x) ^ {c(z) : p0 (z) = x} [ {b(y) : q 0 (y) = x} ^ {ci (zi ) : p0i (zi ) = xi } [ {bi (yi ) : qi0 (yi ) = xi }
Now let (qi0 , p0i ) be pushout. Due to properties of G, we know that i3 is monomorphism and (⌧ (i1 ), ⌧ (p0i )) as well as (⌧ (i2 ), ⌧ (qi0 )) are pullbacks. They are also pullbacks in G T . We show this property for (p0i , ii ): Since di p0i ci , ci (x) = W 0 {di (pi (x)), c(i1 (x)) = ci (x)}. It remains to show that i3 is strong: ^ di (x) = {ci (zi ) : p0i (zi ) = xi } [ {bi (yi ) : qi0 (yi ) = xi } ^ = {c(z) : p0i (zi ) = xi , i1 (zi ) = z} [ {b(y) : qi0 (yi ) = xi , i2 (yi ) = y} ^ = {c(z) : p0 (z) = i3 (xi )} [ {b(y) : q 0 (y) = i3 (xi )} =
d(i3 (x)
⇤ For the existence of arbitrary pushouts in S(GT ), we additionally need:
Proposition 30. Given a morphism b : (h : H ! T ) ! (k : K ! T ) in GT , the pullback functor b⇤ : GT #S k ! GT #S h has a right-adjoint. Proof. We have to construct a co-free pair (b⇤ (a) : (m : M ! T ) ! k 2 G T #S k, "a : (b⇤ (b⇤ (a)) : (g 0 : G0 ! T ) ! h) ! a 2 G T #S h) for each object a : (g : G ! T ) ! h 2 G T #S h. We take the corresponding pair (b⇤ (⌧ (a)) : M ! K, "a : b⇤ (b⇤ (⌧ (a))) ! ⌧ (a)) in the underlying untyped categories and provide suitable typings. The typing m : M ! T is added by m = k b⇤ (a). Thus, b⇤ (a) and b⇤ (b⇤ (a)) are strong. Also "(a) is strong, since b⇤ (b⇤ (a)) is. If there is d : (n : N ! T ) ! k 2 G T #S k and f : b⇤ (d) ! a 2 G T #S h, we obtain unique f ⇤ : N ! M in the underlying category with b⇤ (a) f ⇤ = d and " p⇤ (f ⇤ ) = f . Since d is strong, f ⇤ is. Uniqueness of f ⇤ follows from uniqueness in the underlying untyped category. ⇤ These results provide the fundament on which a concept of inheritance for singlepushout graph transformation can be built. We are already able to write abstract rules. This is illustrated by the example depicted in Figures 6, 7, and 8. 12
instanceOf
Concrete
extends
Type
Class Mutable
port In
Object
Singleton
InUnique
owner
port Out
target
OutUnique
owner target
instanceOf
Attribute
Link
Figure 6. Type Graph for Object-Oriented Meta-Model Figure 6 depicts a type graph in the sense of Definition 22 for a small meta-model for object-oriented systems. The partial order on vertices is generated by the given inheritance relations in UML-notation19. There are only two typesVmissing for the W vertex order to possess all limits, namely ; (Everything) and ; (Anything). We assume that these types are always implicitly added. The type graph specifies a model level and an instance level connected by the instanceOf-edges. On the model level, there are two specialisations of Type, namely the type Concrete the objects of which can possess instances, i. e. can be target of instanceOf-edges, and the type Mutable the objects of which can be owner of associations, i. e. can be target of port-edges from Out-Objects. The type Class is derived from Concrete and Mutable.20 Singleton-types21 are modelled as a specialisation of Class. The edges of type extends model specialisation. We assume that the set of these edges represents a hierarchy, i. e. includes edges for all paths (reflexive and transitive), and does not contain cyclic paths of length greater zero (anti-symmetric). Attribute-objects connect Out- with In-ports. The specialisations of these port classes, i. e. OutUnique and InUnique, will be used later to model multiplicity specifications for attributes. The instance level is very simple. There are Object-objects which obtain a type (Concrete-object) on the model level via an instanceOf-edge. And there are Linkobjects representing instances of Attribute-objects. These objects can connect Object-objects whose instanceOf-target is a Class-object (owner)22 with Objectobjects whose instanceOf-target is a Concrete-object (target).23 Figure 7 depicts the method CO for the operation createObject(¢:Concrete). The method is abstract because it can be applied to objects of all sub-types of Concrete, namely Concrete, Class, Singleton, and Everything. 19http://www.uml.org/
20These four types correspond to programming language types, e. g. in Java, as follows: Type-
Objects are interfaces, Mutable-Objects are classes without instances (called abstract classes), Class-Objects are classes with instances, and Concrete-Objects are base types like int or boolean that have values on the object level which cannot be owner of Link-objects, i. e. do not change their state over time. 21Compare [4]. 22Note that the owner of a link must be concrete and mutable. 23The owner- and target-relations on the instance level must be consistent with the corresponding relations on the model level. We model this constraint by the link creation rule, compare Figure 8. 13
CO
==>
₡:Concrete
₡:Concrete
2:Object
1:instanceOf
Figure 7. Object Creation The operation createLink(¢:Object,a:Attribute,v:Object) is implemented by the method CL which is depicted in Figure 8. It allows the creation of a link only if the types of the receiver (¢) and the given value (v) are specialisations of the owner- and target-type of the given attribute parameter (a) resp. This method is also abstract, since the object 1:Class for example can be matched with class- or singleton-objects and there are 5 type choices for the object 5:Type24. Without specialisation, we would have to write 90 concrete rules for the type variations of the objects 1, 2, 5, and 6. 2:Mutable
:port
3:Out
:extends
:port
a:Attribute
:instanceOf
5:Type
2:Mutable
CL
:extends
:target :owner
1:Class
4:In
6:Concrete
==>
:instanceOf
₡:Object
:port
3:Out
4:In
:extends :owner
1:Class ₡:Object
6:Concrete
:instanceOf 7:Link
:owner
5:Type
:extends
a:Attribute
:instanceOf
v:Object
:port
:target
:instanceOf
:target
v:Object
Figure 8. Link Creation But abstract methods is not the end of the game. Now, we introduce a mechanism that allows to extend rules. This is equivalent to method redefinition or polymorphism in object-oriented programming. A good example is the object creation rule CO in Figure 7. It does not always work right: The rule can create several instances for a Singleton-type. Figure 9 shows the redefinition of createObject(¢:Concrete) in Figure 7 by a more special method COS , namely createObject(¢:Singleton). The redefinition does not create an Object, if there is already one instance for the Singleton-class25. Note that CO has been made a sub-rule of COS by the morphism pair (i, j). ₡:Concrete
1:instanceOf
2:Object
i 〈 CO 〉
1:instanceOf
3:instanceOf 3:instanceOf
3:instanceOf
3:instanceOf
4:Object 4:Object
j
2:Object 4:Object
4:Object
===========> COS
== COS-CO == > ₡:Singleton
Figure 9. Singleton Creation 24Here, we do not count Everything. 25Note that the redefinition is the identity morphism. 14
=>
CO 〈 i 〉 > == ==
₡:Singleton ₡:Singleton
= == == == ==
₡:Singleton ₡:Singleton
₡:Concrete
==>
======>
i
CO ===========>
1/3:instanceOf
2/4:Object
Figure 9 also shows the application CO@i which provides the remainder COS CO, compare Definition 15. Note, that every direct derivation COs @m with the redefinition coincides with the derivation sequence that applies the sub-rule CO at the match m i followed by the remainder application (COS CO) @m hCO hiii, compare Corollary 17. Thus, the application of a sub-rule-structured rule corresponds to a super-call in object-oriented programming. We can think of the sub-rule as “shared code” that is always executed, in the example “adding an instance”, and the remainder as the set of additional actions specified by the redefinition, in the example “identifying of the new and the old instance”. Before we look at more complex examples, we formalise the presented feature of rule-extension and “application of the most specific rule”. Definition 31. (Polymorphic Graph Transformation System) A polymorphic graph transformation system (T, P, P , MP ) consists of a type graph T, a finite set of partial morphisms P ✓ GT , representing the rules, a partial rule order P ✓ P ⇥ P , representing the specialisation relation on rules, and a family MP of sub-rule specifications26 (lt,t0 : L ! L0 , rt,t0 : R ! R0 ) for every pair t0 : L0 ! R0 P t : L ! R satisfying: Every rule has a unique root rule, i. e. (5)
t3 P t1 ^ t3 P t2 =) 9t : t1 P t ^ t2 P t
The sub-rule specifications are consistent with the sub-rule order, i. e. (6) (7)
(lt,t , rt,t ) = (idL , idR ) for each t : L ! R 2 P (lt,t00 , rt,t00 ) = (lt0 ,t00
lt,t0 , rt0 ,t00
rt,t0 ) for each triple t00 P t0 P t
Note that t0 is a sub-rule of t, if t is a specialisation of t0 , since specialisation on rules means extension27. Condition (5) specifies that each rule is a specialisation of a unique most general rule. The analogy to object orientation is that each method implements a unique operation. From object-orientation, we inherit the idea that the most specific method has to be chosen, if several methods for the same operation are applicable. This concept translates to our approach as follows: Definition 32. (Match and Derivation in Polymorphic System) A match m for rule t in a system P GT = (T, P, P , MP ) is a most specific match, if for all rules t0 , b t 2 P with t0 P b t, t P b t and all matches m0 for t0 we have: m0 lbt,t0 = 0 m lbt,t =) t P t ^ m lt0 t = m0 . A direct derivation in P GT is a direct derivation at a most specific match.28 The specialisation OutUnique of Out-ports stands for the multiplicity 0 . . . 1. Figure 10 depicts an extension CLO of the rule CL in Figure 8 which guarantees the corresponding uniqueness property for outgoing edges. The rule CL is redrawn in Figure 10 in black, the additional parts of CLO CL are drawn in grey: CLO removes an existing link, when a new link is added. 26Compare Definition 15. 27Thus, the partial orders on types and rules are consistent: less than means more special. 28Note that rule redefinition formulates a negative application condition in the sense of [8]: A
rule is not applicable if a more special rule is applicable. 15
Symmetrically, we can extend CL by a rule CLI that removes an existing In-link of an object o, when a new In-link of the same attribute with a InUnique-port is added to o. Now, we have the situation CLO CL CLI and the chance that there is no most specific match, namely if a link shall be added whose Attribute is unique at the In- and the Out-end between two objects that both possess such a link on the owner resp. target side. We need a special rule for this situation. This rule can be automatically generated, namely by the computation of the amalgamated rule CLO +CL CLI , compare Definition 18. Due to Proposition 21, it provides the correct effect. Thus, the automatic addition of all amalgamated rule extensions can help to solve the “method selection problem” in multiple redefinition situations. 2:Mutable
:port
3:OutUnique 4:In
:extends 1:Class
:target :owner a:Attribute
:instanceOf ₡:Object
:port
5:Type
:extends 6:Concrete
CLO
==>
:instanceOf :instanceOf :owner
8:Link
:target
2:Mutable
:port
3:OutUnique 4:In
:extends 1:Class
:target :owner
a:Attribute
:instanceOf
v:Object
₡:Object
:port
5:Type
:extends 6:Concrete
:instanceOf :instanceOf :owner
7:Link
:target
v:Object 9:Object
9:Object
Figure 10. Extension of Link Creation The example demonstrates a maximum of code sharing among rule extensions: CLO and CLI reuse CL and CLO +CL CLI reuses CLO and CLI and, indirectly, also CL. Every extension adds behaviour and does not change behaviour of the more general rules. These properties are guaranteed by Corollary 17 and Proposition 21. Therefore, we obtain a relatively stable system behaviour although we admit (possibly dynamic) specialisation of rules. Thus, the devised concept has a clear compositional semantics and seems promising from the practical point of view. 5. Conclusions In this paper, we extended single-pushout graph transformation by inheritance and polymorphism. The introduced polymorphism is controlled, since it allows to add behaviour by rule extension but forbids changes of behaviour. This extension mechanism qualifies graph transformation as a modelling technique for extendable frameworks. Since non-monic rules are possible, effects of negative application conditions [8] can be modelled, compare for example Figure 9. There are two directions for future theoretical research. After having handled inheritance for the double- and the single-pushout approach in [14] resp. in this paper, the concepts have to be generalised to the sesqui-pushout approach [1]. And, due to the precise compositional semantics, there is a good chance to generalise theoretical results of the algebraic approach, e. g. the critical pair analysis, to polymorphic systems. From the practical point of view, future research has to investigate the gained increase in expressiveness. Besides addition, deletion and identification of objects, the application of a single-pushout rule L l D !r R with inheritance at match m can also specialise the types of objects, namely for those x 2 D for which (i) r is not strong, i. e. R(r(x)) D(x), or (ii) which are identified by r with an item y 6= x such that the types of m(l(x)) and m(l(y)) are different. 16
Another interesting practical issue is the invention of a methodology for the development of message-based object-oriented systems, for example in the sense of [5], starting from arbitrary polymorphic graph transformation systems. References [1] Andrea Corradini, Tobias Heindel, Frank Hermann, and Barbara König. Sesqui-pushout rewriting. In Andrea Corradini, Hartmut Ehrig, Ugo Montanari, Leila Ribeiro, and Grzegorz Rozenberg, editors, ICGT, volume 4178 of Lecture Notes in Computer Science, pages 30–45. Springer, 2006. [2] Hartmut Ehrig, Karsten Ehrig, Ulrike Prange, and Gabriele Taentzer. Fundamentals of Algebraic Graph Transformation. Springer, 2006. [3] Hartmut Ehrig, Michael Pfender, and Hans Jürgen Schneider. Graph-grammars: An algebraic approach. In FOCS, pages 167–180. IEEE, 1973. [4] Erich Gamma et al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1994. [5] Ana Paula Lüdtke Ferreira and Leila Ribeiro. Derivations in object-oriented graph grammars. In Hartmut Ehrig, Gregor Engels, Francesco Parisi-Presicce, and Grzegorz Rozenberg, editors, ICGT, volume 3256 of Lecture Notes in Computer Science, pages 416–430. Springer, 2004. [6] Ulrike Golas, Leen Lambers, Hartmut Ehrig, and Fernando Orejas. Attributed graph transformation with inheritance: Efficient conflict detection and local confluence analysis using abstract critical pairs. Theor. Comput. Sci., 424:46–68, 2012. [7] Esther Guerra and Juan de Lara. Attributed typed triple graph transformation with inheritance in the double pushout approach. Technical Report UC3M-TR-CS-06-01, Technical Report Universidad Carlos III de Madrid, 2006. [8] Annegret Habel, Reiko Heckel, and Gabriele Taentzer. Graph grammars with negative application conditions. Fundam. Inform., 26(3/4):287–313, 1996. [9] Tobias Heindel. Hereditary pushouts reconsidered. In Hartmut Ehrig, Arend Rensink, Grzegorz Rozenberg, and Andy Schürr, editors, ICGT, volume 6372 of Lecture Notes in Computer Science, pages 250–265. Springer, 2010. [10] Richard Kennaway. Graph rewriting in some categories of partial morphisms. In Hartmut Ehrig, Hans-Jörg Kreowski, and Grzegorz Rozenberg, editors, Graph-Grammars and Their Application to Computer Science, volume 532 of Lecture Notes in Computer Science, pages 490–504. Springer, 1990. [11] Felix Klar, Alexander Königs, and Andy Schürr. Model transformation in the large. In Ivica Crnkovic and Antonia Bertolino, editors, ESEC/SIGSOFT FSE, pages 285–294. ACM, 2007. [12] Michael Löwe. Algebraic approach to single-pushout graph transformation. Theor. Comput. Sci., 109(1&2):181–224, 1993. [13] Michael Löwe. A unifying framework for algebraic graph transformation. Technical Report 2012/03, FHDW-Hannover, 2012. [14] Michael Löwe, Harald König, Christoph Schulz, and Marius Schultchen. Algebraic graph transformations with inheritance. In Juliano Iyoda and Leonardo Mendonça de Moura, editors, SBMF, volume 8195 of Lecture Notes in Computer Science, pages 211–226. Springer, 2013. [15] H. M. MacNeille. Partially ordered sets. Trans. Amer. Math. Soc., 42(3):416–460, 1937.
17
ISSN 1863-7043