75
On Data-Structure Rewriting Rachid Echahed LIG Lab, Grenoble France June, 2010

On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

On Data-Structure Rewriting

Rachid EchahedLIG Lab, Grenoble

France

June, 2010

Page 2: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Rewriting (reminder)

I A rewrite relation R is a binary relation

R ⊆ A× A

I (u, v) ∈ R is read “u rewrites into v ” and written

u → v

Page 3: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Rewriting (reminder)

R ⊆ A× A

I A = set of strings over a vocabulary (V ∗)I A = set of states of the form (variables, valuation)I A = set of Turing Machine configurationsI A = set of lambda-termsI A = set of trees (or terms)I A = set of clausesI A = set of process termsI A = . . .

Page 4: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Rewriting

R ⊆ A× A

I How to define a rewrite relation R?I How to define a run or the execution of a program?

I A rewrite derivation : u0 is the initial “call”

u0 → u1 → . . .→ un

I A narrowing derivation :w0 is the initial goal (to solve):

w0 σ1 w1 . . . σn wn

Where

wi σ wi+1 iff σ(wi )→ wi+1

wi is an element of A with partial informationσ instantiates wi

Page 5: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Motivation : Extension of Term Rewriting ; sharingsubterms

Function definitions by means of term rewrite rules

0 + x → xsucc(x) + y → succ(x + y)double(x) → x + x

Very well established domain with several results : Confluence,Termination, Strategies, Proof methods (equational reasoning,induction) etc.

double(x) // +

x

doublet

//+

t

Page 6: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Sharing Subterms (information) and Term Rewriting

Consider the following rules:

f (a,b) → ca → b

Sharing does not preserve properties of tree (term) rewriting !

f (a,a)→ f (a,b)→ c

f a

// f b

6→

[Plump 99] survey on rewriting with “dags”.

Page 7: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Motivation (continued)

I Data-structure rewritingincluding cyclic data-structures with pointerssuch as circular lists, doubly-linked lists, etc.

I Data-structures are more complex than terms (Cycles,Sharing)

I Difficult to encode efficiently using termsI Usually described by pointers (⇒ pointer rewriting)I Formally described as term-graphs

term-graphs = terms with cycles and sharing

Page 8: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Term-graphs[Barendregt et al. 87][Plump 99, survey on acyclic term-graphs]

Let Ω be a set of operation symbols.A term-graph t over Ω is defined by:

I a set of nodes Nt ,I a subset of labeled nodes NΩ

t ⊆ Nt ,I a labeling function Lt : NΩ

t → Ω,I a successor function St : NΩ

t → N∗t ,

1 : f1

~~~~~~~~2

3

2 : b 3 : g

12

4 : • 5 : h

1

gg

Page 9: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Term-graphs

[Barendregt et al. 87][Plump 99, survey on acyclic term-graphs]

Let Ω be a set of operation symbols and F a set of featuresymbols.A term-graph t over Ω and F is defined by:

I a set of nodes Nt ,I a set of edges Et

I a subset of labeled nodes NΩt ⊆ Nt ,

I a node labeling function Lnt : NΩ

t → Ω,I an edge labeling function Le

t : Et → FI a source function St : Et → Nt ,I a target function Tt : Et → Nt ,

Page 10: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

(Term-)Graph Rewriting

I Which graphs? (Term-Graphs)I Which rules?I Which rewrite relation?

Two main approachesI Algorithmic approachesI Algebraic approaches (DPO,SPO, . . .)

Page 11: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Transformation

I Handbook of Graph Grammars and Computing by GraphTransformation (World Scientific)

I Vol 1: Foundations, ed. G. Rozenberg, 1997I Vol 2: Applications, Languages and Tools

eds. H. Ehrig, G. Engels, H.-J. Kreowski and G.Rozenberg, 1999

I Vol 3: Concurrency, Parallelism and Distributioneds. H. Ehrig, H.-J. Kreowski, U. Montanari and G.Rozenberg, 1999

I A Monograph in Theoretical Computer Science (An EATCSseries)H. Ehrig, K. Ehrig, U. Prange, G. Taentzer: Fundamentalsof Algebraic Graph Transformation. Springer-Verlag, 2006

Page 12: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Outline

Introduction

Motivations

Termgraph Rewrite Systems

Confluence and Rewrite Strategies

