Commit 7b0513b1 authored by Martin Clochard's avatar Martin Clochard

New example: eratosthene sieve

parent 3340cf05
module Sieve
use import int.Int
use import array.Array
use import ref.Ref
use import number.Prime
predicate no_factor_lt (bnd num:int) =
num > 1 /\ forall k l. 1 < l < bnd /\ k > 1 -> num <> k * l
let incr (r:ref int) : unit
ensures { !r = old !r + 1 }
= r := !r + 1
let sieve (n:int) : array bool
requires { n > 1 }
returns { m -> length m = n /\ forall i. 0 <= i < n -> m[i] <-> prime i }
= let t = Array.make n true in
t[0] <- false;
t[1] <- false;
let i = ref 2 in
while !i < n do
invariant { 1 < !i <= n }
invariant { forall j. 0 <= j < n -> t[j] <-> no_factor_lt !i j }
variant { n - !i }
if t[!i] then begin
let r = ref !i in
while !r * !i < n do
invariant { 1 < !r <= n }
invariant { forall j. 0 <= j < n ->
t[j] <-> (no_factor_lt !i j /\
forall k. 1 < k < !r -> j <> k * !i) }
variant { n - !r }
t[!r * !i] <- false;
incr r;
done;
assert { forall j. 0 <= j < n /\ t[j] ->
(forall k l. 1 < l < !i + 1 -> j = k * l /\ k > 1 ->
(if l = !i then k < !r && false else false) && false) &&
no_factor_lt (!i+1) j }
end else assert { forall j. 0 <= j < n /\ no_factor_lt !i j ->
(forall k l. 1 < l < !i + 1 -> j = k * l /\ k > 1 ->
(if l = !i then (forall k0 l. 1 < l < !i /\ k0 > 1 /\ !i = k0 * l ->
j = (k*k0) * l && false) && false
else false) && false) && no_factor_lt (!i+1) j };
incr i
done;
assert { forall j. 0 <= j < n /\ no_factor_lt n j -> prime j };
assert { forall j. 0 <= j < n /\ prime j ->
forall k l. 1 < l < n /\ k > 1 -> j = k * l -> l >= j && false };
t
end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE why3session PUBLIC "-//Why3//proof session v5//EN"
"http://why3.lri.fr/why3session.dtd">
<why3session shape_version="4">
<prover id="0" name="Alt-Ergo" version="0.99.1" timelimit="5" memlimit="1000"/>
<prover id="1" name="Z3" version="4.3.1" timelimit="5" memlimit="1000"/>
<prover id="5" name="Eprover" version="1.8-001" timelimit="5" memlimit="1000"/>
<file name="../eratosthene.mlw" expanded="true">
<theory name="Sieve" sum="a84c73b798e84f0d280c74dcdb167347" expanded="true">
<goal name="WP_parameter incr" expl="VC for incr">
<proof prover="0"><result status="valid" time="0.01" steps="4"/></proof>
</goal>
<goal name="WP_parameter sieve" expl="VC for sieve">
<transf name="split_goal_wp">
<goal name="WP_parameter sieve.1" expl="1. array creation size">
<proof prover="0"><result status="valid" time="0.02" steps="4"/></proof>
</goal>
<goal name="WP_parameter sieve.2" expl="2. index in array bounds">
<proof prover="0"><result status="valid" time="0.02" steps="5"/></proof>
</goal>
<goal name="WP_parameter sieve.3" expl="3. index in array bounds">
<proof prover="0"><result status="valid" time="0.01" steps="8"/></proof>
</goal>
<goal name="WP_parameter sieve.4" expl="4. loop invariant init">
<proof prover="0"><result status="valid" time="0.02" steps="10"/></proof>
</goal>
<goal name="WP_parameter sieve.5" expl="5. loop invariant init">
<proof prover="0"><result status="valid" time="0.02" steps="22"/></proof>
</goal>
<goal name="WP_parameter sieve.6" expl="6. type invariant">
<proof prover="0"><result status="valid" time="0.03" steps="13"/></proof>
</goal>
<goal name="WP_parameter sieve.7" expl="7. index in array bounds">
<proof prover="0"><result status="valid" time="0.01" steps="13"/></proof>
</goal>
<goal name="WP_parameter sieve.8" expl="8. loop invariant init">
<proof prover="0"><result status="valid" time="0.02" steps="15"/></proof>
</goal>
<goal name="WP_parameter sieve.9" expl="9. loop invariant init">
<proof prover="5"><result status="valid" time="0.05"/></proof>
</goal>
<goal name="WP_parameter sieve.10" expl="10. type invariant">
<proof prover="0"><result status="valid" time="0.02" steps="18"/></proof>
</goal>
<goal name="WP_parameter sieve.11" expl="11. index in array bounds">
<proof prover="0"><result status="valid" time="0.02" steps="18"/></proof>
</goal>
<goal name="WP_parameter sieve.12" expl="12. loop invariant preservation">
<proof prover="1"><result status="valid" time="0.01"/></proof>
</goal>
<goal name="WP_parameter sieve.13" expl="13. loop invariant preservation">
<proof prover="0"><result status="valid" time="0.17" steps="85"/></proof>
</goal>
<goal name="WP_parameter sieve.14" expl="14. loop variant decrease">
<proof prover="0"><result status="valid" time="0.04" steps="21"/></proof>
</goal>
<goal name="WP_parameter sieve.15" expl="15. assertion">
<transf name="split_goal_wp">
<goal name="WP_parameter sieve.15.1" expl="1. assertion">
<proof prover="0"><result status="valid" time="0.69" steps="77"/></proof>
</goal>
<goal name="WP_parameter sieve.15.2" expl="2. assertion">
<proof prover="0"><result status="valid" time="0.02" steps="41"/></proof>
</goal>
<goal name="WP_parameter sieve.15.3" expl="3. assertion">
<proof prover="0"><result status="valid" time="0.86" steps="77"/></proof>
</goal>
<goal name="WP_parameter sieve.15.4" expl="4. assertion">
<proof prover="0"><result status="valid" time="0.03" steps="25"/></proof>
</goal>
<goal name="WP_parameter sieve.15.5" expl="5. assertion">
<proof prover="0"><result status="valid" time="0.05" steps="59"/></proof>
</goal>
</transf>
</goal>
<goal name="WP_parameter sieve.16" expl="16. loop invariant preservation">
<proof prover="0"><result status="valid" time="0.02" steps="19"/></proof>
</goal>
<goal name="WP_parameter sieve.17" expl="17. loop invariant preservation">
<proof prover="0"><result status="valid" time="0.15" steps="70"/></proof>
</goal>
<goal name="WP_parameter sieve.18" expl="18. loop variant decrease">
<proof prover="0"><result status="valid" time="0.03" steps="19"/></proof>
</goal>
<goal name="WP_parameter sieve.19" expl="19. assertion">
<transf name="split_goal_wp">
<goal name="WP_parameter sieve.19.1" expl="1. assertion">
<proof prover="0"><result status="valid" time="0.03" steps="40"/></proof>
</goal>
<goal name="WP_parameter sieve.19.2" expl="2. assertion">
<proof prover="0"><result status="valid" time="0.02" steps="27"/></proof>
</goal>
<goal name="WP_parameter sieve.19.3" expl="3. assertion">
<proof prover="0"><result status="valid" time="0.06" steps="35"/></proof>
</goal>
<goal name="WP_parameter sieve.19.4" expl="4. assertion">
<proof prover="0"><result status="valid" time="0.14" steps="69"/></proof>
</goal>
<goal name="WP_parameter sieve.19.5" expl="5. assertion">
<proof prover="0"><result status="valid" time="0.02" steps="22"/></proof>
</goal>
<goal name="WP_parameter sieve.19.6" expl="6. assertion">
<proof prover="0"><result status="valid" time="0.03" steps="31"/></proof>
</goal>
</transf>
</goal>
<goal name="WP_parameter sieve.20" expl="20. loop invariant preservation">
<proof prover="0"><result status="valid" time="0.02" steps="16"/></proof>
</goal>
<goal name="WP_parameter sieve.21" expl="21. loop invariant preservation">
<transf name="split_goal_wp">
<goal name="WP_parameter sieve.21.1" expl="1. loop invariant preservation">
<proof prover="0"><result status="valid" time="0.02" steps="28"/></proof>
</goal>
<goal name="WP_parameter sieve.21.2" expl="2. loop invariant preservation">
<proof prover="0"><result status="valid" time="0.02" steps="40"/></proof>
</goal>
</transf>
</goal>
<goal name="WP_parameter sieve.22" expl="22. loop variant decrease">
<proof prover="0"><result status="valid" time="0.03" steps="16"/></proof>
</goal>
<goal name="WP_parameter sieve.23" expl="23. assertion">
<proof prover="1"><result status="valid" time="0.08"/></proof>
</goal>
<goal name="WP_parameter sieve.24" expl="24. assertion">
<transf name="split_goal_wp">
<goal name="WP_parameter sieve.24.1" expl="1. assertion">
<proof prover="5"><result status="valid" time="0.08"/></proof>
</goal>
<goal name="WP_parameter sieve.24.2" expl="2. assertion">
<proof prover="0"><result status="valid" time="0.04" steps="28"/></proof>
</goal>
</transf>
</goal>
<goal name="WP_parameter sieve.25" expl="25. type invariant">
<proof prover="0"><result status="valid" time="0.03" steps="13"/></proof>
</goal>
<goal name="WP_parameter sieve.26" expl="26. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="55"/></proof>
</goal>
</transf>
</goal>
</theory>
</file>
</why3session>
Markdown is supported
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