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

Define strictify function at each base type, plus evident property #2459

Draft
wants to merge 2 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
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ New modules
Additions to existing modules
-----------------------------

* New operation in `Data.Bool.Base`:
```agda
strictify : Bool → Bool
```

* New lemma in `Data.Bool.Properties`:
```agda
strictify≗id : strictify b ≡ b
```

* New operation in `Data.Fin.Base`:
```agda
strictify : Fin n → Fin n
```

* New lemma in `Data.Fin.Properties`:
```agda
strictify≗id : strictify i ≡ i
```

* In `Data.List.Relation.Unary.All`:
```agda
search : Decidable P → ∀ xs → All (∁ P) xs ⊎ Any P xs
Expand All @@ -65,10 +85,17 @@ Additions to existing modules
++⁺ˡ : Reflexive R → ∀ zs → (_++ zs) Preserves (Pointwise R) ⟶ (Pointwise R)
```

* New operation in `Data.Nat.Base`:
```agda
strictify : ℕ → ℕ
```

* New lemmas in `Data.Nat.Properties`: adjunction between `suc` and `pred`
```agda
suc[m]≤n⇒m≤pred[n] : suc m ≤ n → m ≤ pred n
m≤pred[n]⇒suc[m]≤n : .{{NonZero n}} → m ≤ pred n → suc m ≤ n

strictify≗id : strictify n ≡ n
```

* New lemma in `Data.Vec.Properties`:
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Bool/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ infix 0 if_then_else_
if_then_else_ : Bool → A → A → A
if true then t else f = t
if false then t else f = f

strictify : Bool → Bool
strictify b = if b then true else false
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh my. This is the code that I consider amongst the most evil! Can't it at least be written

strictify true = true
strictify false = false

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree that, in novice programmers, that one-line expression is truly evil.

But it nevertheless is precisely equivalent to your definition (up to reduction/strictness behaviour), for the (additional) cost of a (generatively distinct) function definition.

Cf. defining _++_ directly, or as an instance of foldr?

Copy link
Contributor

Choose a reason for hiding this comment

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

I sure hope those definitions are precisely equivalent! strictify is weird, I don't really mind if it has a cost.

I personally think the computational _++_ should be defined in terms of foldr.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, as with my comments below, I believe on my understanding of Agda's evaluation strategy that they are reduction-behaviour equivalent, in that they are each

  • blocked, neutral terms, if the Bool scrutinee is unevaluated/neutral
  • the relevant literal value, depending on the value of the scrutinee

I've wondered if, as in a separate issue/PR, we considered systematically adding the dependent/non-dependent eliminators for Data.X, the 'evil' arises because of our habit of not seeing if_then_else as 'merely' the non-dependent eliminator for Bool? (The separate evil of novice programmers writing if b then true else false in a strict language is another matter...)


7 changes: 7 additions & 0 deletions src/Data/Bool/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ true <? _ = no (λ())
{ isStrictTotalOrder = <-isStrictTotalOrder
}

------------------------------------------------------------------------
-- Properties of strictify

strictify≗id : ∀ b → strictify b ≡ b
strictify≗id true = refl
strictify≗id false = refl

------------------------------------------------------------------------
-- Properties of _∨_

Expand Down
7 changes: 7 additions & 0 deletions src/Data/Fin/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ pinch {suc n} _ zero = zero
pinch {suc n} zero (suc j) = j
pinch {suc n} (suc i) (suc j) = suc (pinch i j)

------------------------------------------------------------------------
-- Other operations

strictify : Fin n → Fin n
strictify zero = zero
strictify (suc i) = suc (strictify i)

------------------------------------------------------------------------
-- Order relations

Expand Down
8 changes: 8 additions & 0 deletions src/Data/Fin/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ zero = refl
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (suc k) =
cong suc (cast-trans (ℕ.suc-injective eq₁) (ℕ.suc-injective eq₂) k)

------------------------------------------------------------------------
-- Properties of strictify
------------------------------------------------------------------------

strictify≗id : (i : Fin n) → strictify i ≡ i
strictify≗id zero = refl
strictify≗id (suc i) = cong suc (strictify≗id i)

------------------------------------------------------------------------
-- Properties of _≤_
------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions src/Data/Nat/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ _! : ℕ → ℕ
zero ! = 1
suc n ! = suc n * n !

------------------------------------------------------------------------
-- Other operations

strictify : ℕ → ℕ
strictify zero = zero
strictify (suc n) = suc (strictify n)

------------------------------------------------------------------------
-- Extensionally equivalent alternative definitions of _≤_/_<_ etc.

Expand Down
8 changes: 8 additions & 0 deletions src/Data/Nat/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,14 @@ module ≤-Reasoning where

open ≤-Reasoning

------------------------------------------------------------------------
-- Properties of strictify
------------------------------------------------------------------------

strictify≗id : ∀ n → strictify n ≡ n
strictify≗id zero = refl
strictify≗id (suc n) = cong suc (strictify≗id n)

------------------------------------------------------------------------
-- Properties of _+_
------------------------------------------------------------------------
Expand Down