46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import operator
|
|
import fractions
|
|
|
|
from typesystem import Integer, Rational, Function, Or
|
|
|
|
types = {
|
|
'+': Or(
|
|
Function([Integer(), Integer()], Integer()),
|
|
Function([Integer(), Rational()], Rational()),
|
|
Function([Rational(), Integer()], Rational()),
|
|
Function([Rational(), Rational()], Rational())
|
|
),
|
|
'-': Or(
|
|
Function([Integer()], Integer()),
|
|
Function([Rational()], Rational()),
|
|
Function([Integer(), Integer()], Integer()),
|
|
Function([Integer(), Rational()], Rational()),
|
|
Function([Rational(), Integer()], Rational()),
|
|
Function([Rational(), Rational()], Rational())
|
|
),
|
|
'*': Or(
|
|
Function([Integer(), Integer()], Integer()),
|
|
Function([Integer(), Rational()], Rational()),
|
|
Function([Rational(), Integer()], Rational()),
|
|
Function([Rational(), Rational()], Rational())
|
|
),
|
|
'/': Or(
|
|
Function([Integer(), Integer()], Rational()),
|
|
Function([Integer(), Rational()], Rational()),
|
|
Function([Rational(), Integer()], Rational()),
|
|
Function([Rational(), Rational()], Rational())
|
|
)
|
|
}
|
|
|
|
def minus(first, second = None):
|
|
if second is None:
|
|
return -first
|
|
else:
|
|
return first-second
|
|
|
|
impls = {
|
|
'+': operator.add,
|
|
'-': minus,
|
|
'*': operator.mul,
|
|
'/': fractions.Fraction
|
|
}
|