An Algebraic Compiler for LANG1 on StackM

Algebra("LANG1syn", [],
[ [LANG1Var, poop],
[numQ, numcomp],
["tr", [true]],
["fa", [false]],
["not", "notcomp(arg[1])"],
["and", "andcomp(arg[1], arg[2])"],
["eq", "eqcomp(arg[1], arg[2])"],
["leq", "leqcomp(arg[1], arg[2])"],
["id2ae", "id2aecomp(arg[1])"],
["neg", "negcomp(arg[1])"],
["succ", "succcomp(arg[1])"],
["pred", "predcomp(arg[1])"],
["add", "addcomp(arg[1], arg[2])"],
["mult", "multcomp(arg[1], arg[2])"],
["assign", "assigncomp(arg[1], arg[2])"],
["compos", "composcomp(arg[1], arg[2])"],
["if", "ifcomp(arg[1], arg[2], arg[3])"],
["wdo", "wdocomp(arg[1], arg[2])"] ])
 

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:

           1     get(a)
           2     get(b)
           3     leq
           4     branch(11)
           5     get(c)
           6     1
           7     add
           8     put(c)
           9     true
           10   branch(16)
           11   get(c)
           12   1
           13   neg
           14   add
           15   put(c)


    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:
     
     




    end

    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:
     

           1   1
           2    put(b)
           3   1
           4    get(a)
           5    leq
           6    branch(9)
           7    true
           8    branch(20)
           9    get(a)
           10  get(b)
           11  mult
           12  put(b)
           13  get(a)
           14  1
           15   neg
           16  add
           17  put(a)
           18  true
           19  branch(3)
    To execute this program on StackM  with "a"=10  takes 150 steps.
     
     





    end