Skip to content

Add WithDefault construct #1450

Closed
Closed
@gallais

Description

@gallais

It's folklore that we can encode default implicit values in a DTT with instance
arguments that may be explicitly applied. We should add this construct to the
stdlib (I just have something else to do right now so I'm posting this so that
I don't forget about it).

Comments on the name of the constructor / field are welcome.

module Default where

record WithDefault (A : Set) (a : A) : Set where
  constructor give
  field value : A
open WithDefault

instance
  default :  {A a}  WithDefault A a
  default {a = a} = give a

open import Agda.Builtin.Nat

inc : Nat  {{step : WithDefault Nat 1}}  Nat
inc m {{step}} = m + step .value

open import Agda.Builtin.Equality

_ : inc 23
_ = refl

_ : inc 2 {{step = give 3}} ≡ 5
_ = refl

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions