wxlfrank
wxlfrank

Reputation: 39

Is there an easier way to specify this?

As in the following code, is there any way to make macros for the expressions NP + NF2 + NF1 + NT + NR and AnonActive + Aactive + AsetTurn + Astart + Acrit + Acheck + APF2 + APF1 + ATP + ATR + AF2R + AF1R, so that I can refer to them later by name rather than writing the expressions out directly?

sig NP{}
sig NF2{}
sig NF1{}
sig NT{}
sig NR{}

sig AnonActive{src:one NP, trg:one NP}
sig Aactive{src:one NP, trg:one NP}
sig AsetTurn{src:one NP, trg:one NP}
sig Astart{src:one NP, trg:one NP}
sig Acrit{src:one NP, trg:one NP}
sig Acheck{src:one NP, trg:one NP}
sig APF2{src:one NP, trg:one NF2}
sig APF1{src:one NP, trg:one NF1}
sig ATP{src:one NT, trg:one NP}
sig ATR{src:one NT, trg:one NR}
sig AF2R{src:one NF2, trg:one NR}
sig AF1R{src:one NF1, trg:one NR}

sig Graph{nodes:set NP+NF2+NF1+NT+NR, 
          arrows:set AnonActive + Aactive + AsetTurn + Astart 
                     + Acrit + Acheck + APF2 + APF1 + ATP + ATR 
                     + AF2R + AF1R}

Upvotes: 1

Views: 59

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25054

One simple way would be to declare abstract signatures Node and Arrow, and declare each of your existing signatures as extending one or the other of them.

abstract sig Node {}
sig NP extends Node {}
sig NF2 extends Node {}
... 

Since all your arrows have the same relations, the relations can be declared in the Arrow sig; the restrictions on the kinds of nodes allowed as source and target can be given using signature facts, as for example:

abstract sig Arrow {
  src:  one Node,
  trg:  one Node
}
sig AnonActive extends Arrow {}{
  one + trg in NP
}
...
sig AF1R extends Arrow {}{
  src in NF1
  trg in NR
}

Now the declaration of Graph is quite simple (and, at least to some eyes, perhaps a bit clearer):

sig Graph {
  nodes: set Node,
  arrows: set Arrow 
}

Another approach (less good in the case shown, I think, but useful in cases where the first approach cannot be applied) would be to define functions with the appropriate names:

fun Node : set univ {
  NP + NF2 + NF1 + NT + NR
}
fun Arrow : set univ {
  AnonActive + Aactive + AsetTurn + Astart 
  + Acrit + Acheck + APF2 + APF1 + ATP + ATR 
  + AF2R + AF1R
}

N.B. I have not checked these for syntax errors ...

Upvotes: 1

Related Questions