matsud224 / wamcompiler

Prolog implementation based on Warren's abstract machine
The Unlicense
41 stars 6 forks source link

Temporary fix for bug #7

Open FemtoEmacs opened 4 years ago

FemtoEmacs commented 4 years ago

I placed a temporary fix for the neck-cut bug. I will not make a Pull request because I myself am not satisfied with the solution. It works, but is not very elegant. In any case, after the patch, I was able to solve Werner Hett's Ninety-Nine Prolog problems from 1 to 35. I used the numeration that appears at the page of UNICAMP, not Hett's. It seems that my awkward and coarse patch did not impaired the speed of wamcompiler. You will find wamcompiler with the patch and the Ninety-Nine Prolog problems here at the Femto Emacs fork of your repository:

https://github.com/FemtoEmacs/wamcompiler

You need to download three files for testing the Ninety-Nine Prolog problems, to wit:

  1. rnd.lisp -- this file contains a random number generator and need to be load before executing (repl)
  2. p99.pl -- that contains 35 problems from Hett's collection
  3. wamcompiler with the patch

In a nutshell, the patch introduces a `true` predicate in front of the cut, in case of neck-cut. Thus, the neck-cut becomes a normal cut.

```lisp
(defun add-true(s)
  (if (and (consp s) (equal (car s) '(|!|)) ) (cons '(|true|) s) s))

(defun divide-head-body (clause)
  (destructuring-bind
    (head body clause-type)
        (cond
            ( (and (eq (car clause) '|:-|) (= (arity clause) 2))
               (list (cadr clause) 
                     (add-true (flatten-comma (caddr clause))) 'rule))
;;; ... etc

(define-prolog-builtin "true" ()
  (let  ((wam-code '((proceed))))
    (setf *P* wam-code)))