Nic*_*off 8 polymorphism haskell type-families
所以我有一个函数apply :: proxy tf -> tf Int -> tf Int
,它接受一个旨在携带一个类型族的代理,并将Int应用于该类型族以确定第二个参数的类型和返回值.但是,我收到GHC的一些令人困惑的回应.
{-# LANGUAGE TypeFamilies #-}
import Data.Proxy
type family F (a :: *) :: * where
F Int = ()
f :: Proxy F
f = Proxy
apply :: proxy tf -> tf Int -> tf Int
apply _ x = x
-- Doesn't typecheck.
test1 :: ()
test1 = apply f ()
-- Typechecks fine
test2 :: ()
test2 = let g = apply f in g ()
Run Code Online (Sandbox Code Playgroud)
test1
拒绝编译GHC吐出这个错误:
tftest.hs:16:9:
Couldn't match expected type ‘()’ with actual type ‘F Int’
In the expression: apply f ()
In an equation for ‘test1’: test1 = apply f ()
tftest.hs:16:17:
Couldn't match expected type ‘F Int’ with actual type ‘()’
In the second argument of ‘apply’, namely ‘()’
In the expression: apply f ()
Run Code Online (Sandbox Code Playgroud)
令人困惑的是,评论test1
和使用let绑定test2
使得GHC很开心,一切都编译得很好.谁能解释一下这里发生了什么?
Rei*_*ton 12
所以我有一个函数
apply :: proxy tf -> tf Int -> tf Int
,它需要一个代理服务器来承载一个类型系列
你不能这样做.必须始终完全应用类型族,就像它们是泛化的类型同义词一样.类型变量永远不能实例化为欠饱和类型族.
这是GHC 7.8.3中的一个错误,它从一开始就没有拒绝你的程序
f :: Proxy F
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
231 次 |
最近记录: |