<p dir="ltr">Dear All,</p>
<p dir="ltr">This has reminded me that perhaps there is an easier way.</p>
<p dir="ltr">I have a Map, whose elements are indexed by a subset of their structure.</p>
<p dir="ltr">I.e. if we have MyType = MyType I T T O E</p>
<p dir="ltr">Where I T O and E are types defined elsewhere.</p>
<p dir="ltr">The Map the indexes elements of type MyType by a pair, (T, T).</p>
<p dir="ltr">I want to be able to index by a pair, independent of order. I had thought about indexing by a pair of pairs, where the elemtns could be the same but reversed.</p>
<p dir="ltr">However, the alternative might be to index by a pair, but define that pair as a type, and alter its Eq => definition:</p>
<p dir="ltr">MyPair = (T, T)<br>
where (t, t') == (t', t) -- I know this syntax is wrong</p>
<p dir="ltr">I could then use that as the index to the Map.</p>
<p dir="ltr">Does that approach make some sense?</p>
<p dir="ltr">Thanks,<br>
Matt</p>
<br><div class="gmail_quote"><div dir="ltr">On Tue, 23 Jun 2015 15:33 Kostiantyn Rybnikov <<a href="mailto:k-bx@k-bx.com">k-bx@k-bx.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>Imants,<br><br></div>You are right. The problem is not in IO here, it's that if you have access to data-constructor, you can do things like:<br><br>six :: TypeValInt 6<br>six = TypeValInt 5<br><br></div>Initially, I was making an assumption that you won't be using a data-constructor. After thinking about it a bit, I should note that my code isn't much different from just using a "smart constructor" approach, e.g. hiding a real MyP constructor, and instead providing a function:<br><br></div>mkMyP (a, b) = MyP (a, b) (b, a)<br><br></div>and exporting only this function. This would make sure all your users only create a valid set of data.<br><div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 23, 2015 at 5:05 PM, Imants Cekusins <span dir="ltr"><<a href="mailto:imantc@gmail.com" target="_blank">imantc@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On 23 June 2015 at 14:54, Kostiantyn Rybnikov <<a href="mailto:k-bx@k-bx.com" target="_blank">k-bx@k-bx.com</a>> wrote:<br>
> Hi Matt. I don't know how bad is this, but here's what I came up with.<br>
</span>...<br>
<br>
this modified for IO version accepts any input, including that which<br>
should have caused error:<br>
<br>
or did I do something wrong?<br>
<span><br>
<br>
{-# LANGUAGE DataKinds #-}<br>
{-# LANGUAGE KindSignatures #-}<br>
{-# LANGUAGE ExistentialQuantification #-}<br>
{-# LANGUAGE PolyKinds #-}<br>
{-# LANGUAGE StandaloneDeriving #-}<br>
<br>
</span>module PairsMatchedKR where<br>
<br>
import GHC.TypeLits<br>
<br>
data TypeValInt (n::Nat) = TypeValInt Int<br>
<span> deriving (Show)<br>
<br>
one :: TypeValInt 1<br>
one = TypeValInt 1<br>
<br>
two :: TypeValInt 2<br>
two = TypeValInt 2<br>
<br>
data MyP a b = MyP (TypeValInt a, TypeValInt b) (TypeValInt b, TypeValInt a)<br>
deriving (Show)<br>
<br>
main :: IO ()<br>
main = do<br>
putStrLn "Hello!"<br>
</span> x1 <- getLine<br>
x2 <- getLine<br>
x3 <- getLine<br>
x4 <- getLine<br>
<br>
print (MyP (tvi x1, tvi x2) (tvi x3, tvi x4))<br>
<span><br>
class TypeVal (g :: a -> *)<br>
instance TypeVal TypeValInt<br>
<br>
data MyPGen a b = forall g. (TypeVal g, Show (g a), Show (g b))<br>
=> MyPGen (g a, g b) (g b, g a)<br>
deriving instance Show (MyPGen a b)<br>
<br>
<br>
</span>tvi:: String -> TypeValInt (n::Nat)<br>
tvi = TypeValInt . read<br>
<div><div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>