[Haskell-cafe] Problem trying to get class Bounded to work

Cale Gibbard cgibbard at gmail.com
Mon May 22 19:26:24 EDT 2006


Hello,

There's a prelude function called asTypeOf which might help here:
asTypeOf :: a -> a -> a
asTypeOf = const

You can use maxBound `asTypeOf` (fst3 . head $ elements)

Another option is GHC's scoped type variables, which you use by adding
a type signature to your pattern. See:
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#scoped-type-variables

 - Cale

On 22/05/06, Brian Hulley <brianh at metamilk.com> wrote:
> Brian Hulley wrote:
> > Hi -
> > I've got the following function which doesn't compile:
> >
> >  createMonoMultiFont
> >      :: (MonadException m, Enum i, Bounded i)
> >      => [(i, Font, Colour)] -> m (MonoMultiFont i)
> >  createMonoMultiFont elements =
> >      liftIO . block $ do
> >           mmfRaw <- duma_MonoMultiFont_create (fromEnum (maxBound::i))
> >           mmfPtr <- newForeignPtr duma_MonoMultiFont_Release mmfRaw
> >           return $ MonoMultiFont mmfPtr
> >
> > The problem is that ghc complains that there is no instance for
> > Bounded i even though I've written "Bounded i" in the type
> > declaration for the function.
>
> Here is a gross hack that compiles:
>
> createMonoMultiFont elements =
>       liftIO . block $ do
>           let
>               (onei,_,_):_ = (undefined, undefined, undefined) : elements
>               upper :: Bounded a => a -> a
>               upper _ = maxBound
>               last = upper onei
>
>           mmfRaw <- duma_MonoMultiFont_create (fromEnum last)
>           -- adding of elements to mmfRaw ommitted for clarity
>           mmfPtr <- newForeignPtr duma_MonoMultiFont_Release mmfRaw
>           return $ MonoMultiFont mmfPtr
>
> Still, it would be nice to be able to just write (maxBound::i) since the
> dictionary for i is present in the result.
>
> Brian.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list