为什么在箭头表达式中使用时字段不同?

Why are fields different when used in an arrow expression?

以下签名描述了照片管理应用程序的状态:

sig ApplicationState {
    catalogs: set Catalog,
    catalogState: catalogs -> one CatalogState
}

一个签名,当然是一个集合。在本例中,它创建了一组 ApplicationStates:

ApplicationState0
ApplicationState1
...

catalogs 是一个字段。它将每个 ApplicationState 映射到一组目录值:

ApplicationState0, Catalog0
ApplicationState0, Catalog1
ApplicationState1, Catalog0
...

catalogState 也是一个字段。它将每个 ApplicationState 映射到一个关系。关系是:

catalogs -> one CatalogState

该关系表示:将目录的每个值映射到一个 CatalogState 值。我们已经看到了目录,我将在这里重复:

ApplicationState0, Catalog0
ApplicationState0, Catalog1
ApplicationState1, Catalog0
...

因此,该关系表示将这些元组中的每一个映射到一个 CatalogState,如下所示:

ApplicationState0, Catalog0, CatalogState0
ApplicationState0, Catalog1, CatalogState0
ApplicationState1, Catalog0, CatalogState0
...

好的,回到catalogState。之前我们说过它将每个 ApplicationState 映射到一个关系,我们只是看到了那个关系是什么。所以,我相信 catalogState 表示与 arity=4 的关系,如下所示:

ApplicationState0, ApplicationState0, Catalog0, CatalogState0
ApplicationState0, ApplicationState0, Catalog1, CatalogState0
ApplicationState0, ApplicationState1, Catalog0, CatalogState0
...

但是,当我 运行 Alloy 求值器时,它说 catalogState 是一个三元关系。我从这个例子中得出的结论是:

  1. 通常一个字段名表示一个关系。

  2. 箭头表达式中使用的字段名称不表示关系。相反,它表示关系的第 2 列(关系的范围)。

是吗? Software Abstractions 书中哪里对此进行了解释?

Sofware Abstractions(第二版第 97 页)的第 4.2.2 节开始

Relations are declared as fields of signatures.

我认为这至少解决了您的部分问题。 (我认为浏览 'field' 和关系的索引条目并阅读它们指向的每个部分可能会有所帮助。)

你说

A field name used in an arrow expression does not denote a relation. Rather, it denotes column 2 of the relation (the range of the relation).

这听起来可能很迂腐,但事实并非如此:字段名称总是表示关系。然而,在签名声明的上下文中,它们隐含地带有 this. 前缀,这会删除关系的第一列。在您的声明 catalogState: catalogs -> one CatalogState 中,对 catalogs 的引用确实是对 ApplicationState 和 Catalog 的二元关系的引用。然而,在这种情况下,它会默默地扩展为 this.catalogs,它计算出一组 Catalog 个体。在 Software Abstractions.

的 4.2.2 节中引入了关键字 this

声明的基数约束也可能是您示例中的一个复杂因素;我不会在这里解释它们的作用。我只会说,当我 运行 遇到基数约束问题时,我经常发现非常仔细地阅读附录 B 中语言参考的相关部分通常足以让我理解发生了什么在。 (我承认有时候读了不止一遍。)