Skip to content

Lemmata for if_then_else_ #2747

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ Additions to existing modules
∙-cong-∣ : x ∣ y → a ∣ b → x ∙ a ∣ y ∙ b
```

* In `Data.Bool.Properties`:
```agda
if-eta : ∀ b → (if b then x else x) ≡ x
if-idem-then : ∀ b → (if b then (if b then x else y) else y) ≡ (if b then x else y)
if-idem-else : ∀ b → (if b then x else (if b then x else y)) ≡ (if b then x else y)
if-swap-then : ∀ b c → (if b then (if c then x else y) else y) ≡ (if c then (if b then x else y) else y)
if-swap-else : ∀ b c → (if b then x else (if c then x else y)) ≡ (if c then x else (if b then x else y))
if-not : ∀ b → (if not b then x else y) ≡ (if b then y else x)
if-∧ : ∀ b → (if b ∧ c then x else y) ≡ (if b then (if c then x else y) else y)
if-∨ : ∀ b → (if b ∨ c then x else y) ≡ (if b then x else (if c then x else y))
if-xor : ∀ b → (if b xor c then x else y) ≡ (if b then (if c then y else x) else (if c then x else y))
if-cong : b ≡ c → (if b then x else y) ≡ (if c then x else y)
if-cong-then : ∀ b → x ≡ z → (if b then x else y) ≡ (if b then z else y)
if-cong-else : ∀ b → y ≡ z → (if b then x else y) ≡ (if b then x else z)
if-cong₂ : ∀ b → x ≡ z → y ≡ w → (if b then x else y) ≡ (if b then z else w)
```

* In `Data.Fin.Base`:
```agda
_≰_ : Rel (Fin n) 0ℓ
Expand Down
66 changes: 66 additions & 0 deletions src/Data/Bool/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,72 @@ if-float : ∀ (f : A → B) b {x y} →
if-float _ true = refl
if-float _ false = refl

if-eta : ∀ b {x : A} →
(if b then x else x) ≡ x
if-eta false = refl
if-eta true = refl

if-idem-then : ∀ b {x y : A} →
(if b then (if b then x else y) else y) ≡ (if b then x else y)
if-idem-then false = refl
if-idem-then true = refl

if-idem-else : ∀ b {x y : A} →
(if b then x else (if b then x else y)) ≡ (if b then x else y)
if-idem-else false = refl
if-idem-else true = refl

if-swap-then : ∀ b c {x y : A} →
(if b then (if c then x else y) else y)
≡ (if c then (if b then x else y) else y)
if-swap-then false false = refl
if-swap-then false true = refl
if-swap-then true _ = refl

if-swap-else : ∀ b c {x y : A} →
(if b then x else (if c then x else y))
≡ (if c then x else (if b then x else y))
if-swap-else false _ = refl
if-swap-else true false = refl
if-swap-else true true = refl

if-not : ∀ b {x y : A} →
(if not b then x else y) ≡ (if b then y else x)
if-not false = refl
if-not true = refl

if-∧ : ∀ b {c} {x y : A} →
(if b ∧ c then x else y) ≡ (if b then (if c then x else y) else y)
if-∧ false = refl
if-∧ true = refl

if-∨ : ∀ b {c} {x y : A} →
(if b ∨ c then x else y) ≡ (if b then x else (if c then x else y))
if-∨ false = refl
if-∨ true = refl

if-xor : ∀ b {c} {x y : A} →
(if b xor c then x else y) ≡ (if b then (if c then y else x) else (if c then x else y))
if-xor false = refl
if-xor true {false} = refl
if-xor true {true } = refl

if-cong : ∀ {b c} {x y : A} → b ≡ c →
Copy link
Member

Choose a reason for hiding this comment

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

Thinking about it, isn't this just cong (if_then x else y)? And similar for the other cong lemmas. Is it worth including?

Copy link
Contributor Author

@pmbittner pmbittner Jun 26, 2025

Choose a reason for hiding this comment

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

Yes, it is indeed exactly that.

The benefit of the if-cong lemmas is that we can infer some of the branches automatically with implicit arguments, so we can omit them. That was the reason why I created these cong-lemmas in our project because sometimes we had large expressions in these branches and it was tedious to spell them out. Example:

open import Data.Bool using (if_then_else_)
open import Data.Bool.Properties using (if-cong-else)
open import Data.Nat using (ℕ; _+_)
open import Data.Nat.Properties using (+-comm)
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; cong)

looooooong-expression = 1

with-if-cong :  b (x y : ℕ)
   (if b then looooooong-expression else x + y)
  ≡ (if b then looooooong-expression else y + x)
with-if-cong b x y = if-cong-else b (+-comm x y)

with-cong :  b (x y : ℕ)
   (if b then looooooong-expression else x + y)
  ≡ (if b then looooooong-expression else y + x)
with-cong b x y = cong (if b then looooooong-expression else_) (+-comm x y)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This does not help with all elements within an if_then_else_ though because we still have to spell out the condition (b in the example above).

(if b then x else y) ≡ (if c then x else y)
if-cong refl = refl

if-cong-then : ∀ b {x y z : A} → x ≡ z →
(if b then x else y) ≡ (if b then z else y)
if-cong-then _ refl = refl

if-cong-else : ∀ b {x y z : A} → y ≡ z →
(if b then x else y) ≡ (if b then x else z)
if-cong-else _ refl = refl

if-cong₂ : ∀ b {x y z w : A} → x ≡ z → y ≡ w →
(if b then x else y) ≡ (if b then z else w)
if-cong₂ _ refl refl = refl

------------------------------------------------------------------------
-- Properties of T

Expand Down