# Avoiding unrealistic constraints in Alloy specifications

DevFeed: [Avoiding unrealistic constraints in Alloy specifications](<https://devfeed.tech/articles/don-t-let-alloy-facts-make-your-specs-a-fiction-52977.md>)

Original publisher: [Read original article](<https://www.hillelwayne.com/post/alloy-facts/>)

Published: 2024-04-10T00:00:00Z

Content type: article

Language: en

Sources: [Hillel Wayne](<https://devfeed.tech/sources/hillel-wayne.md>)

Topics: [Specifications](<https://devfeed.tech/topics/specifications.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Package Management](<https://devfeed.tech/topics/package-management.md>)

Tags: [alloy](<https://devfeed.tech/tags/alloy.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [dependency](<https://devfeed.tech/tags/dependency.md>), [enums](<https://devfeed.tech/tags/enums.md>), [error-handling](<https://devfeed.tech/tags/error-handling.md>), [formal-methods](<https://devfeed.tech/tags/formal-methods.md>), [input](<https://devfeed.tech/tags/input.md>), [invariant](<https://devfeed.tech/tags/invariant.md>), [specifications](<https://devfeed.tech/tags/specifications.md>)

## AI overview

The article explains how overly strong Alloy facts can make invalid system states impossible to represent, preventing the specification from testing error handling. It uses package dependency models, including self-dependencies, to show why specifications should represent both valid software behavior and invalid environmental inputs.

## Source excerpt

I've recently done a lot of work in Alloy and it's got me thinking about a common specification pitfall. Everything in the main post applies to all formal specifications, everything in dropdowns is for experienced Alloy users. Consider a simple model of a dependency tree. We have a set of top-level dependencies for our program, which have their own dependencies, etc. We can model it this way in Alloy: sig Package { , depends_on: set Package } run {some depends_on} Show Alloy tip I'm going to use a slightly different model for the next example: abstract sig Package { , depends_on: set Package } lone sig A, B extends Package {} run {some depends_on} I do things this way because it gives visualizations with A and B instead of Package$0 and Package$1. Alloy has built-in enums but they don't play nice with the rest of the language (you can't extend them or give them fields). show all If we look through some of the generated examples, we see something odd: a package can depend on itself! These kinds of nonsensical situations arise often when we're specifying, because we have an intent of what the system should be but don't explicitly encode it. When this happens, we need to add additional constraints to prevent it. For this reason, Alloy has a special "fact" keyword:1 fact no_self_deps { all p: Package { p not in p.depends_on } } Show Alloy tip You can write the same fact purely relationally: fact { no depends_on & iden } In general, the model checker evaluates purely-relational expressions much faster than quantified expressions. This can make a big difference in large models! show all Alloy will not generate any models that violate a fact, nor will it look for invariant violations in them. It's a "fact of reality" and doesn't need to be explored at all. The pitfall of facts "No self-deps" is a great example of a fact. It's also an example of a terrible fact. Beginners often make this mistake where they use facts to model the system, which quickly leads to problems. Cons