brief introduction to logic

284
תתתתתת תתתתתת תתתתתת תתתת: תתתתתתת תתתתת תתת תתת: תתתת תתתתת226 תתתת תתתת: תתת ת16:00-17:00 תתתתת:8765

Upload: irving

Post on 02-Feb-2016

59 views

Category:

Documents


0 download

DESCRIPTION

Brief Introduction to Logic. Outline. Historical View Propositional Logic : Syntax Propositional Logic : Semantics Satisfiability Natural Deduction : Proofs. Historical view. Philosophical Logic 500 BC to 19th Century Symbolic Logic Mid to late 19th Century Mathematical Logic - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Brief Introduction to Logic

לוגיקה מתמטית מתקדמת

פרופסור דורון פלד מרצה:226חדר: מדעי המחשב

16:00-17:00שעות קבלה: יום א 8765 טלפון:

Page 2: Brief Introduction to Logic

מה לומדים פה?

•http://hebrew.joelonsoftware.com/Articles/AdviceforComputerScienceC.html

Page 3: Brief Introduction to Logic

חידה

שלשה ילדים משחקים בחוץ. חלק מתלכלכים על מצחם בבוץ, אבל לא יודעים

זאת.עובר אב של אחד מהם ואומר:

"לפחות אחד מכם מלוכלך בבוץ על מצחו, מי יודע שמצחו מלוכלך?"

אף ילד לא עונה. הוא שואל שוב, ואף ילד לא עונה, בפעם השלישית שהוא שואל, כל הילדים עונים יחד שראשם שלהם

מלוכלך.הכיצד?

Page 4: Brief Introduction to Logic

חידה

שלשה ילדים משחקים בחוץ. חלק מתלכלכים על מצחם בבוץ, אבל לא יודעים

זאת.עובר אב של אחד מהם ואומר:

"לפחות אחד מכם מלוכלך בבוץ על מצחו, מי יודע שמצחו מלוכלך?"

אף ילד לא עונה. הוא שואל שוב, ואף ילד לא עונה, בפעם השלישית שהוא שואל, כל הילדים עונים יחד שראשם שלהם

מלוכלך.הכיצד?

Page 5: Brief Introduction to Logic

מדוע אנחנו לומדים לוגיקה?

לוגיקה הינה בבסיס של מתמטיקה.•עונה על שאלות פילוסופיות של "מה ניתן •

להוכיח".מהווה כלי עבור בדיקה ואימות של תכנה •

וחומרה.בבסיס של מסדי נתונים. תעשיה של מאות •

ביליוני יורו.PROLOGתכנות בלוגיקה •

Page 6: Brief Introduction to Logic

מה נלמד?

מה זה "מבנה" לוגי?•(SEMANTICS( ומשמעות )SYNTAXתחביר )•

של לוגיקת פסוקים ולוגיקה מסדר ראשון.דוגמאות לנוסחאות בלוגיקה•

xy)x<yz)x<zz<y( מתי זה נכון? מה זה אומר?

ספיקות של נוסחא: מעל מבנה נתון, מעל כל •המבנים.

כח ביטוי: מה ניתן לתאר בלוגיקה, מה לא ניתן? •כיצד ניתן להוכיח שלא ניתן לתאר מבנה כלשהו

בלוגיקה מסדר ראשון.מתי ניתן להוכיח דברים ומתי לא?•

Page 7: Brief Introduction to Logic

מה עוד נלמד?

כיצד ניתן לכתוב הוכחות בלוגיקה )מערכת הוכחה(?•האם ניתן להוכיח רק דברים "נכונים"? נאותות:•האם ניתן להוכיח כל דבר נכון שניתן שלמות:•

לכתוב?משפט השלמות ללוגיקה מסדר ראשון.•GOEDELמשפט אי השלמות של •

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

Page 8: Brief Introduction to Logic

אקסיומות )פורמלי, תאורטי, לא תכנות, מזכיר קצת את המאמר?(

1. (())

2. ((())(()()))

3. (((F)F))

4. (v()(v))

. אינו חופשי ב-vכאשר 5. v)v()t(

. אשר מופיע קשור ב--tכאשר אין משתנה ב

Page 9: Brief Introduction to Logic

כללי הוכחה )זה ממשיך...(

• MP )Modus Ponens(

• GEN )Generalization()v(x )x(

אינו מופיע חופשי באף אחת מההנחות vכאשר

Page 10: Brief Introduction to Logic

דוגמא )כן, בהתחלה זה כזה מסובך להוכיח דבר כזה פשוט(

vw )v,w( |- wv )v,w( vw )v,w( Assumption vw )v,w( w )v,w( A5 w )v,w( MP )1+2( w )v,w( )v,w( A5 )v,w( MP )3+4( v )v,w( GEN )5( w v )v,w( GEN )6(

Page 11: Brief Introduction to Logic

דוגמא

vw )v,w( |- wv )v,w( vw )v,w( Assumption vw )v,w( w )v,w( A5 w )v,w( MP )1+2( w )v,w( )v,w( A5 )v,w( MP )3+4( v )v,w( GEN )5( w v )v,w( GEN )6(

Page 12: Brief Introduction to Logic

נזכיר אך לא נתעמק )אנו מביני עולם...(

האם ניתן לתאר את כל המתמטיקה )תורת•הקבוצות( בלוגיקה?

, האם יש בה סתירות?ZFCמערכת אקסיומות •

Page 13: Brief Introduction to Logic

נלמד אפליקציות )ככל שירשה הזמן(

PROLOGתכנות בלוגיקה •

AI ב-PLANNINGתכנון •

אימות של תכניות )יש קורס שלם: שיטות •אימות לתכנה(. אימות ידני ואוטומטי.

(.PVSכלים להוכחה )•

Page 14: Brief Introduction to Logic

איפה כדאי להסתכל?

Roger C. Lyndon, Notes on Logic

(Short, Simple, but unpleasant notation)…

http://cs.biu.ac.il/~doronp/logic.html

Page 15: Brief Introduction to Logic

2שעור

תחביר וסמנטיקה

Page 16: Brief Introduction to Logic

על תחביר וסמנטיקה

אחד העקרונות החשובים של לוגיקה:• הסימון וכללי הכתיבה הנאותהתחביר:הפרדה בין

: המשמעות של נוסחאות.סמנטיקהשל נוסחאות, וה

התחביר הפרדה דומה קיימת בשפות תכנות:•(Syntax והסמנטיקה. שגיאות תחביר מתגלות בזמן )

הידור )קומפילציה(. בזמן זה גם מתורגמת התכנית לפרוצדורות שהמחשב יכול לבצע. )מחרוזת(

הסמנטיקה של השפה מכתיבה מה יתבצע.

Page 17: Brief Introduction to Logic

בעיה בהגדרת סמנטיקה

כיצד מגדירים סמנטיקה של לוגיקה? בעיה:•, האם יש צורך פורמליתאם לוגיקה הינה שפה

להגדיר אותה באמצעות שפה פורמלית אחרת?אפשר להגדיר אותה בשפה "מתמטית", אבל רצינו

להגדיר את המתמטיקה בלוגיקה.הסמנטיקה מוגדרת בשפה טבעית, שתקרא פתרון:•

(Meta Language)פה "מטה שפה" )ולא שפה מתה...(.

לוגיקה )למשל עללוגיקה תשמש להוכחות. הוכחות •קריאות יחידה של נוסחא( יעשו במטה שפה.

Page 18: Brief Introduction to Logic

תחביר של תורת הפסוקים

Var={p0, p1, p2, …, pn}

Symb={/\, \/, , ¬, T, F, ), (}

כמו Varאנו מתייחסים לאיברים של הינה הקבוצה של Symbאטומים.

הסימנים הלוגיים.

Page 19: Brief Introduction to Logic

היא תת קבוצה של WFFקבוצת הגדרה:קבוצת המילים הסופיות מעל מוגדרת בצורה

אינדוקטיבית כדלקמן:בסיס האינדוקציה:

WFF היא בVar מ-piכל )1()2( )3(

סגירות: אז גם WFF( אם הם ב1) אז גם WFF( אם הם ב2) אז גם WFF( אם הם ב3) אז גם WFF( אם ב4)

קבוצת הנוסחאות הבנויות היטב )נב"ה =WFFכמחרוזות )

Symb Var

F WFFT WFF

1 2, 1 2( ) 1 2( ) 1 2( )

1 2,

1 2,

Page 20: Brief Introduction to Logic

תמיד נשתמש באותיות יווניות קטנות כדי לציין WFFאיברים מ

לעיתים נרשום והכוונה היא שכל המשתנים במילה הם מתוך

דוגמא:

הנוסחאות הבאות הן נוסחאות בנויות היטב :WFFמ

1 2 3( , , ..... )np p p p1 2 3, , ..... np p p p

1 3 1

1 2 1 3

1 2 1 2

(( ) ( )),

((( ) ) ),

( ), ( )

p T p p

p p p p

p p p p

Page 21: Brief Introduction to Logic

דוגמאות נוספות

הקבוצה מוגדרת באופן דומה אבל ( מתנאי 3( מהבסיס ו)3( ו-)1 ב)רקבשימוש הסגירות.

:WFF שייכים ללא הביטויים הבאים 1.

. הביטויים הבאים שייכים ל :2

{ , }FWFF WFF

1 1

1 2 1 2 1 2

((( )), ( )), ( ),

, ( , ( )) , p1p2 ))

p p

p p p p p p

{ , }FWFF

1 1

1 1

((( ) ) ),

(( ) ( ))

p F p F

p F p F

Page 22: Brief Introduction to Logic

הדרגה של נוסחא בונה את הנוסחאות WFFההגדרה האינדוקטיבית של

בשלבים . הגדרות אינדוקטיביות הן בנייה מלמטה למעלה. משפט הקריאה היחידה מאפשר לנו

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

הגדרה: פונקציה:rankתהי

בסיס:אם אז

אנחנו קוראים ל גם נוסחא אטומיתסגירות: יהיו אזי

נמצאים ב . מוגדר כ המינימלי כך ש

WFF N0 { , }WFF T F Var

( ) 0rank 0WFF

1 2, nWFF 0WFF

1 1 2 1 2 1 2 1, ( ), ( ), ( ),

1( )rank n N1 nWFF

nWFF

1nWFF

Page 23: Brief Introduction to Logic

1

1 2 1 2

1 2 1 2

1 2 1 2

( ) 1 max{ ( ), ( )};

( ) 1 max{ ( ), ( )};

( ) 1 max{ ( ), ( )};

( ) 1 ( )

n nWFF WFF

rank rank rank

rank rank rank

rank rank rank

rank rank

Page 24: Brief Introduction to Logic

הזה?rankלמה זה טוב ה

.rankניתן להוכיח דברים באינדוקציה על ה• מבטא את גובה העץ כאשר כותבים את rankה•

הנוסחא בצורה של עץ.

כיצד מקבלים בצורה אינדוקטיבית את גודל •הנוסחא כתלות בתתי הנוסחא שלה?

\/

/\p

p p

Page 25: Brief Introduction to Logic

תתי נוסחאות

כל הנוסחאות המשמשות בבניה הרקורסיבית של• שלה.תתי נוסחאותנוסחא הן

יש את תתי ( )p2F((p1F ))למשל לנוסחא •הנוסחאות הבאות:

p1

)p1F(p2

)p2F( ))p1F()p2F((

Page 26: Brief Introduction to Logic

החלפת תתי נוסחאות(substitution)

נרשום לפעמים נוסחא בצורה פרמטרית:• )1, 2, … , n(

כממלאי מקום עבורiצורת כתיבה זו תשתמש בסימונים •נוסחאות. למשל

)1, 2(= ))1F()2 F((

1=(p1p3)אם נציב •

2=)p2F(:נקבל

((( p1p3)F)))p2F(F( )

כל נוסחא מתפרקת בצורה יחידה לתתי קריאות יחידה:•נוסחאות. הוכחה בתרגול!

Page 27: Brief Introduction to Logic

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

( היא WFFמשמעות נוסחה בנוייה היטב )• הוא שקר 0}, כאשר 0,1אלמנט מתוך הקבוצה {

(FALSE ו )1( הוא אמת TRUE.) F ו-T הסמנטיים ו-1 ו-0נבחין בין הערכים •

הסינטקניים.ניתן לחשוב על טבלת אמת כעל תיאור •

התנהגות של מעגל בוליאני מסויים.

טבלאות האמת מתארות את התנהגות •,הקשרים הלוגיים בהתאמה. ,

Page 28: Brief Introduction to Logic

השמות אמת

הגדרה

השמת ערך אמת היא פונקצייה:•

את כל השמות האמת.=Asנסמן ב •

כעל רגיסטרים במצב מסוים Varניתן לחשוב על המשתנים ב • כעל פונקצייה אשר בודקת את התוכן של כל Zנתון ועל רגיסטר.

: {0,1}z Var

{0,1}Var

Page 29: Brief Introduction to Logic

, זאת אומרת }0,1{ ויחזירו }0,1{-יה של ערכים nהטבלאות יכילו •שנוצרת הפונקצייה:

קבוצתה ההשמות: נרצה לתת פירוט לכל אחד מהמשתנים האטומיים • בכל אחד מהם: } 0,1{של הנוסחה, זאת אומרת, נשים

•As היא קבוצת ההשמות

היא פונקצייה שמקיימת:Mפוקנציית המשמעות •

M:WFFxAs{0,1}•F,Tהם עוד סימון

:{0,1} {0,1}TT

: {0,1}Z Var

Page 30: Brief Introduction to Logic

טבלאות אמתהגדרה

היא פונקצייה:TT-י, nטבלת אמת מסדר

לכל אחד מן הסימנים נשייך טבלת אמת בינארית

שתיוצג באופן הבא:

:{0,1} {0,1}nTT

, , , ,TT TT TT

TT(0,0) 0TT (0,1) 0TT

(0,0) 0TT

TT

(1,0) 0TT (1,1) 1TT

(0,1) 1TT (1,0) 1TT (1,1) 1TT

Page 31: Brief Introduction to Logic

טבלאות אמת - המשך

TT(0,0) 1TT

(0,1) 1TT (1,0) 0TT (1,1) 1TT

(1) 0TT

(0) 1TT

1TTT 0FTT

FTT TTT

TT

Page 32: Brief Introduction to Logic

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

היא פונקצייהMPLפונקציית משמעות

MPL : WFF x As {0,1}

את פונקציית המשמעות MPL נציין ע"י הגדרהלשם תחשיב הפסוקים שמוגדרים בדרך

אינדוקטיבית כדלהלן :בסיס:

MPL)pi,z) = z(pi) ;

