| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Documentation for the `Any` predicate over `List` |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
module README.Data.List.Relation.Unary.Any where |
| 8 |
|
| 9 |
open import Data.List.Base using ([]; _∷_) |
| 10 |
open import Data.Nat.Base using (ℕ; _+_; _<_; s≤s; z≤n; _*_; _∸_; _≤_) |
| 11 |
open import Data.Nat.Properties using (≤-trans; n≤1+n) |
| 12 |
|
| 13 |
------------------------------------------------------------------------ |
| 14 |
-- Any |
| 15 |
|
| 16 |
-- The predicate `Any` encodes the idea of at least one element of a |
| 17 |
-- given list satisfying a given property (or more formally a |
| 18 |
-- predicate, see the `Pred` type in `Relation.Unary`). |
| 19 |
|
| 20 |
open import Data.List.Relation.Unary.Any as Any |
| 21 |
|
| 22 |
-- A proof of type Any consists of a sequence of the "there" |
| 23 |
-- constructors, which says that the element lies in the remainder of |
| 24 |
-- the list, followed by a single "here" constructor which indicates |
| 25 |
-- that the head of the list satisfies the predicate and takes a proof |
| 26 |
-- that it does so. |
| 27 |
|
| 28 |
-- For example a proof that a given list of natural numbers contains |
| 29 |
-- at least one number greater than or equal to 4 can be written as |
| 30 |
-- follows: |
| 31 |
|
| 32 |
lem₁ : Any (4 ≤_) (3 ∷ 5 ∷ 1 ∷ 6 ∷ []) |
| 33 |
lem₁ = there (here 4≤5) |
| 34 |
where |
| 35 |
4≤5 = s≤s (s≤s (s≤s (s≤s z≤n))) |
| 36 |
|
| 37 |
-- Note that nothing requires that the proof of `Any` points at the |
| 38 |
-- first such element in the list. There is therefore an alternative |
| 39 |
-- proof for the above lemma which points to 6 instead of 5. |
| 40 |
|
| 41 |
lem₂ : Any (4 ≤_) (3 ∷ 5 ∷ 1 ∷ 6 ∷ []) |
| 42 |
lem₂ = there (there (there (here 4≤6))) |
| 43 |
where |
| 44 |
4≤6 = s≤s (s≤s (s≤s (s≤s z≤n))) |
| 45 |
|
| 46 |
-- There also exist various operations over proofs of `Any` whose names |
| 47 |
-- shadow the corresponding list operation. The standard way of using |
| 48 |
-- these is to use `as` to name the module: |
| 49 |
|
| 50 |
import Data.List.Relation.Unary.Any as Any |
| 51 |
|
| 52 |
-- and then use the qualified name `Any.map`. For example, map can |
| 53 |
-- be used to change the predicate of `Any`: |
| 54 |
|
| 55 |
lem₃ : Any (3 ≤_) (3 ∷ 5 ∷ 1 ∷ 6 ∷ []) |
| 56 |
lem₃ = Any.map 4≤x⇒3≤x lem₂ |
| 57 |
where |
| 58 |
4≤x⇒3≤x : ∀ {x} → 4 ≤ x → 3 ≤ x |
| 59 |
4≤x⇒3≤x = ≤-trans (n≤1+n 3) |
| 60 |
|
| 61 |
-- Properties of how list functions interact with `Any` can be |
| 62 |
-- found in: |
| 63 |
|
| 64 |
import Data.List.Relation.Unary.Any.Properties |