-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.lisp
executable file
·204 lines (179 loc) · 5.16 KB
/
logic.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
;;;; Hey, Emacs, this is a -*- Mode: Lisp; Syntax: Common-Lisp -*- file!
;;;;
;;;; Programming should be fun. Programs should be beautiful.
;;;; -- Paul Graham
;;;;
;;;; Name: logic.lisp
;;;;
;;;; Started: Sat Nov 27 01:39:46 2010
;;;; Modifications:
;;;;
;;;; Purpose:
;;;;
;;;;
;;;;
;;;; Calling Sequence:
;;;;
;;;;
;;;; Inputs:
;;;;
;;;; Outputs:
;;;;
;;;; Example:
;;;;
;;;; Notes:
;;;;
;;;;
(defpackage logic
(:use common-lisp)
(:export :=> :<=> :dual :print-truth-table :truth-table :xor))
(in-package logic)
;;;
;;; Haskell Craft section 3.1
;;;
;; (defun xor (p q)
;; (ccase p
;; ((t) (not q))
;; ((nil) q)))
;; (defun xor (p q)
;; (and (or p q)
;; (not (and p q))))
;; (defmacro xor (p q)
;; (let ((p1 (gensym))
;; (q1 (gensym)))
;; `(let ((,p1 ,p)
;; (,q1 ,q))
;; (and (or ,p1 ,q1)
;; (not (and ,p1 ,q1)))) ))
(defmacro xor (&rest args)
(case (length args)
(0 t) ;?
(1 nil)
(2 (let ((p1 (gensym))
(q1 (gensym)))
`(let ((,p1 ,(first args))
(,q1 ,(second args)))
(and (or ,p1 ,q1)
(not (and ,p1 ,q1)))) ))
(otherwise `(xor ,(first args) (xor ,@(rest args)))) ))
;;;
;;; Implication is asymmetric. It returns either T or the value of Q, which
;;; may be a generalized boolean value.
;;;
;; (defmacro => (p q)
;; `(or (not ,p) ,q))
;; (defmacro => (p q)
;; `(if ,p ,q t))
;;;
;;; Left-associative
;;;
(defmacro -> (&rest args)
(destructuring-bind (p q . rest) args
(if (null rest)
`(=> ,p ,q)
(let ((ps (butlast args))
(q (last args)))
`(or (not (-> ,@ps)) ,@q)))) )
;;;
;;; Right-associative
;;;
(defmacro => (&rest args)
(destructuring-bind (p q . rest) args
(if (null rest)
`(or (not ,p) ,q)
(let ((ps (butlast args))
(q (last args)))
`(=> (and ,@ps) ,@q)))) )
(defmacro => (&rest args)
(case (length args)
((0 1) (error "?!"))
(2 `(or (not ,(first args)) ,(second args)))
(otherwise `(=> ,(first args) (=> ,@(rest args)))) ))
;; (defmacro <=> (p q)
;; (let ((p1 (gensym))
;; (q1 (gensym)))
;; `(let ((,p1 ,p)
;; (,q1 ,q))
;; (and (=> ,p1 ,q1)
;; (=> ,q1 ,p1)))) )
(defmacro <=> (&rest args)
(case (length args)
(0 nil) ;?
(1 t)
(2 (let ((p1 (gensym))
(q1 (gensym)))
`(let ((,p1 ,(first args))
(,q1 ,(second args)))
(or (and ,p1 ,q1)
(not (or ,q1 ,p1)))) ))
(otherwise `(<=> ,(first args) (<=> ,@(rest args)))) ))
;; (defmacro <=> (&rest args)
;; (case (length args)
;; (0 nil) ;?
;; (1 t)
;; (2 (let ((p1 (gensym))
;; (q1 (gensym)))
;; `(let ((,p1 ,(first args))
;; (,q1 ,(second args)))
;; (and (=> ,p1 ,q1)
;; (=> ,q1 ,p1)))) )
;; (otherwise `(<=> ,(first args) (<=> ,(second args) ,@(cddr args)))) ))
;;;
;;; Does this make sense? No. It's wrong!
;;;
;; (defmacro xor (&rest args)
;; `(and (or ,@args) (not (and ,@args))))
(defun truth-permutations (n)
(if (= n 1)
(list (list t) (list nil))
(let ((result (truth-permutations (1- n))))
(append (mapcar #'(lambda (l) (cons t l)) result)
(mapcar #'(lambda (l) (cons nil l)) result)))) )
(defmacro truth-table ((&rest vars) &body body)
(let ((row (gensym)))
`(mapcar #'(lambda (,row) (destructuring-bind ,vars ,row ,@body)) ',(truth-permutations (length vars)))) )
;; (defmacro truth-table ((&rest vars) &body body)
;; `(list ,@(mapcar #'(lambda (vals) `(let ,(mapcar #'list vars vals) ,@body)) (truth-permutations (length vars)))))
;(mapcar #'(lambda (vals) `(let ,(mapcar #'list '(p q r) vals))) (truth-permutations 3))
(defun print-table (labels inputs outputs)
(format t "~{~5A ~} ~%" labels)
(loop for input in inputs
for output in outputs
do (format t "~{~5A ~} ~A~%" input output)))
(defmacro print-truth-table ((&rest vars) &body body)
(let ((row (gensym))
(inputs (truth-permutations (length vars)))
(outputs (gensym)))
`(let ((,outputs (mapcar #'(lambda (,row) (destructuring-bind ,vars ,row ,@body)) ',inputs)))
(print-table '(,@vars ,@body) ',inputs ,outputs))))
;;;
;;; Non short-circuiting &/|
;;;
(defun & (&rest args)
(every #'identity args))
;;;
;;; D'oh! What a name!
;;;
(defun \| (&rest args)
(some #'identity args))
;;;
;;; Do we really want a macro here?
;;;
;;; Instead:
;;; (dual '(and p (and (not q) (not r))))
;;; (dual (dual '(and p (and (not q) (not r)))))
;;;
(defmacro dual (body)
(rebuild-dual body))
(defun rebuild-dual (expression)
(cond ((atom expression) (replace-dual-atom expression))
((endp (rest expression)) (cons (rebuild-dual (first expression)) '()))
(t (cons (rebuild-dual (first expression)) (rebuild-dual (rest expression)))) ))
(defun replace-dual-atom (obj)
(case obj
(and 'or)
(or 'and)
((t) nil)
((nil) t)
((xor => <=>) (error "The dual is only defined in terms of AND, OR, and NOT."))
(otherwise obj)))