12
$\begingroup$

How can I check if a symbol is pre-defined by Mathematica, in a way that's backwards compatible with old versions?

i.e. I would like some CoreLanguageQ[x_Symbol] which matches CoreLangaugeQ[Print], but not CoreLanguageQ[f]

Then it can be used to help make code backwards compatible, eg.

If[!CoreLanguageQ[Echo], Echo[x_]:= (Print[x];x)]

Thank you.

$\endgroup$
  • 5
    $\begingroup$ Maybe something like CoreLanguageQ[x_Symbol] := Context[x]==="System`" ? $\endgroup$ – QuantumDot Jun 19 '18 at 1:05
  • 8
    $\begingroup$ Mathematica's "core language" is ill defined. As @QuantumDot notes, the best you can probably do is check whether a symbol is in the "System`" context. Package development aspects of the language weren't terribly well designed so the language is somewhat bloated in this regard. It does not have a tight "core language" like most modern languages. $\endgroup$ – b3m2a1 Jun 19 '18 at 1:42
  • 1
    $\begingroup$ My definition is 'any symbol that's defined when I restart the kernel'. I will try System, thankyou. $\endgroup$ – Joe Jun 19 '18 at 19:05
9
$\begingroup$

You can look it up using WolframLanguageData, which has a list of all pre-defined symbols:

ClearAll[CoreLanguageQ]
SetAttributes[CoreLanguageQ, HoldAll]
CoreLanguageQ[x_] := With[{name = SymbolName[x]}, CoreLanguageQ[name]]
CoreLanguageQ[x_String] := With[{
   names = WolframLanguageData[All, "Name"]
   }, MemberQ[names, x]]

CoreLanguageQ[Plot]
(* Out: True *)

CoreLanguageQ[Plott]
(* Out: False *)

Looking at the context of the symbol is also a viable approach. However, the WolframLanguageData approach is appealing because the documentation states that

WolframLanguageData[] gives a list of all Wolfram Language symbols.

Which is to say, the list returned by this function is by definition Wolfram Language. This is as close as a definition of "core language" that we can come.

For the problem in the updated question, it would seem appropriate to check if a symbol exists in the System context like QuantumDot suggests in a comment.

The only backward compatible way to use this would be to create lists of functions available for specific versions and include them in your code. For example:

names = WolframLanguageData[All, "Name"];
versionIntroduced = WolframLanguageData[All, "VersionIntroduced"];
allowed = Pick[names, Thread[versionIntroduced <= 10]];

In this code, allowed holds all the symbols that exist in version 10, presuming that no symbol previously introduced was removed.

$\endgroup$
  • $\begingroup$ Ah I apologise. This answers my original question, but now I see that my original question wasn't what I wanted to ask. WolframLanguageData[] is a relatively new function and I want to use this for backwards compatability; eg. If[!CoreLanguageQ[Echo], Echo[x_]:= (Print[x];x)]. I've updated my question $\endgroup$ – Joe Jun 19 '18 at 19:03
  • $\begingroup$ @Joe ok, then QuantumDot's suggestion seems more appropriate. $\endgroup$ – C. E. Jun 19 '18 at 19:29
  • $\begingroup$ Yes I think so. Thankyou for your solution though it's nice $\endgroup$ – Joe Jun 20 '18 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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