Rewriting Systems
Total Page:16
File Type:pdf, Size:1020Kb
Languages and Compilers. Languages and Compilers Continuation of the course in Programming Languages. • New computation paradigms: Course web page ) Marco Comini; • Logic: Prolog These slides available on my web page. • Functional logic: Curry • Formal description of declarative languages behavior: Exam: • functional programming: rewriting systems, lambda calculus, pattern matching • Similar to the exam for Programming Languages: • logic programming: unification, SLD-derivations • three small projects, one at the end of first semester, during the • functional logic programming : narrowing January-February exam period, (one month time window); • Compilers • group projects (1-3 persons), • parsing • attribute grammars • first project: ∼ 2 days for a group of 3 persons. • type checking • intermediate code generation 24 hours on first semester + 48 hours on the second. P. Di Gianantonio (Udine) Rewriting Systems 1 / 82 P. Di Gianantonio (Udine) Rewriting Systems 2 / 82 Rewriting systems Text book: TeReSe-Lite Freely available on line. The course only condisiders the material contained in the following Term rewriting systems sections: are the founding theory for: • 0.0 • functional programming, • 1.0, 1.1, 1.2 • logic programming, • 2.0, 2.1, 2.2, 2.3, 2.7.0, 2.7.1 • functional logic programming, • 3.1, 3.2.0, 3.3.2, • concurrent systems. • 4.1, 4.8.0 Here, n.0 indicates the first part of Chapter n, before Section n.1 starts. When subsections are indicated, as in 2.7.0, it is sufficient to read the material contained in the specified subsections, omitting the other parts of the section. P. Di Gianantonio (Udine) Rewriting Systems 3 / 82 P. Di Gianantonio (Udine) Rewriting Systems 4 / 82 Rewriting as computation Several form of computation can be formulated as rewriting systems: In evaluating an expression, e.g. (3 + 5 + 6) · (1 + 2) • Turing machine; • Post canonical systems; • One moves from one expression to the following one. • lambda calculus, functional programming; • An finite set of rules are applied. • logic programming; • Local rewriting. • ... • Non-determinism – confluence. indeed • Result as termination. • computation proceeds by steps, • each step of computation is local (modify just a local of the system). P. Di Gianantonio (Udine) Rewriting Systems 5 / 82 P. Di Gianantonio (Udine) Rewriting Systems 6 / 82 Abstract reduction system Abstract reduction system one abstracts on the nature of rewritten (reduced) objects. One just considers the graph of reductions. Definition (Abstract reduction system) The shape of the computation graph give information on the nature of computation. A := (A; !) • deterministic programs (non-deterministic); • reversible computing; • A set • termination, non-termination, • ! binary relation on A • confluence. • the set A represents the possible states of the computation; • the relation ! represents the single steps of reduction (rewriting, computation). P. Di Gianantonio (Udine) Rewriting Systems 7 / 82 P. Di Gianantonio (Udine) Rewriting Systems 8 / 82 Labeled reductions ARS, definitions: When a !α b one says that: • a reduces to b in a single step of (α-)reduction (computation), In some areas (e.g, concurrence), one considers a set of reduction • b is a (α-)reduct, in one step, of a , relations. • A := (A; f!αj α 2 Ig) a is an (α-)expansion, in one step, of b. These structures are also called labeled transition systems (LTS) Definition (Reduction sequence) is a, finite or infinite, sequence is the form: Motivations: ! ! ! ! • to distinguish among several kind of reduction rules, a0 a1 ::: an • the reduction generates side-effects that need to be specified. One says that; • a0 reduces to an (in several steps); • an is a reduct (by several steps) of a0. P. Di Gianantonio (Udine) Rewriting Systems 9 / 82 P. Di Gianantonio (Udine) Rewriting Systems 10 / 82 ARS, derived relations Property on ARS: Confluence Different reductions, starting from the same element, can re-converge to a One can look at an ARS as a directed graphs (possibly labeled). common element. Definition !+ is the transitive closure of ! An element a 2 A is called weakly confluent or weakly Church-Rosser a !∗ b iff a reduces to b in n steps with n > 0 (WCR) if as a graph: there exists a non-empty path from a to b, 8b; c. b a ! c we have that: 9d. b !∗ d ∗ c. !∗ is the reflexive and transitive closure of ! for every a, a !∗ a. An element a 2 A is called confluent or Church-Rosser (CR) if 8b; c. b ∗ a !∗ c we have that: 9d. b !∗ d ∗ c. a = b is the symmetric, reflexive and transitive closure of ! implicitly: if a ! b then a and b are taken as equal, An ARS is (weakly) Church-Rosser if every element is (weakly) as a graph: a and b belong to the same connected Church-Rosser. component. CR ensures that different computations can re-converge on a common Notation: The syntactic equality is denoted by ≡. state. WCR is easier to verify. WCR is a strictly weaker property that CR. Counterexample: . P. Di Gianantonio (Udine) Rewriting Systems 11 / 82 P. Di Gianantonio (Udine) Rewriting Systems 12 / 82 CR reduction system: examples an Several notion of confluence counterexamples There exists a plethora of different notions of confluence, in common they Examples: have the structure of the definitions, they differ for the used relations. • evaluation of arithmetic expressions ; Definition • pure functional languages; • • deterministic computation. Diamond property: 8b; c. b a ! c implies 9d. b ! d c, • !α weakly commutes with !β : 8b; c. b α a !β c implies 9d. Counterexamples: ∗ ∗ b !β d α c, • definitions by pattern matching, • ... • logic programming, The most meaningful notions remain CR and WCR. • concurrent systems. P. Di Gianantonio (Udine) Rewriting Systems 13 / 82 P. Di Gianantonio (Udine) Rewriting Systems 14 / 82 ARS properties: termination ARS properties: confluence on normal forms Assumption: the final results of computation are the elements that cannot be further reduced. Definition Definition • an ARS has the normal form property(NF) if ∗ • a is in normal form if @ b:a ! b. 8a; b . b normal form ^ a = b ) a ! b • • a is weakly normalizing(WN) if 9 b in normal form s.t . a !∗ b. an ARS has the unique normal form property(UN) it 8a; b . a; b, normal forms ^ a = b ) a ≡ b • a is strong normalizing(SN) if there is no infinite reduction starting from a. Theorem • an ARS is WN (SN) if every element in it is WN (SN). NF ) UN Show that the reverse implication does not hold. P. Di Gianantonio (Udine) Rewriting Systems 15 / 82 P. Di Gianantonio (Udine) Rewriting Systems 16 / 82 Logical implication among properties Term rewriting system (TRS) ARS defines an abstract notion. TRS special cases of ARS where: Theorem • the elements (computation objects) are first order terms on a given • ^ ) SN WCR CR algebra; • WN ^ UN ) CR • reduction given by a set of rules: • CR ) NF • defined parametrically (using variables and pattern matching) • rules applicable everywhere (in any context). The relation ! is called rewriting (instead of reduction). P. Di Gianantonio (Udine) Rewriting Systems 17 / 82 P. Di Gianantonio (Udine) Rewriting Systems 18 / 82 Motivating example Terms defined by a syntax Natural numbers arithmetic Signature Σ: list of constants and functions organized by arity. Terms generated signature: Example: h0 : 0; S : 1; add; mult : 2i. h0 : 0; S : 1; add; mult : 2i. Constants can be seen as functions with 0 arguments. The syntax may contain also an extra set of variables V. Rewriting rules: Definition • ! add(x; 0) x The set of terms on Σ, V is the set T(Σ; V) generated by the following • add(x; S(y)) ! S(add(x; y)) rules: • mult(x; 0) ! 0 • 8x 2 V x 2 T(Σ; V) • mult(x; S(y)) ! add(mult(x; y); x) • 8f 2 Σ; arity(f) = n; t1;::: tn 2 T(Σ; V) f(t1;::: tn) 2 T(Σ; V) Problems: • What are the (closed) terms in normal form. According to definition a constant c should be written as c(), but we use • Reduce mult(S(S(0)); S(S(0))). the usual writing. • Show that mult(S(S(0)); S(S(0))) e` CR. Only prefix notation, no infix notation. • Define the rewriting rules for subtraction. Exercise. Define the syntax for Boolean algebra. P. Di Gianantonio (Udine) Rewriting Systems 19 / 82 P. Di Gianantonio (Udine) Rewriting Systems 20 / 82 Alternative definition Counting Definition • Ground terms: closed, not containing variables, belonging to T(Σ; ;) The set of terms generated by the grammar: • Linear term: each variable occurs at most once. • Var(t) denotes the set of variable occurring in t: T := x j x j ::: 1 2 t ground , Var(t) = ;. c1() j c2() j ::: • The length of a term t, (denoted by jtj) is the number of functions and u (T) j u (T) j ::: 1 2 variable symbols appearing in t: f (T; T) j f (T; T) j ::: 1 2 • jxj = 1 g1(T; T; T) j g2(T; T; T) j ::: • jf(t1;:::; tn)j = 1 + jt1j + ::: + jtnj The depth of a term t in the maximal number of consecutive application: • depth(x) = 1 • depth(f(t1;:::; tn)) = 1 + max(depth(t1);:::; depth(tn)) P. Di Gianantonio (Udine) Rewriting Systems 21 / 82 P. Di Gianantonio (Udine) Rewriting Systems 22 / 82 Context Term tree Rewriting rules can be applied to subterms, that is terms inside a context. Context: what is remain of a terms having removed a subterm. Formally: It is useful to thought to terms as ordered trees. Definition • variables an constants are the leaves. A context Σ is a term generated by a signature Σ: h[]: 0i, • functions are internal nodes with having as children the arguments. Symbol [], called hole, represent a missing subterm. We just consider in one hole context. Notation.