13

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.

Jojo
  • 1,278
  • 8
  • 19
  • 5
    Maybe something like CoreLanguageQ[x_Symbol] := Context[x]==="System`" ? – QuantumDot Jun 19 '18 at 01:05
  • 8
    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. – b3m2a1 Jun 19 '18 at 01:42
  • 1
    My definition is 'any symbol that's defined when I restart the kernel'. I will try System, thankyou. – Jojo Jun 19 '18 at 19:05

1 Answers1

10

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.

C. E.
  • 70,533
  • 6
  • 140
  • 264
  • 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 – Jojo Jun 19 '18 at 19:03
  • @Joe ok, then QuantumDot's suggestion seems more appropriate. – C. E. Jun 19 '18 at 19:29
  • Yes I think so. Thankyou for your solution though it's nice – Jojo Jun 20 '18 at 17:17
  • You might want to add some AtomQ check. This produces an error for me when x is not atomic. – Kvothe Jun 22 '22 at 22:11