ATC tutorial: ACL2 representation of C conditional statements.
Previous tutorial pages have shown how to represent C functions whose bodies consists of return statements and possibly local variable declarations and assignments (including assignments to the function parameters). This tutorial page explains how to represent C functions whose bodies are (possibly nested) conditional statements.
If the body of an ACL2 function is an if,
the body of the corresponding C function is an
However, note that if tests in ACL2 are (generalized) booleans,
i.e. they must return non-
For example, the ACL2 function
(defun |f| (|x| |y| |z|) (declare (xargs :guard (and (c::sintp |x|) (c::sintp |y|) (c::sintp |z|)))) (if (c::boolean-from-sint |x|) |y| |z|))
represents the C function
int f(int x, int y, int z) { if (x) { return y; } else { return z; } }
As another example, the ACL2 function
(defun |g| (|e|) (declare (xargs :guard (c::sintp |e|))) (if (c::boolean-from-sint (c::ge-sint-sint |e| (c::sint-dec-const 0))) (if (c::boolean-from-sint (c::lt-sint-sint |e| (c::sint-dec-const 1000))) (c::sint-dec-const 1) (c::sint-dec-const 2)) (c::sint-dec-const 3))) )
represents the C function
int g(int e) { if (e >= 0) { if (e < 1000) { return 1; } else { return 2; } } else { return 3; } }
The arguments of boolean-from-sint in if tests
may be the same ones used to describe the expressions
returned by
Previous: ACL2 representation of C assignments