tics seman erational op induction; softcis500/cis500-f05/lectures/0912.pdf · cis 500 soft w are f...

66
CIS 500 Software Foundations Fall 2005 Induction; Operational Semantics CIS 500, Induction; Operational Semantics 1

Upload: others

Post on 20-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

CIS500

SoftwareFoundations

Fall2005

Induction;OperationalSemantics

CIS500,Induction;OperationalSemantics1

Page 2: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Announcements

Reviewrecitationsstartthisweek.Youmaygotoanyrecitationsectionthat

youwish.Youdonotneedtoregisterforthesection,nordoyouneedto

attendthesamesectiontheentiresemester.Ifyouneedhelpfindingastudy

group,wewillmatchpeopleupinrecitationsectionsthisweek.

Wed3:30-5:00PMLevine315Bohannon

Thurs10:30-12PMLevine612Aydemir

Thurs1:30-3PMLevine512Bohannon

Fri9:30-11AMLevine512Aydemir

Firsthomeworkassignmentisdueoneweekfromtoday.

CIS500,Induction;OperationalSemantics2

Page 3: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

StructuralInduction

CIS500,Induction;OperationalSemantics3

Page 4: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Booleanterms:Syntax

RecallthedefinitionofthelanguageB:

t::=true

false

nott

iftthentelset

ThiswasashorthandnotationforthedefinitionofthesetB.

ThesetBofbooleantermsisthesmallestsetsuchthat

1.{true,false}⊆B;

2.ift1∈B,then{nott1}⊆B;

3.ift1∈B,t2∈B,andt3∈B,thenift1thent2elset3∈B.

CIS500,Induction;OperationalSemantics4

Page 5: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Booleanterms:Semantics

WedefinedthesemanticsofBusingtherelationEval.If(t1,t2)∈Evalthen

t2isthemeaningoft1.RecallthatEvalisthesmallestsetclosedunderthe

followingrules:

1.(true,true)∈Eval

2.(false,false)∈Eval

3.(nott,true)∈Evalwhen(t,false)∈Eval

4.(nott,false)∈Evalwhen(t,true)∈Eval

5.(ift1thent2elset3,t)∈Evalwheneither:

�(t1,true)∈Evaland(t2,t)∈Eval

�(t1,false)∈Evaland(t3,t)∈Eval

CIS500,Induction;OperationalSemantics5

Page 6: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Provingpropertiesofprogramminglanguages

Supposewewanttoprovethatevaluationisdeterministic.Inotherwords:For

alltthereexistsatmostonet′

suchthat(t,t′)∈Eval.

CIS500,Induction;OperationalSemantics6

Page 7: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

StructuralInduction

Wecanuseinductionforbooleanterms.Thewaywehavedefinedtermsgives

usaninductionprinciple:

Forallt∈B,P(t)istrueifandonlyif

�P(true)andP(false)hold

�forallt1∈B,ifP(t1)holds,thenP(nott1)hold.

�forallt1,t2,t3∈B,ifP(t1),P(t2)andP(t3)holds,then

P(ift1thent2elset3)holds.

CIS500,Induction;OperationalSemantics7

Page 8: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Proofsbyinduction

We’llprovethatevaluationisdeterministic.Inotherwords:Foralltthere

existsatmostonet′

suchthat(t,t′)∈Eval.

Thisgivesustheproperty:

P(t)=existsatmostonet′

suchthat(t,t′)∈Eval.

Sowewanttoshow:

�P(true)(i.e.existsatmostonet′

suchthat(true,t′)∈Eval)

�P(false)

�P(nott1)giventhatP(t1)holds.

�P(ift1thent2elset3)giventhatP(t1),P(t2)andP(t3)allhold.

CIS500,Induction;OperationalSemantics8

Page 9: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Booleanterms:Semantics

WedefinedthesemanticsofBusingtherelationEval.If(t1,t2)∈Evalthen

t2isthemeaningoft1.RecallthatEvalisthesmallestsetclosedunderthe

followingrules:

1.(true,true)∈Eval

2.(false,false)∈Eval

3.(nott,true)∈Evalwhen(t,false)∈Eval

4.(nott,false)∈Evalwhen(t,true)∈Eval

5.(ift1thent2elset3,t)∈Evalwheneither:

�(t1,true)∈Evaland(t2,t)∈Eval

�(t1,false)∈Evaland(t3,t)∈Eval

CIS500,Induction;OperationalSemantics9

