Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 6e204a966e8c42d976f99a1700ce5f20 > files > 3713

ghc-7.4.2-4.mga5.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Data.Ratio</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
window.onload = function () {pageLoad();setSynopsis("mini_Data-Ratio.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">haskell2010-1.1.0.1: Compatibility with Haskell 2010</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Safe Haskell</th><td>Safe</td></tr></table><p class="caption">Data.Ratio</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Specification
</a></li></ul></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">data</span>  <a href="#t:Ratio">Ratio</a> a</li><li class="src short"><span class="keyword">type</span> <a href="#t:Rational">Rational</a> = <a href="Data-Ratio.html#t:Ratio">Ratio</a> <a href="Prelude.html#t:Integer">Integer</a></li><li class="src short"><a href="#v:-37-">(%)</a> :: <a href="Prelude.html#t:Integral">Integral</a> a =&gt; a -&gt; a -&gt; <a href="Data-Ratio.html#t:Ratio">Ratio</a> a</li><li class="src short"><a href="#v:numerator">numerator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -&gt; a</li><li class="src short"><a href="#v:denominator">denominator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -&gt; a</li><li class="src short"><a href="#v:approxRational">approxRational</a> :: <a href="Prelude.html#t:RealFrac">RealFrac</a> a =&gt; a -&gt; a -&gt; <a href="Data-Ratio.html#t:Rational">Rational</a></li></ul></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><span class="keyword">data</span>  <a name="t:Ratio" class="def">Ratio</a> a <a href="../base-4.5.1.0/src/GHC-Real.html#Ratio" class="link">Source</a></p><div class="doc"><p>Rational numbers, with numerator and denominator of some <code><a href="Prelude.html#t:Integral">Integral</a></code> type.
</p></div><div class="subs instances"><p id="control.i:Ratio" class="caption collapser" onclick="toggleSection('i:Ratio')">Instances</p><div id="section.i:Ratio" class="show"><table><tr><td class="src"><a href="../base-4.5.1.0/Data-Typeable-Internal.html#t:Typeable1">Typeable1</a> <a href="Data-Ratio.html#t:Ratio">Ratio</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Prelude.html#t:Enum">Enum</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src"><a href="Prelude.html#t:Eq">Eq</a> a =&gt; <a href="Prelude.html#t:Eq">Eq</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Prelude.html#t:Fractional">Fractional</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Prelude.html#t:Num">Num</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Prelude.html#t:Ord">Ord</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src">(<a href="Prelude.html#t:Integral">Integral</a> a, <a href="Prelude.html#t:Read">Read</a> a) =&gt; <a href="Prelude.html#t:Read">Read</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Prelude.html#t:Real">Real</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Prelude.html#t:RealFrac">RealFrac</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr><tr><td class="src">(<a href="Prelude.html#t:Integral">Integral</a> a, <a href="Prelude.html#t:Show">Show</a> a) =&gt; <a href="Prelude.html#t:Show">Show</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty">&nbsp;</td></tr></table></div></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:Rational" class="def">Rational</a> = <a href="Data-Ratio.html#t:Ratio">Ratio</a> <a href="Prelude.html#t:Integer">Integer</a><a href="../base-4.5.1.0/src/GHC-Real.html#Rational" class="link">Source</a></p><div class="doc"><p>Arbitrary-precision rational numbers, represented as a ratio of
 two <code><a href="Prelude.html#t:Integer">Integer</a></code> values.  A rational number may be constructed using
 the <code><a href="Data-Ratio.html#v:-37-">%</a></code> operator.
</p></div></div><div class="top"><p class="src"><a name="v:-37-" class="def">(%)</a> :: <a href="Prelude.html#t:Integral">Integral</a> a =&gt; a -&gt; a -&gt; <a href="Data-Ratio.html#t:Ratio">Ratio</a> a<a href="../base-4.5.1.0/src/GHC-Real.html#%25" class="link">Source</a></p><div class="doc"><p>Forms the ratio of two integral numbers.
</p></div></div><div class="top"><p class="src"><a name="v:numerator" class="def">numerator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -&gt; a<a href="../base-4.5.1.0/src/GHC-Real.html#numerator" class="link">Source</a></p><div class="doc"><p>Extract the numerator of the ratio in reduced form:
 the numerator and denominator have no common factor and the denominator
 is positive.
</p></div></div><div class="top"><p class="src"><a name="v:denominator" class="def">denominator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a =&gt; <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -&gt; a<a href="../base-4.5.1.0/src/GHC-Real.html#denominator" class="link">Source</a></p><div class="doc"><p>Extract the denominator of the ratio in reduced form:
 the numerator and denominator have no common factor and the denominator
 is positive.
</p></div></div><div class="top"><p class="src"><a name="v:approxRational" class="def">approxRational</a> :: <a href="Prelude.html#t:RealFrac">RealFrac</a> a =&gt; a -&gt; a -&gt; <a href="Data-Ratio.html#t:Rational">Rational</a><a href="../base-4.5.1.0/src/Data-Ratio.html#approxRational" class="link">Source</a></p><div class="doc"><p><code><a href="Data-Ratio.html#v:approxRational">approxRational</a></code>, applied to two real fractional numbers <code>x</code> and <code>epsilon</code>,
 returns the simplest rational number within <code>epsilon</code> of <code>x</code>.
 A rational number <code>y</code> is said to be <em>simpler</em> than another <code>y'</code> if
</p><ul><li> <code><code><a href="Prelude.html#v:abs">abs</a></code> (<code><a href="Data-Ratio.html#v:numerator">numerator</a></code> y) &lt;= <code><a href="Prelude.html#v:abs">abs</a></code> (<code><a href="Data-Ratio.html#v:numerator">numerator</a></code> y')</code>, and
</li><li> <code><code><a href="Data-Ratio.html#v:denominator">denominator</a></code> y &lt;= <code><a href="Data-Ratio.html#v:denominator">denominator</a></code> y'</code>.
</li></ul><p>Any real interval contains a unique simplest rational;
 in particular, note that <code>0/1</code> is the simplest rational of all.
</p></div></div><h1 id="g:1">Specification
</h1><div class="doc"><pre> module  Data.Ratio (
     Ratio, Rational, (%), numerator, denominator, approxRational ) where
 
 infixl 7  %
 
 ratPrec = 7 :: Int
 
 data  (Integral a)      =&gt; Ratio a = !a :% !a  deriving (Eq)
 type  Rational          =  Ratio Integer
 
 (%)                     :: (Integral a) =&gt; a -&gt; a -&gt; Ratio a
 numerator, denominator  :: (Integral a) =&gt; Ratio a -&gt; a
 approxRational          :: (RealFrac a) =&gt; a -&gt; a -&gt; Rational
 
 
 -- &quot;reduce&quot; is a subsidiary function used only in this module.
 -- It normalises a ratio by dividing both numerator
 -- and denominator by their greatest common divisor.
 --
 -- E.g., 12 `reduce` 8    ==  3 :%   2
 --       12 `reduce` (-8) ==  3 :% (-2)
 
 reduce _ 0              =  error &quot;Data.Ratio.% : zero denominator&quot;
 reduce x y              =  (x `quot` d) :% (y `quot` d)
                            where d = gcd x y
 
 x % y                   =  reduce (x * signum y) (abs y)
 
 numerator (x :% _)      =  x
 
 denominator (_ :% y)    =  y
 
 
 instance  (Integral a)  =&gt; Ord (Ratio a)  where
     (x:%y) &lt;= (x':%y')  =  x * y' &lt;= x' * y
     (x:%y) &lt;  (x':%y')  =  x * y' &lt;  x' * y
 
 instance  (Integral a)  =&gt; Num (Ratio a)  where
     (x:%y) + (x':%y')   =  reduce (x*y' + x'*y) (y*y')
     (x:%y) * (x':%y')   =  reduce (x * x') (y * y')
     negate (x:%y)       =  (-x) :% y
     abs (x:%y)          =  abs x :% y
     signum (x:%y)       =  signum x :% 1
     fromInteger x       =  fromInteger x :% 1
 
 instance  (Integral a)  =&gt; Real (Ratio a)  where
     toRational (x:%y)   =  toInteger x :% toInteger y
 
 instance  (Integral a)  =&gt; Fractional (Ratio a)  where
     (x:%y) / (x':%y')   =  (x*y') % (y*x')
     recip (x:%y)        =  y % x
     fromRational (x:%y) =  fromInteger x :% fromInteger y
 
 instance  (Integral a)  =&gt; RealFrac (Ratio a)  where
     properFraction (x:%y) = (fromIntegral q, r:%y)
                             where (q,r) = quotRem x y
 
 instance  (Integral a)  =&gt; Enum (Ratio a)  where
     succ x           =  x+1
     pred x           =  x-1
     toEnum           =  fromIntegral
     fromEnum         =  fromInteger . truncate        -- May overflow
     enumFrom         =  numericEnumFrom               -- These numericEnumXXX functions
     enumFromThen     =  numericEnumFromThen   -- are as defined in Prelude.hs
     enumFromTo       =  numericEnumFromTo     -- but not exported from it!
     enumFromThenTo   =  numericEnumFromThenTo
 
 instance  (Read a, Integral a)  =&gt; Read (Ratio a)  where
     readsPrec p  =  readParen (p &gt; ratPrec)
                               (\r -&gt; [(x%y,u) | (x,s)   &lt;- readsPrec (ratPrec+1) r,
                                                 (&quot;%&quot;,t) &lt;- lex s,
                                                 (y,u)   &lt;- readsPrec (ratPrec+1) t ])
 
 instance  (Integral a)  =&gt; Show (Ratio a)  where
     showsPrec p (x:%y)  =  showParen (p &gt; ratPrec)
                               showsPrec (ratPrec+1) x . 
                               showString &quot; % &quot; . 
                               showsPrec (ratPrec+1) y)
 
 
 
 approxRational x eps    =  simplest (x-eps) (x+eps)
         where simplest x y | y &lt; x      =  simplest y x
                            | x == y     =  xr
                            | x &gt; 0      =  simplest' n d n' d'
                            | y &lt; 0      =  - simplest' (-n') d' (-n) d
                            | otherwise  =  0 :% 1
                                         where xr@(n:%d) = toRational x
                                               (n':%d')  = toRational y
 
               simplest' n d n' d'       -- assumes 0 &lt; n%d &lt; n'%d'
                         | r == 0     =  q :% 1
                         | q /= q'    =  (q+1) :% 1
                         | otherwise  =  (q*n''+d'') :% n''
                                      where (q,r)      =  quotRem n d
                                            (q',r')    =  quotRem n' d'
                                            (n'':%d'') =  simplest' d' r' d r
</pre></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.11.0</p></div></body></html>