haskell - 为什么 Haskell 允许这种递归 (?) 类型?

标签 haskell types type-inference

我看到了以下定义:

getCC :: t -> ContT r m (t, t -> ContT r m b)
getCC x0 = callCC (\c -> let f x = c (x, f) in return (x0, f))

但是如果有人隐藏了 getCC 的类型声明,我将无法弄清楚。

f 的类型是什么?

嗯,f 是一个接受 t 并返回 Cont r m (t, *something*) 的函数。但是某物必须与f具有相同的类型!

所以f::Cont r m (t, Cont r m (t, Cont r m (t, Cont r m (t, ...)))

ghc 为什么/如何推断出 f::t -> Cont r m b)

最佳答案

类型中没有递归:

{-# LANGUAGE ScopedTypeVariables #-}

import Control.Monad.Trans.Cont

getCC :: t -> ContT r m (t, t -> ContT r m b)
getCC x0 = callCC (\(c :: (t, t -> ContT r m b) -> ContT r m b) 
                   -> let f :: t -> ContT r m b
                          f x = c (x, f)
                      in return (x0, f))

关于haskell - 为什么 Haskell 允许这种递归 (?) 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157871/

相关文章:

Haskell Riak 驱动程序 : Making a simple 'put' operation

mysql - MySQL 中货币的最佳数据类型是什么?

c++ - 有没有办法让函数返回一个类型名?

f# - 类型推断创建了不需要的约束

F# 多态类型

parsing - 如何在 Haskell 中打印 Functor 和 Applicative 的中间结果

haskell - 为什么 Alternative 类型类需要是 Control.Applicative 的子类

haskell + "Remove a parameter"

python : function type parameter

Haskell:读取并输入签名