-
Consider the following (trivial) code: foo(a(X)) :- bar(X).
foo(b(X)) :- baz(X). and the following query: ?- foo(c(X)). What is the best/recommended way to distinguish failure to match head of a goal from other fault? I commonly tag my variables with "types" for example: There is a common pattern: foo(a(X)) :- !, bar(X).
foo(b(X)) :- !, baz(X).
foo(Y) :- throw(error(domain_error(unmatched_head,Y),_)). Or some variations of it. I find it quite inelegant, because I need to modify every |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 13 replies
-
It all depends on what your predicate is about. Is it truly relational? Then go along library(error) must_be/2 and can_be/2. If you issue a domain error, the first argument should be the domain not the observed problem. In general it is better to state with the error what is expected and less what is 'the problem'. See the error classification on how errors are formulated in ISO. |
Beta Was this translation helpful? Give feedback.
-
As a beginner, I do not use the cut (!), more, my understanding of the Prolog cut operator is only vague, mostly like this: do not use it, if you want declarative programming. But, If I have to distinguish and really explicitly report to the user (me) the "unmatched head case" then... Then, as @UWN wrote: it all depends... But, anyway, with one very strict/hard assumption, I see something like this:
I know... I see you, the thumb down... |
Beta Was this translation helpful? Give feedback.
-
Why I started this seemingly trivial topic is because it is actually a major pain point for writing programs of any substantial size. Prolog has so much potential to analyze your code for errors, but in the end I need to validate predicate parameters at runtime like in JavaScript. I feel that there is a more elegant solution... |
Beta Was this translation helpful? Give feedback.
-
How do these unintended failures happen in the cases you mean? Do they arise for example when one mistypes a goal in a predicate? In such cases, for pure monotonic programs, you can use |
Beta Was this translation helpful? Give feedback.
It all depends on what your predicate is about. Is it truly relational? Then go along library(error) must_be/2 and can_be/2.
Currently, your common pattern misses an instantiation error, as
?- foo(G).
just sticks to the first clause.If you issue a domain error, the first argument should be the domain not the observed problem. In general it is better to state with the error what is expected and less what is 'the problem'. See the error classification on how errors are formulated in ISO.