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:
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: 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):
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_" //
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" //
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] //
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: