main/
defect_program.py(1.2 KB) #!/usr/bin/env python3
"""
Small program built from function-defects.
Program:
square_plus_one(n) = add (mul n n) 1
Everything is expressed through the current function-defect layer in church.py.
"""
from church import add, mul, normal_form, succ
def apply2(fn, a, b):
"""Apply a curried binary function-defect to two arguments."""
return fn.apply(a).apply(b)
def square(n):
"""square(n) = mul n n"""
return apply2(mul(), n, n)
def square_plus_one(n):
"""square_plus_one(n) = add (mul n n) 1"""
return apply2(add(), square(n), normal_form(1))
def demo() -> None:
two = normal_form(2)
three = normal_form(3)
print("=" * 60)
print("DEFECT PROGRAM")
print("=" * 60)
print()
print("Program:")
print(" square_plus_one(n) = add (mul n n) 1")
print()
print(f" input 2 = {two}")
print(f" square(2) = {square(two)}")
print(f" square_plus_one(2) = {square_plus_one(two)}")
print()
print(f" input 3 = {three}")
print(f" square(3) = {square(three)}")
print(f" square_plus_one(3) = {square_plus_one(three)}")
print()
print("Extra:")
print(f" succ(square_plus_one(2)) = {succ().apply(square_plus_one(two))}")
if __name__ == "__main__":
demo()