Hi pd,

> > The environments are nested association lists, with numbers for the levels
> > and
> > then the symbols for the values at these levels.
>
> and levels are related to backtracking somehow?

Yes. When looking up values for symbols, the symbols at individual levels are
unified with constants or other symbols at these levels.


> >    (be + (@A @B @C)
> >       (^ @C (+ @A @B))
> >       T )
> >
> >    (be + (@A @B @C)
> >       (^ @B (- @C @A))
> >       T )
> >
> >    (be + (@A @B @C)
> >       (^ @A (- @C @B))
> >       T )
> >
> >    (be + (@A 0 @A))
> >
> >    (be + (@A @B @C)
> >       (^ @Z (dec @C))
> >       (+ @A @Y @Z)
> >       (^ @B (inc @Y)) )
> >     ...
> 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.


> 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).


> Also, when a goal return a non terminating list, what happend if we try the
> function solve with it? is there any way to get an exception or a stream?

'solve' is not very useful here. You could terminate with a 'throw', but then
you don't have the result. 'pilog' is more general:

   : (make
      (let N 5
         (catch NIL
            (pilog '((+ @X @Y 7))
               (and (lt0 (dec 'N)) (throw))
               (link (cons @X @Y)) ) ) ) )
   -> ((7 . 0) (6 . 1) (5 . 2) (4 . 3) (3 . 4))

but still clumsy.

Except for very simple cases I also don't use 'pilog' (e.g. in a database query
'collect' is better), but code it directly:

   : (for ((I . Q) (goal '((+ @X @Y 7))) (prove Q))
      (bind @ (println (cons @X @Y)))
      (T (= I 5)) )
   (7 . 0)
   (6 . 1)
   (5 . 2)
   (4 . 3)
   (3 . 4)
   -> NIL

   : (make
      (for ((I . Q) (goal '((+ @X @Y 7))) (prove Q))
         (bind @ (link (cons @X @Y)))
         (T (= I 5)) ) )
   -> ((7 . 0) (6 . 1) (5 . 2) (4 . 3) (3 . 4))

☺/ A!ex

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

Reply via email to