roy l crole
play

Roy L. Crole explain what inductively defined sets are; and do - PDF document

Midlands Graduate School, University of Birmingham, April 2008 1 Midlands Graduate School, University of Birmingham, April 2008 2 Operational Semantics Introduction Abstract Machines and By the end of this introduction,


  1. ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 1 Midlands Graduate School, University of Birmingham, April 2008 2 Operational Semantics Introduction Abstract Machines and By the end of this introduction, you should be able to Correctness � briefly explain the meaning of syntax and semantics; � give a snap-shot overview of the course; Roy L. Crole � explain what inductively defined sets are; and � do simple rule inductions. University of Leicester, UK ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 3 Midlands Graduate School, University of Birmingham, April 2008 4 Some Answers What’s Next? Background � Programming Languages are formal languages used to “communicate” with a “computer”. � Programming languages may be “low level”. They give � What is a Programming Language? direct instructions to the processor (instruction set � What is Syntax? architecture). � What is Semantics? � Or “high level”. The instructions are indirect—being (eg) compiled for the processor—but much closer to concepts understood by the user (Java, C++, . . . ). ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 5 Midlands Graduate School, University of Birmingham, April 2008 6 � Syntax refers to particular arrangements of “words � Semantics is the study of “meaning”. and letters” eg David hit the ball or � In particular, syntax can be given meaning. The word if t > 2 then H = Off. run can mean � A grammar is a set of rules which can be used to • execution of a computer program, specify how syntax is created. • spread of ink on paper, . . . � Examples can be seen in automata theory, or � Programming language syntax can be given a programming manuals. semantics—at least in theory!. We need this to write � Theories of syntax and grammars can be meaningful programs . . . developed—ideas are used in compiler construction. ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 7 Midlands Graduate School, University of Birmingham, April 2008 8 Top Level view of Course Semantic descriptions are often informal. Consider while ( expression ) command ; � Define syntax for programs P and types σ ; adapted from Kernighan and Ritchie 1978/1988, p 224: � (define type assignments P :: σ ); The command is executed repeatedly so long as the value � define operational semantics looking like of the expression remains unequal to 0; the expression must ( P , s ) ⇓ ( V , s ′ ) P ⇓ V ; have arithmetic or pointer type. The execution of the (test) � and compile P and V to abstract machine instructions expression, including all side effects, occurs before each P �→ [[ P ]] and V �→ ( | V | ) execution of the command. → t ( | V | ) � Then prove correctness: P ⇓ V iff [[ P ]] �− We want to be more precise, more succinct. ✫ ✪ ✫ ✪

  2. ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 9 Midlands Graduate School, University of Birmingham, April 2008 10 Example Inductive Definition What’s Next? Inductively Defined Sets Let Var be a set of propositional variables. Then the set Prpn of propositions of propositional logic is inductively defined � Specify inductively defined sets; programs, types etc by the rules will be defined this way. BNF grammars are a form of φ ψ inductive definition; abstract syntax trees are also defined [ P ∈ Var ] ( A ) ( ∧ ) P φ ∧ ψ inductively. φ ψ φ ψ φ � Define Rule Induction; properties of programs will be ( ∨ ) ( → ) ( ¬ ) proved using this. It is important. φ ∨ ψ φ → ψ ¬ φ Each proposition is created by a deduction . . . ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 11 Midlands Graduate School, University of Birmingham, April 2008 12 Example of Rule Induction Inductively Defined Sets in General Consider the set of trees T defined inductively by � Given a set of rules, a deduction is a finite tree such T 1 T 2 that [ n ∈ Z ] n +( T 1 , T 2 ) − each leaf node label c occurs as a base rule Let L ( T ) be the number of leaves in T , and N ( T ) be the ( ∅ , c ) ∈ R number of + -nodes of T . We prove (see board) − for any non-leaf node label c , if H is the set of children of c then ( H , c ) ∈ R is an inductive rule. ∀ T ∈ T . L ( T ) = N ( T )+ 1 � The set I inductively defined by R consists of those where the functions L , N : T → N are defined recursively by elements e which have a deduction with root node e . One • L ( n ) = 1 and L (+( T 1 , T 2 )) = L ( T 1 )+ L ( T 2 ) may prove ∀ e ∈ I . φ ( e ) for a property φ ( e ) by rule induction. See the notes . . . • N ( n ) = 0 and N (+( T 1 , T 2 )) = N ( T 1 )+ N ( T 2 )+ 1 ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 13 Midlands Graduate School, University of Birmingham, April 2008 14 Chapter 1 What’s Next? Types and Expressions By the end of this chapter, you should be able to � describe the programs (syntax) of a simple imperative language called IMP; � We define the types and expressions of IMP . � We give an inductive definition of a formal type � give a type system to IMP and derive types; system. � explain the idea of evaluation relations; � derive example evaluations. ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 15 Midlands Graduate School, University of Birmingham, April 2008 16 Program Expressions and Types for IMP � The types of the language IMP are given by the grammar The program expressions are given (inductively) by σ :: = int | bool | cmd P c :: = constant � A location environment L is a finite set of (location, l | memory location type) pairs, with type being just int or bool : P iop P ′ | integer operator L = l 1 :: int ,..., l n :: int , l n + 1 :: bool ,..., l m :: bool P bop P ′ | boolean operator l : = P ′ | assignment � Given L , then any P whose locations all appear in L P ; P ′ | sequencing can (sometimes) be assigned a type; we write P :: σ to if P then P ′ else P ′′ | conditional indicate this, and define such type assignments inductively. while P do P ′ | while loop ✫ ✪ ✫ ✪

  3. ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 17 Midlands Graduate School, University of Birmingham, April 2008 18 [any n ∈ Z ] Example: Deduction of a Type Assignment n :: int T :: bool F :: bool P 1 :: int P 2 :: int [ l :: int ∈ L ] [ bop ∈ BOpr ] l :: int P 1 bop P 2 :: bool l :: σ P :: σ l :: int 5 :: int D 3 D 4 skip :: cmd l : = P :: cmd l : = l − 1 ; l ′ : = l ′ ∗ l :: cmd l ≥ 5 :: bool D 2 if l ≥ 5 then l ′ : = 1 else ( l : = l + 1 ; l ′ : = l ′ ∗ l ) :: cmd P 1 :: bool P 2 :: cmd P 3 :: cmd P 1 :: bool P 2 :: cmd if P 1 then P 2 else P 3 :: cmd while P 1 do P 2 :: cmd ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 19 Midlands Graduate School, University of Birmingham, April 2008 20 States What’s Next? An Evaluation Relation � A state s is a finite partial function Loc → Z ∪ B . � For example s = � l 1 �→ 4 , l 2 �→ T , l 3 �→ 21 � � There is a state denoted by s { l �→ c } : Loc → Z ∪ B which � We define a notion of state. is the partial function � We define an evaluation relation for IMP .  if l ′ = l c  � We look at an example. ( s { l �→ c } )( l ′ ) def = s ( l ′ ) otherwise  � We say that state s is updated at l by c . ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 21 Midlands Graduate School, University of Birmingham, April 2008 22 An Evaluation Relation [ provided l ∈ domain of s ] ⇓ LOC ( l , s ) ⇓ ( s ( l ) , s ) Consider the following evaluation relationship ( l ′ : = T ; l : = 4 + 1 , �� ) ⇓ ( skip , � l ′ �→ T , l �→ 5 � ) ( P 1 , s ) ⇓ ( n 1 , s ) ( P 2 , s ) ⇓ ( n 2 , s ) ⇓ OP ( P 1 op P 2 , s ) ⇓ ( n 1 op n 2 , s ) The idea is Starting program ⇓ final result ( P , s ) ⇓ ( c , s ) ⇓ ASS ( l : = P , s ) ⇓ ( skip , s { l �→ c } ) We describe an operational semantics which has assertions which look like ( P 1 , s 1 ) ⇓ ( skip , s 2 ) ( P 2 , s 2 ) ⇓ ( skip , s 3 ) ⇓ SEQ ( P , s ) ⇓ ( c , s ) ( P , s 1 ) ⇓ ( skip , s 2 ) and ( P 1 ; P 2 , s 1 ) ⇓ ( skip , s 3 ) ✫ ✪ ✫ ✪ ✬ ✩ ✬ ✩ Midlands Graduate School, University of Birmingham, April 2008 23 Midlands Graduate School, University of Birmingham, April 2008 24 Example Evaluations ( P , s 1 ) ⇓ ( F , s 1 ) ( P 2 , s 1 ) ⇓ ( skip , s 2 ) ⇓ COND 2 ( if P then P 1 else P 2 , s 1 ) ⇓ ( skip , s 2 ) We derive deductions for (( 3 + 2 ) ∗ 6 , s ) ⇓ ( 30 , s ) ( P 1 , s 1 ) ⇓ ( T , s 1 ) ( P 2 , s 1 ) ⇓ ( skip , s 2 ) ( while P 1 do P 2 , s 2 ) ⇓ ( skip , s 3 ) ( while P 1 do P 2 , s 1 ) ⇓ ( skip , s 3 ) and ( P 1 , s ) ⇓ ( F , s ) ( while l = 1 do l : = l − 1 , � l �→ 1 � ) ⇓ ( skip , � l �→ 0 � ) ⇓ LOOP 2 ( while P 1 do P 2 , s ) ⇓ ( skip , s ) ✫ ✪ ✫ ✪

Recommend


More recommend