Pure, Declarative, and Constructive Arithmetic Relations (Declarative Pearl) ⋆ Oleg Kiselyov 1 , William E. Byrd 2 , Daniel P. Friedman 2 , and Chung-chieh Shan 3 1 FNMOC oleg@pobox.com 2 Indiana University {webyrd,dfried}@cs.indiana.edu 3 Rutgers University ccshan@cs.rutgers.edu Abstract. We present decidable logic programs for addition, multipli- cation, division with remainder, exponentiation, and logarithm with re- mainder over the unbounded domain of natural numbers. Our predicates represent relations without mode restrictions or annotations. They are fully decidable under the common, DFS-like, SLD resolution strategy of Prolog or under an interleaving refinement of DFS. We prove that the evaluation of our arithmetic goals always terminates, given argu- ments that share no logic variables. Further, the (possibly infinite) set of solutions for a goal denotes exactly the corresponding mathematical re- lation. (For SLD without interleaving, and for some infinite solution sets, only half of the relation’s domain may be covered.) We define predicates to handle unary (for illustration) and binary representations of natural numbers, and prove termination and completeness of these predicates. Our predicates are written in pure Prolog, without cut (!), var/1, or other non-logical operators. The purity and minimalism of our approach allows us to declare arithmetic in other logic systems, such as Haskell type classes. 1 Introduction Logic programming is said to be programming with relations, but arithmetic is often dealt with in a non-relational, restricted way. For example, Prolog’s built- in is/2 predicate for evaluating arithmetic expressions does not allow free (un- bound) logic variables in the expressions. Whereas the goal Z is 6*7 succeeds, binding 42 to Z, the related goal 42 is X*Y is considered erroneous because its multiplicands ‘are not sufficiently instantiated.’ Multiplication is not treated as a ternary relation between multiplicands and the product because of mode restrictions on the first two arguments. Constraint logic programming (CLP) overcomes this drawback to some extent [1]; for example, disjunctive Datalog [2] treats arithmetic relationally. Indeed, relational handling of arithmetic was one of the motivations for CLP. Unfortunately, this flexibility has a price: CLP restricts ⋆ We thank Ronald Garcia and the anonymous reviewers for many helpful comments.