默认以下约束来键入' Double'使用科学时

时间:2014-10-28 15:23:20

标签: haskell

我有两个科学数字,这些数字必须是我想要转换为Ints的整数,如下所示。

请忽略代码约定和代码的惯用语。

> import qualified Data.Vector as V
> import Data.Scientific

> cToTy (Array v) = case V.toList v of
>     [String nm, Number p, Number s] 
>      | all (==True) $ map isInteger [p,s]  -- We make sure they're always integers
>      , [pr,sc] <- map (forceEitherToInt . floatingOrInteger) [p,s] -- And then hack them out

> forceEitherToInt :: (RealFloat r, Integral i) => Either r i -> Int
> forceEitherToInt (Left _a) = 0 -- This shouldn't happen, so we'll default it to 0 all the time.
> forceEitherToInt (Right a) = fromIntegral a

然而,我收到了这个警告,我无法弄清楚如何摆脱它们。

JsonUtils.lhs:76:26: Warning:
    Defaulting the following constraint(s) to type ‘Double’
      (RealFloat r0) arising from a use of ‘forceEitherToInt’
    In the first argument of ‘(.)’, namely ‘forceEitherToInt’
    In the first argument of ‘map’, namely
      ‘(forceEitherToInt . floatingOrInteger)’
    In a stmt of a pattern guard for
                   a case alternative:
      [pr, sc] <- map (forceEitherToInt . floatingOrInteger) [p, s]

有没有人有任何想法或想法?

1 个答案:

答案 0 :(得分:3)

首先,你可以明确地导致错误,如果&#34;都不会发生&#34 ;;第二,你可以放弃RealFloat作为@bheklilr状态;第三,如果你更喜欢双重,你可以更具体地指定双重;第四,如果你写下B&#39; A,它可以帮助匈牙利表示法。而不是&#39; B到A&#39;在你的名字。所以你可以写一下例如:

intFromEither :: (Integral i) => Either Double i -> Int
intFromEither (Left d) = error $ 
    "intFromEither tried to coerce `Left " ++ show d ++ "` to an Int."
intFromEither (Right i) = fromIntegral i

您可以将上面的Double替换为x,以使其更加通用;这里最常见的类型签名是(Integral a, Num b) => Either t a -> b