;;;; -*- mode:lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE:               cl-types-graph.lisp
;;;;LANGUAGE:           Common-Lisp
;;;;SYSTEM:             Common-Lisp
;;;;USER-INTERFACE:     NONE
;;;;DESCRIPTION
;;;;
;;;;    Generates the COMMON LISP type hierarchy graph.
;;;;    See http://www.informatimago.com/articles/cl-types/
;;;;
;;;;AUTHORS
;;;;    <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;MODIFICATIONS
;;;;    2010-10-10 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;;    GPL
;;;;
;;;;    Copyright Pascal J. Bourguignon 2010 - 2010
;;;;
;;;;    This program is free software; you can redistribute it and/or
;;;;    modify it under the terms of the GNU General Public License
;;;;    as published by the Free Software Foundation; either version
;;;;    2 of the License, or (at your option) any later version.
;;;;
;;;;    This program is distributed in the hope that it will be
;;;;    useful, but WITHOUT ANY WARRANTY; without even the implied
;;;;    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;;;;    PURPOSE.  See the GNU General Public License for more details.
;;;;
;;;;    You should have received a copy of the GNU General Public
;;;;    License along with this program; if not, write to the Free
;;;;    Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;;    Boston, MA 02111-1307 USA
;;;;**************************************************************************


(defun cl-type-graph ()
  (let* ((path (format nil "cl-types-in-~(~A~)" (substitute #\- #\space (lisp-implementation-type))))
         (font "Times")
         (types
          ;; some types have different names, so let's put them in unique classes
          (loop
             :with unique-types = '()
             :with types = (let ((types '()))
                             (do-external-symbols (s :cl)
                               (when (ignore-errors (subtypep s 't))
                                 (push s types)))
                             types)
             :while types
             :do (let ((a  (pop types))
                       (others '())
                       (aclass '()))
                   (dolist (b types)
                     (if (and (subtypep a b)
                              (subtypep b a))
                         (push b aclass)
                         (push b others)))
                   (if aclass
                       (push (cons a aclass) unique-types)
                       (push a unique-types))
                   (setf types others))
             :finally (return unique-types)))
         ;; let's build the 'is-a-subtype-of' graph.
         (graph (loop
                   :for a :in types
                   :nconc (loop
                             :for b :in types
                             :when (and (not (eq a b))
                                        (subtypep (if (listp a)
                                                      (first a)
                                                      a)
                                                  (if (listp b)
                                                      (first b)
                                                      b)))
                             :collect (list a b)))))

    (dolist (node types)
      (unless (find-if (lambda (arc)
                         (or (and (eq node (first arc))
                                  (not (if (consp (second arc))
                                           (member 't (second arc))
                                           (eq 't (second arc)))))
                             (and (eq node (second arc))
                                  (not (if (consp (first arc))
                                           (member 'nil (first arc))
                                           (eq 'nil (first arc)))))))
                       graph)
        (setf graph (delete node graph :key (function first)))
        (setf graph (delete node graph :key (function second)))))

    (with-open-file (dot (format nil "~A.dot" path)
                         :direction :output
                         :if-does-not-exist :create
                         :if-exists :supersede)
      (format dot "digraph ~S {~%" (lisp-implementation-type))
      (format dot "~Trankdir = LR~%")
      (format dot "~Tnode [ shape=box; fontname=\"~A\" ];~%" font)
      (dolist (arc graph)
        (format dot "~T\"~A\" -> \"~A\";~%" (first arc) (second arc)))
      (format dot "}~%")
      path)))

#-test
(cl-type-graph)


#+test
(let ((path (cl-type-graph)))
  (asdf:run-shell-command "tred ~A.dot |dot -Tps /dev/stdin -o ~:*~A.ps" path)
  (asdf:run-shell-command "gsview ~A.ps &" path))


#|

clall can be found at:  http://tinyurl.com/2urswh9

#!/bin/bash
clall '(load "cl-types-graph.lisp")'
for f in cl-types-in-*.dot ; do
tred < $f | dot -Tps /dev/stdin > ${f/.dot/.ps}
done
for f in  cl-types-in-*.ps ; do
gsview $f &
done


|#
ViewGit