Date: Mon, 11 Nov 1996 17:26:01 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Sun, 01 Sep 1996 06:41:27 GMT Content-length: 9724 Using GNU Common Lisp on Department Workstations

Using GNU Common Lisp (GCL)

  1. If you have not already created your instructional account on one of the departmental workstations used for this class, log in as newuser and set it up following the instructions given. It takes about half an hour for the account to be created and ready for use.

  2. Log in to your account and open two xterm windows. In the two windows you should see the default UNIX prompt '>'. One window will be used to run the version of Common Lisp we will be using called gcl. The other window will be used to edit UNIX files containing the Lisp functions and programs you write.

  3. Run the GCL interpreter using the command gcl in the first window. Once invoked, GCL initializes the system, prints a message and enters the top-level prompt-read-eval-print loop, using the prompt '>'. You can enter any s-expression for evaluation to this prompt.

    Since GCL has the same prompt as the default UNIX prompt on the workstations, you might want to change the UNIX prompt to be something else. To do this, run the shell command (you can put this command in your ".cshrc.local" file to avoid doing it every time):

         set prompt=whatever-you-like
    

  4. GCL has on-line help facilities. For example, within GCL:
        >(help)  ;; introductory help message
        >(help symbol)  ;; documentation on everything
            ;; associated with symbol.  E.g., (help 'load)
        >(help 'step)
        >(help 'trace)
        >(help 'compile-file)
        >(help 'dribble)
        >(help 'any-Lisp-function-that-you-are-interested-in)
        >(help* string)  ;; documentation on all symbols
            ;; containing string as a sub-string.  E.g.,
            ;; (help* "remove") describes the functions remove,
            ;; remove-if, remove-duplicates, and remove-if-not
        >(apropos string)  ;; like help*, except only lists the names
        >(documentation symbol type)  ;; prints online
            ;; documentation string for the symbol of the given type, if
            ;; available.
    

    You can also use GNU's info facility to access documentation. A WWW version is available; in addition, this material may be accessed by the UNIX program info (type info at the UNIX prompt) or within Emacs (by typing "ESC-x info").

  5. Edit your program in a file using a text editor such as vi or Emacs. For example, the following creates a file named my-functions.lisp and defines a single Lisp function named welcome in it:
         UNIX> vi my-functions.lisp
    
         (defun welcome () (format t "Hello! Welcome."))
    

  6. Emacs commands for Editing Lisp. Since Emacs is written in lisp, the command "C-h f function-name" will work to describe general lisp functions, as well as the editing actions. Emacs has three lisp modes: emacs-lisp-mode, lisp-mode, and lisp-interaction-mode. The complexities of the Emacs lisp modes are beyond the scope of this introductory document; however, if Emacs is your editor of choice, its powerful lisp modes are worth investigating. See Some Tips on Editing Lisp Code for more ideas.

  7. Vi Commands for Editing Lisp.
    Since there are many ('s and )'s in Lisp programs, you should set up vi to aid your entering Lisp functions readably and correctly. There are three options of interest: lisp, which changes the "(" and ")" commands to move backward and forward over s-expressions, (2) autoindent, which does automatic indenting, and (3) showmatch, which shows the matching "(" when a ")" is typed. To set these you can either set them each time you enter vi by doing:
         :se ai sm lisp
    

    or to have these settings set every time you enter vi, just edit the .exrc file in your home directory to include the line:

         set ai sm lisp
    

    Finally, there is also an operator in vi that realigns existing lines as though they had been typed in with lisp and autoindent set. This is the = operator. Using the command =% at the beginning of a function will realign all of the lines in that function. See also the use of the (,),{, }, [[ and ]] commands for moving around within Lisp functions. Here is a brief explanation to help you try them out. The ( and ) commands move backward and forward over s-expressions. The { and } commands are like ( and ) but don't stop at atoms. These can be used to skip to the next list, or through a comment quickly. The [[ and ]] commands advance and retreat to lines beginning with a (. They are useful for dealing with entire function definitions.

  8. Call Vi from Within GCL:

    As an alternative to keeping separate windows for vi and GCL, we can define a Lisp function "vi" which will allow us to call vi from within GCL, and automatically load the changes when we're done editing. To do this, save the contents of ~dyer/public/html/cs540/lisp-vi.lisp in a file in your own directory, and load that file into GCL.

  9. In the GCL window, load all of the functions defined in a file into GCL:
        >(load "my-functions.lisp")
    

    While GCL is loading all of the functions in this file it does some simple syntax checking such as unmatched parentheses and misused punctuation. Your functions will not be loaded successfully if it has any of these syntax errors. In this case, go back to the vi window, edit your functions, and then reload the file in the GCL window.

  10. Run your program in GCL:
        >(welcome)
        Hello! Welcome.
    

    If there are any runtime errors, use vi in the other window to modify your program, and then, in the GCL window, reload the file containing the program and run it again.

  11. Compile your program:
        >(compile-file "my-functions.lisp")
        >(load "my-functions.o")
        >(welcome)
    

    After compiling, an executable file my-functions.o is created in your current directory. Debug your program using the interpreter first, and compile for the optimized executable code only when everything is debugged.

  12. Make a typescript of a terminal session:

    One way is to use dribble:

        >(dribble file-name-given-as-a-string) ;; start typescript
        Run programs here plus anything else you want saved
        >(dribble)  ;; stop typescript
    

    Everything displayed on the screen will be saved in the given file including both what you type and what the Lisp interpreter responds.

    You can also use the UNIX command script filename before starting GCL, with the UNIX command exit after ending the GCL session. This appears to give slightly better looking output.

  13. Trace or single-step your program.
         >(trace func-name) ;; trace named function
         >(untrace func-name) ;; stop tracing named function
         >(step (func-name parameters-if-any)) ;; single-step mode
    

  14. Exit GCL.
         >(bye)     ;; Control-d will also stop GCL
    

  15. Lisp Break Package.
    Lisp invokes a "break package" whenever it encounters an error or the user aborts execution. The break package is recognizable as the prompt changes from '>' to '>>'. There are many options for you to choose in this package:

    Break-loop Command Summary ([] indicates optional arg)

    :help       this summary
    :bl [j]     show local variables and their values, or
    	      segment of vs if compiled in j stack frames
                  starting at the current one
    :bt [n]     BACKTRACE [n steps]
    :down [i]   DOWN i frames (one if no i)
    :env        describe ENVIRONMENT of this stack frame
    	      (for interpreted).
    :fr [n]     show frame n
    :loc [i]    return i'th local of this frame if its
    	      function is compiled (si::loc i)
    :r          RESUME (return from the current break loop)
    :up [i]     UP i frames (one if no i)
    
    Example: print a backtrace of the last 4 frames
    
    >>:bt 4
    
    Note:  (use-fast-links nil) makes all non system function
    calls be recorded in the stack.   (use-fast-links t) is
    the default
    
    Low level commands:
    ------------------
    :p [i]           make current the i'th PREVIOUS frame
    		   (in list show by :b)
    :n [i]           make current the i'th NEXT frame
    		   (in list show by :b)
    :go [ihs-index]  make current the frame corresponding
    		   to ihs-index
    :m               print the last break message
    :c               show function of the current ihs frame
    :q [i]           quit to top level
    :r               resume from this break loop
    :b               full backtrace of all functions and
    		   special forms
    :bs [name]       backward search for frame named 'name'
    :fs  [name]      search for frame named 'name'
    :vs [from] [to]  Show value stack between FROM and TO
    :ihs [from] [to] Show Invocation History Stack
    :bds ['v1 'v2 ..]Show previous special bindings of
    		   v1, v2,.. or all if no v1
    

  16. Be sure to log out of the workstation when you leave. Hold down the left mouse button and click logout.