Skip to content

Commit 5ac8015

Browse files
authored
Data.List.Fresh.NonEmpty (#1624)
1 parent 9315159 commit 5ac8015

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

CHANGELOG.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,14 @@ Deprecated names
342342
sym-↔ ↦ ↔-sym
343343
```
344344

345-
* In `Data.Fin.Base`:
346-
two new, hopefully more memorable, names `↑ˡ` `↑ʳ` for the 'left', resp. 'right' injection of a Fin m into a 'larger' type, `Fin (m + n)`, resp. `Fin (n + m)`, with argument order to reflect the position of the Fin m argument.
345+
* In `Data.Fin.Base`:
346+
two new, hopefully more memorable, names `↑ˡ` `↑ʳ` for the 'left', resp. 'right' injection of a Fin m into a 'larger' type, `Fin (m + n)`, resp. `Fin (n + m)`, with argument order to reflect the position of the Fin m argument.
347347
```
348348
inject+ ↦ flip _↑ˡ_
349349
raise ↦ _↑ʳ_
350350
```
351351

352-
* In `Data.Fin.Properties`:
352+
* In `Data.Fin.Properties`:
353353
```
354354
toℕ-raise ↦ toℕ-↑ʳ
355355
toℕ-inject+ n i ↦ sym (toℕ-↑ˡ i n)
@@ -358,11 +358,11 @@ two new, hopefully more memorable, names `↑ˡ` `↑ʳ` for the 'left', resp. '
358358
Fin0↔⊥ ↦ 0↔⊥
359359
```
360360

361-
* In `Data.Vec.Properties`:
361+
* In `Data.Vec.Properties`:
362362
```
363363
[]≔-++-inject+ ↦ []≔-++-↑ˡ
364364
```
365-
Additionally, `[]≔-++-↑ʳ`, by analogy.
365+
Additionally, `[]≔-++-↑ʳ`, by analogy.
366366

367367
* In `Foreign.Haskell.Either` and `Foreign.Haskell.Pair`:
368368
```
@@ -393,6 +393,11 @@ New modules
393393
Data.Default
394394
```
395395

396+
* A small library for a non-empty fresh list:
397+
```
398+
Data.List.Fresh.NonEmpty
399+
```
400+
396401
* Show module for unnormalised rationals:
397402
```
398403
Data.Rational.Unnormalised.Show

src/Data/List/Fresh/NonEmpty.agda

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- A non-empty fresh list
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.List.Fresh.NonEmpty where
10+
11+
open import Level using (Level; _⊔_)
12+
open import Data.List.Fresh as List# using (List#; []; cons; fresh)
13+
open import Data.Maybe.Base using (Maybe; nothing; just)
14+
open import Data.Nat using (ℕ; suc)
15+
open import Data.Product using (_×_; _,_)
16+
open import Relation.Binary as B using (Rel)
17+
18+
private
19+
variable
20+
a r : Level
21+
A : Set a
22+
R : Rel A r
23+
24+
------------------------------------------------------------------------
25+
-- Definition
26+
27+
infixr 5 _∷#⁺_
28+
29+
record List#⁺ (A : Set a) (R : Rel A r) : Set (a ⊔ r) where
30+
constructor _∷#⁺_
31+
field
32+
head : A
33+
tail : List# A R
34+
{rel} : fresh A R head tail
35+
36+
open List#⁺
37+
38+
------------------------------------------------------------------------
39+
-- Operations
40+
41+
uncons : List#⁺ A R A × List# A R
42+
uncons (x ∷#⁺ xs) = x , xs
43+
44+
[_] : A List#⁺ A R
45+
[ x ] = x ∷#⁺ []
46+
47+
length : List#⁺ A R
48+
length (x ∷#⁺ xs) = suc (List#.length xs)
49+
50+
-- Conversion
51+
52+
toList# : List#⁺ A R List# A R
53+
toList# l = cons (l .head) (l .tail) (l .rel)
54+
55+
fromList# : List# A R Maybe (List#⁺ A R)
56+
fromList# [] = nothing
57+
fromList# (cons a as ps) = just (record { head = a ; tail = as ; rel = ps })

0 commit comments

Comments
 (0)