使用递归Type-> Type函数定义类型

时间:2020-01-09 16:11:54

标签: idris

在Idris中,通常将List定义如下:

data List a = Nil | (::) a (List a)

但是我想尝试使用递归List函数来定义没有data的{​​{1}}:

Type -> Type

这可以编译,但是在List' : Type -> Type List' a = Either () (a, List' a) 情况下我无法匹配。例如,如果我定义

Either

编译器说:

isEmpty : List' a -> Bool
isEmpty (Left _) = True
isEmpty (Right _) = False

因此类型检查器似乎没有将When checking left hand side of isEmpty: When checking an application of Main.isEmpty: Type mismatch between Either a1 b (Type of Left l) and List' a (Expected type) 扩展到其List' a定义中。似乎类型定义中的递归调用会触发类型检查器-如果我改为像这样定义Either _ _

List'

然后将List' a = Either () (a, ()) 定义编译正确。 (当然,它实际上不再是列表了。)

我想知道的是:

  • 为什么这完全不起作用?有没有办法检查类型检查器中发生的事情?
  • 有没有办法使这种类型定义起作用?还是我们必须使用data

0 个答案:

没有答案
相关问题