Debian Codegraph @

src/Data/List/Fresh/NonEmpty.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- A non-empty fresh list
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Fresh.NonEmpty where
10
11 open import Data.List.Fresh as List# using (List#; []; cons; fresh)
12 open import Data.Maybe.Base using (Maybe; nothing; just)
13 open import Data.Nat.Base using (ℕ; suc)
14 open import Data.Product.Base using (_×_; _,_)
15 open import Level using (Level; _⊔_)
16 open import Relation.Binary.Core 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 })