简体   繁体   English

使用 Double In 语句在合金中输入错误

[英]Type Error in Alloy using Double In Statement

I am trying to write a predicate that will make all Bananas and Fresh Apples expensive.我正在尝试编写一个使所有香蕉和新鲜苹果变得昂贵的谓词。 I am able to achieve one of the conditions but never both.我能够达到其中一个条件,但不能同时达到。 I'm very new to using Alloy, any help would be very much appreciated.我对使用 Alloy 很陌生,任何帮助将不胜感激。

Below is my code, the error is occurring because I'm using a double In Statement but I'm unsure how I could write this without having to use two in statements.下面是我的代码,发生错误是因为我使用了双 In 语句,但我不确定如何在不必使用两个 in 语句的情况下编写它。 The error I receive is "Type Error, this must be a set or relation"我收到的错误是“类型错误,这必须是一个集合或关系”

sig Fruit{}
sig Banana, Apple, Pear extends Fruit {}
sig Fresh, Expensive in Fruit{}

pred BananasAndFreshApplesAreExpensive {
Apple in (Expensive & Fresh) + Banana in Expensive
} 
run BananasAndFreshApplesAreExpensive

Another way:其它的办法:

all bananas is represented by the set Banana all fresh apples is represented by the set Fresh & Apple x is expensive is represented by x in Expensive所有香蕉由集合Banana所有新鲜苹果由集合Fresh & Apple表示 x 是昂贵的由x in Expensive

So所以

Banana in Expensive
(Fresh & Apple) in Expensive

or just要不就

Banana + (Fresh & Apple) in Expensive

If I correctly understand what you're trying to do, the code is not failing because you have 2 "in" statements, it's because you're using a union operator ("+") instead of a logical AND ("and" or "&&").如果我正确理解您要执行的操作,则代码不会失败,因为您有 2 个“in”语句,这是因为您使用的是联合运算符(“+”)而不是逻辑 AND(“and”或“&&”)。

The return from a predicate has to evaluate to TRUE or FALSE.谓词的返回必须评估为 TRUE 或 FALSE。

If you are trying to say the following:如果你想说以下几点:

  • All instances of Apple are expensive and fresh AND苹果的所有实例都是昂贵和新鲜的,并且
  • All instances of Banana are expensive Banana 的所有实例都很昂贵

...the following code will do it: ...下面的代码将做到这一点:

sig Fruit{}
sig Banana, Apple, Pear extends Fruit {}
sig Fresh, Expensive in Fruit{}

pred BananasAndFreshApplesAreExpensive {
Apple in (Expensive & Fresh) and Banana in Expensive
} 

run BananasAndFreshApplesAreExpensive

However, if you are trying to say:但是,如果您想说:

  • Instances of Apple that are fresh are expensive AND新鲜的苹果实例价格昂贵且
  • Instances of Banana are expensive香蕉的实例很昂贵

...the following code is one way to do that: ...以下代码是一种方法:

sig Fruit{}
sig Banana, Apple, Pear extends Fruit {}
sig Fresh, Expensive in Fruit{}

pred BananasAndFreshApplesAreExpensive {
    (all a: Apple | a in Fresh => a in Expensive) and
    Banana in Expensive
} 

run BananasAndFreshApplesAreExpensive

Keep in mind, though, that this does not say anything about instances of Apple that are not fresh.但是请记住,这并不能说明 Apple 实例不新鲜。 In other words, it will allow instances of Apple that are not fresh to be expensive.换句话说,它将允许不新鲜的 Apple 实例变得昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM