Use <br>&nbsp; value :: a -&gt; Integer<br><br><br><div><span class="gmail_quote">On 8/18/07, <b class="gmail_sendername">DavidA</b> &lt;<a href="mailto:polyomino@f2s.com">polyomino@f2s.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br><br>I am trying to implement quadratic fields Q(sqrt d). These are numbers of the<br>form a + b sqrt d, where a and b are rationals, and d is an integer.<br><br>In an earlier attempt, I tried<br>data QF = QF Integer Rational Rational
<br>(see <a href="http://www.polyomino.f2s.com/david/haskell/hs/QuadraticField.hs.txt">http://www.polyomino.f2s.com/david/haskell/hs/QuadraticField.hs.txt</a>)<br>The problem with this approach is that it&#39;s not really type-safe:
<br>I can attempt to add a + b sqrt 2 to c + d sqrt 3, whereas this should be a<br>type error because 2 /= 3.<br><br>So I thought I&#39;d have a go at doing it with phantom types. In effect I&#39;d be<br>using phantom types to simulate dependent types. Here&#39;s the code:
<br><br>{-# OPTIONS_GHC -fglasgow-exts #-}<br><br>import Data.Ratio<br><br>class IntegerType a where<br>&nbsp;&nbsp;&nbsp;&nbsp;value :: Integer<br><br>data Two<br>instance IntegerType Two where value = 2<br><br>data Three<br>instance IntegerType Three where value = 3
<br><br>data QF d = QF Rational Rational deriving (Eq)<br><br>instance IntegerType d =&gt; Show (QF d) where<br>&nbsp;&nbsp;&nbsp;&nbsp;show (QF a b) = show a ++ &quot; + &quot; ++ show b ++ &quot; sqrt &quot; ++ show value<br><br>instance IntegerType d =&gt; Num (QF d) where
<br>&nbsp;&nbsp;&nbsp;&nbsp;QF a b + QF a&#39; b&#39; = QF (a+a&#39;) (b+b&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;negate (QF a b) = QF (-a) (-b)<br>&nbsp;&nbsp;&nbsp;&nbsp;QF a b * QF c d = QF (a*c + b*d*value) (a*d + b*c)<br>&nbsp;&nbsp;&nbsp;&nbsp;fromInteger n = QF (fromInteger n) 0<br><br>The problem is, this doesn&#39;t work. GHC complains:
<br>&nbsp;&nbsp;&nbsp;&nbsp;The class method `value&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;mentions none of the type variables of the class IntegerType a<br>&nbsp;&nbsp;&nbsp;&nbsp;When checking the class method: value :: Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;In the class declaration for `IntegerType&#39;<br><br>
Is what I&#39;m trying to do reasonable? If no, what should I be doing instead? If<br>yes, why doesn&#39;t GHC like it?<br><br>Thanks, David<br><br>_______________________________________________<br>Haskell-Cafe mailing list
<br><a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></blockquote></div><br>