20110225

:constructor tricks

Every once in a while I discover things in CL that somehow I had not thought about before. They have been there sometimes since CLtL1 but, somehow, I did not register them before. (I am slow, I know; and I am not claiming that this is anything new. But this is the Internet :) )

Fancy :constructor uses are one of these things.

Suppose you wanted to construct a immutable tree node structure which recorded the height of the sub-tree upon construction. Here is an iteration of what you may do.

(defstruct node
   (height 0)
   left
   right)

Now you need to create the constructor for nodes. Not worrying about nil nodes, you may start by doing the obvious (assuming a max* function capable of dealing with nil values):

(defun build-node (l r)
   (make-node :left l
              :right r
              :height (1+ (max* (node-height l) (node-height r)))))
Ok. Fine. But why having two constructors, with the standard make-node essentially useless (you would not want to advertise it, as height is a computed slot)?

However, you can do the following:

(defstruct (node
           (:constructor make-node (left right
                                    &aux
                                    (height (1+ (max* (node-height left) (node-height right))))))
   (height 0)
   left
   right)
This will work as expected; the key trick is to leverage the &aux parameter to collect the computation. Adding the appropriate :read-only declarations, you end up with:
(defstruct (node
           (:constructor make-node (left right
                                    &aux
                                    (height (1+ (max* (node-height left) (node-height right))))))
   (height  0 :read-only t :type (mod 42))
   (left  nil :read-only t :type (or null node))
   (right nil :read-only t :type (or null node))
A perfectly fine immutable node structure.


(cheers)

20110224

NEW structure and class constructor handling?

In a previous post I mused about having a new new operator for Common Lisp.

The only rub in the scheme I came up with was the handling of DEFSTRUCT constructors, which had to be somehow recorded in the system in order to make them known to the new operator.

The solution was less than satisfactory, and I had been thinking about how to come up with something more pleasant. Now I believe I found something acceptable by relaxing the basic new invariant (cfr., the spec) and admitting two "pseudo" types as arguments. Suppose we had a "pseudo" type (class class-name) and a "pseudo" type (struct structure-class-name). Now I could write:

cl-prompt> (defclass foo () ())
#<STANDARD CLASS FOO>

cl-prompt> (newq (class foo))
#<FOO @ 42>
The twist is that now I have the right structure to plug in the constructor for structures.
cl-prompt> (defstruct baz q w e)
BAZ

cl-prompt> (newq (struct baz) :q 42)
#S(BAZ :Q 42 :W NIL :E NIL) ; The standard MAKE-BAZ constructor is called.

cl-prompt> (defstruct (bar (:constructor barify (a s &optional d)
                           (:constructor new-bar))
               a s d)
BAR

cl-prompt> (newq (struct bar :by barify) 1 2)
#S(BAR :A 1 :S 2 :D NIL)

cl-prompt> (newq (struct bar :by new-bar) :s 2 :a 1)
#S(BAR :A 1 :S 2 :D NIL)

I think this does it and it is not so bad. Any comments?


(cheers)

20110216

EQUALITY and COMPARISON to CDR

I submitted the proposal for equality and comparison (cfr. my previous post) to the CDR discussion fora.
(cheers)

20110130

All things being equal...

Alright, this has been floating around for some time and it is time to write it up.

Several object-oriented languages (e.g., Java, Ruby, C++, etc.) have ways to define equality and comparison operators or methods. In Java the Object class defines the equals method, which can be specialized in user code and the Comparable<T> interface.

In Common Lisp, while all the machinery is there to provide such facilities, there has been no push to provide them. I think there are two reasons for this. The first one lies in the widespread use of :test keywords and the programming style it encourages, especially when writing libraries. The second in the influential KMP's paper on The Best of Intentions: EQUAL Rights - and Wrongs - in Lisp.

The first reason does not really needs discussing, the second, a bit. In my honest opinion, there are no real reasons not to have a notion of generic equality (and comparisons) on top of the standard :test machinery, provided that some caution is taken.

Having said so, I built methods for the following generic functions.

(defgeneric AEQUALIS (a b &rest keys &key recursive-p &allow-other-keys)
 (:method ((a T) (b T) &rest keys &key recursive-p &allow-other-keys)
    (eq a b))
 )

(defgeneric COMPARE (a b &rest keys &key recursive-p &allow-other-keys)
   ...)

AEQUALIS is Latin for "equal". The generic function COMPARE returns <, >, =, or /=.

I prepared a specification for them (and an implementation) in order to post a CDR. You can find it in the CLEQCMP directory on my Google docs.

Comments welcome.

Enjoy


(cheers)

20110107

NEW project on common-lisp.net

There is a NEW project on common-lisp.net. The new-op pages contain documentation and download instructions to get the code for the new operator in Common Lisp I described in a previous post.
(cheers)

20101219

CLAZY and ENUMERATIONS

I submitted CLAZY and CL-ENUMERATIONS to Quicklisp and found out that there were some problems with them. Mostly due to (unwarranted) package locks in SBCL and CLISP. Fixing these problems led to some revisitation of the code base and subsequent improvements (I hope).

Both libraries should appear in future Quicklisp releases.


(cheers)

20101206

HTML in Common Lisp: code or fill?

I finally got around to write my very own HTML documentation system. Of course, this may be pointless and useless, but I found the alternatives "out there" lacking this or that (obviously!). So here I am, trying to automate the process of producing documentation like that of CLAZY, which I hand-crafted in the past. In the process, needless to say, I got carried away and got lost in the land of featurism only to contemplate my overall ignorance of most things webbish.

As usual, Edi Weitz came to my rescue by offering his CL-WHO and HTML-TEMPLATE, which raised a question: which of the two approaches is best? Note that the answer should consider how many dependencies my system files can have.

Let's start with the simple problem I have: I need to produce a .html file that contains, say, a "header" for my documentation. I could do it using CL-WHO in the following way (just a snippet, not working...)

(defun produce-header-file (fs header-pathname)
(declare (type frameset fs)
      (type pathname header-pathname))
(let ((fs-order (frameset-order fs))
   (fs-head-title (frameset-head-title fs))
   (fs-body-title (frameset-body-title fs))
   )
(with-open-file (hs header-pathname
                   :direction :output
                   :if-exists :supersede
                   :if-does-not-exist :create)
 (with-html-output (hs hs :indent t)
   (fmt "~A" (cl-fad:pathname-as-file header-pathname))
   (fmt +doctype-frameset-control-string+)
   (:html
    (:head
     (:title (str fs-head-title))
     (:link :rel "stylesheet" :href "clstyle.css"))
    (:body :style "margin: 0pt 0pt 0pt 0pt;"
     (:div
      :class "header"
      :style "padding-left: 2em; padding-top: 5pt; color: #41286f; font-size: 14pt"
      (:strong (str fs-body-title))
      (:div
       :class "navigation"
       :style "right: 2m"
       (:a
        :href "index.html"
        :class "navigation-link-selected"
        :target "_parent"
        (str "Home"))
       )))))
 )))

Or I could use HTML-TEMPLATE and ship around a "template" file like the following (again, this is just a snippet and contains errors):

<!-- header.html -->

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title><!-- TMPL_VAR HEAD_TITLE --></title>
<link rel="stylesheet" href="clstyle.css">
</head>

<body style="margin: 0pt 0pt 0pt 0pt;">

<div class="header"
style="padding-left: 2em; padding-top: 5pt;  ">
<strong><i><!-- TMPL_VAR BODY_TITLE --></i></strong><br/>

<div class="navigation" style="right: 2m">
<!-- TMPL_LOOP frames -->
<!-- TMPL_IF current_frame -->
<a href=<!-- TMPL_VAR frame --> class="navigation-link-selected" target=_parent><!-- TMPL_VAR f_nav_name ->></a>
<!-- TMPL_ELSE -->
<a href=<!-- TMPL_VAR frame --> class="navigation-link" target=_parent><!-- TMPL_VAR f_nav_name ->></a>
<!-- /TMPL_IF -->
<!-- /TMPL_LOOP -->
</div>

</div>

</body>
</html>

<!-- end of file : header.html -->

I am inclined to go the CL-WHO way. I do not have to ship around an extra file and record its location etc etc; yet the HTML-TEMPLATE does have appeal. Any ideas out there?


(cheers)