Skip to content

Add Properties of List actions on Bools#3005

Merged
jamesmckinna merged 7 commits into
masterfrom
bool-listaction-props
Jun 17, 2026
Merged

Add Properties of List actions on Bools#3005
jamesmckinna merged 7 commits into
masterfrom
bool-listaction-props

Conversation

@Taneb

@Taneb Taneb commented Jun 8, 2026

Copy link
Copy Markdown
Member

I'm not sure of the name for and-locate and or-locate, but they're the whole reason I ended up making this PR...

@jamesmckinna

Copy link
Copy Markdown
Collaborator

It looks as though I added a note to Data.Bool.ListAction suggesting it be refactored to make use of https://github.com/agda/agda-stdlib/blob/master/src/Data/List/Effectful/Foldable.agda but I no longer entirely remember what I had had in mind!

Would it be better to refactor this PR to make use of the properties defined there? And perhaps to do that prior refactoring?

@JacquesCarette

Copy link
Copy Markdown
Collaborator

I'm okay with and-locate and or-locate. But definitely most of these properties are true more generally (of [commutative] monoid folds), with the locate property being of monoids with a 'zero' element.

I would probably be fine with adding these as-is, and refactoring later when more general proofs exist.

@jamesmckinna

Copy link
Copy Markdown
Collaborator

I'm okay with and-locate and or-locate. But definitely most of these properties are true more generally (of [commutative] monoid folds), with the locate property being of monoids with a 'zero' element.

I would probably be fine with adding these as-is, and refactoring later when more general proofs exist.

Fine by me.

FTR, I think that the converse directions to the locate lemmas follow from having a zero element, but these lemmas further require that the monoid operation be Conical wrt that zero?

@Taneb

Taneb commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

Yes, it requires there are no "zero divisors" in the monoid, which is not the case in, for example, the monoid of integers modulo 6 under multiplication, where 2 * 3 = 0

@jamesmckinna

Copy link
Copy Markdown
Collaborator

Add a CHANGELOG and you'll be good to go.

@jamesmckinna

Copy link
Copy Markdown
Collaborator

OK, so I experimented with my proposed refactoring, and surprise surprise not all was plain sailing...

  • Data.Bool.Properties doesn't export any (obvious!) definitions of ∧-monoid, ∨-monoid, so the ++-homo theorem isn't directly applicable... (groan)
  • there is some more plumbing to do to ensure that foldr and foldMap id are appropriately correctly identified, so that the theorem, even if applicable, actually returns the correct result definitionally... (sigh)
  • I must at some point have run of steam refactoring Permutation with the result that the appeal to foldr-commMonoid itself requires so much extraneous additional plumbing to apply... (bewilderment)

So I'll write a cut-down review, and then let's merge this as-is, and figure out the mess afterwards!

@jamesmckinna jamesmckinna left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All but the CHANGELOG typo are optional.

But my general meta-comment for the library going forward ought perhaps to be that we try to 'take the high road', ie. reuse all the abstraction machinery we have developed, rather than taking the 'low' road of proving things directly by pattern-matching, which otherwise is too 'easy'/'tempting' in cases like these?


-- and

and-++ : ∀ bs cs → and (bs ++ cs) ≡ and bs ∧ and cs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This (morally) should have the proof and-++ bs cs = ++-homo ∧-monoid id bs {cs} but doesn't, for reasons given.

Comment on lines +38 to +44
∨-distribˡ-and : ∀ b cs → b ∨ and cs ≡ all (b ∨_) cs
∨-distribˡ-and b [] = ∨-zeroʳ b
∨-distribˡ-and b (c ∷ cs) = trans (∨-distribˡ-∧ b c (and cs)) (cong ((b ∨ c) ∧_) (∨-distribˡ-and b cs))

∨-distribʳ-and : ∀ b cs → and cs ∨ b ≡ all (_∨ b) cs
∨-distribʳ-and b [] = ∨-zeroˡ b
∨-distribʳ-and b (c ∷ cs) = trans (∨-distribʳ-∧ b c (and cs)) (cong ((c ∨ b) ∧_) (∨-distribʳ-and b cs))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These two however, should be rewritten in terms of Data.List.Properties.foldr-map, given the definition of all as a foldr (and) followed by a map? Or is the ergonomics of doing such similarly too tortured to be worth the effort?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was my best effort:

