[Haskell-cafe] different behaviours with or without putStrLn

Andras Slemmer 0slemi0 at gmail.com
Mon Jan 20 11:50:14 UTC 2014


Off the top of my head i can think of two non-overlapping solution, both
are ugly but maybe someone has an idea to make one nice:

The first is to bake the constraint into the GADT:

data MyGADT a where
   MyField :: NFData b => b -> MyGADT (Maybe b)

instance NFData (MyGADT a) where
   rnf (MyField b) = rnf b

This is bad because NFData should have nothing to do with the definition of
the datastructure

The second is to use implication constraints:

import Data.Constraint

data MyGADT a where
   MyField :: b -> MyGADT (Maybe b)

newtype Constr a = Constr (forall b. (a ~ Maybe b) :- NFData b)

class DConstr a where
    constr :: Constr a

instance (NFData a) => DConstr (Maybe a) where
    constr = Constr (Sub Dict)

instance (DConstr a) => NFData (MyGADT a) where
   rnf (MyField b) = case constr :: Constr a of
                       Constr (Sub Dict) -> rnf b


this is ugly because for every non-Maybe index you need to define an
"absurd" instance of DConstr. e.g. for Int:

instance DConstr Int where
    constr = Constr undefined -- should be (Constr (Sub !absurd!)) but
there is no way to denote absurdity in Haskell



On 19 January 2014 23:16, Bob Ippolito <bob at redivi.com> wrote:

> For that to work, I believe you must enable FlexibleInstances and possibly
> also OverlappingInstances (depending on what the rest of the GADT looks
> like).
>
>
> On Sun, Jan 19, 2014 at 3:00 PM, Carter Schonwald <
> carter.schonwald at gmail.com> wrote:
>
>> Try
>>
>> *instance NFData a => NFData (MyGADT (Maybe a) )*
>>
>>
>> On Sunday, January 19, 2014, Corentin Dupont <corentin.dupont at gmail.com>
>> wrote:
>>
>>> Hi,
>>> I've tried the make an instance of NFData for all types, although I have
>>> a problem with the GATD:
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *data MyGADT a where   MyField :: b -> MyGADT (Maybe b) instance NFData
>>> a => NFData (MyGADT a) where   rnf (MyField b) = (rnf b)*
>>> Gives me :
>>>     Could not deduce (NFData b) arising from a use of `rnf'
>>>     from the context (NFData a)
>>>
>>> Which I understand (that's because a ~ *Maybe b *and not* a ~ b *due to
>>> the declaration of *MyField b *being of type *MyGADT (Maybe b)*)
>>>
>>> *But I have no idea how to solve that?? *
>>>
>>> *Thanks...*
>>>
>>> *Corentin*
>>>
>>>
>>>
>>> On Mon, Jan 13, 2014 at 1:10 AM, Brandon Allbery <allbery.b at gmail.com>wrote:
>>>
>>>> On Sun, Jan 12, 2014 at 6:44 PM, Corentin Dupont <
>>>> corentin.dupont at gmail.com> wrote:
>>>>
>>>>> One question:
>>>>> Since it works with putStrLn, can I simulate the behaviour of putStrLn
>>>>> without actually... printing anything? As a workaround...
>>>>>
>>>>
>>>> Worst case, open a handle on /dev/null and hPutStr to it.
>>>>
>>>> I'd probably try to figure out how to write appropriate NFData
>>>> instances, though.
>>>>
>>>>  --
>>>> brandon s allbery kf8nh                               sine nomine
>>>> associates
>>>> allbery.b at gmail.com
>>>> ballbery at sinenomine.net
>>>> unix, openafs, kerberos, infrastructure, xmonad
>>>> http://sinenomine.net
>>>>
>>>
>>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140120/b19ac46c/attachment.html>


More information about the Haskell-Cafe mailing list