Skip to content

Commit 37e0a70

Browse files
committed
Forgot to add formtest.py
Note: we need to implement FormatValues()
1 parent 5d226c9 commit 37e0a70

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

test/formtest.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Example to show how new Format-directed Boxing might work.
3+
4+
This is a minimal example so we can discuss feasiblity.
5+
"""
6+
from mathics.builtin.forms.base import box
7+
from mathics.core.atoms import IntegerM1, Integer1, Rational
8+
from mathics.core.expression import Expression
9+
from mathics.core.symbols import SymbolDivide
10+
from mathics.core.systemsymbols import (
11+
SymbolMakeBoxes,
12+
SymbolPower,
13+
SymbolSqrt,
14+
SymbolStandardForm,
15+
)
16+
from mathics.session import MathicsSession
17+
18+
session = MathicsSession(character_encoding="ASCII")
19+
20+
21+
# Hacky pseudo boxing rules.
22+
# Currently in MakeBox rule rewriting occurs via MakeBox rewrite rules
23+
# which are attached to various Builtin classes.
24+
25+
# The rewrite is performed as a part of rewriting portion of Expression evaluation.
26+
# We probably want to segregate these rules from other kinds of rules.
27+
#
28+
# The below hacky code is to simulate the rule behavior for
29+
# just a few kinds of things so we don't have to hook into the
30+
# complex rewrite mechanism in use in general evaluation. The implementation we use
31+
# is just good enough for the new kinds of things we need.
32+
# It is not intended to be used in a final implementation.
33+
34+
35+
def sqrt_boxform_fn(expr):
36+
return Expression(SymbolSqrt, expr.elements[0])
37+
38+
39+
def power_boxform_fn(expr):
40+
new_expr = expr.elements[0]
41+
if new_expr.elements[-1] == IntegerM1:
42+
return Expression(SymbolDivide, Integer1, new_expr.elements[0])
43+
elif new_expr.elements[-1] == Rational(1, 2):
44+
return Expression(SymbolSqrt, new_expr.elements[0])
45+
return new_expr
46+
47+
48+
boxform_rules = {SymbolSqrt: sqrt_boxform_fn, SymbolPower: power_boxform_fn}
49+
50+
51+
def apply_formatvalues_rules(expr, evaluation):
52+
"""
53+
Hacky replacement for rules() found in Expression rewrite_apply_eval().
54+
Note, we need to add builtin FormatValues() and the internals that go with that.
55+
"""
56+
if expr.elements[-1] not in (SymbolStandardForm,): # Or more generally $BoxForms
57+
# For other forms, there might be other transformations too, and this should
58+
# be discussed.
59+
# For simplicity, we will just handle a small number of $BoxForms rules
60+
return expr
61+
62+
# Remove the Form from expression "expr"
63+
unboxed_expr = expr.elements[0]
64+
65+
if unboxed_expr.head in boxform_rules:
66+
new_expr = boxform_rules[unboxed_expr.head](expr)
67+
return new_expr
68+
return unboxed_expr
69+
70+
71+
# Begin demo code.
72+
73+
for expr_str in (
74+
"1 / x", # Show off "Division" boxing
75+
"a ^ b", # Show off "Power" boxing
76+
"Sqrt[a]", # Show off "Square-root boxing"
77+
):
78+
expr = session.evaluate(expr_str)
79+
print("Input expression: ", expr)
80+
81+
# Here is how Mathics currently evaluates MakeBoxes
82+
boxed_expr = Expression(SymbolMakeBoxes, expr, SymbolStandardForm)
83+
print("Mathics MakeBoxes: ", boxed_expr)
84+
print("Eval'd Makeboxes: ", boxed_expr.evaluate(session.evaluation))
85+
86+
# Here is how Mathics might better box an expression.
87+
# First we apply MakeBox boxing transformation rules.
88+
# This handles expression rewriting.
89+
transformed_boxed_expr = apply_formatvalues_rules(boxed_expr, session.evaluation)
90+
91+
boxed_expr2 = box(transformed_boxed_expr, session.evaluation, SymbolStandardForm)
92+
print("New MakeBoxes: ", boxed_expr2)
93+
print("-" * 30)
94+
print("")

0 commit comments

Comments
 (0)