בדיקת טיפוסים

34
םםםםם םםםםםםםAho, Sethi, Ullman – Chapter 6

description

בדיקת טיפוסים. Aho, Sethi, Ullman – Chapter 6. בדיקת טיפוסים (type checking). המטרות העיקריות של בדיקת הטיפוסים בדיקת תקינות פתרון של בעיות overloading ו- polymorphism בדיקת שקילות של טיפוסים. בדיקת טיפוסים (type checking). ההקשר הכללי – בדיקות סטטיות דוגמאות בדיקת טיפוסים - PowerPoint PPT Presentation

Transcript of בדיקת טיפוסים

Page 1: בדיקת טיפוסים

בדיקת טיפוסים

Aho, Sethi, Ullman – Chapter 6

Page 2: בדיקת טיפוסים

( type checking)בדיקת טיפוסים

המטרות העיקריות של בדיקת הטיפוסיםבדיקת תקינות פתרון של בעיות overloading -וpolymorphism

בדיקת שקילות של טיפוסים

Page 3: בדיקת טיפוסים

בדיקות סטטיותההקשר הכללי – דוגמאות

בדיקת טיפוסיםבדיקת תקינות של זרימת הבקרה

goto?ליעד לא מוגדר בדיקת יחידּות

?שני משתנים עם אותו השם בדיקות הקשורות במופעים של שמות בתכנית

begin A … end B?דינאמיותבדיקות המתבצעות בזמן ריצה נקראות

( type checking)בדיקת טיפוסים

Page 4: בדיקת טיפוסים

front endשלב הניתוח - תזכורת –

תוכנית מקור

תוכנית מטרה

lexical analysis

syntax analysis

semantic analysis

token stream

syntax tree

decorated syntax tree

scanner

parser

היכן מוסיפים את בדיקות הטיפוסים?

Page 5: בדיקת טיפוסים

front endשלב הניתוח - תזכורת –

תוכנית מקור

תוכנית מטרה

lexical analysis

syntax analysis

semantic analysis

token stream

syntax tree

decorated syntax tree

scanner

parser

Type Checking

Page 6: בדיקת טיפוסים

front endשלב הניתוח - תזכורת –

תוכנית מקור

תוכנית מטרה

lexical analysis

syntax analysis

semantic analysis

token stream

syntax tree

decorated syntax tree

scanner

parser

Type Checking

Page 7: בדיקת טיפוסים

בדיקת טיפוסים – למה זה טוב?

)דיון בכתה(

Page 8: בדיקת טיפוסים

type systems

יחוס של טיפוס לתוצאה של פעולה

“If both operands of the operators of additions, subtraction

and multiplication are of type integer, then the result is of

type integer” [The Pascal Report]

“The result of the unary & operator is a pointer to the object

referred to by the operand. If the type of the operand is ‘…’,

the type of the result is ‘pointer to …’.”

[The C Reference Manual]

Page 9: בדיקת טיפוסים

type systems

(type expressionsביטויי טיפוסים )טיפוס בסיסי

boolean, character, integer, real,…

enumerated types…, void , type error שמות של ביטויי טיפוסים תוצאת ההפעלה שלtype constructor ביטויי טיפוס יכולים להכיל משתנים מטיפוס

"ביטוי טיפוס".

Page 10: בדיקת טיפוסים

type constructor

מערכים

(מכפלה קרטזית )(class או struct או recordרשומות )

כמו מכפלה קרטזית, אבל עם שמות

(pointersמחוונים )

פונקציות

Page 11: בדיקת טיפוסים

type systems

array [3..6] of realarray)3..6, real(

var p: ↑rowpointer )row(

type row = record

address: integer;

name: array [1..5] of char;

end;

record ))address integer(

)name array)1..5, char(((

var table: array [1..100] of rowarray )1..100, row(

function mod )integer, integer( : integer

integer integer → integer

Page 12: בדיקת טיפוסים

ייצוג ביטויי טיפוס כגרף

)char x char → pointer )integerעבור

עלים – טיפוסים בסיסיים

type constructorsשאר הצמתים – תוצאה של הפעלה של

integercharchar

pointer

ייצוג כעץ

integerchar

pointer

ייצוג כגרף חסר מעגלים

Page 13: בדיקת טיפוסים

type systems

היא אוסף חוקים להצמדת ביטויי מערכת טיפוסיםטיפוס לחלקים שונים של התוכנית

מימוש מערכת הטיפוסים של שפת תכנות – באמצעות type checkers

ניתן לוודא תקינות בזמן soundבמערכת טיפוסים שהיא קומפילציה

נקראת – soundשפה עבורה מערכת הטיפוסים היא strongly typed

באופן מעשי, בדיקות מסוימות ניתן לבצע רק בזמן ריצה:חריגה מגבולות מערך. דוגמא

Page 14: בדיקת טיפוסים

הסקת טיפוסים וטיפול בשגיאות

integer הוא x ניתן להסיק ש- x + 5דוגמא – מהביטוי

הערה – גם בשפות בהן הטיפוסים דינאמיים, ידיעה טובה של הטיפוסים מסייעת ליצירת קוד יעיל יותר /

לאינטרפרטציה יעילה יותר

הערה – שפות מסוימות מחייבות מערכת משוכללת של הסקת טיפוסים

טיפול בשגיאותבאמצעות הסקת טיפוסים וזיהוי שגיאות על ידי הגדרת טיפוס שגיאה, על מנת למנוע

התפשטות של שגיאות

Page 15: בדיקת טיפוסים

type checkerדוגמא ל-

תוכנית היא הכרזות + ביטוי

P → D ; E

משפטי הכרזה

D → D ; D

D → id : T

T → char

T → integer

T → ↑T

T → array [ num ] of T

הכרזה על פונקציות

T → T → T

Page 16: בדיקת טיפוסים

type checkerדוגמא ל-

ביטויים

E → literal

E → num

E → id

E → E mod E

E → E [ E ]

E → ↑E

הפעלה של פונקציות

E → E ) E (

Page 17: בדיקת טיפוסים

type checkerדוגמא ל-

תוכנית היא הכרזות + ביטוי

P → D ; E

משפטי הכרזה

D → D ; D

D → id : T { addtype )id.entry, T.type( }

T → char { T.type := char }

T → integer { T.type := int }

T → ↑T1 { T.type := pointer )T1.type( }

T → array [ num ] of T1 { T.type := array )1..num.val, T1.type( }

הכרזה על פונקציות

T → T1 → T2 { T.type := T1.type → T2.type }

Page 18: בדיקת טיפוסים

type checkerדוגמא ל-

ביטויים

E → literal { E.type := char }

E → num { E.type := int }

E → id { E.type := lookup )id.entry( }

E → E1 mod E2 { E.type :=

if E1.type = int and E2.type = int then int else type_error }

E → E1 [ E2 ] {E.type :=

if E2.type = int and E1.type = array )s, t( then t else type_error }

E → ↑E1 {E.type := if E1.type=pointer)t( then t else type_error }

הפעלה של פונקציות

E → E1 ) E2 ( { E.type := if E2.type = s and E1.type = s → t then t else type_error }

Page 19: בדיקת טיפוסים

הרחבת השפה

נוסיף לשפה משפטים.

תוכנית היא כעת:

P → D; Sהכללים שהגדרנו עבור ביטויים עדיין שימושיים, משום

שמשפטים כוללים בתוכם ביטויים.

Page 20: בדיקת טיפוסים

משפטים

משפטים

S → id := E

S → if E then S

S → while E do S

S → S ; S

Page 21: בדיקת טיפוסים

משפטים

משפטים

S → id := E { S.type := if id.type = E.type then void else type_error }

S → if E then S1 { S.type := if E.type = boolean then S1.type else type_error }

S → while E do S1 { S.type := if E.type = boolean then S1.type else type_error }

S → S1 ; S2 { S.type := if S1.type = void and S2.type = void then void else type_error }

איך נראים משפטי השמה ?Cבשפת

הרעיון:error propagation

Page 22: בדיקת טיפוסים

equivalence of type expressions

מושג השקילות הכרחי להגדרת נכונות של תכנית

תמונת השקילות של ביטויים לא תמיד מובנת user defined typesמאליה בשפות בהן יש

הערה: בדיקת השקילות היא לפעמים חלקית, למשל

האם מערכים בעלי תחומי הגדרה שוניםשקולים?

Page 23: בדיקת טיפוסים

structural equivalence

function sequiv )s, t(: boolean; begin

if s and t are the same basic type then return true

else if s = array)s1, s2( and t = array)t1, t2( then

return sequiv )s1, t1( and sequiv )s2, t2(

else if s = s1 x s2 and t = t1 t2 then

return sequiv )s1, t1( and sequiv )s2, t2(

else if s = pointer )s1( and t = pointer )t1( then

return sequiv )s1, t1(

else if s = s1 → s2 and t = t1 → t2 then

return sequiv )s1, t1( and sequiv )s2, t2(

else return false

end;

Page 24: בדיקת טיפוסים

הערות

אפשר להרחיב את האלגוריתם על מנת שיטפל בסוגים נוספים של טיפוסים

האלגוריתם מסוגל לטפל בעצים או בגרפים חסרי מעגלים

הערה: נתבונן בכלל העוסק בגבולות של מערכים

else if s = array)s1, s2( and t = array)t1, t2( then

return sequiv )s1, t1( and sequiv )s2, t2(

על ידי ויתור על המרכיב הזה נוותר על זהות גדלי המערך

Page 25: בדיקת טיפוסים

equivalence of type expressions

user defined types

type link =↑cell;

var next: link;

last: link;

p: ↑ cell;

q, r: ↑ cell;

ו– p ולכן לא ברור האם identical typeבפסקל לא הוגדר המושג nextשווים

ובאופן כללי

name equivalence ≠ structural equivalence

Page 26: בדיקת טיפוסים

equivalence of type expressions

recordsהגדרות מעגליות -- בעיה אופיינית לטיפול ב-

type link =↑cell;

cell = record

info: integer;

next: link;

end;

pointernextintegerinfo

xx

x

cell = record

על ידי הצבה + הצבעהcell

pointernextintegerinfo

xx

x

cell = record

על ידי הצבה

Page 27: בדיקת טיפוסים

equivalence of type expressions

Cהגדרות מעגליות -- בעיה אופיינית לטיפול ב-

struct cell {

int info;

struct cell *next;

};

חוץ מ- עבור כל הטיפוסים structural equivalence הגישה –struct

צריכים להיות מוגדרים לפני השימוש, חוץ type namesכל ה- שעדיין לא הוכרזstructממצביעים ל-

cell

pointernextint info

xx

x

cell = struct

Page 28: בדיקת טיפוסים

המרת טיפוסים

.מפורשתהמרת טיפוסים יכולה להיות כדי chr ו-ordלמשל, בפסקל משתמשים בפונקציות

ולהיפך.integer ל-charלהמיר מ-

, implicit )משתמעתהמרת טיפוסים יכולה להיות גם (.coercionנקרא לעיתים

המרה משתמעת מתבטאת לעיתים קרובות בקוד כקריאה לפונקציה פנימית.

מומלץ לבצע המרות משתמעות של קבועים בזמן הידור ולא בזמן ריצה...

Page 29: בדיקת טיפוסים

המרת טיפוסים

productionSemantic Rule

E→num

E → num . num

E → id

E → E1 op E2

E.type :=

E.type :=

E.type :=

E.type :=

integer

real

lookup)id.entry(

if E1.type = integer and E2.type = integer then integer

else if E1.type = integer and E2.type = real then real

else if E1.type = real and E2.type = integer then real

else if E1.type=real and E2.type=real then real

else type_error

real ל-integerכללים להמרה משתמעת מ-

Page 30: בדיקת טיפוסים

overloading

הן של פונקציות והן של overloadingיתכן אופרטורים

overloaded symbol מקבל משמעות שונה – בתלות בהקשר

resolution– קביעת משמעות יחידה

overloadedדוגמא – פעולות אריתמטיות הן

Page 31: בדיקת טיפוסים

קבוצת אפשרויות לטיפוסים עבור תת ביטויים

Ada ב- *הרחבה של פעולת ה- דוגמא:

function “*” )i, j : integer( return complex;

function “*” )x, y : complex( return complex;

האפשרויות:

integer integer → integer

integer integer → complex

complex complex → complex

מוגדר על פי ההקשר5 * 3הטיפוס של

. אם integer מוגדר כ- 3*5 הוא כל הביטוי אזי (3*5*)2אם הוא 3*5, אזי complex מטיפוס z באשר )z*)5*3הביטוי הוא

complexמטיפוס

Page 32: בדיקת טיפוסים

overloadingהמרת טיפוסים –

type checking rules for coercion from integer to real

type_error ריק – E.typesאם

צריך בסופו של דבר להקבע טיפוס יחיד; התהליך נקרא Adaב- narrowing

productionsemantic rule

E→id

E→E1 ) E2 (

E.types :=

E.types :=

lookup ) id.entry (

{ t | there exists an s in E2.types such that s→t is in E1.types }

Page 33: בדיקת טיפוסים

type systemsדברים שמסבכים

ירושה

generics

הסקת טיפוסים

Page 34: בדיקת טיפוסים

הסקת טיפוסים

:Haskellפונקציה ב-

length [ ] = 0

length )first:rest( = 1 + length rest

הסקת הטיפוס:

list → int