MPL)T,z) = TTT = 1;

MPL)F,z) = TTF = 0;

Page 33: Brief Introduction to Logic

המשך(( פונקצית המשמעות

המשך הגדרה אינדוקטיבית

MPL))12(,z(=TT)MPL)1,z(,MPL)2,z((

MPL))12(,z(=TT )MPL)1,z(,MPL)2,z((

MPL))12(,z(=TT )MPL)1,z(,MPL)2,z((

MPL) ,z(=TT )MPL),z((

Page 34: Brief Introduction to Logic

{ , }FWFF

{ , }FWFF WFF

מוגדרת עבור נוסחאות ,F{ M{פונקציית המשמעות מוגדרות היטב מסוג .

להיות,F{ M{כאשר נגדיר את .As Xהצימצום של ל

PLM{ , }FM

Page 35: Brief Introduction to Logic

מדוע בחרנו באוסף הקשרים המסוים הזה ולא אחר?

נדרוש שלמות פונקציונלית, כלומר, כל נוסחא ניתן •לבטא באמצעות הקשרים הלוגיים שבחרנו.

היא קבוצה שלמה פונקציונלית?{ /\, \/, ¬} האם •

שלמה פונקציונלית?{ /\, ¬} האם •

?}, F {ומה עם •

?}{ומה עם •

Page 36: Brief Introduction to Logic

שלמות פונקציונלית))1, 2, … , n-ארית. אזי קיימת נוסחה nתהי טבלת אמת

עבורה

)1, 2, … , n(,z( = TT)MPL)1,z(, MPL)2,z(, … , MPL)n,z(( MPL(

אפשר למעשה למצוא את הנוסחה ב או ב .

בטבלהXנתאר בתור את השורה ה-

TT בטבלה 0 עבור i ו 1 עבור iכאשר כל הוא הנוסחא הכללית תהיה

. 1כאשר הן השורות בטבלה שמחזירות = DNFלמעשה קיבלנו את הנוסחא ב Disjunctive Normal Form .

TT

, FWFF

,WFF

xC1 2(( ) ... )x mC l l l

il1 2... mi i ic c c

1, 2, ..., mi i i

Page 37: Brief Introduction to Logic

לדוגמא

:NANDטבלת אמת של •

TTNAND)0,0(=1

TTNAND)0,1(=1

TTNAND)1,0(=1

TTNAND)1,1(=0

. נקבלp2 ו-p1נניח שתתי הנוסחאות הן

¬(p1/\¬p2¬(/\)p1/\p2(/\)p1/\¬p2)ובאופן יותר כללי:

¬(1/\¬2¬(/\)1/\ 2(/\)1/\¬ 2 )

Page 38: Brief Introduction to Logic

בלבד אינה לוגיקה בעלת הסימן שלמה פונקצינלית

הוכחה:בתרגול•.עוד דוגמא: לוגיקה רק עם •. p0¬נניח שאנו רוצים לבטא את הפסוק • שקול לפסוק בו כל משתנה כל פסוק רק עם •

יופיע רק פעם אחת.אז הפסוק יהיה p0אם יופיע משתנה אחר מ- •

תלוי גם בערך המשתנה הזה, בניגוד לנוסחא ¬p0 .לכן לא יכול להופיע משתנה נוסף .

.p0¬ לא שקולה ל- p0אבל •

Page 39: Brief Introduction to Logic

קריאות יחידה

לדוגמא, יש לנו שפה שבה שתי מילים:•Z-ו ZZ

השפה נכתבת ללא רווחים.?ZZZ ? ZZZZכיצד להבין

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

בלוגיקה של פסוקים יש קריאות יחידה )יוכח בתרגול(.

Page 40: Brief Introduction to Logic

תלות במספר משתנים סופי

כל נוסחא פסוקית תלויה בהצבתה במספר משתנים •סופי, כלומר במשתנים המופיעים בנוסחא.

( )p2F(F(((F(p1p3 )))למשל:•.p1 p2 p3תלוי רק במשתנים

כיצד להוכיח? כי "רואים את זה"?•הוכחה באינדוקציה על אורך הנוסחא, או על סדרת •

הבניה שלה מתתי נוסחאות. z2 ו-z1אז ערך הנוסחא יהיה זהה עבור כל שתי הצבות •

.p1 p2 p3הנותנות ערך לוגי זהה לשלושת המשתנים מה עם נוסחאות שכלל לא תלויות בערכי הצבת האמת?•

Page 41: Brief Introduction to Logic

Tautologies

נוסחאות שלא תלויות בהצבת האמת.•דוגמאות:•

(p1)p2p1()((p1)p2p3()))p1p2()p1p3(()

הסכמות המתאימות אינן תלויות בתתי •הנוסחאות המוצבות:

((1)23()))12()13(()

Page 42: Brief Introduction to Logic

כיצד להוכיח שנוסחא היא טאוטולוגיה?

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

, עבור כל צרוף לוגי 1 ו-0 קומבינציות של n 2כמות העבודה: •של משתני הנוסחא )אם משתנה מופיע כמה פעמים

משתמשים באותו ערך לוגי(. האם ניתן להוכיח באופן יותר פשוט?•

יש כל מיני היוריסטיקות )אלגוריתמים שעובדים הרבה פעמים יותר טוב, אבל לא תמיד(, אבל בעקרון, זו אחת הבעיות

הפתוחות החשובות ביותר במדעי המחשב.מערכת הוכחה )נלמד בהמשך(. במקום זאת:•

Page 43: Brief Introduction to Logic

ספיקות של נוסחאSatisfiability

נוסחא היא ספיקה אם קיימת הצבת אמת •.1למשתניה הנותנת ערך לוגי

דוגמאות:•(p1\/p2)

כל טאוטולוגיה היא כמובן ברת סיפוק. האם •קיים קשר נוסף בין טאוטולוגיות ונוסחאות

ברות סיפוק?

Page 44: Brief Introduction to Logic

קשר בין טאוטולוגיות ונוסחאות ברות סיפוק

מהגדרתן,

היא ¬ שהיא טאוטולוגיה, עבור כל נוסחא 1.אינה ברת סיפוק, ולהיפך

היא ¬ שהיא אינה ברת סיפוק, לכל נוסחא 2.טאוטולוגיה.

Page 45: Brief Introduction to Logic

בדיקת ספיקות של נוסחא

שוב ניתן לבדוק את כל הצרופים הלוגיים של ערכי •משתני הנוסחא, לפי הסמנטיקה של הנוסחא וטבלאות האמת של הקשרים המופיעים בה.

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

לנוסחא.1לבדוק עם כל ההצבות נותנות ערך אמת נכון מספיק אומר שיש אבל האם העובדה ש"ניחוש"•

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

? )יורחב בקורס NP=Pהמחשב עשרות שנים, האם בחישוביות(.

Page 46: Brief Introduction to Logic

Substitution of variables by formulas

Let be a WFF. A function s:VarWFF is called a substitution function.

We define inductively a function subst),s( that replaces in all the variables pi by s)pi(.

Basis: subst)pi,s(=s)pi(, subst)F,s(=F, subst)T,s(=T.

Closure: subst))1/\ 2(,s(=)subst)1,s(/\subst)2,s((

subst))1\/2(,s(=)subst)1,s(\/subst)2,s((

Etc.

Page 47: Brief Introduction to Logic

For example

s={<p1,)p3\/p4(>,<p2,)p5\/p6(>}

subst))p1\/p2(,s(=)subst)p1,s(\/subst)p2,s((=

(( p3\/p4(/\)p5\/p6))

Finite dependency of substitution: Let be a WFF with all its propositions inside {p1,p2,…,pn}. Let s1 and s2 be two substitution functions such that for each i, s1)pi(=s2)pi(. Then subst),s1(=subst),s2(.

Page 48: Brief Introduction to Logic

Some propositions

If is not satisfiable and s is a substitution, then subst),s( is not satisfiable.

If is a tautology and s is a substitution, then subst),s( is a tautology.

Page 49: Brief Introduction to Logic

Propositional Logic Proof System

3שיעור

הוכחות בתחשיב הפסוקים

Page 50: Brief Introduction to Logic

מערכת הוכחהLyndon

אקסיומות )סכמות(:A1 ) )((A2 )) )(()) () (((A3 ))) F(F((

כלל היסק MP-Modus Ponens

Page 51: Brief Introduction to Logic

הוכחה

יכול להופיע:--|בכל שלב בהוכחה

.פסוק מ- 1.

אקסיומה.2.

MPאם בשורות קודמות הופיעו ההנחות של 3. יכולה להופיע.MPאז המסקנה של

נרשום בכל שורה גם את ההצדקה )אקסיומה או כלל היסק(.

Page 52: Brief Introduction to Logic

מה משמעות ההוכחה?

שפרושו כדלקמן:==|נסמן

לכל 1 הנותנת ערך אמת Zכל הצבת אמת . לפסוק 1, נותנת גם ערך הפסוקים שב-

1,2,...n הינה אוסף סופי של נוסחאות אם

( (1/\2/\.../\n))אזי ניתן היה גם לכתוב

את גוררת לוגית כי נאמר

==| הינה ההוכחה כי --|אזי

Page 53: Brief Introduction to Logic

דוגמאות להוכחות |--)(

1 .())(() A12( .()()()))()()(( )A23( .()())( )MP )1.,2.(4 .()() A15( . )MP )4.,3.(

A1 ) )((A2 )) )(()) () (((

Page 54: Brief Introduction to Logic

דוגמא להוכחה: שלילה גורפת}F --| {

1 .F assumption2( .F))F(F( )A1

3 .((F)F )MP)1.,2.(4( .((F)F) )A35 . MP)3.,4.(

בדומה, ניתן להוכיח כי, )F(}|--{

A1 ) )((

A3 ))) F(F((

Page 55: Brief Introduction to Logic

We repeat some notation and definitions.

An assignment z satisfies a formula if MPL), z(=1|== means that logically implies , i.e., each assignment that satisfies all the formulas in also satisfies .|-- means that we can prove from ,i.e., there is a proof sequence that uses assumptions from , the axioms A1, A2, A3 and the proof rule MP, and ends with .

Page 56: Brief Introduction to Logic

Lemma)*(: |== iff {F} is unsatisfiable.

One direction: from |==, any assignment that satisfies must also satisfy , thus cannot satisfy {F}.

Conversely, if {F} is unsatisfiable, any assignment that satisfies cannot satisfy {F}, hence it must satisfy .

Page 57: Brief Introduction to Logic

מונוטוניות

מקבוצת אם ניתן להוכיח פסוק מסוים • גם מכל קבוצה אז ניתן להוכיח את פסוקים

.המכילה אתהוכחה: אפשר פשוט לחזור על כל סידרת ההוכחה,

מבלי להשתמש בפסוקים הנוספים.

להלן נשתמש במשפט כאשר כבר הוכחנו גרירה •מתוך קבוצה של פסוקים נוכל להגדיל את

הקבוצה כרצוננו.

Page 58: Brief Introduction to Logic

משפט הדדוקציה

|-- if and only if {}|-- כוון משמאל לימין:

|-- אם את MP אז ניתן להסיק על ידי נתון וגם כעת בנוסף ל-

.כוון מימין לשמאל:

ונראה {}|-- נסתכל על סדרת הוכחה של שכל שורה שבה באינדוקציה על אורך סידרת ההוכחה

. מבלי להשתמש בהנחה )) ניתן להוכיח הוכחנו

Page 59: Brief Introduction to Logic

הוכחת דדוקציה לפי מקרים =)שימוש בהנחה הנוספת(

ואת זה כבר הוכחנו.אזי נדרש להוכיח או .הינה שימוש באקסיומה

אובהנחה ))1A : ))נשתמש בסכמה .MP על ידי שימוש ב () ונסיק את אקסיומה

התקבל על ידי שימוש ב MP משורות שבהן (. , ו-)מופיע

, )) ( ) ( )אז באינדוקציה ניתן להוכיח )))2A: )) ) (()) ()נשתמש ב

.() פעמיים ונקבל MPנשתמש כעת ב

Page 60: Brief Introduction to Logic

כעת מותר להשתמש בהוכחות במשפט הדדוקציה

פישוט הוכחות: כל פעם שנרצה להוכיח טענה מסוג |-- נוכל במקום זאת להוסיף את

במקום.להנחות ולהוכיח את חשוב שבכל שלב נזכור תחת איזו קבוצת הנחות

מוכיחים )רצוי לסמן את הקבוצה בצעד ההוכחה, אם לא לחלוטין ברור(.

נוכיח מספר משפטים נוספים שיעזרו לפשט הוכחות שאסור ונשתמש בהם )חוץ מאשר עם נאמר

