1
绑定类型变量我发现很难形容我的问题,所以在这里不用什么:,只有发生在断言
我有一个函数的类型一堆断言。这些断言依赖于不用于函数的任何参数的类型变量,但仅用于内部绑定。每当我使用这个函数时,它都不会编译,因为当然,编译器没有任何信息可以猜出绑定我的类型变量的类型。下面是代码:
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances,
UndecidableInstances, FlexibleContexts, EmptyDataDecls, ScopedTypeVariables,
TypeOperators, TypeSynonymInstances #-}
class C a a' where convert :: a -> a'
class F a b where apply :: a -> b
class S s a where select :: s -> a
data CInt = CInt Int
instance S (Int,String) Int where select (i,_) = i
instance F Int CInt where apply = CInt
f :: forall s a b . (S s a, F a b) => s -> b
f s =
let v = select s :: a
y = apply v :: b
in y
x :: Int
x = f (10,"Pippo")
这里是产生错误:
FunctorsProblems.hs:21:4:
No instances for (F a Int, S (t, [Char]) a)
arising from a use of `f' at FunctorsProblems.hs:21:4-17
Possible fix:
add an instance declaration for (F a Int, S (t, [Char]) a)
In the expression: f (10, "Pippo")
In the definition of `x': x = f (10, "Pippo")
Failed, modules loaded: none.
Prelude>
是的,你说的是正确的。什么是问题?您是否在寻找对错误的解释或可能的解决方法? – 2010-03-19 16:41:20
我希望了解我如何帮助编译器理解计算的中间类型,但我不知道如何... 所以是的,这是我寻找的解决方法! – 2010-03-20 09:27:28