Evaluation order of Prolog fork and findall

64 views
Skip to first unread message

nha...@gmail.com

unread,
Jul 18, 2022, 12:32:01 PM7/18/22
to Shen
I'm compiling Shen prolog directly to an SWI prolog backend and converting data back and forth via its FFI.

Since Shen prolog allows nested calls to regular Shen functions within its expressions, I'm lifting some of the nested calls into administrative normal form before compiling to SWI prolog. Higher order calls with prolog functions need to be kept as unevaluated prolog terms, so they can get passed to SWI's 'call' at some point.

I hit a bit of a snag with Shen Prolog's call, fork, and findall due to the way they use higher order predicates, and how they rely on closures and the fact that Shen prolog functions have extra hidden parameters, which ends up changing the evaluation order.

For example, (findall V (three (push1 V)) X) doesn't follow applicative order, since the call to 'three' is wrapped in lambda. It's unclear what the best way to deal with this is. At the moment, the call to 'push1' is being lifted out of the findall due to ANF. Can I just leave it like that, or do I have to simulate the lazy evaluation in all higher order uses of prolog predicates?  

(defprolog three
  [1|1] <--;
  [1|2] <--;
  [1|3] <--;)

(define push1
  X -> (cons 1 X))

(defprolog test-find2
  X <-- (findall V (three (push1 V)) X);)

Produces:

[findall V [lambda Y6448
             [lambda Y6449
               [lambda Y6450
                 [lambda Y6451
                   [three [push1 [shen.deref V V6453]]
                          Y6448 Y6449 Y6450 Y6451]]]]]
         V6452 V6453 V6454 V6455 V6456]



nha...@gmail.com

unread,
Jul 18, 2022, 5:17:15 PM7/18/22
to Shen
Actually, I think I sorted it out, it doesn't seem to be as complicated as I thought.

In SWI you can just call compound expressions in the findall body like: findall(V,(writeln("2"),three(V),writeln("3")),VS).

Then all you have to do is delimit the ANF transform on prolog calls. 

Mark Tarver

unread,
Jul 19, 2022, 3:11:58 AM7/19/22
to Shen
There is an issue with the compilation of lambda expressions within the 
body of a Horn clause in Shen Prolog.

Shen, www.shenlanguage.org, copyright (C) 2010-2021, Mark Tarver
version: S32.01, language: Common Lisp, platform: SBCL 2.0.0
port 3.1, ported by Mark Tarver


(0-) (defprolog f
  X <-- (return ((/. Y Y) X));)
; in: DEFUN f
;     (FUNCALL #'(LAMBDA ((|shen.deref| Y V6146)) (|shen.deref| Y V
6146))
;              (|shen.deref| V6145 V6146))
; ==>
;   (SB-C::%FUNCALL #'(LAMBDA ((|shen.deref| Y V6146)) (|shen.deref
| Y V6146))
;                   (|shen.deref| V6145 V6146))
;
; caught ERROR:
;   Required argument is not a symbol: (shen.deref Y V6146)
;
; compilation unit finished
;   caught 1 ERROR condition
(fn f)


I've fixed this in SP over the cloud.

connecting to the web ...

Shen, copyright (C) 2010-2020 Mark Tarver
www.shenlanguage.org, Shen Professional Edition 32.02
running under Common Lisp, implementation: SBCL
port 3.1 ported by Mark Tarver
commercially licensed to Mark Tarver


(0-) (defprolog f
       X <-- (return ((/. Y Y) X));)
(fn f)

(1-) (prolog? (f a))
a

32.02 will be released in the next few days.

Mark

nha...@gmail.com

unread,
Jul 28, 2022, 4:11:32 AM7/28/22
to Shen
In the test suite, there are calls like:

(report "einsteins riddle"

   (load "einsteins-riddle.shen") loaded
   (prolog? (riddle)) german)


https://shenlanguage.org/TBoS/tbos_376.html#34; mentions that certain higher order predicates only work when they are defined before being used.

In the above code the (prolog?) statement gets compiled before the load is performed, so 'riddle' is unknown to the system.

I know this works with the default Shen prolog implementation, but do I need to make this work with my SWI prolog backend?

Mark Tarver

unread,
Jul 29, 2022, 2:21:59 AM7/29/22
to Shen
I think that in the above case the prolog? statement is macroexpanded although no Prolog
call is made.  The logical order therefore is that riddle is loaded and the macroexpanded 
code is then called.

If you're experimenting with a logical-functional language you're free to choose your own
strategy.

The footnote in TBoS refers to  arguments to higher order predicates in Prolog which are not used in this
example though at the moment I cannot find an instance where Shen fails in this instance.

Mark 

Reply all
Reply to author
Forward
0 new messages