martin | 18 May 16:54 2014
Picon

Constants in Class definitions - could not deduce

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.

(Continue reading)

Claude Heiland-Allen | 18 May 17:14 2014
Picon

Re: Constants in Class definitions - could not deduce

Hi Martin,

On 18/05/14 15:54, martin wrote:
> 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
(Continue reading)

Erik Hesselink | 18 May 19:45 2014
Picon

Re: Constants in Class definitions - could not deduce

On Sun, May 18, 2014 at 5:14 PM, Claude Heiland-Allen
<claude <at> mathr.co.uk> wrote:
> Hi Martin,
>
>
> On 18/05/14 15:54, martin wrote:
>>
>> 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
(Continue reading)

martin | 19 May 00:32 2014
Picon

Re: Constants in Class definitions - could not deduce

Thanks, typeOf did the trick

Am 05/18/2014 05:14 PM, schrieb Claude Heiland-Allen:
> Hi Martin,
> 
> On 18/05/14 15:54, martin wrote:
>> 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
(Continue reading)


Gmane