Narrowing

A Modal Logic for Graph Transformation

Conclusion

Page 13: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Algorithmic approach[Barendregt et al. 87]Shape of a rule:

L→ R

where L and R are rooted term-graphs.A rule can be defined as one graph together with two roots

(L + R, r1, r2)

where r1 and r2 are the roots of L and R respectivelyLet ρ be the rule (L + R, r1, r2)We say that G rewrites to H using the rule ρ if

I L matches a subgraph of G (h : L→ G |n)

I (build phase) Construct graph G1 = G + h(R)

I (redirection phase) G2 = [h(r1) h(r2)]G1

I (garbage collection phase) H = G2 |rootA cumbersome definition, hard to deal with in practice!

Page 14: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Rewrite Rules with actions

Shape of a rewrite rule :

[L | C]→ R

I L is a term-graph patternI C is a node constraint,

∧ni=1(αi 6≈ βi).

I R is a sequence of actions a1; a2; . . . ; an

Page 15: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Actions

We consider three kinds of actions :I Node definition α : f (α1, . . . , αn)

I Edge redirection αi β

I Global redirection α β

Page 16: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Application of actions

a[t ] denotes the application of action(s) a on the term-graph tI Let t = n : f (p,q :a)

n : f1

2

=======

p q :a

I Let t1 = p :h(p)[t ] = n : f (p :h(p),q : a)

n : f1

2

<<<<<<<

p :h

1

KKq :a

Page 17: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Application of actionsa[t ] denotes the application of action(s) a on the term-graph t

I Let t1 = p :h(p)[t ] = n : f (p :h(p),q : a)

n : f1

2

<<<<<<<

p :h

1

KKq :a

I Let t2 = n2 p[t1] = n : f (p :h(p),p); q : a

n : f1

2uup :h

1

KKq :a

Page 18: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Application of actions

a[t ] denotes the application of action(s) a on the term-graph tI Let t2 = n2 p[t1] = n : f (p :h(p),p); q : a

n : f1

2uup :h

1

KKq :a

I Let t3 = p q[t2] = n : f (q,q); p :h(q)

n : f

1 <<<<<<< 2

p :h 1 // q :a

Page 19: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Rewrite Step

Let t be a term-graph

Let ρ be a rewrite rule [L | C]→ R

t rewrite to s at node α, t →α s iff:

I ∃m : L→ t a homomorphism (ρ-matcher)I m(rootL) = α

I α is reachable from roottI m(C) holdsI s = m(R)[t ]

Page 20: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Term-Graph Rewrite Systems (tGRS)–Example–

Length of a circular list :

r : length(p)→ r : length′(p,p)

r : length′(p1 : cons(n,p2),p2)→ r : s(0)

[r : length′(p1 : cons(n,p2),p3) | p2 6≈ p3]→ r : s(q); q :length′(p2,p3)

Remark: term rewrite systems are tGRS’s.

Page 21: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Term-Graph Rewrite Systems–Example–

In-situ list reversal :

o : reverse(p)→ o : rev(p,nil)

o : rev(p1 : cons(n,nil),p2)→ p1 2 p2; o p1

