Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add section on how to express "FOR ALL" in Rego #1527

Merged
merged 1 commit into from
Jun 28, 2019

Conversation

tsandall
Copy link
Member

This is a common question that comes up. Until we have a keyword that
lets users express "FOR ALL" we should have docs we can point to.

Fixes #1307

Signed-off-by: Torin Sandall torinsandall@gmail.com

@tsandall tsandall requested a review from timothyhinrichs June 27, 2019 05:54
There must be no apps named "bitcoin-miner".
```

You may start by defining a rule named `no_bitcoin_miners` like so:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better if it's clearer that this is the mistake...

"A common mistake is to try encoding the following with the rule named no_bitcoin_miners like so:"

}
```

Unfortunately this rule is incorrect. You can see this by testing the rule:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also use some to show that this is incorrect....

"It becomes clear that this is incorrect when you use the some keyword, because the rule is true whenever there is some app that is not a bitcoin-miner."

no_bitcoin_miners {
    some i
    app := apps[i]
    app.name != "bitcoin-miner"
}
```

quantified_. This means that rule bodies and queries express FOR ANY and not FOR
ALL. To express FOR ALL in Rego use [Negation](#negation) and a bit of basic
logic:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a bridge that gives the english version before the 2 complements:

"For this policy, you define a rule that finds if there exists a bitcoin-mining app (which is easy using the some keyword). And then you use negation to check that there is NO bitcoin-mining app. Technically, you're using 2 negations and an existential quantifier, which is logically the same as a universal quanitfier."

not any_bitcoin_miners
}

any_bitcoin_miners {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use some here for additional clarity


> Whether you use negation or comprehensions to express FOR ALL is up to you.
> The comprehension version is more concise and does not require a helper rule
> while the negation version is more verbose but a bit simpler.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the negation version allows for more complex logic: ORs

#### For All

```ruby
# assert no values in set match predicate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/match predicate/make function f true

# assert no values in set match predicate
count({x | set[x]; f(x)}) == 0

# assert all values in set match predicate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/match predicate/make function f true

@tsandall tsandall force-pushed the fix-1307 branch 2 times, most recently from beca9a2 to 8819e19 Compare June 27, 2019 19:04
This is a common question that comes up. Until we have a keyword that
lets users express "FOR ALL" we should have docs we can point to.

Fixes open-policy-agent#1307

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
@tsandall
Copy link
Member Author

@timothyhinrichs thanks. Incorporated all of your feedback. Let me know if anything else should be changed before merging.

@timothyhinrichs
Copy link
Member

timothyhinrichs commented Jun 28, 2019 via email

@tsandall tsandall merged commit 7d15f46 into open-policy-agent:master Jun 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document typical "for all" patterns
2 participants