Download presentation
Presentation is loading. Please wait.
1
1 Semantics Q1 2007 S EMANTICS (Q1,’07) Week 4 Jacob Andersen PhD student andersen@daimi.au.dk
2
2 Semantics Q1 2007 News…
3
3 Semantics Q1 2007 Outline (Structural) Induction Finite vs. Infinite Stores Functional Languages (Let) Compound Definitions (Let++) Type-Checking Definitions (Let B ++)
4
4 Semantics Q1 2007 (S TRUCTURAL ) I NDUCTION
5
5 Semantics Q1 2007 Principle of Mathematical Induction Let P be a predicate (i.e. a boolean function): then we have that: Intuitive: ? P: N { true, false } n N : P(n) P(0) induction stepbase case Principle of mathematical induction: P(n) P(n+1) P(3) P(0)P(0) => P(1)P(1) => P(2)P(2) => P(3)
6
6 Semantics Q1 2007 Mathematical Induction example Prove the following theorem: All non-empty subsets of ℕ have a smallest element. or by contraposition: Live exercise… [Think 3 mins; then interactively on the whiteboard] ∀ A ⊆ ℕ : A ≠ ∅ (∃ l ∈ A : ∀ a ∈ A : l ≤ a ) ∀ A ⊆ ℕ : (∀ l ∈ A : ∃ a ∈ A : l>a ) A = ∅ Let A ⊆ ℕ be given. Assume: ∀ l ∈ A : ∃ a ∈ A : l>a Prove: A = ∅
7
7 Semantics Q1 2007 Mathematical Induction example What about: Will work ??? Instead this will work: ∀ A ⊆ ℕ : (∀ l ∈ A : ∃ a ∈ A : l>a ) A = ∅ Let A ⊆ ℕ be given. Assume: ∀ l ∈ A : ∃ a ∈ A : l>a Prove: A = ∅ P(n) ≝ n ∉ A P(n) P(n+1) NO ! P(n) ≝ ∀ m ∈ ℕ : m ≤ n m ∉ A n ℕ : P(n) P(0) P(n) P(n+1) ∀ n, m ∈ ℕ : m ≤ n m ∉ A or: ∀ m ∈ ℕ : m ∉ A A ∩ ℕ = ∅ (Since A ⊆ ℕ ) A = ∅
8
8 Semantics Q1 2007 Structural Induction (for Exp) Given: Arithmetic Expressions ( e Exp) – e ::= n | v | e 0 +e 1 e Exp : P(e) P(n) composite (inductive) case base cases Principle of structural induction: P(e 0 ) P(e 1 ) P(e 0 +e 1 ) P(v) and
9
9 Semantics Q1 2007 Intuition: Induction vs. Str’ Induction Induction: Holds for ? Structural Induction: Holds for ? P(0)P(0) => P(1)P(1) => P(2)P(2) => P(3) P(3) P(7+(x+y)) P(7) P(x) P(y) P(x+y) P(7+(x+y))
10
10 Semantics Q1 2007 Structural Induction Examples Given: Arithmetic Expressions ( e Exp) – Determinism of SOS* for L-expressions : “One-step evaluation of expressions is deterministic” e ::= n | v | e 0 +e 1 P(e) e’,e’’:[( | _ e e’ | _ e e’’) => e’= e’’] e: P(e) */ using the (side-effectless) small-step operational semantics Let be given: Prove: (i.e., “prove property, P, holds for all expressions”)
11
11 Semantics Q1 2007 Proof Structure (according to SI) Base cases: –Case [ n ]; show : –Case [ v ]; show : Inductive case: –Case [ e 0 +e 1 ]; show : –assume induction hypothesis [lhs]: P(e 0 ) P(e 1 ) –show inductive step [rhs]: P(e 0 +e 1 ) P(n) e’,e’’:( | _ n e’ | _ n e’’) => e’=e’’ P(v) e’,e’’:( | _ v e’ | _ v e’’) => e’=e’’ P(e 0 ) e’,e’’:( | _ e 0 e’ | _ e 0 e’’) => e’=e’’ P(e 1 ) e’,e’’:( | _ e 1 e’ | _ e 1 e’’) => e’=e’’ P(e 0 +e 1 ) e’,e’’:( | _ e 0 +e 1 e’ | _ e 0 +e 1 e’’) => e’=e’’ P(n) P(v) P(e 0 ) P(e 1 ) => P(e 0 +e 1 )
12
12 Semantics Q1 2007 Proof: Base Case [n] –Case [ n ]; show : –Show implication “ => ” (assume [lhs], show [rhs]): However, since [lhs] is trivially false (no rules for constants), the whole thing is trivially true (aka. “vacuously true”) »Recall that: P(n) e’,e’’:( | _ n e’ | _ n e’’ ) => e’ = e’’ P(n) b : (false => b) true P Q P Q PQ P Q false true falsetrue false true
13
13 Semantics Q1 2007 Proof: Base Case [v] –Case [ v ]; show : –Show implication “ => ” (assume [lhs], show [rhs]): assume [lhs]: and for some e’,e’’ show [rhs]: – could only be because ; thus, e’ = (v) …and similarly: – could only be because ; thus, e’’ = (v) –Thus, we have that: e’ = (v) = e’’ (as required) P(v) e’,e’’:( | _ v e’ | _ v e’’) => e’=e’’ P(v) | _ v e’ e’ = e’’ | _ v e’’ | _ v e’ | _ v (v) | _ v e’’ | _ v (v)
14
14 Semantics Q1 2007 Proof: Inductive Case [e 0 +e 1 ] –Case [ e 0 +e 1 ]; show : –assume induction hypothesis: P(e 0 ) P(e 1 ) –show inductive step: P(e 0 +e 1 ) –Show implication “ => ” (assume [lhs], show [rhs]): Assume [lhs]: and for some e’,e’’ Show [rhs]: When could we have that: (?) –Case Analysis (on e 0 +e 1 ) »Three possibilities ( [SUM 1 ], [SUM 2 ], and [SUM 3 ] )… P(e 0 ) e’,e’’:( | _ e 0 e’ | _ e 0 e’’) => e’=e’’ P(e 1 ) e’,e’’:( | _ e 1 e’ | _ e 1 e’’) => e’=e’’ P(e 0 +e 1 ) e’,e’’:( | _ e 0 +e 1 e’ | _ e 0 +e 1 e’’) => e’=e’’ P(e 0 ) P(e 1 ) => P(e 0 +e 1 ) | _ e 0 +e 1 e’ | _ e 0 +e 1 e’’ e’ = e’’ | _ e 0 +e 1 e
15
15 Semantics Q1 2007 Proof: Case Analysis (on e 0 +e 1 ) –Case Analysis (on e 0 +e 1 ): Case [SUM 1 (e 0 Z)]: – could only be because –But from our induction hypothesis, P(e 0 ) ; i.e. … : … we get that: » and implies that: e 0 ’ = e 0 ” »Thus we have that: e’ = e 0 ’+e 1 = e 0 ”+e 1 = e’’ (as required) | _ e 0 +e 1 e’ |_ e0 e0’ |_ e0 e0’ | _ e 0 +e 1 e 0 ’+e 1 = e’ |_ e0 e0” |_ e0 e0” | _ e 0 +e 1 e 0 ”+e 1 = e’’ | _ e 0 +e 1 e’’ |_ e0 e0’ |_ e0 e0’ |_ e0 e0” |_ e0 e0” P(e 0 ) e’,e’’:( | _ e 0 e’ | _ e 0 e’’) => e’=e’’
16
16 Semantics Q1 2007 Proof: Case Analysis (on e 0 +e 1 ) Case [SUM 2 (e 0 Z, e 1 Z)]: – (analoguous) Case [SUM 3 (e 0 Z, e 1 Z)]: – could only be because –Thus, we have that: e’ = m = n 0 +n 1 = m’ = e’’ (as required) Q.E.D. | _ n 0 +n 1 e’ | _ n 0 +n 1 e’’ | _ n 0 +n 1 m = e’ | _ n 0 +n 1 m’ = e’’ m = n 0 +n 1 m’ = n 0 +n 1
17
17 Semantics Q1 2007 What did we do: Proof Structure Base cases: –Case [ n ]; showed : –Case [ v ]; showed : Inductive case: –Case [ e 0 +e 1 ]; showed : –assumed induction hypothesis [lhs]: P(e 0 ) P(e 1 ) –showed inductive step [rhs] via case analysis: P(e 0 +e 1 ) P(n) e’,e’’:( | _ n e’ | _ n e’’) => e’=e’’ P(v) e’,e’’:( | _ v e’ | _ v e’’) => e’=e’’ P(e 0 ) e’,e’’:( | _ e 0 e’ | _ e 0 e’’) => e’=e’’ P(e 1 ) e’,e’’:( | _ e 1 e’ | _ e 1 e’’) => e’=e’’ P(e 0 +e 1 ) e’,e’’:( | _ e 0 +e 1 e’ | _ e 0 +e 1 e’’) => e’=e’’ P(n) P(v) P(e 0 ) P(e 1 ) => P(e 0 +e 1 )
18
18 Semantics Q1 2007 I NFINITE vs. F INITE S TORES
19
19 Semantics Q1 2007 Problem: Infinite Stores At this point Plotkin starts using stores with no upper bound. Problem: (Var infinite) –Solution: (for V FIN Var) –Now define finite configurations: We shall not worry about this (like TM vs. DFA) Store = Var Z v Store V = V Z V,E := { | Var E ( e ) V Store V } appropriate store all vars in “V”
20
20 Semantics Q1 2007 D EFINITIONS (FUNCTIONAL LANGs)
21
21 Semantics Q1 2007 Introducing Binding Constructs Consider the language ”Let”: Expressions ( e Exp): –Example Let program: –Note: the actual identifier names are irrellevant (only the use-def links are significant) e ::= n | x | e e’ | let x = e in e’ { +, -, * } …where: x + let x = 5 in x + let x = 1 + x in x * x Definition (def) Usage (use): Free use Bound use use-def link Terminology definition scope for ' x '
22
22 Semantics Q1 2007 Free Variables Determine the free variables? FV: Exp P(Var) –FV( n ) = Ø –FV( x ) = { x } –FV( e e’ ) = FV( e ) FV( e’ ) –FV( let x=e in e’ ) = FV( e ) ( FV( e’ ) \ { x } ) e ::= n | x | e e’ | let x = e in e'
23
23 Semantics Q1 2007 Dynamic Semantics (Trans. Sys.) Define environments: recall terminology (no side-effects) Configurations: i.e., numbers Transition relation: ; writing for = { } T = { | z Z } Env = Var Z Note: this is done differently in the note [SOS]: through “relative transition systems” that are (implicitly) relative to an environment. |- e e (, ) ‘ ’
24
24 Semantics Q1 2007 Dynamic Semantics [n] | [x] | [e e] Structural Operational Semantics: [ BOP 1 ] [ BOP 2 ] [ BOP 3 ] [ VAR ] |- e 0 e 1 e 0 ’ e 1 |- e 0 e 0 ’ |- z 0 z 1 z z = z 0 z 1 |- z 0 e 1 z 0 e 1 ’ |- e 1 e 1 ’ |- x z z = (x)
25
25 Semantics Q1 2007 Dynamic Semantics [ let-in ] Structural Operational Semantics: »Note: the premises work in the extended environment Live exercise: eval in env: [ LET 2 ] [ LET 3 ] [ LET 1 ] |- let x = e 0 in e 1 let x = e 0 ’ in e 1 |- e 0 e 0 ’ |- let x = z in n n |- let x = z in e 1 let x = z in e 1 ’ [x z] |- e 1 e 1 ’ let x=y in x [y7][y7]
26
26 Semantics Q1 2007 C OMPOUND D EFINITIONS
27
27 Semantics Q1 2007 Compound Definitions Consider language Let++: –Expressions ( e Exp): –Definitions (d Def): e ::= n | x | e e’ | let d in e d ::= nil | x = e | d ; d’ | d and d’ | d in d’ x + 3y = x+7 Graphically: Z Z Env [x1][x1] [x1][x1][y8][y8] 4 { +, -, * } …where:
28
28 Semantics Q1 2007 Graphically: [x = e] | [d ; d’] Definition block: –E.g.: Sequential definition: –E.g.: let d in e d 0 ; d 1 let x=a ; y=b in b * c + y a b c x y b c >
29
29 Semantics Q1 2007 Graphically: [d and d’] | [d in d’] Simultaneous definition: –E.g.: Private definition: –E.g.: d 0 and d 1 d 0 in d 1 >
30
30 Semantics Q1 2007 C OMPOUND D EFINITIONS Semantics of Let++
31
31 Semantics Q1 2007 Static Semantics: Let++ Problem: I.e. non-disjoint simultaneous definitions! Solution: static semantics def / exp “well-formedness relations”: ‘|- wfd ’ / ‘|- wfe ’: let x = 1 and x = 2 in x | _ wfe e | _ wfe e’ | _ wfe e e’ Exp well-formedness: | _ wfe n | _ wfe x | _ wfd d | _ wfe e | _ wfe let d in e Definition well-formedness: | _ wfd nil | _ wfe e | _ wfd x = e | _ wfd d | _ wfd d’ | _ wfd d ; d’ | _ wfd d | _ wfd d’ | _ wfd d and d’ | _ wfd d | _ wfd d’ | _ wfd d in d’ DV( d ) DV( d’ ) = Ø
32
32 Semantics Q1 2007 Recall: Free Variables for Let Let: –FV( n ) = Ø –FV( x ) = { x } –FV( e e’ ) = FV( e ) FV( e’ ) –FV( let x=e in e’ ) = FV( e ) ( FV( e’ ) \ { x } ) Let++: –FV( let d in e’ ) = FV( d ) ( FV( e’ ) \ DV( d ) ) –So we need FV( d ) and DV( d ) …
33
33 Semantics Q1 2007 Defining Variables and Free Variables DV: Def P(Var) –DV( nil ) = Ø –DV( x = e )= { x } –DV( d ; d’ ) = DV( d ) DV( d’ ) –DV( d and d’ )= DV( d ) DV( d’ ) –DV( d in d’ )= DV( d’ ) FV: Def P(Var) –FV( nil ) = Ø –FV( x = e )= FV( e ) –FV( d ; d’ ) = FV( d ) ( FV( d’ ) \ DV( d ) ) –FV( d and d’ )= FV( d ) FV( d’ ) –FV( d in d’ )= FV( d ) ( FV( d’ ) \ DV( d ) ) + should be disjoint union d ::= nil | x = e | d ; d’ | d and d’ | d in d’
34
34 Semantics Q1 2007 Dynamic Semantics Expression Configurations: –Problem: what should definitions evaluate to? Environments (like expressions evaluate to numbers) –Solution: “environments as results” (add intermediate syntax): Definition Configurations: E = { | |- wfe e } T E = { | z Z } only well-formed exps d ::= nil | x = e | d ; d’ | d and d’ | d in d’ | 0 D = { | |- wfd d } T D = { } only well-formed defs Question: Why not (just) define: D = { } ∪ Env T D = Env ??? Note: This is a static property!
35
35 Semantics Q1 2007 Dynamic Semantics: Exp Structural Operational Semantics: »Note: the premisis works in the 0 - extended environment [ LET 2 ] E [ LET 3 ] E [ LET 1 ] E |- let d in e let d’ in e |- d d’ |- let 0 in n n |- let 0 in e let 0 in e’ [ 0 ] |- e e’
36
36 Semantics Q1 2007 Structural Operational Semantics: [ NIL ] D |- nil [] [ DEF 1 ] D |- x = e x = e’ |- e e’ [ DEF 2 ] D |- x = n [x n] Dynamic Sem.: Def [nil] | [x=e]
37
37 Semantics Q1 2007 Dynamic Sem.: Def [d;d’] Structural Operational Semantics: »Note: the premises works in the 0 - extended environment »Note: the result is the combined environment, 0 [ 1 ] [ SEQ 2 ] D [ SEQ 3 ] D [ SEQ 1 ] D |- d 0 ; d 1 d 0 ’ ; d 1 |- d 0 d 0 ’ |- 0 ; 1 0 [ 1 ] |- 0 ; d 1 0 ; d 1 ’ [ 0 ] |- d 1 d 1 ’ Live exercise: evaluate in environment: x = 2 ; x = x+y [ y = 1]
38
38 Semantics Q1 2007 Dynamic Sem.: Def [d and d’] Structural Operational Semantics: »Notice the independence of the two operands ( d 0 & d 1 ) »Note: the result is the disj. combined environment, 0 1 [ AND 2 ] D [ AND 3 ] D [ AND 1 ] D |- d 0 and d 1 d 0 ’ and d 1 |- d 0 d 0 ’ |- 0 and 1 0 1 |- 0 and d 1 0 and d 1 ’ |- d 1 d 1 ’ Q: what happens if operands do not contain disjoint definitions (e.g. "x=1 and x=2") A: trans. sys. was only def'd for well-formed exps/defs! Hence, no such runtime errors!
39
39 Semantics Q1 2007 Dynamic Sem.: Def [d in d’] Structural Operational Semantics: »Note: the premises works in the 0 - extended environment »Note: the result is only the last environment, 1 [ IN 2 ] D [ IN 3 ] D [ IN 1 ] D |- d 0 in d 1 d 0 ’ in d 1 |- d 0 d 0 ’ |- 0 in 1 1 |- 0 in d 1 0 in d 1 ’ [ 0 ] |- d 1 d 1 ’
40
40 Semantics Q1 2007 T YPE C HECKING D EFINITIONS
41
41 Semantics Q1 2007 Adding Boolean Variables: Let B ++ Consider language Let B ++: –Mixed Expressions ( e Exp): –Definitions (d Def): –Note: The type of a var is now context dependent I.e. a context-free (grammar) approach will not suffice –Examples: ? ? … e ::= n | t | x | ~ e | e e’ | e ? e’ : e” | let d in e d ::= nil | x = e | d ; d’ | d and d’ | d in d’ { +, -, *, =, or } …where: x or tt x * x type definition (annotation) { bool, int } …where:
42
42 Semantics Q1 2007 Static Semantics: Let B ++ FV E, DV D and FV D as before …adding: We now need type environments: Type = { bool, int } Define static semantics (type checking rel’s): “ e has type (given type environment )” “ d yields type env. (given type env. )” FV E ( e ? e’: e” ) = FV E ( e ) FV E ( e’ ) FV E ( e” ) TEnv V = Var Type |- e : |- d :
43
43 Semantics Q1 2007 Static Semantics (expressions) Expressions: [ NUM ] E |- n : int |- t : bool [ TVL ] E [ VAR ] E |- x : (x) [ VAR ] E |- ~ e : bool |- e : bool [ LET ] E |- let d in e : |- d : [ ] |- e : [ IFE ] E |- e 0 ? e 1 : e 2 : |- e 0 : bool |- e 1 : 1 |- e 2 : 2 = 1 =2 = 1 =2 [ BOP ] E |- e 0 e 1 : type ( 0, 1 ) |- e 0 : 0 |- e 1 : 1
44
44 Semantics Q1 2007 Static Semantics (definitions) Definitions: [ NIL ] D |- nil : [] [ DEF ] D |- x = e : [x ] |- e : ’ = ’ = ’ [ SEQ ] D |- d 0 ; d 1 : 0 [ 1 ] |- d 0 : 0 [ 0 ] |- d 1 : 1 [ AND ] D |- d 0 and d 1 : 0 1 |- d 0 : 0 |- d 1 : 1 [ IN ] D |- d 0 in d 1 : 1 |- d 0 : 0 [ 0 ] |- d 1 : 1 DV( d 0 ) DV( d 1 ) = Ø Note: combined environment Note: only last environment Note: disjoint environment Note: type check
45
45 Semantics Q1 2007 Dynamic Semantics: Let B ++ Type Environment (Compile-time): Type = { bool, int } Environment (Runtime): Val = B Z, B = { tt, ff } –Define type correspondence relation ‘~’: –Note: TEnv = Var Type Env = Var Val ~ x V: ( x ) = bool ( x ) B ( x ) = int ( x ) Z ( 0 ~ 0 1 ~ 1 ) 0 [ 1 ] ~ 0 [ 1 ]
46
46 Semantics Q1 2007 Exp. Transition System for Let B ++ Let++ Expression Configurations: Let B ++ Expression Configurations: – Var Type: Env := »i.e., “only type corresponding runtime environments” = { | |- wfe e } T = { | z Z } only well-formed exps = { | Env : |- e : } T = { | Env r B Z } { ( Var B Z ) | ~ }
47
47 Semantics Q1 2007 Def. Transition System for Let B ++ Let++ Definition Configurations: Let B ++ Definition Configurations: – Var Type: Env := »i.e., “only type corresponding runtime environments” V = { | |- wfd d } only well-formed defs T V = { } = { | Env : |- V d : } T = { | Env } { ( Var B Z ) | ~ }
48
48 Semantics Q1 2007 Exp. Dynamic Semantics: Let B ++ SOS of Expressions for Let B ++: –E.g. [LET]: [ LET 2 ] [ LET 3 ] [ LET 1 ] Env |- let d in e let d’ in e |- d d’ |- let 0 in r r |- let 0 in e let 0 in e’ [ 0 ] |- e [ 0] e’ 0 ~ 00 ~ 0
49
49 Semantics Q1 2007 Def. Dynamic Semantics: Let B ++ SOS of Definitions for Let B ++: –E.g [SEQ]: [ SEQ 2 ] [ SEQ 3 ] [ SEQ 1 ] |- d 0 ; d 1 d 0 ’ ; d 1 |- d 0 d 0 ’ |- 0 ; 1 0 [ 1 ] |- 0 ; d 1 0 ; d 1 ’ [ 0 ] |- d 1 [ 0] d 1 ’ Env 0 ~ 00 ~ 0
50
50 Semantics Q1 2007 "Three minutes paper" Please spend three minutes writing down the most important things that you have learned today (now). After 1 day After 1 week After 3 weeks After 2 weeks Right away
51
51 Semantics Q1 2007 Next week (CCS): Concurrency and Communication Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.