[Haskell-cafe] Problem with existential type and instance.

Roman Cheplyaka roma at ro-che.info
Mon Jul 28 08:19:05 UTC 2014


On a second thought, you don't have to recreate Maybe; you can get away with a
simple wrapper

  data SomeMonadIO = forall m . MonadIO m => SomeMonadIO (m ())

  newtype EventHandlers = EventHandlers { onDeleteWindow :: Maybe SomeMonadIO }

Roman

* Magicloud Magiclouds <magicloud.magiclouds at gmail.com> [2014-07-28 16:13:40+0800]
> I think that is what I need. Thanks.
> 
> 
> On Mon, Jul 28, 2014 at 4:04 PM, Roman Cheplyaka <roma at ro-che.info> wrote:
> 
> > What do you mean? MonadIO m is a constraint. You have to supply some type
> > that
> > satisfies this constraint. IO is one such type; you could use any other.
> >
> > Alternatively, if you *don't* want to store a MonadIO dictionary in the
> > Nothing
> > case, create your own Maybe type:
> >
> >   data MaybeMonadIO
> >     = NoMonadIO
> >     | forall m . MonadIO m => JustMonadIO (m ())
> >
> >   newtype EventHandlers = EventHandlers { onDeleteWindow :: MaybeMonadIO }
> >
> > * Magicloud Magiclouds <magicloud.magiclouds at gmail.com> [2014-07-28
> > 15:50:05+0800]
> > > I see. Thank you.
> > >
> > > Any anyway I could use (MonadIO m) here, in Nothing data type?
> > >
> > >
> > > On Mon, Jul 28, 2014 at 3:47 PM, Roman Cheplyaka <roma at ro-che.info>
> > wrote:
> > >
> > > > * Magicloud Magiclouds <magicloud.magiclouds at gmail.com> [2014-07-28
> > > > 13:46:53+0800]
> > > > > Hi,
> > > > >
> > > > >   For code like below, how to make it compilable?
> > > > >
> > > > > data EventHandlers = forall m. MonadIO m => EventHandlers {
> > > > onDeleteWindow
> > > > > :: Maybe (m ()) }
> > > > > instance Default EventHandlers where
> > > > >   def = EventHandlers Nothing
> > > >
> > > > GHC has to store some MonadIO dictionary in the existential type, but
> > it
> > > > cannot
> > > > figure out which dictionary to store (it can be any).
> > > >
> > > > You can tell it which one to use by supplying a type annotation, e.g.:
> > > >
> > > >   def = EventHandlers (Nothing :: Maybe (IO ()))
> > > >
> > > > Roman
> > > >
> > >
> > >
> > >
> > > --
> > > 竹密岂妨流水过
> > > 山高哪阻野云飞
> > >
> > > And for G+, please use magiclouds#gmail.com.
> >
> 
> 
> 
> -- 
> 竹密岂妨流水过
> 山高哪阻野云飞
> 
> And for G+, please use magiclouds#gmail.com.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140728/9dd6f03f/attachment.sig>


More information about the Haskell-Cafe mailing list