我刚开始玩 Idris,并尝试在其中插入一些 Haskell 机器:
namespace works
data Auto a b = AutoC (a -> (b, Auto a b))
const_auto : b -> Auto a b
const_auto b = AutoC (\_ => (b, const_auto b))
但是,我现在想概括Auto a b
为AutoM m a b
采用一个额外的参数,以便它可以单子生成其输出,并且m
是单子。我的直觉是那m
会有 type Type -> Type
,但随后类型检查器抱怨那不匹配(Type, Type) -> Type
。所以我试着让它多态一点:
namespace doesntwork
data AutoM : {x : Type } -> (m : x -> Type) -> (a : Type) -> (b : Type) -> Type where
AutoMC : (a -> m (b, AutoM m a b)) -> AutoM m a b
data Identity a = IdentityC a
Auto : Type -> Type -> Type
Auto = AutoM Identity
这至少是类型检查。但是当我尝试时:
const_auto : Monad m => {m : x -> Type } -> {a : Type} -> b -> AutoM m a b
const_auto b = AutoMC (\_ => return (b, const_auto b))
然而,这并不好:
When elaborating right hand side of Stream.doesntwork.const_auto:
When elaborating an application of function Prelude.Monad.return:
Can't unify
(A, B) (Type of (a, b))
with
(b, AutoM m4 a b) (Expected type)
而且我无法理解类型错误。为什么在世界上会(a, b)
提到类型,而a
在定义的任何地方都没有使用const_auto
?我觉得自己的定义AutoM
已经有问题了,但我真的不知道为什么或如何。