29.01.2014 Views

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 11. PROGRAM VERIFICATION 100<br />

let vcgen (wc : wctxt) (fd : fundec) (pre : T.term option) : T.term → T.term =<br />

(fun t → T.t forall close (vsymbols <strong>of</strong> function wc fd) [ ] (pre impl t wc fd pre t))<br />

The function validateWhyCtxt Adds the term p to the Why3 context as a pro<strong>of</strong> goal, and invokes<br />

the external prover. In this example the prover is given a timeout <strong>of</strong> two minutes, and the result<br />

is echoed to the terminal. One might also choose to use a pro<strong>of</strong> assistant such as Coq [2] as the<br />

back-end to Why3, in which case, we could enter into an interactive pro<strong>of</strong> session.<br />

Alternately, there are a range <strong>of</strong> options for what could be done here. We could do anything from<br />

simply directing Why3 to emit all <strong>of</strong> the pro<strong>of</strong> obligations for a program for examination o-line, to<br />

halting compilation and emitting an error if a pro<strong>of</strong> obligation is not discharged during compilation.<br />

The right choice likely depends on the stage <strong>of</strong> development the code is in, not to mention the goals<br />

<strong>of</strong> the t<br />

let validateWhyCtxt (w : wctxt) (p : T.term) : unit = (∗...∗)<br />

The function processFunction initializes the Why3 context with fresh variables for the local variables<br />

and formal parameters <strong>of</strong> the function before checking to see if it has any postconditions. If<br />

so, it tries to nd a precondition, generates the verication condition for the postcondition, and<br />

nally invokes validateWhyCtxt on the resulting term.<br />

let processFunction (wc : wctxt) (fd : fundec) (loc : location) : unit =<br />

wc.vars ←<br />

L.fold left (fun m vi → SM.add vi.vname (make symbol vi.vname) m)<br />

SM.empty (fd.slocals @ fd.sformals);<br />

match post <strong>of</strong> function wc fd with<br />

| None → ()<br />

| Some g →<br />

let pre = pre <strong>of</strong> function wc fd in<br />

let vc = vcgen wc fd pre g in<br />

validateWhyCtxt wc vc<br />

The function tut11 is the entry point for this module. It initializes the Why3 context and then<br />

iterates over all functions in the le.<br />

let tut11 (f : file) : unit =<br />

let wc = initWhyCtxt (!Ciltutoptions.prover) (!Ciltutoptions.prover version) in<br />

iterGlobals f (onlyFunctions (processFunction wc));<br />

eraseAttrs f

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!