Output Type - Monad Could Not Withdraw

I am creating a way to display dialogue to users.

data DialogConfig tmbe = DialogConfig { _dialogConfig_title :: Dynamic t T.Text , _dialogConfig_content :: b -> m (Dynamic t (Maybe b)) , _dialogConfig_footer :: Dynamic t (Maybe b) -> m (Event te) } dialog :: MonadWidget tm => DialogConfig tmbe -> Event tb -> m (Event t (DialogEvent e)) 

I would like to use some kind of "default" instance to initialize DialogConfig for the dialog function so that I can use it, for example. defaultConfig{_dialogConfig_content=content} . However, I am struggling with the type of inferrence. It works:

 confirmDialog :: forall t m. MonadWidget tm => T.Text -> Event t T.Text -> m (Event t ()) ... evt <- dialog (DialogConfig { _dialogConfig_title = constDyn title , _dialogConfig_content = content , _dialogConfig_footer = buttons} ) contentEvt 

However, when I use some default DialogConfig (for example, directly inserting it here), it does not execute:

 evt <- dialog (DialogConfig { _dialogConfig_title = constDyn mempty , _dialogConfig_content = const $ return $ constDyn Nothing , _dialogConfig_footer = const $ return never } { _dialogConfig_title = constDyn title , _dialogConfig_content = content , _dialogConfig_footer = buttons} ) contentEvt 

Errors:

 Could not deduce (Reflex t0) arising from a use of 'constDyn' from the context (MonadWidget tm) Could not deduce (Monad t1) arising from a use of 'return' from the context (MonadWidget tm) 

I can use ScopedTypeVariables and enter the default configuration in confirmDialog as DialogConfig tmab , and this works, but shouldn't it work even without it? It seems to me that the types are pretty straightforward.

+6
source share
1 answer

As mentioned in the comments, the problem is that updating the record may change the type of record (which may seem unexpected at first). Here is the test in GHCi:

 > data T a = T { tA :: a } > let x = T "foo" > :tx x :: T [Char] > :tx { tA = True } x { tA = True } :: T Bool 

So, we cannot determine the default value:

 > let def :: Read a => T a ; def = T (read "True") > :t def :: T Bool def :: T Bool :: T Bool > :t def { tA = 5 } Could not deduce (Read t0) arising from a use of 'def' The type variable 't0' is ambiguous 

Indeed, the above def can be of any type.

A possible solution may cause the update to be of the same type if the continuation function Ta T a -> T a is required.

 > let defF :: Read a => (T a -> T a) -> T a ; defF f = f (T (read "True")) > :t defF (\d -> d { tA = False }) defF (\d -> d { tA = False }) :: T Bool 

Above d is the default value, which by construction should have the same record type after updating.

With lenses, there may be better approaches.

+4
source

All Articles