Note that there are some explanatory texts on larger screens.

plurals
  1. POhaskell : making a superclass of Num
    primarykey
    data
    text
    <p>I want to make a superclass of Num, called Linear</p> <pre><code>class Linear a where add :: a -&gt; a -&gt; a instance (Num a) =&gt; Linear a where add = (+) </code></pre> <p>I get the error : </p> <pre><code>Illegal instance declaration for `Linear a' (All instance types must be of the form (T a1 ... an) where a1 ... an are *distinct type variables*, and each type variable appears at most once in the instance head. Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Linear a' </code></pre> <p>From what I understand, something about the line <code>instance (Num a) =&gt; Linear a where</code> is incorrect. (It compiles if I use the flags : <code>-XFlexibleInstances -XUndecidableInstances</code>)</p> <p>Is there a way to achieve this without using those scary flags? (and what in the world is undecidable about the code above??)</p> <p><strong>UPDATE</strong> : Added Polynomial type to Linear.</p> <pre><code>newtype Polynomial a = Polynomial (a,[a]) deriving Show-- list of coeffients instance (Linear a) =&gt; Linear (Polynomial a) where add (Polynomial (c1, l1)) (Polynomial (c2, l2)) = Polynomial (add c1 c2, zipWith (add) l1 l2) p1 = Polynomial (0, [3,4,5]) p2 = Polynomial (0, []) main = putStrLn $ show ((add p1 p2):: Polynomial Int) </code></pre> <p>After adding polynomial, it doesn't compile with even those flags and give the error:</p> <pre><code>Overlapping instances for Linear (Polynomial Int) arising from a use of `add' Matching instances: instance Num a =&gt; Linear a -- Defined at Algebra.hs:22:10-28 instance Linear a =&gt; Linear (Polynomial a) -- Defined at Algebra.hs:25:10-44 In the first argument of `show', namely `((add p1 p2) :: Polynomial Int)' In the second argument of `($)', namely `show ((add p1 p2) :: Polynomial Int)' In the expression: putStrLn $ show ((add p1 p2) :: Polynomial Int) </code></pre>
    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.
 

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