∨-distribˡ-and :  b cs  b ∨ and cs ≡ all (b ∨_) cs
∨-distribˡ-and b cs = begin
  b ∨ foldr _∧_ true cs                ≡⟨ foldr-fusion (b ∨_) true (∨-distribˡ-∧ b) cs ⟩
  foldr (b ∨_ -⟨ _∧_ ∣) (b ∨ true) cs  ≡⟨ foldr-map _∧_ (b ∨_) (b ∨ true) cs ⟨
  foldr _∧_ (b ∨ true) (map (b ∨_) cs) ≡⟨ cong (λ e  foldr _∧_ e (map (b ∨_) cs)) (∨-zeroʳ b) ⟩
  foldr _∧_ true (map (b ∨_) cs)       ∎
  where open ≡-Reasoning

I personally don't think it's worth it, but maybe I'm missing a trick

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe you're right... I managed

∨-distribˡ-and b cs = begin
  b ∨ and cs            ≡⟨ foldr-fusion (b ∨_) true (∨-distribˡ-∧ b) cs ⟩
  foldr g (b ∨ true) cs ≡⟨ foldr-cong (λ _ _  refl) (∨-zeroʳ b) cs ⟩
  foldr g true cs       ≡⟨ foldr-map _∧_  (b ∨_) true cs ⟨
  all (b ∨_) cs         ∎
  where
  open ≡-Reasoning
  g : Bool  Bool  Bool
  g z = (b ∨ z) ∧_

which isn't much better, but is more 'abstract'?

I won't fight you for this though!


-- or

or-++ : ∀ bs cs → or (bs ++ cs) ≡ or bs ∨ or cs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ditto. or-++ bs cs = ++-homo ∨-monoid id bs {cs}, or... should be, modulo blah blah blah.

Comment on lines +63 to +69
∧-distribˡ-or : ∀ b cs → b ∧ or cs ≡ any (b ∧_) cs
∧-distribˡ-or b [] = ∧-zeroʳ b
∧-distribˡ-or b (c ∷ cs) = trans (∧-distribˡ-∨ b c (or cs)) (cong ((b ∧ c) ∨_) (∧-distribˡ-or b cs))

∧-distribʳ-or : ∀ b cs → or cs ∧ b ≡ any (_∧ b) cs
∧-distribʳ-or b [] = ∧-zeroˡ b
∧-distribʳ-or b (c ∷ cs) = trans (∧-distribʳ-∨ b c (or cs)) (cong ((c ∧ b) ∨_) (∧-distribʳ-or b cs))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ditto. foldr-map instances?

Comment thread src/Data/Bool/ListAction/Properties.agda
Comment thread src/Data/Bool/ListAction/Properties.agda
Comment thread src/Data/Bool/ListAction/Properties.agda Outdated
Comment thread src/Data/Bool/ListAction/Properties.agda Outdated
Comment thread CHANGELOG.md Outdated
@Taneb

Taneb commented Jun 17, 2026

Copy link
Copy Markdown
Member Author

Data.Bool.Properties doesn't export any (obvious!) definitions of ∧-monoid, ∨-monoid, so the ++-homo theorem isn't directly applicable... (groan)

That's a serious omission that should definitely be fixed!

@jamesmckinna jamesmckinna dismissed their stale review June 17, 2026 15:42

Requested changes have now been made, so I can approve.

@jamesmckinna jamesmckinna left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All fine by me now, and I think the one approval should suffice!

@jamesmckinna jamesmckinna enabled auto-merge June 17, 2026 15:44
@jamesmckinna jamesmckinna added this pull request to the merge queue Jun 17, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 17, 2026
@jamesmckinna jamesmckinna enabled auto-merge June 17, 2026 17:38
@jamesmckinna jamesmckinna added this pull request to the merge queue Jun 17, 2026
Merged via the queue into master with commit 3b5ee82 Jun 17, 2026
13 checks passed
@Taneb Taneb deleted the bool-listaction-props branch June 18, 2026 04:44
@jamesmckinna

Copy link
Copy Markdown
Collaborator

A last thought here, but one which should perhaps be revisited: we define all resp. any in terms of and resp. or, but what if we did it the other way round?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants