Separation logic

Separation logic

Adding the heap

  • memory writes , [ E 1 ] : = E 2 [E_1]:=E_2 [E1]:=E2
  • memory reads, x : = [ E ] x:=[E] x:=[E]
  • memory allocation, x : = c o n s ( E 1 , ⋯   , E n ) x:=cons(E_1,\cdots,E_n) x:=cons(E1,,En)
  • memory deallocation, d i s o p o s e   E disopose\ E disopose E
    s t a c k : v a r − > v a l u e stack :var->value stack:var>value
    h e a p : l o c − > v a l u e heap :loc->value heap:loc>value
    l o c ⊆ v a l u e loc \subseteq value locvalue

Operational semantics

E / s → v x : = E / ( s , h ) → s k i p / ( s [ x : = v ] , h ) {E/s \to v \over x:=E/(s,h) \to skip/(s[x:=v],h)} x:=E/(s,h)skip/(s[x:=v],h)E/sv
E / s → v x : = [ E ] / ( s , h ) → s k i p / ( s [ x : h ( v ) ] , h ) {E/s \to v \over x:=[E]/(s,h) \to skip/(s[x:h(v)],h) } x:=[E]/(s,h)skip/(s[x:h(v)],h)E/sv
E 1 / s → v 1   E 2 / s → v [ E 1 ] : = E 2 / ( s , h ) → s k i p / ( s , h [ v 1 : = v 2 ) ] ) {E_1/s \to v_1 \ E_2/s \to v \over [E_1]:=E_2/(s,h)\to skip /(s,h[v1:=v2)])} [E1]:=E2/(s,h)skip/(s,h[v1:=v2)])E1/sv1 E2/sv
E 1 / s → v 1   ⋯ E n / s → v n      v ⋯ v + ( n − 1 ) ∉ d o m ( h ) x : c o n s ( E 1 , ⋯   , E n ) / ( s , h ) → s k i p / ( s [ x : v ] , h ⊕   v : = v 1 , ⋯ v + ( n − 1 ) : = v n {E_1/s \to v_1 \ \cdots E_n/s \to v_n \ \ \ \ v \cdots v+(n-1)\notin dom(h) \over x:cons(E_1,\cdots,E_n)/(s,h)\rightarrow skip/(s[x:v],h\oplus \ v:=v1,\cdots v+(n-1):=v_n} x:cons(E1,,En)/(s,h)skip/(s[x:v],h v:=v1,v+(n1):=vnE1/sv1 En/svn    vv+(n1)/dom(h)
E / s → v d i s p o s e E / ( s , h ) → s k i p / ( s , h \ v ) {E/s \to v \over dispose E/(s,h) \to skip /(s,h\backslash v)} disposeE/(s,h)skip/(s,h\v)E/sv
R e m a r k : h [ v : v ′ ]   a n d   h \ v a r e d e f i n e d o n l y i f v ∈ d o m ( h ) \bm {Remark}: h[v:v'] \ and \ h\backslash v are defined only if v \in dom(h) Remark:h[v:v] and h\varedefinedonlyifvdom(h)

Frame

{ P } C { Q } { R ∗ P } C { Q ∗ R } \{P\}C\{Q\} \over \{R*P\}C\{Q*R\} {RP}C{QR}{P}C{Q}

statements of separation logic

P , Q : : = T     t r u e ∣ ¬ P       ∣ P ∧ Q ∣ P ∨ Q ∣ S          ∣ P ∗ Q ∣ E 1 ↦ E 2 ∣ e m p t y P,Q :: =T \ \ \ true \\ | \lnot P \ \ \ \ \ \\ | P \land Q\\ | P \lor Q \\ | S \ \ \ \ \ \ \ \ \\ \\ | P*Q\\ | E_1\mapsto E_2\\ | empty P,Q::=T   true¬P     PQPQS        PQE1E2empty
( s , h ) ⊨ e m p t y   i f f   d o m ( h ) = ∅ (s,h) \vDash empty\ iff \ dom(h) = \varnothing (s,h)empty iff dom(h)=
( s , h ) ⊨ E 1 ↦ E 2   i f f   E 1 / s → v 1 ∧ E 2 / s → v 2 ∧ d o m ( h ) = v 1 ∧ h ( v 1 ) = v 2 ( s , h ) ⊨ P ∗ Q   i f f ∃ h 1 , h 2 . d o m ( h 1 ) ∩ d o m ( h 2 ) = ∅ ∧ h 1 ⊕ h 2 = h ∧ ( s , h 1 ) ⊨ P ∧ ( s , h 2 ) ⊨ Q (s,h) \vDash E_1 \mapsto E_2 \ iff \ E_1/s \to v_1 \land E_2/s \to v_2 \land dom(h) = v_1 \land h(v_1)=v_2 \\ (s,h) \vDash P*Q \ iff \\ \exists h_1,h_2.dom(h_1) \cap dom(h_2) = \varnothing \land h_1 \oplus h_2 =h \land (s,h_1) \vDash P \land (s,h_2) \vDash Q (s,h)E1E2 iff E1/sv1E2/sv2dom(h)=v1h(v1)=v2(s,h)PQ iffh1,h2.dom(h1)dom(h2)=h1h2=h(s,h1)P(s,h2)Q

Date types:list

  • l i s t   [ ]   x ≡ e m p t y ∧ x = n i l list \ []\ x \equiv empty \land x = nil list [] xemptyx=nil
  • l i s t   v 1 : : α   x ≡ ∃ j . x ↦ v 1 ( X + 1 ↦ j ) ∗ l i s t   α   j list \ v_1:: \alpha \ x \equiv \exists j.x \mapsto v_1(X+1 \mapsto j)*list \ \alpha \ j list v1::α xj.xv1(X+1j)list α j

Data types :list segment

  • l s e g   [ ]   ( x , y ) ≡ e m p t y ∧ x = y lseg \ []\ (x,y) \equiv empty \land x=y lseg [] (x,y)emptyx=y
  • l s e g   v : : α ( x , y ) ≡ ∃ j . x ↦ v ∗ ( x + 1 ↦ j ) ∗ l s e g   α ( j , y ) lseg \ v::\alpha(x,y) \equiv \exists j.x \mapsto v*(x+1\mapsto j)* lseg \ \alpha(j,y) lseg v::α(x,y)j.xv(x+1j)lseg α(j,y)

Exercise: prove,by structural induction on α \alpha α,that:

l s e g   α ⋅ β    ⟺      ∃   j . l s e g   α ( x , y ) ∗ l s e g   β ( j , y ) lseg \ \alpha \cdot \beta \iff \ \exists\ j.lseg \ \alpha(x,y)*lseg\ \beta(j,y) lseg αβ  j.lseg α(x,y)lseg β(j,y)

(Local)axioms

  • write : { E ↦ _ } [ E ] = E ′ { E ↦ E ′ } \{E \mapsto \_ \} [E] =E'\{E \mapsto E'\} {E_}[E]=E{EE}
  • dispose: { E ↦ _ } d i s p o s e ( E ) { e m p t y } \{E \mapsto \_\}dispose(E)\{empty\} {E_}dispose(E){empty}
  • alloc: { e m p t y } x = c o n s ( E 1 , … , E n ) { x ↦ E 1 ∗ x + 1 ↦ E 2 ∗ … x + ( n − 1 ) ↦ e n } \{empty\}x =cons(E_1,\ldots,E_n)\{x \mapsto E_1 *x+1 \mapsto E_2* \ldots x +\\(n-1) \mapsto e_n\} {empty}x=cons(E1,,En){xE1x+1E2x+(n1)en}

Exercises:prove that:

{ l s e g   α ( i , j ) ∗ j ↦ a , k } k : c o n s ( a , i ) ; i : = k { l s e g   a ⋅ α ( i , j ) } { l s e g   α ( i , j ) ∗ j ↦ a , k } l : = c o n s ( b , k ) ; [ j + 1 ] = l { l s e g   α ⋅ a ⋅ b ( i , k ) } { l s e g   a ⋅ α ( i , k ) } j : = [ i + 1 ] ; d i s p o s e   i ; d i s p o s e   i + 1 ; i : = j   { l s e g   α ( i , k ) } \{lseg \ \alpha(i,j)*j \mapsto a,k\}k:cons(a,i);i:=k \{ lseg \ a \cdot \alpha(i,j)\} \\ \{lseg \ \alpha(i,j)*j \mapsto a,k\}l:=cons(b,k);[j+1]=l \{ lseg \ \alpha \cdot a \cdot b(i,k)\} \\ \{lseg \ a \cdot \alpha(i,k)\} j:=[i+1];dispose \ i ; dispose \ i+1; i:=j \ \{lseg \ \alpha(i,k)\} {lseg α(i,j)ja,k}k:cons(a,i);i:=k{lseg aα(i,j)}{lseg α(i,j)ja,k}l:=cons(b,k);[j+1]=l{lseg αab(i,k)}{lseg aα(i,k)}j:=[i+1];dispose i;dispose i+1;i:=j {lseg α(i,k)}
Remember:
l s e g   [ ]   ( x , y ) ≡ e m p t y ∧ x = y lseg \ [] \ (x,y) \equiv empty \land x=y lseg [] (x,y)emptyx=y
l s e g   v : : α ( x , y ) ≡ ∃ j . x ↦ v ∗ ( x + 1 ↦ j ) ∗ l s e g   α ( j , y ) lseg \ v::\alpha(x,y) \equiv \exists j.x \mapsto v*(x+1 \mapsto j)* lseg \ \alpha(j,y) lseg v::α(x,y)j.xv(x+1j)lseg α(j,y)
Notation : j ↦ a , k j \mapsto a,k ja,k stands for j ↦ a ∗ j + 1 ↦ k j \mapsto a*j+1 \mapsto k jaj+1k

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值