Skip to content

Commit 272388a

Browse files
committed
feat: add support to F and T const in expressions
They are not more considered arbitrary atomic propositions with multiple possible values.
1 parent 47e744f commit 272388a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/truth-table.lisp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
(defun propositionp (symbol)
1212
"Check if the given SYMBOL can be a proposition (letters)"
1313
(and (atom symbol)
14-
(not (valid-operatorp symbol))))
14+
(not (valid-operatorp symbol))
15+
(not (member (symbol-name symbol) '("T" "F") :test #'string-equal))))
1516

1617
(defun set-of-propositions (exp)
1718
"Given a propositional expression EXP return the list of
@@ -91,6 +92,14 @@
9192
(t nil)))
9293

9394

95+
(defun replace-tf (exp)
96+
(cond ((atom exp)
97+
(cond ((string-equal (symbol-name exp) "T") t)
98+
((string-equal (symbol-name exp) "F") nil)
99+
(t exp)))
100+
(t (cons (replace-tf (car exp))
101+
(replace-tf (cdr exp))))))
102+
94103
(defun eval-operations (exp-tree)
95104
"Generate all the truth-table cases and evaluated it based on EXP-TREE"
96105
(let ((cases (group-cases-to-propositions exp-tree)))
@@ -99,7 +108,7 @@
99108
(let ((prop (car pair))
100109
(value (cadr pair)))
101110
(nsubst value prop exp)))
102-
(eval exp)))
111+
(eval (replace-tf exp))))
103112
(let ((exps (stack-of-expressions exp-tree)))
104113
(loop for case in cases
105114
collect (append case

t/test-truth-table.lisp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@
3333
(ok (equal-expression '(~ (~ p))
3434
'p)
3535
"EQUAL EXPRESSION 2")))
36+
37+
(deftest truth-table-tests-with-false-and-true
38+
(testing "== Truth-table tests with F and T as constant!"
39+
(ok (equal (eval-expression '(^ p f))
40+
"FF")
41+
"CONTRADICTION: p ^ f")
42+
43+
(ok (equal (eval-expression '(v p t))
44+
"TT")
45+
"TAUTOLOGY: p v t")
46+
47+
(ok (equal (eval-expression (parse-logic "(~p v q <=> p => q) <=> t"))
48+
"TTTT")
49+
"TAUTOLOGY OF CONDITIONAL DEFINITION")))

0 commit comments

Comments
 (0)