-
Does anyone have a good emacs function for transposing Prolog terms? example:
(* is position of marker)
# before
numthings_(Max, * nonterminal([H|T], X)) -->
numthings_(Max, terminal(H)),
numthings_(Max, nonterminal(T)).
# after
numthings_(nonterminal([H|T], X), Max) -->
numthings_(Max, terminal(H)),
numthings_(Max, nonterminal(T)).
# before
numthings_(nonterminal([H|T], X), Max) -->
numthings_(Max, terminal(H)), *
numthings_(Max, nonterminal(T)).
# after
numthings_(nonterminal([H|T], X), Max) -->
numthings_(Max, nonterminal(T)),
numthings_(Max, terminal(H)). I feel like I've seen @triska do this in his videos and it makes me jealous! Edit: I know the Prolog code is nonsensical, I added an extra argument into the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
We can write rather ad hoc Elisp functions that do this and work correctly most of the time, or at least much of the time, or maybe rather: some of the time, or maybe I should say: There is at least one type of situation where they work correctly. Please see below for a suggestion in this direction, for transposing goals in a way that sometimes works correctly. This is an interesting challenge, and I think it would be good to really think about this: How can we truly solve this so that it always works correctly? For example, can the Scryer parser communicate useful information about term positions to Emacs? What is the minimum that needs to be conveyed so that a function can be written that always does this correctly, or at least throws an error in cases where it would otherwise yield wrong results? Could other use cases also benefit from such information that the parser gathers and communicates to external programs? Our ambition should be to write programs that work correctly in all situations. It is OK to indicate, via exceptions, a situation that the program cannot resolve. It is not OK to yield wrong results. (defun prolog-sometimes-correctly-transpose-goals () (interactive) (transpose-lines 1) (save-excursion (move-end-of-line -1) (when (eq ?. (char-before)) (delete-char -1) (insert ",") (move-end-of-line 2) (when (eq ?, (char-before)) (delete-char -1) (insert "."))))) |
Beta Was this translation helpful? Give feedback.
-
(defun prolog-sometimes-correctly-transpose-goals () I am very impressed at your determination! Up until now I had assumed adding the As long as there's no fundamental reason why Scryer can't parse commas, I see no reason why this shouldn't be a perfect job for Scryer! |
Beta Was this translation helpful? Give feedback.
-
If we restrict ourselves only to static and non-multifile predicates it is doable, but it would be a challenge if you want also to safely switch arguments in all occurrences, (which is imperative to make this feature useful). I would vote for adding it to Prolog language server, which if invoked from emacs also counts as an elisp function ;). |
Beta Was this translation helpful? Give feedback.
We can write rather ad hoc Elisp functions that do this and work correctly most of the time, or at least much of the time, or maybe rather: some of the time, or maybe I should say: There is at least one type of situation where they work correctly. Please see below for a suggestion in this direction, for transposing goals in a way that sometimes works correctly.
This is an interesting challenge, and I think it would be good to really think about this: How can we truly solve this so that it always works correctly? For example, can the Scryer parser communicate useful information about term positions to Emacs? What is the minimum that needs to be conveyed so that a function can be written th…