, למשל במבחן(.להשתמש

Page 61: Brief Introduction to Logic

דוגמאות להוכחות: טרנזיטיויות(}( ,)( --| {))

על פי משפט הדדוקציה, ניתן להוכיח באופן שקול:

(}( ,) ,) --| { 1 .

2( .)

3 . MP)1.,2.(

4( .)

5 . MP)3.,4.(

Page 62: Brief Introduction to Logic

קונטרפוזיציהדוגמאות להוכחות:(}((} --| {)F))F( {)

אז ניתן להסיק( )שזה כמו להוכיח שאם נתון¬( ¬ )

על פי משפט הדדוקציה, ניתן להוכיח באופן שקול:(}( ,)F ,) --| {F

1 .2( .)

3 . MP )1.,2.(4( .F)

5 .F MP )3.,4.(

We did not use A3!

Page 63: Brief Introduction to Logic

:הוכחה לפי מקרים{}|--, {F}|-- then |--

1. |-- ) ( deduction2. --|)F) deduction3. --|)))F(F(( A34. --|)))F(F(( transitivity )3.,1.(5. --|))F())F(F(( contrapositive )2.(6. --|))F(( transitivity )5.,4.(7. --|))F())F(( idempotence.8. --|)))F()F(()))F(())F(F((( A29. --|)))F(())F(F(( MP )7.,8.(10. --|))F(F( MP )6.,9.(11. --|)))F(F(( A312. --| MP )10.,11.(

Page 64: Brief Introduction to Logic

נאותות ושלמות

אם ניתן להוכיח רק גרירות נכונות, כלומר נאותהמערכת היא • .==| אז --|אם

אם ניתן להוכיח כל גרירה, כלומר אם שלמהמערכת היא •==| אז --|.

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

תמיד מתקיימות )אפשר לבדוק על ידי טבלאות האקסיומות:אמת(.

משמר נכונות: אם ההנחות שלו מתקיימות, MPכלל ההוכחה אזי ההסקה שלו מתקיימת.

לכן כל שורה בסדרת ההוכחה הינה נכונה טאוטולוגית או נובעת מהנחות ההוכחה.

Page 65: Brief Introduction to Logic

אי תלות של אקסיומותהאקסיומות לא תלויות אחת אחת בשניה. אם נוריד •

אקסיומה, ישאר כלל הוכחה לא שלם ולא ניתן להוכיח בעזרת האחרות את האקסיומה שהורדנו.

.2A ו-1A אינה תלויה ב-3Aלדוגמא, •. נסתכל על לוגיקה דומה לשלנו, 3Aהוכחה. נוריד את •

(. T )בדומה ל-1 מקבל את הפרוש הסמנטי Fאבל MP וכלל ההיסק 2A ו-1Aבלוגיקה הזו, האקסיומות

עדיין תקפים, וניתן להסיק בהם רק משפטים שפרושם בשתי הלוגיקות. )טאוטולוגיות(1הסמנטי הוא

מיתר האקסיומות וכלל A3אם הינו יכולים להוכיח את • היתה טאוטולוגיה גם בלוגיקה החדשה.A3ההוכחה, אז

אינה טאוטולוגיה בלוגיקה הזו:3Aאבל •( ((F)F)))))T(T((

Page 66: Brief Introduction to Logic

הוכחת שלמות )קשה!(.--| אז ==|רוצים להוכיח כי אם •.=/=| אז -/-|באופן שקול נוכיח כי אם •-/-|נניח •נסדר את כל המשפטים של לוגיקת פסוקים לפי סדר •

מסויים )לפי אורך, ואלפבתי, כמו בספר טלפונים כלפי נוסחאות מאותו אורך(

0, 1, 2.… ,נבנה סדרה הולכת וגדלה של קבוצות פסוקים•

=0 12.…:כדלקמן i { i = i+1אזי { -/-|{ i i }אם

i = i+1אחרת

….*=0 12נגדיר •

Page 67: Brief Introduction to Logic

*תכונות של

-/-|* .מדוע?

.* נמצא ב אז *--|אם • i אז היתה קבוצה * לא נמצא ב כי אם

--|{ i }כך ש- . בסתירה למה שכתוב * --| אזי *--|אבל מכיון ש

מעל.

Page 68: Brief Introduction to Logic

*תכונות של ( F ) וגם , לא יתכן שגם עבור פסוק כלשהו •

.* שניהם ב-נמצאיםאחרת, באיזה שלב, שתי הנוסחאות היו נמצאות, הוכחה:

ומכאן להוכיח כל דבר... F היה ניתן להוכיח MPלפי *. ב-נמצאים( F ) או, או עבור פסוק כלשהו •

. * נמצאים ב אינם i (F=)ו- =k הוכחה: נניח כי ומכאן לפי --|{ k } וגם --|{)F( i }אזי

|--) max)i,k"הוכחה לפי מקרים" ומונוטוניות נובע כי בסתירה לבניה.

אבל לא *כלומר, כל פסוק או השלילה שלו נמצאים ב- •שניהם.

Page 69: Brief Introduction to Logic

*תכונות נוספות של

•Fאינה נמצאת ב * ניתן היה להוכיח מ- אחרת* כל דבר.

• () -נמצא ב * אם ורק אם ( או (F נמצאים .*ב-

אינם נמצאים F) וגם ). אם נמצא ב- *()נניח כי –אבל אז מתקיים .* נמצאים ב- וגם F) אזי )* ב-

}, )(, )F(--|{ F ..סתירה.* נמצאת ב () אזי גם * נמצאת ב אם–

.MP( ו- ))1A ) הוכחה:שימוש ב-. *. נמצאת ב ()גם אזי* נמצאת ב (F( אם–

* |-- ()הוכחה: נראה ש (F( כהנחה נוספת, יחד עם שימוש בדדוקציה מעביר את

.לכן לפי דדוקציה, ניתן להוכיח כל דבר, וביחוד MPו () --| *

Page 70: Brief Introduction to Logic

מכאן...

מתנהגת כסמנטיקה של *קבוצת הנוסחאות • 1הנוסחאות תחת הצבת אמת. היא נותנת ערך אמת

אחרת.0אם נוסחא נמצאת בקבוצה ו-ההצבה תלויה בנוסחאות האטומיות, שכל אחת יכולה

או ששלילתה שייכת.*להיות שייכת ל- כפי שהראינו בתכונות השקף הקודם, בניה זו

קונסיסטנטית עם נוסחאות בנויות היטב.* נמצאות ב- כל הנוסחאות של •. * אינה יכולה להיות שייכת לקבוצה הנוסחא •

להצבה שנותנת ערך אמת 0 מקבלת ערך אמת לכן .=/=| לכן . המכילה את * לכל הנוסחאות ב- 1

Page 71: Brief Introduction to Logic

לוגיקה מסדר ראשון

Page 72: Brief Introduction to Logic

מהי לוגיקה מסדר ראשון?

מאפשרת משתנים מעל מבנה מתמטיהכולל:

,תחום מסויים, למשל השלמים, מחרוזותהטבעיים, צמתים בגרף....

בפונקציות ורלציות )=יחסים(, למשל יחסיםמתמטיים >, <, קשתות בגרף...

מאפשרת להשתמש בכמתים לכל וקיים

Page 73: Brief Introduction to Logic

:משמעות , E Vמבנה של גרף

:"מוגדר על קבוצה של צמתים ה"עולםVהיחסEVV אוסף של זוגות מסודרים(

מעל העולם( מתאר את הקשתות בגרף(v1,v2)E-אם יש קשת מ v1-ל v2.

:לדוגמא

V={1,3,5,12,15,27} E={(1,3),(1,5),(3,12),(5,15),

(12,1),(15,3)}

Page 74: Brief Introduction to Logic

המספרים הטבעיים והאריתמטיקה

המבנהNat:N, +N, N, <N, 0N, 1N

N 0המספרים הטבעיים, כולל - +N

: N N N פונקצית החיבור מעל הטבעיים - xN

: N N N פונקצית הכפל מעל הטבעיים - <N N N היחס "קטן מ-" מעל הטבעיים - 0NN הקבוע "אפס" הטבעיהערך

1N N הקבוע "אחד" הטבעי

Page 75: Brief Introduction to Logic

המספרים הממשיים והאריתמטיקה

המבנה Real:R, +R, R, <R, 0R, 1R

Rהמספרים הממשיים - +R

: R R R פונקצית החיבור מעל הממשיים - xR

: R R R פונקצית הכפל מעל הממשיים - <R R R היחס "קטן מ-" מעל הממשיים - 0RR הקבוע "אפס" הממשי

1R R הקבוע "אחד" הממשי

Page 76: Brief Introduction to Logic

מבנה (סמנטיקה)ו"מילון")תחביר(

A, fA , gA , rA , cA , dA הסמנטי שייך לחלקA ה"עולם" מעליו מדברים – קבוצה

fA , gA פונקציות

rA רלציות

cA , dA קבועים סימנים עבור פונקציות, בתחבירובמקביל, יש

F-ו G סימן עבור הרלציה ,R סימנים עבור ,.D ו-Cקבועים

Page 77: Brief Introduction to Logic

נגדיר את שפת לוגיקה מסדר ראשון

נגדיר אוסף של סימני רלציהRn,m כאשר nהוא ה arity- )מספר הפרמטרים(

מספר סודר.mו- נגדיר אוסף של סימני פונציהFn,m

מספר סודר.m ו-arity הוא הn כאשר נגדיר אוסף של סימני קבועCm

(.0 של arityפונקציות עם )קבועים: נגדיר אוסף של משתניםVm

Page 78: Brief Introduction to Logic

דברים שנרצה לבטא בלוגיקה:

.לכל צומת בגרף יש בדיוק שני שכנים.יש אינסוף מספרים ממשיים בין כל שני מספרים רציונלים יש מספר

רציונלי נוסף.

.אנו נגדיר שפה שבה נוכל לבטא את כל אלו

Page 79: Brief Introduction to Logic

תחביר

נגדיר אותו בשני שלבים

ביטויים

נוסחאות

Page 80: Brief Introduction to Logic

termsנגדיר קודם כל ביטויים :המשתנים וסימני הקבוע הם בסיסterms. אםt1, t2, ... tn-הם ביטויים ו Fn,m הינה פונצקיה

אז arity-nעם Fn,m(t1, t2, … tn).הינו ביטוי

.)לביטויים "קריאות יחידה" )לא נוכיח כאן

Page 81: Brief Introduction to Logic

דוגמאות

נסתכל על מבנה של מספרים )ממשיים, שלמים, מרוכבים(. נכתוב את סימני הפונקציה והרלציה

.arityעם אינדקס שיסמן את השימוש, וללא ה-v7, c0, c1, v13,

F+(v7,c0), Fx(c1,v17),Fx(F+(v7,c0),Fx(v1,v17))

דוגמאות לכתיבה לא נכונה של ביטויים:R<(v7,c1), (v7+c1), (v7xc1)

Page 82: Brief Introduction to Logic

יותר מאוחר...

:נרשה לעצמנו להשתמש בסימונים הרגילים* ,+ ,<

כרגע לא נעשה את זה, מכיון שאנו רוציםלהבחין בין הסימן התחבירי עבור + לבין

הפונקציה חיבור. בדרך כלל, באופן לא כדי לציין את פורמלי משתמשים בסימן +

שניהם. "כשנדע להבחין, נוכל לבטל את ה"פורמליות

הכבדה.

Page 83: Brief Introduction to Logic

קשרים לוגיים, כמתים ושוויון

בנוסף לסימונים במילון, ניתן להשתמשבלוגיקה מסדר ראשון גם

בקשרים לוגיים /\ )או(, \/ )וגם( וכולי,הכמת )קיים: בכמתים:

האקסיסטנציאלי(, ,)לכל, הכמת האוניברסלי(

)נסמן אותו בשקפים באופן =ובסימון השוויון כדי להבהיר שזה סימון בשפה(.מודגש

Page 84: Brief Introduction to Logic

נוסחא של לוגיקה מסדר ראשון נוסחאות אטומיות כבסיסנגדיר קודם:

נוסחא אטומית מתקבלת על ידי הפעלת רלציהRn,m(t1, t2, …, tn) על ביטויים t1, t2, ... tn

. t1=t2או על ידי

מתנהג גם הוא כמו רלציה =הערה: כפי שרואים, הסימן – בינארית. הסיבה שהשימוש בו נפרד הינו כי אנחנו=רוצים להבטיח, כאשר ניתן את המשמעות, שהסימן

מתנהג כרלצית השוויון הסטנדרטית.

Page 85: Brief Introduction to Logic

כעת נגדיר נוסחאות כלליות

:נוסחא אטומית היא נוסחא בסיס:אם אינדוקציה1-ו 2-הן נוסחאות ו viהוא

משתנה אז גם(1/\2) ,(1\/2) ,1, vi1, vi1 הן

נוסחאות.

Page 86: Brief Introduction to Logic

דוגמאות

v3 ( F2,1(v1,v3)=v2/\ (v3=C1/\v3=v2) )

v1 v3 (F2,1(v1,v3)=v2/\ (v3=C1/\v3=v2) )

Page 87: Brief Introduction to Logic

קישור בין תחביר למשמעות

.בינתיים יש לנו מבנים ומילון וביטויים,נשייך לכל סימון במילון אלמנט )כמו פונקציה

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

שתתן לכל משתנה ערך מעל התחום של המבנה.

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

Page 88: Brief Introduction to Logic

פונקצית השמה או הצבה (assignment.)

כמו שהיתה לנו השמה בלוגיקה של פסוקיםשנתנה ערך לכל משתנה פסוקי, כך תהיה לנו

פונקצית השמה פה. הפונקציהz מחזירה ערך לכל משתנה מתוח

העולם )תחום הערכים של המבנה הנדון(.דוגמא, אם העולם הוא של מספרים טבעים

z(v1)=1, z(v2)=100,…,z(v7)=111 ,… השמה אחרת מעל אותו עולם:

z(v1)=13, z(v2)=8,…,z(v8)=1 ,…

Page 89: Brief Introduction to Logic

נפרש סימני יחס, פונקציה, קבוע Pמעל מבנה. נניח פירוש קבוע

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

P(F1,1)=“sin”, P(F2,1)=“+”, P(R2,1)=“<“, P(C1)=“0”.

P)F2,1)=“+”, P(F2,2)=“*”, P(R2,1)=“>“, P(c1)=“1”. כתבנו למשלsin:הסימון במרכאות כי הכוונהF1,1 מתפרש

כפונקצית הסינוס הידועה," מתפרש 0הידוע, ו" מתפרש כיחס "קטן מ"R1,1הסימון

ולא כסימן "אפס". כערך "אפס"

Page 90: Brief Introduction to Logic

נוכל עכשיו לתת פירוש סמנטי MT לביטויים מעל מבנה S

zוהצבה

MT(t, P, z) מוגדר בצורה רקורסיבית MT(vi, P, z) = z(vi) עבור משתנה

MT(Ci,P, z) = P(Ci) עבור קבוע

MT(Fn,m(t1, t2, …, tn),P, z)= P(Fn,m) (MT(t1,P, z), … MT(tn, P, z))

Page 91: Brief Introduction to Logic

דוגמא:נסתכל על ההשמה הבאה

z(v0)=0, z(v1)=2, … z(vi)=2i, …P)F2,0)=“” P(F2,1)=“+” P(C1)=1 נניח:

Nat נסתכל על המבנה של הטבעיים MT(F2,0(v2,F2,1(v3,C1)), P, z)= P(F2,0)(z(v2), P(F2,1) ( z(v3), P(C1)))

z(V2)= 4 P(C1)=1 z(v3)=6.P)F2,0)=“” P(F2,1)=“+” z(v2)=4.P(F2,1) ( z(v3), P(C1))= 6+1=7.P(F2,0)(z(v2), P(F2,1) ( z(v3), P(C1)))=47=28.

Page 92: Brief Introduction to Logic

מעל מבנה של נוסחא Mמשמעות S והצבה z קודם עבור נוסחאות .

אטומיות

M(Rn,m(t1,t2,…tn), S, z)= (MT(t1, P, z), …, MT(tn, P, z))P(Rn,m)

M(t1 = t2, P, z) = MT(t1, P, z ) = MT(t2, P, z)

סימני שוויון, מה משמעותם?3שימו לב, יש פה

Page 93: Brief Introduction to Logic

דוגמא

M (R2,0(C0, F2,0(v2,F2,1(v3,C1))), P, z)=???

P(C0)=0, P(R2,0)=“<“ נניח

MT) F2,0(v2,F2,1(v3,C1)), Nat, z) =28

MT(C0, Nat, Z) = P(C0) = 0.

M(…, Nat, z) = (0, 28)< (= true).

Page 94: Brief Introduction to Logic

נגדיר יחס בין הצבות

שונות לכל היותר לגבי הערך z2 וz1ההצבות viשהן נותנות למשתנה

z1 =i z2

Page 95: Brief Introduction to Logic

אחרי דרך ארוכה...משמעות של נוסחא מסדר ראשון

M((1/\2), P, z)= TT/\(M(1, P, z), M(2, P, z))

M((1\/2), S, z)= TT\/(M(1, P, z), M(2, P, z))

M(1, P, z)= TT(M(1, P, z))

M(vi1, P, z)=Exists z’, z’ =i z such that M(1, P, z’)

M(vi1, P, z)=For each z’, z’ =i z, M(1, P, z’)

Page 96: Brief Introduction to Logic

דוגמאות. נפרש P(F2,1)=“” P(C1)=1

v3 (F2,1(v1,v3)=v2/\ (v3=C1/\v3=v2) )

v1 מחלק את v2

v1 v3 (F2,1(v1,v3)=v2/\ (v3=C1/\v3=v2) )

v2 .ראשוני

Page 97: Brief Introduction to Logic

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

v1v2 (R<(v1,v2) v3(R<(v1,v3) /\ R<(v3,v2)))

?מה המשמעות

המשמעות – תלויה במבנה, האם בכל המבנים?trueהמשמעות תמיד

Page 98: Brief Introduction to Logic

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

v1v2 (R<(v1,v2) v3(R<(v1,v3) /\ R<(v3,v2)))

< שהינו סדר מלא )כל שני במבנה עם יחס מספרים סדורים אחד יחסית לשני(, המשמעות הינה

שבין כל שני מספרים יש מספר שלישי Density.

הערך הלוגי של המשפט הינוtrue עבור המספרים עבור המספרים falseהממשיים או הרציונלים, ו-

הטבעיים או השלמים. וגם, אין הכרח שהסימוןR> יתאים לסדר מלא או

אחר!יכול להיות שהוא מיצג יחס האומר שהפרמטר

הראשון מחלק את השני.

Page 99: Brief Introduction to Logic

משתנים חופשיים וקשורים

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

.חופשייםאחרת הם .ההגדרה המדויקת הינה רקורסיבית נניח שמוגדרvar(t) המשתנים של –

tהביטוי (.var(t)להגדיר את )רעיון טוב לתרגיל בית:

Page 100: Brief Introduction to Logic

הגדרה פורמלית של משתנים חופשיים:

free(t1=t2)=var(t1)var(t2) free(Rn,m(t1,t2,…,tn)=var(t1) … var(tn) free(\/)=free() free() free(/\)=free() free() free()=free() free(vi )=free()\{vi} free(vi )=free()\{vi}

Page 101: Brief Introduction to Logic

דוגמאv3 (F2,1(v1,v3)=v2/\(v3=C1/\v3=v2))

var(F2,1(v1,v3))={v1,v3}

free(v3=C1)={v3}

free(v3=v2)=free(v3=v2)={v2,v3}

free((v3=C1/\v3=v2))={v2,v3}

free((F2,1(v1,v3)=v2/\(v3=C1/\v3=v2)))={v1,v2,v3}

free(v3(F2,1(v1,v3)=v2/\(v3=C1/\v3=v2)))={v1,v2}

Page 102: Brief Introduction to Logic

עוד על משתנים קשורים וחפשיים

כל הופעה של משתנהvi בנוסחא בתוךvi.הינה הופעה קשורה

אתגר לבית: הגדר את קבוצת המשתנים.bound()הקשורים בנוסחא

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

(R2,2(v0,v1)/\v1R2,0(v0,v1)).v1אין קשר בין שני המופעים השונים של

Page 103: Brief Introduction to Logic

הופעה חוזרת ונשנית של משתנה אין קשר בין המופעים קשור:

v1(R(v0,v1)/\ v2(R(v1,v2)/\ v1(R(v2,v1)/\v2(R(v1,v2)/\

R(v2,v3)))))-נניח שR לא כתבנו את האינדקסים(

שיש צמתים בגרףהפעם...( היא רלציה מעל ביניהם קשת.

v0 v3

Page 104: Brief Introduction to Logic

מערכת הוכחה ללוגיקה מסדר ראשון

פרופסור דורון פלד

אוניברסיטת בר אילן

Page 105: Brief Introduction to Logic

מערכת הוכחה של לוגיקה מסדר ראשון

נחזור למערכת הלוגיקה שלנו שבה(F( הינו קיצור עבור¬. :קיצור נוסף

vi¬vi¬.

Page 106: Brief Introduction to Logic

סימון

φ)x( או יותר פעמים המשתנה 0 היא נוסחא שבה term הינו ה)φ)t אזי term הוא t. אם xהחופשי

xהמתקבל על ידי החלפת כל המקומות שבהם .φמופיע חופשי ב-

:)אם קל להוכיח )באינדוקציה מבניתt אינו מכיל MT(t,P,z)=MT(x,P,Z)אז אם )φ)x משתנה הקשור ב

אז גם M(φ(x),P,z)=M(φ(t),P,z).

Page 107: Brief Introduction to Logic

הוכחה וגרירה לוגית

|--. להוכיח את אפשר מההנחות

|==

trueלכל מבנה ותחת כל השמה הנותנים ערך אמת . ל- true ניתן ערך אמת לכל הנוסחאות ב-

Page 108: Brief Introduction to Logic

אקסיומות

1. (())

2. ((())(()()))

3. (((F)F))

4. (v()(v))

. אינו חופשי ב-vכאשר 5. v)v()t(

. אשר מופיע קשור ב--tכאשר אין משתנה ב

Page 109: Brief Introduction to Logic

מדוע ההגבלה על האקסיומה הרביעית?

1. (v()(v)) אינו חופשי ב-vכאשר

ניקח את המשפט הבא:v )R)v(R)v(()R)v(v R)v((

הינו זוגי.v פרושו ש-)R)vנניח ש אם כשמספר זוגי אז הוא זוגי )תמיד אז המשפט אומר:

זוגי, אז כל המספרים הם זוגיים...vנכון(, אז כש-

Page 110: Brief Introduction to Logic

מדוע ההגבלה על האקסיומה החמישית?

(v )v()t()

אשר מופיע קשור ב--tכאשר אין משתנה ב

ניקח את המשפט )האומר: יש לפחות שני ערכים שונים(

)v w )v=wF) )w )w=w(F))

)w )v=wF והוא קשור ב-tמיצג את wכאן

Page 111: Brief Introduction to Logic

אקסיומות נוספות עבור שוויון

Page 112: Brief Introduction to Logic

כללי הוכחה