o : rev(p1 : cons(n,p2 : cons(m,p3),p4)→ p1 2 p4; o 1p2; o 2 p1Visual Programming would help!

Page 22: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

DPO approach of rewrite rules with actions

A categorical approach can be found in [TERMGRAPH 06,ENTCS07, RTA07]

L

m

Kloo

d

r // R

m′

G D

l ′oo r ′// H

Figure: Double pushout: a rewrite step (G→ H)

Redirections of edges (pointers) are handled byK = disconnection(L,E ,N) and the morphisms l and r .Remark: Morphisms l and r are not injective! D is not unique!

Page 23: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Confluence

f (x)→ xg(x)→ x

The following term-graph

n : f

q :g

aa

rewrites ton : f

q :g

Page 24: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Confluence

α : f (β : c)→ β : a;α β

α : g(β : c)→ β : b;α β

p : f

<<<<<<<q :g

q :c

The label of node q may end as q : a or q : b

Page 25: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Computing with non-confluentorthogonal Term-graph Rewrite Systems

How to evaluate the following term-graph ?I addlast(length(n : [1,2]),n)I Two normal forms

I [1,2,2] (evaluate addlast after length)I [1,2,3] (evaluate length after addlast)

Page 26: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Term-graphs with Priority

[PPDP06][RTA07][RTA08]I Endow Term-graphs with priorities (G, <G) to express

which node should be evaluated firstI m1 :addlast(m2 : length(n : [1,2]),n); m1 < m2

I Priorities should not be a total order (stay declarative)I Which nodes should be ordered?I Solution: Order only nodes producing a “side-effect”

Page 27: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Strategies and needed nodes

A strategy φ is a partial function which takes a rootedterm-graph t and returns a node (position) n and a rule R,

φ(t) = (n,R)

such that the term-graph t can be reduced at node n using therule R,

t →n t ′

Page 28: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Needed Nodes

Let φ be a rewrite strategy.Let φ(t) = (p,R).The node p is needed iff for all derivations

t →β1 t1 →β2 . . . tn−1 →βn tn

such that tn is a value, there exists i ∈ [1..n] s.t. βi = p

Page 29: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Inductively sequential Term Rewrite Systems

I Constitute a subclass of TRSs for which efficient rewritestrategies are available [Antoy 92]

I Are as expressive as Strongly Sequential TRSsI Are the basis of modern functional and logic programming

languages.I Are defined by means of data-structures called Definitional

trees

Page 30: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Definitional Trees -case of terms-

Let R be the following TRSf(k,nil) → R1f(0,cons(x,l)) → R2f(succ(n),cons(x,l)) → R3

A definitional tree of operator f is a hierarchical structure whoseleaves are the rules defining f .

f(k, l)f(k, nil)→ R1f(k, cons (x, u))

f(0, cons (x,u))→ R2f(succ(y), cons (x,u))→ R3

Page 31: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Definitional trees-case of term-graphs-

r : length′(p1 : nil ,p2 : •)→ rhs1r : length′(p1 : cons(n : •,p2 : •),p2)→ rhs2[r : length′(p1 : cons(n : •,p2 : •),p3 : •) | p2 6

.= p3]→ rhs3

A definitional tree T of the operation length′ is given bellow:

r : length′(p1 : •,p2 : •)r : length′(p1 : nil ,p2 : •)→ rhs1r : length′(p1 : cons(n : •,p3 : •),p2 : •)

r : length′(p1 : cons(n : •,p2 : •),p2)→ rhs2[r : length′(p1 : cons(n : •,p2 : •),p3 : •) | p2 6

.= p3]→ rhs3

Page 32: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

A Rewrite strategy φ

Consider the following definitional tree T of the operation g :

r : g(p1 : •,p2 : •)r : g(p1 : nil ,p2 : •)→ rhs1r : g(p1 : cons(n : •,p3 : •),p2 : •)

r : g(p1 : cons(n : •,p2 : •),p2)→ rhs2[r : g(p1 : cons(n : •,p2 : •),p3 : •) | p2 6

.= p3]→ rhs3

φ(1 : g (2 : g(3 : g(nil ,p),q),4 : g(nil ,o)))= φ(2 : g(3 : g(nil ,p),q))= φ(3 : g(nil ,p))= (3,Rule1)

Page 33: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Naive extension of TRS’s

Contrary to term rewriting, Definitional trees are not enough toensure the neededness of positions computed by the strategyφ, in the context of term-graph rewriting.

Proposition: Let SP = 〈Ω,R〉 be tGRS such that Ω isconstructor-based and the rules of every defined operation arestored in a definitional tree. Let t be a rooted term-graph. Then,

1. if φ(t) = (p,R), the node p is not needed in general.2. if φ(t) is not defined, g can still have a constructor normal

form.

Page 34: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Counter-examples

r : f (p : 0)→ r p r : h(p : 0,q : succ(n : •))→ q pr : f (p : succ(p′ : •))→ r p

Let t = n : succ

r : succ

p : f

q : succ

s : h

__

u : 0

φ(t) = (p, r : f (p : succ(p′ : •))→ r p).However, the node p is not needed in t .

Page 35: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Counter-examples

r : g(p : 0)→ r p r : h(p : 0,q : succ(n : •))→ q p

Let t = n : succ

r : succ

p : g

q : succ

s : h

``

u : 0

φ(t) is not defined!.However, the term-graph t rewrites to n : succ(u : 0).

Page 36: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Inductively Sequential Term-Graph Rewrite Systems

Let SP = 〈Ω,R〉 be a tGRS.SP is called inductively sequential iff

I The rules of every defined operation can be stored in adefinitional tree and

I for all rules [L | C]→ r in R, for all global (respectively,local) redirections of the form p q (respectively, p i qfor some i), occurring in the right-hand side r , p = RootL.

Page 37: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Main Properties of Strategy Φ

In presence of Inductively Sequential Term-Graph RewriteSystems

I The positions computed by Φ are neededI Φ is c-normalizingI Φ is c-hyper-normalizingI Derivations computed by Φ have minimal length

Page 38: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Confluence

Inductively sequential tGRS are not confluent!

f (p : •,p)→ 0[f (p : •,q : •) | p 6= q]→ 1r : g(q : •)→ r q

Let t = n : fzzuuuuu

$$IIIII

p : g // q : 0

There are two different derivations starting from t :

t →n 1t →p f (q : 0,q)→n 0

Page 39: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Admissible term-graphs

[JICSLP98]Ω is contructor-based, i.e. Ω = D ∪ C and D ∩ C = ∅D is a set of defined operationsC is a set of constructors

A term-graph is admissible if none of its cycles includes adefined operation.

n :succ(n) is an admissible term-graphn :+(n,n) and n : tail(n) are not admissible

Page 40: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Admissible term-graphs

The set of admissible term-graphs is not closed under rewriting

n : f (m)→ q :g(n); n m

Let Ω = D ∪ C with C = 0, succ and D = f ,g

n1 : f (m1 :0)→ q1 :g(q1)

Page 41: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Admissible Inductively sequential Term-Graph RewriteSystems

Let SP = 〈Ω,R〉 be an inductively sequential tGRS. SP iscalled admissible iff for all rules [π | C]→ r in R the followingconditions are satisfied

I for all global (respectively, local) redirections of the formp q (respectively, p i q for some i), occurring in theright-hand side r , we have p = Rootπ and q 6= Rootπ.

I for all actions of the form α : f (β1, . . . , βn), for all i ∈ 1..n,βi 6= Rootπ

I the set of actions of the form α : f (β1, . . . , βn), appearing inr , do not construct a cycle including a defined operation.

I Constraint C includes disequations of the form p 6 .= qwhere p and q are labeled by constructor symbols.

Page 42: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Admissible Inductively sequential Term-Graph RewriteSystems

[ICGT08][JICSLP98]In presence of Admissible Inductively sequential Term-GraphRewrite Systems

I The set of admissible term-graphs is closed under therewrite relation defined by admissible rules.

I Φ computes needed positionsI Admissible term-graphs admit unique normal forms

Page 43: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Narrowing

wi σ wi+1 iff σ(wi)→ wi+1

I Rewriting = Matching + TransformationI Narrowing = Unification + Transformation

Page 44: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Narrowing–Motivation–

I Automated deduction [Slagle 74] [Fay 79][Hullot 80]I Functional and Logic Programming [Goguen and

Meseguer 84, ...]I Security verification [Meadows 89, ...]I Reachability Analysis [Meseguer and Thati 05, ...]I ...

Page 45: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Narrowing

Instantiate goal variables and apply a reduction step

0 + X → Xs(X ) + Y → s(X + Y )

U + s(0) = s(s(0)) U 7→s(V ) s(V + s(0)) = s(s(0)) V 7→0 s(s(0)) = s(s(0))

Computed answer: U 7→ s(0)

Page 46: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Some Results

Needed Term narrowing [POPL04][JACM2000](main operational semantics of current functionalogicprogramming languages)Needed Graph Narrowing [JICSLP98]Needed Collapsing Narrowing [Gratra 2000]Narrowing-based algorithm for data-structure rewriting[ICGT06]

I Goalo : equal(p : length(q), s(s(0))) = true

I Solution : a circular list of length two[q : cons(n1, r : cons(n2,q)) | q 6≈ r ]

Page 47: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Narrowing: What do we transform?

Rule

o : f (p : a,q, r) −→ p : b; o 3 q

Rewrite Stepso1,p1,q1 and r1 are constants (names or addresses)

o1 : f (p1 : a,q1 : a, r1) −→ o1 : f (p1 : b,q1 : a,q1)

o1 : f (p1 : a,p1, r1) −→ o1 : f (p1 : b,p1,p1)

Page 48: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Narrowing: What do we transform?

Rule

o : f (p : a,q, r) −→ p : b; o 3 q

Rewrite Steps (o1,p1,q1 and r1 are constants)o1 : f (p1 : a,q1 : a, r1) −→ o1 : f (p1 : b,q1 : a,q1)o1 : f (p1 : a,p1, r1) −→ o1 : f (p1 : b,p1,p1)

Narrowing steps (o2,p2,q2 and r2 are variables)

o2 : f (p2,q2 : a, r2) ?

σ labels node p2 with symbol a.o2 : f (p2,q2 : a, r2) σ o2 : f (p2 : b,q2 : a,q2) | p2 6≈ q2o2 : f (p2,q2 : a, r2) σ∪q2 7→p2 o2 : f (p2 : b,p2,p2)

Page 49: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Narrowing: What do we transform?

Rule

o : f (p : a,q, r) −→ p : b; o 3 q

Narrowing steps(o2,p2,q2 and r2 are variables)

o2 : f (p2,q2 : a, r2) σ [apply(o2 : f (p2 : a,q2 : a, r2),p2 : b; o2 3 q2)] [apply(o2 : f (p2 : a,q2 : a, r2),p2 : b; o2 3 q2) | p2 6≈ q2] [apply(o2 : f (p2 : b,q2 : a, r2),o2 3 q2) | p2 6≈ q2] [apply(o2 : f (p2 : a,q2 : a,q2), ε) | p2 6≈ q2] [o2 : f (p2 : a,q2 : a,q2) | p2 6≈ q2]

Page 50: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Narrowing: What do we transform?

Rule

o : f (p : a,q, r) −→ p : b; o 3 q

Narrowing stepso2,p2,q2 and r2 are variables

o2 : f (p2,q2 : a, r2) σ [apply(o2 : f (p2 : a,q2 : a, r2),p2 : b; o2 3 q2)] q2 7→p2 [apply(o2 : f (p2 : a,p2, r2),p2 : b; o2 3 q2)] [apply(o2 : f (p2 : b,p2, r2),o2 3 q2)] [apply(o2 : f (p2 : b,p2,p2), ε)] o2 : f (p2 : b,p2,p2)

Page 51: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

g-terms

Symbolic handling of actionsG is a term-graphφ is a conjunction of disequationsτ is a sequence of actions

[G | φ]

[apply(G, τ) | φ]

Example:[o2 : f (p2 : a,q2 : a,q2) | p2 6≈ q2]

[apply(o2 : f (p2 : b,q2 : a, r2),o2 3 q2) | p2 6≈ q2]

Page 52: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Narrowing Rules

Superposition rule (SUP)

[G | ψ]τ SUP,ρ,θ [H | ψ′]σ(τ)

If:I G is a term-graphI ρ is rewrite rule [L | φ]→ RI σ is a most general unifier of L and G such that:

I σ(L) and σ(G) are compatibleI The root of L unifies with a labeled node in G

(non-variable unification)I H = apply(σ(G) ∪ σ(L), σ(R))

I θ = (σ,K ) with K = σ(L)\σ(G)

I ψ′ = σ(ψ) ∧ σ(φ) ∧∧

p∈affected by(σ(τ)),q∈K Ω p 6 .= q.

Page 53: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Narrowing Rules

Action rule: simplify (SIM)

[apply(G, ε) | ψ]τ SIM [G | ψ]τ

Page 54: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Narrowing Rules

Action rule: execute (EXE)

[apply(G, α.u) | ψ]τ EXE [apply(α[G],u) | ψ′]τ.α

If:I action α is not a node creation andI [G | ψ] is ready for action α.

Page 55: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Narrowing Rules

Action rule: new node (NEW)

[apply(G,n+.u) | ψ]τ NEW ,σ [apply(n+[G], σ(u)) | ψ′]τ.n+

If:I σ = n 7→ n′ where n′ is a fresh effective nodeI ψ′ = ψ ∧

∧p∈V∩NG

(p 6≈ n′)

Page 56: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Narrowing Rules

Isolation rule with equality (EQU)

[apply(G, α.u) | ψ]τ EQU,σ [apply(σ(G), σ(α).σ(u)) | σ(ψ)]σ(τ)

If:I there exists a node n ∈ affected by(α),I m is not an α-isolated node in G andI σ is a substitution (compatible with G) such thatσ(n) = σ(m)

o2 : f (p2,q2 : a, r2) σ [apply(o2 : f (p2 : a,q2 : a, r2),p2 : b; o2 3 q2)] q2 7→p2 [apply(o2 : f (p2 : a,p2, r2),p2 : b; o2 3 q2)]

Page 57: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Narrowing Rules

Isolation rule with disequality (DIS)

[apply(G, α.u) | ψ]τ DIS [apply(G, α.u) | ψ ∧ n 6 .= m]τ

If:I there exists a node n ∈ affected by(α),I m is not an α-isolated node in G

o2 : f (p2,q2 : a, r2) σ [apply(o2 : f (p2 : a,q2 : a, r2),p2 : b; o2 3 q2)] [apply(o2 : f (p2 : a,q2 : a, r2),p2 : b; o2 3 q2) | p2 6≈ q2]

Page 58: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Computed Solutions

[G0 | True] σ1 · · · σn [Gn | φ]

Computed Solution is : (σ1 · · ·σn, φ)

Example:

I Goalo : equal(p : length(q), s(s(0))) = true

I Solution[q : cons(n1, r : cons(n2,q)) | q 6≈ r ]

Page 59: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Graph Narrowing: Soundness and Completeness

Proposition 1: The proposed narrowing rules are sound.If [G | True] σ [H | φ]then there exists a ground substitution θ satisfying φ such that:Gσθ −→∗ Hθ

Proposition 2: The proposed narrowing rules are complete.If Gσ −→∗ H, σ being irreducible.Then, there exist two substitutions θ and γ and a term-graph G′

such that :

I [G | True] ∗θ[G′ | φ]

I γ satifies φI σ = θγ

I G′γ = H

Page 60: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modal Logic and Graph Transformation– motivations–

I Specify graph shapes (data-structures)I Circular listI Balanced tree

Graph properties can be specified within several logics,such as :

I Separation Logic,I Monadic second order logic,I Modal logics (e.g., LTL, CTL, µ-calculus, etc).

I Verification of graph transformation:I InvariantI Reachability

I Need to define new logics able to specify rule applicationand graph transformation.

Page 61: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Dynamic Logic

I AgentsI KnowledgeI Actions

Evaluate a formula in a model⇒ Transform the consideredmodel

Page 62: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

A Modal Logic for Graph Rewriting

I G |= φ

I G!R |= φ where G!R is the normal form of GI G!R |= φ iff G |= [R∗]φ

Page 63: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

A Modal Logic for Graph Rewriting : Lgr

LanguageI Formulas :

φ ::= p |⊥| ¬φ | φ ∨ φ | [α]φ

I Actions :

α ::= a | α∗ | α;α | α ∨ α | modifiers.

[α]Φ :“After performing” actions α, formula Φ holds.

Page 64: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modifiers

I Add a new nodeI Add a new label (to current node)I remove a label from the current nodeI Add the label “a” to the edges going from a Φ-node to a

Ψ-node.I . . .

I Graph modifiers :I UI nI ~nI φ?I (ω :=g φ)I (ω :=l φ)I (a + (φ, ψ))I (a− (φ, ψ))

Page 65: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modifiers–Example–

[p :=g⊥][p :=l >](p ∧ [a](¬p ∧ q))

p

a

a // p,q

q

⇒ •

a

a // q

q

⇒ p

a

a // q

q

Page 66: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modal Logic: Lgr

Semantics (informally)

I Gr |= piff p holds at node r

I Gr |= [a]ϕ

iff Gn |= ϕ for all nodes n such that the edge r a→ n ∈ G.

I Gr |= [ω :=g ⊥][ω :=l >] ϕiff H r |= ϕ, H is obtained from G by tagging the node r by ω(ω does not hold outside node r ).

Page 67: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modal Logic: Lgr

Semantics

I Gr |= [a− (φ, ψ)]ϕiff H r |= ϕ, H is obtained from G by erasing the edgesn a→ m, such that Gn |= φ and Gm |= ψ.

I Gr |= [a + (φ, ψ)]ϕiff H r |= ϕ, H is obtained from G by adding the edgesn a→ m, such that Gn |= φ and Gm |= ψ.

I Gr |= [f ?]ϕiff Gr |= ϕ and f holds at node r .

I Gr |= [ ~nw ]ϕiff Hnw |= ϕ. H is obtained from G by adding a new nodenw .

Page 68: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Examples of Lgr Specified Properties

I Class of all a-cycle-free rooted termgraphs.[ω :=g >][U][ω :=l ⊥][a+]ω

I Class of all a-circular rooted termgraphs[ω :=g ⊥][U][ω :=l >]〈a+〉ω.

I Class of all (a,b)-binary rooted termgraphs[ω :=g ⊥][U][ω :=l >][a][π :=g >][(a ∪ b)?][π :=l⊥][U](ω → [b][(a ∪ b)?]π).

I Let RG(a) = (n1,n2): the edge n1a→ n2 ∈ G.

G |= [ω :=g ⊥][U][ω :=l >][a]¬ω iff RG(a) is irreflexive.I Classes of circular lists, balanced trees, . . .

Page 69: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Hamiltonian Graphs

The following formula expresses the existence of a Hamiltoniancycle.α stands for a1 ∪ . . . ∪ an, where the ai ’s are the possiblefeatures used in the graph (F = a1, . . . ,an):

〈ω :=g >;π :=g ⊥;ω :=l ⊥;π :=l >; (α;ω?;ω :=l ⊥)?〉(π ∧ [U]¬ω).

Page 70: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Decidability

I With * and without “nw” : the problem of “model checking”(G |= Φ) is decidable.

Page 71: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modal Logic Lgr and Graph Rewriting

Expressing pattern-matching in LgrProposition: Let Gr be a term-graph with root r (adistinguished node). There exists a ?-free action αG and a?-free formula φG such that for all finite rooted termgraphs G′r

′,

G′r′ |= 〈αG〉φG iff there exists a graph homomorphism from G to

G′r′.

We define the action αG and the formula φG as follows:I βG = (π0 :=g ⊥); . . . ; (πN−1 :=g ⊥),

(N being the number of nodes in G)I for all non-negative integers i , if i < N then γ i

G =(¬π0 ∧ . . . ∧ ¬πi−1)?; (πi :=l >); U,

I αG = βG; γ0G; . . . ; γN−1

G ,

Page 72: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modal Logic Lgr and Graph Rewriting

We define the formula φG as follows:I for all non-negative integers i , if i < N then ψi

G = if Ln(i) isdefined then 〈U〉(πi ∧ Ln(i)) else >,

I for all non-negative integers i , j , if i , j < N then χi,jG = if

there exists an edge e ∈ E such that S(e) = i and T (e) = jthen 〈U〉(πi ∧ 〈Le(e)〉πj) else >,

I φG = ψ0G ∧ . . . ∧ ψ

N−1G ∧ χ0,0

G ∧ . . . ∧ χN−1,N−1G .

Page 73: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modal Logic Lgr and Graph Rewriting

Actions representing the right-hand sides can be expressed bythe following elementary formulas :

I Action n : f (a1 ⇒ n1, . . . ,ak ⇒ nk )U;πn?; (f :=l >); (a1 + (πn, πn1)); . . . ; (ak + (πn, πnk )).

I Action na m(a− (πn,>)); (a + (πn, πm)).

I Action n m) (for a-edges)(λa :=g ⊥); (λa :=g 〈a〉πn); (a− (>, πn)); (a + (λa, πm)).

Page 74: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Modal Logic Lgr and Graph Rewriting

I Firing a rule ρ = L→ R

βρ = αL;αR

I Normal form of graph Gr satisfies ϕ: Let R = (∨βρi )

Gr |= [R∗]([R]⊥ ⇒ ϕ)

I Rule ρ preserves the property ϕ:

|= (ϕ⇒ [βρ]ϕ)

Page 75: On Data-Structure Rewritinggpd.sip.ucm.es/charlas/20100623RachidEchahed/rachedSld.pdf · Motivation (continued) I Data-structure rewriting includingcyclicdata-structures withpointers

Conclussion and perspectives

I Admissible termgraphs seem to be a good trade-off toensure confluence and efficient strategies

I Cloning and algebraic approaches (sesqui-pushout)I NarrowingI Visual Programming and Termgraph RewritingI Proof TechniquesI Applications