2
$\begingroup$

I'd like a function that checks whether a symbol is a predefined part of the language or not. More generally, it might check whether it's part of a given package.

isCore[symb_]:=MemberQ[Attributes[symb],Protected]

This seems to work (because predefined symbols tend to be protected), but feels like a hack.

$\endgroup$
3
  • 5
    $\begingroup$ It is a hack indeed. Context would give a better clue on where a symbol stems from, but the concept of package and context are somewhat fluid in Mathematica. $\endgroup$ Sep 5, 2021 at 18:08
  • 3
    $\begingroup$ Some undefined things like CenterDot[] are not protected, but have a defined relationship to input syntax through \[CenterDot]. There are other System` symbols like that. There are unprotected parameters like $UserName that are set by the system but can be reset by the user. Outside System` , there some things that are documented and might be considered part of the language (FEM and other system packages). Then there are undocumented commands, some of which might be argued belong, some not. $\endgroup$
    – Michael E2
    Sep 5, 2021 at 18:43
  • 3
    $\begingroup$ How about Names["System`*"]? $\endgroup$ Sep 5, 2021 at 22:56

1 Answer 1

3
$\begingroup$

System` is a context, not a library. In other languages, this is called a namespace. Therefore, technically speaking, the fact that a symbol is part of the System` context does not mean that it is built-in. You can easily create symbols in the System` context. In practice, however, this is often a good-enough approximation to find built-in symbols.

To determine the context of a symbol, use Context. To find all symbols in the System` context, use Names["System`*"].

Ultimately, it's hard to answer your question because it's unclear what you actually want to do. Determining whether a symbol is "built-in" is not a particularly useful thing. Whether checking a symbol's context is truly sufficient for your actual application (which you did not describe) we cannot say.

$\endgroup$
1
  • $\begingroup$ I've made a symbol named LHold, with HoldAll atribute, which is meant to be used in implementing lazy evaluation. The gist of the idea is the pattern f_[LHold[g_[x___]]] gets replaced by f[g[x]], but only when f itself is built-in (no further delays possible), and I haven't added any exceptions pertaining to the interaction of f and g. Eg. one possible such exception is g could be List and f could be First , whereby I could use patterns to get the first element without having to evaluate the rest. $\endgroup$ Sep 6, 2021 at 8:54

Your Answer

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

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