是否在某些定义的单一功能的情况下是不可能的?
以下进口/语言编译指示需要是否在某些定义的单一功能的情况下是不可能的?
{-# language FlexibleContexts, MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances, CPP #-} import Data.String.Conversions
以下两个功能typechecks细(*种):
(++<>) :: ( ConvertibleStrings a Text,
ConvertibleStrings b Text
)
=> a -> b -> Text
a ++<> b = cs a <> cs b
csappend :: (
ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c)
=> a -> b -> c
csappend a b = cs (a ++<> b)
我试图界定上述两种功能作为单个功能:
singleFunction :: ( ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c,
Monoid Text) => a -> b -> c
singleFunction a b = cs (cs a <> cs b)
*
xyz :: String xyz = do
let s = "test" :: String
csappend (csappend s s :: String) s :: String -- this typechecks
csappend (csappend s s) s :: String -- this does not typechecks
这给了我一个错误(如下图)。我不明白的错误的原因 - 类型的限制似乎是相同的,我不知道是否可能别的东西是错误的原因是什么?
• Could not deduce (ConvertibleStrings a0 c) arising from a use of ‘cs’
from the context: (ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c)
bound by the type signature for:
singleFunction :: (ConvertibleStrings a Text,
ConvertibleStrings b Text, ConvertibleStrings Text c) =>
a -> b -> c
at /home/chris/Projects/Haskell/Hart/Hart.hs:(37,1)-(40,43)
The type variable ‘a0’ is ambiguous
Relevant bindings include
singleFunction :: a -> b -> c
(bound at /home/chris/Projects/Haskell/Hart/Hart.hs:41:1)
These potential instances exist:
instance [safe] ConvertibleStrings StrictText StrictText
-- Defined in ‘Data.String.Conversions’
instance [safe] ConvertibleStrings StrictText String
-- Defined in ‘Data.String.Conversions’
instance [safe] ConvertibleStrings String StrictText
-- Defined in ‘Data.String.Conversions’
...plus one other
...plus 21 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: cs (cs a <> cs b)
In an equation for ‘singleFunction’:
singleFunction a b = cs (cs a <> cs b)
/home/chris/Projects/Haskell/Hart/Hart.hs:41:26: error:
• Could not deduce (ConvertibleStrings a a0)
arising from a use of ‘cs’
from the context: (ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c)
bound by the type signature for:
singleFunction :: (ConvertibleStrings a Text,
ConvertibleStrings b Text, ConvertibleStrings Text c) =>
a -> b -> c
at /home/chris/Projects/Haskell/Hart/Hart.hs:(37,1)-(40,43)
The type variable ‘a0’ is ambiguous
Relevant bindings include
a :: a (bound at /home/chris/Projects/Haskell/Hart/Hart.hs:41:16)
singleFunction :: a -> b -> c
(bound at /home/chris/Projects/Haskell/Hart/Hart.hs:41:1)
These potential instances exist:
instance [safe] ConvertibleStrings StrictText StrictText
-- Defined in ‘Data.String.Conversions’
instance [safe] ConvertibleStrings StrictText String
-- Defined in ‘Data.String.Conversions’
instance [safe] ConvertibleStrings String StrictText
-- Defined in ‘Data.String.Conversions’
...plus one other
...plus 21 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘(<>)’, namely ‘cs a’
In the first argument of ‘cs’, namely ‘(cs a <> cs b)’
In the expression: cs (cs a <> cs b)
/home/chris/Projects/Haskell/Hart/Hart.hs:41:26: error:
• Could not deduce (Monoid a0) arising from a use of ‘<>’
from the context: (ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c)
bound by the type signature for:
singleFunction :: (ConvertibleStrings a Text,
ConvertibleStrings b Text, ConvertibleStrings Text c) =>
a -> b -> c
at /home/chris/Projects/Haskell/Hart/Hart.hs:(37,1)-(40,43)
The type variable ‘a0’ is ambiguous
These potential instances exist:
instance Monoid a => Monoid (IO a) -- Defined in ‘GHC.Base’
instance Monoid Ordering -- Defined in ‘GHC.Base’
instance Monoid Text -- Defined in ‘Data.Text’
...plus 8 others
...plus 19 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘cs’, namely ‘(cs a <> cs b)’
In the expression: cs (cs a <> cs b)
In an equation for ‘singleFunction’:
singleFunction a b = cs (cs a <> cs b)
/home/chris/Projects/Haskell/Hart/Hart.hs:41:34: error:
• Could not deduce (ConvertibleStrings b a0)
arising from a use of ‘cs’
from the context: (ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c)
bound by the type signature for:
singleFunction :: (ConvertibleStrings a Text,
ConvertibleStrings b Text, ConvertibleStrings Text c) =>
a -> b -> c
at /home/chris/Projects/Haskell/Hart/Hart.hs:(37,1)-(40,43)
The type variable ‘a0’ is ambiguous
Relevant bindings include
b :: b (bound at /home/chris/Projects/Haskell/Hart/Hart.hs:41:18)
singleFunction :: a -> b -> c
(bound at /home/chris/Projects/Haskell/Hart/Hart.hs:41:1)
These potential instances exist:
instance [safe] ConvertibleStrings StrictText StrictText
-- Defined in ‘Data.String.Conversions’
instance [safe] ConvertibleStrings StrictText String
-- Defined in ‘Data.String.Conversions’
instance [safe] ConvertibleStrings String StrictText
-- Defined in ‘Data.String.Conversions’
...plus one other
...plus 21 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the second argument of ‘(<>)’, namely ‘cs b’
In the first argument of ‘cs’, namely ‘(cs a <> cs b)’
In the expression: cs (cs a <> cs b)
回答:
此定义不起作用,因为编译器无法推导出中间类型。 你答应编译a
和b
可以转换为Text
,但是当你调用调用的地方定义cs a
结果类型,只有在现场调用编译派生类型之后检查约束。在这里,你马上就多态结果再打电话cs
和GHC不知道你意味着什么类型的。
singleFunction :: ( ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c,
Monoid Text) => a -> b -> c
singleFunction a b = cs (cs a <> cs b)
的情况下,有两个功能的作品,因为++<>
explicityly在返回类型Text
。
所以,如果你想使用Text
作为中间幺你应该更明确
singleFunction :: ( ConvertibleStrings a Text,
ConvertibleStrings b Text,
ConvertibleStrings Text c) => a -> b -> c
singleFunction a b = cs ((cs a <> cs b) :: Text)
以上是 是否在某些定义的单一功能的情况下是不可能的? 的全部内容, 来源链接: utcz.com/qa/263971.html