Академический Документы
Профессиональный Документы
Культура Документы
http://maxima.sourceforge.net//
Abstract
This article is an educated guess on how Mathematicas Blank, BlankSequence and
BlankNullSequence serving as basis for pattern matching work.
{{{a, b}, {b, c}, {c, d}}, {{e, f}, {f, t}, {t, k},
{k, h, i}}, {{l, m}, {m, n}}, {j, k, l}}
2. Conceptual Description
The basic idea behind these sequences is that it allocates the allowed number of elements to
a particular sequence. Blank must get one and only one element, BlankSequence might hold
one or more and BlankNullSequence might hold none or more. First example in article is a
good example for this description. To decide the allocation for each next pattern there is need
to backtrack up to some point and reallocate list elements from there to the present pattern.
That some point is nothing else but BlankSequence(__) or BlankNullSequence(___). Lets
call these containers. Once one of these containers is reached while backtracking then there
is no need to go before this.
{1,2,3,4,5,6,7,8,9,0}/.{a_,b_,c__,d_,e_}:>{{a},{b},{c},{d},{e}}
{{1}, {2}, {3, 4, 5, 6, 7, 8}, {9}, {0}}
{1,2,3,4,5,6,7,8,9,0}/.{a_,b_,c__,d__,e_}:>{{a},{b},{c},{d},{e}}
{{1}, {2}, {3}, {4, 5, 6, 7, 8, 9}, {0}}
In these two examples above, in the first one once control has reached container c, for d,e
content is taken from c.Allocations before c remain untouched irrespective of what follows
c.Pattern is considered a failure if pattern after c could not be satisfied with the input from
c as in the following case.
{1,2,3,4,5}/.{a_,b__,c__,d_,e_,f_}:>{{a},{b},{c},{d},{e},{f}}
{1, 2, 3, 4, 5}
To understand how it backtracks, lets take two examples,
{1, 2, 3, 4, 5} /. {a___, b_} :> {{a}, {b}}
{{1, 2, 3, 4}, {5}}
{1, 2, 3, 4, 5} /. {a___, b_, c_} :> {{a}, {b}, {c}}
{{1, 2, 3}, {4}, {5}}
In first case, b is 5 while when it finds one more Blank following it, it again starts allocation
from beginning Blank and now b gets 4 and c gets 5. In case of more such Blank patterns,
each time allocation starts from first blank in queue to the current Blank.
In case all the patterns are Blank then pattern match is successful only if number of Blank
are equal to number of elements in list.
3. Implementation details
3.1. Unique identity for every element
A good approach to address the problem of finding complement of duplicate elements is to
not have them during the processing at all.There is need to find complement when a stopping
container is followed by another container and allocation of elements happens from beginning
of list from last sub-list of stopping container to the patterns in between, the remaining
elements are kept in last container. So, one can use Maxima built in feature gensym() to
generate a unique symbol for all the list elements so internally the program handles unique
symbols rather than actual output. As part of post processing, one can substitute these in
received results.
list_elements=[1,2,3,4,5]
Could be one of,[a_,[1]],[a_,[2]]....[a_,[5]]
Because BlankSequence must have at least one element,[a__,[1],[2,3,4,5]]
BlankNullSequence can have none or more, [a___,[],[1,2,3,4,5]]
In case of BlankSequence(__) and BlankNullSequence(___) the operations performed on
the last sub-list are same but segregating information differently helps out in keeping the
working mechanism for both same.The second element in both these cases never gets touched
once decided as stopping container.
5. Source
One can avail the source code at https://github.com/pankajsejwal/pattern-matching