问题
I have the following code:
open import Data.Nat
open import Agda.Builtin.Char
open import Data.Maybe
digit' : ℕ → Maybe ℕ
digit' n with compare n (primCharToNat '9')
... | greater _ _ = nothing
... | _ = ?
digit : Char → Maybe ℕ
digit c = digit' (primCharToNat c)
Unfortunately, Agda "load file" command in emacs fails with the following message:
tmp.agda:7,1-8,12
I'm not sure if there should be a case for the constructor less,
because I get stuck when trying to solve the following unification
problems (inferred index ≟ expected index):
m ≟ n
suc (m + k) ≟ 57
when checking the definition of with-6
As far as I understand, Agda understands that primCharToNat '9'
is a constant and is not sure whether there are any preconditions on n
being always less than primCharToNat '9'
(57). Therefore, it is not sure whether it should generate the less
case (I assume it's also the case with equal
case). Can I somehow force Agda into generating all cases?
Background: I'm trying to write a digit : Char → Maybe ℕ
function which should return either just x
if the character passed is a decimal digit or nothing
if it's not. I think about the following algorithm: convert the argument to a natural number (ASCII code, probably) with primCharToNat
and then compare it with primCharToNat '0'
and primCharToNat '9'
.
回答1:
A possible solution consists in abstracting over primCharToNat '9'
:
digit' : ℕ → Maybe ℕ
digit' n with primCharToNat '9' | compare n (primCharToNat '9')
... | _ | greater _ _ = nothing
... | _ | _ = {!!}
来源:https://stackoverflow.com/questions/46708243/how-to-deal-with-agda-not-being-sure-in-whether-to-generate-constructor-case-in