Closed Multisubstitutions.

A``pattern'' is string possibly including the special symbol *, which stay for any list of 0 or more characters. This map checks if a string y, or a pattern y, occurs in a string x. We ignore the difference between upper and lower case.

In[115]:=

StringMemberQ[x_, y_] := StringMatchQ[x, StringJoin["*", y, "*"], IgnoreCase->True] ;

We define two maps computing the beginning and the end of the first occurrence of y in x, if any exists.

In[116]:=

(* StringPosition returns {{a, b}, ...}, where a, b are the beginning and the end of the first ... gnoreCase  True][[1, 2]] ; FirstPosition[x_, y_] := StringPosition[x, y][[1, 1]] ;

Out[116]=

Null^2

We define two maps removing all characters until the first occurrence of y (included), or from the first occurence of y (included).

In[117]:=

EraseUntil[x_, y_] := StringDrop[x, EndFirstPosition[x, y]] ;

In[118]:=

EraseFrom[x_, y_] := StringTake[x, FirstPosition[x, y] - 1] ;

Moving a term from the language of arithmetic to the language of Mathematica only require to switch round and square brackets.

In[119]:=

SquareBrackets[S_] := StringReplace[S, {"(" "[", ")" "]"}] ;

In[120]:=

RoundBrackets[S_] := StringReplace[S, {"]" ")", "[" "("}] ;

In[121]:=

RemoveBlanks[S_] := StringReplace[S, " " ""] ;

We can split a list of terms "t1, t2, ..., tn" into "t1" and "t2, ..., tn". We first move the term list to the Mathematica language, then we compute the length n of the first correct term. "t1" are first n characters. "t2, ..., tn" are the string with the first n character removed, and one more character removed (the comma) if any is left.

In[122]:=

HeadExprLength[S_] := SyntaxLength[SquareBrackets[S]] ;

In[123]:=

RemoveOne[S_] := If[S == "", "", StringDrop[S, 1]]

In[124]:=

HeadExpr[S_] := StringTake[S, HeadExprLength[S]] ;

In[125]:=

TailExpr[S_] := RemoveOne[StringDrop[S, HeadExprLength[S]]] ;

Some examples.

In[126]:=

HeadExpr["x,y"] HeadExpr["f(a,b), g(f(a,b))"] TailExpr["x,y"] TailExpr["f(a,b), g(f(a,b))"]

Out[126]=

x

Out[127]=

f(a,b)

Out[128]=

y

Out[129]=

 g(f(a,b))

Selecting the head and the argument list of a predicate.

In[130]:=

HeadPredicate[S_] := If[StringMemberQ[S, "("], EraseFrom[S, "("], S] ; Arg ... If[StringMemberQ[S, "("], StringDrop[EraseUntil[S, "("], -1], ""] ;

Some examples.

In[132]:=

HeadPredicate["P(x,y)"]

Out[132]=

P

In[133]:=

ArgPredicate["P(x,y)"]

Out[133]=

x,y

The arguments of a multisubstitution are a formula, a list of variables, and a list of closed terms for these variables.

In[134]:=

MultiSub[A_, Var_, L_] := If[Or[Var"", L""], (* No ... n the rest . *)MultiSub[Sub[A, HeadExpr[Var], HeadExpr[L]], TailExpr[Var], TailExpr[L]]] ;

In[135]:=

MultiSub["x^2 + y^2", "x,y", "f(a,b), g(f(a,b))"]

Out[135]=

f(a,b)^2 +  g(f(a,b))^2


Created by Mathematica  (October 17, 2006)