Re: some questions relating pilog

2022-02-15 Thread Alexander Burger
On Tue, Feb 15, 2022 at 06:34:58PM +0100, pd wrote:
> > It tries the first one but does not succeed. The second one matches, so the
> > others are not tried because of the cut.
> 
> This is interesting, in my understanding when matching the first rule it
> should try to unify @C to the result of  @A + @B but since @B is @X  you
> have @B unified to a variable or symbol

This is right.

> and thus @B is not instantiated, it
> has no value and so you cannot do the + operation,

On the Pilog level, @B is indeed unified with @X. But in the Lisp call @B
has the value NIL.


> in my understanding you
> should have got an exception or an error

It is an important feature of PicoLisp that arithmetic functions treat NIL as
"not a number" and not as an error:

   : (+ 3 'a 4)
   !? (+ 3 'a 4)
   a -- Number expected
   ?

   : (+ 3 NIL 4)
   -> NIL


> I suppose what happens is first rule don't give an error trying to add @A
> and @B, it simply fails and returns with @C not unified (not binded) and
> thus the cut is never executed so it tries next rule, the second one, now
> it succeeds and executes the cut stopping further matching.

Exactly.

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: some questions relating pilog

2022-02-15 Thread pd
On Sat, Feb 12, 2022 at 8:37 AM Alexander Burger 
wrote:

>
> > >(be + (@A @B @C)
> > >   (^ @C (+ @A @B))
> > >   T )
> > >
> > >(be + (@A @B @C)
> > >   (^ @B (- @C @A))
> > >   T )
> > I suppose pilog search for rules in order, so the goal  (? (+ 3 @X 7))
> > always matches first three rules with the cut,
>
> It tries the first one but does not succeed. The second one matches, so the
> others are not tried because of the cut.
>

This is interesting, in my understanding when matching the first rule it
should try to unify @C to the result of  @A + @B but since @B is @X  you
have @B unified to a variable or symbol and thus @B is not instantiated, it
has no value and so you cannot do the + operation, in my understanding you
should have got an exception or an error, in fact this is what you get in
prolog:

 ?- +(3,X,7).
   Call: (8) +(3, _7186, 7) ? creep
   Call: (9) 3 is 7-_7186 ? creep
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:[9] 3 is 7-_7806
ERROR:[8] +(3,_7832,7) at /prolog_.pl:30
ERROR:[7] 


I suppose what happens is first rule don't give an error trying to add @A
and @B, it simply fails and returns with @C not unified (not binded) and
thus the cut is never executed so it tries next rule, the second one, now
it succeeds and executes the cut stopping further matching.

In this case my question is, what value is binded to @B when passed to lisp
function + ?   is it nil maybe?   or function ^ detects an unbinded pilog
variable and just returns with false?


> but shouldn't the last goal
> > match also one of the first three line thus avoiding backtracking?
>
> A query like (+ @X @Y 7) will not be satisfied by the first four rules, so

finally the fifth fires (and finally terminates recursion with the fourth).
>

so this is the same case as before, this works because (^ @X (op @A @B))
fails when @A or @B are not binded, being possible to explorer other rule
matching

regards