简体   繁体   中英

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.

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. 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

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 "&&").

The return from a predicate has to evaluate to TRUE or FALSE.

If you are trying to say the following:

  • All instances of Apple are expensive and fresh AND
  • All instances of Banana are expensive

...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. In other words, it will allow instances of Apple that are not fresh to be expensive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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