5
$\begingroup$

Those who don't have/prefer the front end of Mathematica will either use a plain ASCII editor to write a script in an .m file or just use Mathematica directly from a command-line terminal. In either case, everything must be supplied in InputForm.

When working with a Mathematica package, commonly used symbols/functions might be excessively verbose in InputForm, and certain shortcuts/aliases would be greatly desired. Examples of built-in InputForm shortcuts are the infix operators + (Plus), - (Minus), ... and . (Dot).

Two questions that arise as a package developer:

  1. Can I make definitions to InputForm? That is, can I modify how input supplied as InputForm gets parsed into FullForm?

  2. (152744)


Example 1

<...> should correspond to AngleBracket. That is, <a,b,c> should correspond to AngleBracket[a,b,c].


Example 2

Remap character . to package function myDot:

  1. a.b.c should be no longer be parsed in InputForm as Dot[a,b,c]
  2. It should be parsed myDot[a,b,c].

(n.b. the solution for the analogous question in StandardForm assuming a front end is here.)

$\endgroup$
2
  • $\begingroup$ It would be helpful to provide an example of a shortcut you would like to have. $\endgroup$
    – Carl Woll
    Jul 31, 2017 at 17:06
  • $\begingroup$ @CarlWoll Good point, I've added a couple examples. $\endgroup$
    – QuantumDot
    Jul 31, 2017 at 17:17

1 Answer 1

6
$\begingroup$

Unfortunately, I don't think there is. The association between characters and symbol names is burned into the kernel (in the form of a "yacc" grammar), with complete information about associativity, precedence, and tokenization.

Consider something as simple as your dot example. Presumably you want 2.3 to be the real number 2.3, not myDot[2,3]. How would you express that? In the box world you can redefine RowBox[{_,",",_}], but that's only because the FE has done the hardw ork for you of figuring out that 2.3 is boxed as "2.3", not as a RowBox[{"2",".","3"}]. The < > example is worse (and would be hard to do in an FE as well), because you're changing < and > from seaparate, binary operators to a matched pair.

Basically, the FE provides natural places to attach rules, which when all you have is a raw string is not an easy thing to do.

The best I can recommend is to find an operator without built in meaning, like say \[CenterDot], and attach definitions to the corresponding symbol. If you have a keyboard which has a center dot key (or shortcut) that's convinient for you, you can just use that. Otherwise, you'd have to type in \[CenterDot] each time, at which point you may as well be typing myDot[...].

$\endgroup$
3
  • 1
    $\begingroup$ I was afraid this would be the case. I guess the next-best thing to do is to create additional definition of functions with shorter names that point to the longer named functions in the package. Unfortunately, such things wouldn't work in pattern matching if HoldPattern is used. $\endgroup$
    – QuantumDot
    Jul 31, 2017 at 22:01
  • $\begingroup$ Isn't the limitation even more fundamental than that because any definition added by a package (to free operator built-ins) runs the risk of overriding/interacting with end-users' own free- symbol definitions? As there doesn't appear to be any way to assign contexts to these free symbols it seems that they should not be used at all in any package. $\endgroup$ Jan 19, 2020 at 1:30
  • $\begingroup$ This issue is explained in more detail here where it rears its ugly head again. $\endgroup$ Feb 19, 2020 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.