MP )Modus Ponens(

GEN )Generalization()v(x )x(

אינו מופיע חופשי באף אחת מההנחות vכאשר

Page 113: Brief Introduction to Logic

?GENמדוע ההגבלה על

v=0ההנחה:

v v=0 ללא ההגבלה נותן GENשימוש ב-

Page 114: Brief Introduction to Logic

הוכחה

כל שורה יכולה להיות:הנחה מ-•אקסיומה• מופעל על תוצאה של GEN או MPכלל הוכחה •

שורות קודמות.

Page 115: Brief Introduction to Logic

דוגמא

{vw )v,w(} |- wv )v,w( vw )v,w( Assumption vw )v,w( w )v,w( A5: )v)v()t(( w )v,w( MP )1+2( w )v,w( )v,w( A5 )v,w( MP )3+4( v )v,w( GEN )5( w v )v,w( GEN )6(

Page 116: Brief Introduction to Logic

סימטריה דוגמא:

v=w |- w=v

1. v=w Assumption

2. v=w)v=vw=v( A7 φ)v,w( is w=v

3. )v=vw=v( MP )1+2(

4. v v=v A6

5. v v=v v=v A5

6. v=v MP )4+5(

7. w=v MP )6+3( A5 v)v()t( A6 v v=vA7 )v=w( )φ)v,v(φ)v,w((

Page 117: Brief Introduction to Logic

טרנזיטיביות דוגמא:

{u=v, v=w} |- u=w

... use previous proof … u=v |- v=u

1. v=u … previous proof…

2. )v=u)v=wu=w(( A7, A)v,u(=u=w

3. (v=wu=w) MP )1+2(

4. v=w Assumption

5. u=w MP )3+4(

A7 v=u )φ)v,v(φ)v,u(( בחלוף שמות

Page 118: Brief Introduction to Logic

משפט הדדוקציה

|-)( If and only if {} |-

מתקיים גם כאן )הוכחה דומה לתחשיב הפסוקים(

Page 119: Brief Introduction to Logic

Completeness and Expressiveness

Page 120: Brief Introduction to Logic

תזכורת למערכת ההוכחה של אקסיומות לוגיקה מסדר ראשון:

1. ))((2. )))(())()(((3. )))F(F((4. )v)()v((

v אינו חופשי ב- כאשר

5. v(v)(t). אשר מופיע קשור ב--tכאשר אין משתנה ב

Page 121: Brief Introduction to Logic

אקסיומות נוספות עבור שוויון. נחזק אותן עבור הוכחה יותר

קלה...

6 t t=t7 (t1=t2) ((t1) (t2))

מתקבלות מנוסחא משותפת(t1)ו- (t2)כאשר (v)על ידי החלפת )חלק מ(המקומות החופשיים

, בהתאמהt2 או t1ב v של לא נקשר לכמת t2 או t1כאשר אף משתנה ב

. ב-

Page 122: Brief Introduction to Logic

כללי הוכחה MP (Modus Ponens)

GEN (Generalization)(v)x (x)

אינו מופיע חופשי באף אחת מההנחות vכאשר

Page 123: Brief Introduction to Logic

Goal: we want to prove Our first order logic proof system is sound

and complete. That is: |-- if and only if |==

Soundness: |-- implies |== Check that the axioms are tautologies.Check that the proof rules preserves truth.Then, by induction on length of proof any model and assignment that satisfy , satisfies any line in a proof, in particult the last one .

Page 124: Brief Introduction to Logic

Consistency: a syntactic definition, related to the proof system.

A set of formulas is consistent if there is no formula such that |-- and |--(F).

Equivalent definition: |-/-F. Equivalently: is inconsistent if for

each , |--.

Page 125: Brief Introduction to Logic

Lemma |-- iff {(F)} is inconsistent. Proof: if {(F)} is inconsistent

then {(F)} |--. Also, {} |-- .(why?) Therefore, by “proof by cases”, |--.Conversely, if |-- then from {(F)} we can prove F (using MP), thus {(F)} is inconsistent.

Consequence: |-/- iff {(F)} is consistent.

Page 126: Brief Introduction to Logic

More connections

If is consistent then either {(F)} or {} is consistent.Proof: if both are inconsistent, then we can prove from {(F)} and from {}. Hence, we can prove using “proof by cases” from . Contradiction.

is consistent iff every subset of it is consistent.

Page 127: Brief Introduction to Logic

More connections

|== iff {(F)} is unsatisfiable(why?)

If is satifiable then either {(F)} or {} is satisfiable.

Page 128: Brief Introduction to Logic

is consistent is satisfiable.

Easy direction: If is inconsistent, given the soundness of the proof system, its is unsatisfiable. (why?)

Converseley: From consistent we form maximal consisten as follows (hard proof).

Page 129: Brief Introduction to Logic

is consistent is satisfiable

.נראה את המקרה של מספר נוסחאות בר מניה נסדר את כל הנוסחאות לפי סדר מסויים )אלפבתי, כמו

בספר טלפונים( 0, 1, 2.… ,

נבנה סדרה הולכת וגדלה של קבוצות נוסחאות=0 12.…:כדלקמן

i { i = i+1 קונסיסטנטית אזי {{i i אם { .i = i+1אחרת

נגדיר*=0 12.… * היא קונסיסטנטית. כי אם אפשר להוכיח ממנה כל

, ולכן אפשר להוכיח את זה גם Fדבר, אפשר להוכיח גם מתת קבוצה סופית שלה, בסתירה לבניה.

Page 130: Brief Introduction to Logic

*תכונות של

עבור נוסחא כלשהי לא יתכן שגם , וגם ( F ) .* שניהם ב-נמצאיםאחרת, באיזה שלב, שתי הנוסחאות היו הוכחה:

נמצאות, והיינו מקבלים קבוצה לא קונסיסטנטית... עבור נוסחא כלשהי או ,או ( F )ב-נמצאים .*

. * נמצאים ב אינם i (F=)ו- =k הוכחה: נניח כי ומכאן לפי F --|{ k } וגם F --|{(F) i }אזי

|--F max(i,k)"הוכחה לפי מקרים" ומונוטוניות נובע כי בסתירה לבניה.

-כלומר, כל נוסחא או השלילה שלה נמצאים ב* אבל .* נמצא ב אז --| לא שניהם. לכן גם: אם

Page 131: Brief Introduction to Logic

*תכונות נוספות של

Fאינה נמצאת ב * () -נמצא ב * אם ורק אם ( או (F

.*נמצאים ב-n נניח כי()* -נמצא ב אם .( וגם (F אינם

אבל אז .* נמצאים ב- וגם F) אזי )* נמצאים ב-מתקיים

}, (), (F)--|{ F ..סתירהnאם נמצאת ב * אזי גם () נמצאת ב *.

.MP( ו-1A ()) הוכחה:שימוש ב-nאם )F) נמצאת ב * אזי גם () נמצאת ב *.

* |-- ()הוכחה: נראה ש כהנחה נוספת, יחד עם שימוש בדדוקציה מעביר את

)F).ניתן להוכיח כל דבר

Page 132: Brief Introduction to Logic

Problem If we have in * a formula x (x ), we want to have

(t )* for some term t. In fact x (x ) cannot appear in *.

Instead we will have (x ((x ) F)F)

Then * is closed under withnesses. This may not hold. So we’ll add for each such formula a new variable y

and also add (y). [This may extend the set of variables!]We obtain 1 and now perform the * closure on it again to obtain 1

* We obtain like that now 2

* 3* ...

We take #= i*

What we proved before holds also for #

Page 133: Brief Introduction to Logic

Show that is # consistent.

Suppose, by contradiction that when we add (y) to the current set P we obtain an inconsistent set. Thus, P{(y)} |-- F.

From deduction we obtain P |--)(y )F) From GEN we obtain P |-- y (y)F,

which is in contradiction to (x ((x ) F)F), which is already in the set of formulas.

Page 134: Brief Introduction to Logic

We now have “closed under withnesses”

x (x ) =(x ((x ) F)F) is in # iff there is also there some t such that (t ).

Left to right direction, from construction, by adding the new varaible.

Right to left direction:Suppose that we have (t )#. Assume for the contradiction that the negation, x ((x ) F) is # . Then using A5 we havex ((x ) F) ((t) F) and using MP we have ((t) F), which cannot be consistent with (t )#.

Page 135: Brief Introduction to Logic

מכאן...

קבוצת הנוסחאות# מתנהגת כסמנטיקה של הנוסחאות תחת הצבת אמת.

הערך שקבועc מקבל הוא הסימן c.הערך שמשתנהv מקבל הוא הסימן v. באינדוקציה, הערך שביטויt מקבל הוא t. יחסR(t1,t2,...)מתקיים אם הנוסחא

R(t1,t2,...) -נמצאת ב #. כל הנוסחאות של -נמצאות ב # ,כמו במקרה של לוגיקת פסוקים, הטענות הקודמות מראות

באינדוקציה על מבנה הנוסחא שהצבה זאת מתנהגת כהצבה לוגית.

לכן, אם קבוצת נוסחאות הינה קונסיסטנטית, יש לה מודל)מבנה והצבה שמספקים אותה(.

Page 136: Brief Introduction to Logic

Continuing... Suppose now we also have equivalence. What happens when we have t1=t2? We take as an interpretation of a

constant c all the equivalence class of all the terms t such that c=t holds. We denote this by [c] (or, [t]).

Using the axioms A6 and A7 it follows that this definition is consistent, i.e., that if t1=t2 then (t1) is in # iff (t2) is in #

Page 137: Brief Introduction to Logic

Completeness follows

We want to prove that |== implies |--.Equivalently: |-/- implies |=/=.

|-/- implies that {(F)} is consistent.

Therefore, {(F)} is satisfiable.

Thus, |=/=.

Page 138: Brief Introduction to Logic

Therefore:

Recall: |-- if and only if there is some finite 0 such that 0|-- (why?)

Thus: |== if and only if there is some finite 0 such that 0|== (why?)

This is called “Compactness theorem”.

Page 139: Brief Introduction to Logic

Therefore:

Alternatively: is satisfiable iff every finite subset of it is satisfiable: is satisfiable iff is consistent iff |-/-F iff for every finite subset 0 of , 0|-/-F iff for every finite subset 0 of , 0|=/=F iff every finite subset of is satisfiable.

Page 140: Brief Introduction to Logic

We can express some classes of structures (why not

sets?)

1. xR(x,x)2. xy(R(x,y)R(y,x))3. xyz ((R(x,y)/\R(y,z))R(x,z))We can write this using one formula!This defines the structures with R

being an equivalence relation!

Page 141: Brief Introduction to Logic

Provability Suppose we have a class of structures C and we

want to prove using our proof system theorems of this class.

Suppose we can write a set of formulas such that a structure S is in that class iff S satisfies all the formulas in (with having no free variables).

Then every property of the structures in C satisfies that |==. (also, every property that satisfies |== is a property of C).

Proof: every structure that satisfies all the formulas in is in C and therefore satisfies .

Since we have completeness, |--, thus having allows us to prove .

Page 142: Brief Introduction to Logic

A class of structures with infinite set of elements

Let i be a sentence that says that we have at least i elements:2 = xy(x=y), 3 = xyz (((x=y)/\(y=z))/\(x=z))…etc.

Call these collection of sentences .

Page 143: Brief Introduction to Logic

Some inexpressiveness of first order logic We want to say that we have a finite number

of elements in our domain. Suppose (for the contradiction) that we

express exactly that using . Now, take and any finite subset of . It is

satisfiable (by a structure with at least as many element as the highest index in the subset.

Thus, every subset of is also satisfiable. By compactness, is also satisfiable – but it

has an infinite model!!! Contradiction.

Page 144: Brief Introduction to Logic

Having an algorithm for doing the proof?

If |== then by completeness |--. We can enumerate all the possible

proofs from . Say, each time to the i th step. Given enough memeory and time, if there is a proof, it would come out(recursive enumerable).

On the other hand, one can write a set of formulas describing the behavior of a model equivalent to Turing machines, and a formula describing termination. Therefore, proving |-- is not decidable.

Page 145: Brief Introduction to Logic

The big picture For some classes of structures C we can find a set of

first order formulas that are satisfied exactly by structures in C.

For some other structures C, there cannot be such a set of formulas (even an infinite set). Inexpressiveness of FOL.

In case we can find the set of formulas and is a property of C, then we can use our (complete) proof system to prove |--. This will imply that is a property of C.

But while the proof exists, we do not have a decision procedure that will check whether |-- and there is no limit on the amount of time and space needed for reaching such a proof when it exists.

Page 146: Brief Introduction to Logic

146

Program verification: flowchart programs

(Book: chapter 7)

Page 147: Brief Introduction to Logic

147

History

Verification of flowchart programs: Floyd, 1967Hoare’s logic: Hoare, 1969Linear Temporal Logic: Pnueli, Krueger, 1977Model Checking: Clarke & Emerson, 1981

Page 148: Brief Introduction to Logic

148

Program Verification

Predicate (first order) logic.Partial correctness, Total correctnessFlowchart programsInvariants, annotated programsWell founded ordering (for

termination)Hoare’s logic

Page 149: Brief Introduction to Logic

149

Predicate (first order logic)

Variables, functions, predicates

Terms

Formulas (assertions)

Page 150: Brief Introduction to Logic

150

Signature

Variables: v1, x, y18Each variable represents a value of some given

domain (int, real, string, …).Function symbols: f(_,_), g2(_), h.)_,_,_(

Each function has an arity (number of paramenters), a domain for each parameter,

and a range.f:int*int->int (e.g., addition), g:real->real (e.g.,

square root)A constant is a predicate with arity 0.

Relation symbols: R(_,_), Q.)_(Each relation has an arity, and a domain for each

parameter.R : real*real (e.g., greater than).

Q : int (e.g., is a prime).

Page 151: Brief Introduction to Logic

151

Terms

Terms are objects that have values.Each variable is a term.Applying a function with arity n to n

terms results in a new term.

Examples: v1, 5.0, f(v1,5.0), g2(f(v1,5.0))

More familiar notation: sqr(v1+5.0)

Page 152: Brief Introduction to Logic

152

Formulas

Applying predicates to terms results in a formula.

R(v1,5.0), Q(x)More familiar notation: v1>5.0

One can combine formulas with the boolean operators (and, or, not,

implies).R(v1,5.0)->Q(x)

x>1 -> x*x>xOne can apply existentail and universal

quantification to formulas.x Q(X) x1 R(x1,5.0) x y R(x,y)

Page 153: Brief Introduction to Logic

153

A model, A proofs

A model gives a meaning (semantics) to a first order formula:

A relation for each relation symbol.A function for each function symbol.A value for each variable.

An important concept in first order logic is that of a proof. We assume the ability to prove that

a formula holds for a given model.Example proof rule (MP) :

Page 154: Brief Introduction to Logic

154

Flowchart programs

Input variables: X=x1,x2,…,xlProgram variables: Y=y1,y2,…,ym

Output variables: Z=z1,z2,…,zn

start

haltY=f(X)

Z=h(X,Y)

Page 155: Brief Introduction to Logic

155

Assignments and tests

Y=g(X,Y) t(X,Y)FT

Page 156: Brief Introduction to Logic

156

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

Initial condition

Initial condition: the values for the

input variables for which the

program must work.

x1>=0 /\ x2>0

FT

Page 157: Brief Introduction to Logic

157

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

The input-output claim

The relation between the values of the input and the

output variables at termination.

x1=z1*x2+z2\/

0=>z2<x2

FT

Page 158: Brief Introduction to Logic

158

Partial correctness, Termination, Total correctness

Partial correctness: if the initial condition holds and the program

terminates then the input-output claim holds.

Termination: if the initial condition holds, the program terminates.

Total correctness: if the initial condition holds, the program terminates and the

input-output claim holds.

Page 159: Brief Introduction to Logic

159

Subtle point:

The program ispartially correct

withrespect to

x1>=0/\x2>=0and totally correct

with respect tox1>=0/\x2>0

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

T F

Page 160: Brief Introduction to Logic

160

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

Annotating a scheme

Assign an assertion for each pair of

nodes. The assertion expresses

the relation between the

variable when the program counter is

located between these nodes.

A

B

C D

E

FT

Page 161: Brief Introduction to Logic

161

InvariantsInvariants are assertions that hold at each state

throughout the execution of the program.One can attach an assertion to a particular location

in the code:e.g., at(B) (B).

This is also an invariant; in other locations, at(B) does not hold hence the implication holds.

If there is an assertion attached to each location, (A), (B), (C), (D), (E), then their disjunction is

also an invariant: (A)\/(B)\/(C)\/(D)\/(E)(since location is always at one of these locations).

Page 162: Brief Introduction to Logic

162

Annotating a scheme with invariants

A): x1>=0 /\ x2>=0B): x1=y1*x2+y2 /\

y2>=0C): x1=y1*x2+y2 /\

y2>=0 /\ y2>=x2D):x1=y1*x2+y2 /\

y2>=0 /\ y2<x2E):x1=z1*x2+z2\/

0=>z2<x2Notice: (A) is the initial

condition, Eis the input-output

condition.

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

A

B

C D

E

FT

A) Is the precondition of (y1,y2)=(0,x1) and B) is its postcondition

Page 163: Brief Introduction to Logic

163

Preliminary:Relativizing assertions

(B) : x1= y1 * x2 + y2 /\ y2 >= 0Relativize B) w.r.t. the assignment,

obtaining B) [Y\g(X,Y)].e.(B) expressed w.r.t. variables at

A ).

(B)A =x1=0 * x2 + x1 /\ x1>=0Think about two sets of variables,

before={x, y, z, …} after={x’,y’,z’…}.

Rewrite (B) using after, and the assignment as a relation between

the set of variables. Then eliminate after by substitution .

Here: x1’=y1’ * x2’ + y2’ /\ y2’>=0 /\

x1’=x1 /\ x2’=x2 /\ y1’=0 /\ y2’=x1now eliminate x1’, x2’, y1’, y2.’

(y1,y2)=(0,x1)

A

B

A

B

(y1,y2)=(0,x1)

Y=g(X,Y)

Page 164: Brief Introduction to Logic

164

Preliminary:Relativizing assertions

(y1,y2)=(0,x1)

A

B

A

B

(y1,y2)=(0,x1)

A):

(B)A

(B)

Y=g(X,Y)

Y=g(X,Y)

Page 165: Brief Introduction to Logic

165

Verification conditions: assignment

A) B)A

where B)A = B)[Y\g(X,Y)]

A): x1>=0 /\ x2>=0B): x1=y1*x2+y2 /\

y2>=0

B)A=x1=0*x2+x1 /\

x1>=0

(y1,y2)=(0,x1)

A

B

A

B

(y1,y2)=(0,x1)

Y=g(X,Y)

Page 166: Brief Introduction to Logic

166

(y1,y2)=(y1+1,y2-x2)

Second assignment

C): x1=y1*x2+y2 /\

y2>=0 /\ y2>=x2

B): x1=y1*x2+y2 /\

y2>=0B)C:

x1=(y1+1)*x2+y2-x2 /\ y2-x2>=0

C

B

Page 167: Brief Introduction to Logic

