Download code

Jump to: navigation, search

Back to Ternary_Logic_(Lisp)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

logic3.el

  1 (defmacro andor3-helper (val old new)
  2   (let ((first (eval (car new)))
  3         (rest (cdr new)))
  4     (cond ((not new) old)
  5 	  ((equal first val) (list 'andor3-helper val old rest))
  6 	  ((not (equal first (not val))) (list 'andor3-helper val first rest))
  7 	  ((not val)))))
  8 
  9 (defmacro and3 (&rest args)
 10   "Calculate 3-valued and.
 11 This function evaluates all arguments until one of them returns nil,
 12 in which case nil is returned and no further arguments are evaluated. 
 13 If no argument returns nil, the result of the last argument which did
 14 not return t, if any, is returned, otherwise t. All arguments are
 15 evaluated in this case."
 16   (list 'andor3-helper t t args))
 17 
 18 (defmacro or3 (&rest args)
 19   "Evaluate 3-valued or.
 20 This functions evaluates all arguments until one of them returns t, in
 21 which case t is returned and no further arguments are evaluated.  If
 22 no argument returns t, the result of the last non-nil argument, if
 23 any, is returned, otherwise nil."
 24   (list 'andor3-helper nil nil args))
 25 (defun not3 (arg)
 26   "Three-valued not.
 27 Returns t if arg is nil, and nil if arg is t. Otherwise, arg is
 28 returned unchanged."
 29   (or (not arg) (and (not (equal arg t)) arg)))
 30 (defmacro imply3 (arg1 arg2)
 31   "Three-valued implication.
 32 If arg1 is nil, imply3 evaluates to t, and arg2 is not evaluated.
 33 If arg1 is t, imply3 evaluates to arg2.
 34 If arg1 is neither nil nor t, imply3 evaluates to arg1 if arg2 is nil,
 35 and to t otherwise."
 36   (let ((val1 (eval arg1)))
 37     (cond ((not arg1) t)
 38           ((equal val1 t) arg2)
 39           ((not (eval arg2)) `',val1)
 40           (t t))))
 41 (defun unknown-p (arg)
 42   "Returns t if arg is neither nil nor t, nil otherwise."
 43   (and arg (not3 arg) t))
 44 (setq u 'u)
 45 (defun normalize3 (arg)
 46   "Replace any arg which is not t or nil with u"
 47   (if (unknown-p arg) u arg))
 48 (defun test-logic3 ()
 49   "Test the 3-values logic routines."
 50   (interactive)
 51   (let ((one 1)
 52         (two 2))
 53     (or (equal (mapcar (lambda (args)
 54                          (list args '=>
 55                                (eval (cons 'and3 args))
 56                                (eval (cons 'or3 args))))
 57                        '(() (nil) (1) (t)
 58                          (nil nil) (nil 1) (nil t)
 59                          (1 nil) (1 2) (1 t)
 60                          (t nil) (t 1) (t t)))
 61                '((nil => t nil)
 62                  ((nil) => nil nil)
 63                  ((1) => 1 1)
 64                  ((t) => t t)
 65                  ((nil nil) => nil nil)
 66                  ((nil 1) => nil 1)
 67                  ((nil t) => nil t)
 68                  ((1 nil) => nil 1)
 69                  ((1 2) => 2 2)
 70                  ((1 t) => 1 t)
 71                  ((t nil) => nil t)
 72                  ((t 1) => 1 t)
 73                  ((t t) => t t)))
 74         (error "error in and3 or or3"))
 75     (or (equal (mapcar 'not3 '(nil 1 t)) '(t 1 nil))
 76         (error "error in not3"))
 77     (or3 t (error "or3 evaluates after t"))
 78     (and3 nil (error "and3 evaluates after nil"))
 79     (or (and (not (unknown-p t)) (not (unknown-p nil)) (unknown-p 1))
 80         (error "error in unknown-p"))
 81     (or (and (equal (imply3 nil (error "imply3 evaluates after nil")) t)
 82              (equal (imply3 one nil) 1)
 83              (equal (imply3 'one nil) 'one)
 84              (equal (imply3 one two) t)
 85              (equal (imply3 one t) t)
 86              (equal (imply3 t nil) nil)
 87              (equal (imply3 t one) 1)
 88              (equal (imply3 t 'one) 'one)
 89              (equal (imply3 t t) t))
 90         (error "error in imply3"))
 91     (or (and (equal (normalize3 t) t)
 92              (equal (normalize3 nil) nil)
 93              (equal (normalize3 1) u)
 94              (equal (normalize3 u) u))
 95         (error "error in normalize3"))
 96     (let ((a 0))
 97       (and3 (setq a (+ 1 a)) (setq a (+ 2 a)))
 98       (or (equal a 3) (error "and3 evaluation error")))
 99     (let ((a 0))
100       (and3 (setq a (+ 1 a)) t (setq a (+ 2 a)))
101       (or (equal a 3) (error "and3 evaluation error")))
102     (let ((a 0))
103       (and3 (setq a (+ 1 a)) nil (setq a (+ 2 a)))
104       (or (equal a 1) (error "and3 evaluation error")))
105     (let ((a 0))
106       (or3 (setq a (+ 1 a)) (setq a (+ 2 a)))
107       (or (equal a 3) (error "or3 evaluation error")))
108     (let ((a 0))
109       (or3 (setq a (+ 1 a)) nil (setq a (+ 2 a)))
110       (or (equal a 3) (error "or3 evaluation error")))
111     (let ((a 0))
112       (or3 (setq a (+ 1 a)) t (setq a (+ 2 a)))
113       (or (equal a 1) (error "or3 evaluation error")))
114     (let ((a 0))
115       (imply3 (setq a (+ 1 a)) (setq a (+ 2 a)))
116       (or (equal a 3) (error "imply3 evaluation error")))
117     (let ((a 0))
118       (imply3 (setq a (+ 1 a)) t)
119       (or (equal a 1) (error "imply3 evaluation error"))))
120     (message "No error found in logic3.el"))


hijacker
hijacker
hijacker
hijacker