I have already hinted a few times in this forum that I would like to
support certain forms of “meta-programming” for generating ATS source
code.
The first form I intend to support is what I call static meta-programming.
Essentially, the programmer is to be given access to some kind of internal
representation of abstract syntax trees (of ATS programs). This is more or
less like JS programmers having access to DOM trees. Then various sorts
of meta-programming tools can be written to generate ATS source code based
internal abstract syntax trees. For instance, a print function for a
declared datatype
can be automatically generated by applying such a tool.
Say we have the following datatype declaration:
datatype
tree0(a:t@ype) =
| tree0_nil of ()
| tree0_cons of (tree0(a), a, tree0(a))
Then we can expect to generate the following code automatically:
extern
fun
{a:t@ype}
fprint_tree0:fprint_type(tree0(a))
and
fprint_tree0_nil:fprint_type(tree0(a))
and
fprint_tree0_cons:fprint_type(tree0(a))
implement(a)
fprint_val<tree0(a)> = fprint_tree0
implement
{a}(tmp)
fprint_tree0(out, x) =
case+ x of
| tree0_nil _ => fprint_tree0_nil(out, x)
| tree0_cons _ => fprint_tree0_cons(out, x)
implement
{a}(tmp)
fprint_tree0_nil(out, x) =
{
//
val-tree0_nil() = x
val () = fprint(out, “tree0_nil”)
val () = fprint(out, “(”)
val () = fprint(out, “)”)
//
} (* end of [fprint_tree0_nil] *)
implement
{a}(tmp)
fprint_tree0_cons
(out, x0) =
{
//
val-tree0_cons(x1, x2, x3) = x0
//
val () = fprint(out, “tree0_cons”)
val () = fprint(out, “(”)
val () = fprint_tree0 (out, x1)
val () = fprint(out, ", ")
val () = fprint_val (out, x2)
val () = fprint(out, ", ")
val () = fprint_tree0 (out, x3)
val () = fprint(out, “)”)
//
} (* end of [fprint_tree0_cons] *)
In most cases, we should try to generate function templates (instead of
functions). Doing so
can allow us to maximally take advantage of the support for extreme
late-binding of functions
in ATS. In this way, I would certainly hope to get something that is a
great deal more flexible
than type classes.
If you have ideas and suggestions, you are mostly welcome discuss them here.
Cheers!