| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Natural numbers defined using the reflexive-transitive closure, Star |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Star.Nat where |
| 10 |
|
| 11 |
open import Data.Unit.Base using (tt) |
| 12 |
open import Function.Base using (const) |
| 13 |
open import Relation.Binary.Core using (Rel) |
| 14 |
open import Relation.Binary.Construct.Closure.ReflexiveTransitive |
| 15 |
using (Star; ε; _◅_; gmap; map; _◅◅_; _▻▻_; _⋆) |
| 16 |
open import Relation.Binary.Construct.Always using (Always) |
| 17 |
|
| 18 |
-- Natural numbers. |
| 19 |
|
| 20 |
ℕ : Set |
| 21 |
ℕ = Star Always tt tt |
| 22 |
|
| 23 |
-- Zero and successor. |
| 24 |
|
| 25 |
zero : ℕ |
| 26 |
zero = ε |
| 27 |
|
| 28 |
suc : ℕ → ℕ |
| 29 |
suc = _◅_ _ |
| 30 |
|
| 31 |
-- The length of a star-list. |
| 32 |
|
| 33 |
length : ∀ {i t} {I : Set i} {T : Rel I t} {i j} → Star T i j → ℕ |
| 34 |
length = gmap (const _) (const _) |
| 35 |
|
| 36 |
-- Arithmetic. |
| 37 |
|
| 38 |
infixl 7 _*_ |
| 39 |
infixl 6 _+_ _∸_ |
| 40 |
|
| 41 |
_+_ : ℕ → ℕ → ℕ |
| 42 |
_+_ = _◅◅_ |
| 43 |
|
| 44 |
_*_ : ℕ → ℕ → ℕ |
| 45 |
_*_ m = const m ⋆ |
| 46 |
|
| 47 |
_∸_ : ℕ → ℕ → ℕ |
| 48 |
m ∸ ε = m |
| 49 |
ε ∸ (_ ◅ n) = zero |
| 50 |
(_ ◅ m) ∸ (_ ◅ n) = m ∸ n |
| 51 |
|
| 52 |
-- Some constants. |
| 53 |
|
| 54 |
0# = zero |
| 55 |
1# = suc 0# |
| 56 |
2# = suc 1# |
| 57 |
3# = suc 2# |
| 58 |
4# = suc 3# |
| 59 |
5# = suc 4# |