truth table: test_truth_table_tests¶
requirements¶
I want to write a program that makes the tests in test_truth_table.py
pass without looking at them
red: make it fail¶
I close
test_truth_table.py
then delete all the tests in
truth_table.py
and the terminal shows 20 failures, I start with the last oneAttributeError: module 'src.truth_table' has no attribute 'tautology'
green: make it pass¶
I add the name
tautology
the terminal shows NameError
NameError: name 'tautology' is not defined
I point the name to None
tautology = None
the terminal shows TypeError
TypeError: 'NoneType' object is not callable
I make it a function
def tautology(): return None
the terminal shows TypeError
TypeError: tautology() takes 0 positional arguments but 2 were given
I add input arguments to the function definition
def tautology(x, y): return None
the terminal shows AssertionError
AssertionError: None is not true
I change None to True in the return statement
def tautology(x, y): return True
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'project_second'
I add the function
def project_second(x, y): return True
the terminal shows AssertionError
AssertionError: True is not false
I change the return statement
def project_second(x, y): return False
the terminal shows AssertionError
AssertionError: False is not true
I want to see the difference between the inputs and the expected output
def project_second(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
y
is False, I removex
def project_second(x, y): return y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'project_first'
I add the function
def project_first(x, y): return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
x
is False, I removey
def project_first(x, y): return x
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'negate_second'
I add the function
def negate_second(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True) is not false
I add an if statement
def negate_second(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
I add another if statement
def negate_second(x, y): if (x, y) == (True, True): return False if (x, y) == (False, True): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'negate_first'
I add a definition for it
def negate_first(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True)
I add an if statement
def negate_first(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
I add an if statement
def negate_first(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'material_non_implication'
I add the function
def material_non_implication(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True) is not false
I add an if statement
def material_non_implication(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
I add another if statement
def material_non_implication(x, y): if (x, y) == (True, True): return False if (x, y) == (False, True): return False return x, y
the terminal shows AssertionError
AssertionError: (False, False) is not false
I add an if statement
def material_non_implication(x, y): if (x, y) == (True, True): return False if (x, y) == (False, True): return False if (x, y) == (False, False): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'material_implication'
I add the function
def material_implication(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
I add an if statement
def material_implication(x, y): if (x, y) == (True, False): return False return x, y
I get AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_nor'
I add the function
def logical_nor(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True) is not false
I add an if statement
def logical_nor(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
I add another if statement
def logical_nor(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
I add an if statement
def logical_nor(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False if (x, y) == (False, True): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_nand'. Did you mean: 'logical_nor'?
I add the function
def logical_nand(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True) is not false
I add an if statement
def logical_nand(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_equality'
I add the function
def logical_equality(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
I add an if statement
def logical_equality(x, y): if (x, y) == (True, False): return False return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
I add another if statement
def logical_equality(x, y): if (x, y) == (True, False): return False if (x, y) == (False, True): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_disjunction'
I add the function
def logical_disjunction(x, y): return x, y
the terminal shows AssertionError
AssertionError: (False, False) is not false
I add an if statement
def logical_disjunction(x, y): if (x, y) == (False, False): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_conjunction'. Did you mean: 'logical_disjunction'?
I add the function
def logical_conjunction(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
I add an if statement
def logical_conjunction(x, y): if (x, y) == (True, False): return False return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
I add an if statement
def logical_conjunction(x, y): if (x, y) == (True, False): return False if (x, y) == (False, True): return False return x, y
the terminal shows AssertionError
AssertionError: (False, False) is not false
I add another if statement
def logical_conjunction(x, y): if (x, y) == (True, False): return False if (x, y) == (False, True): return False if (x, y) == (False, False): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'exclusive_disjunction
I add a function for it
def exclusive_disjunction(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True) is not false
I add an if statement
def exclusive_disjunction(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AssertionError
AssertionError: (False, False) is not false
I add another if statement
def exclusive_disjunction(x, y): if (x, y) == (True, True): return False if (x, y) == (False, False): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'converse_non_implication'. Did you mean: 'material_non_implication'?
I add the definition for the function
def converse_non_implication(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True) is not false
I add an if statement
def converse_non_implication(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
I add another if statement
def converse_non_implication(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False return x, y
the terminal shows AssertionError
AssertionError: (False, False) is not false
I add an if statement
def converse_non_implication(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False if (x, y) == (False, False): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'converse_implication'. Did you mean: 'converse_non_implication'?
I add the function
def converse_implication(x, y): return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
I add an if statement
def converse_implication(x, y): if (x, y) == (False, True): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'contradiction'
I add the function
def contradiction(x, y): return x, y
the terminal shows AssertionError
AssertionError: (True, True) is not false
I add an if statement
def contradiction(x, y): if (x, y) == (True, True): return False return x, y
the terminal shows AssertionError
AssertionError: (True, False) is not false
I add another if statement
def contradiction(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False return x, y
the terminal shows AssertionError
AssertionError: (False, True) is not false
I add an if statement
def contradiction(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False if (x, y) == (False, True): return False return x, y
the terminal shows AssertionError
AssertionError: (False, False) is not false
I add an if statement
def contradiction(x, y): if (x, y) == (True, True): return False if (x, y) == (True, False): return False if (x, y) == (False, True): return False if (x, y) == (False, False): return False return x, y
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_negation'. Did you mean: 'logical_conjunction'?
I add the function
def logical_negation(x, y): return x, y
the terminal shows TypeError
TypeError: logical_negation() missing 1 required positional argument: 'y'
I change the signature
def logical_negation(x): return x
the terminal shows AssertionError
AssertionError: True is not false
I add “not” to the return statement
def logical_negation(x): return not x
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_identity'. Did you mean: 'logical_equality'?
I add a function
def logical_identity(x): return not x
the terminal shows AssertionError
AssertionError: False is not true
I remove “not”
def logical_identity(x): return x
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_true'. Did you mean: 'logical_nand'?
I add the function
def logical_true(x): return x
the terminal shows TypeError
TypeError: logical_true() missing 1 required positional argument: 'x'
I remove the input parameter and change the return statement
def logical_true(): return None
the terminal shows AssertionError
AssertionError: None is not true
I change None to True in the return statement
def logical_true(): return True
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_false'. Did you mean: 'logical_nand'?
I add the function
def logical_false(): return False
the terminal shows green!
refactor: make it better¶
I can refactor the functions to make them simpler since all the tests are passing
logical_false, logical_true, logical_identity and logical_negation are already simple
contradiction returns False in 4 cases, with 2 inputs there are only 4 cases. I add a return statement
def contradiction(x, y): return False if (x, y) == (True, True): return False if (x, y) == (True, False): return False if (x, y) == (False, True): return False if (x, y) == (False, False): return False return x, y
the test is still green. I remove the other lines
def contradiction(x, y): return False
converse_implication returns False in only one case, I return the logical negation of the if statement, for the 3 cases that return True
def converse_implication(x, y): return (x, y) != (False, True) if (x, y) == (False, True): return False return x, y
still green. I remove the other statements
def converse_implication(x, y): return (x, y) != (False, True)
converse_non_implication has only one case that returns True, it is the missing case. I add a return statement for it
def converse_non_implication(x, y): return (x, y) == (False, True) if (x, y) == (True, True): return False if (x, y) == (True, False): return False if (x, y) == (False, False): return False return x, y
the terminal still shows green and I remove the other lines
def converse_non_implication(x, y): return (x, y) == (False, True)
exclusive_disjunction has two if statements I can put them together as one
def exclusive_disjunction(x, y): if (x, y) == (True, True) or (x, y) == (False, False): return False if (x, y) == (True, True): return False if (x, y) == (False, False): return False return x, y
the test is still green. I remove the other if statements then return the logical negation
def exclusive_disjunction(x, y): return (x, y) != (True, True) and (x, y) != (False, False) if (x, y) == (True, True) or (x, y) == (False, False): return False return x, y
the test is still green, I remove the other lines
def exclusive_disjunction(x, y): return (x, y) != (True, True) and (x, y) != (False, False)
logical_conjunction only has one case that returns True
def logical_conjunction(x, y): return (x, y) == (True, True) if (x, y) == (True, False): return False if (x, y) == (False, True): return False if (x, y) == (False, False): return False return x, y
still green, I remove the other statements
def logical_conjunction(x, y): return (x, y) == (True, True)
logical_disjunction only has one case that returns False
def logical_disjunction(x, y): return (x, y) != (False, False)
logical_equality has two if statements, I use what I know from exclusive_disjunction
def logical_equality(x, y): return (x, y) != (True, False) and (x, y) != (False, True)
the terminal still shows green
logical_nand only has one case that returns False
def logical_nand(x, y): return (x, y) != (True, True)
still green
logical_nor only has one case that returns True
def logical_nor(x, y): return (x, y) == (False, False)
material_implication has only one case that returns False
def material_non_implication(x, y): return (x, y) == (True, False)
material_non_implication has 3 cases that return False
def material_implication(x, y): return (x, y) != (True, False)
x
is True in both cases of negate_first that return False, I add an if statement to show thisdef negate_first(x, y): if x == True: return False if (x, y) == (True, True): return False if (x, y) == (True, False): return False return x, y
the test is still green. I add a return statement
def negate_first(x, y): return x != True if x == True: return False return x, y
still green, I remove the other statements
def negate_first(x, y): return x != True
y
is True in the 2 cases that return False in negate_second. I add a return statement like the one from negate_firstdef negate_second(x, y): return y != True
still green
project_second, project_first and tautology are already simple
I change the return statement in negate_second
def negate_second(x, y): return not y == True return y != True
when
not y
is True it means the statement will beTrue == True
which is a duplication. I remove the second part of the statementdef negate_second(x, y): return not y
the test is still green
I do the same thing with negate_first
def negate_first(x, y): return not x
still green
I use this with material_non_implication
def material_non_implication(x, y): return x and not y
the terminal shows all tests are still passing
I try it with material_implication
def material_implication(x, y): return not x and y return (x, y) != (True, False)
the terminal shows AssertionError
AssertionError: False is not true
def material_implication(x, y): return not x or y
the test is green again, I will use “or” the next time I see
!=
in these testsI do it with logical_nor
def logical_nor(x, y): return not x and not y
then rewrite the return statement in terms of “not” since it happens 2 times
def logical_nor(x, y): return not x not or not y return not x and not y
the terminal shows SyntaxError
SyntaxError: invalid syntax
I comment the line out then factor out “not”
def logical_nor(x, y): return not (x or y) # return not x not or not y return not x and not y
the terminal still shows green. I remove the other statements
def logical_nor(x, y): return not (x or y)
I add a return statement for logical_nand
def logical_nand(x, y): return not x or not y
still green and it factors out to
def logical_nand(x, y): return not (x and y)
-
def logical_equality(x, y): return (not x or y) and (x or not y) return (x, y) != (True, False) and (x, y) != (False, True)
the test is still green, I remove the other statement
-
def logical_disjunction(x, y): return x or y
-
def logical_conjunction(x, y): return x and y
-
def exclusive_disjunction(x, y): return (not x or not y) and (x or y) return (x, y) != (True, True) and (x, y) != (False, False)
still green, I can factor out “not” from the first part of the statement
def exclusive_disjunction(x, y): return not (x and y) and (x or y) return (not x or not y) and (x or y)
the test is still green. I remove the other statement
-
def converse_non_implication(x, y): return not x and y
-
def converse_implication(x, y): return x or not y
all the tests are still passing
review¶
I ran tests using booleans which can be True or False for the operations of the Truth Table from Mathematics
there are 2 nullary operations, they do not take input and are constant
Logical False always returns False
Logical True always returns True
there are 2 unary operations, they take one input
Logical Identity returns its input
Logical Negation returns the negation of its input
there are 16 binary operations, they each take 2 inputs, in this case I named the second input
y
and the first onex
Contradiction always returns False
Converse Implication returns
x or not y
Converse NonImplication returns
not x and y
Exclusive Disjunction returns
not (x and y) and (x or y)
Logical Conjunction returns
x and y
Logical Disjunction returns
x or y
Logical Equality returns
(not x or y) and (x or not y)
Logical NAND returns
not (x and y)
Logical NOR returns
not (x or y)
Material Implication returns
not x or y
Material NonImplication returns
x and not y
Negate First always returns
not x
Negate Second always returns
not y
Project First always returns
x
Project Second always returns
y
Would you like to test lists?