speeding up Shen (S-series) Prolog by backend peephole optimisation?

17 views
Skip to first unread message

dr.mt...@gmail.com

unread,
Mar 15, 2023, 6:21:00 AM3/15/23
to Shen
Neal spoke of speeding up the S series Prolog.   This might have some effect.

Inside the S series Prolog kernel code there are procedure calls or GOTOs done in a functional way.  These calls obviate exponential code generation 

This Shen Prolog.program.

(defprolog house
   [Nationality Pet] <--;)


generates

(defun house
 (V22760 V22761 V22762 V22763 V22764)
 (if
  (shen.unlocked? V22762)
  (let Tm22754
   (shen.lazyderef V22760 V22761)
   (let GoTo22755
    (lambda Nationality
     (lambda Pet
      (do
       (shen.incinfs)
       (thaw V22764))))

    (if
     (cons? Tm22754)
     (let Nationality
      (hd Tm22754)
      (let Tm22756
       (shen.lazyderef
        (tl Tm22754) V22761)
       (let GoTo22757
        (lambda Pet
         (
          (GoTo22755 Nationality) Pet))
        (if
         (cons? Tm22756)
         (let Pet
          (hd Tm22756)
          (let Tm22758
           (shen.lazyderef
            (tl Tm22756) V22761)
           (let GoTo22759
            (freeze
             (GoTo22757 Pet))
            (if
             (= Tm22758
              ())
             (thaw GoTo22759)
             (if
              (shen.pvar? Tm22758)
              (shen.bind! Tm22758
               () V22761 GoTo22759) false)))))
         (if
          (shen.pvar? Tm22756)
          (let Pet
           (shen.newpv V22761)
           (shen.gc V22761
            (shen.bind! Tm22756
             (cons Pet
              ()) V22761
             (freeze
              (GoTo22757 Pet))))) false)))))
     (if
      (shen.pvar? Tm22754)
      (let Nationality
       (shen.newpv V22761)
       (shen.gc V22761
        (let Pet
         (shen.newpv V22761)
         (shen.gc V22761
          (shen.bind! Tm22754
           (cons Nationality
            (cons Pet
             ())) V22761
           (freeze
            (
             (GoTo22755 Nationality) Pet))))))) false)))) false))

I've bolded the relevant code.  What you have is an n-place lambda expression applied to n inputs.  The code is classically correct, but in a language like Scheme or CL, the currying is not needed because (lambda X (lambda Y Z)) is just (lambda (X Y) Z) and
((GoTo22755 Nationality) Pet) would be (GoTo22755 Nationality Pet).  

It's a fiddly optimisation which is why I have not bothered much with it.   It is a backend optimisation.

M.



Reply all
Reply to author
Forward
0 new messages