Definitions are compared by meaning, through a normal form and a truth table over their conditions¶
Context and Problem Statement¶
The decompiler proves each file by compiling it and comparing the result with the input (ADR-0039), and the round-trip tests do the same over every golden file, example and Azure Landing Zones definition. The two are rarely identical as text: Bylaw orders conditions in its own way, flattens nested groups, writes full alias names, chooses notEquals over not + equals, and may emit notEquals 0 where the input had greater 0 on a count. A textual comparison would report almost every pair as different. How does Bylaw decide that two definitions mean the same thing, without claiming more than it can prove?
Decision Drivers¶
- Sound: "equivalent" is only reported when it is true. A false "equivalent" would let the decompiler write a policy that means something else.
- Useful: the comparison proves the equivalences that Bylaw's own compiling and Azure's documented semantics produce, so real round trips pass.
- A difference is reported with where it is, so it can be fixed.
- No Azure connection.
Considered Options¶
- A normal form, then a truth table over the conditions as propositions
- A normal form only
- Comparing behaviour with the local evaluator on generated resources (ADR-0038)
Decision Outcome¶
Chosen option: "A normal form, then a truth table over the conditions as propositions".
What is compared¶
- The rule:
ifby meaning, as below;thenexactly, after normalising (key order, effect names' case, role definition IDs as GUIDs, and ARM expressions with insignificant whitespace removed). - Everything else exactly, after normalising key order and the documented case-insensitivity:
mode,displayName,description,metadata(ignoringdeployedBy), andparameters, with type names' case and the generated metadata that Bylaw adds. An emptydisplayNameordescription, at the top or in a parameter's metadata, is the same as none, and so is a parameter's emptymetadata, since the portal shows nothing for either and Bylaw writes neither (added 2026-11-30, for Azure Community Policy's NZISM initiative, which has a parameter with"description": ""). A policy without amodeis the same as one withmode: Indexed, since Azure documents that "anullmode is the same as usingindexed";versions, the list of versions Azure has published, on built-in and deployed definitions, is ignored, likepolicyType(both added 2026-12-01, for Azure's built-in policies, of which 832 have no mode and all haveversions). An initiative's members are compared as a set by reference ID, with their parameters and groups.
The normal form¶
Each condition tree is rewritten, by rules that hold in Azure Policy:
- Fields: full alias names, compared ignoring case, as Azure does. Reserved fields keep their names.
- Negation:
notEquals,notIn,notLike,notMatch,notMatchInsensitively,notContains,notContainsKeyandexists: falsebecomenotaround their positive operator, andnotis pushed to the leaves by De Morgan's laws. - Groups: nested
allOfinallOf, andanyOfinanyOf, are flattened; a group of one is unwrapped; children are sorted by their canonical JSON; duplicates are removed. - Values: strings in
equals,in,likeandcontainscompared ignoring case, as Azure does;matchandmatchInsensitivelyas documented;inwith one value isequals; booleans and numbers in their canonical form. - Counts: a comparison of a count with a whole number becomes a range over the non-negative integers, so
greater 0,greaterOrEquals 1andnotEquals 0are the same, andlessOrEquals 0andequals 0are their negation. Thewhereis normalised recursively. [*]fields: a field condition on a[*]path that no enclosingcountiterates holds when every element passes, and for an empty array. On a missing array it evaluates as its operator does on a missing field (ADR-0004):notEqualsis true there,equalsfalse. It is normalised as the count of the elements that break it, equal to 0, which is true for a missing array, so it compares with the equivalentcount; for an operator that is false on a missing field, the array must also exist:allOf[ a exists true, count(...) equals 0 ]. Negation (step 2) is applied only once the field is a single element: on an array,notEquals 'x'(no element isx) is notnot(equals 'x')(not every element isx). Added 2026-11-29 (ADR-0045): until then the comparison folded the operator for[*]fields too, which could have called two different rules equivalent;ConvertTo-Bylawnever wrote a[*]field outside a count, so no conversion relied on it. Corrected 2026-11-29: the missing-array case was assumed to be true, as for an empty one, until a sandbox run (build/verify-array-fields) showed it isn't; the comparison had calleda[*] equals vequivalent to the count alone. Of the conversions that relied on it, only one condition in the Azure Landing Zones Library changed, and its policy's rule was the same either way.
The proof¶
After normalising, the two if conditions are compared:
- Identical normal forms are
Equivalent. - Otherwise, the distinct leaf conditions (atoms) are treated as independent propositions, and both formulas are evaluated for every assignment of them. If they agree on every one, the conditions are equivalent whatever the atoms mean, so the result is
Equivalent. This catches what the normal form misses, such as absorption,A or (A and B)beingA, and a requirement split differently betweenWhenandRequire. - If they disagree on some assignment, the result is
Different, with that assignment as an example, e.g. "withpublicNetworkAccess equals Disabledfalse andkind in [...]true, the input matches and the output doesn't". The atoms may not really be independent, for exampleexists: falseandequals 'x'on the same field can't both be true, so a pair reported as different may be equivalent. The result says so, asDifferent as written, and the decompiler treats it as a failure. - The truth table is limited to 16 atoms in each comparison, 65,536 assignments; above that, the result is
Unproven, which also counts as a failure. Atoms inside acount'swhereare compared inside thatcount, so they don't add to the outer table.
Consequences¶
- Good, because "equivalent" is always true, and a difference comes with an example.
- Good, because the comparison needs nothing but the two definitions, and is fast.
- Bad, because the independence of atoms makes the check conservative: a correct conversion that relies on a relationship between atoms, such as a guard, is reported as different. The decompiler avoids this by reproducing guards exactly (ADR-0039), and the local evaluator, if built (ADR-0038), could settle such cases later.
Confirmation¶
Unit tests cover each normalisation rule, with pairs that must be equivalent and pairs that must not; the truth table, including absorption and the 16-atom limit; and the report of the first difference. The round-trip tests use it on every golden file and example.
Pros and Cons of the Options¶
A normal form, then a truth table¶
- Good, because it is sound and proves the common rearrangements.
- Bad, because it can't use relationships between atoms.
A normal form only¶
- Good, because it is simpler.
- Bad, because many correct round trips, such as different
WhenandRequiresplits, would fail.
Comparing behaviour with the local evaluator¶
- Good, because it would understand relationships between atoms.
- Bad, because the evaluator isn't built, and testing on generated resources shows equivalence only for the resources generated, not in general.