Skip to content

Commit

Permalink
Merge pull request #741 from Mathics3/apply-reduction-5
Browse files Browse the repository at this point in the history
apply->eval, long lines, some annotations...
  • Loading branch information
rocky authored Jan 12, 2023
2 parents 29194d8 + 0e1b656 commit 6dab518
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 137 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/isort-and-black-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ jobs:
run: pip install 'click==8.0.4' 'black==22.3.0' 'isort==5.10.1'
- name: Run isort --check .
run: isort --check .
- name: Run isort --check .
- name: Run black --check .
run: black --check .
- name: If needed, commit black changes to the pull request
if: failure()
run: |
black .
git config --global user.name 'autoblack'
git config --global user.email '[email protected]'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
git checkout $GITHUB_HEAD_REF
git commit -am "fixup: Format Python code with Black"
git push
# - name: If needed, commit black changes to the pull request
# if: failure()
# run: |
# black .
# git config --global user.name 'autoblack'
# git config --global user.email '[email protected]'
# git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
# git checkout $GITHUB_HEAD_REF
# git commit -am "fixup: Format Python code with Black"
# git push
2 changes: 1 addition & 1 deletion mathics/algorithm/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def get_part(expression: BaseElement, indices: List[int]) -> BaseElement:
"""Extract part of ``expression`` specified by ``indicies`` and
"""Extract part of ``expression`` specified by ``indices`` and
return that.
"""

Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,15 @@ def _get_unavailable_function(self) -> Optional[Callable]:
of the class. Otherwise, returns ``None``.
"""

def apply_unavailable(**kwargs): # will override apply method
def eval_unavailable(**kwargs): # will override apply method
kwargs["evaluation"].message(
"General",
"pyimport", # see inout.py
strip_context(self.get_name()),
)

requires = getattr(self, "requires", [])
return None if check_requires_list(requires) else apply_unavailable
return None if check_requires_list(requires) else eval_unavailable

def get_option_string(self, *params):
s = self.get_option(*params)
Expand Down
36 changes: 21 additions & 15 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-

"""
Formatting constructs are represented as a hierarchy of low-level symbolic "boxes".
Formatting constructs are represented as a hierarchy of low-level \
symbolic "boxes".
The routines here assist in boxing at the bottom of the hierarchy. At the other end, the top level, we have a Notebook which is just a collection of Expressions usually contained in boxes.
The routines here assist in boxing at the bottom of the hierarchy. \
At the other end, the top level, we have a Notebook which is just a \
collection of Expressions usually contained in boxes.
"""

from mathics.builtin.base import Builtin
Expand Down Expand Up @@ -70,7 +73,8 @@ class ButtonBox(BoxExpression):
"""
<dl>
<dt>'ButtonBox[$boxes$]'
<dd> is a low-level box construct that represents a button in a notebook expression.
<dd> is a low-level box construct that represents a button \
in a notebook expression.
</dl>
"""

Expand Down Expand Up @@ -102,7 +106,7 @@ class FractionBox(BoxExpression):
"FractionLine": "Automatic",
}

def apply(self, num, den, evaluation, options):
def eval(self, num, den, evaluation, options):
"""FractionBox[num_, den_, OptionsPattern[]]"""
num_box, den_box = (
to_boxes(num, evaluation, options),
Expand Down Expand Up @@ -183,11 +187,11 @@ class InterpretationBox(BoxExpression):
attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
summary_text = "box associated to an input expression"

def apply_to_expression(boxexpr, form, evaluation):
def eval_to_expression(boxexpr, form, evaluation):
"""ToExpression[boxexpr_IntepretationBox, form___]"""
return boxexpr.elements[1]

def apply_display(boxexpr, evaluation):
def eval_display(boxexpr, evaluation):
"""DisplayForm[boxexpr_IntepretationBox]"""
return boxexpr.elements[0]

Expand All @@ -209,7 +213,7 @@ class RowBox(BoxExpression):
def __repr__(self):
return "RowBox[List[" + self.items.__repr__() + "]]"

def apply_list(self, boxes, evaluation):
def eval_list(self, boxes, evaluation):
"""RowBox[boxes_List]"""
boxes = boxes.evaluate(evaluation)
items = tuple(to_boxes(b, evaluation) for b in boxes.elements)
Expand Down Expand Up @@ -305,15 +309,15 @@ class SqrtBox(BoxExpression):
"MinSize": "Automatic",
}

def apply_index(self, radicand, index, evaluation, options):
def eval_index(self, radicand, index, evaluation, options):
"""SqrtBox[radicand_, index_, OptionsPattern[]]"""
radicand_box, index_box = (
to_boxes(radicand, evaluation, options),
to_boxes(index, evaluation, options),
)
return SqrtBox(radicand_box, index_box, **options)

