Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nil vs () #8

Closed
cpitclaudel opened this issue Jun 13, 2016 · 4 comments
Closed

nil vs () #8

cpitclaudel opened this issue Jun 13, 2016 · 4 comments
Labels

Comments

@cpitclaudel
Copy link
Collaborator

cpitclaudel commented Jun 13, 2016

In the "issues with serializing to sexps" family, I've just run into the following problem: many lisps do not distinguish between '() and nil, and thus serialize () to nil.

For example in common lisp:

[7]> (format t "~a" '())
NIL

And in Emacs Lisp:

(format "%S" `()) ;; => nil 

Could we add an option to SerAPI to accept nil as a synonym for ()? Currently I get this:

(97 (Query (() None PpStr) Goals))
  (Answer 97 Ack)
  (Answer 97
          (ObjList nil))
(97 (Query (nil None PpStr) Goals))
  (SexpError
   (Sexplib\.Conv\.Of_sexp_error
    (Failure "Sertop_protocol.cmd_of_sexp: unexpected sum tag")
    (97
     (Query
      (nil None PpStr)
      Goals))))

In the meantime, I'll look at whether I can make the Emacs lisp printer produce '().

Looking at Emacs' source code, there doesn't seem to be a way to make it print () instead of nil, but it's pretty easy to write a decent workaround, so I'd call this low priority.

@ejgallego
Copy link
Owner

Indeed, this is handled by Sexplib, need to investigate more.

@ejgallego
Copy link
Owner

I'm much afraid that Sexplib parser won't be easy to modify to support this use case. I'm sorry.

I propose to tag wontfix for now and try to workaround this bug.

@cpitclaudel
Copy link
Collaborator Author

Sounds good. In fact, I'd say we can close this. Here's a small function that will do the trick:

(defun prin1-to-sexp (val)
  "Convert VAL to a sexp.
Crucially, renders nil as (), not nil."
  (if (listp val)
      (concat "(" (mapconcat #'prin1-to-sexp val " ") ")")
    (prin1-to-string val)))

It breaks if passed an improper list (such as (1 . 2)), but that would have confused the sertop side already :)

@ejgallego
Copy link
Owner

Cool, thanks, even if it's a pity to have this minor difference.

Note that this could still be fixed in the future by rolling out our own parser once the whole thing is more stable, so feel free to reopen if needed.

For reference, here are Sexplib:

the last one is interesting as it will determine the escaping done by sertop's printing. I'm not opposed to rolling our own versions if we have good reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants