* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Test your understanding of matching and equational reasoning.
Survey
Document related concepts
Transcript
Matching and Equational Reasoning 1 Instructions The following quiz contains 10 multiple answer questions. A multiple answer question may have zero or more correct answers. 2 Background In this quiz, the symbol is a binary infix operator that will be used to denote “matching”. A match expression will have the form t s and will have a solution if there exists a substitution σ such that σ(t) = s where = denotes a boolean test for syntactic equality. IMPORTANT NOTE: The substitution σ should only be applied to one side of the match equation. That is, in the match equation t s, sigma should only be applied to t (it should not be applied to s). Suppose we are given the following axiom which states that multiplication can be distributed over addition. Axiom 1 x ∗ (y + z) ≈ x ∗ y + x ∗ z Let us consider the match expression: x ∗ (y + z) 5 ∗ (6 + 7). This match expression has a solution because the substitution σ ≈ [x := 5][y := 6][z := 7] will cause σ(x ∗ (y + z)) to evaluate to 5 ∗ (6 + 7) and 5 ∗ (6 + 7) = 5 ∗ (6 + 7) evaluates to true. Thus when σ ≈ [x := 5][y := 6][z := 7] we get the following: x ∗ (y + z) 5 ∗ (6 + 7) σ(x ∗ (y + z)) 5 ∗ (6 + 7) 5 ∗ (6 + 7) = 5 ∗ (6 + 7) true Given that x ∗ (y + z) 5 ∗ (6 + 7), we can conclude (by the rules of equational reasoning) that 5 ∗ (6 + 7) is (semantically) equal to 5 ∗ 6 + 5 ∗ 7. By shifting the syntax slightly, a mathematical expression like 5∗(6+7) can also be expressed as a term belonging to a term algebra. For example, the multiplication symbol ∗ could be denoted by a binary function symbol mult, and addition symbol + could be denoted by a binary function symbol plus. Given this mapping, the expression 5 ∗ (6 + 7) could be equivalently expressed by the term mult(5, plus(6, 7)). (This is essentially the notation that programmers would use if mult and plus were methods of a class or library.) Matching proceeds in this context just like it would in any other. Specifically, a substitution binds variables to (proper) terms. The term mult(5, plus(6, 7)) has the following (sub)terms: 1 mult(5, plus(6, 7)) 5 plus(6, 7) 6 7 2.1 Match Variables and Constant Identifiers An interesting situation arises when dealing with (mathematical) expressions containing symbols that play the role of mathematical variables. For example, can we use the axiom given above to conclude that 5 ∗ (v + 6) is equal to 5 ∗ v + 5 ∗ 6? The answer is yes. Can we say the same about an equation like 5 ∗ (y + 6)? Notice that this expression contains a symbol y and our axiom also contains a symbol y. Are these two symbols the same? The answer is NO! When using equational reasoning the symbols x, y, and z occurring in axioms are match variables. From the perspective of matching, symbols like x, y, and z in expressions can be thought of as constant identifiers. The difference between a match variable and a constant identifier is that a match variable may be bound to a value during the matching process while a constant identifier may not. Consider the following expression: 5 ∗ (z + 6) Can our axiom be used to conclude that 5 ∗ (z + 6) is equal to 5 ∗ z + 5 ∗ 6? The answer should be yes. The substitution that permits us to draw this conclusion is σ ≈ [x := 5][y := z][z := 6]. However, we now have a nameclash problem. Note that depending on what it means to “apply” σ to a term, we could end up with σ(x ∗ y + x ∗ z) yielding 5∗6+5∗6 which is NOT what we had in mind. The problem is that our matching algorithm is getting match variables confused with constant identifiers. In order to avoid such a confusion, you should always (consistently) rename match variables as needed in order to assure that the name spaces between equations and expressions are distinct. For example, one could rename the matching variables in our axiom as follows: Axiom 2 (Renamed) x1 ∗ (y1 + z1 ) ≈ x1 ∗ y1 + x1 ∗ z1 Renaming can be done as often as you like. The goal is to always keep the name spaces distinct. 2.2 Associativity, Commutativity, and Precedence Interesting situations arise as a result of associativity, commutativity and precedence properties of addition and multiplication. Let us again consider using the Axiom 1 to simplify the expression: 5 ∗ (1 + 2 + 3). In this case, our basic matching algorithm encounters two problems. 2.2.1 A Precedence Problem First, what values should the match variables y and z be bound to (i.e., assigned to)? Up to this point, variables could be bound to integer values or symbolic constants (e.g., b,c,...). However, in this example, we would like to bind variables to mathematical expressions (e.g., 1 + 2, (1 + 2), ...). Our initial attempt at constructing a substitution might be: σ ≈ [x := 5][y := 1 + 2][z := 3] 2 Unfortunately, this doesn’t quite do the job, since the application of this substitution to the right-hand side of the axiom yields: 5∗1+2+5∗3 Here we have a precedence problem. In particular, since the precedence of multiplication is higher than the precedence of addition, the above substitution yields an expression whose evaluation will proceed differently than what we had intended. What we really want is an expression like: 5 ∗ (1 + 2) + 5 ∗ 3 How do we modify our matching algorithm to overcome precedence problems like the one shown above? A safe answer is to encapsulate all values in parenthesis. Thus a substitution would be look something like: σ ≈ [x := (5)][y := (1 + 2)][z := (3)] In this approach, a substitution can be correctly applied and then in a post-processing step one could simplify the resulting expression by removing unnecessary parenthesis. Here a set of parenthesis is unnecessary if removing it does not alter the evaluation order of the expression. Note that if the mathematical expression is mapped to a term, then all these issues go away. So one approach would be to approach a matching problem by first mapping all mathematical expressions to equivalent term. However, an issue is that such mappings are not unique! 5 ∗ (1 + 2 + 3) 5 ∗ (1 + 2 + 3) 2.2.2 map −→ map −→ mult(5, add(add(1, 2), 3)) mult(5, add(1, add(2, 3))) An Associativity Issue Operators like addition and multiplication are associative. Formally, this means the following: x + (y + z) ≈ (x + y) + z x ∗ (y ∗ z) ≈ (x ∗ y) ∗ z This knowledge gives rise to two possible substitutions to the match: x ∗ (y + z) 5 ∗ (1 + 2 + 3) In particular: σ1 ≈ [x := (5)][y := (1 + 2)][z := (3)] σ2 ≈ [x := (5)][y := (1)][z := (2 + 3)] Both of these substitutions are correct. 2.2.3 A Commutativity Issue Operators like addition and multiplication are commutative. Formally, this means the following: x+y ≈y+x x∗y ≈y∗x This knowledge can also be used to influence substitutions. HOWEVER, we will not consider commutativity properties in this quiz. That is, we will assume the operators are not commutative. 3 3 Quiz Problems When solving the problems in this quiz, you may only make use of matching and equational reasoning to manipulate expressions. In particular, making use of basic mathematical operations such as addition (e.g., 1 + 1 ≈ 2) and multiplication (2 ∗ 3 ≈ 6) is not permitted. Problem 1 [Multiple Answer 10 points] Indicate which of the following match expressions can be solved? 1. x 5 2. x 5 + 5 3. x + x 5 + 5 4. x + x 5 + 6 5. x + x (5 + 6) + (5 + 6) Problem 2 [Multiple Answer 10 points] Indicate which of the following match expressions can be solved? 1. x ∗ x (5 + 6) ∗ (6 + 5) 2. x + y 5 + 5 3. x + y 5 + 6 4. x + x (5 + 6) + (5 + (5 + 1)) Problem 3 [Multiple Answer 10 points] Indicate which of the following match expressions can be solved? 1. x ∗ (x + y) (4 + 4) ∗ (z + 5) 2. x ∗ (x + y) (3 + 4) ∗ ((3 + 4) + 5) 3. x + y ∗ (z + x ∗ y) (4 + 2) + 3 ∗ (5 + (4 + 2) ∗ 3) 4. x + y ∗ (z + x ∗ y) (4 + 2) + 3 ∗ (5 + 4 + 2 ∗ 3) 5. x ∗ y ∗ y ∗ x 3 ∗ 4 ∗ 4 ∗ 4 ∗ 4 ∗ 3 4 Problem 4 [Multiple Answer 10 points] Indicate which, if any, substitution σ solves the following match expression. x+y 1+2+3−4 1. σ ≈ [x := 1][y := 2 + 3 − 4] 2. σ ≈ [x := 1 + 2 + 3][y := 4] 3. σ ≈ [x := 1 + 2][y := 3 − 4] 4. none of the above Problem 5 [Multiple Answer 10 points] Assuming that σ ≈ [x := 1][y := plus(2, 1)][z := 1], indicate which of the following statements are true. 1. The application σ(plus(x, times(y, z))) results in plus(1, times(plus(2, 1), 1)). 2. The application σ(plus(x, z)) results in plus(1, 1). 3. The application σ(times(plus(y, x), y)) results in times(plus(2, 1), plus(2, 1)). 4. The application σ(times(y, z)) results in times(3, 1). 5. The application σ(times(y, plus(1, plus(a, x)))) results in times(plus(2, 1), plus(1, plus(a, 1))). Problem 6 [Multiple Answer 10 points] Indicate which of the following match expressions can be solved? 1. plus(x,times(3,x)) plus(1,times(3,plus(3,4))) 2. plus(x,times(3,x)) plus(1,times(1,3)) 3. plus(x,times(x,3)) plus(1,times(1,3)) 4. times(minus(x,5),x) times(minus(plus(1,2),5),plus(1,2)) 5. f(x,y,g(y,z)) f(3,h(5),g(h(5),h(5))) 6. g(z,f(z)) g(f(5),f(f(5))) 5 Problem 7 [Multiple Answer 10 points] Suppose that you are given the following equational theory. E= { sq(x) ≈ times(x, x), cube(x) ≈ times(x, sq(x)), quad(x) ≈ times(sq(x), sq(x)) } Which of the following hold? 1. E ` cube(2) ≈ times(2, times(2, 2)) 2. E ` cube(2) ≈ times(times(2, 2), 2) 3. E ` cube(plus(2, 3)) ≈ times(plus(2, 3), sq(plus(2, 3))) 4. E ` quad(2) ≈ times(times(2, 2), times(2, 2)) 5. E ` quad(2) ≈ times(2, times(2, times(2, 2))) Problem 8 [Multiple Answer 10 points] Suppose that you are given the following equational theory. E= { sq(x) ≈ times(x, x), cube(x) ≈ times(x, sq(x)), quad(x) ≈ times(sq(x), sq(x)), times(sq(x), sq(x)) ≈ times(x, cube(x)) } Which of the following hold? 1. E ` quad(2) ≈ times(2, cube(2)) 2. E ` quad(2) ≈ times(2, times(2, times(2, 2))) Problem 9 [Multiple Answer 10 points] Suppose that you are given the following equational theory. E= { sq(x) ≈ times(x, x), cube(x) ≈ times(x, sq(x)), quad(x) ≈ times(sq(x), sq(x)), times(times(x, y), z) ≈ times(x, times(y, z)) } Which of the following hold? 1. E ` quad(2) ≈ times(2, cube(2)) 2. E ` quad(2) ≈ times(2, times(2, times(2, 2))) 6 Problem 10 [Multiple Answer 10 points] Using equational reasoning and the following axioms indicate which terms are equal to: or(x, x) Axiom 3 or(x, y) ≈ or(y, x) Axiom 4 and(x, y) ≈ and(y, x) Axiom 5 or(x, f alse) ≈ x Axiom 6 and(x, true) ≈ x Axiom 7 or(x, not(x)) ≈ true Axiom 8 and(x, not(x)) ≈ f alse Axiom 9 or(x, and(y, z)) ≈ and(or(x, y), or(x, z)) Axiom 10 and(x, or(y, z)) ≈ or(and(x, y), and(x, z)) 1. or(x, and(x, not(x))) 2. and(or(x, not(x)), true) 3. and(or(x, x), true) 4. x 7