wolframite icon indicating copy to clipboard operation
wolframite copied to clipboard

User-defined aliasing

Open light-matters opened this issue 9 months ago • 3 comments

Currently, aliases can be supplied by adding pairs to the relevant maps in wolframite.runtime.defaults, but this is not exposed to consumers of the wolframite library.

A first step would be making this available via a function call, probably at the point of explicit initialization (coming soon!).

light-matters avatar Apr 30 '24 07:04 light-matters

An issue that came up while we were discussing aliases was that it would be useful to have more complicated aliasing than the current 1<->1 swapping of symbols.

For example, I wanted to use (**2 x) instead of (Power x 2), but there is also an open issue (#29) that is a result of this limitation as well.

The first work-around is to include something like

(let [wl-convert #(convert/convert   % opts)
        wl-eval    #(evaluate/evaluate % opts)
        wl-eval-conv (comp wl-eval wl-convert)]

    (wl-eval-conv '(..= WolframitePower2 (fn [x] (Power x 2))))
)

in the core evaluator-init. and {'**2 'WolframitePower2} in a relevant alias map (...defaults.clj namespace). The reason for doing it this way is so that the underlying function isn't created at every eval.

This means that the definition has to be made in two place at once, however.

light-matters avatar Apr 30 '24 08:04 light-matters

A thought that occurred to me is that long-standing shortcuts like this could potentially be put in a wolfram package directly, as used already in the core namespace:

...
(wl-eval-conv '(Needs "Parallel`Developer`"))
...

I don't know if this would have any performance benefits. It's still not a solution for user-defined aliases in any case though.

light-matters avatar Apr 30 '24 08:04 light-matters

NOTE: Might need to reconsider using ** as a default alias for Power as I've just discovered that Wolfram uses this for NonCommutativeMultiply :(.

**i (for i in 2...9) should still be okay though.

light-matters avatar Apr 30 '24 08:04 light-matters

Already possible

You can already do this:

(wl/init! {:aliases '{** Power}})
(wl/eval '(** 2 5)) ; => 32
(wl/init!)
(wl/eval '(** 2 5) {:aliases '{** Power}}) ; => 32

I suggest we add info about this to the docs and then close this issue - and possibly make a new one for more powerful aliases. (Such as the experimental fn aliases explored in #58. WDYT, @light-matters ?

holyjak avatar Jun 28 '24 23:06 holyjak

Great, I like your thinking. I was about to open an issue about having two (multiple?) wolfram.clj files: one which is just simply the wolfram definitions and functions and an 'extended' one which has lots of usability definitions.

light-matters avatar Jun 29 '24 11:06 light-matters

I've added a 'Customizing Wolframite' section to the doc (the latest PR).

light-matters avatar Jun 29 '24 12:06 light-matters