[comp.lang.prolog] variadic predicates

cdsm@ivax.doc.ic.ac.uk (Chris Moss) (10/27/87)

>Date: 23 Oct 87 16:05:00 EDT
>From: Andre (A.N.) Vellino <VELLINO%BNR.BITNET@forsythe.stanford.edu>
>Subject: variadic predicates
>
>I was wondering if anyone on the Digest could think of a good reason for
>objecting to the idea of a Prolog that has
>
>     a) functors with variable arity (the only one that springs
>        to mind is that it is simple to emulate variadic predicates
>        with lists);
>     b) variable functors (so that, for example "?- Functor(Arg)."
>        is a valid query).
>
>-- Andre Vellino 

  There are several Prologs on the market which do this. e.g. LMProlog
(on Lisp machines) and microProlog (which also uses a Lisp-like 
syntax). It's pretty natural with Lisp syntax. See Mats Carlsson's thesis
for some pretty elegant examples (e.g. variadic append).
  But there are significant problems. The biggy is compilation: people
have invented all sorts of clever compilation techniques which work much
better on fixed arity predicates, so that most systems treat foo/2 and
foo/3 (i.e. foo(A,B) and foo(A,B,C)) as entirely different predicates.
  The problem with variable function symbols is: how do you constrain
unification so that only identifiers match? One could have an arbitrary
term, so making expressions that print out but can't be read back in.
  Can you mix the two notations? i.e. convert a program with variadic
predicates to non-variadic? There are problems here. Consider:
   foo(A,B) :- ... 
   foo(A|B) :- ... !, ...        (this represents a variadic definition)
   foo(A,B,C) :- ... 

The second clause is common to both foo/2 and foo/3 and one would need
to copy it both ways when converting, so there's no 1-1 correspondence
between variadic and non-variadic programs (especially when operational
things like cut are considered).

  But ultimately the question is: why bother? In Prolog, unlike most
languages, one can write a list of arguments explicitly anywhere in the
program. So if one wants printf(format, arg1, arg2, ...) one simply writes
printf(format, [arg1, arg2, ...]). Ok it's 2 extra characters to type.
But conceptually it's much simpler, and the program is probably easier to
write too. Efficiency hardly suffers and you don't clobber any programs
that don't want to use this feature.
  For calling a  program with unknown name one can simply say:
     ?- ... apply(P, [arg1,arg2,arg3])
which again is more to type but simpler conceptually, and, if the apply
predicate is built in to your system, no less efficient.

Chris Moss (cdsm@doc.ic.ac.uk)