You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Dafny中如何获取有限映射(finite map)的域?

Getting the Domain of a Finite Map in Dafny

Great question! Let's break this down clearly for you:

Can you get the domain of a given finite map in Dafny?

Absolutely! Dafny does support accessing the domain of a finite map (the set of all keys present in the map), though the syntax might not be exactly what you initially looked for.

How to compute the domain of a finite map m

Dafny doesn't have a built-in m.Domain property or dom(m) function out of the box (which is probably why you didn't spot it in the reference manual), but you have two straightforward ways to get the domain:

  • Use a set comprehension directly: You can construct the domain set inline using the expression { x | x in m }. This iterates over all keys present in m and collects them into a set.
    Example:

    method TestMapDomain() {
      var m := map[1: "a", 2: "b", 3: "c"];
      var domain := { x | x in m };
      assert domain == {1, 2, 3};
    }
    
  • Define your own reusable dom function: If you prefer a cleaner dom(m) syntax (similar to what you mentioned), you can easily create a helper function to encapsulate the set comprehension:

    function dom<K, V>(m: map<K, V>): set<K> {
      { x | x in m }
    }
    
    method TestCustomDom() {
      var m := map["foo": 42, "bar": 99];
      var domain := dom(m);
      assert domain == {"foo", "bar"};
    }
    

This approach gives you the exact syntax you were hoping for, and it's fully compatible with Dafny's verification capabilities.

内容的提问来源于stack exchange,提问作者Kevin S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:35:50