| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An example showing how to define a function taking an optional |
| 5 |
-- argument that default to a specified value if none is passed. |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
module README.Data.Default where |
| 11 |
|
| 12 |
open import Data.Default |
| 13 |
open import Data.Nat.Base hiding (_!) |
| 14 |
open import Relation.Binary.PropositionalEquality |
| 15 |
|
| 16 |
-- An argument of type `WithDefault {a} {A} x` is an argument of type |
| 17 |
-- `A` that happens to default to `x` if no other value is specified |
| 18 |
|
| 19 |
-- Note that you will only get this behaviour if the `default` instance |
| 20 |
-- is in scope so you should either import `Data.Default` in your client |
| 21 |
-- modules or publicly re-export the type and the instance! |
| 22 |
|
| 23 |
-- `inc` increments its argument by the value `step`, defaulting to 1 |
| 24 |
inc : {{step : WithDefault 1}} → ℕ → ℕ |
| 25 |
inc {{step}} n = step .value + n |
| 26 |
|
| 27 |
-- and indeed incrementing 2 gives you 3 |
| 28 |
_ : inc 2 ≡ 3 |
| 29 |
_ = refl |
| 30 |
|
| 31 |
-- but you can also insist that you want to use a bigger increment by |
| 32 |
-- passing the `step` argument explicitly |
| 33 |
_ : inc {{10 !}} 2 ≡ 12 |
| 34 |
_ = refl |