Skip to content

Commit ae421de

Browse files
committed
PredefinedSymbol -> SymbolConstant...
and try to clearify what is up here.
1 parent 64f42cd commit ae421de

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

mathics/builtin/makeboxes.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
Low level Format definitions
66
"""
77

8-
from typing import Union
9-
108
import mpmath
119

1210
from mathics.builtin.base import Builtin, Predefined

mathics/core/symbols.py

+32-21
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ class Symbol(Atom, NumericOperators, EvalMixin):
336336
337337
All Symbols have a name that can be converted to string.
338338
339-
A Variable Symbol is a ``Symbol``` that is associated with a
339+
A Variable Symbol is a ``Symbol`` that is associated with a
340340
``Definition`` that has an ``OwnValue`` that determines its
341341
evaluation value.
342342
@@ -348,7 +348,7 @@ class Symbol(Atom, NumericOperators, EvalMixin):
348348
a constant value that cannot change. System`True and System`False
349349
are like this.
350350
351-
These however are in class PredefinedSymbol. See that class for
351+
These however are in class SymbolConstant. See that class for
352352
more information.
353353
354354
Symbol acts like Python's intern() built-in function or Lisp's
@@ -379,7 +379,7 @@ def __new__(cls, name: str, sympy_dummy=None):
379379
Allocate an object ensuring that for a given ``name`` and ``cls`` we get back the same object,
380380
id(object) is the same and its object.__hash__() is the same.
381381
382-
PredefinedSymbol's like System`True and System`False set
382+
SymbolConstant's like System`True and System`False set
383383
``value`` to something other than ``None``.
384384
385385
"""
@@ -639,37 +639,48 @@ def to_sympy(self, **kwargs):
639639
return builtin.to_sympy(self, **kwargs)
640640

641641

642-
class PredefinedSymbol(Symbol):
642+
class SymbolConstant(Symbol):
643643
"""
644-
A Predefined Symbol is a Constant Symbol of the Mathics system whose value can't
645-
be changed.
644+
A Symbol Constant is Symbol of the Mathics system whose value can't
645+
be changed and has a corresponding Python representation.
646646
647-
Therefore, like say, an Integer constant, we don't need to go
648-
through Definitions to get its value.
647+
Therefore, like an ``Integer`` constant such as ``Integer0``, we don't
648+
need to go through ``Definitions`` to get its Python-equivalent value.
649649
650-
As we do in numeric constants, a PredefinedSymbols Python-equivalent value not its string name
651-
but to its Python-equivalent value. For example for the predefined System`True we
652-
we can set its value to the Python ``True`` value.
650+
For example for the ``SymbolConstant`` ``System`True``, has its
651+
value set to the Python ``True`` value.
652+
653+
Note this is not the same thing as a Symbolic Constant like ``Pi``,
654+
which doesn't have an (exact) Python equivalent representation.
655+
656+
Also note that ``SymbolConstant`` differs from ``Symbol`` in that
657+
Symbol has no value field (even when its value happens to be
658+
representable in Python. Symbols need to go through Definitions
659+
get a Symbol's current value, based on the current context and the
660+
state of prior operations on that Symbol/Definition binding.
661+
662+
In sum, SymbolConstant is partly like Symbol, and partly like
663+
Numeric constants.
653664
"""
654665

655-
# Dictionary of PredefinedSymbols defined so far.
666+
# Dictionary of SymbolConstants defined so far.
656667
# We use this for object uniqueness.
657-
# The key is the PredefinedSymbol object's name, and the
668+
# The key is the SymbolConstant's value, and the
658669
# diectionary's value is the Mathics object representing that Python value.
659-
_predefined_symbols = {}
670+
_symbol_constants = {}
660671

661672
# We use __new__ here to unsure that two Integer's that have the same value
662673
# return the same object.
663674
def __new__(cls, name, value):
664675

665676
name = ensure_context(name)
666-
self = cls._predefined_symbols.get(name)
677+
self = cls._symbol_constants.get(name)
667678
if self is None:
668679
self = super().__new__(cls, name)
669680
self._value = value
670681

671682
# Cache object so we don't allocate again.
672-
self._predefined_symbols[name] = self
683+
self._symbol_constants[name] = self
673684

674685
# Set a value for self.__hash__() once so that every time
675686
# it is used this is fast. Note that in contrast to the
@@ -723,10 +734,10 @@ def symbol_set(*symbols: Tuple[Symbol]) -> FrozenSet[Symbol]:
723734

724735
# Symbols used in this module.
725736

726-
# Note, below we are only setting PredefinedSymbol for Symbols which
737+
# Note, below we are only setting SymbolConstant for Symbols which
727738
# are both predefined and have the Locked attribute.
728739

729-
# An experiment using PredefinedSymbol("Pi") in the Python code and
740+
# An experiment using SymbolConstant("Pi") in the Python code and
730741
# running:
731742
# {Pi, Unprotect[Pi];Pi=4; Pi, Pi=.; Pi }
732743
# show that this does not change the output in any way.
@@ -736,9 +747,9 @@ def symbol_set(*symbols: Tuple[Symbol]) -> FrozenSet[Symbol]:
736747
# more of the below and in systemsymbols
737748
# PredefineSymbol.
738749

739-
SymbolFalse = PredefinedSymbol("System`False", value=False)
740-
SymbolList = PredefinedSymbol("System`List", value=list)
741-
SymbolTrue = PredefinedSymbol("System`True", value=True)
750+
SymbolFalse = SymbolConstant("System`False", value=False)
751+
SymbolList = SymbolConstant("System`List", value=list)
752+
SymbolTrue = SymbolConstant("System`True", value=True)
742753

743754
SymbolAbs = Symbol("Abs")
744755
SymbolDivide = Symbol("Divide")

0 commit comments

Comments
 (0)