2

On one hand, I can use #_ to construct Fins from literals:

open import Data.Fin

data I'mFinnish : Set where
  Mk : Fin 5 → I'mFinnish

foo : I'mFinnish
foo = Mk (# 3)

On the other hand, I can use literals to pattern match on naturals:

open import Data.Nat

data I'mANatural : Set where
  Mk : ℕ → I'mANatural

open import Data.Bool
bar : I'mANatural → Bool
bar (Mk 3) = true
bar _      = false

My question is, can I, on the gripping hand, use literals when matching on Fins? I.e., what can I do to approximate the following, invalid as-is, Agda code:

open import Data.Bool
bar′ : I'mFinnish → Bool
bar′ (Mk 3) = true
bar′ _      = false
Cactus
  • 27,075
  • 9
  • 69
  • 149

1 Answers1

3

You're looking for literal overloading. Quoting documentation, the Agda.Builtin.FromNat module provides a type class

record Number {a} (A : Set a) : Set (lsuc a) where
  field
    Constraint : Nat → Set a
    fromNat : ∀ n → {{_ : Constraint n}} → A

open Number {{...}} public using (fromNat)

{-# BUILTIN FROMNAT fromNat #-}

which allows to overload natural literals. An instance for Fin can be found at the former link too. And you can directly define an instance for I'mFinnish.

effectfully
  • 12,325
  • 2
  • 17
  • 40
  • That looks promising, but I couldn't get it to work: https://gist.github.com/gergoerdi/ec95524e608aa0daebcceaf724fe20b4 still fails on the `Mk 3` pattern with "`suc` is not a constructor of the datatype `Fin` when checking that the pattern `suc (suc (suc zero))` has type `Fin (fromNat 5)`". – Cactus Nov 15 '17 at 08:39
  • 2
    @Cactus, I guess, this gist belongs to the Agda issue tracker. – effectfully Nov 15 '17 at 10:53