Debian Codegraph @

doc/README/Data/List/Relation/Ternary/Interleaving.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Examples showing how the notion of Interleaving can be used
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module README.Data.List.Relation.Ternary.Interleaving where
10
11 open import Level
12 open import Data.List.Base hiding (filter)
13 open import Data.List.Relation.Unary.All
14 open import Function
15 open import Relation.Nullary
16 open import Relation.Unary
17
18 ------------------------------------------------------------------------
19 -- Interleaving
20
21 -- In its most general form, `Interleaving` is parametrised by two
22 -- relations `L` (for Left) and `R` (for Right). Given three lists,
23 -- `xs`, `ys` and `zs`, a proof of `Interleaving xs ys zs` is
24 -- essentially a diagram explaining how `zs` can be pulled apart into
25 -- `xs` and `ys` in a way compatible with `L` and `R`. For instance:
26
27 -- xs zs ys
28 --
29 -- x₁ -- L x₁ z₁ -- z₁
30 -- x₂ -- L x₂ z₂ -- z₂
31 -- z₃ -- R z₃ z₁ -- y₁
32 -- x₃ -- L x₃ z₄ -- z₄
33 -- z₅ -- R z₅ y₂ -- y₂
34
35 open import Data.List.Relation.Ternary.Interleaving.Propositional
36
37 -- The special case we will focus on here is the propositional case: both
38 -- `L` and ̀R` are propositional equality. Rethinking our previous example,
39 -- this gives us the proof that [z₁, ⋯, z₅] can be partitioned into
40 -- [z₁, z₂, z₄] on the one hand and [z₃, z₅] in the other.
41
42 -- One possible use case for such a relation is the definition of a very
43 -- precise filter function. Provided a decidable predicate `P`, it will
44 -- prove not only that the retained values satisfy `P` but that the ones
45 -- that didn't make the cut satisfy the negation of P.
46
47 -- We can make this formal by defining the following record type:
48
49 infix 3 _≡_⊎_
50 record Filter {a p} {A : Set a} (P : Pred A p) (xs : List A) : Set (a ⊔ p) where
51 constructor _≡_⊎_
52 field
53 -- The result of running filter is two lists:
54 -- * the elements we have kept
55 -- * and the ones we have thrown away
56 -- We leave these implicit: they can be inferred from the rest
57 {kept} : List A
58 {thrown} : List A
59 -- There is a way for us to recover the original
60 -- input by interleaving the two lists
61 cover : Interleaving kept thrown xs
62 -- Finally, the partition was made according to the predicate
63 allP : All P kept
64 all¬P : All (∁ P) thrown
65
66 -- Once we have this type written down, we can write the function.
67 -- We use an anonymous module to clean up the function's type.
68
69 module _ {a p} {A : Set a} {P : Pred A p} (P? : Decidable P) where
70
71 filter : ∀ xs → Filter P xs
72 -- If the list is empty, we are done.
73 filter [] = [] ≡ [] ⊎ []
74 filter (x ∷ xs) =
75 -- otherwise we start by running filter on the tail
76 let xs′ ≡ ps ⊎ ¬ps = filter xs in
77 -- And depending on whether `P` holds of the head,
78 -- we cons it to the `kept` or `thrown` list.
79 case P? x of λ where -- [1]
80 (yes p) → consˡ xs′ ≡ p ∷ ps ⊎ ¬ps
81 (no ¬p) → consʳ xs′ ≡ ps ⊎ ¬p ∷ ¬ps
82
83
84
85 -- [1] See the following module for explanations of `case_of_` and
86 -- pattern-matching lambdas
87 import README.Case