Example: Consider the simple conditional statement written in the concrete syntax of LANG1:
"If (a<=b) then c:=(c+(-1)) else c:=(c+1) fi"
We can use doparse to transform this into the corresponding abstract syntax expression in the algebra T(LANG1Syn)
if(leq(id2ae(a( )),id2ae(b( ))), assign(c( ), add(id2ae(c( )),neg(1( )))), assign(c( ), add(id2ae(c( )),1( ))))
and then apply the homomorphism aitch("alg_LANG1StC", _ ) to get the corresponding StackM program:
["get(a)", "get(b)", "leq", "branch(11)", "get(c)", 1, "add", "put(c)", true, "branch(16)", "get(c)", 1, "neg", "add", "put(c)"]
which, looks as follows when written as a program listing:
This computation, for trial inputs "a"=10 and "b"=5, (so,
by default, "c"=0) can be done in two steps by performing the following
expression on the UA-Calculator
setSTM(aitch("alg_LANG1StC", doparse( "If (a<=b) then c:=(c+(-1)) else c:=(c+1) fi")), [["a", 10], ["b", 5]])
and then performing runSTM(10). Try
it:
Example: Consider the simple program, for the factorial function, written in the concrete syntax of LANG1:
"b:=1; While (1<=a) do b:=(a*b); a:=(a+(-1)) od"
We can use doparse to transform this into the corresponding abstract syntax expression in the algebra T(LANG1Syn)
compos(assign(b( ), 1( )), wdo(leq(1( ),id2ae(a( ))), compos(assign(b( ), mult(id2ae(a( )),id2ae(b( )))), assign(a( ), add(id2ae(a( )),neg(1( )))))))
and then apply the homomorphism aitch("alg_LANG1StC", _ ) to get the corresponding StackM program:
[1, "put(b)", 1, "get(a)", "leq", "branch(9)", true, "branch(20)", "get(a)", "get(b)", "mult", "put(b)", "get(a)", 1, "neg", "add", "put(a)", true, "branch(3)"]
which, looks as follows when written as a program listing:
end