def apply(self, radicand, evaluation, options):
def eval(self, radicand, evaluation, options):
"""SqrtBox[radicand_, OptionsPattern[]]"""
radicand_box = to_boxes(radicand, evaluation, options)
return SqrtBox(radicand_box, None, **options)
Expand Down Expand Up @@ -346,11 +350,11 @@ class StyleBox(BoxExpression):
attributes = A_PROTECTED | A_READ_PROTECTED
summary_text = "associate boxes with styles"

def apply_options(self, boxes, evaluation, options):
def eval_options(self, boxes, evaluation, options):
"""StyleBox[boxes_, OptionsPattern[]]"""
return StyleBox(boxes, style="", **options)

def apply_style(self, boxes, style, evaluation, options):
def eval_style(self, boxes, style, evaluation, options):
"""StyleBox[boxes_, style_String, OptionsPattern[]]"""
return StyleBox(boxes, style=style, **options)

Expand Down Expand Up @@ -401,7 +405,7 @@ class SubscriptBox(BoxExpression):
"MultilineFunction": "Automatic",
}

def apply(self, a, b, evaluation, options):
def eval(self, a, b, evaluation, options):
"""SubscriptBox[a_, b__, OptionsPattern[]]"""
a_box, b_box = (
to_boxes(a, evaluation, options),
Expand Down Expand Up @@ -439,7 +443,7 @@ class SubsuperscriptBox(BoxExpression):
"MultilineFunction": "Automatic",
}

def apply(self, a, b, c, evaluation, options):
def eval(self, a, b, c, evaluation, options):
"""SubsuperscriptBox[a_, b__, c__, OptionsPattern[]]"""
a_box, b_box, c_box = (
to_boxes(a, evaluation, options),
Expand Down Expand Up @@ -481,7 +485,7 @@ class SuperscriptBox(BoxExpression):
"MultilineFunction": "Automatic",
}

