[I've cross-posted to comp.lang.pop]
vanroy@prl.dec.com (Peter Van Roy) writes in comp.lang.prolog:
> Date: Tue, 14 Jun 1994 13:04:58 GMT
> Organization: DEC Paris Research Laboratory
>
> Jocelyn Paine writes:
>
> > So I think that we should realise that given Prolog as it is now, people
> > will need to code some algorithms imperatively. The only way to make
> > such programs portable is to design and standardise an imperative
> > companion language to Prolog.
>
> It seems to me that what is needed is a standard _interface_ to an external
> language. The interface has two sides: a Prolog side, and an imperative
> side (for whatever imperative language is used). If the imperative language
> is _changed_, then the Prolog side remains untouched. That is, I can make
> available a program containing a Prolog and a C component (say). Someone
> else could replace this C component by a Pascal component (say), and the
> Prolog code would remain unchanged.
>
> There are many languages/systems that make a distinction between
> the implementation and the interface (C++ is a recent example; the
> one I know best is Modula-2). It's a small step to let the
> implementation be in _any_ language.
>
> Most current imperative languages may be too weak to support this
> kind of thing in a clean way, since Prolog uses backtracking
> (continuation passing, multiple solutions) and has dynamic types
> and automatic memory management.
>
> A language like Scheme or Common Lisp, say, may be strong
> enough to provide representations for all of Prolog's abilities.
> Defaults handle the cases when we don't want to worry about types,
> continuations, and memory management, and the defaults can be
> overridden in a consistent way if we do want to access the full
> power of the Prolog system. For weak imperative languages,
> some of the defaults may not be overridable.
>
> Commercial Prolog systems already provide foreign language
> interfaces; are any of these good enough to serve as the basis
> for a standard? Any vendors out there have opinions on this?
This is not a direct answer, but it may be worth mentioning that for
about 14 years the Sussex University Poplog Prolog has had an
interface to Pop-11 (a Lisp-like language with a syntax a bit closer
to Pascal), which was originally designed by Chris Mellish and has
since been used by many people, including commercial customers of
Integral Solutions Ltd, the distributors of Poplog. (The current
implementation of the interface is mainly by John Gibson, though
the syntax was designed by Chris.)
Perhaps some Poplog user with more time than I can spare could post
a description of the interface (currently available only in the
Poplog Prolog online documentation).
It was designed for a language that shared the heap and most
data-types with Prolog, and whose symbols are available at run-time.
Examples of the formats available (from one of the HELP FILES) are
?- prolog_eval(<prologterm>).
e.g. prolog_eval(nl(quote(N)).
where "nl(5)" in Pop-11 means print 5 newlines, and N is a prolog
variable whose value is to be passed to Pop-11, and which is
dereferenced by "quote". Note that nl produces no result. (Pop-11
procedures, as in Forth, leave results on the pop-11 "user" stack.)
The following can be used to run a Pop-11 procedure that produces a
result:
?- prolog_eval(<prologterm>, <prologvar>).
e.g.
?- prolog_eval(sqrt(quote(X)), Y).
This will invoke the Pop-11 procedure "sqrt" to find the square root
of X (which should be a number, of any kind including small or big
integer, float, double float, ratio or complex number) and the
result will be unified with Y. The above is actually equivalent to
?- Y is sqrt(X).
There is a library infix operator "are" that generalises this to
allow Pop-11 procedures to return more than one result, in which
case the results are put into a list which is unified with the
result variable. E.g. the pop-11 integer divide operator "//"
produces remainder and quotient, and can be invoked (in Pop-11)
either as
//(10,3)
or
10 // 3
both of which produce the integers 1 and 3 as results. So
?- L are //(X,Y).
will create a list containing 1 and 3 and bind it to L.
It is also possible to store Prolog values in Pop-11 variables,
which can sometimes be far more efficient than using the database.
Also Pop-11 has generalised hash tables, arrays, etc. and these can
also be accessed via the above interface and used to supplement the
prolog database where efficiency is an issue.
Of course, using these facilities means that you are not treating
Prolog as a logic programming language, merely as a powerful high
level language!
Pop-11 does run-time type checking (like Lisp and Prolog) so error
handling in such cases is dependent on the Pop-11 error handler,
which is far from perfect. A good standard interface would have to
specify communication between error handlers.
Pop-11 (in Poplog) has access to the prolog implementation (in fact
Poplog prolog is actually implemented in Pop-11) including the
unifier, the continuation passing mechanism, backtracking
procedures, etc. So it is possible in Pop-11 to synthesize a new
prolog procedure/predicate, associate it with an appropriate name in
the Prolog symbol table, and thereafter call it explicitly via the
normal prolog syntax. Moreover, Pop-11 procedures can invoke Prolog
predicates too, so a Prolog predicate could invoke a Pop-11
procedure which invokes another Prolog procedure and then finally
returns to the original prolog. It's up to the implementor whether
to cope with fails or not. I have no idea whether it would be
possible in general for such "callbacks" to be part of some
standard for external interfaces.
I suspect it would all be too implementation dependent.
Perhaps the-one way interface could be standardised. However what
I've described so far provides an interface to another language
whose symbols are available at run time and which shares a heap with
Prolog.
(One reason Poplog prolog cannot run quite as fast as the fastest
stand-alone prologs is that it requires a more general garbage
collector: a failed Prolog procedure might have created a list and
left it in a Pop-11 variable or data-structure. But invoking
Pop-11 can sometimes more than compensate for the difference in
speed: e.g. using hash-tables (properties) instead of the prolog
database can produce orders of magnitude improvements in some
programs.)
Communication with a REALLY external language eg. C or Fortran can
be done via the Pop-11 external interface. But this requires a huge
amount of extra complexity, for handling mismatches between
name-spaces, mismatches between types, and mismatches between the
machine representation of the same thing (e.g. integers in Poplog
and integers in C), and even more complexity since callbacks were
added in order to allow interfacing to external X widget libraries
whose event-handlers might need to invoke Poplog procedures. (Pop-11
event-handlers could call Prolog).
The online documentation for the exernal interface in Pop-11 takes
several thousand lines of text. (Even more if you add all the extra
stuff about X event handling, data coercion, etc.)
I expect at least the same degree of complexity would be required in
a full Prolog interface to C.
Maybe some kind Poplog Prolog user would care to summarise the
current facilities better than I can do (I mostly use Pop-11, not
Prolog!)
This would not necessary be offered as some kind of prototype
standard, but as an illustration of the kind of complexity that
would need to be addressed in an external interface rich enough to
support at least what Poplog users can now do, including linking
in externally developed widget sets and handling callbacks.
It may be impossible to define a standard interface to external
object oriented language with multiple inheritance, multi-methods,
etc. They differ too much. Probably ad-hoc language-specific
interfaces will be needed (e.g. for C++, CLOS, Pop-11 Objectclass or
whatever.)
Apologies if all this is "old-hat" to prolog experts, or irrelevant
to the question.
Aaron
---
--
Aaron Sloman,
School of Computer Science, The University of Birmingham, B15 2TT, England
EMAIL A.Sloman@cs.bham.ac.uk OR A.Sloman@bham.ac.uk
Phone: +44-(0)21-414-4775 Fax: +44-(0)21-414-4281
|