Log in

A CL workbook - Liam Healy

Jun. 21st, 2010

10:36 pm - A CL workbook

Previous Entry Share Next Entry

I am looking for a way to conveniently keep a session of Lisp interaction. For example, when I write down a problem and solution for class, I define a succession of variables with defparameter (or a slightly more convenient form I've defined). In a later year, if I give the problem again with different numbers, I just change the defining parameters, and recalculate. This is clumsy, but it mostly works. What I'd really like is to define a few input parameters, and stepwise calculate the intermediate quantities. A single defun isn't appropriate because I need the intermediate values to show to the students. The defparameter approach is tedious and prone to error.

What I envision is a structure or class instance with "given values" provided at the beginning, and steps consisting of the form and the value. A new instance of the same problem would have the same forms to evaluate, but different values. It might also be nice to branch, i.e., at some step in the calculation, continue with a different set of calculations. but still be able to keep the tree structure (kind of like git for Lisp forms? hmmm).

Is there anything like this (in any language) out there? I tried some googling but if there's something along these lines out there, I'm not using the right keywords. For lack of a better word, I'm calling it a Lisp workbook or notebook. Bonus if it will integrate with LaTeX and/or org-mode.



Date:June 22nd, 2010 05:15 am (UTC)

babel + org + git?

I've not quite gotten org + babel + slime working quite as you describe, but it is a start (trying to make the R/ESS and emacs-lisp babel modes merge into a Common Lisp + slime mode).

Will post if I get it working, though...

(Reply) (Thread)
From: lhealy
Date:June 23rd, 2010 02:04 am (UTC)

Re: babel + org + git?

Sounds interesting. Have you seen MMM Mode?
(Reply) (Parent) (Thread)
Date:June 22nd, 2010 05:57 am (UTC)

A CL workbook

Maybe trace does some of what you want?

(Reply) (Thread)
From: lhealy
Date:June 23rd, 2010 02:06 am (UTC)

Re: A CL workbook

No, too crude. I would like a neat and readable (and re-runnable) output; some of my output is formatted directly in LaTeX and I use that directly. Also, it be very hard to specify e.g. which '+ is the one I want to see.
(Reply) (Parent) (Thread)
Date:June 22nd, 2010 06:10 am (UTC)
> I am looking for a way to conveniently keep a session of Lisp interaction.

See what your implementation does for "(dribble filename)".

(Reply) (Thread)
From: lhealy
Date:June 23rd, 2010 02:07 am (UTC)


Useful, but it's one-way, i.e., output only. It doesn't lend itself to changing parameters and rerunning.
(Reply) (Parent) (Thread)
Date:June 22nd, 2010 11:47 am (UTC)


CLWEB may work for you. It's a literate programming system for Common Lisp with good emacs integration. You can easily separate parameters from solution code, use sections to create different versions of solutions, and you can write about the problems in LaTeX in the same file.

(Reply) (Thread)
Date:June 22nd, 2010 11:49 am (UTC)


Made a thinko, plain TeX not LaTeX
(Reply) (Parent) (Thread)
From: lhealy
Date:June 23rd, 2010 02:10 am (UTC)


I'm superficially familiar with Knuth's work, so this is interesting. But, there's no (specific) documentation. Do you have examples?
(Reply) (Parent) (Thread)
Date:June 22nd, 2010 11:52 am (UTC)

Example Paste

An example paste would be useful. ~ tmh
(Reply) (Thread)
From: lhealy
Date:June 23rd, 2010 02:25 am (UTC)

Re: Example Paste

Sure. Notice how I paste in some multi-line output from CL inside
a comment, then I have individual line comments. And this is
a "real life" example, so instead of (sidereal-time (epoch lageos))
I repeat the time:
(sidereal-time #d2010-03-01T13:20:00)
meaning that I if that changed I'd have to repeat it.
And then I define it later, this time with (epoch lageos). Ugh.

            #(#_-4323.000000000000000_km #_-784.700000000000000_km
            #(#_-2.674000000000000_km/s #_5.011000000000000_km/s
            (MAKE-TIMEPOINT :TIMEPARSE '(0 20 13 1 3 2010) :DAY-ONLY NIL :SCALE
            :SUMMARY 'NIL))
     %% (sidereal-time #d2010-03-01T13:20:00)
    The sidereal time is $\gst{} = -0.7614\dgr$ from the formula
      given in class, so using 
      $\rv_{\text{ITRF}} = \rotz(\gst{}) \rv_{\text{IJK}}$, 
        %% (defp lageos-st (sidereal-time (epoch lageos)))
        %% (ijk-to-ecef-rot lageos-st)
        \rv =
        0.9999 & \fltpt{-1.329}{-2} & 0.000 \\
        \fltpt{1.329}{-2} & 0.9999 & 0.000 \\
        0.000 & 0.000 & 1.000
        %% (nf (pos lageos))
      which is
      %% (defp lageosb-posvel (ijk-to-ecef lageos-st lageos))
        %% (numerica-format (subseq lageosb-posvel 0 3))
        \rv =
(Reply) (Parent) (Thread)
Date:June 23rd, 2010 12:34 pm (UTC)

Re: Example Paste

My first thought is that you need something along the lines of WEB or NOWEB. (Disclaimer: I am biased towards NOWEB). It would need to be able to evaluate the chunks of code, so it would be an extension of NOWEB. Then, I realized that you also need macro like control over evaluation to store intermediate values. It wasn't clear to me what the semantics of that might look like.

Regardless, I'm not aware of any tool that does that. Looks like an opportunity to develop one for fun and profit or fame and glory. :-)

~ tmh
(Reply) (Parent) (Thread)
From: http://tentpost.com/openid/dherring.php
Date:June 22nd, 2010 03:16 pm (UTC)

Possible comparables

Sounds somewhat like a spreadsheet, mathcad, mathematica, or matlab.
(Reply) (Thread)
From: lhealy
Date:June 23rd, 2010 02:14 am (UTC)

Re: Possible comparables

Somewhat, don't know, not really, not really.

Spreadsheet idea is interesting. I think there was a CL
spreadsheet at one point. Not sure how I'd embed that
in LaTeX, but that's only an optional extra anyway.

I don't know mathcad, but suspect it's similar (for
these purposes) to mma and matlab. There's nothing wrong
with mma and matlab, but lisp has the REPL and to that
extent has the same features of mma and matlab.
I want more than that. At one point mma had a thing
called "notebooks" (I haven't used mma in years, so
it might be something different now) and that is
kind of what I'm getting at.
(Reply) (Parent) (Thread)
Date:June 24th, 2010 01:22 pm (UTC)

Re: Possible comparables


Maxima has something like latex(expresion) and you can also use Lisp with maxima. There is a maxima to Lisp translator.

So you can program in Lisp using maxima commands to extend the language and use tex(expr) or latex(expr) to write the result. Also there is a maxima command to be used inside emacs
(Reply) (Parent) (Thread)
Date:June 27th, 2010 04:34 pm (UTC)

Do you mean something like this?

(defclass hash-slots-object (standard-object)
  ((slot-table :initform (make-hash-table) :reader slot-table)))

(defmethod slot-missing ((class standard-class) (object hash-slots-object)
                         slot-name operation &optional new-value)
  (ecase operation
    (setf (setf (gethash slot-name (slot-table object)) new-value)) 
    (slot-boundp (nth-value 1 (gethash slot-name (slot-table object))))
    (slot-makunbound (remhash slot-name (slot-table object)) object)
    (slot-value (multiple-value-bind
                    (value foundp)
                    (gethash slot-name (slot-table object))
                  (if foundp value
                    (slot-unbound class object slot-name))))))
(defmacro object-let (object (&rest bindings) &body body)
  `(with-slots ,(mapcar #'car bindings) ,object
     (psetq ,@(loop for (var value) in bindings
                    nconc (list var value)))

(defmacro object-let* (object (&rest bindings) &body body)
  (let ((obj (gensym)))
    (if bindings
      `(let ((,obj ,object))
         (object-let ,obj (,(car bindings))
           (object-let* ,obj ,(cdr bindings) ,@body)))
      `(progn ,@body))))

(defun example ()
  (let ((object (make-instance 'hash-slots-object)))
    (object-let* object
        ((x 1)
         (y 2)
         (z (+ x y))
         (a (/ z y))
         (b (+ a a)))

...at the end, you can inspect the object and it will have all the slots set to the intermediate results.

Pascal Costanza
(Reply) (Thread)
Date:July 2nd, 2010 12:34 pm (UTC)
Emacs Calc has an embeded mode. Take a look at it!
(Reply) (Thread)