-- | Lazy natural numbers.-- Addition and multiplication recurses over the first argument, i.e.,-- @1 + n@ is the way to write the constant time successor function.---- Note that (+) and (*) are not commutative for lazy natural numbers-- when considering bottom.moduleData.Number.Natural(Natural,infinity)whereimportData.MaybedataNatural=Z|SNaturalinstanceShowNaturalwhereshowsPrecpn=showsPrecp(toIntegern)instanceEqNaturalwherex==y=x`compare`y==EQinstanceOrdNaturalwhereZ`compare`Z=EQZ`compare`S_=LTS_`compare`Z=GTSx`compare`Sy=x`compare`ymaybeSubtract::Natural->Natural->MaybeNaturala`maybeSubtract`Z=JustaSa`maybeSubtract`Sb=a`maybeSubtract`b_`maybeSubtract`_=NothinginstanceNumNaturalwhereZ+y=ySx+y=S(x+y)x-y=fromMaybe(error"Natural: (-)")(x`maybeSubtract`y)Z*y=ZSx*y=y+x*yabsx=xsignumZ=Zsignum(S_)=SZfromIntegerx|x<0=error"Natural: fromInteger"fromInteger0=ZfromIntegerx=S(fromInteger(x-1))instanceIntegralNaturalwhere-- Not the most efficient version, but efficiency isn't the point of this module. :)quotRemxy=ifx<ythen(0,x)elselet(q,r)=quotRem(x-y)yin(1+q,r)div=quotmod=remtoIntegerZ=0toInteger(Sx)=1+toIntegerxinstanceRealNaturalwheretoRational=toRational.toIntegerinstanceEnumNaturalwheresucc=SpredZ=error"Natural: pred 0"pred(Sa)=atoEnum=fromIntegralfromEnum=fromIntegralenumFromThenTofromthnto|from<=thn=gofrom(to`maybeSubtract`from)wheregofromNothing=[]gofrom(Justcount)=from:go(step+from)(count`maybeSubtract`step)step=thn-fromenumFromThenTofromthnto|otherwise=go(from+step)wheregofrom|from>=to+step=letnext=from-stepinnext:gonext|otherwise=[]step=from-thnenumFroma=enumFromThenToa(Sa)infinityenumFromThenab=enumFromThenToabinfinityenumFromToac=enumFromThenToa(Sa)c-- | The infinite natural number.infinity::Naturalinfinity=Sinfinity