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

Loading the default translations from a file to avoid heavy list operations #23

Merged
merged 3 commits into from
Dec 9, 2022

Conversation

akirak
Copy link
Contributor

@akirak akirak commented Dec 9, 2022

I am submitting this PR to address the following error which is commented at MELPA (melpa/melpa#8106):

lean4-input.el:185:1: Error: Lisp nesting exceeds `max-lisp-eval-depth'

My suggestion here is to read the data from an external JSON file, instead of hard-coding it in Emacs Lisp. Using json-parse-buffer which is built-in since Emacs 27, you can deserialize the file into a hash table where values are vectors. I assume the data is static, so it doesn't have to be customizable.

I used the following snippet to serialize the data:

(with-temp-buffer
  (let ((table (make-hash-table :test #'equal)))
    (unwind-protect
        (progn
          (map-do (lambda (key values)
                    (puthash key
                             (apply #'vector values)
                             table))
                  lean4-input-translations)
          (insert (json-serialize table)))
      (clrhash table)))
  (json-pretty-print (point-min) (point-max))
  (append-to-file (point-min) (point-max) "translations.json"))

The limitation of this solution is that you cannot add comments or organize the translation entries into groups. It's a limitation of JSON. Alternatively, you could dump the data into an elisp data file using prin1.

If you merge this PR, you have to update the MELPA recipe as follows. Otherwise, the data file won't become part of artifacts, so the input method won't work:

(lean4-mode :fetcher github :repo "leanprover/lean4-mode"
            :files (:defaults ("data" "data/*.json")))

Hope this PR helps and lean4-mode will become available from MELPA!

Expanding `max-lisp-eval-depth' indicates it is doing a heavy list operation.

With the built-in JSON support of Emacs, it is possible to load data directly
into vectors, which minimizes list operations. Nowadays, IO is not a bottleneck,
so reading from a file won't sacrifice performance.
@Kha
Copy link
Contributor

Kha commented Dec 9, 2022

Thanks, this is great! We should use this opportunity to sync up with the main "source of truth", which is https://github.com/leanprover/vscode-lean4/blob/master/vscode-lean4/src/abbreviation/abbreviations.json. Could you adapt your code to that format? I think just copying the file into this repo is fine.

@akirak
Copy link
Contributor Author

akirak commented Dec 9, 2022

Could you adapt your code to that format?

It is possible to adopt the code, but some entries have {$CURSOR}, which apparently are snippets and unsuitable for support with the input method. Is it okay to drop them?

@Kha
Copy link
Contributor

Kha commented Dec 9, 2022

Yes, dropping them sounds good.

@akirak
Copy link
Contributor Author

akirak commented Dec 9, 2022

Okay, I will work on it from now.

@akirak
Copy link
Contributor Author

akirak commented Dec 9, 2022

I have made a change to use the data for the vscode extension.

@Kha Kha merged commit 89d6bfa into leanprover-community:master Dec 9, 2022
@Kha
Copy link
Contributor

Kha commented Dec 9, 2022

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants