matsud224 / wamcompiler

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

Book about wamprolog and checking at cut point #5

Closed FemtoEmacs closed 4 years ago

FemtoEmacs commented 4 years ago

Hi, Daiki. As I promissed, I am writing a book on the wamcompiler and the use of the Prolog/Lisp combo for practical applications. I am not a good writer, therefore my text is full of mistakes. However, Vernon Siepple, an Englishman who is a professional writer, will spend 2 hours a week reviewing my text. Mr. Vernon thinks that the subject is very interesting, but unfortunately he cannot spend more than two hours a week on the book. Patricia, one of the best cartoonists I know, will take care of the Illustrations. Again, she cannot spend more than a couple of hours a week in this kind of voluntary job. Thus, the first edition of the book will have very few cartoons.

As for the technical aspects, I modified the send-query procedure to check whether the cut point is a valid integer. If it is not, send-query raises a condition of prolog-builtin-predicate-error. One of the consequences of the error in the implementation of the cut is that the + not as failure does not work, and I was forced to implement a version of the + not predicate that does not depend on the cut. You will find my definition of the cut at the end of the wamcompiler.lisp file. Please, try to improve my definition for efficiency. You can test the + not definition with the implementation of the Nim game as found in the book How to Solve it in Prolog, by Helder Coelho. By the way, I intend to implement all the examples of Coelho's book.

Since you added a cut-based definition of + not in the prelude, I infer that the error does not show up in previous versions of the wamcompiler. Otherwise, you certainly would have noticed that the + not definition was not working. Could you check whether the handling of the cut was working in a previous version of the wamcompiler?

