Chapter 9. Through ATS to PHP

Through-one-to-all is a catchy phrase often used to describe ATS. When given a programming language X, a programmer often assumes automatically that X is just meant for constructing programs manually. Sometimes, a (much) more productive approach to writing code in X is to write some code in another programming language for generating the needed code in X.

PHP is a programming language of great popularity for primarily supporting the server-side web development. In this chapter, I plan to demonstrate a style of co-programming with ATS and PHP. In practice (of this style of co-programming), ATS is mainly intended for high-level programming (that, for instance, makes extensive use of combinators) and PHP for relatively low-level programming (that, for instance, handles direct interactions with the web server running PHP code).

It is straightforward to use ATS to generate PHP code. Suppose that the ATS code stored in a file of the name ats2php.dats needs to be compiled into PHP. This can be done as follows in two steps:


patsopt -o ats2php_dats.c -d ats2php.dats
atscc2php -o ats2php_dats.php -i ats2php_dats.c

The code contained in the file ats2php.dats is first compiled into C (via a call to patsopt) and then transpiled from C into PHP (via a call to atscc2php). With piping, these two steps can be accomplished by issuing the following command-line:


patsopt -d ats2php.dats | atscc2php -o ats2php_dats.php -i -

Note that the PHP code generated from the ATS source in ats2php.dats is stored in ats2php_dats.php.

In order for the PHP code generated from ATS source to run, the PHP file libatscc2php_all.php needs to be loaded first. For instance, loading can be done by executing the following line (of PHP code):


include_once "$LIBATSCC2PHP_DIR/libatscc2php_all.php"

where $LIBATSCC2PHP_DIR refers to a directory containing a copy of the file libatscc2php_all.php.

As a concrete example, please see the code in Factorial.dats. At the beginning of the file, the following code is present:

// #define ATS_DYNLOADFLAG 0 // #define ATS_EXTERN_PREFIX "Factorial_" #define ATS_STATIC_PREFIX "_Factorial_" //

which indicates to patsopt that no dynload-function (for the purpose of code initialization) is to be generated when the ATS source is compiled into C. The string value of ATS_EXTERN_PREFIX is used as a prefix to form names for functions in C that are generated from compiling functions in ATS given the special external name mac#%. And the string value of ATS_STATIC_PREFIX is used as a prefix to form names for static functions in C that are generated from compiling ATS source.

For compiling ATS to PHP, we need to have access to the LIBATSCC2PHP library, which can be done by adding the following lines:

// #define LIBATSCC2PHP_targetloc "$PATSHOME\ /contrib/libatscc2php/ATS2-0.3.2" // #include "{$LIBATSCC2PHP}/staloadall.hats" //

The version we use here is ATS2-0.3.2, which is the latest stable release of LIBATSCC2PHP.

In the following code, a function of the name Factorial is declared and then implemented in ATS:

// extern fun Factorial(n: int): int = "mac#%" // (* ****** ****** *) // implement Factorial(n) = (fix loop(i: int, res: int) =<cloref1> if i < n then loop(i+1, res*(i+1)) else res )(0, 1) // end of [Factorial] //

Note that the use of the string "mac#%" simply indicates to patsopt that the function Factorial is to be given the same name prefixed by the string value of ATS_STATIC_PREFIX when compiled into C. In contrast, the name automatically chosen (by patsopt) for Factorial contains a long prefix (for carrying some namespace information) if the string "mac#%" is not present. If a different name is needed, please replace the symbol % in mac#% with the name.

When doing co-programming with ATS and PHP, one often wants to include some PHP code in ATS source so that the included code can be directly pasted into the PHP code generated from the ATS source. The code written between %{^ and %} is considered external, where the symbol ^ means that the code is to be pasted near the beginning of the generated code. If the symbol ^ is omitted or replaced with $, the code is to be pasted near the bottom of the generated code.

Please find on-line the entirety of the code used in this chapter. The mentioned URL link(s) can be found as follows: