<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Reply to myself:<br>
<br>
I realized, with GADTs, it does assume Ord a, it's Ord b that fails. I can see how that cannot be derived from the context. Since Foo is only used for types with Ord, though, is there any way that I could let GHC know that this is going to be the case by definition.<br>
<br>
(So basically, in a way that would make fmap not typecheck on Foo when trying to use a function (a -> b) where Ord b does not hold)<br>
<br>
Sorry I did not realize this earlier.<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Haskell-Cafe <haskell-cafe-bounces@haskell.org> on behalf of CASANOVA Juan <Juan.Casanova@ed.ac.uk><br>
<b>Sent:</b> 23 February 2021 18:14<br>
<b>To:</b> haskell-cafe@haskell.org <haskell-cafe@haskell.org><br>
<b>Subject:</b> [Haskell-cafe] DatatypeContexts / alternative</font>
<div> </div>
</div>
<style type="text/css" style="display:none">
<!--
p
{margin-top:0;
margin-bottom:0}
-->
</style>
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Hello again, Haskell Cafe,<br>
<br>
Here again with another doubt on how am I supposed to. This one should be fairly easier, I believe.<br>
<br>
In short, I would like to have a functor type that can only be applied to certain type parameters. Why? Well, something like this:<br>
<br>
module DatatypeContextsExample where
<div><br>
</div>
<div>import Data.Map</div>
<div>import Data.Bifunctor</div>
<div><br>
</div>
<div>data Foo t = Foo (Map t t)</div>
<div><br>
</div>
<div>instance Functor Foo where</div>
fmap f (Foo m) = Foo (fromList (fmap (bimap f f) (toList m)))<br>
<br>
This does not compile, because I am using toList and fromList, which require (Ord t). But I cannot really have Foo be a functor in this way without it. The thing is, every time I am going to use it, t is actually going to be Ord. But how do I tell Haskell to
have this constraint?<br>
<br>
DatatypeContexts seems to be the extension that allows this:<br>
<br>
<span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span>data Ord t => Foo t = Foo (Map t t)<br>
<br>
But this has two issues. First, DatatypeContexts is deprecated. Second, more importantly, it still does not type check! It produces the exact same error, saying that Ord t could not be inferred. It should be inferred from the very fact that I am using the type
Foo, but GHC does not seem to want to understand this.<br>
<br>
I could also try with GADTs:<br>
<br>
data Foo t where<br>
Foo :: Ord t => Map t t -> Foo t<br>
<br>
But this <b>still</b> gives the same error on the fmap definition, stating it cannot use Foo without Ord. But I am pattern-matching against it, shouldn't
<b>that</b> be enough to <b>know</b> that (Ord t) is going to hold? I don't understand it.<br>
<br>
Is there anything simple I am missing? Any other simple way to achieve this? Any fundamental problem with what I am trying to do?<br>
<br>
Thanks as usual,<br>
Juan.<br>
</div>
The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336. Is e buidheann carthannais a th’ ann an Oilthigh Dhùn Èideann, clàraichte an Alba, àireamh clàraidh SC005336.
</div>
</body>
</html>