6
votes

My code as follows:

calcstep ::Integral a => a -> a
calcstep  n = calcstep2 n 0

calcstep2 :: Integral (Integral a, Integral b) => a -> b -> a
calcstep2 1 k = k
calcstep2 n k | odd n = calcstep2 (n/2) (k+1)
              | otherwise = calcstep2 (n*3+1) (k+1)

The error is as follows:

Non type-variable argument

in the constraint: Integral (Integral a, Integral b)

(Use -XFlexibleContexts to permit this)

In the type signature for `calcstep2': calcstep2 :: Integral (Integral a, Integral b) => a -> b -> a

Failed, modules loaded: none.

What does it mean? how can I fix it?

2

2 Answers

16
votes

In ghci you can set FlexibleContexts like this:

:set -XFlexibleContexts

In the source file, at the beginning, you should use:

{-# LANGUAGE FlexibleContexts #-}

In the GHC manual you can find more about using language extensions.

In any case I think Chris' answer is closer to what you really want.

15
votes

The context Integral (Integral a, Integral b) is probably not what you intended. It is more likely that you want (Integral a, Integral b) as in

calcstep ::Integral a => a -> a
calcstep  n = calcstep2 n 0

calcstep2 :: (Integral a, Integral b) => a -> b -> a
calcstep2 1 k = k
calcstep2 n k | odd n     = calcstep2 (n `div` 2) (k+1)
              | otherwise = calcstep2 (n * 3 + 1) (k+1)