Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I, er... I almost hate to suggest this, because doing this is kinda horrible, but... doesn't your code work as is?</p> <pre><code>{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE OverlappingInstances #-} import Data.Maybe(fromJust) lk x = flip lookup x flipPair = uncurry $ flip (,) class (Eq a, Eq b) =&gt; Convert a b where mapping :: [(a, b)] mapping = error "No mapping defined" convert :: a -&gt; b convert = fromJust . lk mapping instance (Convert a b) =&gt; Convert b a where convert = fromJust . lk (map flipPair mapping) data XX = One | Two | Three deriving (Show, Eq) data YY = Eno | Owt | Eerht deriving (Show, Eq) instance Convert XX YY where mapping = [(One, Eno), (Two, Owt), (Three, Eerht)] main = do print $ (convert One :: YY) print $ (convert Owt :: XX) </code></pre> <p>And:</p> <pre><code>[1 of 1] Compiling Main ( GeneralConversion.hs, interpreted ) Ok, modules loaded: Main. *Main&gt; main Eno Two *Main&gt; </code></pre> <p>I'm not sure how useful such a type class is, and all the standard disclaimers about dubious extensions apply, but that much seems to work. Now, if you want to do anything <em>fancier</em>... like <code>Convert a a</code> or <code>(Convert a b, Convert b c) =&gt; Convert a c</code>... things might get awkward.</p> <hr> <p>I suppose I might as well leave a few thoughts about why I doubt the utility of this:</p> <ul> <li><p>In order to use the conversion, both types must be unambiguously known; likewise, the existence of a conversion depends on both types. This limits how useful the class can be for writing very generic code, compared to things such as <code>fromIntegral</code>.</p></li> <li><p>The use of <code>error</code> to handle missing conversions, combined with the above, means that any allegedly generic function using <code>convert</code> will be a seething pit of runtime errors just waiting to happen.</p></li> <li><p>To top it all off, the generic instance being used for the reversed mapping is in fact a <em>universal instance</em>, only being hidden by overlapped, more specific instances. That <code>(Convert a b)</code> in the context? That lets the reversed mapping work, but doesn't restrict it to only reversing instances that are specifically defined.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload