Answer depends on a clause position - is that acceptable? #3004
-
Hello. While trying to parse numbers I found out that answers are different when default clause of a
When I run it, I see the following
But when I move
I am wondering if this is okay to rely on clause definition position, or I have done something very wrong somewhere. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 23 replies
-
In a pure Prolog program order of clause can only influence termination properties or order of solutions. Generally it is a bad idea to rely on it. Also you are using |
Beta Was this translation helpful? Give feedback.
-
If
And:
So there are more solutions (the answer(s) is the same, the order of the solutions doesn't matter in logic programming, it's a set), but with So with As I see it. |
Beta Was this translation helpful? Give feedback.
-
Returning to the OP question, problem lies in |
Beta Was this translation helpful? Give feedback.
-
Thank you all for the comments! |
Beta Was this translation helpful? Give feedback.
-
As you said in a comment, you need more (parsing a programming language) than converting a string to a numeric data-type. And as I said, I'm only a naive user of Prolog. But, what about an approach like this (yes, it's quick and dirty and naive, no "optimization thinking" here) for converting a string to a number (yes, you need more, and this is only a toy even for "string-numbers"):
That In my imagination, it can be done in another way (to get the "num-string" ready to converting): by filtering out the separator, or mapping the separator to the empty string, if I know that I have only digits with separator(s). Or with a different definition of And then, for example:
And now, maybe, time for
Because of:
|
Beta Was this translation helpful? Give feedback.
-
As I see it, your I apologize for writing here again; I use your code (plus my writing, plus examples/writing of others) as my own learning opportunity. So, thank you for posting it/the question here. |
Beta Was this translation helpful? Give feedback.
-
A great remark, I think. And maybe everything is on the web somewhere (for example, @UWN and @triska, Prolog experts and computer scientists, write things on-line), but I think, it might be a good candidate for "the first steps" on the Scryer web site, as parsing numbers (even in various DSL-like data-forms) from a text file might be useful. My naive view... Here, I tried something different. A "brutal", extreme top-down, slow & "baroque", numerically fragile, definition of
|
Beta Was this translation helpful? Give feedback.
In a pure Prolog program order of clause can only influence termination properties or order of solutions. Generally it is a bad idea to rely on it. Also you are using
once/1
that means that your grammar is ambiguous and you just limiting yourself to the first solution which may or may not be what you want. Try to define unambiguous grammar.