Article View: comp.lang.prolog
Article #2848Re: variadic predicates
From: Chris Moss
Date: Tue, 27 Oct 1987 11:03
Date: Tue, 27 Oct 1987 11:03
50 lines
3169 bytes
3169 bytes
>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)
Technical Details
Message-ID:
<126@gould.doc.ic.ac.uk>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!uunet!mcvax!ukc!eagle!icdoc!ivax!cdsmReferences:
<8710250756.AA09651@ucbvax.Berkeley.EDU>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!mcvax!ukc!eagle!icdoc!ivax!cdsm From: cdsm@ivax.doc.ic.ac.uk (Chris Moss) Newsgroups: comp.lang.prolog Subject: Re: variadic predicates Message-ID: <126@gould.doc.ic.ac.uk> Date: Tue, 27-Oct-87 06:03:42 EST Article-I.D.: gould.126 Posted: Tue Oct 27 06:03:42 1987 Date-Received: Sat, 31-Oct-87 02:48:57 EST References: <8710250756.AA09651@ucbvax.Berkeley.EDU> Sender: news@doc.ic.ac.uk Reply-To: cdsm@doc.ic.ac.uk (Chris Moss) Organization: Dept. of Computing, Imperial College, London, UK. Lines: 50