title: Reference inferred type in multi parameter type classes
body: <p>I have a data type with 3 phantom types attached so i can define functions classes for each combination. This method removes much duplicate code:</p>
  <pre><code>newtype Dd x y z = ToDd Cudd.DdNode deriving (Eq,Show)
  
  --Class for all instances where the first subtype (x) is specified
  class DdT a b c where
      neg :: Dd a b c -&gt; Dd a b c
  
  --Class for all instances that are completely specified
  class DdTFS a b c where
      bot :: Dd a b c 
      top :: Dd a b c 
  </code></pre>
  <p>with this i can define functions for multiple cases</p>
  <pre><code>--bot and top get no arguments, thus the instance types are fully specified
  instance DdTFS B F1 S1 where
    bot = ToDd (Cudd.readLogicZero mgr)
    top = ToDd (Cudd.readOne mgr)
  
  instance DdTFS Z F1 S1 where
    bot = ToDd $ Cudd.zddReadZero
    top = ToDd $ Cudd.zddReadOne
  -- ..etc for every combination of types
  
  --x is set to B, y and z are inferred
  instance DdT B a b where
    neg (ToDd b) = ToDd $ Cudd.cuddNot b
  
  --x is set to Z, y and z are inferred
  instance DdT Z a b where
    neg (ToDd z) = ToDd $ Cudd.ifthenelse (ToDd z) (bot :: Dd Z a b) (top :: Dd Z a b)
  </code></pre>
  <p>In the last line lies my problem, i had hoped ghci would infer (for a and b) the data types from the given argument, z. Instead i get &quot;No instance for (DdTFS Z a1 b1) arising from a use of ‘top’&quot; and &quot;No instance for (DdTFS Z a1 b1) arising from a use of ‘bot’&quot; when compiling. Is there an easy fix for what i am trying to achieve? or am i approaching the problem wrong?</p>
  <p>Edit: I tried</p>
  <pre><code>neg z = ToDd $ Cudd.ifthenelse z ((bot mgr)`asTypeOf` z) ((bot mgr)`asTypeOf` z)
  </code></pre>
  <p>but this tells me that &quot;No instance for (DdTFS Z a b) arising from a use of ‘bot’&quot;</p>
  <p>Edit 2: replaced &quot;z&quot; in the last line with (ToDd z)</p>