def __lt__(self, other): return match(other, { Nat.O: lambda: False, Nat.S: lambda no: match(self, { Nat.O: lambda: True, Nat.S: lambda ns: ns < no, }), })
def __sub__(self, other): return match(self, { Nat.O: lambda: self, Nat.S: lambda ns: match(other, { Nat.O: lambda: self, Nat.S: lambda no: ns - no, }), })
def add(self, data): return match(self, { BTree.leaf: lambda: BTree.branch(BTree.leaf(), BTree.leaf(), data), BTree.branch: lambda l, r, d: BTree.branch(l.add(data), r, d) if data <= d \ else BTree.branch(l, r.add(data), d) })
def __add__(self, other): return match(self, { Nat.O: lambda: other, Nat.S: lambda n: n + Nat.S(other) })
def to_int(self): return match(self, { Nat.O: lambda: 0, Nat.S: lambda n: 1 + n.to_int() })
def __pow__(self, other): return match(other, { Nat.O: lambda: Nat.S(Nat.O()), # forall x, x ** 0 = 1 Nat.S: lambda no: self * (self ** no), # x ** y = x * x ** (y - 1) })
def __mul__(self, other): return match(self, { Nat.O: lambda: Nat.O(), Nat.S: lambda ns: other + (other * ns), # x * y = y + (x - 1) * y })
def __repr__(self): return match(self, { BTree.leaf: lambda: "", BTree.branch: lambda l, r, d: repr(l) + "/" + repr(d) + "\\" + repr(r), })