Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ New modules
* `Codata.Guarded.Stream.Relation.Unary.Linked` for a proof that each pair
of consecutive elements of a stream are related.

* `Data.Bool.ListAction.Properties` for properties of conjunction and
disjunction of lists.

* A new type of lists that grow on the right.
This is typically useful to model contexts of typing rules
or type accumulators that need to be reversed in the base case.
Expand Down
87 changes: 87 additions & 0 deletions src/Data/Bool/ListAction/Properties.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Booleans: properties of conjunction and disjunction of lists
------------------------------------------------------------------------

{-# OPTIONS --without-K --safe #-}

module Data.Bool.ListAction.Properties where

open import Data.Bool.Base
open import Data.Bool.Properties
open import Data.Bool.ListAction
open import Data.List.Base hiding (and; or; all; any)
open import Data.List.Membership.Propositional using (_∈_)
open import Data.List.Relation.Binary.Permutation.Propositional using (_↭_; ↭⇒↭ₛ)
import Data.List.Relation.Binary.Permutation.Propositional.Properties as ↭
open import Data.List.Relation.Binary.Permutation.Setoid.Properties
open import Data.List.Relation.Unary.Any using (here; there)
open import Function.Base using (_∘′_)
open import Relation.Binary.Core using (_Preserves_⟶_)
open import Relation.Binary.PropositionalEquality.Core
open import Relation.Binary.PropositionalEquality.Properties
using (module ≡-Reasoning)

------------------------------------------------------------------------
-- Properties

-- 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.

and-++ [] cs = refl
and-++ (b ∷ bs) cs = begin
b ∧ and (bs ++ cs) ≡⟨ cong (b ∧_) (and-++ bs cs) ⟩
b ∧ (and bs ∧ and cs) ≡⟨ ∧-assoc b (and bs) (and cs) ⟨
(b ∧ and bs) ∧ and cs ∎
where open ≡-Reasoning

∨-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))
Comment on lines +39 to +45

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!


and-↭ : and Preserves _↭_ ⟶ _≡_
and-↭ p = foldr-commMonoid ≡-setoid ∧-isCommutativeMonoid (↭⇒↭ₛ p)
Comment thread
jamesmckinna marked this conversation as resolved.

and-locate : ∀ bs → and bs ≡ false → false ∈ bs
and-locate (false ∷ bs) p = here refl
and-locate (true ∷ bs) p = there (and-locate bs p)

-- 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.

or-++ [] cs = refl
or-++ (b ∷ bs) cs = begin
b ∨ or (bs ++ cs) ≡⟨ cong (b ∨_) (or-++ bs cs) ⟩
b ∨ (or bs ∨ or cs) ≡⟨ ∨-assoc b (or bs) (or cs) ⟨
(b ∨ or bs) ∨ or cs ∎
where open ≡-Reasoning

∧-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))
Comment on lines +64 to +70

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?


or-↭ : or Preserves _↭_ ⟶ _≡_
or-↭ p = foldr-commMonoid ≡-setoid ∨-isCommutativeMonoid (↭⇒↭ₛ p)
Comment thread
jamesmckinna marked this conversation as resolved.

or-locate : ∀ bs → or bs ≡ true → true ∈ bs
or-locate (false ∷ bs) p = there (or-locate bs p)
or-locate (true ∷ bs) p = here p

-- all

all-↭ : ∀ {a} {A : Set a} (p : A → Bool) → all p Preserves _↭_ ⟶ _≡_
all-↭ p = and-↭ ∘′ ↭.map⁺ p

-- any

any-↭ : ∀ {a} {A : Set a} (p : A → Bool) → any p Preserves _↭_ ⟶ _≡_
any-↭ p = or-↭ ∘′ ↭.map⁺ p
Loading