CS195Y Lecture 6

2/10/16

Announcement: Alloy 1 due tomorrow

Even if you have overflow-protection, counting can still be subtle. For examle:

sig N { edges : set N}
run { #N = 9 } for 9 N, 5 int //satisfiable
run { #N = 9 } for 9 N //unsatifiable!

for 9 N says for up to nine instances of type N, for exactly 9 N says there must be exactly nine instances of type N

Continuing Tic Tac Toe:

Relational style: only uses relational operators (join, equality, union, intersection, transpose, etc)
Predicate style: uses quantifiers (all, some, etc)
In practice, you often use a combination of the two styles.
[] is sort of the opposite of dot join. It just does the join on the other side of the relation. You can also think of it as indexing into an array in a lot of cases. For example, board[r][c] will give you the piece at index (r,c) on the board.
Now we want to show that a predicate written in relational calculus style is equivalent to the same predicate written in predicate calculus style.

pred identicalFlip[b: Board] ...
pred identicalFlip2[b: Board] ...
assert equivalent {
  all b : Board |
    identicalFlip[b] iff identicalFlip2[b]
}
check equivalent

Most of the time, it is better to use predicates than assertions because you can combine them together and reuse them.
check tries to run the negation of your assertion. It will basically try to find an instance of your assert statement not holding, show you the counterexample if possible or tell you that it did not find a counterexample.

Now we’re going to actually model a game

A game is a sequence of boards. But not just any sequence of arbitrary boards. The seqence needs to satisfy:

fact: Use this for things that are intrinsic to the model and you don’t want to see instances that don’t abide by this constraint. For example, in our Tic Tac Toe example, we have a fact that says boards start as empty. We are totally uninterested in any “Games” where the board sequence does not start with the empty board.
boards.first: gives the first board in the sequence
boards.inds: gives a set of all the integer indices in the sequence (so for ten boards, we would get {0,1,...,9}
board.lastIdx: gives the index of the last board in the sequence
Changing the Skolem Depth will chenge the level of detail Alloy shows you in the Visualizer.