Name: Anonymous 2015-03-06 3:04
Example usage:
If you're wondering about the denumeration scheme, it's simply assigning to each number a list of binary numbers. These binary numbers correspond to the horn clause for them, and that's how the logic sentence is produced.
E.g.
Variables are conveniently named pn
Anyway, enjoy the code lisp weenies.
> (logic-from-list (doit 5))
"p0^p1" ; This is the 5th logic sentence in my denumeration
If you're wondering about the denumeration scheme, it's simply assigning to each number a list of binary numbers. These binary numbers correspond to the horn clause for them, and that's how the logic sentence is produced.
E.g.
> (doit 17)
(0 0 1 1
> (logic-from-list *)
"~p0^~p1^p2^p3"
Variables are conveniently named pn
Anyway, enjoy the code lisp weenies.
(defun new-variable (prefix)
(let ((counter -1))
(lambda ()
(format nil "~A~D" prefix (incf counter)))))
(defun logic-from-list (list)
(let ((variable (new-variable "p")))
(reduce (lambda (x y)
(concatenate 'string x "^" y))
(loop for x in list
append (list (concatenate 'string
(if (zerop x)
"~"
"")
(funcall variable)))))))
(defun complement-num (x)
(if (eq x 0) 1 0))
(defun how-many-vars (number)
(if (< number 2)
1
(do ((i 0 (1+ i)))
((> (expt 2 i) (+ number 2))
(1- i)))))
(defun f (vars id)
"6 vars: id from 0 to 2^5 is normal
from 2^5 to 2^6 is complementary of 2^6 - i"
(if (>= id (expt 2 (1- vars)))
(mapcar #'complement-num
(f vars (- (expt 2 vars) id 1)))
(let ((*print-base* 2))
(let ((result
(loop for x across (write-to-string id)
collect (if (eq x #\0) 0 1))))
(if (> vars (length result))
(append (make-list (- vars (length result))
:initial-element 0)
result)
result)))))
(defun doit (id)
(let ((vars (how-many-vars id)))
(f vars (- id (expt 2 vars) -2))))
(defun doit-2 (values)
(let ((vars (log (length values) 2)))
(when (zerop (nth-value 1 (floor vars)))
(if (every #'zerop values)
(concatenate 'string
"~("
(doit-2 (make-list (length values)
:initial-element 1))
")")
(reduce (lambda (x y)
(concatenate 'string "(" x ")v(" y ")"))
(loop for i in (reverse values)
for j from 0
if (not (zerop i)) collect (logic-from-list
(f (floor vars) j))))))))