Mechanized Semantics of Simple Imperative Programming Constructs*
Total Page:16
File Type:pdf, Size:1020Kb
App eared as technical rep ort UIB Universitat Ulm Fakultat fur Informatik Dec Mechanized Semantics of Simple Imp erative Programming Constructs H Pfeifer A Dold F W von Henke H Rue Abt Kunstliche Intelligenz Fakultat fur Informatik Universitat Ulm D Ulm verifixkiinformatikuniulmde Abstract In this pap er a uniform formalization in PVS of various kinds of semantics of imp er ative programming language constructs is presented Based on a comprehensivede velopment of xed p oint theory the denotational semantics of elementary constructs of imp erative programming languages are dened as state transformers These state transformers induce corresp onding predicate transformers providing a means to for mally derivebothaweakest lib eral precondition semantics and an axiomatic semantics in the style of Hoare Moreover algebraic laws as used in renement calculus pro ofs are validated at the level of predicate transformers Simple reformulations of the state transformer semantics yield b oth a continuationstyle semantics and rules similar to those used in Structural Op erational Semantics This formalization provides the foundations on which formal sp ecication of program ming languages and mechanical verication of compilation steps are carried out within the Verix pro ject This research has b een funded in part by the Deutsche Forschungsgemeinschaft DFG under pro ject Verix Contents Intro duction Related Work Overview A brief description of PVS Mechanizing Domain Theory Denotational Semantics for Statements State Transformers State Transformer Semantics Predicate Transformers Relating Various Styles of Semantics Weakest Lib eral Precondition Hoare Calculus Algebraic Laws Structural Op erational Semantics Continuations Intro ducing Variable Declarations Example A Simple Programming Language Conclusions App endix A Denotational Semantics State transformers A A Program State A State Transformer Semantics A Predicates A Predicate Transformers A Predicate Transformer Semantics i B Various Styles of Semantics B Weakest Precondition B Hoare Logic B Algebraic Laws B SOS B Continuation B Putting it Together C A Simple Programming Language C Simple Commands C Simple Declarations C The Language D Pro of of Lemma while strans adm ii Mechanized Semantics of Simple Imperative Programming Constructs Intro duction Formal verication of compiler implementations requires the semantics of the programming languages involved b e mo deled adequately in a formal system Dep ending on the asp ect of a programming language or its compilation one wants to describ e certain styles of semantics are more suitable than others A mathematically precise denition of a language for example would often b e accomplished using denotational semantics SS Ten Sto On the other hand if one is interested in developing a prototyp e implementation one might describ e the semantics of the language using an op erational style while an axiomatics semantics Flo Hoa would b e most useful if verication of programs is of concern In this pap er we present a generic mo delling in PVS of dierent kinds of semantics of the basic imp erative programming constructs Starting from a denotational semantics based on state transformers we show how to derive the characteristic rules of various semantic styles including weakest precondition semantics Hoare logic continuationstyle seman tics and structural op erational semantics The formalization is mo dular in the sense that there are separate PVS sp ecications for each programming construct In addition the comp onents of a programming language such as expressions statements and variable declarations are identied with their semantics Thus only the abstract struc ture of the languages comp onents is laid down and the formalization can hence b e applied to every programming language that consists of one or more of the constructs covered in this pap er The mathematical theory underlying the work presented here is well understo o d This pap er adds nothing new in this resp ect in fact most of the theorems can be found in standard textb o oks such as Win Sch Bes However we think that the main contribution of our work is a uniform treatment of dierentstyles of semantics in a formal system This allows for doing formal reasoning ab out b oth programs and their compilation within the same framework Moreover since manyinterchangeable forms of semantics are available the reasoning can b e carried out selecting that semantic formalism suiting b est All theorems presented here have b een proved within PVS Most of the theories are reprinted in the App endix The complete sp ecications and pro ofs are available from the authors up on request Related Work There havebeenseveral rep orts related to the mo delling of semantics of programming lan guages in a theorem prover including Sok Mas Cam HM and there are stan dard techniques for formalizing various styles of semantics For example our presentation regarding predicate transformers and algebraic semantics is comparable to a formalization in the HOL system of a guarded command language with weakest precondition semantics by BackandvWright BvW The work presented here is most closely related to that of Gordon Gor and Nipkow Nip since in contrast to the pap ers mentioned ab ove they deal with several forms of semantics Gordon uses the HOL system to mechanically derive the rules of Hoare logic of Mechanized Semantics of Simple Imperative Programming Constructs a simple whilelanguage from their denotational semantics Gor Furthermore he de scrib es how to apply HOL tactics to generate verication conditions based on the derived Hoare rules Nipkow Nip presents a formalization of the rst chapters of Winskels text book on programming language semantics Win in the theorem prover Isab elleHOL He denes op erational denotational and axiomatic semantics of a simple imp erative pro gramming language and proves their equivalence Additionally pro ofs of soundness and completeness of a verication condition generator are sketched In contrast to Nipkow we do not dene dierent forms of semantics for the same language but follow Gordon and showhowtoderivethecharacteristic rules of the various semantic formalisms from a denotational semantics This saves us from having to prove explicitly the equivalence of the dierent semantics Overview The remainder of the pap er is organized as follows after giving a brief overview of the sp ecication and verication system PVS in Section the remaining sections describ e the various PVS theories develop ed The overall structure of these theories is illustrated in Figure Each no de in this hierarchy graph represents one PVS theory and the edges represent imp orting relations Theories towards the b ottom are used imp orted bythose ab ovethem In order to b e able to dene the denotational semantics of lo ops and recursive pro cedures we have develop ed a rather comprehensive formalization of the theory of xed p oints in PVS This formalization is only briey summarized in Section a detailed description can b e found in a companion pap er BDvH Consequently the b ottom entry in Figure is to b e regarded as standing for a whole cluster of theories the concept of state transformers that Climbing up the hierarchy Section describ es are dened in theory srel Next the denotational semantics of imp erative programming language constructs based on state transformers is presented in Section The de nitions are split into several theories and are combined in theory statements Predicate transformers as dened in the theories ptrans and mpt are intro duced in Section State transformers and predicate transformers provide the basis for a uniform treatmentofvar ious styles of semantics such as weakest precondition semantics Hoarestyle semantics and algebraic laws see Section The corresp onding theories can b e found further up in the theory hierarchy and are comprised by the theory semantics The denitions in the theories mentioned so far abstract from the state on which programs op erate A concrete implementation of such a state is given in Section Finally as an example Section describ es how the formalizations can b e used to dene the semantics of a simple while language Mechanized Semantics of Simple Imperative Programming Constructs simple_program simple_commands simple_declarations semantics vardecl continuation_lems wp_hoare sos laws hoare_logic wp assert_assume continuation statements stmt_mpt state control_structures assignment mpt ptrans var_environment sequence skip expr location srel bexpr finsets relation deterministic (Fixed−Point Theory) Figure Hierarchy of PVS theories formalizing various kinds of semantics of simple con structs in imp erative