Skip to content

Commit 438f9ed

Browse files
authored
Improve Data.List.Base (fix agda#2359; deprecate use of with agda#2123) (agda#2365)
* refactor towards `if_then_else_` and away from `yes`/`no` * reverted chnages to `derun` proofs * undo last reversion! * layout
1 parent 3c49163 commit 438f9ed

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

src/Data/List/Base.agda

+26-20
Original file line numberDiff line numberDiff line change
@@ -364,45 +364,49 @@ removeAt (x ∷ xs) (suc i) = x ∷ removeAt xs i
364364

365365
takeWhile : {P : Pred A p} Decidable P List A List A
366366
takeWhile P? [] = []
367-
takeWhile P? (x ∷ xs) with does (P? x)
368-
... | true = x ∷ takeWhile P? xs
369-
... | false = []
367+
takeWhile P? (x ∷ xs) = if does (P? x)
368+
then x ∷ takeWhile P? xs
369+
else []
370370

371371
takeWhileᵇ : (A Bool) List A List A
372372
takeWhileᵇ p = takeWhile (T? ∘ p)
373373

374374
dropWhile : {P : Pred A p} Decidable P List A List A
375375
dropWhile P? [] = []
376-
dropWhile P? (x ∷ xs) with does (P? x)
377-
... | true = dropWhile P? xs
378-
... | false = x ∷ xs
376+
dropWhile P? (x ∷ xs) = if does (P? x)
377+
then dropWhile P? xs
378+
else x ∷ xs
379379

380380
dropWhileᵇ : (A Bool) List A List A
381381
dropWhileᵇ p = dropWhile (T? ∘ p)
382382

383383
filter : {P : Pred A p} Decidable P List A List A
384384
filter P? [] = []
385-
filter P? (x ∷ xs) with does (P? x)
386-
... | false = filter P? xs
387-
... | true = x ∷ filter P? xs
385+
filter P? (x ∷ xs) =
386+
let xs′ = filter P? xs in
387+
if does (P? x)
388+
then x ∷ xs′
389+
else xs′
388390

389391
filterᵇ : (A Bool) List A List A
390392
filterᵇ p = filter (T? ∘ p)
391393

392394
partition : {P : Pred A p} Decidable P List A (List A × List A)
393-
partition P? [] = ([] , [])
394-
partition P? (x ∷ xs) with does (P? x) | partition P? xs
395-
... | true | (ys , zs) = (x ∷ ys , zs)
396-
... | false | (ys , zs) = (ys , x ∷ zs)
395+
partition P? [] = [] , []
396+
partition P? (x ∷ xs) =
397+
let ys , zs = partition P? xs in
398+
if does (P? x)
399+
then (x ∷ ys , zs)
400+
else (ys , x ∷ zs)
397401

398402
partitionᵇ : (A Bool) List A List A × List A
399403
partitionᵇ p = partition (T? ∘ p)
400404

401405
span : {P : Pred A p} Decidable P List A (List A × List A)
402-
span P? [] = ([] , [])
403-
span P? ys@(x ∷ xs) with does (P? x)
404-
... | true = Product.map (x ∷_) id (span P? xs)
405-
... | false = ([] , ys)
406+
span P? [] = [] , []
407+
span P? ys@(x ∷ xs) = if does (P? x)
408+
then Product.map (x ∷_) id (span P? xs)
409+
else ([] , ys)
406410

407411

408412
spanᵇ : (A Bool) List A List A × List A
@@ -453,9 +457,11 @@ wordsByᵇ p = wordsBy (T? ∘ p)
453457
derun : {R : Rel A p} B.Decidable R List A List A
454458
derun R? [] = []
455459
derun R? (x ∷ []) = x ∷ []
456-
derun R? (x ∷ xs@(y ∷ _)) with does (R? x y) | derun R? xs
457-
... | true | ys = ys
458-
... | false | ys = x ∷ ys
460+
derun R? (x ∷ xs@(y ∷ _)) =
461+
let ys = derun R? xs in
462+
if does (R? x y)
463+
then ys
464+
else x ∷ ys
459465

460466
derunᵇ : (A A Bool) List A List A
461467
derunᵇ r = derun (T? ∘₂ r)

src/Data/List/Properties.agda

+15-15
Original file line numberDiff line numberDiff line change
@@ -1156,13 +1156,13 @@ module _ {P : Pred A p} (P? : Decidable P) where
11561156
filter-all : {xs} All P xs filter P? xs ≡ xs
11571157
filter-all {[]} [] = refl
11581158
filter-all {x ∷ xs} (px ∷ pxs) with P? x
1159-
... | no ¬px = contradiction px ¬px
1160-
... | true because _ = cong (x ∷_) (filter-all pxs)
1159+
... | false because [¬px] = contradiction px (invert [¬px])
1160+
... | true because _ = cong (x ∷_) (filter-all pxs)
11611161

11621162
filter-notAll : xs Any (∁ P) xs length (filter P? xs) < length xs
11631163
filter-notAll (x ∷ xs) (here ¬px) with P? x
1164-
... | false because _ = s≤s (length-filter xs)
1165-
... | yes px = contradiction px ¬px
1164+
... | false because _ = s≤s (length-filter xs)
1165+
... | true because [px] = contradiction (invert [px]) ¬px
11661166
filter-notAll (x ∷ xs) (there any) with ih filter-notAll xs any | does (P? x)
11671167
... | false = m≤n⇒m≤1+n ih
11681168
... | true = s≤s ih
@@ -1178,8 +1178,8 @@ module _ {P : Pred A p} (P? : Decidable P) where
11781178
filter-none : {xs} All (∁ P) xs filter P? xs ≡ []
11791179
filter-none {[]} [] = refl
11801180
filter-none {x ∷ xs} (¬px ∷ ¬pxs) with P? x
1181-
... | false because _ = filter-none ¬pxs
1182-
... | yes px = contradiction px ¬px
1181+
... | false because _ = filter-none ¬pxs
1182+
... | true because [px] = contradiction (invert [px]) ¬px
11831183

11841184
filter-complete : {xs} length (filter P? xs) ≡ length xs
11851185
filter P? xs ≡ xs
@@ -1190,13 +1190,13 @@ module _ {P : Pred A p} (P? : Decidable P) where
11901190

11911191
filter-accept : {x xs} P x filter P? (x ∷ xs) ≡ x ∷ (filter P? xs)
11921192
filter-accept {x} Px with P? x
1193-
... | true because _ = refl
1194-
... | no ¬Px = contradiction Px ¬Px
1193+
... | true because _ = refl
1194+
... | false because [¬Px] = contradiction Px (invert [¬Px])
11951195

11961196
filter-reject : {x xs} ¬ P x filter P? (x ∷ xs) ≡ filter P? xs
11971197
filter-reject {x} ¬Px with P? x
1198-
... | yes Px = contradiction Px ¬Px
1199-
... | false because _ = refl
1198+
... | true because [Px] = contradiction (invert [Px]) ¬Px
1199+
... | false because _ = refl
12001200

12011201
filter-idem : filter P? ∘ filter P? ≗ filter P?
12021202
filter-idem [] = refl
@@ -1234,13 +1234,13 @@ module _ {R : Rel A p} (R? : B.Decidable R) where
12341234

12351235
derun-reject : {x y} xs R x y derun R? (x ∷ y ∷ xs) ≡ derun R? (y ∷ xs)
12361236
derun-reject {x} {y} xs Rxy with R? x y
1237-
... | yes _ = refl
1238-
... | no ¬Rxy = contradiction Rxy ¬Rxy
1237+
... | true because _ = refl
1238+
... | false because [¬Rxy] = contradiction Rxy (invert [¬Rxy])
12391239

12401240
derun-accept : {x y} xs ¬ R x y derun R? (x ∷ y ∷ xs) ≡ x ∷ derun R? (y ∷ xs)
12411241
derun-accept {x} {y} xs ¬Rxy with R? x y
1242-
... | yes Rxy = contradiction Rxy ¬Rxy
1243-
... | no _ = refl
1242+
... | true because [Rxy] = contradiction (invert [Rxy]) ¬Rxy
1243+
... | false because _ = refl
12441244

12451245
------------------------------------------------------------------------
12461246
-- partition
@@ -1253,7 +1253,7 @@ module _ {P : Pred A p} (P? : Decidable P) where
12531253
... | true = cong (Product.map (x ∷_) id) ih
12541254
... | false = cong (Product.map id (x ∷_)) ih
12551255

1256-
length-partition : xs (let (ys , zs) = partition P? xs)
1256+
length-partition : xs (let ys , zs = partition P? xs)
12571257
length ys ≤ length xs × length zs ≤ length xs
12581258
length-partition [] = z≤n , z≤n
12591259
length-partition (x ∷ xs) with ih length-partition xs | does (P? x)

0 commit comments

Comments
 (0)