我有以下使用SnocList
的功能import Data.List.Views
takeLast : (n : Nat) -> List a -> List a
takeLast n xs with (snocList xs)
takeLast Z [] | Empty = []
takeLast Z (ys ++ [x]) | (Snoc rec) = []
takeLast (S k) [] | Empty = []
takeLast (S k) (ys ++ [x]) | (Snoc rec) = takeLast k ys ++ [x] | rec
它需要给定List中的最后n个元素。我希望它等同于以下函数。
takeLast' : (n : Nat) -> (xs : List a) -> List a
takeLast' n xs = reverse $ take n $ reverse xs
当我尝试以下示例证明
时takeLastsAreEquivalent : takeLast' 2 [1, 2, 3] = takeLast 2 [1, 2, 3]
takeLastsAreEquivalent = Refl
我从类型检查器中收到以下错误。
When checking right hand side of takeLastsAreEquivalent with expected type
takeLast' 2 [1, 2, 3] = takeLast 2 [1, 2, 3]
Type mismatch between
with block in Main.takeLast Integer 2 [1, 2, 3] (snocList [1, 2, 3]) =
with block in Main.takeLast Integer 2 [1, 2, 3] (snocList [1, 2, 3]) (Type of Refl)
and
[2, 3] = with block in Main.takeLast Integer 2 [1, 2, 3] (snocList [1, 2, 3]) (Expected type)
Specifically:
Type mismatch between
with block in Main.takeLast Integer 2 [1, 2, 3] (snocList [1, 2, 3])
and
[2, 3]
Holes: Main.takeLastsAreEquivalent
当我在REPL中运行它们时,一切似乎都很好。
*/Example> takeLast' 2 [1, 2, 3]
[2, 3] : List Integer
*/Example> :t takeLast' 2 [1, 2, 3]
takeLast' 2 [1, 2, 3] : List Integer
*/Example> takeLast 2 [1, 2, 3]
[2, 3] : List Integer
*/Example> :t takeLast 2 [1, 2, 3]
takeLast 2 [1, 2, 3] : List Integer
我知道这不是一个真实的证明,但我只想说明这些是如何工作的,我正在研究一个例子。