Debian Codegraph @

src/Data/List/Fresh.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Fresh lists, a proof relevant variant of Catarina Coquand's contexts
5 -- in "A Formalised Proof of the Soundness and Completeness of a Simply
6 -- Typed Lambda-Calculus with Explicit Substitutions"
7 ------------------------------------------------------------------------
8
9 -- See README.Data.List.Fresh and README.Data.Trie.NonDependent for
10 -- examples of how to use fresh lists.
11
12 {-# OPTIONS --cubical-compatible --safe #-}
13
14 module Data.List.Fresh where
15
16 open import Level using (Level; _⊔_)
17 open import Data.Bool.Base using (true; false; if_then_else_)
18 open import Data.Unit.Polymorphic.Base using (⊤)
19 open import Data.Product.Base using (∃; _×_; _,_; -,_; proj₁; proj₂)
20 open import Data.List.Relation.Unary.All using (All; []; _∷_)
21 open import Data.List.Relation.Unary.AllPairs using (AllPairs; []; _∷_)
22 open import Data.Maybe.Base as Maybe using (Maybe; just; nothing)
23 open import Data.Nat.Base using (ℕ; zero; suc)
24 open import Function.Base using (_∘′_; flip; id; _on_)
25 open import Relation.Nullary using (does)
26 open import Relation.Unary as U using (Pred)
27 open import Relation.Binary.Core using (Rel)
28 import Relation.Binary.Definitions as B using (Reflexive)
29 open import Relation.Nary using (_⇒_; ∀[_])
30
31
32 private
33 variable
34 a b p r s : Level
35 A : Set a
36 B : Set b
37
38 ------------------------------------------------------------------------
39 -- Basic type
40
41 -- If we pick an R such that (R a b) means that a is different from b
42 -- then we have a list of distinct values.
43
44 module _ {a} (A : Set a) (R : Rel A r) where
45
46 data List# : Set (a ⊔ r)
47 fresh : (a : A) (as : List#) → Set r
48
49 data List# where
50 [] : List#
51 cons : (a : A) (as : List#) → fresh a as → List#
52
53 -- Whenever R can be reconstructed by η-expansion (e.g. because it is
54 -- the erasure ⌊_⌋ of a decidable predicate, cf. Relation.Nary) or we
55 -- do not care about the proof, it is convenient to get back list syntax.
56
57 -- We use a different symbol to avoid conflict when importing both
58 -- Data.List and Data.List.Fresh.
59 infixr 5 _∷#_
60 pattern _∷#_ x xs = cons x xs _
61
62 fresh a [] = ⊤
63 fresh a (x ∷# xs) = R a x × fresh a xs
64
65 -- Convenient notation for freshness making A and R implicit parameters
66 infix 5 _#_
67 _#_ : {R : Rel A r} (a : A) (as : List# A R) → Set r
68 _#_ = fresh _ _
69
70 ------------------------------------------------------------------------
71 -- Operations for modifying fresh lists
72
73 module _ {R : Rel A r} {S : Rel B s} (f : A → B) (R⇒S : ∀[ R ⇒ (S on f) ]) where
74
75 map : List# A R → List# B S
76 map-# : ∀ {a} as → a # as → f a # map as
77
78 map [] = []
79 map (cons a as ps) = cons (f a) (map as) (map-# as ps)
80
81 map-# [] _ = _
82 map-# (a ∷# as) (p , ps) = R⇒S p , map-# as ps
83
84 module _ {R : Rel B r} (f : A → B) where
85
86 map₁ : List# A (R on f) → List# B R
87 map₁ = map f id
88
89 module _ {R : Rel A r} {S : Rel A s} (R⇒S : ∀[ R ⇒ S ]) where
90
91 map₂ : List# A R → List# A S
92 map₂ = map id R⇒S
93
94 ------------------------------------------------------------------------
95 -- Views
96
97 data Empty {A : Set a} {R : Rel A r} : List# A R → Set (a ⊔ r) where
98 [] : Empty []
99
100 data NonEmpty {A : Set a} {R : Rel A r} : List# A R → Set (a ⊔ r) where
101 cons : ∀ x xs pr → NonEmpty (cons x xs pr)
102
103 ------------------------------------------------------------------------
104 -- Operations for reducing fresh lists
105
106 length : {R : Rel A r} → List# A R → ℕ
107 length [] = 0
108 length (_ ∷# xs) = suc (length xs)
109
110 ------------------------------------------------------------------------
111 -- Operations for constructing fresh lists
112
113 pattern [_] a = a ∷# []
114
115 fromMaybe : {R : Rel A r} → Maybe A → List# A R
116 fromMaybe nothing = []
117 fromMaybe (just a) = [ a ]
118
119 module _ {R : Rel A r} (R-refl : B.Reflexive R) where
120
121 replicate : ℕ → A → List# A R
122 replicate-# : (n : ℕ) (a : A) → a # replicate n a
123
124 replicate zero a = []
125 replicate (suc n) a = cons a (replicate n a) (replicate-# n a)
126
127 replicate-# zero a = _
128 replicate-# (suc n) a = R-refl , replicate-# n a
129
130 ------------------------------------------------------------------------
131 -- Operations for deconstructing fresh lists
132
133 uncons : {R : Rel A r} → List# A R → Maybe (A × List# A R)
134 uncons [] = nothing
135 uncons (a ∷# as) = just (a , as)
136
137 head : {R : Rel A r} → List# A R → Maybe A
138 head = Maybe.map proj₁ ∘′ uncons
139
140 tail : {R : Rel A r} → List# A R → Maybe (List# A R)
141 tail = Maybe.map proj₂ ∘′ uncons
142
143 take : {R : Rel A r} → ℕ → List# A R → List# A R
144 take-# : {R : Rel A r} → ∀ n a (as : List# A R) → a # as → a # take n as
145
146 take zero xs = []
147 take (suc n) [] = []
148 take (suc n) (cons a as ps) = cons a (take n as) (take-# n a as ps)
149
150 take-# zero a xs _ = _
151 take-# (suc n) a [] ps = _
152 take-# (suc n) a (x ∷# xs) (p , ps) = p , take-# n a xs ps
153
154 drop : {R : Rel A r} → ℕ → List# A R → List# A R
155 drop zero as = as
156 drop (suc n) [] = []
157 drop (suc n) (a ∷# as) = drop n as
158
159 module _ {P : Pred A p} (P? : U.Decidable P) where
160
161 takeWhile : {R : Rel A r} → List# A R → List# A R
162 takeWhile-# : ∀ {R : Rel A r} a (as : List# A R) → a # as → a # takeWhile as
163
164 takeWhile [] = []
165 takeWhile (cons a as ps) =
166 if does (P? a) then cons a (takeWhile as) (takeWhile-# a as ps) else []
167
168 -- this 'with' is needed to cause reduction in the type of 'takeWhile (a ∷# as)'
169 takeWhile-# a [] _ = _
170 takeWhile-# a (x ∷# xs) (p , ps) with does (P? x)
171 ... | true = p , takeWhile-# a xs ps
172 ... | false = _
173
174 dropWhile : {R : Rel A r} → List# A R → List# A R
175 dropWhile [] = []
176 dropWhile aas@(a ∷# as) = if does (P? a) then dropWhile as else aas
177
178 filter : {R : Rel A r} → List# A R → List# A R
179 filter-# : ∀ {R : Rel A r} a (as : List# A R) → a # as → a # filter as
180
181 filter [] = []
182 filter (cons a as ps) =
183 let l = filter as in
184 if does (P? a) then cons a l (filter-# a as ps) else l
185
186 -- this 'with' is needed to cause reduction in the type of 'filter-# a (x ∷# xs)'
187 filter-# a [] _ = _
188 filter-# a (x ∷# xs) (p , ps) with does (P? x)
189 ... | true = p , filter-# a xs ps
190 ... | false = filter-# a xs ps
191
192 ------------------------------------------------------------------------
193 -- Relationship to List and AllPairs
194
195 toList : {R : Rel A r} → List# A R → ∃ (AllPairs R)
196 toAll : ∀ {R : Rel A r} {a} as → fresh A R a as → All (R a) (proj₁ (toList as))
197
198 toList [] = -, []
199 toList (cons x xs ps) = -, toAll xs ps ∷ proj₂ (toList xs)
200
201 toAll [] ps = []
202 toAll (a ∷# as) (p , ps) = p ∷ toAll as ps
203
204 fromList : ∀ {R : Rel A r} {xs} → AllPairs R xs → List# A R
205 fromList-# : ∀ {R : Rel A r} {x xs} (ps : AllPairs R xs) →
206 All (R x) xs → x # fromList ps
207
208 fromList [] = []
209 fromList (r ∷ rs) = cons _ (fromList rs) (fromList-# rs r)
210
211 fromList-# [] _ = _
212 fromList-# (p ∷ ps) (r ∷ rs) = r , fromList-# ps rs