<div dir="ltr">Thank you guys.<div><br></div><div style>I cannot use a explicit type for there are quite a few of them. But from MigMit, I understand why my original cannot work.</div></div><div class="gmail_extra"><br><br>
<div class="gmail_quote">On Sat, Jun 22, 2013 at 4:46 AM, Vincent Ambo <span dir="ltr"><<a href="mailto:tazjin@gmail.com" target="_blank">tazjin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Is there a reason why you can't use an explicit type variable?<div><br></div><div><div><font face="courier new, monospace">{-# LANGUAGE OverloadedStrings, ExistentialQuantification #-}</font></div><div>
<font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">import Data.Aeson</font></div><div><font face="courier new, monospace">import Control.Applicative</font></div><div><font face="courier new, monospace">import Control.Monad (mzero)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">data ActionData j</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">        </span>= (FromJSON j, ToJSON j) => AD j j</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">instance ToJSON (ActionData j) where</font></div><div class="im"><div><font face="courier new, monospace"> toJSON (AD o n) = object [ "oldData" .= o</font></div>
<div><font face="courier new, monospace"> , "newData" .= n ]</font></div><div><font face="courier new, monospace"><br></font></div></div><div><font face="courier new, monospace">instance (ToJSON j, FromJSON j) => FromJSON (ActionData j) where</font></div>
<div class="im">
<div><font face="courier new, monospace"> parseJSON (Object v) = AD</font></div><div><font face="courier new, monospace"> <$> v .: "oldData"</font></div><div><font face="courier new, monospace"> <*> v .: "newData"</font></div>
<div><font face="courier new, monospace"> parseJSON _ = mzero</font></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">2013/6/21 Miguel Mitrofanov <span dir="ltr"><<a href="mailto:miguelimo38@yandex.ru" target="_blank">miguelimo38@yandex.ru</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Forgot to reply all, as usual.<br>
<br>
-------- Пересылаемое сообщение --------<br>
21.06.2013, 12:52, "Miguel Mitrofanov" <<a href="mailto:miguelimo38@yandex.ru" target="_blank">miguelimo38@yandex.ru</a>>:<br>
<br>
Actually, this is not the real error you should care about. Try removing FromJSON instance completely, and you'll get a lot more. And these are fundamental: you have to decide what "j" to use when serializing. Haskell won't automagically substitute some suitable type for you.<br>
<br>
So, that's a classic mismatch: for serializing (ToJSON) you need your "j" type to be known to the AD value (meaning: it should be quantified existentially), but for deserializing you need it to be any type (quantified universally).<br>
<br>
All in all, AD seems to be the wrong type.<br>
<br>
21.06.2013, 12:18, "Magicloud Magiclouds" <<a href="mailto:magicloud.magiclouds@gmail.com" target="_blank">magicloud.magiclouds@gmail.com</a>>:<br>
<div><div><br>
> data ActionData = AD { oldData :: (FromJSON j, ToJSON j) => j<br>
> , newData :: (FromJSON j, ToJSON j) => j}<br>
> instance ToJSON ActionData where<br>
> toJSON (AD o n) = object [ "oldData" .= o<br>
> , "newData" .= n ]<br>
> instance FromJSON ActionData where<br>
> parseJSON (Object v) = AD<br>
> <$> v .: "oldData"<br>
> <*> v .: "newData"<br>
> parseJSON _ = mzero<br>
><br>
> I got when compile:<br>
> No instance for (FromJSON (forall j. (FromJSON j, ToJSON j) => j))<br>
> arising from a use of `.:'<br>
> Possible fix:<br>
> add an instance declaration for<br>
> (FromJSON (forall j. (FromJSON j, ToJSON j) => j))<br>
> In the second argument of `(<$>)', namely `v .: "oldData"'<br>
> In the first argument of `(<*>)', namely `AD <$> v .: "oldData"'<br>
> In the expression: AD <$> v .: "oldData" <*> v .: "newData"<br>
><br>
> --<br>
> 竹密岂妨流水过<br>
> 山高哪阻野云飞<br>
><br>
> And for G+, please use magiclouds#<a href="http://gmail.com" target="_blank">gmail.com</a>.<br>
</div></div>> ,<br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
-------- Завершение пересылаемого сообщения --------<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>竹密岂妨流水过<br>山高哪阻野云飞<br><br>And for G+, please use magiclouds#<a href="http://gmail.com" target="_blank">gmail.com</a>.
</div>