167

(z1,z2)=(y1,y2)

Third assignment

D):x1=y1*x2+y2 /\ y2>=0 /\

y2<x2

E):x1=z1*x2+z2 /\ 0<=z2<x2

E)D:

x1=y1*x2+y2 /\ 0<=y2<x2

E

D

Page 168: Brief Introduction to Logic

168

Verification conditions: tests

B) /\ t(X,Y) C)

B) /\¬t(X,Y) D)

B): x1=y1*x2+y2 /\y2>=0

C): x1=y1*x2+y2 /\ y2>=0 /\ y2>=x2D):x1=y1*x2+y2 /\

y2>=0 /\ y2<x2

y2>=x2

B

C

D

B

C

Dt(X,Y)

FT

FT

Page 169: Brief Introduction to Logic

169

Verification conditions: tests

y2>=x2

B

C

D

B

C

Dt(X,Y)

FT

FT

t(X,Y)¬t(X,Y)

B)

C)

Page 170: Brief Introduction to Logic

170

Partial correctness proof:An induction on length of execution

B)

B)

D)

C)

Initially, states satisfy the initial conditions.

Then, passing from one set of states to another, we preserve the invariants at the appropriate location.

We prove: starting with a state satisfying the initial conditions, if are at a point in the execution, the invariant there holds.

Not a proof of termination!

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

A

B

C D

E

A)

no

no

yes

yes

T F

Page 171: Brief Introduction to Logic

171

Exercise: prove partial correctness

Initial condition:

x>=0

Input-output claim:

z=x!

start

halt

(y1,y2)=(0,1)

y1=x

(y1,y2)=(y1+1,(y1+1)*y2) z=y2

TF

Page 172: Brief Introduction to Logic

172

What have we achieved?

For each statement S that appears between points X and Y we showed that if the control is

in X when (X) holds (the precondition of S) and S is executed, then (Y) (the postcondition of S)

holds.Initially, we know that (A) holds.The above two conditions can be combined into

an induction on the number of statements that were executed:

If after n steps we are at point X, then (X) holds.

Page 173: Brief Introduction to Logic

173

Another example

(A) : x>=0

(F) : z^2<=x<(z+1)^2

z is the biggest numberthat is not greaterthan sqrt x.

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 174: Brief Introduction to Logic

174

Some insight

1+3+5(+…+2n+1(=)n+1^)2

y2 accumulates theabove sum, untilit is bigger than x.

y3 ranges over oddnumbers 1,3,5…,

y1 is n-1.

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 175: Brief Introduction to Logic

175

Invariants

It is usually sufficient to have one invariant for every loop (cycle in the program’s graph).

We will have(C)=y1^2<=x\/

y2=(y1+1)^2\/

y3=2*y1+1

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 176: Brief Introduction to Logic

176

Obtaining (B)

By backwards substitution in (C).

(C)=y1^2<=x\/

y2=(y1+1)^2\/

y3=2*y1+1

(B)=y1^2<=x\/

y2+y3=(y1+1)^2\/

y3=2*y1+1

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 177: Brief Introduction to Logic

177

Check assignment condition

(A)=x>=0(B)=y1^2<=x\/

y2+y3=(y1+1)^2\/

y3=2*y1+1(B) relativized is

0^2=>x\/

0+1(=0+1^)2\/

1=2*0+1

Simplified: x>=0

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 178: Brief Introduction to Logic

178

Obtaining (D)

By backwards substitution in(B).

(B)=y1^2<=x\/ y2+y3=(y1+1)^2\/

y3=2*y1+1

(D)=(y1+1)^2<=x\/ y2+y3+2=(y1+2)^2\/

y3+2=2*(y1+1)+1

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 179: Brief Introduction to Logic

179

Checking

(C)=y1^2<=x\/

y2=(y1+1)^2\/

y3=2*y1+1

(C)/\y2<=x) (D)

(D)=(y1+1)^2<=x\/

y2+y3+2=(y1+2)^2\/

y3+2=2*(y1+1)+1

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 180: Brief Introduction to Logic

180

y1^2<=x\/

y2=(y1+1)^2\/

y3=2*y1+1 /\y2<=x )y1+1^(2<=x\/

y2+y3+2=(y1+2)^2\/

y3+2=2*(y1+1)+1

y1^2<=x\/

y2=(y1+1)^2\/

y3=2*y1+1 /\y2<=x )y1+1^(2<=x\/

y2+y3+2=(y1+2)^2\/

y3+2=2*(y1+1)+1

y1^2<=x /\

y2=(y1+1)^2 /\

y3=2*y1+1 /\y2<=x (y1+1)^2<=x /\

y2+y3+2=(y1+2)^2 /\

y3+2=2*(y1+1)+1

Page 181: Brief Introduction to Logic

181

Not finished!

Still needs to:

Calculate (E) bysubstituting backwardsfrom (F).

Check that(C)/\y2>x(E)

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 182: Brief Introduction to Logic

182

Exercise: prove partial correctness. Initially: x1>0/\x2>0. At termination: z1=gcd(x1,x2).

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

Page 183: Brief Introduction to Logic

183

Annotation of program with invariants

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F

T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

z1=gcd(x1,x2)

x1>0 /\ x2>0

gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0

gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1y2

gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1<y2

gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1>y2

y1=gcd(x1,x2)

A

B

D

EF

G

H

Page 184: Brief Introduction to Logic

184

Part 1

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F

T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

(A)= x1>0 /\ x2>0

(B)=gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0 A

B

D

EF

G

H

(B)’rel= gcd(x1,x2)=gcd(x1,x2)/\x1>0/\x2>0 (A)

(B)’rel

Page 185: Brief Introduction to Logic

185

Part 2a

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F

T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

(B)= gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0

(D)=gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1y2 A

B

D

EF

G

H

(B)/\¬)y1=y2( (D)

Page 186: Brief Introduction to Logic

186

Part 2b

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F

T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

(G)= y1=gcd(x1,x2)

A

B

D

EF

G

H

(B)= gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0

(B)/\)y1=y2( (G)

Page 187: Brief Introduction to Logic

187

Part 3

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

(D)= gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1y2

(E)=gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1<y2

(F)=(gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1>y2

A

B

D

EF

G

H

(D)/\)y1>y2( (F)

(D)/\¬)y1>y2( (E)

Page 188: Brief Introduction to Logic

188

Part 4

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

x1>0 /\ x2>0

(B)= gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0

(E)= gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1<y2

(F)= gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0/\y1>y2

A

B

D

EF

G

H

(B)’relE=gcd(y1,y2-y1)=gcd(x1,x2)/\y1>0/\y2-y1>0(B)’relF=gcd(y1-y2,y2)=gcd(x1,x2)/\y1-y2>0/\y2>0

(E) (B)’rel1 (F) (B)’rel2

Page 189: Brief Introduction to Logic

189

Annotation of program with invariants

halthalt

startstart

)y1,y2(=)x1,x2()y1,y2(=)x1,x2(

z1=y1z1=y1

y1=y2F

T

y1>y2y1>y2

y2=y2-y1y2=y2-y1y1=y1-y2y1=y1-y2

TF

(H)= z1=gcd(x1,x2)

(G)= y1=gcd(x1,x2)

A

B

D

EF

G

H

(H)’rel= y1=gcd(x1,x2)

(G) (H)’rel2

Page 190: Brief Introduction to Logic

190

Proving termination

Page 191: Brief Introduction to Logic

191

Well-founded sets

Partially ordered set (W,<): If a<b and b<c then a<c (transitivity). If a<b then not b<a (asymmetry). Not a<a (irreflexivity).

Well-founded set (W,<): Partially ordered. No infinite decreasing chain a1>a2>a3>…

Page 192: Brief Introduction to Logic

192

Examples for well founded sets Natural numbers with the bigger than

relation. Finite sets with the set inclusion relation. Strings with the substring relation. Tuples with alphabetic order:

(a1,b1)>(a2,b2) iff a1>a2 or [a1=a2 and b1>b2].

(a1,b1,c1)>(a2,b2,c2) iff a1>a2 or [a1=a2 and b1>b2] or [a1=a2 and b1=b2 and c1>c2].

Page 193: Brief Introduction to Logic

193

Why does the program terminate

y2 starts as x1. Each time the loop is

executed, y2 is decremented.

y2 is natural number The loop cannot be

entered again when y2<x2.

start

halt

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

(y1,y2)=(0,x1)

A

B

D

E

falsey2>=x2

C

true

Page 194: Brief Introduction to Logic

194

Proving termination

Attach a function u(N) to each point N.

Annotate the flowchart with invariants, and prove their consistency conditions.Choose a well-founded set (W,<).

Prove that (N) (u(N) in W).

Page 195: Brief Introduction to Logic

195

How not to stay in a loop?

Show that u(M)>=u(N)’rel.

At least once in each loop, show that u(M)>u(N).

S

M

N

TN

M

Page 196: Brief Introduction to Logic

196

How not to stay in a loop?

For stmt: (M)(u(M)>=u(N)’rel)

Relativize since we need to compare values not syntactic expressions.

For test (true side):((M)/\test)(u(M)>=u(N))

For test (false side):((M)/\

¬test)(u(M)>=u(L))

stmt

M

N

test

N

M

true

L

false

Page 197: Brief Introduction to Logic

197

What did we achieve?

There are finitely many control points. The value of the function u cannot

increase. If we return to the same control point,

the value of u must decrease (its a loop!).

The value of u can decrease only a finite number of times.

Page 198: Brief Introduction to Logic

198

Why does the program terminate

u(A)=x1u(B)=y2u(C)=y2u(D)=y2u(E)=z2

W: naturals : <greater than

start

halt

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

(y1,y2)=(0,x1)

A

B

D

E

falsey2>=x2

C

true

Page 199: Brief Introduction to Logic

199

Recall partial correctness annotation

A): x1>=0 /\ x2>=0B): x1=y1*x2+y2 /\

y2>=0C): x1=y1*x2+y2 /\

y2>=0 /\ y2>=x2D):x1=y1*x2+y2 /\

y2>=0 /\ y2<x2E):x1=z1*x2+z2 /\ 0<=z2<x2

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

A

B

C D

E

falsetrue

Page 200: Brief Introduction to Logic

200

Strengthen for termination

A): x1>=0 /\ x2>0B): x1=y1*x2+y2 /\

y2>=0/\x2>0C): x1=y1*x2+y2 /\

y2>=0/\y2>=x2/\x2>0D):x1=y1*x2+y2 /\

y2>=0 /\ y2<x2/\x2>0E):x1=z1*x2+z2 /\ 0<=z2<x2

start

halt

(y1,y2)=(0,x1)

y2>=x2

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

A

B

C D

E

falsetrue

Page 201: Brief Introduction to Logic

201

Strengthen for termination

A): x1>=0 /\ x2>0 u(A)>=0B): x1=y1*x2+y2 /\ y2>=0/\

x2>0u(B)>=0C): x1=y1*x2+y2 /\y2>=0

/\y2>=x2/\x2>0u(c)>=0D):x1=y1*x2+y2 /\ y2>=0 /\

y2<x2/\x2>0u(D)>=0E):x1=z1*x2+z2 /\ 0<=z2<x2u(E)>=0This proves that u(M) is natural for

each point M.

u(A)=x1u(B)=y2u(C)=y2u(D)=y2u(E)=z2

Page 202: Brief Introduction to Logic

202

We shall show:u(A)=x1u(B)=y2u(C)=y2u(D)=y2u(E)=z2A)u(A)>=u(B)’relB)/\

y2>=x2u(B)>=u(C)

C)u(C)>u(B)’relB)/\

y2<x2u(B)>=u(D)

D)u(D)>=u(E)’rel

start

halt

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

(y1,y2)=(0,x1)

A

B

D

E

falsey2>=x2

C

true

Page 203: Brief Introduction to Logic

203

Proving decrement

C): x1=y1*x2+y2 /\ y2>=0 /\ y2>=x2/\x2>0

u(C)=y2u(B)=y2u(B)’rel=y2-x2

C) y2>y2-x2(notice that C) x2>0)

start

halt

(y1,y2)=(y1+1,y2-x2) (z1,z2)=(y1,y2)

(y1,y2)=(0,x1)

A

B

D

E

falsey2>=x2

C

true

Page 204: Brief Introduction to Logic

204

Integer square prog.

(C)=y1^2<=x /\ y2=(y1+1)^2 /\ y3=2*y1+1

(B)=y1^2<=x /\ y2+y3=(y1+1)^2

\/ y3=2*y1+1

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 205: Brief Introduction to Logic

205

u(A)=x+1u(B)=x-y2+1u(C)=max(0,x-y2+1)u(D)=x-y2+1u(E)=u(F)=0u(A)>=u(B)’relu(B)>u(C)’relu(C)>=u(D)u(C)>=u(E)u(D)>=u(B)’relNeed some invariants,i.e., y2<=x/\y3>0at points B and D,and y3>0 at point C.

start

(y1,y2,y3)=(0,0,1)

A

halt

y2>x

(y1,y3)=(y1+1,y3+2) z=y1

B

C

D

F

truefalse

E

y2=y2+y3

Page 206: Brief Introduction to Logic

Modal Logicsלוגיקות מודליות

Lectures of Doron PeledBar Ilan University

Page 207: Brief Introduction to Logic

לוגיקות מודליות

עד כה דנו בלוגיקה סטטית: תכונה התקיימהאו לא התקיימה.

נרצה לדון בלוגיקות דינמיות: קיום התכונהיכול להשתנות, למשל בזמן.

:עבור נכונות שימוש נרחב ללוגיקות כאלו בבינה Multiagent systemsשל תכניות,

מלכותית, בכלכלה וכולי.

Page 208: Brief Introduction to Logic

דוגמאות

:ראש הממשלה הוא כעת לוגיקה של זמן זה לא היה נכון, ויהיה בעבר, בנימין נתניהו

שבו זה לא יהיה נכון.עתידגם לוגיקה של מקום: בבאר שבע חם ויבש, אבל

