How does math deal with ambiguous definitions?

I read about how symbolic languages โ€‹โ€‹work, paying more attention to Mathematica. As I understand it, for evaluation, you should apply a sequence of transformation rules to the input until more matching conversion rules are found and calls the result of the "exit".

But then the problem arises: what if more than one of those conversion rules matches this expression? I tried this first example:

A[x_, 3] := 0; A[x_, y_] := 1; A[a, b] => 1 A[k, 3] => 0 

I believe I can explain this by saying that 3 matches 3 โ€œbetterโ€ than y . Then my second experiment:

 B[x_, 3] := 0; B[4, y_] := 1; B[4, 3] => 0 

Why is this? I expected to see some kind of error.

+4
source share
1 answer

Precedent is simply the order in which the functions are defined.

 ClearAll[B] B[x_, 3] := 0; B[4, y_] := 1; B[4, 3] (* 0 *) ClearAll[B] B[4, y_] := 1; B[x_, 3] := 0; B[4, 3] (* 1 *) 

Beware, everything gets confused if you redefine functions.

 ClearAll[B] B[x_, 3] := 0; B[4, y_] := 1; B[4, y_] := 2; B[x_, 3] := 3; B[4, 3] (* 3 *) 

Please note that the determinants are correctly changed, but the order is in accordance with the original sequence. (hence the liberal use of ClearAll when dealing with such things)

To see the order of use:

 ??B 
+4
source

Source: https://habr.com/ru/post/1487077/


All Articles