Movatterモバイル変換
[0]ホーム
\begin{code}moduleGHC.Enum(Bounded(..),Enum(..),boundedEnumFrom,boundedEnumFromThen,)whereimportGHC.BaseimportData.Tuple()default()
\end{code}%*********************************************************%* *\subsection{Class declarations}%* *%*********************************************************\begin{code}classBoundedawhereminBound,maxBound::aclassEnumawheresucc::a->apred::a->atoEnum::Int->afromEnum::a->IntenumFrom::a->[a]enumFromThen::a->a->[a]enumFromTo::a->a->[a]enumFromThenTo::a->a->a->[a]succ=toEnum.(`plusInt`oneInt).fromEnumpred=toEnum.(`minusInt`oneInt).fromEnumenumFromx=maptoEnum[fromEnumx..]enumFromThenxy=maptoEnum[fromEnumx,fromEnumy..]enumFromToxy=maptoEnum[fromEnumx..fromEnumy]enumFromThenTox1x2y=maptoEnum[fromEnumx1,fromEnumx2..fromEnumy]boundedEnumFrom::(Enuma,Boundeda)=>a->[a]boundedEnumFromn=maptoEnum[fromEnumn..fromEnum(maxBound`asTypeOf`n)]boundedEnumFromThen::(Enuma,Boundeda)=>a->a->[a]boundedEnumFromThenn1n2|i_n2>=i_n1=maptoEnum[i_n1,i_n2..fromEnum(maxBound`asTypeOf`n1)]|otherwise=maptoEnum[i_n1,i_n2..fromEnum(minBound`asTypeOf`n1)]wherei_n1=fromEnumn1i_n2=fromEnumn2
\end{code}%*********************************************************%* *\subsection{Tuples}%* *%*********************************************************\begin{code}instanceBounded()whereminBound=()maxBound=()instanceEnum()wheresucc_=error"Prelude.Enum.().succ: bad argument"pred_=error"Prelude.Enum.().pred: bad argument"toEnumx|x==zeroInt=()|otherwise=error"Prelude.Enum.().toEnum: bad argument"fromEnum()=zeroIntenumFrom()=[()]enumFromThen()()=letmany=():manyinmanyenumFromTo()()=[()]enumFromThenTo()()()=letmany=():manyinmany
\end{code}\begin{code}instance(Boundeda,Boundedb)=>Bounded(a,b)whereminBound=(minBound,minBound)maxBound=(maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc)=>Bounded(a,b,c)whereminBound=(minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd)=>Bounded(a,b,c,d)whereminBound=(minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede)=>Bounded(a,b,c,d,e)whereminBound=(minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf)=>Bounded(a,b,c,d,e,f)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg)=>Bounded(a,b,c,d,e,f,g)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh)=>Bounded(a,b,c,d,e,f,g,h)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh,Boundedi)=>Bounded(a,b,c,d,e,f,g,h,i)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh,Boundedi,Boundedj)=>Bounded(a,b,c,d,e,f,g,h,i,j)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh,Boundedi,Boundedj,Boundedk)=>Bounded(a,b,c,d,e,f,g,h,i,j,k)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh,Boundedi,Boundedj,Boundedk,Boundedl)=>Bounded(a,b,c,d,e,f,g,h,i,j,k,l)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh,Boundedi,Boundedj,Boundedk,Boundedl,Boundedm)=>Bounded(a,b,c,d,e,f,g,h,i,j,k,l,m)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh,Boundedi,Boundedj,Boundedk,Boundedl,Boundedm,Boundedn)=>Bounded(a,b,c,d,e,f,g,h,i,j,k,l,m,n)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)instance(Boundeda,Boundedb,Boundedc,Boundedd,Boundede,Boundedf,Boundedg,Boundedh,Boundedi,Boundedj,Boundedk,Boundedl,Boundedm,Boundedn,Boundedo)=>Bounded(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o)whereminBound=(minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound,minBound)maxBound=(maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound,maxBound)
\end{code}%*********************************************************%* *\subsection{Type @Bool@}%* *%*********************************************************\begin{code}instanceBoundedBoolwhereminBound=FalsemaxBound=TrueinstanceEnumBoolwheresuccFalse=TruesuccTrue=error"Prelude.Enum.Bool.succ: bad argument"predTrue=FalsepredFalse=error"Prelude.Enum.Bool.pred: bad argument"toEnumn|n==zeroInt=False|n==oneInt=True|otherwise=error"Prelude.Enum.Bool.toEnum: bad argument"fromEnumFalse=zeroIntfromEnumTrue=oneIntenumFrom=boundedEnumFromenumFromThen=boundedEnumFromThen
\end{code}%*********************************************************%* *\subsection{Type @Ordering@}%* *%*********************************************************\begin{code}instanceBoundedOrderingwhereminBound=LTmaxBound=GTinstanceEnumOrderingwheresuccLT=EQsuccEQ=GTsuccGT=error"Prelude.Enum.Ordering.succ: bad argument"predGT=EQpredEQ=LTpredLT=error"Prelude.Enum.Ordering.pred: bad argument"toEnumn|n==zeroInt=LT|n==oneInt=EQ|n==twoInt=GTtoEnum_=error"Prelude.Enum.Ordering.toEnum: bad argument"fromEnumLT=zeroIntfromEnumEQ=oneIntfromEnumGT=twoIntenumFrom=boundedEnumFromenumFromThen=boundedEnumFromThen
\end{code}%*********************************************************%* *\subsection{Type @Char@}%* *%*********************************************************\begin{code}instanceBoundedCharwhereminBound='\0'maxBound='\x10FFFF'instanceEnumCharwheresucc(C#c#)|not(ord#c#==#0x10FFFF#)=C#(chr#(ord#c#+#1#))|otherwise=error("Prelude.Enum.Char.succ: bad argument")pred(C#c#)|not(ord#c#==#0#)=C#(chr#(ord#c#-#1#))|otherwise=error("Prelude.Enum.Char.pred: bad argument")toEnum=chrfromEnum=ordenumFrom(C#x)=eftChar(ord#x)0x10FFFF#enumFromTo(C#x)(C#y)=eftChar(ord#x)(ord#y)enumFromThen(C#x1)(C#x2)=efdChar(ord#x1)(ord#x2)enumFromThenTo(C#x1)(C#x2)(C#y)=efdtChar(ord#x1)(ord#x2)(ord#y)eftCharFB::(Char->a->a)->a->Int#->Int#->aeftCharFBcnx0y=gox0wheregox|x>#y=n|otherwise=C#(chr#x)`c`go(x+#1#)eftChar::Int#->Int#->StringeftCharxy|x>#y=[]|otherwise=C#(chr#x):eftChar(x+#1#)yefdCharFB::(Char->a->a)->a->Int#->Int#->aefdCharFBcnx1x2|delta>=#0#=go_up_char_fbcnx1delta0x10FFFF#|otherwise=go_dn_char_fbcnx1delta0#where!delta=x2-#x1efdChar::Int#->Int#->StringefdCharx1x2|delta>=#0#=go_up_char_listx1delta0x10FFFF#|otherwise=go_dn_char_listx1delta0#where!delta=x2-#x1efdtCharFB::(Char->a->a)->a->Int#->Int#->Int#->aefdtCharFBcnx1x2lim|delta>=#0#=go_up_char_fbcnx1deltalim|otherwise=go_dn_char_fbcnx1deltalimwhere!delta=x2-#x1efdtChar::Int#->Int#->Int#->StringefdtCharx1x2lim|delta>=#0#=go_up_char_listx1deltalim|otherwise=go_dn_char_listx1deltalimwhere!delta=x2-#x1go_up_char_fb::(Char->a->a)->a->Int#->Int#->Int#->ago_up_char_fbcnx0deltalim=go_upx0wherego_upx|x>#lim=n|otherwise=C#(chr#x)`c`go_up(x+#delta)go_dn_char_fb::(Char->a->a)->a->Int#->Int#->Int#->ago_dn_char_fbcnx0deltalim=go_dnx0wherego_dnx|x<#lim=n|otherwise=C#(chr#x)`c`go_dn(x+#delta)go_up_char_list::Int#->Int#->Int#->Stringgo_up_char_listx0deltalim=go_upx0wherego_upx|x>#lim=[]|otherwise=C#(chr#x):go_up(x+#delta)go_dn_char_list::Int#->Int#->Int#->Stringgo_dn_char_listx0deltalim=go_dnx0wherego_dnx|x<#lim=[]|otherwise=C#(chr#x):go_dn(x+#delta)\end{code}%*********************************************************%* *\subsection{Type @Int@}%* *%*********************************************************Be careful about these instances. (a) remember that you have to count down as well as up e.g. [13,12..0] (b) be careful of Int overflow (c) remember that Int is bounded, so [1..] terminates at maxIntAlso NB that the Num class isn't available in this module. \begin{code}instanceBoundedIntwhereminBound=minIntmaxBound=maxIntinstanceEnumIntwheresuccx|x==maxBound=error"Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"|otherwise=x`plusInt`oneIntpredx|x==minBound=error"Prelude.Enum.pred{Int}: tried to take `pred' of minBound"|otherwise=x`minusInt`oneInttoEnumx=xfromEnumx=xenumFrom(I#x)=eftIntxmaxInt#where!(I#maxInt#)=maxIntenumFromTo(I#x)(I#y)=eftIntxyenumFromThen(I#x1)(I#x2)=efdIntx1x2enumFromThenTo(I#x1)(I#x2)(I#y)=efdtIntx1x2yeftInt::Int#->Int#->[Int]eftIntx0y|x0>#y=[]|otherwise=gox0wheregox=I#x:ifx==#ythen[]elsego(x+#1#)eftIntFB::(Int->r->r)->r->Int#->Int#->reftIntFBcnx0y|x0>#y=n|otherwise=gox0wheregox=I#x`c`ifx==#ythennelsego(x+#1#)efdInt::Int#->Int#->[Int]efdIntx1x2|x2>=#x1=casemaxIntofI#y->efdtIntUpx1x2y|otherwise=caseminIntofI#y->efdtIntDnx1x2yefdtInt::Int#->Int#->Int#->[Int]efdtIntx1x2y|x2>=#x1=efdtIntUpx1x2y|otherwise=efdtIntDnx1x2yefdtIntFB::(Int->r->r)->r->Int#->Int#->Int#->refdtIntFBcnx1x2y|x2>=#x1=efdtIntUpFBcnx1x2y|otherwise=efdtIntDnFBcnx1x2yefdtIntUp::Int#->Int#->Int#->[Int]efdtIntUpx1x2y|y<#x2=ify<#x1then[]else[I#x1]|otherwise=let!delta=x2-#x1!y'=y-#deltago_upx|x>#y'=[I#x]|otherwise=I#x:go_up(x+#delta)inI#x1:go_upx2efdtIntUpFB::(Int->r->r)->r->Int#->Int#->Int#->refdtIntUpFBcnx1x2y|y<#x2=ify<#x1thennelseI#x1`c`n|otherwise=let!delta=x2-#x1!y'=y-#deltago_upx|x>#y'=I#x`c`n|otherwise=I#x`c`go_up(x+#delta)inI#x1`c`go_upx2efdtIntDn::Int#->Int#->Int#->[Int]efdtIntDnx1x2y|y>#x2=ify>#x1then[]else[I#x1]|otherwise=let!delta=x2-#x1!y'=y-#deltago_dnx|x<#y'=[I#x]|otherwise=I#x:go_dn(x+#delta)inI#x1:go_dnx2efdtIntDnFB::(Int->r->r)->r->Int#->Int#->Int#->refdtIntDnFBcnx1x2y|y>#x2=ify>#x1thennelseI#x1`c`n|otherwise=let!delta=x2-#x1!y'=y-#deltago_dnx|x<#y'=I#x`c`n|otherwise=I#x`c`go_dn(x+#delta)inI#x1`c`go_dnx2\end{code}
[8]ページ先頭