באלסקה קר ורטוב.( לוגיקה של ידעknowledge:) רובוט A

45המשחק כדורגל יודע שהכדור נמצא ב אינו יודע איפה Bמעלות ימינה, רובוט

הכדור )לא רואה אותו(.

Page 209: Brief Introduction to Logic

שימושים

.הוכחת נכונות של מערכות חומרה ותכנהלמערכות כאלו יש התנהגות המשנה את

אחר כך x=1המצב שלהם בזמן. בתחילה רק עולה, אחר כך הוא מגיע xהערך של

ואחר כך יורד.100לערך הוכחת הנכונות תשתמש בטענות לוגיות המאפשרות לבטא את השינוי של המצב

בזמן.

Page 210: Brief Introduction to Logic

שימושים נוספים

לוגיקות של ידע

העובדה שידע מסויים נתון או אינו נתון במערכת יכול לסייע בהבנת ותכנון המערכת. רובוטים למשל, יכולים "לדעת"

או לא "לדעת" נתונים מסוימים. במיוחד אם אותם נתונים היו או לא היו ברי הסקה מהנתונים שקיבלו )דרך

חיישנים שונים, או כתנאי התחלה להפעלתם(.

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

בתחרות ומנצלים חוסר ידע מסויים(.

Page 211: Brief Introduction to Logic

עולמות שונים לוגיקה מודלית:

p,q

p

יש לנו עולמות שונים, בכל עולם מתקימות תכונות שונות )פסוקיות או מסדר ראשון(.

יכולים ליצג קיום של מוצר מסוים.p, qלמשל,

במקום לציין את השלילה של תכונה,

פשוט לא נציין אותה. כלומר הצומת הריקה

תקיים ¬p, ¬q

Page 212: Brief Introduction to Logic

עולמות שונים לוגיקה מודלית:

p,q

p

יש גם קשר בין העולמות שיסומן על ידי קשתות.

הקשר יכול לציין דברים רבים:כל צומת מציינת מצב )למשל של משתני תכנית מחשב( וקשת מציינת מעבר זמן בין מצב

למצב.

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

Page 213: Brief Introduction to Logic

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

והקשר ביניהם.

תחביר:

::= p | ¬ | (/\) | (\/) | [] | <>.P הינו משתנה פסוקי מאוסף משתנים פסוקיים pכאשר

((=)\/)אפשר להגדיר קשרים חדשים, למשל

דוגמאות:

(p[]<>p)((p/\<>q)[]<>(p/\[]q))

Page 214: Brief Introduction to Logic

סמנטיקה, לא פורמלי.)נוסחא מתפרשת מעל עולם )צומת ,לנוסחאות שהן נוסחאות אטומיות )פסוקיות

מסדר ראשון(, יש את הפרוש הרגיל מעל העולם. יש את הפרוש ¬/\, \/, לקשרים הלוגיים הרגילים

הרגיל. מסמן שהתכונה נכונה לכל העולמות ][הקשר

הישיגים מהעולם הנוכחי על ידי קשת. מסמן שהתכונה נכונה לפחות לעולם <>הקשר

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

Page 215: Brief Introduction to Logic

מה מתקיים בעולם של הצומת הימנית תחתונה?

p,q

p

p, ¬q, p/\¬q, p\/q ,…

<>p, []q

והצומת השמאלית התחתונה?

¬ p, ¬ q, ¬(p\/q), []p, <>[]q.

Page 216: Brief Introduction to Logic

סמנטיקה, פורמלי

Kripkeנגדיר מבנה (W, R, L, P)P – a set of propositionsW – a set of “states” (or “worlds”).R – a relation RWxW “accessibility

relation”

L: W2P Assigns the set of propositions that hold in each state.

Page 217: Brief Introduction to Logic

עולמות שונים לוגיקה מודלית:

s1

p,q

s2

p

s3

P={p,q}

W={s1,s2,s3}

R={(s1,s1),(s1,s2),(s2,s1), (s3,s1),(s3,s2)}

L(s1)={p,q}, L(s2)={p},L(s3)=

Page 218: Brief Introduction to Logic

כעת אפשר להגדיר את המשמעות בצורה רקורסיבית. המשמעות מוגדרת לכל מצב

)עולם(

x|==p iff pL(x) x|== iff x|=/= x|==/\ iff x|== and x|== x|==\/ iff x|== or x|== x|==[] iff for each yW such that

R(x,y), y|== x|==<> iff there exists yW such

that R(x,y), y|==

Page 219: Brief Introduction to Logic

אם בכל מצב ובכל שקולה )=( ל- נוסחא מתקיימת מתקיימת אם ורק אם עולם

[]=<> <>=[] [](p/\q)=([]p/\[]q) <>(p\/q)=(<>p\/<>q)

אבלo [](p\/q)([]p\/[]q)o <>(p/\q)(<>p/\<>q)

למה? האם הגרירה בכיוון מסויים מתקיימת?

Page 220: Brief Introduction to Logic

אי שוויונות שלא מתקיימים

[](p\/q)([]p\/[]q) כן מתקיים אבל

<>(p/\q)(<>p/\<>q) כן מתקיים אבל

q

p

Page 221: Brief Introduction to Logic

עבור נוסחה מודלית ברת סיפוק(satisfiable) אם מתקיימת עבור מצב

מסויים במודל מסויים. אינה ברת סיפוק(unsatisfiable) אינה מתקיימת עבור

כל מצב בכל מודל. נכונה\מתקיימת במודל, אם מתקיימת עבור כל המצבים

במודל.Valid )אם מתקיימת בכל מודל )בכל מצב ==|Valid במשפחה של מודלים המקיימים הגבלה מסויימת

בהנחה |==)כפי שנראה בהמשך(. גם כאן נסמן שמשפחת המודלים ידועה.

Page 222: Brief Introduction to Logic

אקסיומות וכללי היסק

כל האקסיומות וMP.מתורת הפסוקים עבור כל המודלים(valid):

K []()([][])

GENכלל היסק: []

Page 223: Brief Introduction to Logic

עבור משפחות מודליםT [] Reflexive: w,(w,w)R

D []<> Serial: wW w’W (w,w’)R

4 [][][] Transitive: w,w’,w’’W(w,w’)R/\ (w’,w’’)R (w,w’’)R

5 <>[]<> Euclidean: w,w’,w’’W(w,w’)R/\ (w,w’’)R (w’,w’’)R

Page 224: Brief Introduction to Logic

מבנה היררכי של משפחות. חץ פרושו שההיררכיה הראשונה בעלת יותר מבנים ופחות

מההיררכיה השניהvalidתכונות בה

K

K4 K5 KD KT=KDT

K45 KD5 KD4

KD45

KT4=KTD4

KT5=KT45=KDT5=KDT45

Page 225: Brief Introduction to Logic

מודלים לינארים

טרנזיטיבי)כל זוג מצבים מסודר )סדר לינאריאין מעגלים

מתאים לתאור ביצוע של תכנית )שינויממצב למצב(.

X=0 X=0X=2X=1X=1

Page 226: Brief Introduction to Logic

חידה

שלשה ילדים משחקים בחוץ. חלק מתלכלכים על מצחם בבוץ, אבל לא יודעים

זאת.עובר אב של אחד מהם ואומר:

"לפחות אחד מכם מלוכלך בבוץ על מצחו, מי יודע שמצחו מלוכלך?"

אף ילד לא עונה. הוא שואל שוב, ואף ילד לא עונה, בפעם השלישית שהוא שואל, כל הילדים עונים יחד שראשם

שלהם מלוכלך.הכיצד?

Page 227: Brief Introduction to Logic

חידה

שלשה ילדים משחקים בחוץ. חלק מתלכלכים על מצחם בבוץ, אבל לא יודעים

זאת.עובר אב של אחד מהם ואומר:

"לפחות אחד מכם מלוכלך בבוץ על מצחו, מי יודע שמצחו מלוכלך?"

אף ילד לא עונה. הוא שואל שוב, ואף ילד לא עונה, בפעם השלישית שהוא שואל, כל הילדים עונים יחד שראשם

שלהם מלוכלך.הכיצד?

Page 228: Brief Introduction to Logic

הנחות

הילדים לא משקרים1.

כל ילד אינו רואה את מצחו שלו2.

כל ילד חכם ויכול להסיק כל היסק לוגי 3.הגיוני ממה שהוא רואה.

Page 229: Brief Introduction to Logic

לא יכול להבחין בין המקרים

Page 230: Brief Introduction to Logic

אוסף העולמות האפשרייםm-muddy, n-not muddy

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Page 231: Brief Introduction to Logic

רלציות שקילות בין עולמות מסמנת אי יכולת להבדילקשתות עצמיות הקיימות כאן עבור כל הרלציות. לא ציינו:

כתום נסמן את העולם האמיתי:

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rc

Rb

Rb

Rb

Rb

Ra

Ra

Ra

Ra

Rc

Rc

Page 232: Brief Introduction to Logic

שמשתתף אומר Kc סימון מודאלי לידע )סוכן(

c ש-יודע .מתקיים

נניחmc פרושו שמשתתף c-מלוכלך ו ¬mc פרושו אינו מלוכלך.cשמתתף

Ka(mb/\mc)

KcKa mb

Kc(ma/\Ka mb)בדוגמא שלנו, רוציםKxmx .עבור אחד המשתתפים

:סימון נוסף C-אומר ש הינו ידע משותף, כלומר, כל וכל משתתף יודע שכל משתתף יודע משתתף יודע את

וכולי.את

Page 233: Brief Introduction to Logic

משמש בדומה לכמת ][ בלוגיקה Kaהכמת מודלית

ההבדל הינו שיש לנו מספר רלציות ביןצמתים ולא רק רלציה אחת. לכן, לכל רלציה

יש את הכמת שלה. כל רלציהRa ,בין צמתים הינה רצלית שקילות

כלומר רפלקסיבית, טרנזיטיבית וסימטרית.אין את הדואלי <> ל-][. אבל אפשר לכתוב

¬Ka¬

Page 234: Brief Introduction to Logic

סמנטיקה

s|==p iff ps s|==/\ iff s|== and s|== s|==¬ iff s|=/= s|==Ka iff r (s,r) Ra, r|== s|==C iff Ka and KaKb, KcKa etc.

Page 235: Brief Introduction to Logic

תכונה

Kx(/\) Kx/\Kx

אבל לא מתקיים Kx(\/) Kx\/Kx

Page 236: Brief Introduction to Logic

בעולם האמיתי?cמה יודע

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rc

Rb

Rb

Rb

Rb

Ra

Ra

Ra

Ra

Rc

Rc

Page 237: Brief Introduction to Logic

אינו יכול להבחין בין המצבים השקולים cמשתתף Kc(ma/\mb) לכן: Rcלפי

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rc

Rb

Rb

Rb

Rb

Ra

Ra

Ra

Ra

Rc

Rc

Page 238: Brief Introduction to Logic

Ka(mb/\mc)בעולמות אלו?בעולמות הירוקים:a ומה יודע

Ka(mb/\¬mc) : בעולמות הכחוליםKcKa (mb) לכן:

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rc

Rb

Rb

Rb

Rb

Ra

Ra

Ra

Ra

Rc

Rc

ידע על קשת משותף לשני הצמתים המחוברים

אליו

Page 239: Brief Introduction to Logic

KcKaKb ?

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rc

Rb

Rb

Rb

Rb

Ra

Ra

Ra

Ra

Rc

Rc

Kb)¬ma/\¬mc(

Kb)ma/\¬mc(

Kb)¬ma/\mc(

Kb)ma/\mc(

Page 240: Brief Introduction to Logic

KcKaKbtrue

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rc

Rb

Rb

Rb

Rb

Ra

Ra

Ra

Ra

Rc

Rc

Kb)na/\¬nc(

Kb)ma/\¬nc(

Kb)¬na/\mc(

Kb)ma/\mc(

KaKbmc

KaKb¬mc

Page 241: Brief Introduction to Logic

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:n, b:n, c:n

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rc

Rb

Rb

Rb

Rb

Ra

Ra

Ra

Ra

Rc

Rc

Kb)¬ma/\¬mc(

Kb)ma/\¬mc(

Kb)¬ma/\mc(

Kb)ma/\mc(

KaKbmc

KaKb¬mc

אחרי הצהרת האבמה שהאב אמר הינו ידע משותף )כי כולם שמעו את

זה יחד ונכחו שהאחרים שמעו וכולי(.

Page 242: Brief Introduction to Logic

אחרי הצהרת האב בעולם האמיתי כולם יודעים גם

C(ma\/mb\/mc)

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rb

Rb

Rb

Ra

Ra

Ra

Rc

Rc

Kb)¬ma/\mb/\¬mc(

Kb)ma/\¬mc(

Kb)¬ma/\mc(

Kb)ma/\mc(

KaKbmc

KaKb¬mc

Ka)ma/\¬mb/\¬nc(

Ka)¬ma/\¬mb/\mc(

אבל אף אחד לא מודיע שהוא יודע שיש

לו בוץ על המצח

Page 243: Brief Introduction to Logic

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

המתאימות

a:m, b:m, c:m a:n, b:n, c:m

a:n, b:m, c:m

a:m, b:m, c:m

a:m, b:n, c:n a:n, b:m, c:n

a:m, b:m, c:n

Rc

Rb

Rb

Rb

Ra

Ra

Ra

Rc

Rc

Kb)¬ma/\mb/\¬mc(

Kb)ma/\¬mc(

Kb)¬ma/\mc(

Kb)ma/\mc(

KaKbmc

KaKb¬m

c

Ka)ma/\¬mb/\¬mc(

Ka)¬ma/\¬mb/\mc(

Page 244: Brief Introduction to Logic

מכיון שאף אחד לא הודיע שמצחו מלוכלך, בפעם השניה שהאב שואל, המצב הוא הבא:

a:m, b:m, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:m, b:m, c:n

Rc

Rb

Ra

Ka)ma/\¬mb/\mc(/\Kc)ma/\¬mb/\mc(

Kb)¬ma/\mb/\mc(/\Kc)¬ma/\mb/\mc(

Ka)ma/\mb/\¬mc(/\Kb)ma/\mb/\¬mc(

Page 245: Brief Introduction to Logic

מכיון שגם בפעם השניה אף אחד לא ידע שמצחו מלוכלך, בפעם השלישית ימחקו העולמות המקווקוים

a:m, b:m, c:m

a:n, b:m, c:m

a:m, b:n, c:m

a:m, b:m, c:n

Rc

Rb

Ra

Ka)ma/\¬mb/\mc(/\Kc)ma/\¬mb/\mc(

Kb)¬ma/\mb/\mc(/\Kc)¬ma/\mb/\mc(

Ka)ma/\mb/\¬mc(/\Kb)ma/\mb/\¬mc(

Page 246: Brief Introduction to Logic

מכיון שגם בפעם השניה אף אחד לא ידע שמצחו מלוכלך, בפעם השלישית ימחקו העולמות המקווקוים

a:m, b:m, c:m

ועכשיו שלשת הילדים יודעים שמצחם מלוכלך

Page 247: Brief Introduction to Logic

לוגיקה של ידע יורשת את תכונותיה מהלוגיקהKT45

אם אני יודע על גרירה, ואני יודע שצידה הימני מתקיים, אני יודע גם שצידה השמאלי מתקיים )או, בקיצור, אני יודע

