comp.lang.ada
 help / color / mirror / Atom feed
From: Phil Thornley <phil.jpthornley@gmail.com>
Subject: Re: [Spark] Proving GCD
Date: Fri, 17 Mar 2017 06:00:51 -0700 (PDT)
Date: 2017-03-17T06:00:51-07:00	[thread overview]
Message-ID: <2ac671c3-ea12-4a53-9ee8-dc72b9702bb2@googlegroups.com> (raw)
In-Reply-To: <eaf03e7d-a2dd-4490-9156-3a25c32634eb@googlegroups.com>

On Friday, March 17, 2017 at 9:33:08 AM UTC, Frédéric PRACA wrote:
> Le jeudi 16 mars 2017 21:27:24 UTC+1, Frédéric PRACA a écrit :
> > Hi guys,
> 
> Here is the new version of the contract
> function gcd (A : Natural; B : Positive) return Natural
>      with
>        Global => null,
>        Depends => (gcd'Result => (A,B)),
>        Post =>  A mod gcd'Result = 0 
>        and B mod gcd'Result = 0 
>        and ((A > 0 and then gcd'Result <= A) OR (A = 0 and then gcd'result = B));
> 
> It was tested with the following code
> Pragma SPARK_Mode;
> 
> with Ada.text_io; use Ada.text_io;
> with GCD_Package; use GCD_Package;
> 
> procedure TestGCD is
> 
> begin
>    Put_line ("GCD (420, 252) = " & Integer'Image(gcd(a => 420,
>                                                      b => 252)));
>    
>    Put_line ("GCD (512, 252) = " & Integer'Image(gcd(a => 512,
>                                                      b => 252)));
>    
>    Put_line ("GCD (1, 1) = " & Integer'Image(gcd(a => 1,
>                                                  b => 1)));
>    
>    Put_line ("GCD (0, 1) = " & Integer'Image(gcd(a => 0,
>                                                  b => 1)));
> end TestGCD;
> 
> And I still have the same message from Spark. Any idea ?

Any non-trivial code with loops will need to have loop invariants. These links explain why and a bit about how to write them:
http://www.spark-2014.org/entries/detail/spark-2014-rationale-loop-invariants
http://www.spark-2014.org/entries/detail/gnatprove-tips-and-tricks-how-to-write-loop-invariants

I would also recommend starting with an easier example than the GCD algorithm. A Google search for "SPARK loop invariant" will throw up lots more examples. (But some of these will be about the previous version of SPARK - now referred to as SPARK 2005.)

Note that your postcondition states that the result is a common divisor of A and B, but does not state that it is the greatest such divisor.

Cheers,

Phil

  reply	other threads:[~2017-03-17 13:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-16 20:27 [Spark] Proving GCD Frédéric Praca
2017-03-17  9:33 ` Frédéric PRACA
2017-03-17 13:00   ` Phil Thornley [this message]
2017-03-17 13:24     ` Frédéric PRACA
2017-03-17 13:28       ` Frédéric PRACA
2017-03-17 17:16         ` Phil Thornley
  -- strict thread matches above, loose matches on Subject: below --
2017-03-16 20:31 Frédéric Praca
2017-03-17  1:20 ` Brian Kolden
2017-03-17  9:07   ` Frédéric PRACA
2017-04-05 23:52 ` moy
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox