Remove predicate relations in Alloy - alloy

I'm learning how to use Alloy, and I've coded this:
module test/SlotsAndFillers
sig Slot { content: one Filler}
sig Filler { slots: set Slot}
fact AllSlotsAreOwned {
all s:Slot | some x:Filler | s in x.slots
}
fact ImproperParthoodSlots {
all x:Filler | some y, z:Slot | y in x.slots implies z in x.slots and x in z.content
}
fact SlotInheritance {
all x, y : Filler, z, z' : Slot | x != y and z != z' and z in y.slots and x in z.content and z' in x.slots implies z' in y.slots
}
fact SingleOccupancy {
all x:Slot, y:Filler | x in y.slots implies one z:Filler | z in x.content
}
fact MutualOccupancyIsIdentity {
all x, y: Filler, z, z': Slot | x != y and z != z' and
z in y.slots and x in z.content and z' in x.slots and y in z'.content implies x = y
}
pred show() {}
run show
When I execute and show models, in addition to both relations content and slots, the models also have new relations, named $show_x, $show_y and $show_z. By testing, I understood that these relations are present if I add things inside the predicate. But for this code, show is empty and there are still these relations. So I've two questions: where do they come from and how to remove them? I know that I can hide them, but then the models look all (nearly) the same. So I would like to explore different models that uses only my two relations and not new ones.
Do not hesitate to reformulate or retitle this post.
Thanks!

When you are executing a run command followed by a predicate, you are actually generating instances satisfying that predicate.
From my understanding, when the predicate is named, the instance visualizer tries to highlight which elements of the instance satisfy the predicate. In your case, "everything" is conforming to this empty predicate and so the name of your predicate is shown everywhere.
A simple workaround is to simply not name the predicate that you give as parameter to the run command:
module test/SlotsAndFillers
sig Slot { content: one Filler}
sig Filler { slots: set Slot}
fact AllSlotsAreOwned {
all s:Slot | some x:Filler | s in x.slots
}
fact ImproperParthoodSlots {
all x:Filler | some y, z:Slot | y in x.slots implies z in x.slots and x in z.content
}
fact SlotInheritance {
all x, y : Filler, z, z' : Slot | x != y and z != z' and z in y.slots and x in z.content and z' in x.slots implies z' in y.slots
}
fact SingleOccupancy {
all x:Slot, y:Filler | x in y.slots implies one z:Filler | z in x.content
}
fact MutualOccupancyIsIdentity {
all x, y: Filler, z, z': Slot | x != y and z != z' and
z in y.slots and x in z.content and z' in x.slots and y in z'.content implies x = y
}
run {}
EDIT:
It seems that this fix doesn't solve the problem. Indeed quantification variables defined in facts are still displayed by the instance viewer.
The only option left (I can think of) to get rid of them is to manually disable their representation in the Theme Menu.
(for the 3 relations $x,$y, $z, set "show as arcs" to off)

Related

Multiple declarations errors when overloading typeclass function

The following code is a code example for overloading the functions of the partial ordered typeclass (POrd) from a lecture slide.
When trying to run this code, I get the following multiple declarations errors for the function 'pcompare' and for the operators.
> class Eq a => POrd a where
> pcompare :: a -> a -> Maybe Ordering
> (~<), (~>), (~<=), (~>=) :: a -> a -> Bool
>
> -- Minimal complete definition:
> -- (~<)& (~>) | pcompare
> pcompare x y | x == y = Just EQ
> | x ~< y = Just LT
> | x ~> y = Just GT
> | otherwise = Nothing
>
> x ~<= y = pcompare x y == Just LT || x == y
> x ~< y = pcompare x y == Just LT
> x ~>= y = pcompare x y == Just GT || x == y
> x ~> y = pcompare x y == Just GT
I get the following error messages:
lecture9.lhs:5:1: error:
Multiple declarations of `pcompare'
Declared at: lecture9.hs:2:4
lecture9.hs:5:1
|
5 | pcompare x y | x == y = Just EQ
| ^^^^^^^^
lecture9.lhs:10:3: error:
Multiple declarations of `~<='
Declared at: lecture9.lhs:3:4
lecture9.lhs:10:3
|
10 | x ~<= y = pcompare x y == Just LT || x == y
| ^^^
lecture9.lhs:11:3: error:
Multiple declarations of `~<'
Declared at: lecture9.lhs:3:4
lecture9.lhs:11:3
|
11 | x ~< y = pcompare x y == Just LT
| ^^
lecture9.lhs:12:3: error:
Multiple declarations of `~>='
Declared at: lecture9.lhs:3:4
lecture9.lhs:12:3
|
12 | x ~>= y = pcompare x y == Just GT || x == y
| ^^^
lecture9.lhs:13:3: error:
Multiple declarations of `~>'
Declared at: lecture9.lhs:3:4
lecture9.lhs:13:3
|
13 | x ~> y = pcompare x y == Just GT
| ^^
I don't understand what causes the multiple declarations error in this example, as it looks very similar to the official PartialOrd typeclass implementation.
As the commenters on your question point out, the issue is one of indentation. For what it's worth, the > symbols are appropriate considering you're trying to compile a literate Haskell file (.lhs) rather than a regular Haskell file. (That said, in a regular Haskell file (.hs), they would cause parsing errors.)
In Haskell, "top-level" declarations are not indented at all. For instance, the definition of the POrd class is not indented. On the flip side, non-top-level declarations must be indented. The type declarations of pcompare and the other operators are part of the POrd class and, as such, are indented.
The definitions of pcompare and the other operators must also be indented, but they can technically go in two different places. They could be defined for particular instances of POrd, or they could be defined as defaults for the POrd class. Based on the definitions themselves, it seems these are intended as defaults.
Long story short, if you indent the definitions of pcompare and the other operators, you should be good to go. (Keep in mind that the > still needs to be at the start of the line, so add the extra spaces just after the >, as in > pcompare x y | x == y ....)

Possibility of having parameterized definitions in haskell?

Is there a way to compactly write multiple definitions in haskell via case, without having to repeat, other than the input parameters, the exact same syntax? The only possible solution I can imagine so far is a macro.
Below is an example of defining binary max and min functions. Can we compress
max' x y
| x > y = x
| otherwise = y
min' x y
| x < y = x
| otherwise = y
into something like
(max',min') x y
| x (>,<) y = x
| otherwise = y
?
Edit:
I know this allows us to parametrize over the "grumpy face", but it seems like there still could be a more succinct form.
maxmin x y f
| f x y = x
| otherwise = y
max' x y = maxmin x y (>)
min' x y = maxmin x y (<)
Well, you can always do this:
select op x y
| x `op` y = x
| otherwise = y
max' = select (>)
min' = select (<)
I.e. extract the common parts into a function and turn the differences into parameters.

whats the advantage of using guards in Haskell?

count_instances :: (Int)->([Int])->Int
count_instances x [] = 0
count_instances x (t:ts)
| x==t = 1+(count_instances x ts)
| otherwise = count_instances x ts
i just want to know whats so good about using guards in this Question ?
A guard can be a way to write only one half of an if-then-else expression; you can omit the else and have a partial function.
-- Leave the function undefined for x /= y
foo x y | x == y = ...
You can do the same with a case statement, but it's more verbose
foo x y = case x == y of
True -> ...
It's also easier to list several unrelated conditions as a set of alternatives than it is with a nested if-then-else or case expressions.
foo x y | p1 x y = ...
foo x y | p2 x y = ...
foo x y | p3 x y = ...
foo x y = ...
vs
foo x y = if p1 x y then ...
else (if p2 x y then ...
else (if p3 x y then ... else ...))
Patterns with guards are probably the most concise way to write code that otherwise would require nested case/if expressions.
Not the least advantage is that a where clause applies to all the guards right hand sides. This is why your example could be even more concise:
count_instances :: (Int)->([Int])->Int
count_instances x [] = 0
count_instances x (t:ts)
| x==t = 1+rest
| otherwise = rest
where rest = count_instances x ts
A guard is haskell's most general conditional statement, like if/then/else in other languages.
Your code shows a straight forward implementation of counting contents of a list equal to a given parameter. This is a good example to learn how haskell's recursion works.
An alternative implementation would be
count_instances :: Int -> [Int] -> Int
count_instances i = length . filter (==i)
that reuses already existing functions from the Prelude module. This is shorter and probably more readable.

More general pattern matching

If I have a function, for example
f :: Int -> Int -> Int
f x y = x + y
and I want to have different functionality based on the parameters, I use pattern matching.
I have only found the syntax of how to match against concrete values, e.g.
f 0 y = y
Is it possible to match against something more general?
I would like to have different functionality in the case that the first parameter is less than 0. A second case could be if the second parameter exceeds a certain value.
You can use guards:
f x y | x < 0 = ...
f x y | y > someValue = ...
f x y | otherwise = ...
Sure, there is a mechanism called guards for that:
f x y | x < 0 = y

Alloy - comparing in first order logic

How does one compare the equality of functions in Alloy? Something like:
--[(All x)(Exists y)[R(x,y)]
-- and (All x)(All y)[R(x,y) -> R(y,x)]]
-- =
-- (All x)[R(x,x)] and
assert checkEquality{
( all m: Model, x:m.A| some y:m.A | (y in x.(m.R)) ) and
( all m: Model, x:m.A, y:m.A | (y in x.(m.R) -> x in y.(m.R)) ) =
( all m: Model, x:m.A | (x in x.(m.R))
}
Here's an elementary version. Guessing by the '(All x)(All y)[R(x,y) -> R(y,x)]]' part, you've probably thought of something more special; in that case, please specify your question further.
sig Value {}
pred p1 [x, y: Value] {
// ...
}
pred p2 [x, y: Value] {
// ...
}
assert equ_pred {
all x, y: Value | p1 [x, y] <=> p2 [x, y]
}
check equ_pred

Resources