Haskell没有出现(小数a0)的实例 [英] Haskell No instance for (Fractional a0) arising

查看:168
本文介绍了Haskell没有出现(小数a0)的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Haskell中有以下代码:

  powmod base 1 m = mod base m 
powmod base exp m |即使exp = mod(pow2 * pow2)m
|否则= mod(base * powmod base(exp-1)m)m
where
pow2 = powmod base(div exp 2)m

part1 jn 0 = 0
part1 jnk =(part1 jn(k-1))+(powmod 16(nk)r)/ r
其中
r = 8 * k + j

代码在ghci上没有任何问题。问题出现在我试图调用函数part1为:

  part1 4 10 4 

没有实例(分数a0)由于使用它'类型
变量
a0'是不明确的注意:有几个潜在的
实例:instance Integral a =>分数(GHC.Real.Ratio a)
- 定义在 GHC.Real'实例分数双 - 定义在 GHC.Float'实例分数浮点 - 定义于 GHC.Float'
print'的第一个参数中,即`it'在
交互式GHCi命令的列表中:print it

我不明白这个问题。任何帮助都会很棒。谢谢

解决方案

这里的问题是一个非常常见的问题 - 混入 / code>与整合操作(如 mod ) - 这导致两个约束:一个用于 Integral 和一个用于小数(/)) - 但是没有基本的数字类型,它们都是它们的一个实例 - 所以最终会遇到不可解析的约束条件,而GHC(i)不会找到匹配的实例并投诉。



如果您签名的话,其中大部分会变得很明显 - 是的,您不必如此,但正如您在这里看到的那样,无论如何这都是非常有价值的。



假设你想在 Integer 上工作,你可以得到:

  part1: :整数 - >整数 - >整数 - >整数
part1 jn 0 = 0
part1 jnk =(part1 jn(k-1))+(powmod 16(nk)r)`div` r
where
r = 8 * k + j

注意我替换了(/) div ? Haskell一旦包含预期类型就会指出这一点。



注意当然

  part1 :: Integral a => a  - > a  - > a  - > 

也可以工作


I have the following code in Haskell:

powmod base 1 m = mod base m
powmod base exp m | even exp  = mod (pow2 * pow2) m
                  | otherwise = mod (base * powmod base (exp - 1) m) m
     where
        pow2 = powmod base (div exp 2) m

part1 j n 0 = 0
part1 j n k = (part1 j n (k-1)) + (powmod 16 (n-k) r)/r
   where
     r = 8*k+j

The code is loaded without any problem on ghci. The problem comes when I try to call the function part1 as:

part1 4 10 4

I get:

No instance for (Fractional a0) arising from a use of it' The type variablea0' is ambiguous Note: there are several potential instances: instance Integral a => Fractional (GHC.Real.Ratio a) -- Defined in GHC.Real' instance Fractional Double -- Defined inGHC.Float' instance Fractional Float -- Defined in GHC.Float' In the first argument ofprint', namely `it' In a stmt of an interactive GHCi command: print it

I don't understand this problem. Any help would be great. Thanks

解决方案

Your problem here is a very common one - it roots into you mixing (/) with Integral operations like mod - this leads to two constraints: one for Integral and one for Fractional (the (/)) - but there is no basic number type which is an instance of both of them - so you end up with unsolvable constraints and GHC(i) will not find a matching instance and complain.

Most of this becomes obvious if you signatures - yes you don't have to but as you can see here it's valuable to do so anyway.

Assuming you want to work over Integer you get this:

part1 :: Integer -> Integer -> Integer -> Integer
part1 j n 0 = 0
part1 j n k = (part1 j n (k-1)) + (powmod 16 (n-k) r) `div` r
   where
     r = 8*k+j

note that I replaced (/) with div? Haskell points you to this as soon as you include expected types.

also note that of course

part1 :: Integral a => a -> a -> a -> a

will work as well

这篇关于Haskell没有出现(小数a0)的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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