Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, UnboxedTuples #-}{-# 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.-------------------------------------------------------------------------------moduleGHC.Num(moduleGHC.Num,moduleGHC.Integer,moduleGHC.Natural)where#include "MachDeps.h"importGHC.BaseimportGHC.IntegerimportGHC.Natural#if !defined(MIN_VERSION_integer_gmp)import{-# SOURCE#-}GHC.Exception.Type(underflowException)#endifinfixl7*infixl6+,-default()-- Double isn't available yet,-- and we shouldn't be using defaults anyway-- | Basic numeric class.---- The Haskell Report defines no laws for 'Num'. However, '(+)' and '(*)' are-- customarily expected to define a ring and have the following properties:---- [__Associativity of (+)__]: @(x + y) + z@ = @x + (y + z)@-- [__Commutativity of (+)__]: @x + y@ = @y + x@-- [__@fromInteger 0@ is the additive identity__]: @x + fromInteger 0@ = @x@-- [__'negate' gives the additive inverse__]: @x + negate x@ = @fromInteger 0@-- [__Associativity of (*)__]: @(x * y) * z@ = @x * (y * z)@-- [__@fromInteger 1@ is the multiplicative identity__]:-- @x * fromInteger 1@ = @x@ and @fromInteger 1 * x@ = @x@-- [__Distributivity of (*) with respect to (+)__]:-- @a * (b + c)@ = @(a * b) + (a * c)@ and @(b + c) * a@ = @(b * a) + (c * a)@---- Note that it /isn't/ customarily expected that a type instance of both 'Num'-- and 'Ord' implement an ordered ring. Indeed, in 'base' only 'Integer' and-- 'Rational' do.classNumawhere{-# MINIMAL(+),(*),abs,signum,fromInteger,(negate|(-))#-}(+),(-),(*)::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(-)#-}{-# INLINEnegate#-}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.{-# INLINEsubtract#-}subtract::(Numa)=>a->a->asubtractxy=y-x-- | @since 2.01instanceNumIntwhereI#x+I#y=I#(x+#y)I#x-I#y=I#(x-#y)negate(I#x)=I#(negateInt#x)I#x*I#y=I#(x*#y)absn=ifn`geInt`0thennelsenegatensignumn|n`ltInt`0=negate1|n`eqInt`0=0|otherwise=1{-# INLINEfromInteger#-}-- Just to be sure!fromIntegeri=I#(integerToInti)-- | @since 2.01instanceNumWordwhere(W#x#)+(W#y#)=W#(x#`plusWord#`y#)(W#x#)-(W#y#)=W#(x#`minusWord#`y#)(W#x#)*(W#y#)=W#(x#`timesWord#`y#)negate(W#x#)=W#(int2Word#(negateInt#(word2Int#x#)))absx=xsignum0=0signum_=1fromIntegeri=W#(integerToWordi)-- | @since 2.01instanceNumIntegerwhere(+)=plusInteger(-)=minusInteger(*)=timesIntegernegate=negateIntegerfromIntegerx=xabs=absIntegersignum=signumInteger#if defined(MIN_VERSION_integer_gmp)-- | Note that `Natural`'s 'Num' instance isn't a ring: no element but 0 has an-- additive inverse. It is a semiring though.---- @since 4.8.0.0instanceNumNaturalwhere(+)=plusNatural(-)=minusNatural(*)=timesNaturalnegate=negateNaturalfromInteger=naturalFromIntegerabs=idsignum=signumNatural#else-- | Note that `Natural`'s 'Num' instance isn't a ring: no element but 0 has an-- additive inverse. It is a semiring though.---- @since 4.8.0.0instanceNumNaturalwhereNaturaln+Naturalm=Natural(n+m){-# INLINE(+)#-}Naturaln*Naturalm=Natural(n*m){-# INLINE(*)#-}Naturaln-Naturalm|m>n=raise#underflowException|otherwise=Natural(n-m){-# INLINE(-)#-}abs(Naturaln)=Naturaln{-# INLINEabs#-}signum(Naturaln)=Natural(signumn){-# INLINEsignum#-}fromInteger=naturalFromInteger{-# INLINEfromInteger#-}#endif
[8]ページ先頭