Skip to content
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

Add new Bool action on a RawMonoid plus properties #2450

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ New modules
Additions to existing modules
-----------------------------

* In `Algebra.Definitions.RawMonoid` action of a Boolean on a RawMonoid:
```agda
_∧_ : Bool → Carrier → Carrier
_∧′_∙_ : Bool → Carrier → Carrier → Carrier
```

* In `Algebra.Properties.Monoid.Mult` properties of the Boolean action on a RawMonoid:
```agda
∧-homo-true : true ∧ x ≈ x
∧-assocˡ : b ∧ (b′ ∧ x) ≈ (b Bool.∧ b′) ∧ x
b∧x∙y≈b∧x+y : b ∧′ x ∙ y ≈ (b ∧ x) + y
```

* In `Data.List.Relation.Unary.All`:
```agda
search : Decidable P → ∀ xs → All (∁ P) xs ⊎ Any P xs
Expand Down
18 changes: 17 additions & 1 deletion src/Algebra/Definitions/RawMonoid.agda
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# OPTIONS --cubical-compatible --safe #-}

open import Algebra.Bundles using (RawMonoid)
open import Data.Bool.Base as Bool using (Bool; true; false; if_then_else_)
open import Data.Nat.Base as ℕ using (ℕ; zero; suc)
open import Data.Vec.Functional as Vector using (Vector)

Expand All @@ -21,7 +22,7 @@ open RawMonoid M renaming ( _∙_ to _+_ ; ε to 0# )
open import Algebra.Definitions.RawMagma rawMagma public

------------------------------------------------------------------------
-- Multiplication by natural number
-- Multiplication by natural number: action of the (0,+)-rawMonoid
------------------------------------------------------------------------
-- Standard definition

Expand Down Expand Up @@ -65,3 +66,18 @@ suc n ×′ x = n ×′ x + x

sum : ∀ {n} → Vector Carrier n → Carrier
sum = Vector.foldr _+_ 0#

------------------------------------------------------------------------
-- 'Conjunction' with a Boolean: action of the Boolean (true,∧)-rawMonoid
------------------------------------------------------------------------

infixr 8 _∧_

_∧_ : Bool → Carrier → Carrier
b ∧ x = if b then x else 0#
Copy link
Contributor

Choose a reason for hiding this comment

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

Strictly speaking, this could be defined for any Pointed structure with an apartness relation, as an action on a Pointed structure.

How is the above 'conjuction'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I think that's fair... I was being pretty ad hoc in wanting to add this by analogy with the Nat action already defined there, as then these two instances of a phenomenon (eventually: wreath product) are the relevant ones for the Algebra.Solver.*CommutativeMonoid refactorings #2407 #2457 ... and I'm still unhappy about the general picture for Pointed things in the library, so don't (yet) want to reopen that can of worms...

As for it being 'conjunction', well it clearly generalises it: for Carrier being that of the Monoid structure on Bool given by _∨_ with unit false,

b ∧ x = if b then x else false

is a definition of 'conjunction'... yes!?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that is the operational definition of 'conjunction' on Bool.

I guess that I was thinking along the lines of "if b is the least element then x else some-distinguished-point" as the general meaning. It feels more action-y. 'conjunction' feels like it's a coincidence when Carrier is Bool.


-- tail-recursive optimisation
Copy link
Contributor

Choose a reason for hiding this comment

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

optimisation of what? It doesn't seem like an optimisation of the above? What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As with the TCOptimised forms of (monoid) sum and multiplication, the 'optimisation' lies in avoiding having to explicitly track terms of the form x + 0# when these can be computed away instead to x... But I admit I haven't quite got my story straight yet... ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As witnessed by b∧x∙y≈b∧x+y...

Copy link
Contributor

Choose a reason for hiding this comment

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

It still feels like a stretch. I do like the optimization of an action not doing a (statically known) null operation, a lot. I'm still worried that this particular case is a coincidence rather than fundamental!

Copy link
Contributor Author

@jamesmckinna jamesmckinna Aug 27, 2024

Choose a reason for hiding this comment

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

Well I think it extends to the Nat cases as well (but obscured in the current presentation, which doesn't introduce the tail-recursive forms... but see #2475 )

infixl 8 _∧′_∙_

_∧′_∙_ : Bool → Carrier → Carrier → Carrier
b ∧′ x ∙ y = if b then x + y else y
18 changes: 16 additions & 2 deletions src/Algebra/Properties/Monoid/Mult.agda
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# OPTIONS --cubical-compatible --safe #-}

open import Algebra.Bundles using (Monoid)
open import Data.Bool.Base as Bool using (Bool; true; false; if_then_else_)
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; NonZero)
open import Relation.Binary.Core using (_Preserves_⟶_; _Preserves₂_⟶_⟶_)
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
Expand Down Expand Up @@ -34,7 +35,7 @@ open import Algebra.Definitions _≈_
-- Definition

open import Algebra.Definitions.RawMonoid rawMonoid public
using (_×_)
using (_×_; _∧_; _∧′_∙_)

------------------------------------------------------------------------
-- Properties of _×_
Expand All @@ -60,7 +61,7 @@ open import Algebra.Definitions.RawMonoid rawMonoid public
×-homo-+ : ∀ x m n → (m ℕ.+ n) × x ≈ m × x + n × x
×-homo-+ x 0 n = sym (+-identityˡ (n × x))
×-homo-+ x (suc m) n = begin
x + (m ℕ.+ n) × x ≈⟨ +-cong refl (×-homo-+ x m n) ⟩
x + (m ℕ.+ n) × x ≈⟨ +-congˡ (×-homo-+ x m n) ⟩
x + (m × x + n × x) ≈⟨ sym (+-assoc x (m × x) (n × x)) ⟩
x + m × x + n × x ∎

Expand All @@ -78,3 +79,16 @@ open import Algebra.Definitions.RawMonoid rawMonoid public
n × x + m × n × x ≈⟨ +-congˡ (×-assocˡ x m n) ⟩
n × x + (m ℕ.* n) × x ≈⟨ ×-homo-+ x n (m ℕ.* n) ⟨
(suc m ℕ.* n) × x ∎

-- _∧_ is homomorphic with respect to Bool._∧_.

∧-homo-true : ∀ x → true ∧ x ≈ x
∧-homo-true _ = refl

∧-assocˡ : ∀ b b′ x → b ∧ (b′ ∧ x) ≈ (b Bool.∧ b′) ∧ x
∧-assocˡ false _ _ = refl
∧-assocˡ true _ _ = refl

b∧x∙y≈b∧x+y : ∀ b x y → b ∧′ x ∙ y ≈ (b ∧ x) + y
b∧x∙y≈b∧x+y true _ _ = refl
b∧x∙y≈b∧x+y false _ y = sym (+-identityˡ y)