Movatterモバイル変換


[0]ホーム

URL:


\begin{code}
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, UnboxedTuples #-}-- We believe we could deorphan this module, by moving lots of things-- around, but we haven't got there yet:{-# OPTIONS_GHC -fno-warn-orphans #-}{-# OPTIONS_HADDOCK hide #-}------------------------------------------------------------------------------- |-- Module      :  GHC.Num-- Copyright   :  (c) The University of Glasgow 1994-2002-- License     :  see libraries/base/LICENSE---- Maintainer  :  cvs-ghc@haskell.org-- Stability   :  internal-- Portability :  non-portable (GHC Extensions)---- The 'Num' class and the 'Integer' type.-------------------------------------------------------------------------------#include "MachDeps.h"#if SIZEOF_HSWORD == 4#define DIGITS       9#define BASE         1000000000#elif SIZEOF_HSWORD == 8#define DIGITS       18#define BASE         1000000000000000000#else#error Please define DIGITS and BASE-- DIGITS should be the largest integer such that--     10^DIGITS < 2^(SIZEOF_HSWORD * 8 - 1)-- BASE should be 10^DIGITS. Note that ^ is not available yet.#endif-- #hidemoduleGHC.Num(moduleGHC.Num,moduleGHC.Integer)whereimportGHC.BaseimportGHC.EnumimportGHC.ShowimportGHC.Integerinfixl7*infixl6+,-default()-- Double isn't available yet,-- and we shouldn't be using defaults anyway
\end{code}%*********************************************************%* *\subsection{Standard numeric class}%* *%*********************************************************\begin{code}
-- | Basic numeric class.---- Minimal complete definition: all except 'negate' or @(-)@class(Eqa,Showa)=>Numawhere(+),(-),(*)::a->a->a-- | Unary negation.negate::a->a-- | Absolute value.abs::a->a-- | Sign of a number.-- The functions 'abs' and 'signum' should satisfy the law:---- > abs x * signum x == x---- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)-- or @1@ (positive).signum::a->a-- | Conversion from an 'Integer'.-- An integer literal represents the application of the function-- 'fromInteger' to the appropriate value of type 'Integer',-- so such literals have type @('Num' a) => a@.fromInteger::Integer->a{-# INLINE (-) #-}{-# INLINE negate #-}x-y=x+negateynegatex=0-x-- | the same as @'flip' ('-')@.---- Because @-@ is treated specially in the Haskell grammar,-- @(-@ /e/@)@ is not a section, but an application of prefix negation.-- However, @('subtract'@ /exp/@)@ is equivalent to the disallowed section.{-# INLINE subtract #-}subtract::(Numa)=>a->a->asubtractxy=y-x
\end{code}%*********************************************************%* *\subsection{Instances for @Int@}%* *%*********************************************************\begin{code}
instanceNumIntwhere(+)=plusInt(-)=minusIntnegate=negateInt(*)=timesIntabsn=ifn`geInt`0thennelsenegateIntnsignumn|n`ltInt`0=negateInt1|n`eqInt`0=0|otherwise=1{-# INLINE fromInteger #-}-- Just to be sure!fromIntegeri=I#(integerToInti)quotRemInt::Int->Int->(Int,Int)quotRemInta@(I#_)b@(I#_)=(a`quotInt`b,a`remInt`b)-- OK, so I made it a little stricter.  Shoot me.  (WDP 94/10)divModInt::Int->Int->(Int,Int)divModIntx@(I#_)y@(I#_)=(x`divInt`y,x`modInt`y)-- Stricter.  Sorry if you don't like it.  (WDP 94/10)
\end{code}%*********************************************************%* *\subsection{The @Integer@ instances for @Show@}%* *%*********************************************************\begin{code}
instanceShowIntegerwhereshowsPrecpnr|p>6&&n<0='(':integerToStringn(')':r)-- Minor point: testing p first gives better code-- in the not-uncommon case where the p argument-- is a constant|otherwise=integerToStringnrshowList=showList__(showsPrec0)-- Divide an conquer implementation of string conversionintegerToString::Integer->String->StringintegerToStringn0cs0|n0<0='-':integerToString'(-n0)cs0|otherwise=integerToString'n0cs0whereintegerToString'::Integer->String->StringintegerToString'ncs|n<BASE=jhead(fromIntegern)cs|otherwise=jprinth(jsplitf(BASE*BASE)n)cs-- Split n into digits in base p. We first split n into digits-- in base p*p and then split each of these digits into two.-- Note that the first 'digit' modulo p*p may have a leading zero-- in base p that we need to drop - this is what jsplith takes care of.-- jsplitb the handles the remaining digits.jsplitf::Integer->Integer->[Integer]jsplitfpn|p>n=[n]|otherwise=jsplithp(jsplitf(p*p)n)jsplith::Integer->[Integer]->[Integer]jsplithp(n:ns)=casen`quotRemInteger`pof(#q,r#)->ifq>0thenq:r:jsplitbpnselser:jsplitbpnsjsplith_[]=error"jsplith: []"jsplitb::Integer->[Integer]->[Integer]jsplitb_[]=[]jsplitbp(n:ns)=casen`quotRemInteger`pof(#q,r#)->q:r:jsplitbpns-- Convert a number that has been split into digits in base BASE^2-- this includes a last splitting step and then conversion of digits-- that all fit into a machine word.jprinth::[Integer]->String->Stringjprinth(n:ns)cs=casen`quotRemInteger`BASEof(#q',r'#)->letq=fromIntegerq'r=fromIntegerr'inifq>0thenjheadq$jblockr$jprintbnscselsejheadr$jprintbnscsjprinth[]_=error"jprinth []"jprintb::[Integer]->String->Stringjprintb[]cs=csjprintb(n:ns)cs=casen`quotRemInteger`BASEof(#q',r'#)->letq=fromIntegerq'r=fromIntegerr'injblockq$jblockr$jprintbnscs-- Convert an integer that fits into a machine word. Again, we have two-- functions, one that drops leading zeros (jhead) and one that doesn't-- (jblock)jhead::Int->String->Stringjheadncs|n<10=caseunsafeChr(ord'0'+n)ofc@(C#_)->c:cs|otherwise=caseunsafeChr(ord'0'+r)ofc@(C#_)->jheadq(c:cs)where(q,r)=n`quotRemInt`10jblock=jblock'{- ' -}DIGITSjblock'::Int->Int->String->Stringjblock'dncs|d==1=caseunsafeChr(ord'0'+n)ofc@(C#_)->c:cs|otherwise=caseunsafeChr(ord'0'+r)ofc@(C#_)->jblock'(d-1)q(c:cs)where(q,r)=n`quotRemInt`10
\end{code}%*********************************************************%* *\subsection{The @Integer@ instances for @Num@}%* *%*********************************************************\begin{code}
instanceNumIntegerwhere(+)=plusInteger(-)=minusInteger(*)=timesIntegernegate=negateIntegerfromIntegerx=xabs=absIntegersignum=signumInteger
\end{code}%*********************************************************%* *\subsection{The @Integer@ instance for @Enum@}%* *%*********************************************************\begin{code}
instanceEnumIntegerwheresuccx=x+1predx=x-1toEnum(I#n)=smallIntegernfromEnumn=I#(integerToIntn){-# INLINE enumFrom #-}{-# INLINE enumFromThen #-}{-# INLINE enumFromTo #-}{-# INLINE enumFromThenTo #-}enumFromx=enumDeltaIntegerx1enumFromThenxy=enumDeltaIntegerx(y-x)enumFromToxlim=enumDeltaToIntegerx1limenumFromThenToxylim=enumDeltaToIntegerx(y-x)lim{-# RULES"enumDeltaInteger"      [~1] forall x y.  enumDeltaInteger x y     = build (\c _ -> enumDeltaIntegerFB c x y)"efdtInteger"           [~1] forall x y l.enumDeltaToInteger x y l = build (\c n -> enumDeltaToIntegerFB c n x y l)"enumDeltaInteger"      [1] enumDeltaIntegerFB   (:)    = enumDeltaInteger"enumDeltaToInteger"    [1] enumDeltaToIntegerFB (:) [] = enumDeltaToInteger #-}enumDeltaIntegerFB::(Integer->b->b)->Integer->Integer->benumDeltaIntegerFBcxd=x`seq`(x`c`enumDeltaIntegerFBc(x+d)d)enumDeltaInteger::Integer->Integer->[Integer]enumDeltaIntegerxd=x`seq`(x:enumDeltaInteger(x+d)d)-- strict accumulator, so--     head (drop 1000000 [1 .. ]-- works{-# NOINLINE [0] enumDeltaToIntegerFB #-}-- Don't inline this until RULE "enumDeltaToInteger" has had a chance to fireenumDeltaToIntegerFB::(Integer->a->a)->a->Integer->Integer->Integer->aenumDeltaToIntegerFBcnxdeltalim|delta>=0=up_fbcnxdeltalim|otherwise=dn_fbcnxdeltalimenumDeltaToInteger::Integer->Integer->Integer->[Integer]enumDeltaToIntegerxdeltalim|delta>=0=up_listxdeltalim|otherwise=dn_listxdeltalimup_fb::(Integer->a->a)->a->Integer->Integer->Integer->aup_fbcnx0deltalim=go(x0::Integer)wheregox|x>lim=n|otherwise=x`c`go(x+delta)dn_fb::(Integer->a->a)->a->Integer->Integer->Integer->adn_fbcnx0deltalim=go(x0::Integer)wheregox|x<lim=n|otherwise=x`c`go(x+delta)up_list::Integer->Integer->Integer->[Integer]up_listx0deltalim=go(x0::Integer)wheregox|x>lim=[]|otherwise=x:go(x+delta)dn_list::Integer->Integer->Integer->[Integer]dn_listx0deltalim=go(x0::Integer)wheregox|x<lim=[]|otherwise=x:go(x+delta)
\end{code}
[8]ページ先頭

©2009-2025 Movatter.jp