Imperative programming

Download Report

Transcript Imperative programming

Imperative programming
Name
Binding
Variable
Data Type
Type checking
Type equivalence
Von Neumann Architecture
Introduction
 We use imperative languages, at least in part, because we
use von Neumann machines
 Data and programs stored in same memory
 Memory is separate from CPU
 Instructions and data are piped from memory to CPU
 Basis for imperative languages
 Variables model memory cells
 Assignment statements model piping
 Iteration is efficient
Name
ั entity ในโปรแกรม
 String ที่ใช้ในการกาหนดชื่อให้กบ
เพื่อเก็บข้อมูลและเรี ยกใช้ขอ้ มูลที่ตอ้ งการ
 ชื่อตัวแปร
 ชื่อโปรแกรมย่อย
 ชื่อค่าคงที่
ฯลฯ
 หรื ออาจเรี ยกว่า identifiers
Name
 กฎเกณฑ์ในการตั้งชื่อ
 Maximum length
 Special character
 Case sensitivity
 Special words
Length
 ถ้ าสันไป
้ ก็ไม่สื่อความหมาย
 ตัวอย่าง
 FORTRAN I: maximum 6
 COBOL: maximum 30
 FORTRAN 90 and ANSI C: maximum 31
 Ada and Java: no limit, and all are significant
 C++: no limit, but implementers often impose one
Special character
 หลายภาษาอนุญาตให้ ใช้ สญ
ั ลักษณ์พิเศษได้
 อาจใช้ ได้ เพียงสัญลักษณ์เดียว หรื อหลายสัญลักษณ์
 บางภาษาอาจมีข้อกาหนดในการใช้
 ตัวอย่าง
 COBOL: ใช้
hyphen (-) ได้ แต่ห้ามใช้ นาหน้ าหรื อท้ ายชื่อ
 C-like : ใช้ underscore (_) ได้ โดยไม่มีข้อบังคับใดๆ
Case sensitivity
 ตัวอักษรตัวเล็ก ตัวใหญ่ตา่ งกัน
 ข้ อเสีย
ชื่อที่เขียนคล้ ายกัน แต่ไม่ใช่ชื่อเดียว (poor readability)
 ในภาษา C++ และ Java ทาให้ ใช้ งานยาก เพราะชื่อ predefined
names เป็ นแบบผสมตัวเล็กตัวใหญ่
 e.g. IndexOutOfBoundsException
 ชื่อในภาษา C, C++ และ Java เป็ น case sensitive

Special words
ภาษาส่วนมากจะมีกลุม่ ชื่อที่เป็ น predifined names
 keywords หรื อ reserved words
 มีความหมายเฉพาะ ไม่สามารถนามาใช้ เป็ น identified ได้

Keyword : a word that is special only in certain contexts
ตัวอย่าง FORTRAN
REAL APPLE -> ประกาศตัวแปรประเภทจานวนจริ ง
REAL = 3.4
-> กาหนดค่าให้ตวั แปรชื่อ REAL
REAL INTEGER
INTEGER REAL
 Reserved word : a special word that cannot be used as a user-defined
name เช่น int, if, while ในภาษา C

Binding
Binding : an association, such as between an attribute and an entity,
or between an operation and a symbol.
เป็ นการเชื่อม (bind) ชื่อที่กาหนดและคุณสมบัติของชื่อ (attribute) เข้าด้วยกัน
 Binding time : the time at which a binding takes places.

 Language design time - bind operator symbol กับ operation
 Language implementation time - bind fl. pt. type กับ representation
 Compile time - bind variable กับ type
 Load time - bind a variable กับ memory cell
 Runtime - bind a nonstatic local variable กับ memory cell
Variable
 Variable : an abstraction of a memory cell
 มีคุณสมบัติ (attribute) 6
ประการ คือ name, address, value,
type, lifetime,scope
 Name
 Address : memory address ที่เก็บค่าของตัวแปร
 ตัวแปรอาจมี address ที่ต่างกันในเวลาที่ต่างกันขณะ execution
