Like this presentation? Why not share!

552

-1

-1

Published on

Function Macros & Their Expansion; Composition; Using Composition in Function Computability Proofs

No Downloads

Total Views

552

On Slideshare

0

From Embeds

0

Number of Embeds

37

Shares

0

Downloads

0

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Theory of Computation Function Macros & Their Expansion, Composition, Using Composition in Function Computability Proofs Vladimir Kulyukin www.vkedco.blogspot.com
- 2. Outline Our L Macro Library ● Function Macros & Their Expansion ● Composition ● Using Composition in Function Computability Proofs ●
- 3. Our L Macro Library
- 4. Macro GOTO <LABEL> Z1 ← Z1 + 1 IF Z1 != 0 GOTO <LABEL>
- 5. Macro V ← 0 [A1] V1 ← V1 - 1 IF V1 != 0 GOTO A1
- 6. Macro V1 ← V2 [A1] [A2] [A3] [A4] V1 ← 0 IF V2 != 0 GOTO A2 GOTO A3 V2 ← V2 – 1 V1 ← V1 + 1 Z1 ← Z1 + 1 GOTO A1 IF Z1 != 0 GOTO A4 GOTO E1 Z1 ← Z1 – 1 V2 ← V2 + 1 GOTO A3 Bring V2 down to 0 Bring V1 up to the original value of V2 Bring Z1 up to the original value of V2 Bring Z1 down to 0 Bring V2 back up from 0 to its original value
- 7. Macro V1 + V2 [A1] [A2] Y ← V1 Z1 ← V2 IF Z1 != 0 GOTO A2 GOTO E1 Z1 ← Z1 – 1 Y ←Y+1 GOTO A1
- 8. Macro V1 * V2 [A1] [A2] Z1 ← 0 Z2 ← V2 IF Z2 != 0 GOTO A2 GOTO E1 Z2 ← Z2 – 1 Z1 ← V1 + Y Y ← Z1 GOTO A1
- 9. Macro V1 - V2 [A1] [A2] [A3] Y ← V1 Z1 ← V2 IF Z1 != 0 GOTO A2 GOTO E1 IF Y != 0 GOTO A3 GOTO A2 Y ←Y-1 Z1 ← Z1 - 1 GOTO A1
- 10. Function Macros & Their Expansion
- 11. Function Macros ● ● ● Let f(x) be a partially computable function Let Pf be a program that computes f What assumptions we can make about Pf? Pf has the following variables: Y, X1, …. , Xn, Z1, …, Zk Pf has the following labels: A1, …, Aj The last assumption is true, because we can rename the labels to be only A labels For every dispatch instruction IF Vi != 0 GOTO Aj, Aj exists. No loss of generality here, because we can do a simple syntactic check to verify this condition and refuse to compile a program if it does not meet this condition
- 12. Function Macros What do we need to do to use macros of the form? W f V1 ,..., Vn
- 13. Logical Steps of Macro Expansion A macro is expanded in the context of a larger program PL ● When the macro is expanded, we need to ensure that its instructions are not in conflict with the instructions that come before it (precontext) and the instructions that come after it (post-context) ●
- 14. Context-Sensitive Macro Expansion Pre-Context L Instruction 1 L Instruction 2 PL L Instruction 3 … Post-Context W ← f(V1, …, Vn) … L Instruction r Function Macro to Expand
- 15. Variable & Label Replacements Pf Suppose some function f is computed by a program Pf ● Suppose P is a larger program that has n L input and k local variables and j labels into which f is expanded ● We can always find a natural number m greater than all other subscripts used in PL (variable & label subscripts) ●
- 16. Variable & Label Replacements in Pf When m is found, we replace Y references in P with f Zm ● All input variable references are replaced according to the following mapping (replacement is denoted by →): X1 Zm+1, X2 Zm+2, …, Xn Zm+n ● All local variable references are replaced as follows: Z1 Zm+n+1, Z2 Zm+n+2, …, Zk Zm+n+k ● The end label E1 (or E) is replaced with Em ● All labels are replaced as follows: A1 Am+1, A2 Am+2, … , Am+j ●
- 17. Variable & Label Replacements Pf Let Qm denote Pf ' s source code with all the replacements made as specified. Formally, Qm Pf ( Z m , Z m 1 ,..., Z m n , Z m n 1 ,..., Z m n k , Em , Am 1 ,..., Am j ), where 1. Z m replaces Y; 2. Z m 1 ,..., Z m n replace X 1 ,..., X n ; 3. Z m n 1 ,..., Z m n k replace Z1 ,..., Z k ; 4. Em replaces E1 ; 5. Am 1 ,..., Am j replace A1 ,..., A j
- 18. Macro Expansion Thus, the macro W f V1 ,..., Vn is expanded into : Zm 0 Z m 1 V1 Assign V values into new input variables ... Z m n Vn Z m n 1 0 Assign 0's to new local variables ... Z mnk 0 Modified Code of Pf Qm [ Em ] W Z m
- 19. Example PL 01. 02. 03. 04. 05. 06. Z1 Z1 Z2 Z2 Z3 Y ← Z1 + 1 ← Z1 + 1 ← Z2 + 1 ← Z2 + 1 ← Z1 * Z2 ← Z3 Line 05 is of the form W ← f(V1, V2), where W = Z3, V1 = Z1, V2 = Z2 and f is Multiplication Pf : Macro V1 * V2 01. 02. 03. [A1] 04. 05. [A2] 06. 07. 08. Z1 ← 0 Z2 ← V2 IF Z2 != 0 GOTO A2 GOTO E1 Z2 ← Z2 – 1 Z1 ← V1 + Y Y ← Z1 GOTO A1
- 20. Example • PL has no input variables (n = 0) and no labels (j = 0) • PL has 3 local variables so (k = 3) • Let us set m to max(n, j, k) + 1 = 4
- 21. Example • Pf has the following replacements (→ denotes “replacement”) • Recall that m = 4, n = 2, because Pf has two input variables V1 & V2; j = 2, because Pf has two labels: A1 & A2; and k = 2, because Pf has two local variables: Z1 & Z2 • Y → Z4 • V1 → Z5 (5 = m + 1 = 4) (V1 is the 1st input variable) • V2 → Z6 (6 = m + 2 + 6) (V2 is the 2nd input variable) • Z1 → Z7 (7 = m + n + 1 = 4 + 2 + 1) • Z2 → Z8 (8 = m + n + 2 = 4 + 2 + 2) • E1 → E4 • A1 → A5 (5 = m + 1 = 4 + 1) • A2 → A6 (6 = m + 2 = 4 + 2)
- 22. Example: Assigning V Values to New Input Variables & 0's to New Local Variables Replacements Y V1 V2 Z1 Z2 E1 A1 A2 → → → → → → → → Z4 Z5 Z6 Z7 Z8 E4 A5 A6 Asssignment of Variables in Macro Expansion Z4 ← 0 Z5 ← Z1 Z6 ← Z2 Z7 ← 0 Z8 ← 0 // Z4 is the new Y // 1st parameter V1 // 2nd parameter V2 // Z7 replaces Z1 // Z8 replaces Z2
- 23. Example: Modified Pf Replacements Y V1 V2 Z1 Z2 E1 A1 A2 → → → → → → → → Z4 Z5 Z6 Z7 Z8 E4 A5 A6 Modified Pf after replacements are made: 01. 02. 03. [A5] 04. 05. [A6] 06. 07. 08. Z7 ← 0 Z8 ← Z5 IF Z8 != 0 GOTO A6 GOTO E4 Z8 ← Z8 – 1 Z7 ← Z5 + Z4 Z4 ← Z7 GOTO A5
- 24. 01. Z1 ← Z1 + 1 02. Z1 ← Z1 + 1 03. Z1 ← Z2 + 1 04. Z2 ← Z2 + 1 05. Z4 ← 0 // Z4 is the new Y 06. Z5 ← Z1 // 1st parameter V1 07. Z6 ← Z2 // 2nd parameter V2 08. Z7 ← 0 // Z7 replaces Z1 09. Z8 ← 0 // Z8 replaces Z2 10. Z7 ← 0 11. Z8 ← Z6 12. [A5] IF Z8 != 0 GOTO A6 13. GOTO E4 Example: Modified PL 14. [A6] Z8 ← Z8 – 1 15. Z7 ← Z5 + Z4 16. Z4 ← Z7 17. GOTO A5 18. [E4] Z3 ← Z4 19. Y ← Z3 Black lines are lines from original Pl; red lines are the macro expansion of Pf
- 25. Function Macro Expansion • • • If the macro itself contains other macros, they can be recursively expanded in the same fashion When all the the expansions are done, the program contains only primitive instructions Since this expansion is algorithmic (the macros expands in the same way modulo variables and labels), we can assume that all macros contain only primitive instructions
- 26. Dispatching on Predicates Once we know how to handle W ← f(V1, …, Vn), we can make our conditional dispatches more powerful by dispatching on predicates (functions that return 0 or 1) • Let P(V1, …, Vn) be a predicate • Then IF P(V1, …, Vn) GOTO L expands into: 1. Zk ← P(V1, …, Vn) 2. IF Zk != 0 GOTO L •
- 27. Composition
- 28. Where To Now? ● ● ● ● A scientific theory starts with primitives and proceeds to combine those primitives into increasingly complex structures We will proceed in the same fashion: we will combine computable functions in such a way that the output of one becomes the input of another Such function combination is called composition This is the basis of functional programming: programs return values consumed by other programs that, in turn, return new values consumed by still other programs, etc.
- 29. Definition: Composition Let f be a function of k variables. Let g1 ,..., g k be functions of n variables. Let h x1 ,..., xn f g1 x1 ,..., xn ,..., g k x1 ,..., xn , Then h is obtained from f and g1 ,..., g k by composition.
- 30. Theorem 1.1 (Ch. 3) If h is obtained from the (partially) computable functions f, g1, …, gk by composition, then h is (partially) computable.
- 31. Proof 1.1 (Constructive!) Z1 g1 ( X 1 ,..., X n ) ... Z k g k ( X 1 ,..., X n ) Y f ( Z1 ,..., Z k ) If g1 ,..., g k , f are partially computable, so is h. If g1 ,..., g k , f are computable, so is h.
- 32. Theorem’s Implications ● ● ● This simple theorem has an important implication for software reuse Essentially, it states that incremental software development is possible (provably possible!): we can develop new functions by composing the functions that have already been implemented Software libraries can be developed from other libraries
- 33. Using Composition in Function Computability Proofs
- 34. Example 1 Show that h x 2 x is computable
- 35. Example 1 Proof (Constructive and requires no coding!) : g ( x) x is computable. f ( x, y ) x y is computable. Then h( x) f ( g ( x), g ( x)) x x is computable (by Theorem 1.1).
- 36. Example 2 Show that h x 4 x is computable 2
- 37. Example 2 Proof (Constructive and no coding) : f ( x, y ) x y is computable. g ( x) 2 x is computable. Then h( x) f ( g ( x), g ( x)) 2 x 2 x is computable (by Theorem 1.1).
- 38. Reading Suggestions ● Chapters 2 & 3, Computability, Complexity, & Languages by Davis, Weyuker, Sigal

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment