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

Preview:

DESCRIPTION

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

Citation preview

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

Aho, Sethi, Ullman – Chapter 6

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

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

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

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

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

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

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

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

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

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

תוכנית מקור

תוכנית מטרה

lexical analysis

syntax analysis

semantic analysis

token stream

syntax tree

decorated syntax tree

scanner

parser

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

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

תוכנית מקור

תוכנית מטרה

lexical analysis

syntax analysis

semantic analysis

token stream

syntax tree

decorated syntax tree

scanner

parser

Type Checking

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

תוכנית מקור

תוכנית מטרה

lexical analysis

syntax analysis

semantic analysis

token stream

syntax tree

decorated syntax tree

scanner

parser

Type Checking

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

)דיון בכתה(

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]

type systems

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

boolean, character, integer, real,…

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

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

type constructor

מערכים

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

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

(pointersמחוונים )

פונקציות

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

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

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

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

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

integercharchar

pointer

ייצוג כעץ

integerchar

pointer

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

type systems

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

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

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

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

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

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

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

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

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

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

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

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

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

type checkerדוגמא ל-

ביטויים

E → literal

E → num

E → id

E → E mod E

E → E [ E ]

E → ↑E

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

E → E ) E (

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 }

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 }

הרחבת השפה

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

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

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

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

משפטים

משפטים

S → id := E

S → if E then S

S → while E do S

S → S ; S

משפטים

משפטים

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

equivalence of type expressions

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

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

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

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

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;

הערות

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

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

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

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

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

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

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

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

על ידי הצבה

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

המרת טיפוסים

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

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

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

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

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

המרת טיפוסים

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כללים להמרה משתמעת מ-

overloading

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

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

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

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

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

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מטיפוס

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 }

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

ירושה

generics

הסקת טיפוסים

הסקת טיפוסים

:Haskellפונקציה ב-

length [ ] = 0

length )first:rest( = 1 + length rest

הסקת הטיפוס:

list → int

Recommended