.

.

......

Inference with Rules (1) .

.. NAKAMURA Masaki .

Toyama Pref. Univ. . December 17, 2013

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 1 / 22 .

.

......

Content

1 Term rewriting systems

.. 2 Termination Semantic methods Syntactic methods Incremental proofs of termination

..

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 2 / 22 ......

Equational reasoning by rewriting

The CafeOBJ reduction command implements equational logic by rewriting and can be used as a powerful interactive theorem proving system { { X + 0 = X (1) X + 0 → X (1) E = R = X + s(Y ) = s(X + Y ) (2) X + s(Y ) → s(X + Y ) (2)

Bidirectional equations are not suitable Equations are regarded as left-to-right for automated equational reasoning rewrite rules

s(0) + s(s(0)) =2+ s(s(0) + s(0)) s(0) + s(s(0)) =?E s(s(0)) + s(0) ↓ ↓ =2+ s(s(s(0) + 0)) 2 2 =1+ s(s(s(0))) s(s(0) + s(0)) s(s(s(0)) + 0) ↓ ↓ =1− s(s(s(0)) + 0) 2 1 → =2− s(s(0)) + s(0) s(s(s(0) + 0)) 1 s(s(s(0)))

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 3 / 22 ......

How to describe equational specifications

Not all equational specifications can work well as rewriting systems

R0 = {a → b, b → a} may result in an infinite loop → → → ··· a R0 b R0 a R0

R1 = {a → b, a → c} cannot prove b = c by rewriting

← → 6→ 6→ b R1 a R1 c, but c R1 a, b R1 a Unfortunately, CafeOBJ system does not find suitable rewrite rules (directions) from input equational specifications. We need a way to describe equational specifications as rewrite systems.

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 4 / 22 ......

Terms

To concentrate to understand a mechanism of rewriting, we first assume simple equational specifications which has only one sort, operators without any attributes (assoc, comm, etc) and unconditional equations Σ : the set of operators

f , g, h,... ∈ Σ

TΣ(X ) (abbr. T ): the set of terms constructed from operators in Σ and variables in the variable set X

X , Y , Z,... ∈ X

f (0), g(c, X ), h(f (0), g(c, X ), Y ),... ∈ TΣ(X )

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 5 / 22 ......

Term rewriting systems

A (Σ-)rewrite rule is defined as a pair (l, r) ∈ TΣ(X ) × TΣ(X ) of terms, denoted by l → r, which satisfies the following variable conditions l 6∈ X , that is, the left-hand side of any rewrite rule is not a variable

X + 0 → X : OK X → X + 0 : NG

V (r) ⊆ V (l), that is, all variables appearing in r should appear in l

X + s(Y ) → s(X + Y ): OK 0 → X ∗ 0 : NG

A pair (Σ, R) of a set Σ of operators and a set R of Σ-rewrite rules is called a term rewriting system (TRS). When we call R a TRS, its Σ is the set of all operators appearing in R.

The CafeOBJ reduction command ignores equations which do not satisfy the first variable conditions, e.g. X → X + 0.

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 6 / 22 ......

Subterms

The set O(t) of positions of a term t is defined as O(x) = {ε} and { } ∪ { ∈ N ∗ | ∈ { } ∈ } O(f (¯tn)) = ε i.p + i n¯ , p O(ti )

Ex. Let t = x + s(y). O(t) = {ε, 1, 2, 2.1}

where ε is the empty string, and ¯tn is abbr. of t1,... tn.

The subterm of a term t at position p ∈ O(t), denoted by t|p, is defined as t|ε = t and f (¯tn)|i.p = ti |p

Ex. t|ε = x + s(y), t|1 = x, t|2 = s(y), t|2.1 = y

0 The subterm ≥sub is defined as follows: t ≥sub t if and only 0 if t = t|p for some p ∈ O(t), and the strict subterm relation >sub is defined as ≥sub \ =.

Ex. x + s(y) >sub s(y) >sub y

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 7 / 22 ......

Substitution

0 0 The replacement of t with t at position p ∈ O(t), denoted by t[t ]p, 0 0 0 0 is defined as t[t ]ε = t and f (¯tn)[t ]i.p = f (..., ti [t ]p,...) Ex. Let t = x + s(y).

t[0 + 0]1 = (0 + 0) + s(y) and t[0 + 0]2.1 = x + s(0 + 0)

A map θ ∈ T X from the set of variables X to a set of terms T is called a substitution if Dom(θ) = {x ∈ X | θ(x) =6 x} is finite.

We write θ = {x0 ← t0, x1 ← t1,..., xn ← tn} when θ(xi ) = ti for all i ∈ Dom(θ) The instance of a term t by θ, denoted by tθ, is defined as xθ = θ(x) and f (¯tn)θ = f (tnθ).

tθ0 = s(0) + s(0) if θ0 = {x ← s(0), y ← 0} tθ1 = s(y) + s(x + z) if θ1 = {x ← s(y), y ← x + z}

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 8 / 22 ......

Rewrite relation

For a TRS R, the rewrite relation →R is defined as follows:

0 X 0 t →l→r t ⇐⇒ ∃θ ∈ T .∃p ∈ O(t). t|p = lθ ∧ t = t[rθ]p 0 0 t →R t ⇐⇒ ∃l → r ∈ R.t →l→r t

Ex. Let R+ = {(1) : X + 0 → X , (2) : X + s(Y ) → s(X + Y )}

s(s(0)) + s(0) →(2) s(s(s(0)) + 0) →(1) s(s(s(0))) →+ → R is the transitive of R (more than zero steps) →∗ → R is the reflexive and of R (zero or more than zero steps). →∗ s(s(0)) + s(0) R+ s(s(s(0))) An instance of l of some l → r ∈ R is called a redex A term t is called an (R-)normal form if there is no u such that t →R u.

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 9 / 22 ......

Equational reasoning

∗ A =R is defined as (→R ∪ ←R ) 0 When we regard R as a set of equations, t =R t means the equation can be deduced from the axioms R. One of the purposes of the TRS 0 is to prove t =R t by rewriting. ↓ 0 →∗ 0 →∗ t R t if and only if there exists u such that t R u and t R u. We call t and t0 joinalble. 0 0 It is trivial that t =R t ⇐ t ↓R t 0 0 The converse t =R t ⇒ t ↓R t is not always true as I showed Termination and confluence properties give us a sufficient condition 0 0 under which t =R t ⇔ t ↓R t

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 10 / 22 ......

Termination and confluece

R is terminating if there is no infinite rewrite sequence t0 →R t1 →R ··· . ↓ 0 ∃ →∗ ∧ →∗ 0 R is confluent if t R t whenever u. u R t u R t Termination guarantees the existence of a normal form of an input term and it can be computable in finite time, and confluence guarantees the uniqueness of normal forms.

If a term t is reduced into normal forms t1 and t2, then there exists u →∗ ←∗ s.t. t1 R u R t2. Since they are normal forms, we have t1 = t2. [Proposition] Let R be a terminating and confluent TRS. Then, 0 0 t =R t ⇔ t ↓R t

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 11 / 22 ......

Proving termination

Termination is undecidable, that is, there is no algorithm to solve the problem: whether R is terminating or not. There are several useful methods and tools to prove termination A basic idea is to find a well-founded ordering > on terms satisfying 0 0 t →R t implies t > t . > is well-founded if there is no infinite decreasing sequence a0 > a1 > ··· , e.g. the strict ordering on natural numbers 0 0 It is not easy to prove t > t for all t →R t since →R may be infinite (even if R is finite)

For R0 = {f (X , Y ) → X }, → → we have f (a, b) R0 a and f (f (a, b), c) R0 f (a, c), and so on

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 12 / 22 ......

Reduction ordering

An irreflexive (t >6 t) and transitive relation is called a strict ordering A relation >⊆ T × T is stable if for all substitution θ and terms t, t0, t > t0 implies tθ > t0θ Let Σ the set of all operators. A relation >⊆ T × T is monotonic if for all f ∈ Σ, ¯sn, ti ∈ T , si > ti implies f (..., si−1, si , si+1,...) > f (..., si−1, ti , si+1,...) A rewrite ordering is a stable and monotic strict ordering A reduction ordering is a well-founded rewrite ordering [Proposition] R is terminating if and only if there exists a reduction ordering > s.t. l > r for all l → r ∈ R Two kinds of methods to obtain a reduction ordering have been considered: semantic methods and syntactic methods

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 13 / 22 ......

Semantic methods

Semantic methods interpret a term into an element of an ordered set (A, >) n A Σ-algebra A consists of a carrier set A and functions fA : A → A for all f ∈ Σ. For an assignment a : X → A, a term t is interpreted as a(t) ∈ A, where a is extended on terms by a(f (¯tn)) = fA(a(tn)) A well-founded monotone Σ-algebra (A, >) is a Σ-algebra A with a well-founded ordering >⊆ A × A where for all f ∈ Σ, ¯an, bi ∈ A, ai > bi implies fA(..., ai−1, ai , ai+1,...) > fA(..., ai−1, bi , ai+1,...). 0 X 0 >A⊆ T × T is defined as t >A t ⇔ ∀a ∈ A .a(t) > a(t ). [Proposition] Let A be a well-founded monotone Σ-algebra. >A is a reduction ordering

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 14 / 22 ......

Proving termination by semantic methods

A Σ-algebra N of natural numbers is often used to prove termination. > on natural numbers is well-founded: ··· > 2 > 1 > 0.

[Example] Let R0 = {f (X , Y ) → X }. When we give fN (x, y) = x − y, it is not monotonic since 10 − 5 >6 10 − 0 even if 5 > 0. When fN (x, y) = x + y, then >N is rewrite ordering but is not reduction ordering since fA(x, 0) = x. fN (x, y) = x + y + 1 works well since fA(x, y) = x + y + 1 > x for all x, y.

[Example] Let R+ = {X + 0 → X , X + s(Y ) → s(X + Y )}. 0N = 1, sN (x) = x + 1 and +N (x, y) = x + 2y + 1. Then, +N (x, 0N ) = +N (x, 1) = x + 2 + 1 > x, and +N (x, sN (y)) = x + 2(y + 1) + 1 = x + 2y + 3, sN (+N (x, y)) = (x + 2y + 1) + 1 = x + 2y + 2

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 15 / 22 ......

Syntactic methods

To define RPO (one of the classical syntactic methods), we prepare some notions here A is a quasi-ordering if it is reflexive and transitive. For a quasi-ordering D, the strict ordering and the equivalent relation of D are defined as a B b ⇔ a D b ∧ b 6 Da, a ∼ b ⇔ a D b ∧ b D a. A multiset is a collection where duplicated elements are allowed, e.g. {|a, a, b|} and {|a, b|} are different. FM(A) is the set of all finite multisets whose elements are of A, e.g. {|0, 2, 2|} ∈ FM(N ). A multiset ordering >mul ⊆ FM(A) × FM(A) w.r.t. a partial > ⊆ A × A is defined as follows: mul M1 > M2 ⇔ ∃X , Y ∈ FM(A).[X =6 ∅ ∧ X ⊆ M1 ∧ M2 = (M1 \ X ) + Y ∧ ∀y ∈ Y .∃x ∈ X .x > y], e.g. {|2, 2, 3|} >mul {|1, 1, 2, 3|} , where X = {|2|} and Y = {|1, 1|}.

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 16 / 22 ......

Recursive path ordering

For a quasi-ordering D ⊆ Σ × Σ. Recursive path ordering (RPO) >rpo⊆ T × T is defined as

f (¯sm) >rpo g(¯tn) ⇐⇒ (1) ∃i ∈ {m¯ }.si ≥rpo g(¯tn), or (2) f B g ∧ ∀j ∈ {n¯}.f (¯sm) >rpo tj , or ∼ ∧ {| |} mul {| |} (3) f g ¯sm >rpo ¯tn [Proposition] Let D be a quasi-ordering. If B is well-founded, then >sub ⊆ >rpo, and >rpo is reduction ordering.

[Example] R0 = {f (X , Y ) → X } is terminating since f (X , Y ) >sub X

[Example] Termination of R+ = {X + 0 → X , X + s(Y ) → s(X + Y )} can be proved by RPO with D s.t. + B s. We have X + 0 >sub X . X + s(Y ) >rpo s(X + Y ) if X + s(Y ) >rpo X + Y from + B s and (2). {| |} mul {| |} X + s(Y ) >rpo X + Y if X , s(Y ) >rpo X , Y from (3). {| |} mul {| |} X , s(Y ) >rpo X , Y since s(Y ) >sub Y .

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 17 / 22 ......

Describing terminating specifications

To obtain a quasi-ordering B for RPO, we introduce a way to describe a specification in a modular way

Describe a module BASIC-M for constructors

Describe a module Mf for a function f ,

which imports BASIC-M, and Mfn if a function f is defined by functions f¯n We give a condition under which termination of the TRS can be proved by RPO where g B f if Mg imports Mf Between modules, we introduce the notion of hierarchical extension In a module, we introduce the notion of argument decreasing rules

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 18 / 22 ......

Hierarchical extension

The root symbol f of the left-hand side of a rewrite rule f (·) → r ∈ R is called a defined symbol. DR is the set of all defined symbols in R. A module [Σ | R] is a pair of Σ and R. Note that R may have operators which do not include in Σ. e.g. M∗ = [{∗} | {X ∗ 0 → 0, X ∗ s(Y ) → X + (X ∗ Y )}].

Let M0 = (Σ0, R0) be a TRS, M1 = [Σ1 | R1]. A pair of M0 and M1 is called a hierarchical extension, denoted by M0 ← M1, if ∩ ∅ ∪ ⊆ (1) Σ0 Σ1 = , (2) (Σ0 Σ1, E1) is a TRS, and (3) DR1 Σ1.

Σ∗ = {∗} Σ∗ = {∗} R∗ = R∗ =  X ∗ 0 → 0  X ∗ 0 → 0 ∗ → ∗ ∗ → ∗  X s(Y ) X + (X Y )  X s(Y ) X + (X Y ) X ∗ (Y + Z) → (X ∗ Y ) + (X ∗ Z) (X ∗ Y ) + (X ∗ Z) → X ∗ (Y + Z) {∗} {∗ } DR∗ = DR∗ = , +

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 19 / 22 ......

Argument decreasing rules

1  ⊆ × 1 ⇔ ∃ The relations R , R Σ Σ are defined as follows: f R g f (··· ) → r ∈ R. ∃g(··· )

Let g ∈ Σ. f (¯tm) → r is g-argument decreasing if for each subterm {|¯ |} mul {| |} g(¯un) of r, tm >sub u¯n . [Proposition] If each f (¯tm) → r ∈ R is g-argument decreasing for each operator g ∼R f , then R is terminating. { { X ∗ 0 → 0 → R∗ = X + 0 X X ∗ s(Y ) → X + (X ∗ Y ) R+ = → X + s(Y ) s(X + Y ) {  fact(0) → s(0)  − → R = X 0 X fact fact(s(X )) → s(X ) ∗ fact(X ) R− = 0 − Y → 0  ∗ − s(X ) − s(Y ) → X − Y where fact R R +, for R = R+ ∪ R− ∪ R∗ ∪ Rfact .

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 20 / 22 ......

Incremental proofs of termination

Proving termination by argument decreasing rules can be applied incrementally for a hierarchical extension Note that in general, termination of two TRSs does not implies termination of the sum of them: RToyama = {f (X , 0, 1) → f (X , X , X )} and RToyama0 = {g(X , Y ) → X , g(X , Y ) → Y }

[Proposition] Let (Σ0, R0) ← [Σ1 | R1]. If ¯ → ∈ ∼ (0) each f (tm) r R0 is g-argument decreasing for each g R0 f , ¯ → ∈ ∼ (1) each f (tm) r R1 is g-argument decreasing for each g R1 f , then each f (¯tm) → r ∈ R0 ∪ R1 is g-argument decreasing for each ∼ g R0∪R1 f . [Example]

(Σ+, R+) ← [Σ∗ | R∗] and (Σ+ ∪ Σ∗, R+ ∪ R∗) ← [Σfact | Rfact ]

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 21 / 22 ......

References

Enno Ohlebusch. Advanced Topics in Term Rewriting. Springer Publishing Company, 2002. Terese. Term Rewriting Systems, volume 55 of Cambridge Tracts in Theoretical Computer Science. Cambridge University Press, 2003. Felix Schernhammer and Jos´eMeseguer. Incremental checking of well-founded recursive specifications modulo axioms. In Peter Schneider-Kamp and Michael Hanus, editors, PPDP, pages 5–16. ACM, 2011. Xavier Urbain. Modular & incremental automated termination proofs. J. Autom. Reasoning, 32(4):315–355, 2004.

NAKAMURA Masaki (Toyama Pref. Univ.) Inference with Rewriting Rules (1) December 17, 2013 22 / 22