[英]Are there multisets in Alloy?
Is there a way to model a system using bags(multisets) as well in Alloy? 有没有办法在Alloy中使用bag(multisets)来建模系统? And if there is no explicit notion of bags, is there any possible workaround?
而且,如果没有明确的“袋”概念,是否有任何可行的解决方法?
Thanks. 谢谢。
A multiset [aka bag] of E is representable by a function E ->one Natural, or E ->lone (Natural-Zero) (depending on taste as to how to handle absence). E的多集[aka bag]用函数E->一个自然的,或E->孤独的(自然零)表示(取决于口味如何处理缺席)。
open util/natural
sig E {}
sig A { m : E -> one Natural }
sig B { n : E -> lone (Natural-Zero) }
fun bagunion[m, n : univ -> lone Natural]: univ -> lone Natural
{ e : (m+n).univ, x : Natural | e in m.univ-n.univ implies x=e.m
else e in n.univ-m.univ implies x=e.n
else x=add[e.m, e.n] }
There are probably neater ways to do bag union. 可能有更整洁的方式进行袋装合并。
Thanks for all your help but I did it the following way eventually: 感谢您的所有帮助,但最终我通过以下方式做到了:
First I restricted my bags to contain only elements with non-zero multiplicity 首先,我将袋子限制为仅包含非零多重性的元素
module bags
open util/natural
open util/relation
sig Element{}
sig Bag{
elements: Element -> one Natural
}{
all e: Element.elements | e != Zero
}
And coded union/difference/intersection like this: 并这样编码并集/差异/交集:
fun BagUnion[b1, b2 : Element -> one Natural]: Element -> one Natural{
let e = (dom[b1] + dom[b2]) | e -> add[e.b1, e.b2]
}
fun BagDifference[b1, b2 : Element -> one Natural]: Element -> one Natural{
let e = dom[b1] | e -> max[Zero + sub[e.b1, e.b2]]
}
fun BagIntersection[b1, b2 : Element -> one Natural]: Element -> one Natural{
let e = (dom[b1] & dom[b2]) | e -> min[e.b1 + e.b2]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.