להסיק!(

Ka() (Ka Ka)כל מה שאני יודע מתקיים גם בעולם שלי

Kaאני יודע מה אני יודע

Ka KaKaאני יודע מה אני לא יודע

¬Ka Ka¬Ka

Page 248: Brief Introduction to Logic

עוד חידה כובעים אדומים.3 כובעים לבנים ו-2יש בערמה

שלשה נתינים באים אל המלך.

המלך שם על ראש כל אחד כובע ושואל אותם לפי הסדר אם הם יודעים מה הצבע על

ראשם )אין להם מראה והם לא מגלים אחד לשני מה על הראש(.

הראשון אומר "איני יודע".

השני אומר "איני יודע".

השלישי יודע, מה הצבע של כובעו?

Page 249: Brief Introduction to Logic

נסמן

R2לאיש השני כובע אדום – W1לאיש הראשון כובע לבן –

וכולי.

.W2הינו שקול ל- R2¬ למעשה

Page 250: Brief Introduction to Logic

RRR

RWR RRW

RWWWWR WRW

WRR

R3

R3

R2

R2

R1

R1 R1

R3R2

גרף העולמות האפשריים עם רלציות השקילויות

Page 251: Brief Introduction to Logic

RRR

RWR RRW

RWWWWR WRW

WRR

R3

R3

R2

R2

R1

R1 R1

R3R2

אחרי שהראשון אמר שהוא לא יודע איזה כובע C¬R1על ראשו,

Page 252: Brief Introduction to Logic

RRR

RWR RRW

WWR WRW

WRR

R3R2R1

R1 R1

R3R2

אחרי שהראשון אמר שהוא לא יודע איזה כובע C¬R1על ראשו,

Page 253: Brief Introduction to Logic

RRR

RWR RRW

WWR WRW

WRR

R3R2R1

R1 R1

R3R2

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

Page 254: Brief Introduction to Logic

RRR

RWR

WWR

WRR

R2R1

R1

R2

כעת נשאל השלישי, אבל בכל עולם שבו הוא .R3יכול להיות מתקיים

Page 255: Brief Introduction to Logic

בתחילה ובנוסחאות:={C(R1\/R2\/R3),C(R1K2R1),

C(R1K3R1),

C(R2K1R2),

C(R2K3R2),

C(R3K1R3),

C(R3K2R3),

C(¬R1K2¬R1),

C(¬R1K3¬R1),

C(¬R2K1¬R2),

C(¬R2K3¬R2),

C(¬R3K1¬R3),

C(¬R3K2¬R3){

Page 256: Brief Introduction to Logic

RRR

RWR RRW

WWR WRW

WRR

R3R2R1

R1 R1

R3R2

אחרי שהראשון אמר שהוא לא יודע איזה כובע C¬R1על ראשו,

{C)¬K1R1/\¬K1¬R1(}|-- C)R2\/R3(

Page 257: Brief Introduction to Logic

RRR

RWR

WWR

WRR

R2R1

R1

R2

אחרי שהשני לא יודע איזה כובע על ראשו

{C)R2\/R3(, C)¬K2R2/\¬K2¬R2(}|--K3R3

Page 258: Brief Introduction to Logic

Temporal logic

Dynamic, speaks about several “worlds” and the relation between them.

Our “worlds” are the states in an execution.

There is a linear relation between them, each two sequences in our execution are ordered.

Interpretation: over an execution, later over all executions.

Page 259: Brief Introduction to Logic

מודלים לינאריםטרנזיטיבי)כל זוג מצבים מסודר )סדר לינאריאין מעגלים

.)מתאים לתאור ביצוע של תכנית )שינוי ממצב למצב לכל תכנית מתאימים אוסף )קבוצת( כל הביצועים

שלה. תכונה מתקיימת בתכנית אם היא מתקיימת בכל

הביצועים.

X=0 X=0X=2X=1X=1

Page 260: Brief Introduction to Logic

LTL: Syntax

::= () | ¬ | /\ \/ U |O | p

“box”, “always”, “forever”“diamond”, “eventually”, “sometimes”

O “nexttime”

U“until”Propositions p, q, r, … Each represents some

state property (x>y+1, z=t, at_CR, etc.)

Page 261: Brief Introduction to Logic

Semantics over suffixes of execution

O

U

Page 262: Brief Introduction to Logic

Can discard some operators

Instead of <>p, write true U p. Instead of []p, we can write

¬(<>¬p),or ¬(true U ¬p).Because []p=¬¬[]p.¬[]p means it is not true that p holds forever, or at some point ¬p holds or <>¬p.

Page 263: Brief Introduction to Logic

Combinations

[]<>p “p will happen infinitely often”

<>[]p “p will happen from some point forever”.

([]<>p) ([]<>q) “If p happens infinitely often, then q also happens infinitely often”.

Page 264: Brief Introduction to Logic

Some relations: [](/\)=([])/\([]) But <>(/\)(<>)/\(<>)

<>(\/)=(<>)\/(<>) But [](\/)([])\/([])

Page 265: Brief Introduction to Logic

What about

([]<>)/\([]<>)=[]<>(/\)?

([]<>)\/([]<>)=[]<>(\/)?

(<>[])/\(<>[])=<>[](/\)?

(<>[])\/(<>[])=<>[](\/)?

No, just

Yes!!!

Yes!!!

No, just

Page 266: Brief Introduction to Logic

Formal semantic definition Let be a sequence s0 s1 s2 … Let i be a suffix of : si si+1 si+2 … (0 = ) i |= p, where p a proposition, if si|=p. i |= /\ if i |= and i |= . i |= \/ if i |= or i |= . i |= ¬ if it is not the case that i |= . i |= <> if for some ji, j |= . i |= [] if for each ji, j |= . i |= U if for some ji, j|=.

and for each ik<j, k |=.

Page 267: Brief Introduction to Logic

Then we interpret:

For a state:s|=p as in propositional logic.

For an execution:|= is interpreted over a sequence, as in previous slide.

For a system/program:P|=holds if |= for every sequence of P.

Page 268: Brief Introduction to Logic

Spring Example

s1 s3s2

pull

release

releaseextended

malfunction

extended

r0 = s1 s2 s1 s2 s1 s2 s1 …

r1 = s1 s2 s3 s3 s3 s3 s3 …

r2 = s1 s2 s1 s2 s3 s3 s3 …

Page 269: Brief Introduction to Logic

LTL satisfaction by a single sequence

malfunction

s1 s3s2pull

release

releaseextende

dextended

r2 = s1 s2 s1 s2 s3 s3 s3 …

r2 |= extended ??

r2 |= O extended ??

r2 |= O O extended ??

r2 |= <> extended ??

r2 |= [] extended ??

r2 |= <>[] extended ??

r2 |= ¬ <>[] extended ??

r2 |= (¬extended) U malfunction ??

r2 |= [](¬extended->O extended) ??

Page 270: Brief Introduction to Logic

LTL satisfaction by a system

malfunction

s1 s3s2pull

release

releaseextende

dextended

P |= extended ??

P |= O extended ??

P |= O O extended ??

P |= <> extended ??

P|= [] extended ??

P |= <>[] extended ??

P |= ¬ <>[] extended ??

P |= (¬extended) U malfunction ??

P |= [](¬extended->O extended) ??

Page 271: Brief Introduction to Logic

More specifications

[] (PC0=NC0 <> PC0=CR0) [] (PC0=NC0 U Turn=0) Try at home:

- The processes alternate in entering their critical sections.- Each process enters its critical section infinitely often.

Page 272: Brief Introduction to Logic

Proof system¬<>p<-->[]¬p

[](pq)([]p[]q) []p(p/\O[]p) O¬p<-->¬Op [](pOp)(p[]p) (pUq)<-->(q\/(p/\O(pUq))) (pUq)<>q

+ propositional logic axiomatization.

+ proof rule: _p_ []p

Page 273: Brief Introduction to Logic

Traffic light example

Green Yellow Red Always has exactly one light:

[](¬(gr/\ye)/\¬(ye/\re)/\¬(re/\gr)/\(gr\/ye\/re))

Correct change of color?

[]((grU ye)\/(yeU re)\/(reU gr))

Page 274: Brief Introduction to Logic

Another kind of traffic light

GreenYellowRedYellowFirst attempt:

[](((gr\/re) U ye)\/(ye U (gr\/re)))

Correct specification:[]( (gr(gr U (ye /\ ( ye U re ))))

/\(re(re U (ye /\ ( ye U gr ))))

/\(ye(ye U (gr \/ re))))

Needed only when wecan start with yellow

Page 275: Brief Introduction to Logic

L0:While True do NC0:wait(Turn=0); CR0:Turn=1endwhile ||L1:While True do NC1:wait(Turn=1); CR1:Turn=0endwhile

T0:PC0=L0PC0:=NC0T1:PC0=NC0/\Turn=0 PC0:=CR0T2:PC0=CR0 (PC0,Turn):=(L0,1)T3:PC1=L1PC1=NC1T4:PC1=NC1/\Turn=1 PC1:=CR1T5:PC1=CR1 (PC1,Turn):=(L1,0)

Initially: PC0=L0/\PC1=L1

Example: The program

Is this the only reasonable way to model this program?

Page 276: Brief Introduction to Logic

The state graph:Successor relation between reachable states.

Turn=0L0,L1

Turn=0L0,NC1

Turn=0NC0,L1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=0CR0,L1

Turn=1L0,CR1

Turn=1NC0,CR1

Turn=1L0,NC1

Turn=1NC0,NC1

Turn=1NC0,L1

Turn=1L0,L1T0 T0T3 T3

T1 T4T3

T0 T3

T0

T0 T4T1 T3

T2

T2

T5

T5

Page 277: Brief Introduction to Logic

Always ¬(PC0=CR0/\PC1=CR1)(Mutual exclusion)

Turn=0L0,L1

Turn=0L0,NC1

Turn=0NC0,L1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=0CR0,L1

Turn=1L0,CR1

Turn=1NC0,CR1

Turn=1L0,NC1

Turn=1NC0,NC1

Turn=1NC0,L1

Turn=1L0,L1

Page 278: Brief Introduction to Logic

Always if Turn=0 then at some point Turn=1

Turn=0L0,L1

Turn=0L0,NC1

Turn=0NC0,L1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=0CR0,L1

Turn=1L0,CR1

Turn=1NC0,CR1

Turn=1L0,NC1

Turn=1NC0,NC1

Turn=1NC0,L1

Turn=1L0,L1

Page 279: Brief Introduction to Logic

Always if Turn=0 then at some point Turn=1

Turn=0L0,L1

Turn=0L0,NC1

Turn=0NC0,L1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=0CR0,L1

Turn=1L0,CR1

Turn=1NC0,CR1

Turn=1L0,NC1

Turn=1NC0,NC1

Turn=1NC0,L1

Turn=1L0,L1

Page 280: Brief Introduction to Logic

Interleaving semantics:Execute one transition at a time.

Turn=0L0,L1

Turn=0L0,NC1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=1L0,CR1

Turn=1L0,NC1

Need to check the property

for every possible interleaving!

Page 281: Brief Introduction to Logic

Interleaving semantics:Execute one transition at a time.

Turn=0L0,L1

Turn=0L0,NC1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=1L0,CR1

Turn=1L0,NC1

Need to check the property

for every possible interleaving!

Page 282: Brief Introduction to Logic

Interleaving semantics

Turn=0L0,L1

Turn=0L0,NC1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=1L0,CR1

Turn=1L0,NC1

Turn=0L0,L1

Turn=0L0,NC1

Page 283: Brief Introduction to Logic

(][Turn=0 <>Turn=1)

Turn=0L0,L1

Turn=0L0,NC1

Turn=0NC0,L1

Turn=0CR0,NC1

Turn=0NC0,NC1

Turn=0CR0,L1

Turn=1L0,CR1

Turn=1NC0,CR1

Turn=1L0,NC1

Turn=1NC0,NC1

Turn=1NC0,L1

Turn=1L0,L1

Page 284: Brief Introduction to Logic

מה ניתן לעשות עם זה? הוכחה בלוגיקה שתכונות של תכנית מתקיימות )נכונות של

תכנית(.הוכחה אלגוריתמית אוטומטית

Model Checking השיטה זיכתה את ממציאיה בפרסTuring שהוא הפרס

זכה 1996החשוב ביותר במדעי המחשב השנה )ולפני כן במדען ישראלי, אמיר פנואלי בפרס זה על הצגת הלוגיקה

הטמפורלית:"For seminal work introducing temporal logic into

computing science and for outstanding contributions to program and system verification."