Alloy: define relation to only positive integers

别等时光非礼了梦想. 提交于 2019-12-11 14:41:43

问题


I have a model with this sig:

sig Thing {}
sig World {
    quantities: Thing ->one Int,
}

I want to define a constraint on the quantities relationship such that the quantity of each Thing must be a positive int.

I am total beginner with Alloy (and I have no theory background to draw on, am just a Python programmer). I followed through the tutorial but I did not see a recipe for what I want to do.

I know how to:

fact {
    all w: World | w.quantities <something>
}

...but I am not clear how to address members of the right-hand-side of the relationship when writing a fact.

I have defined it as a relationship (rather than having a quantity property on the Thing sig) because I understood from the tutorial that this was necessary in a dynamic model where I want to update the quantity of Things via predicates.

I tried defining a:

sig PositiveInt extends Int {}

...but this is not allowed.


回答1:


updated This kind of subtyping works (imho) best with set enumeration:

 let PositiveInt = { i : Int | i > 0 }
 sig Thing {}
 sig World { quantities : Thing -> one PositiveInt }

┌──────────┬──────────┐
│this/World│quantities│
├──────────┼──────┬───┤
│World⁰    │Thing⁰│7  │
│          ├──────┼───┤
│          │Thing¹│6  │
│          ├──────┼───┤
│          │Thing²│4  │
└──────────┴──────┴───┘


来源:https://stackoverflow.com/questions/52690845/alloy-define-relation-to-only-positive-integers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!