Skip to content

ConvertTo-Bylaw rewrites negated and [*] conditions into forms the language has, rather than the language gaining a Not group or bare [*] fields

Context and Problem Statement

Milestone 7 aims to need Condition (ADR-0040) less when converting the Azure Landing Zones Library (ALZ, ADR-0042). With the full alias snapshot, ALZ needs it for 27 conditions in 11 policy definitions (2026-11-29). 18 of them use not, so the roadmap proposed a Not { } group, or -Not on AnyOf and AllOf. How should Bylaw handle them?

Looking at the 18, none is a not around a group. Each wraps one condition, and they fall into three kinds:

Kind Count Example Already expressible as
A plain field 3 not(keySource equals 'Microsoft.Keyvault') the complementary operator, keySource notEquals 'Microsoft.Keyvault', which ConvertTo-Bylaw already writes
A [*] field outside a count 12, 5 of them inside another count's where not(destinationPortRanges[*] notEquals '*') Count destinationPortRanges[*] -Where { Element | Must -Be '*' } | Must -BeGreaterThan 0
A value count 3 not(count(mandatoryTags where ...) equals length(...)) Count -Value ... | Must -Not -Be ...: Current already works in -Where (ADR-0032), so the not was all that was missing. Converted 2026-11-29 by inverting the count's comparison, since a count is never missing
  • Plain fields. Azure's operators come in complementary pairs, equals/notEquals, in/notIn, contains/notContains, like/notLike, match/notMatch, containsKey/notContainsKey, and exists: true/exists: false, and on a single value each pair is exact, including for a missing field (one of the two is true; ADR-0004's truth table). So not(f op v) is f notOp v. The same holds for a value condition, which has no missing case at all, so not(value op v) is value notOp v (added 2026-12-01, #91: 70 conditions in 11 of Azure's built-in policies). The ordering operators are kept as written, for fields and values alike.
  • [*] fields. A condition on a[*] outside a count means "every element satisfies it", and is true for an empty array (Learn: referencing the array members collection; ADR-0002, ADR-0008 rule 6). So not(a[*] cond) means "some element breaks it", which is a count: count(a[*] where not cond) greater 0. For an empty array both are false. On a missing array, a [*] condition evaluates as its operator does on a missing field (measured 2026-11-29, ADR-0041), so when that is false, as for equals or in, the not holds there, and the count needs the array missing as an alternative: anyOf[ a exists false, count(...) greater 0 ]. It is not a[*] notCond: for ["a", "b"], a[*] equals 'a' and a[*] notEquals 'a' are both false.

The language can already write the first two kinds: Must -Not and the operators, AnyOf/AllOf with De Morgan, and Count ... -Where. ConvertTo-Bylaw just doesn't rewrite a not into those forms, so it falls back to Condition.

One more finding. The semantic comparison that proves conversions (ADR-0041) turns every negated operator into not(positive). For a [*] field that is wrong, for the reason above, so it could call two different definitions equivalent. ConvertTo-Bylaw never writes a [*] field outside a count today, which is likely why no proof has gone wrong, but the rewrite for the second kind depends on the comparison getting exactly this right.

Decision Drivers

  • Fewer Conditions in converted policies, each conversion still proven equivalent.
  • The language stays small: no keyword without a case that needs it.
  • The proof must be sound for [*] fields before a rewrite relies on it.
  • A file says what it means. "Every element, and true when there are none" is easy to misread as "some element": authors often write groupIds[*] equals x when they mean "contains x".
  • A statement means the same wherever it is written: moving it between When, Require and -Where shouldn't change whether it compiles.

Considered Options

  • Rewrite negated conditions in ConvertTo-Bylaw, and fix the comparison for [*] fields
  • Add a Not { } group to the language
  • Both
  • For step 4 (added 2026-11-29): accept bare [*] fields in When and -Where, emitted as written
  • For step 4: accept Each inside -Where, as count(...) equals 0

Decision Outcome

Chosen option: "Rewrite negated conditions in ConvertTo-Bylaw, and fix the comparison for [*] fields", accepted 2026-11-29. ALZ has no case a Not group would serve that the language can't already write, so it waits for one. A not around a group, when one came up, needed no keyword either: see "A not around a group".

In order, as separate pull requests:

  1. Fix the comparison (ADR-0041). A field condition on a [*] path outside a count becomes a quantified form, "no element breaks it": the same normal form as count(a[*] where not cond) equals 0, so a[*] notEquals v is no longer taken as not(a[*] equals v), and the two count forms compare equal. Tests: a[*] notEquals v and not(a[*] equals v) are different; not(a[*] notEquals v) and count(a[*] where a[*] equals v) greater 0 are equivalent; the same inside a count's where. Done (ADR-0041, step 6).
  2. Plain fields. ConvertTo-Bylaw pushes a not around a single field condition into the complementary operator, then writes it as usual, for the equality operators and exists, whose complements the comparison folds; a not around an ordering operator stays Condition. Done: 3 of the 18.
  3. [*] fields at the top level. not(a[*] cond) becomes Count a[*] -Where { <cond on the element, inverted> } | Must -BeGreaterThan 0. A path with a property after [*], such as a[*].b, uses Field b in -Where; one that ends in [*] uses Element. Only when the snapshot has the array, so the language can count it; otherwise the not is kept, for Condition. As a violation, the count is written as Each when its -Where is the language's own, as ConvertTo-Bylaw writes any such count. Done: 7 more.
  4. Every [*] field condition, at every array level, with or without not. See below. 5 of the 18, and 3 more conditions without a not. Done: all 8, leaving 9 Conditions in 5 policy definitions.

After steps 1 to 3, ALZ's Condition count should fall from 27 to 17, and Deny-MgmtPorts-From-Internet from 10 to 5. After step 2 it was 24, in 9 policy definitions, and after step 3 it is 17, in 8. The 3 value counts were converted afterwards by inverting the count's comparison, which leaves 1 Condition in all of ALZ.

A not around a group

Added 2026-11-30. ALZ has no not around a group, but a policy auditing VMs for BYOL licensing has one: not(anyOf[ licenseType equals Windows_Server, imageOffer contains byos, imagePublisher in [...], imagePublisher in (parameter) ]). ConvertTo-Bylaw wrote it with Condition. By De Morgan's laws it is allOf of the four nots, and each of those is a not around a single field, which step 2 writes as the complementary operator. So the decision stands: no Not { } keyword. ConvertTo-Bylaw rewrites not(anyOf[ ... ]) as allOf of the nots, and not(allOf[ ... ]) as anyOf, then rewrites each not as the other steps allow. A child they can't rewrite keeps its not, which is still exact. The comparison applies De Morgan's laws too (ADR-0041), so the proof is exact.

The policy's violation becomes one Require, its compliant form:

Require 'licenseType, imageOffer and imagePublisher are as required' {
    AnyOf {
        Field 'Microsoft.Compute/licenseType' | Must -Be 'Windows_Server'
        Field 'Microsoft.Compute/imageOffer' | Must -ContainText 'byos'
        Field 'Microsoft.Compute/imagePublisher' | Must -BeIn @('Canonical', 'Debian', 'OpenLogic')
        Field 'Microsoft.Compute/imagePublisher' | Must -BeIn (Param AdditionalImages)
    }
}

To keep it one Require, the if condition is split into its conjuncts as the input writes them, the type, When and the violation, before any rewrite. Each conjunct is rewritten on its own, so a rewrite that makes an allOf doesn't spread it across When. The same applies to the guard of step 4: allOf[ a exists true, count(...) equals 0 ] stays in its Require, as an AnyOf of Must -Not -Exist and the count.

Step 4: every [*] field condition

Amended 2026-11-29. Step 4 was planned as [*] fields inside a count's where, waiting for nested arrays. But the language already nests arrays: Count nests inside -Where, and Each and Count nest inside Each, with paths relative to the element (ADR-0032). BYL2005's fix already says "nest one Each or Count per level". So step 4 is step 3's rewrite, applied more widely. It isn't a language change.

What it covers. Of the 17 conditions left after step 3, 8 are a bare [*] field condition that no enclosing count iterates:

Where Count Example
In a count's where, under a second array 5, all in Deny-MgmtPorts-From-Internet not(securityRules[*].destinationPortRanges[*] notEquals '*') inside count(securityRules[*] where ...)
The same, without not 2, in Deploy-Private-DNS-Generic privateLinkServiceConnections[*].groupIds[*] equals groupId inside count(privateLinkServiceConnections[*] where ...)
At the top level, without not 1, in Deny-Storage-ResourceAccessRulesTenantId networkAcls.resourceAccessRules[*].tenantId notEquals [subscription().tenantId]

The rewrite. A field condition is rewritten when its path has a [*] beyond the arrays that enclosing counts iterate, with or without a not. Let a[*] be the path up to the first such [*], and c' the complement of the condition, the same field with the complementary operator, as in step 2:

  • a[*] ... cond becomes count(a[*] where c') equals 0, and not(a[*] ... cond) becomes count(a[*] where c') greater 0.
  • When cond's operator is false on a missing field, such as equals, in, like, contains or exists: true, the rewrite also tests the array, since a missing array gives the operator's missing-field value while its count is 0 (ADR-0041 step 6). So a[*] ... cond becomes allOf[ a exists true, count(...) equals 0 ], and not(a[*] ... cond) becomes anyOf[ a exists false, count(...) greater 0 ], as step 3 already writes. For notEquals, notIn and the others that are true on a missing field, the count alone is exact.
  • The new count iterates a[*]. When the field has another [*] after it, as in a[*].b[*], its where isn't c': on one element of a[*] the condition is still about every element of b[*], so the complementary operator would say "every one breaks it", not "some does". The where is not(cond) rewritten again, one level in, as count(a[*].b[*] where c') greater 0. That gives one count per array level, at the top level or inside another count's where.
  • As in steps 2 and 3, only the equality operators and exists are rewritten. The snapshot must have every array the rewrite counts, and the array itself where the guard tests it, so the language can name them. Otherwise the input is kept, for Condition, as written.
  • The result is written by the decompiler's existing forms. In a Require, a violation count is written as Each when its where is the language's own. Elsewhere it is written as Count ... -Where { } | Must, with Field a | Must -Exist beside it when there is a guard. A path ending in [*] uses Element, and one with a property after it uses Field. For example, the Private DNS condition is an equals, so it is guarded:
Count 'privateLinkServiceConnections[*]' -Where {
    Field privateLinkServiceId | Must -ContainText (Param ResourceType)
    Field groupIds | Must -Exist
    Count 'groupIds[*]' -Where {
        Element | Must -Not -Be (Param GroupId)
    } | Must -Be 0
} | Must -BeLessThan 1

The language is unchanged. A bare [*] field stays a compile error everywhere (BYL2005, ADR-0008 rule 6), including in When and -Where, where nothing is inverted. See the options below. BYL2005's suggested fix names the field after the array, so for a path ending in [*] it prints Field | Must. That is fixed to Element | Must.

Result. All 8 convert, as expected. The 2 in Deploy-Private-DNS-Generic use equals, so they are guarded. The other 6 use notEquals or notIn, so they are not. ALZ is then left with 9 Conditions in 5 policy definitions:

  • 3 are a not around a value count. Converted since, by inverting the count's comparison: Current already worked in -Where.
  • 4 are anyOf[ exists: false, notEquals ], a guard Bylaw never writes, since notEquals is already true on a missing field. Converted since, by writing the parts on their own (ADR-0039).
  • 1 is allOf[ exists: true, less ], a guard Bylaw never writes either, since less is already false on a missing field. Converted since, in the same way.
  • 1 compares a string that looks like an expression, current(...) without brackets, which Azure reads as text.

Consequences

  • Good, because the conversions use the language people already write, and stay proven.
  • Good, because the comparison becomes sound for [*] fields, which matters whatever the decompiler does.
  • Neutral, because an author who thinks in not writes Must -Not, or De Morgan with AnyOf and AllOf, as today.
  • Neutral, because a not around a whole group needs no keyword either: see "A not around a group" below.
  • Good, because a converted [*] condition says what "every element" means, as a count of the elements that break it, with the array required to exist where Azure requires it. So the empty and missing cases are visible rather than implied.
  • Bad, because a converted [*] condition is longer than the input, especially when nested.

Confirmation

Unit tests for the comparison's [*] forms, the decompiler's rewrites, and the ALZ baseline recording the improvement (ADR-0042). For step 4, the unit tests also cover:

  • the rewrite with and without not, at the top level, in a count's where, and under two arrays at the top level;
  • the guard, present for an operator that is false on a missing field and absent for one that is true there;
  • a field the snapshot lacks, which stays Condition as written;
  • the BYL2005 message for a path ending in [*].

Pros and Cons of the Options

Add a Not { } group to the language

  • Good, because it reads as written for a not around a group.
  • Bad, because none of ALZ's 18 is a not around a group, so it removes none of them as written: each would still need the rewrites above to be idiomatic.
  • Bad, because it adds a second way to say what Must -Not and De Morgan already say, with its own missing-field rules to define and test.

Both

  • Good, because it covers a future not around a group.
  • Bad, because it builds the keyword before anything needs it.

Accept bare [*] fields in When and -Where, emitted as written

  • Good, because the conversion is literal and short.
  • Bad, because the same statement would compile in When and fail in Require, which breaks the rule that a statement means the same wherever it is.
  • Bad, because the implicit "every element, true when empty, the operator's value when missing" is the meaning authors most often get wrong, and Bylaw would no longer catch it.

Accept Each inside -Where

  • Good, because Each reads well for "every element".
  • Bad, because Each is a requirement, inverted and guarded, while -Where is written as is. ADR-0032 keeps them apart for that reason, and Count ... | Must -Be 0 already says it.
  • Bad, because it covers only the forms without not.

More Information