Needs["GraphUtilities`"]

Substitution in algebra

This Mathematica notebook is licensed under a MouseOver 
It creates the demonstrations used in the post

I hope anyone interested will feel free to improve this work and to use it in their own publications and coursework.  

This notebook contains the coding needed to produce the diagrams in the post Presenting binops as trees.  Warning : the names of many of the examples are inconsistent with each other.

Basic definitions

makelabel[labs_,n_]:=Inset/@#/.labs&[n]

vertexstyle[n_]:=Framed[Style[n,FontFamily->"Arial",FontSize->12],Background->White,FrameMargins->{{3,3},{3,3}}]

vertexstyle[42]

edgelabelstyle[n_]:=Style[Framed[n,Background->White,FrameMargins->{{0,0},{0,0}},FrameStyle->White],Plain,FontFamily->"Arial"]

edgelabelstyle[42]

vrf1[labs_]:=
VertexRenderingFunction->({White,EdgeForm[Black],Black,Text[vertexstyle[makelabel[labs, #2]],#1]}&)

erf1=EdgeRenderingFunction->({Darker[Red],{Line[#1]},Text[If[#3===None,Null,edgelabelstyle[#3]],Total[#1]/2.]}&)

stree[skel_,labs_,vfr_,erf_,vcr_,imagesize_,asprat_]:=GraphPlot[skel,vfr[labs], erf,VertexCoordinateRules->vcr,EdgeLabeling->True,ImageSize->imagesize,AspectRatio ->asprat]

emptybox="  "

binary operation

binoplabs={1->"Δ",2->"a",3->"b"}

binopskel={1->2,1->3}

binopvcr={1->{1,1},2->{0,0},3->{2,0}}

binop=stree[binopskel,binoplabs,vrf1,erf1,binopvcr,60,1.2]

stree[binopskel,{1->"Δ",2->"a",3->"a"},vrf1,erf1,binopvcr,60,1.2]

binopnovar=stree[binopskel,{1->"Δ",2->emptybox,3->emptybox},vrf1,erf1,binopvcr,60,1.3]

minusnovar=stree[binopskel,{1->"minus",2->emptybox,3->emptybox},vrf1,erf1,binopvcr,60,1.3]

binoponevar=stree[{1->2,1->3,2->4,3->4},{1->"Δ",2->"id",3->"id",4->emptybox},vrf1,VertexCoordinateRules->{1->{1,2},2->{0,1},3->{2,1},4->{1,0}},erf1,70,1.7]

binoponevar2=stree[{1->2,1->3,2->4,3->4},{1->"Δ",2->"id",3->"id",4->"3"},vrf1,VertexCoordinateRules->{1->{1,2},2->{0,1},3->{2,1},4->{1,0}},erf1,70,1.7]

binoponevar3=stree[binopskel,{1->"Δ",2->"3",3->"3"},vrf1,erf1,binopvcr,60,1.3]

examplebinop2=stree[{1->2,1->3},{1->"+",2->"3",3->"5"},vrf1,erf1,binopvcr,70,1.2]

examplebinop2b=stree[{1->2,1->3},{1->"+",2->"5",3->"3"},vrf1,erf1,binopvcr,70,1.2]

examplebinop2evaltrace1=stree[{1->2,1->3},{1->"8",2->"3",3->"5"},vrf1,erf1,binopvcr,70,1.2]

examplebinop2eval=Show[Graphics[{Inset[examplebinop2,{0,0}],Arrowheads[.1],Arrow[{{.3,.5},{.7,.5}}],Inset[Style["eval",13,FontFamily->"Arial"],{0.5,1}],Inset[vertexstyle[8],{1,.5}]}],ImageSize->200.00,AspectRatio->.15]

examplebinop2evaltrace=Show[Graphics[{Inset[examplebinop2,{0,0}],Arrowheads[.1],Arrow[{{.3,.5},{.7,.5}}],Inset[Style["eval",13,FontFamily->"Arial"],{0.5,1}],Inset[examplebinop2evaltrace1,{1,0}]}],ImageSize->200.00,AspectRatio->.15]

examplebinop3=stree[{1->2,1->3},{1->"×",2->"3",3->"5"},vrf1,erf1,binopvcr,70,1.2]

examplebinop3evaltrace=stree[{1->2,1->3},{1->"15",2->"3",3->"5"},vrf1,erf1,binopvcr,70,1.2]

examplebinop3eval=Show[Graphics[{Inset[examplebinop3,{0,0}],Arrowheads[.1],Arrow[{{.3,.5},{.7,.5}}],Inset[Style["eval",13,FontFamily->"Arial"],{0.5,1}],Inset[vertexstyle[15],{1,.5}]}],ImageSize->200.00,AspectRatio->.15]

examplebinop4=stree[{1->2,1->3},{1->"minus",2->"3",3->"5"},vrf1,erf1,binopvcr,70,1.2]

examplebinop4evaltrace=stree[{1->2,1->3},{1->"-2",2->"3",3->"5"},vrf1,erf1,binopvcr,70,1.2]

examplebinop4eval=Show[Graphics[{Inset[examplebinop4,{0,0}],Arrowheads[.1],Arrow[{{.3,.5},{.7,.5}}],Inset[Style["eval",13,FontFamily->"Arial"],{0.5,1}],Inset[vertexstyle[-2],{1,.5}]}],ImageSize->200.00,AspectRatio->.15]

examplelabeledbinop=stree[{{1->2,"L"},{1->3,"R"}},{1->"minus",2->"3",3->"5"},vrf1,erf1,binopvcr,90,1.2]

examplebinopeval4b=stree[{1->2,1->3},{1->"minus"<>"\n"<>"-2",2->"3",3->"5"},vrf1,erf1,binopvcr,80,1.2]

Associative law building blocks

lassoc=GraphPlot[{1->2,2->3,2->4,1->5},{1->"Δ",2->"Δ",3->"a",4->"b",5->"c"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{-.67,-1},4->{.67,-1},5->{2,-1}},
ImageSize->100,AspectRatio->1]

raggedlassoc=GraphPlot[{1->2,1->3,2->4,2->5},{1->"Δ",2->"Δ",3->"c",4->"a",5->"b"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,0},4->{-.67,-1},5->{.67,-1}},
ImageSize->100,AspectRatio->1]

rassoc=GraphPlot[{1->2,1->3,2->4,2->5},{1->"Δ",2->"Δ",3->"a",4->"b",5->"c"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{1.5,0},3->{-.67,-1},4->{.67,-1},5->{2,-1}},
ImageSize->100,AspectRatio->1]

minusex=GraphPlot[{1->2,1->3,2->4,2->5},{1->"minus",2->"minus",3->"c",4->"a",5->"b"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,0},4->{-.67,-1},5->{.67,-1}},
ImageSize->120,AspectRatio->1]

Evaluation

numinus1=GraphPlot[{1->2,1->3,2->4,2->5},{1->"minus",2->"minus",3->emptybox,4->emptybox,5->emptybox}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,0},4->{-.67,-1},5->{.67,-1}},
ImageSize->120,AspectRatio->1.2]

numinus1r=GraphPlot[{1->2,1->3,3->4,3->5},{1->"minus",2->emptybox,3->"minus",4->emptybox,5->emptybox}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,0},4->{1.33,-1},5->{2.67,-1}},
ImageSize->120,AspectRatio->1.2]

numinus2e0=GraphPlot[{1->2,1->3,2->4,2->5,4->6,5->7,3->8 },{1->"minus",2->"minus",3->"id",4->"id",5->"id",6->"6",7->"4",8->"1"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,0},4->{-.67,-1},5->{.67,-1},6->{-.67,-2},7->{.67,-2},8->{2,-2}},
ImageSize->140,AspectRatio->1.4]

numinus2e1=GraphPlot[{1->2,1->3,2->4,2->5 },{1->"minus",2->"minus",3->"1",4->"6",5->"4"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,0},4->{-.67,-1},5->{.67,-1}},
ImageSize->120,AspectRatio->1.2]

numinus2e2=GraphPlot[{1->2,1->3 },{1->"minus",2->"2",3->"1"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,0}},
ImageSize->70,AspectRatio->1]

numinus2e3=Show[Graphics[Inset[vertexstyle[1],{0,0}]],AspectRatio->.1]

numinus3e0=GraphPlot[{1->2,1->3,2->4,2->5,3->7,4->6,5->7 },{1->"minus",2->"minus",3->"id",4->"id",5->"id",6->"3",7->"5"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,-1},4->{-.67,-1},5->{.67,-1},6->{-.67,-2},7->{1.2,-2}},
ImageSize->140,AspectRatio->1.4]

numinus3ebar=GraphPlot[{1->2,1->3,2->4,2->5,3->7,4->6,5->7 },{1->"minus",2->"minus",3->"id",4->"id",5->"id",6->emptybox,7->emptybox}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,-1},4->{-.67,-1},5->{.67,-1},6->{-.67,-2},7->{1.2,-2}},
ImageSize->140,AspectRatio->1.4]

numinus3e1=GraphPlot[{1->2,1->3,2->4,2->5 },{1->"minus",2->"minus",3->"5",4->"3",5->"5"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{2,-1},4->{-.67,-1},5->{.67,-1}},
ImageSize->120,AspectRatio->1.2]

numinus3e2=GraphPlot[{1->2,1->3},{1->"minus",2->"-2",3->"5",4->"3",5->"5",6->"3",7->"5"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,-1},3->{2,-1},4->{-.67,-1},5->{.67,-1},6->{-.67,-2},7->{1.2,-2}},
ImageSize->70,AspectRatio->1.2]

numinus3e3=vertexstyle[-7]

examplebinop3eval=Show[Graphics[{Inset[examplebinop3,{0,0}],Arrowheads[.1],Arrow[{{.3,.5},{.7,.5}}],Inset[Style["eval",13,FontFamily->"Arial"],{0.5,1}],Inset[vertexstyle[15],{1,.5}]}],ImageSize->200.00,AspectRatio->.15]

Manipulate[Show[Graphics[{Arrowheads[.02],Inset[numinus3e0,{0,0}],Arrow[{{.45,0},{.75,0}}],
Inset[Style["eval",13,FontFamily->"Arial"],{0.6,evalheight}],
Inset[numinus3e1,{2.1 inc,0}],
Arrow[{{.4+2inc,0},{.7+2inc,0}}],
Inset[Style["eval",13,FontFamily->"Arial"],{.55+2inc,evalheight}],
Inset[numinus3e2,{3.7 inc,0}],
Arrow[{{.4+3.5 inc,0},{.7+3.5 inc,0}}],Inset[Style["eval",13,FontFamily->"Arial"],{.55+3.5 inc,evalheight}],
Inset[numinus3e3,{5.1inc,0}]}],
ImageSize->500.00,AspectRatio->Automatic],{inc,.55,2.},{evalheight,0.1,1}]

Manipulate[Show[Graphics[{Arrowheads[.02],
Inset[numinus2e1,{2.1 inc,0}],
Arrow[{{.4+2inc,0},{.7+2inc,0}}],
Inset[Style["eval",13,FontFamily->"Arial"],{.55+2inc,evalheight}],
Inset[numinus2e2,{3.7 inc,0}],
Arrow[{{.4+3.5 inc,0},{.7+3.5 inc,0}}],Inset[Style["eval",13,FontFamily->"Arial"],{.55+3.5 inc,evalheight}],
Inset[numinus2e3,{5.1inc,0}]}],
ImageSize->500.00,AspectRatio->Automatic],{inc,.55,2.},{evalheight,0.05,1}]

More associative

llassoc=GraphPlot[{1->2,1->7,2->3,2->6,3->4,3->5},{1->"minus",2->"minus",3->"minus",4->"a",5->"b",6->"c",7->"d"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{-.79,-1},4->{-1.3,-2},5->{-.3,-2},6->{.67,-2},7->{1.67,-2}},
ImageSize->160,AspectRatio->1.2]

lassoc=GraphPlot[{1->2,2->3,2->4,1->5},{1->"+",2->"+",3->"3",4->"4",5->"2"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{-.67,-1},4->{.67,-1},5->{2,-1}},
ImageSize->100,AspectRatio->1]

Substitution

binop=stree[binopskel,binoplabs,vrf1,binopvcr,60,1.2]

lassoc=GraphPlot[{1->2,2->3,2->4,1->5},{1->"Δ",2->"Δ",3->"a",4->"b",5->"c"}//vrf1,VertexCoordinateRules->{1->{1,1},2->{0,0},3->{-.67,-1},4->{.67,-1},5->{2,-1}},
ImageSize->100,AspectRatio->1]