Of course, the checking for a valid integer is a temporary solution. Everybody hopes that you will find time to fix the problem conclusively. In the mean time, here is the modification that I applied to the send-query procedure:

 (cut (let* ( (y (cadr inst))
                  (cut-instruction
                  (when (and (numberp *E*) 
                         (numberp *B*)) 
                    (stack (addr+ *E* 2 y)) )))
            (unless (numberp cut-instruction)
               (error (make-condition 
                    'prolog-builtin-predicate-error
                         :predicate (cons '|!| 0)
                     :cause "Avoid ! after 'V is exp")))
              (when (addr<  cut-instruction *B*)
                (setf *B* cut-instruction )
                (tidy-trail))
              (setq *P* (cdr *P*))))
matsud224 commented 4 years ago

Thank you for sending a PR! And sorry for late response...

I am writing a book on the wamcompiler and the use of the Prolog/Lisp combo for practical applications.

I never seen such a book and I'm very interested in the book of the "Prolog/Lisp combo for practical applications". I'm looking forward to the completion of the book!

In the mean time, here is the modification that I applied to the send-query procedure:

I think that the checking of the integer and showing error message is better than falling into the debugger. But an error message should imply that this error is caused by a compiler or virtual machine's bug.

In addition, although the error message is "Avoid ! after 'V is exp", it's not only is/2 predicate. Here is an example:

> qub(N,F) :- N < 5, N < 4, !.
> ?-qub(2,F).

debugger invoked on a SIMPLE-TYPE-ERROR in thread
#<THREAD "main thread" RUNNING {100399C713}>:
  Argument X is not a REAL: (CON . 2)

I'll try to solve this problem, but I can't make enough time...

matsud224 commented 4 years ago

I added you to the collaborator of this repository. You can push to this repository freely, but I want you to make a PR if you make a big modification to wamcompiler.lisp.

FemtoEmacs commented 4 years ago

The only thing that you will find in the last pull request is the book/tutorial on wamcompiler. For the book, I was able to find a get-around the bug of the cut, and solve problems from 1 to 25 of Hett's Ninety-Nine Prolog Problems.

Em seg., 16 de dez. de 2019 às 12:14, Daiki Matsunaga < notifications@github.com> escreveu:

Thank you for sending a PR! And sorry for late response...

I am writing a book on the wamcompiler and the use of the Prolog/Lisp combo for practical applications.

I never seen such a book and I'm very interested in the book of the "Prolog/Lisp combo for practical applications". I'm looking forward to the completion of the book!

In the mean time, here is the modification that I applied to the send-query procedure:

I think that the checking of the integer and showing error message is better than falling into the debugger. But an error message should imply that this error is caused by a compiler or virtual machine's bug.

In addition, although the error message is "Avoid ! after 'V is exp", it's not only is/2 predicate. Here is an example:

qub(N,F) :- N < 5, N < 4, !. ?-qub(2,F).

debugger invoked on a SIMPLE-TYPE-ERROR in thread

<THREAD "main thread" RUNNING {100399C713}>:

Argument X is not a REAL: (CON . 2)

I'll try to solve this problem, but I can't make enough time...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/matsud224/wamcompiler/pull/5?email_source=notifications&email_token=AFBHKDYMT35THZ3X6ZAKED3QY6LOJA5CNFSM4J3DULW2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEG7A3EQ#issuecomment-566103442, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFBHKD4HPHGTLKNRSISYAF3QY6LOJANCNFSM4J3DULWQ .

FemtoEmacs commented 4 years ago

Hi, Daiki. You have sent the following example of bug causing predicate:

qub(N,F) :- N < 5, N < 4, !.

However, if you check explicitly the type of N, the bug does not show up:

que(N,F) :- N < 5, N < 4, !, number(N). ?- que(2,F). F =

I suspect that, when the CUT instruction appears after (PUT-UNSAFE-VALUE i j), we have a problem. However, if the CUT instruction appears before (PUT-UNSAFE-VALUE i j), the predicate works. Let us see a few examples.

› rlwrap ros run --load "../../wamcompiler.lisp"

Example 1:

qub(N,F) :- N < 5, N < 4, !. ?- qub(2,F). Error in builtin-predicate(!/0): Avoid ! after 'V is expr' or 'V= F' ?-halt.

yes. NIL

Example 2:

qua(N,F) :- N < 5, N < 4, number(N), !. ?- qua(2, F). Error in builtin-predicate(!/0): Avoid ! after 'V is expr' or 'V= F'

You will see that the (CUT 1 1) will appear after (PUT-UNSAFE-VALUE 2 1 2):

?-halt.

yes. NIL

Example 3:

It worked. Then I infer that (CUT 2 2) appears before (PUT-UNSAFE-VALUE 1 1). Let us see:

?-halt.

yes. NIL

Example 4:

I guess that we will find CUT after PUT-UNSAFE-VALUE. And there it is:

?-halt.

yes. NIL

yes.

Example 5: Now, the CUT should appear before PUT-UNSAFE-VALUE. Let us check:

Of course, I can be acting like Skinner's dove. You know the story, instead of feeding his doves whenever they did something cute, Skinner start using the weather to decide when he would feed his doves. In a few moths on this kind of regime, the doves started to dance, coo strange tunes, etc. Skinner concluded that the doves had invented a theory about what they needed to do, in order to receive food.

Em seg., 16 de dez. de 2019 às 12:14, Daiki Matsunaga < notifications@github.com> escreveu:

Thank you for sending a PR! And sorry for late response...

I am writing a book on the wamcompiler and the use of the Prolog/Lisp combo for practical applications.

I never seen such a book and I'm very interested in the book of the "Prolog/Lisp combo for practical applications". I'm looking forward to the completion of the book!

In the mean time, here is the modification that I applied to the send-query procedure:

I think that the checking of the integer and showing error message is better than falling into the debugger. But an error message should imply that this error is caused by a compiler or virtual machine's bug.

In addition, although the error message is "Avoid ! after 'V is exp", it's not only is/2 predicate. Here is an example:

qub(N,F) :- N < 5, N < 4, !. ?-qub(2,F).

debugger invoked on a SIMPLE-TYPE-ERROR in thread

<THREAD "main thread" RUNNING {100399C713}>:

Argument X is not a REAL: (CON . 2)

I'll try to solve this problem, but I can't make enough time...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/matsud224/wamcompiler/pull/5?email_source=notifications&email_token=AFBHKDYMT35THZ3X6ZAKED3QY6LOJA5CNFSM4J3DULW2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEG7A3EQ#issuecomment-566103442, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFBHKD4HPHGTLKNRSISYAF3QY6LOJANCNFSM4J3DULWQ .

matsud224 commented 4 years ago

By the way, I think that the book you're writing should be managed in a separate repository because commits to source code and to TeX code of the book is mixed. If you make the book's repository, I'll add the link to README.

FemtoEmacs commented 4 years ago

Great idea. The book will be in the Femto Emacs:

https://github.com/FemtoEmacs/lisp-plus-prolog

Since Vernon is writing at a very slow pace, wait until you are satisfied with the content of the book, before including the Link. By the way, Vernon and I wrote other books about Prolog. One of our books is Visual Prolog for Tyros, that was very popular a few years ago. We are going to recycle this old stuff. Therefore, don't worry if you find loans from Visual Prolog for Tyros.

Em ter., 17 de dez. de 2019 às 11:19, Daiki Matsunaga < notifications@github.com> escreveu:

By the way, I think that the book you're writing should be managed in a separate repository because commits to source code and to TeX code of the book is mixed. If you make the book's repository, I'll add the link to README.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/matsud224/wamcompiler/pull/5?email_source=notifications&email_token=AFBHKDZIBMT5JIEYTREXHHLQZDNYLA5CNFSM4J3DULW2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHCQOWQ#issuecomment-566560602, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFBHKDYS5AIMIUBZKIJAEUTQZDNYLANCNFSM4J3DULWQ .

matsud224 commented 4 years ago

I fixed the bug of cut by commit 2724406.