Пример #1
0
 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,
             }),
     })
Пример #2
0
 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,
             }),
     })
Пример #3
0
 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)
     })
Пример #4
0
 def __add__(self, other):
     return match(self, {
         Nat.O: lambda: other,
         Nat.S: lambda n: n + Nat.S(other)
     })
Пример #5
0
 def to_int(self):
     return match(self, {
         Nat.O: lambda: 0,
         Nat.S: lambda n: 1 + n.to_int()
     })
Пример #6
0
 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)
     })
Пример #7
0
 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
     })
Пример #8
0
 def __repr__(self):
     return match(self, {
         BTree.leaf: lambda: "",
         BTree.branch: lambda l, r, d:
             repr(l) + "/" + repr(d) + "\\" + repr(r),
     })