Movatterモバイル変換
[0]ホーム
\begin{code}#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#endifmoduleGHC.Num(moduleGHC.Num,moduleGHC.Integer)whereimportGHC.BaseimportGHC.EnumimportGHC.ShowimportGHC.Integerinfixl7*infixl6+,default()
\end{code}%*********************************************************%* *\subsection{Standard numeric class}%* *%*********************************************************\begin{code}class(Eqa,Showa)=>Numawhere(+),(),(*)::a->a->anegate::a->aabs::a->asignum::a->afromInteger::Integer->axy=x+negateynegatex=0xsubtract::(Numa)=>a->a->asubtractxy=yx
\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=1fromIntegeri=I#(integerToInti)quotRemInt::Int->Int->(Int,Int)quotRemInta@(I#_)b@(I#_)=(a`quotInt`b,a`remInt`b)divModInt::Int->Int->(Int,Int)divModIntx@(I#_)y@(I#_)=(x`divInt`y,x`modInt`y)
\end{code}%*********************************************************%* *\subsection{The @Integer@ instances for @Show@}%* *%*********************************************************\begin{code}instanceShowIntegerwhereshowsPrecpnr|p>6&&n<0='(':integerToStringn(')':r)|otherwise=integerToStringnrshowList=showList__(showsPrec0)integerToString::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)csjsplitf::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:jsplitbpnsjprinth::[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$jprintbnscsjhead::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'(d1)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=x1toEnum(I#n)=smallIntegernfromEnumn=I#(integerToIntn)enumFromx=enumDeltaIntegerx1enumFromThenxy=enumDeltaIntegerx(yx)enumFromToxlim=enumDeltaToIntegerx1limenumFromThenToxylim=enumDeltaToIntegerx(yx)limenumDeltaIntegerFB::(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)enumDeltaToIntegerFB::(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]ページ先頭