Commit 49c69719 authored by Raphael Rieu-Helft's avatar Raphael Rieu-Helft

Add my solutions to VerifyThis 2018 problems 1 and 3

parent d71ed220
This diff is collapsed.
(**
{1 VerifyThis @ ETAPS 2018 competition
Challenge 1: Mind the gap}
Author: Raphaël Rieu-Helft (LRI, Université Paris Sud)
*)
module GapBuffer
use import int.Int
use import seq.Seq
use import map.Map as M
use import array.Array
type char
val constant dummy_char : char
type buffer = {
mutable data : array char;
mutable l : int;
mutable r : int
} invariant { 0 <= l <= r <= data.length }
by { data = Array.make 1 dummy_char; l = 0; r = 0 }
function len_contents (b:buffer) : int
= b.data.length - b.r + b.l
function contents (b:buffer) : int -> char
= fun i -> if 0 <= i < b.l then b.data.elts i
else if b.l <= i <= len_contents b
then b.data.elts (i+b.r-b.l)
else dummy_char
function cursor_pos (b:buffer) : int = b.l
predicate same_contents (b1 b2:buffer)
= len_contents b1 = len_contents b2
/\ forall i. 0 <= i < len_contents b1 ->
contents b1 i = contents b2 i
val b: buffer
let left ()
ensures { if old b.l = 0
then b = old b
else cursor_pos b = cursor_pos (old b) - 1 }
ensures { same_contents b (old b) }
= if b.l <> 0
then begin
b.l <- b.l - 1;
b.r <- b.r - 1;
b.data[b.r] <- b.data[b.l]
end
let right ()
ensures { if old b.r = old b.data.length
then b = old b
else cursor_pos b = cursor_pos (old b) + 1 }
ensures { same_contents b (old b) }
= if b.r <> b.data.length
then begin
b.data[b.l] <- b.data[b.r];
b.l <- b.l + 1;
b.r <- b.r + 1
end
let constant k = 42
let grow ()
ensures { b.l = old b.l }
ensures { b.r = old b.r + k }
ensures { same_contents b (old b) }
= let ndata = Array.make (b.data.length + k) dummy_char in
Array.blit b.data 0 ndata 0 b.l;
Array.blit b.data b.r ndata (b.r + k) (b.data.length - b.r);
b.r <- b.r + k;
b.data <- ndata
predicate contents_inserted (newb oldb: buffer) (x:char) (pos:int)
= len_contents newb = len_contents oldb + 1
/\ 0 <= pos <= len_contents oldb
/\ (forall i. 0 <= i < pos -> contents newb i = contents oldb i)
/\ contents newb pos = x
/\ (forall i. pos < i < len_contents newb ->
contents newb i = contents oldb (i-1))
let insert (x:char)
ensures { cursor_pos b = old cursor_pos b + 1 }
ensures { contents_inserted b (old b) x (old b.l) }
=
if b.l = b.r then grow ();
b.data[b.l] <- x;
b.l <- b.l + 1
predicate contents_deleted (newb oldb: buffer) (pos:int)
= len_contents newb = len_contents oldb - 1
/\ 0 <= pos < len_contents oldb
/\ (forall i. 0 <= i < pos -> contents newb i = contents oldb i)
/\ (forall i. pos <= i < len_contents newb ->
contents newb i = contents oldb (i+1))
let delete ()
ensures { if cursor_pos (old b) = 0
then b = old b
else cursor_pos b = old cursor_pos b - 1
/\ contents_deleted b (old b) (old b.l - 1) }
= if b.l <> 0
then b.l <- b.l - 1
else ()
end
\ No newline at end of file
<?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="1.30" timelimit="1" steplimit="0" memlimit="1000"/>
<prover id="1" name="Z3" version="4.5.0" timelimit="1" steplimit="0" memlimit="1000"/>
<file name="../verifythis_2018_mind_the_gap_2.mlw" proved="true">
<theory name="GapBuffer" proved="true">
<goal name="VC dummy_char" expl="VC for dummy_char" proved="true">
<proof prover="1"><result status="valid" time="0.00"/></proof>
</goal>
<goal name="VC buffer" expl="VC for buffer" proved="true">
<proof prover="1"><result status="valid" time="0.01"/></proof>
</goal>
<goal name="VC left" expl="VC for left" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC left.0" expl="index in array bounds" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC left.1" expl="index in array bounds" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC left.2" expl="type invariant" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC left.3" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.01"/></proof>
</goal>
<goal name="VC left.4" expl="postcondition" proved="true">
<proof prover="0"><result status="valid" time="0.02" steps="59"/></proof>
</goal>
<goal name="VC left.5" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC left.6" expl="postcondition" proved="true">
<proof prover="0"><result status="valid" time="0.02" steps="3"/></proof>
</goal>
</transf>
</goal>
<goal name="VC right" expl="VC for right" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC right.0" expl="index in array bounds" proved="true">
<proof prover="1"><result status="valid" time="0.03"/></proof>
</goal>
<goal name="VC right.1" expl="index in array bounds" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC right.2" expl="type invariant" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC right.3" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC right.4" expl="postcondition" proved="true">
<proof prover="0" timelimit="5" memlimit="2000"><result status="valid" time="0.02" steps="55"/></proof>
</goal>
<goal name="VC right.5" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC right.6" expl="postcondition" proved="true">
<proof prover="0"><result status="valid" time="0.02" steps="3"/></proof>
</goal>
</transf>
</goal>
<goal name="VC k" expl="VC for k" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC grow" expl="VC for grow" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC grow.0" expl="array creation size" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC grow.1" expl="precondition" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC grow.2" expl="precondition" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC grow.3" expl="precondition" proved="true">
<proof prover="1"><result status="valid" time="0.03"/></proof>
</goal>
<goal name="VC grow.4" expl="precondition" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC grow.4.0" expl="VC for grow" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC grow.4.1" expl="VC for grow" proved="true">
<proof prover="1"><result status="valid" time="0.03"/></proof>
</goal>
</transf>
</goal>
<goal name="VC grow.5" expl="type invariant" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC grow.6" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC grow.7" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.01"/></proof>
</goal>
<goal name="VC grow.8" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.03"/></proof>
</goal>
</transf>
</goal>
<goal name="VC insert" expl="VC for insert" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC insert.0" expl="index in array bounds" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC insert.1" expl="type invariant" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC insert.2" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.04"/></proof>
</goal>
<goal name="VC insert.3" expl="postcondition" proved="true">
<proof prover="0"><result status="valid" time="0.02" steps="182"/></proof>
<proof prover="1" obsolete="true"><result status="timeout" time="1.00"/></proof>
</goal>
<goal name="VC insert.4" expl="index in array bounds" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC insert.5" expl="type invariant" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC insert.6" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.01"/></proof>
</goal>
<goal name="VC insert.7" expl="postcondition" proved="true">
<proof prover="0"><result status="valid" time="0.03" steps="104"/></proof>
<proof prover="1" obsolete="true"><result status="timeout" time="1.00"/></proof>
</goal>
</transf>
</goal>
<goal name="VC delete" expl="VC for delete" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC delete.0" expl="type invariant" proved="true">
<proof prover="1"><result status="valid" time="0.04"/></proof>
</goal>
<goal name="VC delete.1" expl="postcondition" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC delete.1.0" expl="VC for delete" proved="true">
<proof prover="1"><result status="valid" time="0.01"/></proof>
</goal>
<goal name="VC delete.1.1" expl="VC for delete" proved="true">
<proof prover="1"><result status="valid" time="0.01"/></proof>
</goal>
<goal name="VC delete.1.2" expl="VC for delete" proved="true">
<transf name="introduce_premises" proved="true" >
<goal name="VC delete.1.2.0" expl="VC for delete" proved="true">
<transf name="inline_goal" proved="true" >
<goal name="VC delete.1.2.0.0" expl="VC for delete" proved="true">
<transf name="split_vc" proved="true" >
<goal name="VC delete.1.2.0.0.0" expl="VC for delete" proved="true">
<proof prover="1"><result status="valid" time="0.04"/></proof>
</goal>
<goal name="VC delete.1.2.0.0.1" expl="VC for delete" proved="true">
<proof prover="1"><result status="valid" time="0.03"/></proof>
</goal>
<goal name="VC delete.1.2.0.0.2" expl="VC for delete" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC delete.1.2.0.0.3" expl="VC for delete" proved="true">
<proof prover="1"><result status="valid" time="0.02"/></proof>
</goal>
<goal name="VC delete.1.2.0.0.4" expl="VC for delete" proved="true">
<proof prover="1"><result status="valid" time="0.04"/></proof>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
<goal name="VC delete.2" expl="postcondition" proved="true">
<proof prover="1"><result status="valid" time="0.01"/></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