Debian Codegraph @

src/Tactic/RingSolver/Core/NatSet.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Simple implementation of sets of ℕ.
5 --
6 -- Since ℕ is represented as unary numbers, simply having an ordered
7 -- list of numbers to represent a set is quite inefficient. For
8 -- instance, to see if 6 is in the set {1, 3, 4}, we have to do a
9 -- comparison with 1, then 3, and then 4. But 4 is equal to suc 3, so
10 -- we should be able to share the work accross those two comparisons.
11 --
12 -- This module defines a type that represents {1, 3, 4} as:
13 --
14 -- 1 ∷ 1 ∷ 0 ∷ []
15 --
16 -- i.e. we only store the gaps. When checking if a number (the needle)
17 -- is in the set (the haystack), we subtract each successive member of
18 -- the haystack from the needle as we go. For example, to check if 6 is
19 -- in the above set, we do the following:
20 --
21 -- start: 6 ∈? (1 ∷ 1 ∷ 0 ∷ [])
22 -- test head: 6 ≟ 1
23 -- not equal, so continue: (6 - 1 - 1) ∈? (1 ∷ 0 ∷ [])
24 -- compute: 4 ∈? (1 ∷ 0 ∷ [])
25 -- test head: 4 ≟ 1
26 -- not equal, so continue: (4 - 1 - 1) ∈? (0 ∷ [])
27 -- compute: 2 ∈? (0 ∷ [])
28 -- test head: 2 ≟ 0
29 -- not equal, so continue: (2 - 1 - 1) ∈? []
30 -- empty list: false
31 --
32 -- In this way, we change the membership test from O(n²) to O(n).
33 ------------------------------------------------------------------------
34
35 {-# OPTIONS --cubical-compatible --safe #-}
36
37 module Tactic.RingSolver.Core.NatSet where
38
39 open import Data.Nat.Base as ℕ using (ℕ; suc; zero)
40 open import Data.List.Base as List using (List; _∷_; [])
41 open import Data.List.Scans.Base as Scans using (scanl)
42 open import Data.Maybe.Base as Maybe using (Maybe; just; nothing)
43 open import Data.Bool.Base as Bool using (Bool)
44 open import Function.Base using (const; _∘_)
45 open import Relation.Binary.PropositionalEquality.Core
46 using (_≡_; refl)
47
48 ------------------------------------------------------------------------
49 -- Helper methods
50
51 para : ∀ {a b} {A : Set a} {B : Set b} →
52 (A → List A → B → B) → B → List A → B
53 para f b [] = b
54 para f b (x ∷ xs) = f x xs (para f b xs)
55
56 ------------------------------------------------------------------------
57 -- Definition
58
59 NatSet : Set
60 NatSet = List ℕ
61
62 ------------------------------------------------------------------------
63 -- Functions
64
65 insert : ℕ → NatSet → NatSet
66 insert x xs = para f (_∷ []) xs x
67 where
68 f : ℕ → NatSet → (ℕ → NatSet) → ℕ → NatSet
69 f y ys c x with ℕ.compare x y
70 ... | ℕ.less x k = x ∷ k ∷ ys
71 ... | ℕ.equal x = x ∷ ys
72 ... | ℕ.greater y k = y ∷ c k
73
74 delete : ℕ → NatSet → NatSet
75 delete x xs = para f (const []) xs x
76 where
77 f : ℕ → NatSet → (ℕ → NatSet) → ℕ → NatSet
78 f y ys c x with ℕ.compare x y
79 f y ys c x | ℕ.less x k = y ∷ ys
80 f y [] c x | ℕ.equal x = []
81 f y₁ (y₂ ∷ ys) c x | ℕ.equal x = suc x ℕ.+ y₂ ∷ ys
82 f y ys c x | ℕ.greater y k = y ∷ c k
83
84 -- Returns the position of the element, if it's present.
85 lookup : NatSet → ℕ → Maybe ℕ
86 lookup xs x = List.foldr f (const (const nothing)) xs x 0
87 where
88 f : ℕ → (ℕ → ℕ → Maybe ℕ) → ℕ → ℕ → Maybe ℕ
89 f y ys x i with ℕ.compare x y
90 ... | ℕ.less x k = nothing
91 ... | ℕ.equal y = just i
92 ... | ℕ.greater y k = ys k (suc i)
93
94
95 member : ℕ → NatSet → Bool
96 member x xs = Maybe.is-just (lookup xs x)
97
98 fromList : List ℕ → NatSet
99 fromList = List.foldr insert []
100
101 toList : NatSet → List ℕ
102 toList = List.drop 1 ∘ List.map ℕ.pred ∘ Scans.scanl (λ x y → suc (y ℕ.+ x)) 0
103
104 ------------------------------------------------------------------------
105 -- Tests
106
107 private
108 example₁ : fromList (4 ∷ 3 ∷ 1 ∷ 0 ∷ 2 ∷ []) ≡ (0 ∷ 0 ∷ 0 ∷ 0 ∷ 0 ∷ [])
109 example₁ = refl
110
111 example₂ : lookup (fromList (4 ∷ 3 ∷ 1 ∷ 0 ∷ 2 ∷ [])) 3 ≡ just 3
112 example₂ = refl
113
114 example₃ : toList (fromList (4 ∷ 3 ∷ 1 ∷ 0 ∷ 2 ∷ [])) ≡ (0 ∷ 1 ∷ 2 ∷ 3 ∷ 4 ∷ [])
115 example₃ = refl
116
117 example₄ : delete 3 (fromList (4 ∷ 3 ∷ 1 ∷ 2 ∷ [])) ≡ fromList (4 ∷ 1 ∷ 2 ∷ [])
118 example₄ = refl
119
120 example₅ : delete 3 (fromList (4 ∷ 1 ∷ 2 ∷ [])) ≡ fromList (4 ∷ 1 ∷ 2 ∷ [])
121 example₅ = refl