Commit 6d899914 authored by Mário Pereira's avatar Mário Pereira
Browse files

Extraction doc (wip)

parent 14b414d3
......@@ -71,14 +71,126 @@ files \texttt{max\} and \texttt{}:
> ocamlbuild -pkg zarith main.native
Since Why3's type
Since \why's type
\texttt{int} is translated to OCaml arbitrary precision integers using
the \texttt{ZArith} library, we have to pass option \texttt{-pkg
zarith} to \texttt{ocamlbuild}. In order to get extracted code that
uses OCaml's native integers instead, one has to use Why3's types for
uses OCaml's native integers instead, one has to use \why's types for
63-bit integers from libraries \texttt{} and
\paragraph{Extraction starting point} The \texttt{extract} command accepts three
different targets for extraction: a \whyml file, a module, or a symbol
(function, type, exception). To extract all the symbols from every module of a
file named \texttt{f.mlw}, one should write
> why3 extract -D <driver> f.mlw
To extract only the symbols from module \texttt{M} of file \texttt{f.mlw}, one
should write
> why3 extract -D <driver> -L <dir> f.M
To extract only the symbol \texttt{s} (a function, a type, or an exception) from
module \texttt{M} of file \texttt{f.mlw}, one should write
> why3 extract -D <driver> -L <dir> f.M.s
Note the use of~\texttt{-L <dir>}, for both extraction of a module and a symbol,
in order to state the location of file \texttt{f.mlw}.
\item[\texttt{-{}-flat}] performs a flat extraction, \emph{i.e.}, everything is
extracted into a single file. This is the default behavior. The \texttt{-o}
option should be given the name of a file or, if omitted, the result of
extraction is printed to the standard output.
\item[\texttt{-{}-modular}] each module is extracted in its own, separated
file. The \texttt{-o} option cannot be omitted, and it should be given the
name of an existing directory. This directory will be populated with the
resulting OCaml files.
\item[\texttt{-{}-recursive}] recursively extracts all the dependencies of the
chosen entry point. This option is valid for both \texttt{modular} and
\texttt{flat} options.
We discuss different uses of the \texttt{extract} command though some examples.
Consider the program in Figure~\ref{fig:NQueens2} on
page~\pageref{fig:NQueens2}, where function \texttt{queens} computes a solution for
the \texttt{N}-Queens problem. If we are only interested in extracting this
function, we can proceed as follows:
> why3 extract -D ocaml64 -L . queens.NQueens.queens -o
Here we assume the program is contained in a file \texttt{queens.mlw} and that
we invoke \texttt{extract} from the directory where this file is stored. File
\texttt{} now contains the following OCaml code:
let queens (board: Z.t array) (n: Z.t) : unit =
bt_queens board n (Z.of_string "0")
This code cannot be type-checked by the OCaml compiler, as it depends on
function \texttt{bt\_queens} whose definition is not given. In order to obtain a
\emph{correct} OCaml implementation, we perform a recursive extraction:
> why3 extraction --recursive -D ocaml64 -L . \
queens.NQueens.queens -o
File \texttt{} contents is updated to the code shown in
exception Solution
exception Inconsistent of Z.t
let check_is_consistent (board: Z.t array) (pos: Z.t) : bool =
begin try
let rec for_loop_to (q: Z.t) : unit =
if Z.leq q (Z.sub pos (Z.of_string "1"))
then begin
let bq = board.(Z.to_int q) in
let bpos = board.(Z.to_int pos) in
if Z.equal bq bpos then begin raise (Inconsistent q) end;
if Z.equal (Z.sub bq bpos) (Z.sub pos q)
then begin
raise (Inconsistent q) end;
if Z.equal (Z.sub bpos bq) (Z.sub pos q)
then begin
raise (Inconsistent q) end
end; for_loop_to (Z.add q (Z.of_string "1"))
end end in
for_loop_to (Z.of_string "0"); true
end with
| Inconsistent q -> false
let rec bt_queens (board: Z.t array) (n: Z.t) (pos: Z.t) : unit =
if Z.equal pos n then begin raise Solution end;
let rec for_loop_to1 (i: Z.t) : unit =
if Z.leq i (Z.sub n (Z.of_string "1"))
then begin
board.(Z.to_int pos) <- i;
if check_is_consistent board pos
then begin
bt_queens board n (Z.add pos (Z.of_string "1")) end;
for_loop_to1 (Z.add i (Z.of_string "1"))
end end in
for_loop_to1 (Z.of_string "0")
let queens (board: Z.t array) (n: Z.t) : unit =
bt_queens board n (Z.of_string "0")
\caption{Recursive extraction of \texttt{queens} function.}
%%% Local Variables:
%%% mode: latex
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment