I am having a hard time understanding the behavior of these lambda functions in python 3.10.0
I am trying to define the NOT
logical operator from lambda calculus (see, e.g., the definition on wikipedia
The following implementation is correct:
In [1]: TRUE = lambda a: lambda b: a
...: FALSE = lambda a: lambda b: b
...: NOT = lambda a: a(FALSE)(TRUE)
...: assert NOT(FALSE) == TRUE
However, when I try and do a literal substitution either for FALSE
or TRUE
, the code fails
In [2]: NOT1 = lambda a: a(FALSE)(lambda a: lambda b: a)
...: assert NOT1(FALSE) == TRUE
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[2], line 2
1 NOT1 = lambda a: a(FALSE)(lambda a: lambda b: a)
----> 2 assert NOT1(FALSE) == TRUE
AssertionError:
Can anybody explain me why this happens?
Python function ==
works by object identity. (Trying to implement it any other way ranges from "painfully inconsistent" to "plain impossible".) You have created another function with the same behavior as your NOT
function, but it is not the same function object, so ==
says they're not equal.