| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Basic auxiliary definitions for monoid-like structures |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Algebra.Bundles using (RawMonoid) |
| 10 |
|
| 11 |
module Algebra.Definitions.RawMonoid {a ℓ} (M : RawMonoid a ℓ) where |
| 12 |
|
| 13 |
open import Data.Nat.Base as ℕ using (ℕ; zero; suc) |
| 14 |
open import Data.Vec.Functional as Vector using (Vector) |
| 15 |
open RawMonoid M renaming ( _∙_ to _+_ ; ε to 0# ) |
| 16 |
|
| 17 |
------------------------------------------------------------------------ |
| 18 |
-- Re-export definitions over a magma |
| 19 |
------------------------------------------------------------------------ |
| 20 |
|
| 21 |
open import Algebra.Definitions.RawMagma rawMagma public |
| 22 |
|
| 23 |
------------------------------------------------------------------------ |
| 24 |
-- Multiplication by natural number |
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- Standard definition |
| 27 |
|
| 28 |
-- A simple definition, easy to use and prove properties about. |
| 29 |
|
| 30 |
infixr 8 _×_ |
| 31 |
|
| 32 |
_×_ : ℕ → Carrier → Carrier |
| 33 |
0 × x = 0# |
| 34 |
suc n × x = x + (n × x) |
| 35 |
|
| 36 |
------------------------------------------------------------------------ |
| 37 |
-- Type-checking optimised definition |
| 38 |
|
| 39 |
-- For use in code where high performance at type-checking time is |
| 40 |
-- important, e.g. solvers and tactics. Firstly it avoids unnecessarily |
| 41 |
-- multiplying by the unit if possible, speeding up type-checking and |
| 42 |
-- makes for much more readable proofs: |
| 43 |
-- |
| 44 |
-- Standard definition: x * 2 = x + x + 0# |
| 45 |
-- Optimised definition: x * 2 = x + x |
| 46 |
-- |
| 47 |
-- Secondly, associates to the left which, counterintuitive as it may |
| 48 |
-- seem, also speeds up typechecking. |
| 49 |
-- |
| 50 |
-- Standard definition: x * 3 = x + (x + (x + 0#)) |
| 51 |
-- Our definition: x * 3 = (x + x) + x |
| 52 |
|
| 53 |
infixl 8 _×′_ |
| 54 |
|
| 55 |
_×′_ : ℕ → Carrier → Carrier |
| 56 |
0 ×′ x = 0# |
| 57 |
1 ×′ x = x |
| 58 |
suc n ×′ x = n ×′ x + x |
| 59 |
|
| 60 |
{-# INLINE _×′_ #-} |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- Summation |
| 64 |
------------------------------------------------------------------------ |
| 65 |
|
| 66 |
sum : ∀ {n} → Vector Carrier n → Carrier |
| 67 |
sum = Vector.foldr _+_ 0# |