Page 10: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Proofonchalkboard

CIS500,Induction;OperationalSemantics10

Page 11: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Alternatenotation:Inferencerules

WecanalsodefineEvalusingashorthandnotation.Analternatenotationfor

thesamedefinition:

(true,true)∈Eval(false,false)∈Eval

(t1,true)∈Eval

(nott1,false)∈Eval

(t1,false)∈Eval

(nott1,true)∈Eval

(t1,true)∈Eval(t2,t)∈Eval

(ift1thent2elset3,t)∈Eval

(t1,false)∈Eval(t3,t)∈Eval

(ift1thent2elset3,t)∈Eval

Notethat,justintheBNFnotation,“thesmallestsetclosedunder...”is

implied(butoftennotstatedexplicitly).

Terminology:

�axiomvs.rule

�concreterulevs.rulescheme

CIS500,Induction;OperationalSemantics11

Page 12: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Alternatenotation:relationalsymbols

Ifweabbreviate(t,t′)∈Evalast⇓t

′wecanwritetheserulesevenmore

succinctly:

true⇓truefalse⇓false

t1⇓true

nott1⇓false

t1⇓false

nott1⇓true

t1⇓truet2⇓t

ift1thent2elset3⇓t

t1⇓falset3⇓t

ift1thent2elset3⇓t

Thenotationt⇓t′

isreadas“tevaluatestot’”.

Wewilloftenabbreviaterelationsusingsymbolssuchas⇓,→,`,etc.

CIS500,Induction;OperationalSemantics12

Page 13: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Namingtherules

Itisalsousefultogivenamestoeachrule,sothatwecanrefertothemlater.

true⇓trueB-True

false⇓falseB-Falset1⇓true

nott1⇓falseB-NotTrue

t1⇓false

nott1⇓trueB-NotFalse

t1⇓truet2⇓t

ift1thent2elset3⇓tB-IfTrue

t1⇓falset3⇓t

ift1thent2elset3⇓tB-IfFalse

CIS500,Induction;OperationalSemantics13

Page 14: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Derivations

Theinferencerulenotationleadstoaconvenientnotationforshowingwhya

pairoftermsisintheevaluationrelation.

Saysomeoneaskedyoutoprovethat

iftruethen(notfalse)else(nottrue)⇓true

CIS500,Induction;OperationalSemantics14

Page 15: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Provingpropertiesaboutevaluation

Lasttimeweshowedthattheevaluationrelationwasafunction.

i.e.foralltthereisatmostonet′

suchthatt⇓t′.

Todaywewillshowarelatedproperty:thatevaluationistotal.

i.e.foralltthereisatleastonet′

suchthatt⇓t′.

Howtoprovethisproperty?

CIS500,Induction;OperationalSemantics15

Page 16: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Usestructuralinduction

AgainwewillusethestructuralinductionprinciplefortermsinB:

ForalltinB,P(t)istrue,ifandonlyif

�P(true)andP(false)hold

�forallt1∈B,ifP(t1)holds,thenP(nott1)hold.

�forallt1,t2,t3∈B,ifP(t1),P(t2)andP(t3)holds,then

P(ift1thent2elset3)holds.

Toshowthatevaluationistotal,weneedP(t)tobe“thereexistsat′

such

thatt⇓t′”.

CIS500,Induction;OperationalSemantics16

Page 17: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Strengtheningtheinductionprinciple

WecannotshowthatP(nott1),givenP(t1).

P(t1)tellsusthatt1evaluatestosomet′,butnott1onlyevaluatesift

′is

trueorfalse,andwedon’tknowthat.

Whattodonow?Arewestuck?

CIS500,Induction;OperationalSemantics17

Page 18: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Strengthingtheinductionprinciple

Thesolutionistoproveapropertythatimpliesthepropertythatwewant.

Insteadofshowing

“tthereexistsat′

suchthatt⇓t′”

wewillshow

“forallteithert⇓trueort⇓false”

Provingthesecondpropertyimpliesthatthefirstoneisalsotrue.

ToshowthesecondpropertyweneedP(t)tobe“eithert⇓trueor

t⇓false”.

CIS500,Induction;OperationalSemantics18

Page 19: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Alargerlanguage

CIS500,Induction;OperationalSemantics19

Page 20: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Growingalanguage

Thebooleanlanguageisanextremelysimplelanguage.Thereisnotalotthat

youcansaywithit.

Atthesametime,itisprettyeasytoprovepropertiesaboutit.

Asweaddtotheexpressivenessofalanguage,itusuallybecomesmore

difficulttoshowthatthesamepropertiesaretrue.

Infact,somepropertiesthataretrueforsimplelanguagesarenottruefor

moreexpressivelanguages.

CIS500,Induction;OperationalSemantics20

Page 21: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

ThelanguageArith

Consideralargerlanguage,calledArith,thatincludesbothbooleansand

naturalnumbers:

t::=true

false

iftthentelset

0

succt

predt

iszerot

Whatisthestructuralinductionprincipleforthislanguage?

CIS500,Induction;OperationalSemantics21

Page 22: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Languagedefinability(informally)

Thislanguagedoesnotincludethetermformnott.

However,allisnotlost.Wheneverwewanttosaynott,wecanwrite:

iftthenfalseelsetrue.

Becausenottisdefinable,manyofthesamepropertiesaretrueaboutArith

withnottasaretrueforArithwithoutnott.

Leavingoutnotmeansthatourinductionprinciple(andthereforeourproofs)

areshorter.

CIS500,Induction;OperationalSemantics22

Page 23: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

SemanticsofArith

TodefinethesemanticsofArith,wewillfirstdefineasubsetofthetermsof

Ariththatwillbetheresultofevaluation.

Thesearecalledthevalues.

v::=bv

nv

bv::=true

false

nv::=0

succnv

Weusethemetavariablevtoindicatetermsthatarealsovalues.

CIS500,Induction;OperationalSemantics23

Page 24: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

SemanticsofArith

Note:weareoverloadingthesymbol⇓torefertotwodifferentrelations.

true⇓trueB-True

false⇓falseB-False

t1⇓truet2⇓v

ift1thent2elset3⇓vB-IfTrue

t1⇓falset3⇓v

ift1thent2elset3⇓vB-IfFalse

CIS500,Induction;OperationalSemantics24

Page 25: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Newrules:

0⇓0B-Zero

t1⇓nv

succt1⇓succnvB-Succ

t1⇓0

predt1⇓0B-PredZero

t1⇓succnv

predt1⇓nvB-PredSucc

t1⇓0

iszerot1⇓trueB-IsZeroZero

t1⇓succnv

iszerot1⇓falseB-IsZeroSucc

CIS500,Induction;OperationalSemantics25

Page 26: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Metavariablesareuseful

Wecanreplacethreerules:

true⇓trueB-True

false⇓falseB-False

0⇓0B-Zero

Withonerule:

v⇓vB-Value

CIS500,Induction;OperationalSemantics26

Page 27: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

PropertiesofArith

WeshowedthattwopropertiesweretrueofB,arethesesamepropertiestrue

ofArith?

�Evaluationisdeterministic:forallt,thereisatmostonet′

suchthat

t⇓t′.

�Evaluationistotal:forallt,eithert⇓trueort⇓false.

Thesecondisobviouslyfalse.Whatifwerephraseitas:

�Evaluationistotal:forallt,t⇓v.

CIS500,Induction;OperationalSemantics27

Page 28: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Evaluationisnottotal

�Evaluationistotal:forallt,t⇓v.

Thereisacounterexampletothistheorem.Whatdoessuccfalseevaluateto?

Ifwetrytouseinductiontoshowthistheorem,wheredoestheproofbreak?

Someterms,likesuccfalse,are“meaningless”inoursemantics.

CIS500,Induction;OperationalSemantics28

Page 29: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Stuckterms

It’salittleunsettlingthatevaluationisnottotal.

�Wewanttogivemeaningstoallterms.

�Wewantto(abstractly)describetheexecutionofacomputer.

�Later:somelanguagescontaininfiniteloops.

�Thosetermswon’thavemeaningswiththisstyleofsemanticseither.

�Wanttodistinguishloopsfromerrorslikesuccfalse.

CIS500,Induction;OperationalSemantics29

Page 30: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Small-stepsemantics

CIS500,Induction;OperationalSemantics30

Page 31: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Small-stepsemantics

�Mostofthesemanticswewilldefineinthiscoursewillbeinastylecalled

small-stepoperationalsemantics.

�Coreidea:describethe“intermediate”stepsofevaluationofanabstract

machine.

�Anabstractmachineconsistsof:

�asetofstates

�atransitionrelationonstates,written→

CIS500,Induction;OperationalSemantics31

Page 32: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Small-stepsemantics

�BasedontworelationsbetweentermsofArith:

�small-stepevaluation:t→t′

�multi-stepevaluation:t→∗t

�Small-stepevaluationistheonestepexecutionoftheabstractmachine.

Thestatesofthemachineareterms.

�Multi-stepevaluationisthereflexive,transitiveclosureofsmall-step

evaluation.Itdescribesexecutionsequencesoftheabstractmachine.

�t→∗t

′istotal(becauseofreflexivity).

�t→t′

maynotbetotal(whenthemachinegets“stuck”).

CIS500,Induction;OperationalSemantics32

Page 33: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Normalforms

�Anormalformisatermthatcannotbeevaluatedanyfurther–i.e.aterm

tisanormalform(or“isinnormalform”)isthereisnot′

suchthat

t→t′

�Anormalformisastatewheretheabstractmachineishalted–itcanbe

regardedasa“result”ofevaluation.

�Themeaningofatermtwithsmall-stepsemanticsisatermt′,suchthat

t→∗t

′andt

′isanormalform.

Wesaythatt′

“isthenormalformof”t.

CIS500,Induction;OperationalSemantics33

Page 34: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Normalforms

�ForArith,notallnormalformsarevalues,buteveryvalueisanormal

form.

�Atermlikesuccfalsethatisanormalform,butisnotavalue,is

“stuck”.

CIS500,Induction;OperationalSemantics34

Page 35: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Small-stepsemantics

Booleans:

iftruethent2elset3→t2iffalsethent2elset3→t3

t1→t′

1

ift1thent2elset3→ift′

1thent2elset3

Naturalnumbers:t1→t

1

succt1→succt′

1

pred0→0pred(succnv1)→nv1

Both:

iszero0→trueiszero(succnv1)→falset1→t

1

iszerot1→iszerot′

1

Whatdoallnon-axiomrulesincommon?

CIS500,Induction;OperationalSemantics35

Page 36: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Terminology

Computationrules:

iftruethent2elset3→t2iffalsethent2elset3→t3

Congruencerules:

t1→t′

1

ift1thent2elset3→ift′

1thent2elset3

Computationrulesperform“real”computationsteps.

Congruencerulesdeterminewherecomputationrulescanbeappliednext.

Whatabouttheotherrules?

CIS500,Induction;OperationalSemantics36

Page 37: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Digression

Supposewewantedtochangeourevaluationstrategysothatthethenand

elsebranchesofanifgetevaluated(inthatorder)beforetheguard.How

wouldweneedtochangetherules?

Suppose,moreoverthatiftheevaluationofthethenandelsebranchesleads

tothesamevalue,wewanttoimmediatelyproducethatvalue

(“short-circuiting”theevaluationoftheguard).Howwouldweneedtochange

therules?

Oftheruleswejustinvented,whicharecomputationrulesandwhichare

congruencerules?

CIS500,Induction;OperationalSemantics37

Page 38: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Digression

Supposewewantedtochangeourevaluationstrategysothatthethenand

elsebranchesofanifgetevaluated(inthatorder)beforetheguard.How

wouldweneedtochangetherules?

Suppose,moreoverthatiftheevaluationofthethenandelsebranchesleads

tothesamevalue,wewanttoimmediatelyproducethatvalue

(“short-circuiting”theevaluationoftheguard).Howwouldweneedtochange

therules?

Oftheruleswejustinvented,whicharecomputationrulesandwhichare

congruencerules?

CIS500,Induction;OperationalSemantics37-a

Page 39: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Digression

Supposewewantedtochangeourevaluationstrategysothatthethenand

elsebranchesofanifgetevaluated(inthatorder)beforetheguard.How

wouldweneedtochangetherules?

Suppose,moreoverthatiftheevaluationofthethenandelsebranchesleads

tothesamevalue,wewanttoimmediatelyproducethatvalue

(“short-circuiting”theevaluationoftheguard).Howwouldweneedtochange

therules?

Oftheruleswejustinvented,whicharecomputationrulesandwhichare

congruencerules?

CIS500,Induction;OperationalSemantics37-b

Page 40: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Propertiesofthissemantics

�(Homework):Thissmall-stepsemantics“agrees”withthelarge-step

semanticsfortermsthatdonotgetstuck.Inotherwords,t⇓vifand

onlyift→∗v.

�The→relationisdeterministic.Ift→t′

andt→t′′

thent′

=t′′.

�Evaluationisdeterministic:Thereisatmostonenormalformforaterm

t.(Easytoprove:Followsbecausethe→relationisdeterministic).

�Evaluationistotal:Thereisatleastonenormalformforatermt.(More

difficulttoprove:Mustshowthattherearenoinfinitesequencesof

small-stepevaluation.)

CIS500,Induction;OperationalSemantics38

Page 41: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Reasoningaboutevaluation

CIS500,Induction;OperationalSemantics39

Page 42: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Inductionprinciples

We’veseenthreedefinitionsofsetsandtheirassociatedinductionprinciples:

�Naturalnumbers

�Booleanterms

�Arithmeticterms

GivenasetdefinedwithBNF,itisnottoohardtodescribethestructural

inductionprincipleforthatset.

Forexample:

t::=brillig

tove

snickert

gyretgimblet

Whatisthestructuralinductionprincipleforthislanguage?

CIS500,Induction;OperationalSemantics40

Page 43: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Moreinductionprinciples

However,thesearenottheonlysetsthatwe’vedefinedsofar.

Wedefinedthesemanticsoftheselanguagesusingrelations,andrelationsare

justsets.

Thesesetsalsohaveinductionprinciples.

CIS500,Induction;OperationalSemantics41

Page 44: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Inductiononevaluation

Wecandefineaninductionprincipleforsmall-stepevaluation.Recallthe

definition(justforbooleans,fornow):

iftruethent2elset3→t2E-IfTrue

iffalsethent2elset3→t3E-IfFalse

t1→t′

1

ift1thent2elset3→ift′

1thent2elset3E-If

Whatistheinductionprincipleforthisrelation?

CIS500,Induction;OperationalSemantics42

Page 45: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Usingthisinductionprinciple

Forallt,t′,P(t→t

′)if

�P(iftruethent2elset3→t2)and

�P(iffalsethent2elset3→t3)and

�P(ift1thent2elset3→ift′

1thent2elset3)giventhat

P(t1→t′

1)

Whatdoesitmeantosay

P(ift1thent2elset3→ift′

1thent2elset3)?

CIS500,Induction;OperationalSemantics43

Page 46: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Derivations

Anotherwaytolookatitisintermsofderivations.

Aderivationrecordsthe“justification”foraparticularpairoftermsthatarein

theevaluationrelation,intheformofatree.We’veallreadyseenoneexample:

(exampleontheboard)

Terminology:

�Thesetreesarecalledderivationtrees(orjustderivations)

�Thefinalstatementinaderivationistheconclusion

�Wesaythataderivationisawitnessforitsconclusion(oraproofofits

conclusion)–itrecordsthereasoningstepstojustifytheconclusion

�Whenwereasonabouttheconclusions,wearereasoningaboutderivations

CIS500,Induction;OperationalSemantics44

Page 47: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Observation

Lemma:SupposewearegivenaderivationDwitnessingthepair(t,t′)inthe

→relation.Theneither:

1.thefinalruleusedinDisE-IfTrueandwehave

t=iftruethent2elset3andt′

=t2forsomet2andt3,or

2.thefinalruleusedinDisE-IfFalseandwehave

t=iffalsethent2elset3andt′

=t3forsomet2andt3,or

3.thefinalruleusedinDisE-Ifandwehavet=ift1thent2elset3and

t′

=ift′

1thent2elset3,forsomet1,t′

1,t2andt3;moreoverthe

immediatesubderivationofDwitnessest1→t′

1.

CIS500,Induction;OperationalSemantics45

Page 48: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

InductiononDerivations

Wecannowwriteproofsaboutevaluation“byinductiononderivationtrees.”

GivenanarbitraryderivationDwithconclusiont→t′,weassumethe

desiredresultforitsimmediatesub-derivation(ifany)andproceedbyacase

analysis(usingthepreviouslemma)ofthefinalevaluationruleusedin

constructingthederivationtree.

E.g....

CIS500,Induction;OperationalSemantics46

Page 49: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Inductiononsmall-stepevaluation

Forexample,wecanshowthatsmall-stepevaluationisdeterministic.

Theorem:Ift→t′thenift→t

′′thent

′=t

′′.

Proof:ByinductiononaderivationDoft→t′.

1.SupposethefinalruleusedinDisE-IfTrue,with

t=ift1thent2elset3andt1=trueandt′

=t2.Therefore,thelast

ruleofthederivationoft→t′

cannotbeE-IfFalse,becauset1isnot

false.Furthermore,thelastrulecannotbeE-Ifeither,becausethisrule

requiresthatt1→t′

1,andtruedoesnotsteptoanything.Sothelastrule

canonlybeE-IfTrue.

2.SupposethefinalruleusedinDisE-IfFalse,with

t=iffalsethent2elset3andt′

=t3.Thiscaseissimilartothe

previous.

3.SupposethefinalruleusedinDisE-If,witht=ift1thent2elset3

andt′

=ift′

1thent2elset3,wheret1→t′

1iswitnessedbyaderivation

CIS500,Induction;OperationalSemantics47

Page 50: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

D1.Thelastruleinthederivationoft→t′′

canonlybeE-If,soitmust

bethatt1→t′′

1.Byinductiont′

1=t′′

1sot′

=t′′.

CIS500,Induction;OperationalSemantics48

Page 51: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Whatprincipletouse?

We’veproventhesametheoremusingtwodifferentinductionprinciples.

Q:Whichoneisthebestonetouse?

A:Theonethatworks.

Forthesesimplelanguages,anythingyoucanprovebyinductionont→t′,

youcanprovebystructuralinductionont.Butthatwillnotbethecasefor

everylanguage.

CIS500,Induction;OperationalSemantics49

Page 52: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Well-foundedinduction

CIS500,Induction;OperationalSemantics50

Page 53: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

AQuestion

Whyareanyoftheseinductionprinciplestrue?WhyshouldIbelieveaproof

thatemploysone?

CIS500,Induction;OperationalSemantics51

Page 54: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Well-foundedinduction

Well-foundedinductionisageneralizedformofalloftheseinductionprinciples.

Let≺beawell-foundedrelationonasetA.LetPbeaproperty.Then

∀a∈A.P(a)iff

∀a∈A.([∀b≺a.P(b)]⇒P(a)

ChoosingtherightsetAandrelation≺determinestheinductionprinciple.

CIS500,Induction;OperationalSemantics52

Page 55: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Well-foundedinduction

Forexample,weletA=Nandn≺mdef

=m=n+1.Inthiscase,wecan

rewritepreviousprincipleas:

∀a∈N.P(a)iff

∀a∈N.([∀b≺a.P(b)]⇒P(a)

Now,bydefinitionaiseither0ori+1forsomei:

∀a∈N.P(a)iff

[∀b≺0.P(b)]⇒P(0)∧

∀i∈N.[∀b≺i+1.P(b)]⇒P(i+1)

Simplifyto:

∀a∈N.P(a)iffP(0)∧∀i∈N.P(i)⇒P(i+1)

CIS500,Induction;OperationalSemantics53

Page 56: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Stronginduction

If≺isthe“strictlylessthan”relation<,thentheprinciplewegetisstrong

induction.

∀a∈N.P(a)iff

∀a∈N.([∀b<a.P(b)]⇒P(a)

CIS500,Induction;OperationalSemantics54

Page 57: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Well-foundedrelation

Theinductionprincipleholdsonlywhentherelation≺iswell-founded.

Definition:Awell-foundedrelationisabinaryrelation≺onasetAsuchthat

therearenoinfinitedescendingchains···≺ai≺···≺a1≺a0.

Arethesuccessorand<relationswell-founded?

CIS500,Induction;OperationalSemantics55

Page 58: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Proofofwell-foundedinduction

We’dliketoshowthat:

Theorem:Let≺isawell-foundedrelationonasetA.LetPbeaproperty.

Then∀a∈A.P(a)iff

∀a∈A.([∀b≺a.P(b)]⇒P(a)

The(⇒)directionistrivial.We’llshowthe(⇐)direction.

First,observethatanynonemptysubsetQofAhasaminimalelement,evenif

Qisinfinite.

Now,suppose¬P(a)forsomeainA.Theremustbeaminimalelementmof

theset{a∈A|¬P(a)}.Butthen,¬P(m)yet[∀b≺m.P(b)]whichisa

contradiction.

CIS500,Induction;OperationalSemantics56

Page 59: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Structuralinduction

Well-foundedinductionalsogeneralizesstructuralinduction.

If≺isthe“immediatesubterm”relationforaninductivelydefinedset,then

theprinciplewegetisstructuralinduction.

Forexample,inArith,thetermt1isanimmediatesubtermoftheterm

succt1.

Istheimmediatesubtermrelationwell-founded?

Yes,ifalltermsofAritharefinite.

CIS500,Induction;OperationalSemantics57

Page 60: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Terminationofevaluation

CIS500,Induction;OperationalSemantics58

Page 61: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Terminationofevaluation

Theorem:Foreverytthereissomenormalformt′

suchthatt→∗t

′.

CIS500,Induction;OperationalSemantics59

Page 62: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

AnInductiveDefinition

Wecandefinethesizeofatermwiththefollowingrelation:

size(true)=1

size(false)=1

size(0)=1

size(succt1)=size(t1)+1

size(predt1)=size(t1)+1

size(iszerot1)=size(t1)+1

size(ift1thent2elset3)=size(t1)+size(t2)+size(t3)+1

Note:thisisyetmoreshorthand.Howwouldwewritethisdefinitionwith

inferencerules?

CIS500,Induction;OperationalSemantics60

Page 63: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

InductiononDerivations—AnotherExample

Theorem:Ift−→t′

—i.e.,if(t,t′)∈−→—thensize(t)>size(t

′).

Proof:ByinductiononaderivationDoft−→t′.

1.SupposethefinalruleusedinDisE-IfTrue,with

t=iftruethent2elset3andt′

=t2.Thentheresultisimmediate

fromthedefinitionofsize.

2.SupposethefinalruleusedinDisE-IfFalse,with

t=iffalsethent2elset3andt′

=t3.Thentheresultisagain

immediatefromthedefinitionofsize.

3.SupposethefinalruleusedinDisE-If,witht=ift1thent2elset3

andt′

=ift′

1thent2elset3,where(t1,t′

1)∈−→iswitnessedbya

derivationD1.Bytheinductionhypothesis,size(t1)>size(t′

1).But

then,bythedefinitionofsize,wehavesize(t)>size(t′).

CIS500,Induction;OperationalSemantics61

Page 64: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Terminationofevaluation

Theorem:Foreverytthereissomenormalformt′

suchthatt−→∗

t′.

Proof:

�First,recallthatsingle-stepevaluationstrictlyreducesthesizeoftheterm:

ift−→t′,thensize(t)>size(t

′)

�Now,assume(foracontradiction)that

t0,t1,t2,t3,t4,...

isaninfinite-lengthsequencesuchthat

t0,−→t1,−→t2,−→t3,−→t4−→···,

�Then

size(t0),size(t1),size(t2),size(t3),size(t4),...

isaninfinite,strictlydecreasing,sequenceofnaturalnumbers.

�Butsuchasequencecannotexist—contradiction!

CIS500,Induction;OperationalSemantics62

Page 65: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

Terminationofevaluation

Theorem:Foreverytthereissomenormalformt′

suchthatt−→∗

t′.

Proof:

�First,recallthatsingle-stepevaluationstrictlyreducesthesizeoftheterm:

ift−→t′,thensize(t)>size(t

′)

�Now,assume(foracontradiction)that

t0,t1,t2,t3,t4,...

isaninfinite-lengthsequencesuchthat

t0,−→t1,−→t2,−→t3,−→t4−→···,

�Then

size(t0),size(t1),size(t2),size(t3),size(t4),...

isaninfinite,strictlydecreasing,sequenceofnaturalnumbers.

�Butsuchasequencecannotexist—contradiction!

CIS500,Induction;OperationalSemantics62-a

Page 66: tics Seman erational Op Induction; Softcis500/cis500-f05/lectures/0912.pdf · CIS 500 Soft w are F oundations F all 2005 Induction; Op erational Seman tics CIS 500, Induction; Op

'

&

$

%

TerminationProofs

Mostterminationproofshavethesamebasicform:

Theorem:TherelationR⊆X×Xisterminating—i.e.,thereareno

infinitesequencesx0,x1,x2,etc.suchthat(xi,xi+1)∈Rforeachi.

Proof:

1.Choose

�awell-foundedset(W,<)—i.e.,asetWwithapartialorder<

suchthattherearenoinfinitedescendingchains

w0>w1>w2>...inW

�afunctionffromXtoW

2.Showf(x)>f(y)forall(x,y)∈R

3.Concludethattherearenoinfinitesequencesx0,x1,x2,etc.such

that(xi,xi+1)∈Rforeachi),since,iftherewere,wecould

constructaninfinitedescendingchaininW.

CIS500,Induction;OperationalSemantics63