def apply(self, a, b, evaluation, options):
def eval(self, a, b, evaluation, options):
"""SuperscriptBox[a_, b__, OptionsPattern[]]"""
a_box, b_box = (
to_boxes(a, evaluation, options),
Expand All @@ -505,7 +509,9 @@ def to_expression(self):

class TagBox(BoxExpression):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/TagBox.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/TagBox.html</url>
<dl>
<dt>'TagBox[boxes, tag]'
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/distance/numeric.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Numercial Data
Numerical Data
"""

from mathics.builtin.base import Builtin
Expand Down
69 changes: 43 additions & 26 deletions mathics/builtin/list/constructing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@

from mathics.builtin.base import Builtin, IterationFunction, Pattern
from mathics.builtin.lists import get_tuples
from mathics.core.atoms import Integer, Symbol
from mathics.core.atoms import Integer
from mathics.core.attributes import A_HOLD_FIRST, A_LISTABLE, A_PROTECTED
from mathics.core.convert.expression import to_expression
from mathics.core.convert.sympy import from_sympy
from mathics.core.element import ElementsProperties
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression, structure
from mathics.core.list import ListExpression
from mathics.core.symbols import Atom

SymbolNormal = Symbol("Normal")
from mathics.core.systemsymbols import SymbolNormal


class Array(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Array.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Array.html</url>
<dl>
<dt>'Array[$f$, $n$]'
Expand All @@ -36,10 +38,12 @@ class Array(Builtin):
<dd>returns the $n$-element list '{$f$[$a$], ..., $f$[$a$ + $n$]}'.
<dt>'Array[$f$, {$n$, $m$}, {$a$, $b$}]'
<dd>returns an $n$-by-$m$ matrix created by applying $f$ to indices ranging from '($a$, $b$)' to '($a$ + $n$, $b$ + $m$)'.
<dd>returns an $n$-by-$m$ matrix created by applying $f$ to indices \
ranging from '($a$, $b$)' to '($a$ + $n$, $b$ + $m$)'.
<dt>'Array[$f$, $dims$, $origins$, $h$]'
<dd>returns an expression with the specified dimensions and index origins, with head $h$ (instead of 'List').
<dd>returns an expression with the specified dimensions and index origins, \
with head $h$ (instead of 'List').
</dl>
>> Array[f, 4]
Expand Down Expand Up @@ -70,7 +74,7 @@ class Array(Builtin):

summary_text = "form an array by applying a function to successive indices"

def apply(self, f, dimsexpr, origins, head, evaluation):
def eval(self, f, dimsexpr, origins, head, evaluation: Evaluation):
"Array[f_, dimsexpr_, origins_:1, head_:List]"

if dimsexpr.has_form("List", None):
Expand Down Expand Up @@ -115,7 +119,9 @@ def rec(rest_dims, current):

class ConstantArray(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/ConstantArray.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/ConstantArray.html</url>
<dl>
<dt>'ConstantArray[$expr$, $n$]'
Expand All @@ -137,7 +143,9 @@ class ConstantArray(Builtin):

class Normal(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Normal.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Normal.html</url>
<dl>
<dt>'Normal[expr_]'
Expand All @@ -147,7 +155,7 @@ class Normal(Builtin):

summary_text = "convert objects to normal expressions"

def apply_general(self, expr, evaluation):
def eval_general(self, expr, evaluation: Evaluation):
"Normal[expr_]"
if isinstance(expr, Atom):
return
Expand All @@ -159,7 +167,9 @@ def apply_general(self, expr, evaluation):

class Range(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Range.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Range.html</url>
<dl>
<dt>'Range[$n$]'
Expand All @@ -186,7 +196,7 @@ class Range(Builtin):

summary_text = "form a list from a range of numbers or other objects"

def apply(self, imin, imax, di, evaluation):
def eval(self, imin, imax, di, evaluation: Evaluation):
"Range[imin_?RealNumberQ, imax_?RealNumberQ, di_?RealNumberQ]"

if (
Expand All @@ -213,7 +223,9 @@ def apply(self, imin, imax, di, evaluation):

class Permutations(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Permutations.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Permutations.html</url>
<dl>
<dt>'Permutations[$list$]'
Expand Down Expand Up @@ -248,17 +260,17 @@ class Permutations(Builtin):

summary_text = "form permutations of a list"

def apply_argt(self, evaluation):
def eval_argt(self, evaluation: Evaluation):
"Permutations[]"
evaluation.message(self.get_name(), "argt")

def apply(self, li, evaluation):
def eval(self, li, evaluation: Evaluation):
"Permutations[li_List]"
return ListExpression(
*[ListExpression(*p) for p in permutations(li.elements, len(li.elements))],
)

def apply_n(self, li, n, evaluation):
def eval_n(self, li, n, evaluation: Evaluation):
"Permutations[li_List, n_]"

rs = None
Expand Down Expand Up @@ -291,11 +303,15 @@ def apply_n(self, li, n, evaluation):

class Reap(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Reap.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Reap.html</url>
<dl>
<dt>'Reap[$expr$]'
<dd>gives the result of evaluating $expr$, together with all values sown during this evaluation. Values sown with different tags are given in different lists.
<dd>gives the result of evaluating $expr$, together with all values \
sown during this evaluation. Values sown with different tags \
are given in different lists.
<dt>'Reap[$expr$, $pattern$]'
<dd>only yields values sown with a tag matching $pattern$.
Expand All @@ -305,7 +321,8 @@ class Reap(Builtin):
<dd>uses multiple patterns.
<dt>'Reap[$expr$, $pattern$, $f$]'
<dd>applies $f$ on each tag and the corresponding values sown in the form '$f$[tag, {e1, e2, ...}]'.
<dd>applies $f$ on each tag and the corresponding values sown \
in the form '$f$[tag, {e1, e2, ...}]'.
</dl>
>> Reap[Sow[3]; Sow[1]]
Expand Down Expand Up @@ -339,7 +356,7 @@ class Reap(Builtin):
"Reap[expr_]": "Reap[expr, _]",
}

def apply(self, expr, patterns, f, evaluation):
def eval(self, expr, patterns, f, evaluation: Evaluation):
"Reap[expr_, {patterns___}, f_]"

patterns = patterns.get_sequence()
Expand Down Expand Up @@ -396,7 +413,7 @@ class Sow(Builtin):
"Sow[e_, tag_]": "Sow[e, {tag}]",
}

def apply(self, e, tags, evaluation):
def eval(self, e, tags, evaluation: Evaluation):
"Sow[e_, {tags___}]"

tags = tags.get_sequence()
Expand Down Expand Up @@ -496,14 +513,14 @@ class Tuples(Builtin):

summary_text = "form n-tuples from a list"

def apply_n(self, expr, n, evaluation):
def eval_n(self, expr, n: Integer, evaluation: Evaluation):
"Tuples[expr_, n_Integer]"

if isinstance(expr, Atom):
evaluation.message("Tuples", "normal")
return
n = n.get_int_value()
if n is None or n < 0:
py_n = n.value
if py_n is None or py_n < 0:
evaluation.message("Tuples", "intnn")
return
items = expr.elements
Expand All @@ -518,10 +535,10 @@ def iterate(n_rest):
yield [item] + rest

return ListExpression(
*(Expression(expr.head, *elements) for elements in iterate(n))
*(Expression(expr.head, *elements) for elements in iterate(py_n))
)

def apply_lists(self, exprs, evaluation):
def eval_lists(self, exprs, evaluation: Evaluation):
"Tuples[{exprs___}]"

exprs = exprs.get_sequence()
Expand Down
Loading

0 comments on commit 6dab518

Please sign in to comment.