Guest User

Pure Implicational Calculus in Lisp

a guest
Nov 20th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 5.02 KB | None | 0 0
  1. ;;; I will implement an assistent to develop proofs in
  2. ;;; Pure Positive Implicational Propositional Logic
  3.  
  4. ;;; 1. AXIOMS, due to Jan Lukasiewicz
  5. ;;; For any well-formed implicational formulae P,Q,R:
  6. ;;;
  7. ;;; P1) (P => (Q => P))
  8. ;;; P2) ((P => (Q => R)) => ((P => Q) => (P => R)))
  9.  
  10. ;;; 2. WELL-FORMED FORMULAE (wff)
  11. ;;; Any Lisp symbol will be considered an atomic formula
  12. ;;; eg: propositional variable
  13. ;;;
  14. ;;; W1) Any propositional variable is a wff
  15. ;;; W2) given P and Q well-formed-formulae, (P => Q) is a wff
  16.  
  17. ;;; 3. RULES OF INFERENCE
  18. ;;;
  19. ;;;  I) Substitutivity, for any theorem T containing a propositional
  20. ;;;     variable A, the result of swapping every occurence of A for a
  21. ;;;    given wff Q, will be a Theorem T'
  22. ;;; II) MODUS PONENS (Detachment): for any two previus proved theorems
  23. ;;;     in the following form:
  24. ;;;
  25. ;;;      H        "Minor premisse",
  26. ;;;     (H => C)  "Major premisse", C is itself alone, a theorem.
  27.  
  28. ;;; Predicate to verify if a proposition is a well-formed formula
  29. (defun wff-p (prop)
  30.   (or (symbolp prop)
  31.       (and (listp prop)
  32.        (= (length prop) 3)
  33.        (wff-p (first prop))
  34.        (eq (second prop) '=>)
  35.        (wff-p (third prop)))))
  36.  
  37. ;;; Function to check if two proposition are the same
  38. (defun prop-equal (p q)
  39.   (if (or (symbolp p)
  40.       (symbolp q))
  41.       (eq p q)
  42.     (and (prop-equal (first p)
  43.              (first q))
  44.      (eq (second p)
  45.          (second q))
  46.      (prop-equal (third p)
  47.              (third q)))))
  48.  
  49. ;;;Macro to apply substitutivity in Axiom P1
  50. (defmacro axiom-1 (p q)
  51.   `'(,(if (wff-p p) p  (cadr (macroexpand p)))
  52.      => (,(if (wff-p q) q  (cadr (macroexpand q)))
  53.      => ,(if (wff-p p) p (cadr (macroexpand p)))
  54.      )))
  55.  
  56. ;;;Macro to apply substitutivity in Axiom P2
  57. (defmacro axiom-2 (p q r)
  58.   `'((,(if (wff-p p) p (cadr (macroexpand p)))
  59.        => ( ,(if (wff-p q) q (cadr (macroexpand q)))
  60.         => ,(if (wff-p r) r (cadr (macroexpand r)))
  61.         )) => ((,(if (wff-p p) p (cadr (macroexpand p)))
  62.             => ,(if (wff-p q) q (cadr (macroexpand q)))
  63.             ) => (,(if (wff-p p) p (cadr (macroexpand p)))
  64.               => ,(if (wff-p r) r (cadr (macroexpand r)))
  65.               ))))
  66.  
  67. ;;;Fuction to apply Substitutivity
  68. (defun sb (sub-lst prop)
  69.   (cond ((consp prop)
  70.      (cons (sb sub-lst (first prop))
  71.            (sb sub-lst (cdr prop))))
  72.     ((symbolp prop)
  73.      (if (assoc prop sub-lst)
  74.          (cadr (assoc prop sub-lst))
  75.        prop))
  76.     (t prop)))
  77.  
  78. ;;;Function to apply MODUS PONENS
  79. (defun mp (minor major)
  80.   (if (prop-equal minor (first major))
  81.       (third major)
  82.       nil))
  83.  
  84. ;;;Struct to represent a single line of a proof
  85. (defstruct line
  86.   formula
  87.   (follows-from nil))
  88.  
  89. ;;;Struct to represent a proof
  90. (defstruct proof
  91.   (name "")
  92.   (lines nil))
  93.  
  94. ;;;Function to add a line into a proof
  95. (defun add-line (line proof)
  96.   (setf (proof-lines proof)
  97.     (append (proof-lines proof) (list line))))
  98.  
  99. ;;;Return the formula at line n in a proof
  100. (defun proof-line (n proof)
  101.   (line-formula (nth n (proof-lines proof))))
  102.  
  103. ;;;Function to display the lines of a proof
  104. (defun show-proof (proof)
  105.   (let ((n -1))
  106.     (mapc #'(lambda (line)
  107.           (format t "|~2D| ~A ~65T[~A]"
  108.               (incf n)
  109.               (line-formula line)
  110.               (line-follows-from line))
  111.           (terpri))
  112.       (proof-lines proof)))
  113.   nil)
  114.  
  115. ;;;Showing Peirce's Law is not provable...
  116.  
  117. (defparameter *domain* '(1 2 3))
  118.  
  119. (defparameter *interpretation*
  120.   '( ((1 1) 1)
  121.      ((1 2) 2)
  122.      ((1 3) 3)
  123.      ((2 1) 1)
  124.      ((2 2) 1)
  125.      ((2 3) 3)
  126.      ((3 1) 1)
  127.      ((3 2) 1)
  128.      ((3 3) 1)))
  129.  
  130. (defun interpret (interpretation proposition)
  131.   (if (atom proposition) proposition
  132.     (cadr (assoc (list
  133.           (interpret interpretation (first proposition))
  134.           (interpret interpretation (third proposition)))
  135.           interpretation :test #'equal))))
  136.  
  137. (defparameter *tmp-tuples*
  138.   (let ((tuples nil))
  139.     (dolist (a *domain*)
  140.       (dolist (b *domain*)
  141.     (push (list a b) tuples)))
  142.     (nreverse tuples)))
  143.  
  144. (defparameter *subst-ab*
  145.   (mapcar #'(lambda (tuple) `((a ,(car tuple))
  146.                   (b ,(cadr tuple))))
  147.       *tmp-tuples*))
  148.  
  149. (defparameter *tmp-tuples*
  150.   (let ((tuples nil))
  151.     (dolist (a *domain*)
  152.       (dolist (b *domain*)
  153.     (dolist (c *domain*)
  154.       (push (list a b c) tuples))))
  155.     (nreverse tuples)))
  156.  
  157. (defparameter *subst-abc*
  158.   (mapcar #'(lambda (tuple) `((a ,(car tuple))
  159.                   (b ,(cadr tuple))
  160.                   (c ,(caddr tuple))))
  161.       *tmp-tuples*))
  162.  
  163. ;;;testing Axiom-1
  164. (format t "Testing Axiom-1:~%")
  165. (mapc #'(lambda (tuple)
  166.       (format t "~A : ~A~%" (sb tuple (axiom-1 a b))
  167.           (interpret *interpretation*
  168.                  (sb tuple (axiom-1 a b)))))
  169.       *subst-ab*)
  170.  
  171. ;;;testing Axiom-2
  172. (format t "Testing Axiom-2:~%")
  173. (mapc #'(lambda (tuple)
  174.       (format t "~A : ~A~%" (sb tuple (axiom-2 a b c))
  175.           (interpret *interpretation*
  176.                  (sb tuple (axiom-2 a b c)))))
  177.       *subst-abc*)
  178.  
  179. ;;;And finally Peirce's Law
  180. (format t "Testing Peirce's Law:~%")
  181. (mapc #'(lambda (tuple)
  182.       (format t "~A : ~A~%" (sb tuple '(((a => b) => a) => a))
  183.           (interpret *interpretation*
  184.                  (sb tuple '(((a => b) => a) => a) ))))
  185.       *subst-abc*)
  186.  
Advertisement
Add Comment
Please, Sign In to add comment