• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Theory of Computation (Fall 2013): Function Macros & Their Expansion; Composition; Using Composition in Function Computability Proofs
 

Theory of Computation (Fall 2013): Function Macros & Their Expansion; Composition; Using Composition in Function Computability Proofs

on

  • 573 views

 

Statistics

Views

Total Views
573
Views on SlideShare
315
Embed Views
258

Actions

Likes
0
Downloads
0
Comments
0

30 Embeds 258

http://vkedco.blogspot.com 150
http://www.vkedco.blogspot.com 21
http://vkedco.blogspot.fr 16
http://vkedco.blogspot.in 13
http://www.vkedco.blogspot.in 8
http://www.blogger.com 5
http://vkedco.blogspot.it 5
http://reader.aol.com 4
http://vkedco.blogspot.de 3
http://vkedco.blogspot.co.il 3
http://www.vkedco.blogspot.kr 2
http://www.vkedco.blogspot.com.tr 2
http://vkedco.blogspot.sk 2
http://vkedco.blogspot.com.tr 2
http://vkedco.blogspot.pt 2
http://www.vkedco.blogspot.ro 2
http://vkedco.blogspot.nl 2
http://vkedco.blogspot.cz 2
http://vkedco.blogspot.co.uk 2
http://vkedco.blogspot.com.ar 2
http://vkedco.blogspot.ca 1
http://vkedco.blogspot.ru 1
http://vkedco.blogspot.com.br 1
http://vkedco.blogspot.sg 1
http://vkedco.blogspot.kr 1
http://www.vkedco.blogspot.fr 1
http://www.vkedco.blogspot.nl 1
http://www.vkedco.blogspot.it 1
http://www.vkedco.blogspot.se 1
http://www.vkedco.blogspot.de 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Theory of Computation (Fall 2013): Function Macros & Their Expansion; Composition; Using Composition in Function Computability Proofs Theory of Computation (Fall 2013): Function Macros & Their Expansion; Composition; Using Composition in Function Computability Proofs Presentation Transcript

    • Theory of Computation Function Macros & Their Expansion, Composition, Using Composition in Function Computability Proofs Vladimir Kulyukin www.vkedco.blogspot.com
    • Outline Our L Macro Library ● Function Macros & Their Expansion ● Composition ● Using Composition in Function Computability Proofs ●
    • Our L Macro Library
    • Macro GOTO <LABEL> Z1 ← Z1 + 1 IF Z1 != 0 GOTO <LABEL>
    • Macro V ← 0 [A1] V1 ← V1 - 1 IF V1 != 0 GOTO A1
    • 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
    • Macro V1 + V2 [A1] [A2] Y ← V1 Z1 ← V2 IF Z1 != 0 GOTO A2 GOTO E1 Z1 ← Z1 – 1 Y ←Y+1 GOTO A1
    • 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
    • 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
    • Function Macros & Their Expansion
    • 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
    • Function Macros What do we need to do to use macros of the form? W  f V1 ,..., Vn 
    • 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) ●
    • 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
    • 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) ●
    • 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 ●
    • 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
    • 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 mnk  0 Modified Code of Pf Qm [ Em ] W  Z m
    • 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
    • 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
    • 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)
    • 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
    • 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
    • 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
    • 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
    • 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 •
    • Composition
    • 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.
    • 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.
    • Theorem 1.1 (Ch. 3) If h is obtained from the (partially) computable functions f, g1, …, gk by composition, then h is (partially) computable.
    • 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.
    • 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
    • Using Composition in Function Computability Proofs
    • Example 1 Show that h x   2 x is computable
    • 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).
    • Example 2 Show that h x   4 x is computable 2
    • 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).
    • Reading Suggestions ● Chapters 2 & 3, Computability, Complexity, & Languages by Davis, Weyuker, Sigal