Re: [eclipse-clp-users] Accessing Eclipse predicates

From: <mskala_at_...206...>
Date: Fri, 16 Aug 2013 06:47:36 -0500 (CDT)
On Thu, 15 Aug 2013, Volkan Unsal wrote:
> I mean the string(A) or integer(A). They return a yes or no on the
> commandline, but I am wondering if they can be used within the program as
> well.

To amplify Sergii's examples:  a predicate is not a function that returns
a yes or a no.  It looks like by saying "Z is string(A)" you're attempting
to treat it as one and put the "return value" into Z.  Doing that won't
work.  You may have been confused by experience with other programming
languages in which statements are always expressions, every expression has
a return value, and the interactive interpreter prints the return value of
whatever you enter.

Prolog execution is not expression evaluation, in general.  It is more
like theorem proving, and it succeeds or fails instead of returning a
value.  (Some people do think of that as "returning" success or failure,
but this is why you shouldn't.)  The statement string("foo") succeeds
because it is a true statement.  The statement string(2) fails.  The
command line interface says "yes" or "no" for those cases respectively,
but "yes" and "no" are not return values of some type that exist in the
Prolog program, and you can't assign the "return value" to a variable;
what exists in the Prolog program is the control flow of the predicate
succeeding or failing.

It wouldn't be wholly inaccurate to say that any query that would make the
command line say "no", throws an exception.  However, I don't mean a
Prolog exception (those exist), I only mean that failure causes control
flow to back out to a higher level, something like how exceptions work in
other languages.

Saying "yes" is not the only outcome possible when execution succeeds.
Depending on whether the query left choice points, the command line may
also prompt you for whether you want to see other solutions, which would
be a meaningless question in the ordinary kind of expression-evaluating
language.

If you really want to have a variable Z that will be (unified with the
atom) 'yes' if string(A) succeeds and 'no' otherwise, you can use this,
both directly and within a loop:

  (string(A)->Z=yes;Z=no)

Sergii's example code shows basically the same thing inside a loop,
without using the symbolic values yes and no.

-- 
Matthew Skala
mskala_at_...206...                 People before principles.
http://ansuz.sooke.bc.ca/
Received on Fri Aug 16 2013 - 11:47:44 CEST

This archive was generated by hypermail 2.3.0 : Tue Apr 16 2024 - 09:13:20 CEST