我们说我有一个证明:
z=int(input())
a=0
y=[]
for loop in range(z):
a=int(input())
y.append(a)
print(y.index(min(y))+1)
如果我在我的代码中的其他地方使用它,我将不得不像-- (pseudocode) m and n are natural numbers
proof :: forall m n. (1 <= m, m < n) => SNat n -> SNat m -> (1 <= n - 1)
proof _ _ = unsafeCoerce Refl
那样模式匹配:
proof
但是...
case proof n m of
Refl -> ...
...
适用于任何proof
和n
,所以总是匹配的不必要的模式匹配是在运行时完成的。怎么避免这个?
编辑用例:
m
如果假设使用-- 0 1 2 3 4 ... n-1
--returns Vec n a : 1 0 0 0 0 ... 0
helper :: (UVec a, Num a) => SNat n -> Vec n a
helper n = case isZero n of
NonZero -> 1 `vcons` vpreplicate (sPred n) 0
Zero -> vnil
proof1 :: forall n m . (CmpNat n m ~ GT) => SNat n -> SNat m -> ((CmpNat (n :- 1) (m :- 1)) :~: GT)
proof1 n m = unsafeCoerce Refl
proof2 :: forall n m . (1 <= m, (CmpNat n m) ~ GT) => SNat n -> SNat m -> ((1 <=? (n :- 1)) :~: True)
proof2 n m = unsafeCoerce Refl
-- 0 1 2 ... m . . . . . . n-1
--returns Vec n a : 0 0 0 ... 1 0 0 0 0 ... 0
helper2 :: forall a m n.(UVec a, Num a, 1 <= n, (CmpNat n m) ~ GT) => SNat n -> SNat m -> Vec n a
helper2 size m = case (isZero m, proof1 size m) of
(NonZero, Refl) ->
case (proof2 size m) of
Refl -> (0 :: a) `vcons` helper2 (sPred size) (sPred m)
(Zero,_) -> helper size
,则额外的模式匹配会增加开销。