:
data BigDecimal: Type where
BDZ: BigDecimal
BD: (n : Integer) -> {auto prf: Not (n `mod` 10 = 0)} -> (mag: Integer) -> BigDecimal
prf , n 10 ( , 0), :
- 0
BDZ - n * 10 mag
BD n mag:
BD (n * 10) (mag - 1) , n * 10 10, n 10, BD (n / 10) (mag + 1) .
: , Integer , n `mod` 10 BD, - , , BD 1 3 .
, Nat ural numbers, Data.Nat.DivMod, :
-- Local Variables:
-- idris-packages: ("contrib")
-- End:
import Data.Nat.DivMod
import Data.So
%default total
hasRemainder : DivMod n q -> Bool
hasRemainder (MkDivMod quotient remainder remainderSmall) = remainder /= 0
NotDivides : (q : Nat) -> {auto prf: So (q /= 0)} -> Nat -> Type
NotDivides Z {prf = Oh} n impossible
NotDivides (S q) n = So (hasRemainder (divMod n q))
, BigDecimal Nat:
data Sign = Positive | Negative
data BigNatimal: Type where
BNZ: BigNatimal
BN: Sign -> (n : Nat) -> {auto prf: 10 `NotDivides` n} -> (mag: Integer) -> BigNatimal
BigNatimal; 1000:
bn : BigNatimal
bn = BN Positive 1 3
EDIT 2: Nat BigNatimal s. , fromNat' .
tryDivide : (q : Nat) -> {auto prf : So (q /= 0)} -> (n : Nat) -> Either (q `NotDivides` n) (DPair _ (\n' => n' * q = n))
tryDivide Z {prf = Oh} n impossible
tryDivide (S q) n with (divMod n q)
tryDivide _ (quot * (S q)) | MkDivMod quot Z _ = Right (quot ** Refl)
tryDivide _ (S rem + quot * (S q)) | MkDivMod quot (S rem) _ = Left Oh
fromNat' : (n : Nat) -> {auto prf: So (n /= 0)} -> DPair BigNatimal NonZero
fromNat' Z {prf = Oh} impossible
fromNat' (S n) {prf = Oh} with (tryDivide 10 (S n))
fromNat' (S n) | Left prf = (BN Positive (S n) {prf = prf} 1 ** ())
fromNat' _ | Right (Z ** Refl) impossible
fromNat' _ | Right ((S n') ** Refl) with (fromNat' (S n'))
fromNat' _ | Right _ | (BNZ ** nonZero) = absurd nonZero
fromNat' _ | Right _ | ((BN sign k {prf} mag) ** _) = (BN sign k {prf = prf} (mag + 1) ** ())
fromNat : Nat -> BigNatimal
fromNat Z = BNZ
fromNat (S n) = fst (fromNat' (S n))