120
правок
Изменения
→Целые числа
==Целые числа==
data Int = Plus Nat | Minus Nat deriving (Show,Read)
intZero = Plus Zero
intOne = Plus (Succ Zero)
intNegOne = Minus (Succ Zero)
intNeg :: Int -> Int
intNeg (Plus x) = Minus x
intNeg (Minus x) = Plus x
intCmp :: Int -> Int -> Tri
intCmp (Plus Zero) (Minus Zero) = EQ
intCmp (Minus Zero) (Plus Zero) = EQ
intCmp (Plus Zero) (Minus (Succ x)) = GT
intCmp (Minus Zero) (Plus (Succ x)) = LT
intCmp (Plus (Succ x)) (Minus Zero) = GT
intCmp (Minus (Succ x)) (Plus Zero) = LT
intCmp (Plus x) (Plus y) = natCmp x y
intCmp (Minus x) (Minus y) = natCmp y x
intEq :: Int -> Int -> Bool
intEq (Plus Zero) (Minus Zero) = True
intEq (Minus Zero) (Plus Zero) = True
intEq (Plus Zero) (Minus (Succ x)) = False
intEq (Minus Zero) (Plus (Succ x)) = False
intEq (Plus (Succ x)) (Minus Zero) = False
intEq (Minus (Succ x)) (Plus Zero) = False
intEq (Plus x) (Plus y) = natEq x y
intEq (Minus x) (Minus y) = natEq x y
intLt :: Int -> Int -> Bool
intLt (Plus Zero) (Minus Zero) = False
intLt (Minus Zero) (Plus Zero) = False
intLt (Plus Zero) (Minus (Succ x)) = False
intLt (Minus Zero) (Plus (Succ x)) = True
intLt (Plus (Succ x)) (Minus Zero) = False
intLt (Minus (Succ x)) (Plus Zero) = True
intLt (Plus x) (Plus y) = natLt x y
intLt (Minus x) (Minus y) = natLt y x
infixl 6 .+., .-.
(.+.) :: Int -> Int -> Int
(Plus m) .+. (Plus n) = Plus (m +. n)
(Minus m) .+. (Minus n) = Minus (m +. n)
(Plus (Succ m)) .+. (Minus (Succ n)) = (Plus m) .+. (Minus n)
(Minus (Succ m)) .+. (Plus (Succ n)) = (Plus m) .+. (Minus n)
x .+. (Plus Zero) = x
x .+. (Minus Zero) = x
(Plus Zero) .+. y = y
(Minus Zero) .+. y = y
(.-.) :: Int -> Int -> Int
n .-. m = n .+. (intNeg m)
infixl 7 .*.
(.*.) :: Int -> Int -> Int
(Plus m) .*. (Plus n) = Plus (m *. n)
(Minus m) .*. (Minus n) = Plus (m *. n)
(Plus m) .*. (Minus n) = Minus (m *. n)
(Minus m) .*. (Plus n) = Minus (m *. n)
==Рациональные числа==
==GCD==