0

Let the following simple Alloy code :

sig somme{
f : one Ax1,
g: one Ax2,
}
let Ax1= String
let Ax2= "Spain" + "Italy"

I want to restrict the values of the field f to "Italy" so I write a predicat :

pred show{
 Ax1= "Italy"
}
run show

But this does not work (I have no instances).

So my question is How can I do this without hardcoding it in an axiome but rather in a fact or a predicat to have more flexibility (I may want to write a second predicat to create instances that use only Spain)?

Thank you in advance,

Progman
  • 16,827
  • 6
  • 33
  • 48
Abdelghani
  • 455
  • 2
  • 9
  • 19

2 Answers2

0

Ax1 is a macro, you're using let. You're using it as a programming language's global variable. Alloy has no variables and definitely not global. The role of variables are the relations, defined in fields in the sig. To use a 'fresh' variable, the best way is to create a predicate with it:

pred show[ ax1 : String ] {
     ax1 = "Italy"
}
run show

That said, it is a terrible idea to use String atoms for this. Alloy has a contorted relation with integers, but its relation to Strings is worse. The atoms of String are all the strings in your program, you cannot exclude easily. This almost always fails in the long run. Just use sigs.

enum Country { Italy, Spain, France, Netherlands }

pred show[ ax1 : Country ] {
     ax1 = Italy
}
run show
Peter Kriens
  • 15,196
  • 1
  • 37
  • 55
0

Your show predicate is inconsistent because you define Ax1 as being the set of all Strings. Limiting the set of all strings to be "Italy" (Ax1 = "Italy") is inconsistent with let Ax2= "Spain" + "Italy"

Why don't you directly define Ax1 and Ax2 as Strings ?

sig Somme{
  f : one String,
  g: one String,
}{
  f = "Italy"
}


pred show{
   //little hack to define a set of Strings usable by your model
   "Italy" +"Spain" in univ
}

run show

If you want to know more about using String, you can check the answer to this question: Bug on unconstrained Strings.

You can also follow Peter's advice and define the concept of Country (with enum or the old school ways with signatures). That's generally what you'd do if you're interested in producing an abstraction of your system .

Loïc Gammaitoni
  • 4,173
  • 16
  • 39