่ นละที่ในโปรแกรม
 ตัวแปรอาจมี address ที่ต่างกันเมื่อปรากฏอยูค
 ถ้าตัวแปรสองตัวสามารถใช้ในการเข้าถึงตาแหน่ งใน memory ตาแหน่ง
เดียวกัน จะเรี ยกว่า Alias
Variable (ต่ อ)
 Address (ต่อ): อาจเรี ยกว่า
l-value
 Algol68:




x = y + 1;
x : left-hand side value, l-value, its address
y : right-hand side value, r-value, its name
ML:
C/C++:
Explicit dereferencing
x := !y + 1;
int x, y, *p; x = *p; *p = y;
Variable (ต่ อ)
 Value : ค่าที่เก็บอยูใ่ น address แบ่งเป็ น
 static variable
 เมื่อถูกกาหนดแล้ว จะทาให้รู้ขนาดโครงสร้างที่แน่นอนของตัวแปร
 Binding เกิดขึ ้นก่อนช่วงเวลา run-time
 dynamic variable
 ขอเนื้ อที่หน่วยความจาเมื่อต้องการใช้งานเท่านั้น
่ องหน่วยความจาซึ่ งเก็บ
 ค่าที่บรรจุในตัวแปรจะเป็ นตาแหน่ งที่อยูข
ข้อมูลที่ตอ้ งการ
 Binding เกิดขึ ้นในช่วงเวลา run-time
Variable (ต่ อ)
type
var
student_ptr = ^student_rec;
student_rec = record
id : string[10]
name : string[30];
score : integer;
next : student_ptr;
end;
i : integer;
student : array[1..30] of student_rec;
student : student_ptr;
Variable (ต่ อ)
 Type :
เป็ นตัวกาหนด
ช่วงค่าของตัวแปร (range of values)
 กลุ่มของตัวดาเนิ นการ (set of operations)

 Lifetime : ช่วงเวลานับตั้งแต่ตวั แปรถูก bind เข้ากับ memory
แบ่งเป็ น
static - bind ก่อนจะเริ่ ม execution (load time binding)
 stack-dynamic - bind เมื่อมีการประกาศตัวแปรตัวนั้น (runtime binding)
 explicit heap-dynamic - มีการ allocation หรื อ deallocation โดยใช้คาสัง่ โดยตรง
 implicit heap-dynamic - มีการ allocation หรื อ deallocation โดยใช้คาสัง่ กาหนดค่า

allocation - getting a cell from some pool of available cells
deallocation - putting a cell back
Variable (ต่ อ)
 Scope : บริ เวณหรื อส่ วนของโปรแกรมที่ตวั แปรที่กาหนดจะสามารถถูก
เรี ยกใช้งานได้ หรื อเรี ยกว่าโปรแกรมสามารถมองเห็น (visible) ได้
เฉพาะตัวแปรที่อยูใ่ นขอบเขตที่กาหนด
 Static scope
 ขึ ้นอยู่กบ
ั program text
 ผู้เขียน/คอมไพลเลอร์ ต้องทาหน้ าที่หาตาแหน่งที่ประกาศ เพื่อเชื่อมโยงกับ
ชื่อที่ตวั แปลอ้ างถึง
 การค้ นหาการประกาศ เริ่ มต้ นจาก local แล้ วค่อยๆ ขยายวงออกไปเรื่ อยๆ
จนเจอ
 Dynamic scope
 ขึ ้นอยู่กบ
ั ลาดับการเรี ยกใช้ ในโปรแกรม ไม่ใช่ตาแหน่งที่ประกาศ
 การค้ นหาการประกาศ เริ่ มจากค้ นย้ อนกลับไปตามลาดับการเรี ยก
โปรแกรมย่อย
Variable (ต่ อ)
ตัวอย่ างของ static & dynamic scope
MAIN
- declaration of x
SUB1
- declaration of x ...
call SUB2
...
SUB2
...
- reference to x ...
...
call SUB1
…
MAIN calls SUB1
SUB1 calls SUB2
SUB2 uses x
Static scoping
• อ้างถึง x ใน MAIN's x
Dynamic scoping
• อ้างถึง x ใน SUB1
• Advantage: convenience
• Disadvantage: poor
readability
Variable (ต่ อ)
Monolithic block structure
Program Example1;
var x:integer;
begin
….
end.
Declaration of x
Block structure - เป็ นโครงสร้างที่กาหนดขอบเขตการใช้งานของตัวแปรต่าง ๆ ที่ได้ถูกกาหนดไว้แล้ว
Variable (ต่ อ)
Flat block structure
Program Example2;
var x:integer;
Procedure P;
var y:integer;
begin
….
end; (*P*)
begin
….
end.
Declaration of x
Declaration of y
Declaration of z
Variable (ต่ อ)
Nested block structure
Program Example3;
var x:integer;
Procedure P;
var y:integer;
Procedure Q;
var z:integer;
begin
…
end; (*Q*)
begin
….
end; (*P*)
begin
….
end.
Declaration of x
Declaration of y
Declaration of z
Data Types
การรวมกลุ่มค่าของข้อมูล (value) เข้าด้วยกัน เรี ยกว่าเป็ นชนิดข้อมูล (Type)
 โดยทัว่ ไป ภาษาโปรแกรมจะประกอบด้วยชนิ ดข้อมูล 2 ประเภท คือ
 Primitive Type (Built-in Type) เป็ นชนิ ดข้อมูลที่มีมาพร้อมกับภาษา ซึ่ ง
มีการกาหนดการใช้เนื้อที่หน่วยความจา ค่าที่เป็ นไปได้ วิธีดาเนินการที่
สามารถกระทาได้กบั ชนิ ดข้อมูลไว้แล้ว

integer
16 bit
 value
-215 .. (215-1)
 operation
+ - */

Primitive type ชนิดเดียวกันอาจมีชื่อเรี ยกต่างกันไปในแต่ละภาษา

Pascal : integer , C : int
Data Types (ต่อ)
 User Defined Type เป็ นชนิ ดข้อมูลที่ผเู ้ ขียนโปรแกรมสามารถกาหนดขึ้นใหม่
ได้
่ ล้วขึ้นใหม่ (rename)
 การตั้งชื่อชนิ ดข้อมูลที่มีอยูแ
TYPE int = integer;
VAR a : int;
 การกาหนดชนิ ดข้อมูลที่มีโครงสร้างใหม่ตามที่ตอ
้ งการ
TYPE status = (single, married, divorced)
student_rec = RECORD
id : string[10];
grade : real;
END;
class = array[1..40] of student_rec;
VAR x : status; com, stat : class;
Data Types (ต่อ)

ประโยชน์ของ user defined type
 ช่วยให้โปรแกรมอ่านง่าย เข้าใจง่าย (Readability)
TYPE student_rec = RECORD
id : string[10];
grade : real;
END;
student = student_rec
class = array[1..40] of student;
Data Types (ต่อ)
 สามารถเปลี่ยนแปลงแก้ไขได้ง่าย (Modifiability)
TYPE student_rec = RECORD
id : string[10];
grade : real;
END;
VAR student : student_rec;
VAR student : RECORD
id : string[10];
grade : real;
END;
Data Types (ต่อ)
 สามารถนาไปใช้ซ้ า ๆ กันได้ง่าย (Factorization)
Procedure P;
var
a: student_rec;
begin
...
end;
Procedure Q;
var
b: student_rec;
begin
...
end;
Data Types (ต่อ)

ชนิดข้อมูลแบบมีโครงสร้าง (Composite Types หรื อ Structure Data type
หรื อ Data aggregates) เป็ นชนิดข้อมูลที่เกิดจากการนาค่าของข้อมูล
Primitive Types มากระทาต่อกันด้วยวิธีการต่าง ๆ ดังนี้
 Cartesian Product (record)
 Disjoint Unions (variant)
 Mappings (array)
 Powersets
(set)
Cartesian products
การนาชนิดข้อมูลที่เหมือนหรื อต่างกันมาคูณกัน
 ผลลัพธ์ที่ได้จะเป็ นชนิ ดข้อมูลใหม่ที่มีค่าของข้อมูลอยูใ่ นรู ปคู่ลาดับ
 สัญลักษณ์ S * T หมายถึง เซต S ทา cartesian product กับเซต T
ได้ผลลัพธ์เป็ นคู่ลาดับ (x,y)
S * T = { (x, y) ; x є S ; y є T }
#(S * T) = #S * #T
 ตัวอย่าง
S = { u, v }
T = { a, b, c }
S * T = { (u,a), (u,b), (u,c), (v,a), (v,b), (v,c) }
#(S * T) = 2 * 3 = 6

Cartesian products (ต่อ)
เราสามารถนาเซตมากกว่า 2 เซตมาทา cartesian product ได้
T1 * T2 * ... * Tn = { (t1,t2,..,tn) ; t1 є T1; t2 є T2,.., tn є Tn }
# (T1 * T2 * … * Tn)= #T1 * #T2 * … * #Tn
 ชนิ ดข้อมูลแบบนี้ เช่น Record ใน Pascal, COBOL หรื อ Struct ใน C, PL/I
 RECORD
field1 : type1;
field2 : type2;
…
fieldn : typen;
END;

Cartesian products (ต่อ)
TYPE month = (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec);
date = RECORD
m : month;
d : 1..31;
END;
 { jan, feb, .. , dec } * { 1,2, .. , 31 }
= { (jan,1), (jan,2), (jan,3), … , (jan,31),
(feb,1), (feb,2), (feb,3), … ,(feb,31),
…
(dec,1), (dec,2), (dec,3), … ,(dec,31)}
 จานวนข้อมูล 12 * 31 = 372
คู่ลาดับ

Disjoint unions
การนาชนิดข้อมูลที่ต่างกันมารวมกัน
 ผลลัพธ์ที่ได้จะเป็ นชนิ ดข้อมูลใหม่ที่มีค่าของข้อมูลชนิ ดใดชนิ ดหนึ่ ง
 สัญลักษณ์ S + T หมายถึง เซต S ทา disjoint union กับเซต T
ได้ผลลัพธ์เป็ นค่าของเซต S หรื อ T เซตใดเซตหนึ่ง
S + T = { left x / x є S} U {right y / y є T }
#(S + T) = #S + #T
 ตัวอย่าง
S = { u, v }
T = { a, b, c }
S + T = { u, v, a, b, c }
#(S + T) = 2 + 3 = 6

Disjoint unions (ต่อ)
ชนิดข้อมูลแบบนี้ เช่น Variant record ใน Pascal หรื อ Union ใน C
 RECORD
field1 : type1;
field2 : type2;
…
fieldn : typen;
CASE tag : tag_type OF
label1 : (fieldlist1);
label1 : (fieldlist1);
...
label1 : (fieldlist1);
END;

Disjoint unions (ต่อ)
fix part
tag
variant part
currency exact
int_val
currency approx real_val
TYPE currency = ($,B)
accuracy = (exact, approx)
number = RECORD
unit : currency;
CASE acc : accuracy OF
exact : (int_val : integer);
approx : (real_val : real);
END;
 ค่าที่เป็ นไปได้ของ number คือ
{ …, exact(-2), exact(-1), exact(0), exact(1), exact(2), … } U
{ …, approx(-1.0),..., approx(0.0),…, approx(1.0),… }

Mappings
การใช้ความสัมพันธ์แบบฟังก์ชนั จากค่าของข้อมูลเซตหนึ่ งไปยังค่าของข้อมูลอีก
เซตหนึ่ง
 จะทาให้ขอ
้ มูลชนิดเดียวกันถูกรวบรวมเข้าด้วยกันเป็ นกลุ่ม และสามารถเรี ยกใช้ได้
 สัญลักษณ์ S -> T หมายถึง การ map ค่าทุกค่าของเซต S ไปยังเซต T
S -> T = { m / x є S => m(x) є T }
#(S-> T) = (#T)#S
 ตัวอย่าง
S = { u, v }
T = { a, b, c }
S -> T = { u->a, v->a } { u->a, v->b } { u->a, v->c }
{ u->b, v->a } { u->b, v->b } { u->b, v->c }
{ u->c, v->a } { u->c, v->b } { u->c, v->c }
#(S -> T) = 32 = 9

Mappings (ต่อ)
TYPE color = (red, green, blue)
pixel = array[color] of 0..1;
 การ map ค่าของเซต color ไปบนเซตของ { 0,1 } จะได้
pixel = color -> { 0,1 }
= { red -> 0, green -> 0, blue -> 0 } { red -> 1, green -> 0, blue -> 0 }
{ red -> 0, green -> 0, blue -> 1 } { red -> 1, green -> 0, blue -> 1 }
{ red -> 0, green -> 1, blue -> 0 } { red -> 1, green -> 1, blue -> 0 }
{ red -> 0, green -> 1, blue -> 1 } { red -> 1, green -> 1, blue -> 1 }

Powersets
ชนิดข้อมูลที่ตอ้ งการรวบรวมข้อมูลที่มีชนิ ดเดียวกันเข้าด้วยกันเช่นเดียวกับชนิด
ข้อมูลแบบอาเรย์ แต่การทางานกับเซตไม่ตอ้ งการดึงข้อมูลทีละตัวในเซต แต่
ต้องการทางานกับเซตทั้งชุด
 การดาเนิ นการกับเซตเหมือนกับการดาเนิ นการของทฤษฎีเซต คือ
union, intersection, different
 สัญลักษณ์ S หมายถึง เซตของสับเซตทั้งหมดของเซต S
S={s/s S}
# S = 2#S

Powersets (ต่อ)
TYPE color = (red, green, blue)
Hue = set of color
 ค่าที่เป็ นไปได้ท้ งั หมดของเซต hue คือเซตของสับเซตทั้งหมดของ color หรื อ
powerset ของเซต color
color = { { }, {red}, {green}, {blue},
{red, green}, {red,blue}, {green,blue},
{red, green, blue} }

Type checking
การตรวจสอบชนิดข้อมูล ก่อนที่จะมีการดาเนินการ (operation) ใด เพื่อป้ องกัน
ไม่ให้โปรแกรมทางานที่เป็ นไปไม่ได้กบั ชนิ ดข้อมูลหนึ่ง ๆ
เช่น นาข้อมูลอักขระมาทา operation คูณ กับข้อมูลบูลีน
 ประเภทของ Type checking แบ่งตามช่วงเวลาที่ตรวจสอบ
 Static type checking ตรวจสอบชนิ ดข้อมูลในช่วงเวลาการแปลโปรแกรม
(compile time) โดยตรวจสอบว่าชนิดของตัวแปรที่กาหนดไว้ถกู ต้อง
หรื อไม่
 Dynamic type checking ตรวจสอบชนิ ดข้อมูลในช่วงเวลาการทางานของ
โปรแกรม (run time) ตัวแปลภาษาจะไม่สามารถตรวจสอบได้วา่ กาหนด
ชนิดข้อมูลของตัวแปรถูกต้องหรื อไม่ จนกว่าจะ execute

Type checking (ต่อ)
Pascal
Function Even ( n : integer ) : boolean;
begin
Even := (n mod 2) = 0
end;
Lisp
Function Even ( n );
begin
Even := n mod 2 = 0
end;
Type equivalence
เทคนิคที่ใช้ในการตรวจสอบว่าตัวแปร 2 ตัวหรื อมากกว่า เป็ นตัวแปรชนิด
เดียวกันหรื อไม่
 วิธีการตรวจสอบมี 2 ลักษณะ คือ
 Name Equivalence
ตัวแปร X และ Y จะเป็ นตัวแปรที่มี Name equivalence กันก็ต่อเมื่อตัวแปร
ทั้งสองมีชื่อชนิดของตัวแปร (Type name) เหมือนกัน
 Structure Equivalence
ตัวแปร X และ Y จะเป็ นตัวแปรที่มี Structure equivalence กันก็ต่อเมื่อตัว
แปรทั้งสองมีโครงสร้างเหมือนกัน

Type equivalence (ต่อ)
TYPE arr_type = array[1..20] of integer;
VAR x, y : array[1..20] of integer;
a : arr_type;
b : RECORD
c : integer;
d : arr_type;
END;
CONST N = 2;
VAR
a : array[1..N] of char;
b : array[1..N] of char;
a name equivalence b.d
a structure equivalence b