Skip to content

Commit ef51773

Browse files
mmaterarocky
authored andcommitted
Handle optional with a first element that is not a Pattern[]
1 parent f66757a commit ef51773

File tree

2 files changed

+14
-80
lines changed

2 files changed

+14
-80
lines changed

mathics/builtin/attributes.py

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -654,81 +654,6 @@ class ReadProtected(Predefined):
654654
summary_text = "attribute of symbols with hidden definitions"
655655

656656

657-
class Flat(Predefined):
658-
"""
659-
<dl>
660-
<dt>'Flat'
661-
<dd>is an attribute that specifies that nested occurrences of
662-
a function should be automatically flattened.
663-
</dl>
664-
665-
A symbol with the 'Flat' attribute represents an associative
666-
mathematical operation:
667-
>> SetAttributes[f, Flat]
668-
>> f[a, f[b, c]]
669-
= f[a, b, c]
670-
671-
'Flat' is taken into account in pattern matching:
672-
>> f[a, b, c] /. f[a, b] -> d
673-
= f[d, c]
674-
675-
#> SetAttributes[{u, v}, Flat]
676-
#> u[x_] := {x}
677-
#> u[]
678-
= u[]
679-
#> u[a]
680-
= {a}
681-
#> u[a, b]
682-
: Iteration limit of 1000 exceeded.
683-
= $Aborted
684-
#> u[a, b, c]
685-
: Iteration limit of 1000 exceeded.
686-
= $Aborted
687-
#> v[x_] := x
688-
#> v[]
689-
= v[]
690-
#> v[a]
691-
= a
692-
#> v[a, b] (* in Mathematica: Iteration limit of 4096 exceeded. *)
693-
= v[a, b]
694-
#> v[a, b, c] (* in Mathematica: Iteration limit of 4096 exceeded. *)
695-
: Iteration limit of 1000 exceeded.
696-
= $Aborted
697-
"""
698-
699-
summary_text = "attribute for associative symbols"
700-
701-
702-
class Orderless(Predefined):
703-
"""<dl>
704-
<dt>'Orderless'
705-
<dd>is an attribute that can be assigned to a symbol $f$ to
706-
indicate that the elements $ei$ in expressions of the form
707-
$f$[$e1$, $e2$, ...] should automatically be sorted into
708-
canonical order. This property is accounted for in pattern
709-
matching.
710-
</dl>
711-
712-
The leaves of an 'Orderless' function are automatically sorted:
713-
>> SetAttributes[f, Orderless]
714-
>> f[c, a, b, a + b, 3, 1.0]
715-
= f[1., 3, a, b, c, a + b]
716-
717-
A symbol with the 'Orderless' attribute represents a commutative
718-
mathematical operation.
719-
>> f[a, b] == f[b, a]
720-
= True
721-
722-
'Orderless' affects pattern matching:
723-
>> SetAttributes[f, Flat]
724-
>> f[a, b, c] /. f[a, c] -> d
725-
= f[b, d]
726-
727-
"""
728-
729-
summary_text = "attribute for commutative symbols"
730-
731-
732657
class SequenceHold(Predefined):
733658
"""
734659
<url>

mathics/core/pattern.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,20 @@ def yield_head(head_vars, _):
393393
elif pat_elem.get_head_name() == "System`Optional":
394394
if len(pat_elem.elements) == 2:
395395
pat, value = pat_elem.elements
396-
assert pat.get_head_name() == "System`Pattern"
397-
key = pat.elements[0].atom.name
396+
if pat.get_head_name() == "System`Pattern":
397+
key = pat.elements[0].atom.name
398+
else:
399+
# if the first element of the Optional
400+
# is not a `Pattern`, then we need to
401+
# store an empty element.
402+
key = ""
398403
optionals[key] = value
399404
elif len(pat_elem.elements) == 1:
400405
pat = pat_elem.elements[0]
401-
assert pat.get_head_name() == "System`Pattern"
402-
key = pat.elements[0].atom.name
406+
if pat.get_head_name() == "System`Pattern":
407+
key = pat.elements[0].atom.name
408+
else:
409+
key = ""
403410
# Now, determine the default value
404411
defaultvalue_expr = Expression(
405412
SymbolDefault, pattern_head, Integer(default_indx)
@@ -420,7 +427,9 @@ def yield_head(head_vars, _):
420427
if len(optionals) == 0:
421428
return
422429

423-
# Load the default values in vars
430+
# Remove the empty key and load the default values in vars
431+
if "" in optionals:
432+
del optionals[""]
424433
vars.update(optionals)
425434
# Try to match the non-optional element with the expression
426435
new_pattern.match(

0 commit comments

Comments
 (0)