Introduction to Programming in ATS: | ||
---|---|---|
<<< Previous | Introduction to Dependent Types | Next >>> |
Typechecking in ATS involves generating and solving constraints. As an example, the code below gives an implementation of the factorial function:
In this implementation, the function fact is assigned the following type: which means that fact returns a natural number r when applied to a natural number n. When the code is typechecked, the following constraints need to be solved:For each natural number n, n > 0 implies n - 1 >= 0
For each natural number n and each natural number r1, n > 0 implies n * r1 >= 0
For each natural number n, 1 >= 0 holds.
As a more interesting example, the following code implements MacCarthy's famous 91-function:
fun f91 {i:int} (x: int i) : [j:int | (i < 101 && j==91) || (i >= 101 && j==i-10)] int (j) = if x >= 101 then x-10 else f91 (f91 (x+11)) // end of [f91] |
Currently, the constraint-solver implemented for ATS/Anairiats makes use of machine-level arithmetic (in contrast to the standard arithmetic of infinite precision). This is done primarily for the sake of efficiency. In the presence of machine-level arithmetic overflow during constraint-solving, results returned by the constraint-solver are likely to be incorrect. While such cases can be readily constructed, their appearances in practice seem rare.
<<< Previous | Home | Next >>> |
Introduction to Dependent Types | Up | Example: String Processing |