[Haskell-cafe] Constants in Class definitions - could not deduce

martin martin.drautzburg at web.de
Sun May 18 14:54:37 UTC 2014


Hello all,

in my attempt to solve a Calendar related problem, I wanted to define Months and Weekdays such that I can increment a
value. Unlike the "succ" Funktion in Enum my "inc" function shall circle endlessly.

I came up with the following

class (Ix a, Enum a) => Incrementable a where
        from :: a
        to  :: a
        inc :: a -> a
        inc x = [from .. to] !! ((1 + index (from, to) x) `mod` rangeSize (from,to))

but GHC complains:

/home/martin/projects/haskell/euler/p19.hs:39:65:
    Could not deduce (Ix a0) arising from a use of `rangeSize'
    from the context (Incrementable a)

I suppose this is because GHC does not know that I want "to" and "from" to be taken from the same instance as x. If I
replace the signature of "from" by "a -> a" and implement it by using a function which throws aways its argument, the
types become clear and GHC no longer complains.

Alternatively I can omit the implementation of "inc" in the class and leave that to the instances. I can easily fix the
types in the instances, but this leads to some code duplication.

I tried (from::a) to fix the type, but this had no effect. How is this done correctly?


More information about the Haskell-Cafe mailing list