title: How to establish an ordering between types in Haskell
body: <p>I need to establish ordering between <code>* -&gt; *</code> types based on that each member of one type can be represented by another. This is a homomorphism.</p>
  
  <p>The problem is that I can define the transitivity of the <code>!&lt;=!</code> relation, but the type checker cannot figure it out. It is also very ambiguous, <code>Identity !&lt;=! Maybe</code> could be derived from <code>Identity !&lt;=! Maybe</code> or <code>Identity !&lt;=! Identity !&lt;=! Maybe</code>, ... Each derivation comes with a different (but equivalent) definition for <code>repr</code>.</p>
  
  <p>So I'm looking for other ways to create a reflexive and transitive relationship.</p>
  
  <pre><code>{-# LANGUAGE ScopedTypeVariables, TypeOperators, MultiParamTypeClasses, FlexibleInstances, UndecidableInstances, AllowAmbiguousTypes, OverlappingInstances #-}
  
  import Control.Monad.Identity
  import Data.Maybe
  
  class x !&lt;=! y where
    repr :: x a -&gt; y a
  
  instance x !&lt;=! x where
    repr = id
  
  instance Identity !&lt;=! Maybe where
    repr = return . runIdentity
  
  instance Maybe !&lt;=! [] where
    repr = maybeToList
  
  instance (x !&lt;=! y, y !&lt;=! z) =&gt; x !&lt;=! z where
    repr = r2 . r1
      where r1 :: x a -&gt; y a
            r1 = repr
            r2 :: y a -&gt; z a
            r2 = repr
  </code></pre>
  
  <p>note: I tried this on GHC 7.8. You may have to remove <code>AllowAmbiguousTypes</code>.</p>
  
  <p>Edit: I would like to do something like <code>repr (Identity 3 :: Identity Int) :: [Int]</code></p>