[Haskell-cafe] Why this doesn't type checked

Victor Nazarov asviraspossible at gmail.com
Mon Jun 28 00:51:45 EDT 2010


On Mon, Jun 28, 2010 at 12:33 AM, Daniel Fischer
<daniel.is.fischer at web.de> wrote:
> On Sunday 27 June 2010 21:52:18, Victor Nazarov wrote:
>> I've allways found code like
>>
>> > -- maxBound (undefined :: Int)
>>
>> a bit strange
>
> Well, that would indeed be a bit strange since
>
> maxBound :: (Bounded a) => a
>
> and function types aren't instances of Bounded, so it'd be
>
> maxBound :: Int
> maxBound :: Char
> maxBound :: Bool
> ...
>

Yes, you are right. I've just tried to find some standard type-indexed function.
Discard everything about maxBound.

>> as any usage of undefined is.
>> Being Ruby on Rails developer
>> I've personally found that one of the main
>> Rails motos is being as readable as possible.
>
> That's good.
>
>> Code must be as close to english as possible.
>
> That not, not always, anyway. Mathematical algorithms for example tend to
> be obfuscated by englishifying.

I understand it, but RoR is trying to get as close to english as
possible as I see it.

>
>> Embeded DSLs like rspec are made mostly to
>> made specs as close to english as possible.
>>
>
>
>> What we get with this instances is following code.
>>
>> > main =
>> >   do print (sizeof :: Sizeof Word16)
>>
>> Let's try it.
>>
>> $ runhaskell this.lhs
>> this.lhs:78:14:
>>     Couldn't match expected type `Int'
>>            against inferred type `Sizeof sizeable'
>>       NB: `Sizeof' is a type function, and may not be injective
>>     In the first argument of `print', namely
>>         `(sizeof :: Sizeof Word16)'
>>     In the expression: print (sizeof :: Sizeof Word16)
>>     In the expression: do { print (sizeof :: Sizeof Word16) }
>
> Right. Since Sizeof Word8 is Int too, the type can't help determining the
> value.
>

Then it should be ambiguous type-parameter error or something like
this, why Int is expected?

>>
>> What can I do with this code to make it type-check?
>
> newtype Size a = Size { unSize :: Int }
>
> class Sizeable a where
>    sizeof :: Size a
>
> instance Sizeable Word8 where
>    sizeof = Size 1
>
> instance Sizeable Word16 where
>    sizeof = Size 2
>
> ...
>
> main = print . unSize $ sizeof :: Size Word16
>
>

Year this is good enough for me, thanx :)

-- 
Victor Nazarov


More information about the Haskell-Cafe mailing list