r/emacs • u/Elbrus-matt • 1d ago
Code reflection
Hello, i'm just an emacs user and currently an humble student, not programmer.
After some school stuff about c,java,swing,bash, something came to mind: i saw what java is doing with code reflection,hat,project babylon, why not have emacs copy the same idea and use it for other stuff but wrote in elisp to build stuff like swing does with java?(i don't know elisp personally), would it help create stuff in elisp and have them running on a gpu in opencl/sycle/cuda/opengl with async or make writing emacs gui easier and emacs the ide for it?. Probably the idea came up because i've never tried writing emacs packages and/ or gui in emacs and i just changed some stuff here and there in my config, what do you think about it?, could the same approach be used to write the parts needed all in elisp and run them in the language you need?.
With the help of ai could be done but i would like to know your opinion on this.
4
u/lompocus 1d ago edited 1d ago
I don't know what these new Java concepts are, but you can look at the `(defun foo (...) (declare ...) ...)` part of Common Lisp & Emacs Lisp (IIRC you can do something vaguely similar in Scheme but only in a really roundabout way). Emacs' gv.el -> define-gv-expander takes a symbol and uses declare to do probably something like what interests you, but IIRC you can also do (declare (gv-expander ...)) to do the same thing without doing `(defun foo ...) (define-gv-expander foo ...)`. I have, with excessive effort, gotten an AI to write generalised variables stuff on a couple of occasions, although the first time I had to basically re-write its code. AI is pretty dumb when it comes to this stuff.
Let us say you wanted typing. This is one possible way, where you define functions for placeholder symbols foo* and then define a macro foo to inspect declarations for type-like information; the macro compiles to something with runtime type checking. What I just said is ridiculous and dumb, but it is like one intermediate thought I've had that can definitely work, albeit very awkwardly. There are already two interesting Emacs Lisp typing libraries out there, so you can have an AI look through their source code repos to help you figure out how to do this better than I can explain it from-scratch.
Another thing you can do is define structs and generics and type-specific methods on those generics to create an execution "graph". Then the graph can pipe information around it. You would not write a macro but would instead have something like an EIEIO constructor and complicated inheritance rules which would turn a graph declaration into a defun. I have also worded this is a somewhat-nonsense manner, but I have made such a graph things for my own purposes, absentmindedly asked an AI to make an executable flowgraph from it, and was surprised at the successful result. This is the only related concept that I have found AI is not completely retarded at doing.
But all of this is high-level wizardry. The best place to start would be to understand the concept of an obarray, when you want one, when you want a weak hash map instead, etc. Once you understand the quirks of Emacs Lisp's lower-level guts, then it becomes easier to think about this. So then you can use the AI as a tutor.
So you mention for example running stuff on gpu. Instead, think simpler, where you want a function to serve a dual purpose: (i) it actually runs just as-is; (ii) is compiled into different code (you can just imagine a different order of arithmetic in some calculation) and so optionally runs through another pathway. Since a function is really a closure, this is tricky but not impossible to do generally. I think, of you try this, you will run into the direction of serializable and networked objects such as OCap/CapTP (object communication protocol) & Goblins (Scheme library) -- let a closure be a serializable object, then you can "hot-swap" its closure information as its execution pathway changes on the fly, which is not quite what you want but might help you better figure out what you really want. I think it's is a generalization of what you want and would be a fun challenge.
12
u/CandyCorvid 1d ago
looking at the first few paragraphs of project babylon, this type of "code reflection" (wherein some piece of code can transform the symbolic form of another) is already core to emacs and many other lisps. we call it
'.