我如何接受仅受类型类限制的参数 [英] How do I accept parameters restricted only by type class

查看:122
本文介绍了我如何接受仅受类型类限制的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下。我想参数给我的功能,只受到类型的限制。我在它们上调用类型类的函数,然后我可以使用它们。但我收到各种错误,因为我试图这样做。

  { - #LANGUAGE RankNTypes,TypeSynonymInstances,FlexibleInstances, UndecidableInstances# - } 

class PlotValue a where
value :: a - > Double
instance PlotValue Double其中
value = id
--instance PlotValue Int其中
--value x = fromIntegral x
instance(Integral a)=> PlotValue a其中
值x = fromIntegral x
实例PlotValue String其中
值x = 5

类型输入=(PlotValue a,PlotValue b)=> (也许a,也许b)

test ::输入 - > String
test(Just a,Just b)=(show $ value a)++(show $ value b)

main = do
putStrLn(show(test strl,只是6.4)))

目前的错误尝试):

  Test5.hs:17:5:
无法匹配预期的类型`Input'类型`(a,b)'
在模式中:(只是a,只是b)
在`test'的定义中:
test(Just a,Just b)=(show $ value a)++(show $ value b)

Test5.hs:20:30:
无法匹配预期的类型'a'与推断的类型'[Char]'
`a'是一个刚性类型变量,由
绑定,多态类型
`forall a b。 (PlotValue a,PlotValue b)=> (也许是a,也许是b)'
在Test5.hs:20:19
'Just'的第一个参数,即``strl''
在表达式中:Juststrl
在`test'的第一个参数中,即`(Juststrl,Just 6.4)'

Test5.hs:20:43:
无法推断出小数b)上下文中的
(PlotValue a,PlotValue b)在Test5.hs:20:43-45
中由字面值'6.4'产生的
可能的修正:
add (小数b)到
的背景下,多态类型
`forall a b。 (PlotValue a,PlotValue b)=> (也许是a,也许是b)'
'Just'的第一个参数,即'6.4'
在表达式中:只需6.4
在`test'的第一个参数中,即` (Juststrl,Just 6.4)'


解决方案

一些小事情。主要是,正如斯蒂芬所指出的那样,在一个类型同义词下隐藏一个自由类型变量通常是非常愚蠢的。

#LANGUAGE RankNTypes,TypeSynonymInstances,FlexibleInstances,OverlappingInstances,UndecidableInstances# - }

class PlotValue a where
value :: a - > Double
实例PlotValue Double其中
value = id
实例(Integral a)=> PlotValue a其中
值x = fromIntegral x
实例PlotValue String其中
值x = 5

test ::(PlotValue a,PlotValue b)=> (也许a,也许b) - > String
test(Just a,Just b)=(show $ value a)++(show $ value b)

main = do
putStrLn(show(test strl,Just(6.4 :: Double))))


Code is below. I'd like to have parameters to my function that are only restricted by type class. I call a function of the type class on them and then I can use them. But I'm getting various errors as I'm trying to do this.

{-# LANGUAGE RankNTypes, TypeSynonymInstances, FlexibleInstances, UndecidableInstances #-}

class PlotValue a where
  value :: a -> Double
instance PlotValue Double where
  value = id
--instance PlotValue Int where
  --value x = fromIntegral x
instance (Integral a) => PlotValue a where
  value x = fromIntegral x
instance PlotValue String where
  value x = 5

type Input = (PlotValue a, PlotValue b) => (Maybe a, Maybe b)

test :: Input -> String
test (Just a, Just b) = (show $ value a) ++ (show $ value b)

main = do
  putStrLn (show ( test (Just "strl", Just 6.4)))

Current errors (though they change a little depending on what I try):

Test5.hs:17:5:
    Couldn't match expected type `Input' against inferred type `(a, b)'
    In the pattern: (Just a, Just b)
    In the definition of `test':
        test (Just a, Just b) = (show $ value a) ++ (show $ value b)

Test5.hs:20:30:
    Couldn't match expected type `a' against inferred type `[Char]'
      `a' is a rigid type variable bound by
          the polymorphic type
            `forall a b. (PlotValue a, PlotValue b) => (Maybe a, Maybe b)'
            at Test5.hs:20:19
    In the first argument of `Just', namely `"strl"'
    In the expression: Just "strl"
    In the first argument of `test', namely `(Just "strl", Just 6.4)'

Test5.hs:20:43:
    Could not deduce (Fractional b)
      from the context (PlotValue a, PlotValue b)
      arising from the literal `6.4' at Test5.hs:20:43-45
    Possible fix:
      add (Fractional b) to the context of
        the polymorphic type
          `forall a b. (PlotValue a, PlotValue b) => (Maybe a, Maybe b)'
    In the first argument of `Just', namely `6.4'
    In the expression: Just 6.4
    In the first argument of `test', namely `(Just "strl", Just 6.4)'

解决方案

Fixed a number of small things. Mainly, as stephen pointed out, hiding a free type variable under a type synonym is generally sort of silly and bad.

{-# LANGUAGE RankNTypes, TypeSynonymInstances, FlexibleInstances, OverlappingInstances, UndecidableInstances #-}

class PlotValue a where
  value :: a -> Double
instance PlotValue Double where
  value = id
instance (Integral a) => PlotValue a where
  value x = fromIntegral x
instance PlotValue String where
  value x = 5

test :: (PlotValue a, PlotValue b) => (Maybe a, Maybe b) -> String
test (Just a, Just b) = (show $ value a) ++ (show $ value b)

main = do
  putStrLn (show ( test (Just "strl", Just (6.4::Double))))

这篇关于我如何接受仅受类型类限制的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