Separation Logic for a Higher-Order Typed Language

Separation Logic for a Higher-Order Typed Language

Separation Logic for a Higher-Order Typed Language [Extended Abstract] Neelakantan Krishnaswami John Reynolds Jonathan Aldrich Carnegie Mellon University Carnegie Mellon University Carnegie Mellon University 5000 Forbes Avenue 5000 Forbes Avenue 5000 Forbes Avenue Pittsburgh, PA 15213 Pittsburgh, PA 15213 Pittsburgh, PA 15213 neelk+@cs.cmu.edu jcr+@cs.cmu.edu jcr+@cs.cmu.edu ABSTRACT Separation logic, originally developed by John Reynolds Separation logic is an extension of Hoare logic which per- and Peter O’Hearn [18, 10, 8], and elaborated by many mits reasoning about low-level imperative programs that use others, is a powerful extension of Hoare logic for reasoning shared mutable heap structure. In this work, we create an about pointer programs. To handle the difficulties described extension of separation logic that permits effective, modu- above, separation logic extends the assertion language of lar reasoning about typed, higher-order functional programs Hoare logic with spatial connectives. They are spatial in that use aliased mutable heap data, including pointers to the sense that they permit formulas to range over just part code. of the heap: a spatial proposition p ∗ q, written using the “separating conjunction”, holds when p holds for one part of the heap, and q holds for the other part of it. Thus, aliasing Categories and Subject Descriptors between p and q is implicitly forbidden, removing the need D.3.0 [Programming Languages]: General; F.3.1 [Logic to write tedious and extensive noninterference conditions. and Meaning of Programs]: Specifying and Verifying Separation further enables local reasoning about the heap: and Reasoning About Programs If one command in a program does its job in a heap frag- ment described by a proposition p, and another does its job in a fragment described by q, then we can safely combine General Terms the commands to work together in a heap described by p ∗ q Separation Logic — the separating conjunction’s disjointness property means that the two subroutines cannot interfere with each other. However, most of the work on separation logic has fo- Keywords cused on low-level languages, which are strictly first-order Separation logic, specification logic, imperative, functional, and where features like the precise layout of data in memory aliasing, monads, ML, call-by-value and identity of pointers as integers are visible. In this work, we define a specification logic for a a high- level language: a monadic lambda calculus in which pointers 1. INTRODUCTION are a separate data type. Pointers may contain values of any Traditionally, pointer programs – programs which make type, including code (denoted by closures), such as functions use of aliased, mutable data values – have been difficult to and frozen monadic computations. Thus, our programming analyze, for two reasons. First, aliasing allows an assign- language resembles a subset of ML, except for the use of ment to a pointer to nonlocally affect other variables that monadic form to make effects and their order of execution contain the same pointer. This requires specifications to more explicit. Our program logic uses separation logic as the explicitly enumerate all the possible interference patterns essential mechanism for reasoning about imperative compu- between mutable variables and fields – and the number of tations, and simultaneously permits us to use the equational possible interference clauses can rise quadratically with the theory of the lambda calculus to reason about the functional number of such objects in a program. Second, the heap is a aspect of programs. global data structure, and changes to it are visible to every In Section 2, we give the static and dynamic semantics part of a program, greatly complicating efforts at modular of our language, and then in Section 3 define an assertion reasoning. language with the separating conjunction and implication in it and give its semantics. Finally, in Section 4 we define a notion of Hoare triple for our language, and then build a specification logic on top of that, which uses triples (contain- Permission to make digital or hard copies of all or part of this work for ing separation logic formulas) as its atomic formulas. This personal or classroom use is granted without fee provided that copies are gives us a two-level logic in which we can relatively easily not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to write program specifications. republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. 2. THE MONADIC LANGUAGE SPACE 2006 Charlestown, SC USA Copyright 200X ACM X-XXXXX-XX-X/XX/XX ...$5.00. The programming language we work with is a monadic call-by-value lambda calculus. Following Davies and Pfen- The decision to explicitly model what would happen with ning [7], we stratify the language into two syntactic cate- dangling pointers ends up substantially simplifying the se- gories, expressions and computations, which are described mantics of the new connectives of the assertion language in Figure 1. Expressions are the purely functional fragment of separation logic, as we will see in the next two sections. of the language, where evaluation always terminates and Informally, we use separation logic to reason about heap corresponds to the beta-rule of the lambda calculus. fragments, and the abort state lets the reduction relation We type expressions using the typing judgement Γ ` e : τ, “tell us” when a partial heap did not contain enough data described in Figure 2. Expressions include the usual in- for evaluation to proceed. troduction and elimination forms for sum, product, and Also, one standard choice still worth drawing attention function types. Additionally, we have reference types and to is that dynamic allocation via newτ v is nondeterministic. monadic types. The evaluation rule for allocation promises to return a new The type ref τ classifies references that point to a value pointer not in the domain of the heap, but does not say what of type τ. Departing slightly from the usual presentation, value that pointer will take on. This is important, because it pointer values take on the form lτ – that is, a pointer is is necessary in order for the frame property discussed below tagged with the type of the value it points to. This simplifies to hold. the semantics of the separation logic assertions, which will The metatheory of this language is fairly standard. The be described in the next section. A computation c that only wrinkle is that we have to prove soundness twice, one produces a value of type τ, can be suspended and turned for each of the two judgements for expressions and compu- into a first-class value [c] of the monadic type τ. A value of tations. We have the usual proof of type soundness for the monadic type is a frozen computation and does not evaluate expression language via the progress and type preservation any further, which keeps side-effects from infecting the pure lemmas. part of the language. Neither reference values nor frozen computations have any Proposition 1 (Expression Progress). If ` e : τ, elimination rules in the expression language, which ensures then either e is a value or there exists an e0 such that e ; e0. that no expression can have a side-effect. We make this syn- tactically apparent in the dynamic semantics of expressions, Proposition 2 (Expression Subject Reduction). 0 0 given in Figure 5, by simply leaving out the store altogether If ` e : τ and e ; e , then ` e : τ. from its reduction relation (e ; e0). We have a judgement Γ ` c ÷ τ, given in Figure 3, which These are proved by structural induction on typing deriva- characterizes well-formed computations. A computation is tions and evaluation derivations, respectively. either an expression (which becomes a computation with- Additionally, we also show that expressions always reduce ∗ out side-effects); reading (!e), writing (e := e0), or creating to a value. We take e ; v to be the transitive closure of (newτ e) a reference; or sequencing two computations via the one-step evaluation relation. monadic sequencing with the form letv x = e in c.1 Se- quencing takes an expression of type τ, evaluates it to a Proposition 3 (Termination). If ` e : τ, then there ∗ value [c0], and then executes c0 and passes the result (a value exists a v such that e ; v. plus side-effects) to c. As an example, consider the following program, which is We prove this using a straightforward logical relations ar- a computation of type nat. gument [17], though it also follows immediately from the fact that the simply typed lambda calculus is strongly nor- letv r = [new 5] in malizing. letv dummy = [r := 17] in Once we have soundness for our expression language, we letv n = [!r] in can use it to prove soundness for computation terms. (n - 5) Here, we have a computation which creates a new pointer, Proposition 4 (Partial Computation Progress). If ` c÷τ and σ ok, then either c is a value v, or there exists pointing to 5, and then updates it to point to 17, and then a c0 and σ0 such that hc; σi ; hc0; σ0i, or hc; σi ; abort. dereferences the pointer and returns the dereferenced value minus 5, for a final result of 12. We freeze basic commands Proposition 5 (Computation Subject Reduction). and turn them into monadic values when we put them in If ` c ÷ τ and σ ok and hc; σi ; hc0; σ0i, then ` c0 ÷ τ brackets (e.g., [!r]), and then we can run them in the se- and σ0ok. quential order using the monadic let-binding construct.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us