Skip to content

About

This is a hack that enables symbol links (common lisp).

Resources

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

symbol-links

This is a hack that enables symbol links. Works on SBCL, CMUCL, CCL and Lispworks (thanks to phoe).

For a patched SBCL with additional defpackage options see my fork.

Related work

See symbol-nicknames for a lengthy discussion on the theme.

Installation

Clone this repository to ~/quicklisp/local-projects directory and load the system with ql:quickload or asdf:load-system:

(ql:quickload :symbol-links)  ; or (asdf:load-system :symbol-links)

To enable this hack use enable-hack! function:

(symbol-links:enable-hack!)
; => T
;; Now symbol links are working!

You can also disable the hack with disable-hack!:

(symbol-links:disable-hack!)
; => NIL
;; Now symbol links aren't working.

That does not discard any created symbol links, so if you re-enable the hack all links will be restored.

Description

Symbol links attempt to provide similar functionality to python’s from import as

from math import factorial as f

would translate to

(symbol-links:link f math:factorial)

You can create a link from one symbol to another and it will be evaluated at read-time, allowing to create another name for a symbol.

For example you can create a shorthand for multiple-value-bind:

(symbol-links:link mvb multiple-value-bind)
;; or using the underlying function
;; (symbol-links:add-symbol-link 'mvb 'multiple-value-bind)
'mvb  ; => MULTIPLE-VALUE-BIND
(mvb (x y) (values 1 2)
  (+ x y))
; => 3

Removing such a link can be tricky - how can you reference the initial symbol? There are several solutions:

  1. Store symbol before creating a link:
    (defparameter *plus* '+)
    (symbol-links:link + -)
    (+ 10)  ; => -10
    (symbol-links:remove-symbol-link *plus*)
    (+ 10)  ; => 10
        
  2. You can disable symbol links by modifying special variable *follow-symbol-links*:
    (symbol-links:link + -)
    (setf symbol-links:*follow-symbol-links* nil)
    (+ 10)  ; => 10
    (symbol-links:unlink +)
    ;; or with an underlying function
    ;; (symbol-links:remove-symbol-link '+)
    (setf symbol-links:*follow-symbol-links* t)
    (+ 10)  ; => 10
        
  3. You can define a reader macro that reads with *follow-symbol-links* set to NIL and use it to reference symbol that has an outgoing link:
    (defun |#@-reader| (stream char arg)
      (declare (ignore char arg))
      (symbol-links:without-symbol-links
        (read stream t nil t)))
    (set-dispatch-macro-character #\# #\@ #'|#@-reader|)
    
    (symbol-links:link + -)
    '+   ; => -
    '#@+  ; => +
    (symbol-links:unlink #@+)
    (+ 10)  ; => 10
        
  4. You can reference all symbols that are linked to some symbol with symbol-linked-by function:
    (symbol-links:link + -)
    (symbol-links:symbol-linked-by '-)
    ; => (COMMON-LISP:+)
    (symbol-links:remove-symbol-link (car *))
    (+ 10)  ; => 10
        

API

*follow-symbol-links* [special variable]

Set this variable to T to enable following symbol links. Set this variable to NIL to disable following symbol links.

without-symbol-links [macro]

(without-symbol-link &body body)

Binds *follow-symbol-links* to NIL during the execution of the body.

add-symbol-link

(add-symbol-link from to) => created-p

Adds a symbol link from symbol from to symbol to.

Returns T if a new link was created and NIL otherwise.

Exceptional situations

Signals an correctable error if there is a conflicting link going from symbol from.

Signals an error if a circular link is about to be created.

link [macro]

(link from to) => created-p

Same as add-symbol-link but from and to must be unquoted.

remove-symbol-link

(remove-symbol-link symbol) => removed-p

Removes a symbol link going from symbol if there is any.

Returns T if the link was removed and NIL otherwise.

unlink [macro]

(unlink symbol) => removed-p

Same as remove-symbol-link but symbol must be unquoted.

relink [macro]

(relink from to) => created-p

Same as link, but removes any prior link going from from.

symbol-link

(symbol-link symbol) => link, link-p

If there is a link going from symbol returns the symbol it points to and T.

If there is no link going from symbol returns NIL and NIL.

symbol-linked-by

(symbol-linked-by symbol) => linked-by-list

Returns a list of all symbols that have a link going to symbol.

About

This is a hack that enables symbol links (common lisp).

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages