Guest User

Untitled

a guest
Jun 21st, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 8.26 KB | None | 0 0
  1. (setq *theory* "6ffc9680fbe00a58d70cdeb319f11205ed998131ce51bb96f16c7904faf74a3d")
  2.  
  3. (defun pfgize-name (c)
  4.   (let ((c (format nil "~d" c)))
  5.     (if (equal c "")
  6.     "blank"
  7.       (let ((c0 (char-code (aref c 0))))
  8.     (if (or (and (>= c0 65) (<= c0 90)) (and (>= c0 97) (<= c0 122)))
  9.         c
  10.       (format nil "c~d" c))))))
  11.  
  12. (defun ivytrm-sig (m probsig)
  13.   (if (consp m)
  14.       (let ((f (car m)))
  15.     (if (assoc f probsig)
  16.         (ivyspine-sig (cdr m) probsig)
  17.       (ivyspine-sig (cdr m) (acons f (list 'I (length (cdr m))) probsig))))
  18.     probsig))
  19.  
  20. (defun ivyspine-sig (ml probsig)
  21.   (if ml
  22.       (ivyspine-sig (cdr ml) (ivytrm-sig (car ml) probsig))
  23.     probsig))
  24.  
  25. (defun ivyatm-sig (a probsig)
  26.   (if (consp a)
  27.       (if (eq (car a) '=)
  28.       (ivyspine-sig (cdr a) probsig)
  29.     (let ((p (car a)))
  30.       (if (assoc p probsig)
  31.           (ivyspine-sig (cdr a) probsig)
  32.         (ivyspine-sig (cdr a) (acons p (list 'O (length (cdr a))) probsig)))))
  33.     probsig))
  34.  
  35. (defun ivycl-sig (cl probsig)
  36.   (if (consp cl)
  37.       (case (car cl)
  38.     (OR
  39.      (ivycl-sig (caddr cl) (ivycl-sig (cadr cl) probsig)))
  40.     (NOT
  41.      (ivyatm-sig (cadr cl) probsig))
  42.     (t (ivyatm-sig cl probsig)))
  43.     probsig))
  44.  
  45. (defun ivypf-sig (ivypf &optional probsig)
  46.   (if ivypf
  47.       (ivypf-sig (cdr ivypf) (ivycl-sig (caddar ivypf) probsig))
  48.     probsig))
  49.  
  50. (defun pfg-tp-r (rtp ar)
  51.   (if (> ar 0)
  52.       (format nil "TpArr ind ~d" (pfg-tp-r rtp (- ar 1)))
  53.     (if (eq rtp 'O)
  54.     "Prop"
  55.       "ind")))
  56.    
  57. (defun pfg-tp (tp)
  58.   (pfg-tp-r (car tp) (cadr tp)))
  59.  
  60. (defun ivytrm-frees (m vl)
  61.   (if (consp m)
  62.       (ivyspine-frees (cdr m) vl)
  63.     (if (member m vl)
  64.     vl
  65.       (cons m vl))))
  66.  
  67. (defun ivyspine-frees (ml vl)
  68.   (if ml
  69.       (ivyspine-frees (cdr ml) (ivytrm-frees (car ml) vl))
  70.     vl))
  71.  
  72. (defun ivyatm-frees (a vl)
  73.   (if (consp a)
  74.       (ivyspine-frees (cdr a) vl)
  75.     vl))
  76.  
  77. (defun ivycl-frees (cl &optional vl)
  78.   (if (consp cl)
  79.       (case (car cl)
  80.     (OR
  81.      (ivycl-frees (caddr cl) (ivycl-frees (cadr cl) vl)))
  82.     (NOT
  83.      (ivyatm-frees (cadr cl) vl))
  84.     (t (ivyatm-frees cl vl)))
  85.     vl))
  86.  
  87. (defun pfg-trm (m)
  88.   (if (consp m)
  89.       (pfg-spine (pfgize-name (car m)) (cdr m))
  90.     (format nil "~d" m)))
  91.  
  92. (defun pfg-spine (h ml)
  93.   (if ml
  94.       (pfg-spine (format nil "Ap ~d ~d" h (pfg-trm (car ml))) (cdr ml))
  95.     h))
  96.  
  97. (defun pfg-atm (a)
  98.   (if (consp a)
  99.       (if (eq (car a) '=)
  100.       (pfg-spine "Eq ind" (cdr a))
  101.     (pfg-spine (pfgize-name (car a)) (cdr a)))
  102.     (format nil "unknownatomcase:~d" a)))
  103.  
  104. (defun pfg-lit (l)
  105.   (if (and (consp l) (eq (car l) 'NOT))
  106.       (format nil "Imp ~d False" (pfg-atm (cadr l)))
  107.     (pfg-atm l)))
  108.  
  109. (defun pfg-lit-neg (l)
  110.   (if (and (consp l) (eq (car l) 'NOT))
  111.       (pfg-atm (cadr l))
  112.     (format nil "Imp ~d False" (pfg-atm l))))
  113.  
  114. (defun pfg-clause-2 (cl)
  115.   (if cl
  116.       (format nil "Imp ~d ~d" (pfg-lit-neg (car cl)) (pfg-clause-2 (cdr cl)))
  117.     (format nil "False")))
  118.  
  119. (defun pfg-clause-r (vl cl)
  120.   (if vl
  121.       (format nil "All ~d ind ~d" (car vl) (pfg-clause-r (cdr vl) cl))
  122.     (pfg-clause-2 cl)))
  123.  
  124. (defun ivycl-marked-lits (cl pos)
  125.   (if (consp cl)
  126.       (case (car cl)
  127.     (OR
  128.      (if pos
  129.          (cond ((equal (car pos) 1)
  130.             (append (ivycl-marked-lits (cadr cl) (cdr pos)) (mapcar #'(lambda (l) (cons 'NIL l)) (ivycl-lits (caddr cl)))))
  131.            ((equal (car pos) 2)
  132.             (append (mapcar #'(lambda (l) (cons 'NIL l)) (ivycl-lits (cadr cl)))
  133.                 (ivycl-marked-lits (caddr cl) (cdr pos))))
  134.            (t (break)))
  135.        (break)))
  136.     (t (list (cons 't cl))))
  137.     (break)))
  138.  
  139. (defun ivycl-lits (cl)
  140.   (if (consp cl)
  141.       (case (car cl)
  142.     (OR (append (ivycl-lits (cadr cl)) (ivycl-lits (caddr cl))))
  143.     (t (list cl)))
  144.     (if (eq cl 'FALSE)
  145.     nil
  146.       (format nil "unknownprop:~d" cl))))
  147.  
  148. (defun pfg-clause (cl)
  149.   (pfg-clause-r (reverse (ivycl-frees cl)) (ivycl-lits cl)))
  150.  
  151. (setq *ivylineh* (make-hash-table :test #'equal))
  152.  
  153. (defun pfg-just-line (x)
  154.   (let ((just (cadr x)))
  155.     (if (consp just)
  156.     (case (car just)
  157.       (INPUT (format nil "H~d" (car x))) ; should not happen, but this seems reasonable if it does
  158.       (INSTANTIATE
  159.        (let ((vl (reverse (ivycl-frees (caddr (gethash (cadr just) *ivylineh*)))))
  160.          (pf (format nil "H~d" (cadr just)))
  161.          (theta (caddr just)))
  162.          (dolist (x vl pf)
  163.            (let ((m (assoc x theta)))
  164.          (setq pf (format nil "TmAp ~d ~d" pf (pfg-trm (cdr m))))))))
  165.       (FLIP
  166.        (let ((pf (format nil "H~d" (cadr just)))
  167.          (pos (caddr just)))
  168.          (format nil "TODO: FLIP ~d AT ~d" pf pos)))
  169.       (RESOLVE
  170.        (let ((pf1 (format nil "H~d" (cadr just)))
  171.          (pos1 (caddr just))
  172.          (pf2 (format nil "H~d" (nth 3 just)))
  173.          (pos2 (nth 4 just)))
  174.                     ; need to generalize this
  175.          (cond ((and (equal pos1 nil) (equal pos2 nil))
  176.             (format nil "PrAp ~d ~d" pf2 pf1))
  177.            (t
  178.             (let* ((cl1 (ivycl-marked-lits (caddr (gethash (nth 1 just) *ivylineh*)) pos1))
  179.                (cl2 (ivycl-marked-lits (caddr (gethash (nth 3 just) *ivylineh*)) pos2))
  180.                (cl (ivycl-lits (caddr x)))
  181.                (vl1 (reverse (ivycl-frees (caddr (gethash (nth 1 just) *ivylineh*)))))
  182.                (vl2 (reverse (ivycl-frees (caddr (gethash (nth 3 just) *ivylineh*)))))
  183.                (vl (reverse (ivycl-frees (caddr x))))
  184.                (litcnt 0)
  185.                (pf nil)
  186.                (la nil))
  187.               (when (or vl vl1 vl2)
  188.             (error "unhandled resolve case with free vars"))
  189.               (dolist (l cl)
  190.             (incf litcnt)
  191.             (push (cons l (format nil "A~d" litcnt)) la)
  192.             (if pf
  193.                 (setq pf (format nil "~d PrLa A~d ~d" pf litcnt (pfg-lit-neg l)))
  194.               (setq pf (format nil "PrLa A~d ~d" litcnt (pfg-lit-neg l)))))
  195.               (when (cdr cl1)
  196.             (dolist (l cl1)
  197.               (if (car l)
  198.                   (setq pf1 (format nil "PrAp ~d A0" pf1))
  199.                 (let ((a (assoc (cdr l) la :test #'equal)))
  200.                   (setq pf1 (format nil "PrAp ~d ~d" pf1 (cdr a))))))
  201.             (dolist (l cl1)
  202.               (when (car l)
  203.                 (setq pf1 (format nil "PrLa A0 ~d ~d" (pfg-lit-neg (cdr l)) pf1)))))
  204.               (dolist (l cl2)
  205.             (if (car l)
  206.                 (setq pf2 (format nil "PrAp ~d ~d" pf2 pf1))
  207.               (let ((a (assoc (cdr l) la :test #'equal)))
  208.                 (setq pf2 (format nil "PrAp ~d ~d" pf2 (cdr a))))))
  209.               (if pf
  210.               (format nil "~d ~d" pf pf2)
  211.             pf2))))))
  212.       (PARAMOD
  213.        (let ((pf1 (format nil "H~d" (cadr just)))
  214.          (pos1 (caddr just))
  215.          (pf2 (format nil "H~d" (nth 3 just)))
  216.          (pos2 (nth 4 just)))
  217.          (format nil "TODO: PARAMOD ~d AT ~d WITH ~d AT ~d" pf1 pos1 pf2 pos2)))
  218.       (t (format nil "UNKNOWNJUST:~d" (car just))))
  219.       "UNKNOWNJUST")))
  220.  
  221. (defun ivy-to-pfg (fn gn)
  222.   (let* ((f (open fn :direction :input))
  223.      (g (open gn :direction :output :if-exists :supersede :if-does-not-exist :create))
  224.      (ivypf nil)
  225.      (emptyclauseline nil)
  226.      (thmcnt 0))
  227.     (if *theory*
  228.     (format g "Document ~d~%" *theory*)
  229.       (format g "Document Empty~%"))
  230.     (if (equal *theory* "6ffc9680fbe00a58d70cdeb319f11205ed998131ce51bb96f16c7904faf74a3d")
  231.     (format g "Base ind~%Let False : Prop := Prim 1~%")
  232.       (format g "Base ind~%Def False : Prop := All p Prop p~%"))
  233.     (loop while (setq ivypf (read f nil nil)) do
  234.       (setq *ivylineh* (make-hash-table :test #'equal))
  235.       (dolist (x ivypf) (setf (gethash (car x) *ivylineh*) x))
  236.       (let* ((probsig (reverse (ivypf-sig ivypf)))
  237.          (ivyinput (remove-if-not #'(lambda (x) (and (consp (cadr x)) (eq (caadr x) 'INPUT))) ivypf)))
  238.         (format g "Thm ivy~d :" (incf thmcnt))
  239.         (setq *probsig* probsig)
  240.         (setq *g* g)
  241.         (dolist (x probsig)
  242.           (setq *x* x)
  243.           (format g " All ~d ~d" (pfgize-name (car x)) (pfg-tp (cdr x))))
  244.         (format g "~%")
  245.         (dolist (inp ivyinput)
  246.           (format g " Imp ~d~%" (pfg-clause (caddr inp))))
  247.         (format g " False~% :=")
  248.         (dolist (x probsig)
  249.           (format g " TmLa ~d ~d" (pfgize-name (car x)) (pfg-tp (cdr x))))
  250.         (format g "~%")
  251.         (dolist (inp ivyinput)
  252.           (format g " PrLa H~d ~d~%" (car inp) (pfg-clause (caddr inp))))
  253.         (dolist (x ivypf)
  254.           (unless (and (consp (cadr x)) (eq (caadr x) 'INPUT))
  255.         (if (eq (caddr x) 'FALSE)
  256.             (setq emptyclauseline x)
  257.           (format g " PrAp PrLa H~d ~d~%" (car x) (pfg-clause (caddr x))))))
  258.         (if emptyclauseline
  259.         (format g " ~d~%" (pfg-just-line emptyclauseline))
  260.           (format g " ...~%"))
  261.         (dolist (x (reverse ivypf))
  262.           (unless (and (consp (cadr x)) (eq (caadr x) 'INPUT))
  263.         (unless (eq (caddr x) 'FALSE)
  264.           (format g " ~d~%" (pfg-just-line x)))))))
  265.     (close g)
  266.     (close f)))
Add Comment
Please, Sign In to add comment