Idris 的“BorrowedType”背后的意图是什么?

What's the intention behind Idris' `BorrowedType`?

在 idris 中,有一个名为 UniqueType 的宇宙,类型的值只能在其中使用一次。据我所知,它可以用来编写高性能代码。 但是一个值只能使用一次的事实通常是太有限了,所以有一种方法可以借用一个值而不是使用它:

data Borrowed : UniqueType -> BorrowedType where ...

Borrowed 数据类型在 Idris 中定义如上。为什么它不简单 return Type 而是引入另一个类型域 (BorrowedType)?

As the documentation explains,对 BorrowedType 类型的 Borrowed 值有限制:

Unlike a unique value, a borrowed value may be referred to as many times as desired. However, there is a restriction on how a borrowed value can be used. After all, much like a library book or your neighbour’s lawnmower, if a function borrows a value it is expected to return it in exactly the condition in which it was received!

The restriction is that when a Borrowed type is matched, any pattern variables under the Read which have a unique type may not be referred to at all on the right hand side (unless they are themselves lent to another function).

此限制(以及 lend 的宽大处理)是通过类型检查器中的特殊类型规则实现的。这些规则需要适用于某些东西,这就是为什么 BorrowedType 必须是不同于常规 Type 的另一种类型(对于它们没有特殊的 lend/Read 键入规则).