chapter 2
play

Chapter 2: Chapter 2: Programs and Computable Programs and - PowerPoint PPT Presentation

Computer Science Theory (Master Course) Yazd Univ. Chapter 2: Chapter 2: Programs and Computable Programs and Computable Functions Functions M. Farshi A Programming Language Mohammad Farshi Some Examples of Department of Computer


  1. Computer Science Theory (Master Course) Yazd Univ. Chapter 2: Chapter 2: Programs and Computable Programs and Computable Functions Functions M. Farshi A Programming Language Mohammad Farshi Some Examples of Department of Computer Science Programs Yazd University Syntax Computable Functions More about Macros 1395-1 1 / 29

  2. A Programming Language Computability theory is based on a specific programming language L . Yazd Univ. Assumptions and Definitions: Chapter 2: numbers: positive integers. Programs and Computable Functions X i s: input variables M. Farshi Y : output variable A Programming Z i s: local variables Language Some Examples of The subscript 1 is often omitted (i.e. X is X 1 ). Programs The output variable Y and the local variables Z i Syntax initially have the value 0 . Computable Functions A "program" of L will then consist of a list (i.e., a finite More about Macros sequence) of instructions. Each line of a program can have a "label", which comes at the begining of instruction and within [ ] . 2 / 29

  3. A Programming Language List of instructions of L : Yazd Univ. instruction intrepretation V ← V + 1 increment Chapter 2: Programs and V ← V − 1 decrement Computable Functions If V � = 0 GOTO L conditional branch M. Farshi A Programming Language Some Examples of Programs Syntax Computable Functions More about Macros 3 / 29

  4. A Programming Language Some Examples of Programs List of instructions of L : instruction intrepretation Yazd Univ. V ← V + 1 increment V ← V − 1 decrement Chapter 2: Programs and If V � = 0 GOTO L conditional branch Computable Functions M. Farshi Example: A Programming Language [ A ] X ← X − 1 Some Examples of Y ← Y + 1 Programs Syntax IF X � = 0 GOTO A Computable Functions Output of the Program: More about Macros If X � = 0 , the output is x (i.e. Y has the value of X ). If X = 0 , the output is 1 (i.e. Y has the value of 1 ). 4 / 29

  5. A Programming Language Some Examples of Programs List of instructions of L : instruction intrepretation Yazd Univ. V ← V + 1 increment V ← V − 1 decrement Chapter 2: Programs and If V � = 0 GOTO L conditional branch Computable Functions M. Farshi Example: A Programming Language [ A ] X ← X − 1 Some Examples of Y ← Y + 1 Programs Syntax IF X � = 0 GOTO A Computable Functions Output of the Program: More about Macros If X � = 0 , the output is x (i.e. Y has the value of X ). If X = 0 , the output is 1 (i.e. Y has the value of 1 ). 4 / 29

  6. A Programming Language Some Examples of Programs List of instructions of L : instruction intrepretation Yazd Univ. V ← V + 1 increment V ← V − 1 decrement Chapter 2: Programs and If V � = 0 GOTO L conditional branch Computable Functions M. Farshi Example: A Programming Language [ A ] X ← X − 1 Some Examples of Y ← Y + 1 Programs Syntax IF X � = 0 GOTO A Computable Functions Output of the Program: More about Macros If X � = 0 , the output is x (i.e. Y has the value of X ). If X = 0 , the output is 1 (i.e. Y has the value of 1 ). 4 / 29

  7. A Programming Language Some Examples of Programs List of instructions of L : instruction intrepretation Yazd Univ. V ← V + 1 increment V ← V − 1 decrement Chapter 2: Programs and If V � = 0 GOTO L conditional branch Computable Functions M. Farshi Example: A Programming [ A ] X ← X − 1 Language Y ← Y + 1 Some Examples of Programs IF X � = 0 GOTO A Syntax Computable Output of the Program: Functions More about The program computes the function: Macros � 1 If x = 0 f ( x ) = otherwise. x 5 / 29

  8. A Programming Language Some Examples of Programs Example: Yazd Univ. [ A ] X ← X − 1 Y ← Y + 1 Chapter 2: Programs and IF X � = 0 GOTO A Computable Functions M. Farshi Output of the Program: A Programming If one consider the above program to copy the value of X Language to Y , then it has a "bug": it does not work correctly when Some Examples of Programs the input is 0 . Syntax Computable Functions More about Macros 6 / 29

  9. A Programming Language Some Examples of Programs Example: [ A ] X ← X − 1 Yazd Univ. Y ← Y + 1 Chapter 2: IF X � = 0 GOTO A Programs and Computable Functions New Program: M. Farshi [ A ] IF X � = 0 GOTO B A Programming Language Z ← Z + 1 Some Examples of IF Z � = 0 GOTO E Programs [ B ] X ← X − 1 Syntax Y ← Y + 1 Computable Functions Z ← Z + 1 More about IF Z � = 0 GOTO A Macros Note: We use Z because we can not use instructions like GOTO A. 7 / 29

  10. A Programming Language Macro Expansion Macro expansion: Yazd Univ. We can use GOTO A in our programs, but we replace it with Chapter 2: Programs and Z ← Z + 1 Computable Functions IF Z � = 0 GOTO A M. Farshi that satisfies in L . A Programming Later, we will talk about the way we choose variable Language Z distinct to all other local variables. Some Examples of Programs Syntax Computable Functions More about Macros 8 / 29

  11. A Programming Language Some Examples of Programs New Program: Yazd Univ. [ A ] IF X � = 0 GOTO B Z ← Z + 1 Chapter 2: Programs and IF Z � = 0 GOTO E Computable Functions [ B ] X ← X − 1 M. Farshi Y ← Y + 1 A Programming Z ← Z + 1 Language IF Z � = 0 GOTO A Some Examples of Programs Note: The program does copy the value of X into Y but it Syntax destroyes X (i.e. the value of X is 0 at the end). Computable Functions More about Macros 9 / 29

  12. A Programming Language Some Examples of Programs Fixed Program: [ A ] IF X � = 0 GOTO B Yazd Univ. GOTO C Chapter 2: [ B ] X ← X − 1 Programs and Computable Y ← Y + 1 Functions Z ← Z + 1 M. Farshi GOTO A A Programming Language [ C ] IF Z � = 0 GOTO D Some Examples of GOTO E Programs [ D ] Z ← Z − 1 Syntax X ← X + 1 Computable Functions GOTO C More about Macros Note: Later, we will use V ← V ′ with above code as a macro. 10 / 29

  13. A Programming Language Some Examples of Programs Macro for V ← 0 : [ L ] V ← V − 1 Yazd Univ. IF V � = 0 GOTO L Chapter 2: Macro for V ← V ′ : Programs and Computable Functions V ← 0 IF V ′ � = 0 GOTO B M. Farshi [ A ] GOTO C A Programming Language V ′ ← V ′ − 1 [ B ] Some Examples of V ← V + 1 Programs Syntax Z ← Z + 1 Computable GOTO A Functions [ C ] IF Z � = 0 GOTO D More about Macros GOTO E [ D ] Z ← Z − 1 V ′ ← V ′ + 1 GOTO C 11 / 29

  14. A Programming Language Some Examples of Programs Notes: Macro for V ← V ′ : Adding Z ← 0 at the Yazd Univ. V ← 0 beginning of the macro IF V ′ � = 0 GOTO B [ A ] is unnecessary. Chapter 2: Programs and GOTO C Computable Z should be distinct V ′ ← V ′ − 1 Functions [ B ] from all local variables M. Farshi V ← V + 1 of the main program. A Programming Z ← Z + 1 Language Same for Labels in the GOTO A Some Examples of marco. Programs [ C ] IF Z � = 0 GOTO D Label E in the macro Syntax GOTO E Computable should replace by label [ D ] Z ← Z − 1 Functions V ′ ← V ′ + 1 of the instruction after More about Macros the macro in the main GOTO C program. 12 / 29

  15. A Programming Language Some Examples of Programs A Program for A Program for f ( x 1 , x 2 ) = x 1 + x 2 : f ( x 1 , x 2 ) = x 1 × x 2 : Yazd Univ. Y ← X 1 Z 2 ← X 2 Chapter 2: Z ← X 2 [ B ] IF Z 2 � = 0 GOTO A Programs and Computable [ B ] IF Z � = 0 GOTO A GOTO E Functions GOTO E [ A ] Z 2 ← Z 2 − 1 M. Farshi [ A ] Z ← Z − 1 Z 1 ← X 1 + Y A Programming Y ← Y + 1 Y ← Z 1 Language Some Examples of GOTO B GOTO B Programs Syntax Note: Computable We used Z 1 ← X 1 + Y Functions Y ← Z 1 More about Macros instead of Y ← X 1 + Y since in the summation macro all 3 variables should be distinct. If not, the result is not correct. 13 / 29

  16. A Programming Language Some Examples of Programs A Program for A Program for f ( x 1 , x 2 ) = x 1 + x 2 : f ( x 1 , x 2 ) = x 1 × x 2 : Yazd Univ. Y ← X 1 Z 2 ← X 2 Chapter 2: Z ← X 2 [ B ] IF Z 2 � = 0 GOTO A Programs and Computable [ B ] IF Z � = 0 GOTO A GOTO E Functions GOTO E [ A ] Z 2 ← Z 2 − 1 M. Farshi [ A ] Z ← Z − 1 Z 1 ← X 1 + Y A Programming Y ← Y + 1 Y ← Z 1 Language Some Examples of GOTO B GOTO B Programs Syntax Note: Computable We used Z 1 ← X 1 + Y Functions Y ← Z 1 More about Macros instead of Y ← X 1 + Y since in the summation macro all 3 variables should be distinct. If not, the result is not correct. 13 / 29

  17. A Programming Language Some Examples of Programs A Program for A Program for f ( x 1 , x 2 ) = x 1 + x 2 : f ( x 1 , x 2 ) = x 1 × x 2 : Yazd Univ. Y ← X 1 Z 2 ← X 2 Chapter 2: Z ← X 2 [ B ] IF Z 2 � = 0 GOTO A Programs and Computable [ B ] IF Z � = 0 GOTO A GOTO E Functions GOTO E [ A ] Z 2 ← Z 2 − 1 M. Farshi [ A ] Z ← Z − 1 Z 1 ← X 1 + Y A Programming Y ← Y + 1 Y ← Z 1 Language Some Examples of GOTO B GOTO B Programs Syntax Note: Computable We used Z 1 ← X 1 + Y Functions Y ← Z 1 More about Macros instead of Y ← X 1 + Y since in the summation macro all 3 variables should be distinct. If not, the result is not correct. 13 / 29

Recommend


More recommend