首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACSL -不能证明函数

ACSL -不能证明函数
EN

Stack Overflow用户
提问于 2016-01-25 15:32:42
回答 1查看 165关注 0票数 0

我想证明这个功能,但没有结果。我也在使用另一个函数,但我证明了这一点。

有人能帮我吗?

代码语言:javascript
复制
        I'm using Frama-C Sodium version with Alt-ergo 3 as prover.

  Given a not-empty string x.  An integer number p such that
    0 < p ≤|x| is meant to be "a period of x" if

      x[i] = x[i + p]

    for i = 0, 1, ... , |x| − p − 1.
    Note that, for each not-empty string, the length of the string
    is a period of itself.  In this way, every not-empty string
    has got at least one period.  So is well formed the concept of minimum
    period of string x, denoted by per(x):

      per(x) = min { p | p is period of x }.

    Write a C function

       unsigned per(const char x[], unsigned l)

    that, given a string x of length l, returns per(x). 

下面是我到目前为止编写的代码和规范:

代码语言:javascript
复制
/*@ 
    requires l > 0;
    requires p >= 0;

    behavior zero:
      assumes  p == l;
      ensures \result == 1;

    behavior one: 
      assumes l != p && (l%p) == 0;
      assumes \forall int i; 0 <= i < l-p-1 ==> x[i] == x[i+p];
      ensures \result == 1;

   behavior two:
      assumes l != p && (l%p) == 0;
      assumes !(\forall int i; 0 <= i < l-p-1 ==> x[i] == x[i+p]);
      ensures \result == 0;

   behavior three:
     assumes p != l && l%p != 0;
     ensures \result == 0;

   complete behaviors;
   disjoint behaviors;
*/

unsigned has_period(const char x[], unsigned int p, unsigned l) {
    if (p == l) return 1;
    if ((l % p) != 0) return 0;
        /*@
            loop assigns i;

            loop invariant \forall int j; 0 <= j < i ==> (x[j] == x[j+p]);
            loop invariant i <= l-p-1;
            loop invariant i >= 0;
        */

        for (int i = 0 ; i < l-p-1 ; ++i) {
            if (x[i] != x[i + p])
               return 0;
        }     

    return 1; 
}

/*
   predicate has_period(char* x, unsigned int p, unsigned l) =
      \forall int i; i < (l-p-1) ==>  x[i] == x[i+p]; 
*/

/*@
    requires l > 0;
    requires \valid(x+(0..l-1));

    ensures 1 <= \result <= l;
    ensures \forall unsigned int i; i < (l-\result-1) ==> x[i] == x[i+\result];
    ensures \forall unsigned int p; 1 <= p < \result ==> !(\forall int i; i < (l-p-1) ==> x[i] == x[i+p]);
*/

unsigned per(const char x[], unsigned l) {
     int p = 1;

    /*@
        loop assigns p;

        loop invariant 1 <= p <= l;
        loop invariant \forall unsigned j; 1 <= j < p ==> !(\forall int i; i < (l-j-1) ==> x[i] == x[i+j] || (l%p) == 0);
        loop invariant p >= 0;
    */

    while(p < l && !has_period(x,p,l)) 
        ++p;

    return p;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-25 18:20:59

如果你告诉我们你的具体问题是什么,而不是一个通用的“它不起作用”,这会有所帮助,但以下是一些要点:

  • 您的合同缺少assigns条款。在使用WP时,这些是而不是可选的,特别是对于在开发过程中调用的has_period之类的函数。WP需要这些子句来知道在调用过程中哪些位置可能发生了变化,通过使用补语,哪些位置保持不变。
  • 你给出了一个很好的关于周期字符串的半形式定义。您应该使用它来定义ACSL谓词periodic (使用与C函数has_period相同的参数),并根据该谓词编写您的规范。这将大大简化您的注释。特别地,has_period不需要4种行为:如果periodic持有它必须返回1,如果periodic不保存,它必须返回0
  • 使用-wp-rte会导致大量未经证实的义务。根据您的任务,您可能希望加强您的规范,特别是关于x所指向的位置的有效性。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34996324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档