Exercise 2: Rewriting "not (is_valid and is_authorized)" via De Morgan's Law — Possible Solution ==================================================================== APPLYING THE LAW ------------------------------ The original condition is ~(is_valid ^ is_authorized) - a negated AND. Per this chapter's own De Morgan's Law for AND, "~(p ^ q) = ~p v ~q" - negating an AND flips it into an OR of the individual negations. Applying this directly: not (is_valid and is_authorized) == (not is_valid) or (not is_authorized) So the rewritten condition is: if (not is_valid) or (not is_authorized): WHY THIS IS GUARANTEED TO BEHAVE IDENTICALLY ------------------------------ De Morgan's Law isn't a heuristic or a stylistic suggestion - it's a proven logical equivalence, verifiable by comparing the two expressions' truth tables row by row across every possible combination of is_valid and is_authorized: is_valid | is_authorized | valid^auth | ~(valid^auth) | ~valid v ~auth ---------|----------------|-----------|----------------|---------------- T | T | T | F | F T | F | F | T | T F | T | F | T | T F | F | F | T | T The last two columns match on every single row. Since is_valid and is_authorized are booleans, there are only these four possible combinations of values they could ever hold - and the two expressions agree on all four, so there is no possible runtime state in which they could ever produce different results. WHY THIS MATTERS BEYOND JUST BEING "TIDIER" ------------------------------ Per this chapter's own reasoning, "if two boolean expressions are logically equivalent, replacing one with the other can never change what your program does." This isn't a readability preference alone - it's a mathematically guaranteed safe refactor, which is exactly why proving the equivalence via truth table (rather than just assuming the rewrite "looks right") is the correct way to justify it. WHY THE COMMON ALTERNATIVE REWRITE WOULD BE WRONG ------------------------------ Per this chapter's own warning, writing "not is_valid and not is_authorized" instead would actually compute ~valid ^ ~auth (both must be false) rather than ~valid v ~auth (at least one must be false) - a genuinely different condition that only agrees with the correct rewrite in the all-true and all-false rows, and disagrees in the two mixed rows. WHY THIS WORKS AS AN ANSWER ------------------------------ It applies the specific law by name, shows the rewritten condition, proves the equivalence exhaustively via a full truth table covering every possible boolean combination, and explains why this constitutes a real guarantee rather than just a stylistic improvement.