在代码合约中使用 Contract.ForAll

发布于 2024-09-06 18:23:28 字数 991 浏览 8 评论 0原文

好的,我还有另一个代码合同问题。我有一个关于接口方法的合同,如下所示(为了清楚起见,省略了其他方法):

[ContractClassFor(typeof(IUnboundTagGroup))]
public abstract class ContractForIUnboundTagGroup : IUnboundTagGroup
{
    public IUnboundTagGroup[] GetAllGroups()
    {
        Contract.Ensures(Contract.Result<IUnboundTagGroup[]>() != null);
        Contract.Ensures(Contract.ForAll(Contract.Result<IUnboundTagGroup[]>(), g => g != null));

        return null;
    }
}

我有使用如下所示接口的代码:

    public void AddRequested(IUnboundTagGroup group)
    {
            foreach (IUnboundTagGroup subGroup in group.GetAllGroups())
            {
                AddRequested(subGroup);
            }
            //Other stuff omitted
    }

AddRequested 需要一个非空输入参数(它实现了一个具有 Requires 契约的接口),因此我在传递到 AddRequested 的子组上收到“requires unproven: group != null”错误。我是否正确使用了 ForAll 语法?如果是这样,并且求解器根本不理解,是否有另一种方法可以帮助求解器识别合同,或者我只需要在调用 GetAllGroups() 时使用假设?

Okay, I have yet another Code Contracts question. I have a contract on an interface method that looks like this (other methods omitted for clarity):

[ContractClassFor(typeof(IUnboundTagGroup))]
public abstract class ContractForIUnboundTagGroup : IUnboundTagGroup
{
    public IUnboundTagGroup[] GetAllGroups()
    {
        Contract.Ensures(Contract.Result<IUnboundTagGroup[]>() != null);
        Contract.Ensures(Contract.ForAll(Contract.Result<IUnboundTagGroup[]>(), g => g != null));

        return null;
    }
}

I have code consuming the interface that looks like this:

    public void AddRequested(IUnboundTagGroup group)
    {
            foreach (IUnboundTagGroup subGroup in group.GetAllGroups())
            {
                AddRequested(subGroup);
            }
            //Other stuff omitted
    }

AddRequested requires a non-null input parameter (it implements an interface which has a Requires contract) and so I get a 'requires unproven: group != null' error on the subGroup being passed into AddRequested. Am I using the ForAll syntax correctly? If so and the solver simply isn't understanding, is there another way to help the solver recognize the contract or do I simply need to use an Assume whenever GetAllGroups() is called?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2024-09-13 18:23:28

代码合同用户手册指出,“静态合同检查器不会尚未处理 ForAll 或 Exists 量词。”在此之前,在我看来,选项是:

  1. 忽略警告。
  2. 在调用 AddRequested() 之前添加 Contract.Assume(subGroup != null)
  3. 在调用 AddRequested() 之前添加检查。也许if(子组
    == null) throw new InvalidOperationException()
    if (subGroup != null)
    AddRequested(子组)

选项 1 并没有真正的帮助。选项 2 存在风险,因为即使 IUnboundTagGroup.GetAllGroups() 不再确保后置条件,它也会绕过 AddRequested() Requires 契约。我会选择选项 3。

The Code Contracts User Manual states, "The static contract checker does not yet deal with quantiers ForAll or Exists." Until it does, it seems to me the options are:

  1. Ignore the warning.
  2. Add Contract.Assume(subGroup != null) before the call to AddRequested().
  3. Add a check before the call to AddRequested(). Maybe if (subGroup
    == null) throw new InvalidOperationException()
    or if (subGroup != null)
    AddRequested(subGroup)
    .

Option 1 doesn't really help. Option 2 is risky because it will circumvent the AddRequested() Requires contract even if IUnboundTagGroup.GetAllGroups() no longer ensures that post-condition. I'd go with option 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文