Use <br> value :: a -> Integer<br><br><br><div><span class="gmail_quote">On 8/18/07, <b class="gmail_sendername">DavidA</b> <<a href="mailto:polyomino@f2s.com">polyomino@f2s.com</a>> 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'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'd have a go at doing it with phantom types. In effect I'd be<br>using phantom types to simulate dependent types. Here's the code:
<br><br>{-# OPTIONS_GHC -fglasgow-exts #-}<br><br>import Data.Ratio<br><br>class IntegerType a where<br> 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 => Show (QF d) where<br> show (QF a b) = show a ++ " + " ++ show b ++ " sqrt " ++ show value<br><br>instance IntegerType d => Num (QF d) where
<br> QF a b + QF a' b' = QF (a+a') (b+b')<br> negate (QF a b) = QF (-a) (-b)<br> QF a b * QF c d = QF (a*c + b*d*value) (a*d + b*c)<br> fromInteger n = QF (fromInteger n) 0<br><br>The problem is, this doesn't work. GHC complains:
<br> The class method `value'<br> mentions none of the type variables of the class IntegerType a<br> When checking the class method: value :: Integer<br> In the class declaration for `IntegerType'<br><br>
Is what I'm trying to do reasonable? If no, what should I be doing instead? If<br>yes, why doesn'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>