我正在尝试通过自然数计算奇偶校验和一半的下限:
data IsEven : Nat -> Nat -> Type where
Times2 : (n : Nat) -> IsEven (n + n) n
data IsOdd : Nat -> Nat -> Type where
Times2Plus1 : (n : Nat) -> IsOdd (S (n + n)) n
parity : (n : Nat) -> Either (Exists (IsEven n)) (Exists (IsOdd n))
我尝试了明显的实现parity
:
parity Z = Left $ Evidence _ $ Times2 0
parity (S Z) = Right $ Evidence _ $ Times2Plus1 0
parity (S (S n)) with (parity n)
parity (S (S (k + k))) | Left (Evidence _ (Times2 k)) =
Left $ rewrite plusSuccRightSucc k k in Evidence _ $ Times2 (S k)
parity (S (S (S ((k + k))))) | Right (Evidence _ (Times2Plus1 k)) =
Right $ rewrite plusSuccRightSucc k k in Evidence _ $ Times2Plus1 (S k)
这种类型检查并按预期工作。但是,如果我尝试标记parity
为total
,伊德里斯开始抱怨:
parity is possibly not total due to: with block in parity
with
我看到的唯一块parity
是从parity (S (S n))
to递归调用的块parity n
,但显然这是有根据的,因为n
在结构上小于S (S n)
.
我如何说服 Idrisparity
是完全的?