Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell : Use -XFlexibleContexts to permit this?

Tags:

haskell

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?

like image 866
user3239558 Avatar asked Jan 27 '14 07:01

user3239558


2 Answers

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.

like image 166
Danny Navarro Avatar answered Sep 23 '22 13:09

Danny Navarro


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)
like image 43
Chris Taylor Avatar answered Sep 21 '22 13:09

Chris Taylor