What is never equal to itself?












2















Is there value in Prolog that is not equal to itself? I write answer to some question about min of tree and this answer also says that if tree is empty min is null. Sounds good idea first but now when I think it sounds like bad idea.



It is kinda OK if null <> null, no problem. But in Prolog I see null is just atom so....



?- null = null.
true.

?- null == null.
true.

?- dif(null, null).
false.


How can I make some term in Prolog that always say:



?- dif(Something, Something).
true.


But if it is any other thing and not this term that is the null thing still say false.?



Or if this is not how I should think in Prolog then how should I think about not true. and also not false. but "neither true nor false because something is missing"?










share|improve this question




















  • 1





    Such a thing is not possible, you'd have to make your own predicate with the semantics you want, probably by pattern matching on an atom like null.

    – Daniel Lyons
    Nov 21 '18 at 4:49











  • @DanielLyons but how do you know it is not possible? If it is possible but you do not know is it the same as not possible? Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Like null.

    – user10631003
    Nov 21 '18 at 5:01






  • 1





    Prolog's = operator is not simply a way of asking if two things are equal, it is the heart and soul of unification itself which is one of the two major differences between Prolog and all other languages. It would be quite a strange and warty mess if that operator sometimes did not unify simply because some imperative programmer came along wanting to repeat the billion dollar mistake. Again, you could make your own equality predicate, but as you suspect, null is just a bad idea that has no place in declarative programming.

    – Daniel Lyons
    Nov 21 '18 at 7:48






  • 2





    As far as the answer you gave to the other question, it is up to you whether you want a list that really has no minimum to actually succeed with a minimum of null. As your code in that answer shows, it does become a little cumbersome dealing with the null case. It's easier to just define the predicate such that it succeeds if there is a minimum, but fails otherwise. Which is better? It depends upon the use case, which often isn't given in such questions.

    – lurker
    Nov 21 '18 at 12:16











  • Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Yes, but false means "not provable". Prolog can only tell you if it can prove something. If it can't, it fails. It has no way of knowing the "universe of what is possible but not provable" with the given facts/rules. So it can't have a "maybe" answer. null itself, as you say, is just an atom. It has no truth value. null = null must be false since null is an atom and, in Prolog, if two atoms are identical, they are necessarily (trivially) unifiable.

    – lurker
    Nov 21 '18 at 13:50


















2















Is there value in Prolog that is not equal to itself? I write answer to some question about min of tree and this answer also says that if tree is empty min is null. Sounds good idea first but now when I think it sounds like bad idea.



It is kinda OK if null <> null, no problem. But in Prolog I see null is just atom so....



?- null = null.
true.

?- null == null.
true.

?- dif(null, null).
false.


How can I make some term in Prolog that always say:



?- dif(Something, Something).
true.


But if it is any other thing and not this term that is the null thing still say false.?



Or if this is not how I should think in Prolog then how should I think about not true. and also not false. but "neither true nor false because something is missing"?










share|improve this question




















  • 1





    Such a thing is not possible, you'd have to make your own predicate with the semantics you want, probably by pattern matching on an atom like null.

    – Daniel Lyons
    Nov 21 '18 at 4:49











  • @DanielLyons but how do you know it is not possible? If it is possible but you do not know is it the same as not possible? Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Like null.

    – user10631003
    Nov 21 '18 at 5:01






  • 1





    Prolog's = operator is not simply a way of asking if two things are equal, it is the heart and soul of unification itself which is one of the two major differences between Prolog and all other languages. It would be quite a strange and warty mess if that operator sometimes did not unify simply because some imperative programmer came along wanting to repeat the billion dollar mistake. Again, you could make your own equality predicate, but as you suspect, null is just a bad idea that has no place in declarative programming.

    – Daniel Lyons
    Nov 21 '18 at 7:48






  • 2





    As far as the answer you gave to the other question, it is up to you whether you want a list that really has no minimum to actually succeed with a minimum of null. As your code in that answer shows, it does become a little cumbersome dealing with the null case. It's easier to just define the predicate such that it succeeds if there is a minimum, but fails otherwise. Which is better? It depends upon the use case, which often isn't given in such questions.

    – lurker
    Nov 21 '18 at 12:16











  • Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Yes, but false means "not provable". Prolog can only tell you if it can prove something. If it can't, it fails. It has no way of knowing the "universe of what is possible but not provable" with the given facts/rules. So it can't have a "maybe" answer. null itself, as you say, is just an atom. It has no truth value. null = null must be false since null is an atom and, in Prolog, if two atoms are identical, they are necessarily (trivially) unifiable.

    – lurker
    Nov 21 '18 at 13:50
















2












2








2








Is there value in Prolog that is not equal to itself? I write answer to some question about min of tree and this answer also says that if tree is empty min is null. Sounds good idea first but now when I think it sounds like bad idea.



It is kinda OK if null <> null, no problem. But in Prolog I see null is just atom so....



?- null = null.
true.

?- null == null.
true.

?- dif(null, null).
false.


How can I make some term in Prolog that always say:



?- dif(Something, Something).
true.


But if it is any other thing and not this term that is the null thing still say false.?



Or if this is not how I should think in Prolog then how should I think about not true. and also not false. but "neither true nor false because something is missing"?










share|improve this question
















Is there value in Prolog that is not equal to itself? I write answer to some question about min of tree and this answer also says that if tree is empty min is null. Sounds good idea first but now when I think it sounds like bad idea.



It is kinda OK if null <> null, no problem. But in Prolog I see null is just atom so....



?- null = null.
true.

?- null == null.
true.

?- dif(null, null).
false.


How can I make some term in Prolog that always say:



?- dif(Something, Something).
true.


But if it is any other thing and not this term that is the null thing still say false.?



Or if this is not how I should think in Prolog then how should I think about not true. and also not false. but "neither true nor false because something is missing"?







prolog prolog-dif






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 19:03









false

10.5k771144




10.5k771144










asked Nov 21 '18 at 4:28







user10631003















  • 1





    Such a thing is not possible, you'd have to make your own predicate with the semantics you want, probably by pattern matching on an atom like null.

    – Daniel Lyons
    Nov 21 '18 at 4:49











  • @DanielLyons but how do you know it is not possible? If it is possible but you do not know is it the same as not possible? Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Like null.

    – user10631003
    Nov 21 '18 at 5:01






  • 1





    Prolog's = operator is not simply a way of asking if two things are equal, it is the heart and soul of unification itself which is one of the two major differences between Prolog and all other languages. It would be quite a strange and warty mess if that operator sometimes did not unify simply because some imperative programmer came along wanting to repeat the billion dollar mistake. Again, you could make your own equality predicate, but as you suspect, null is just a bad idea that has no place in declarative programming.

    – Daniel Lyons
    Nov 21 '18 at 7:48






  • 2





    As far as the answer you gave to the other question, it is up to you whether you want a list that really has no minimum to actually succeed with a minimum of null. As your code in that answer shows, it does become a little cumbersome dealing with the null case. It's easier to just define the predicate such that it succeeds if there is a minimum, but fails otherwise. Which is better? It depends upon the use case, which often isn't given in such questions.

    – lurker
    Nov 21 '18 at 12:16











  • Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Yes, but false means "not provable". Prolog can only tell you if it can prove something. If it can't, it fails. It has no way of knowing the "universe of what is possible but not provable" with the given facts/rules. So it can't have a "maybe" answer. null itself, as you say, is just an atom. It has no truth value. null = null must be false since null is an atom and, in Prolog, if two atoms are identical, they are necessarily (trivially) unifiable.

    – lurker
    Nov 21 '18 at 13:50
















  • 1





    Such a thing is not possible, you'd have to make your own predicate with the semantics you want, probably by pattern matching on an atom like null.

    – Daniel Lyons
    Nov 21 '18 at 4:49











  • @DanielLyons but how do you know it is not possible? If it is possible but you do not know is it the same as not possible? Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Like null.

    – user10631003
    Nov 21 '18 at 5:01






  • 1





    Prolog's = operator is not simply a way of asking if two things are equal, it is the heart and soul of unification itself which is one of the two major differences between Prolog and all other languages. It would be quite a strange and warty mess if that operator sometimes did not unify simply because some imperative programmer came along wanting to repeat the billion dollar mistake. Again, you could make your own equality predicate, but as you suspect, null is just a bad idea that has no place in declarative programming.

    – Daniel Lyons
    Nov 21 '18 at 7:48






  • 2





    As far as the answer you gave to the other question, it is up to you whether you want a list that really has no minimum to actually succeed with a minimum of null. As your code in that answer shows, it does become a little cumbersome dealing with the null case. It's easier to just define the predicate such that it succeeds if there is a minimum, but fails otherwise. Which is better? It depends upon the use case, which often isn't given in such questions.

    – lurker
    Nov 21 '18 at 12:16











  • Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Yes, but false means "not provable". Prolog can only tell you if it can prove something. If it can't, it fails. It has no way of knowing the "universe of what is possible but not provable" with the given facts/rules. So it can't have a "maybe" answer. null itself, as you say, is just an atom. It has no truth value. null = null must be false since null is an atom and, in Prolog, if two atoms are identical, they are necessarily (trivially) unifiable.

    – lurker
    Nov 21 '18 at 13:50










1




1





Such a thing is not possible, you'd have to make your own predicate with the semantics you want, probably by pattern matching on an atom like null.

– Daniel Lyons
Nov 21 '18 at 4:49





Such a thing is not possible, you'd have to make your own predicate with the semantics you want, probably by pattern matching on an atom like null.

– Daniel Lyons
Nov 21 '18 at 4:49













@DanielLyons but how do you know it is not possible? If it is possible but you do not know is it the same as not possible? Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Like null.

– user10631003
Nov 21 '18 at 5:01





@DanielLyons but how do you know it is not possible? If it is possible but you do not know is it the same as not possible? Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Like null.

– user10631003
Nov 21 '18 at 5:01




1




1





Prolog's = operator is not simply a way of asking if two things are equal, it is the heart and soul of unification itself which is one of the two major differences between Prolog and all other languages. It would be quite a strange and warty mess if that operator sometimes did not unify simply because some imperative programmer came along wanting to repeat the billion dollar mistake. Again, you could make your own equality predicate, but as you suspect, null is just a bad idea that has no place in declarative programming.

– Daniel Lyons
Nov 21 '18 at 7:48





Prolog's = operator is not simply a way of asking if two things are equal, it is the heart and soul of unification itself which is one of the two major differences between Prolog and all other languages. It would be quite a strange and warty mess if that operator sometimes did not unify simply because some imperative programmer came along wanting to repeat the billion dollar mistake. Again, you could make your own equality predicate, but as you suspect, null is just a bad idea that has no place in declarative programming.

– Daniel Lyons
Nov 21 '18 at 7:48




2




2





As far as the answer you gave to the other question, it is up to you whether you want a list that really has no minimum to actually succeed with a minimum of null. As your code in that answer shows, it does become a little cumbersome dealing with the null case. It's easier to just define the predicate such that it succeeds if there is a minimum, but fails otherwise. Which is better? It depends upon the use case, which often isn't given in such questions.

– lurker
Nov 21 '18 at 12:16





As far as the answer you gave to the other question, it is up to you whether you want a list that really has no minimum to actually succeed with a minimum of null. As your code in that answer shows, it does become a little cumbersome dealing with the null case. It's easier to just define the predicate such that it succeeds if there is a minimum, but fails otherwise. Which is better? It depends upon the use case, which often isn't given in such questions.

– lurker
Nov 21 '18 at 12:16













Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Yes, but false means "not provable". Prolog can only tell you if it can prove something. If it can't, it fails. It has no way of knowing the "universe of what is possible but not provable" with the given facts/rules. So it can't have a "maybe" answer. null itself, as you say, is just an atom. It has no truth value. null = null must be false since null is an atom and, in Prolog, if two atoms are identical, they are necessarily (trivially) unifiable.

– lurker
Nov 21 '18 at 13:50







Because in Prolog if it is not true it is false but in reality if it is not true maybe it is also not false but we just do not know it. Yes, but false means "not provable". Prolog can only tell you if it can prove something. If it can't, it fails. It has no way of knowing the "universe of what is possible but not provable" with the given facts/rules. So it can't have a "maybe" answer. null itself, as you say, is just an atom. It has no truth value. null = null must be false since null is an atom and, in Prolog, if two atoms are identical, they are necessarily (trivially) unifiable.

– lurker
Nov 21 '18 at 13:50














1 Answer
1






active

oldest

votes


















3














Just for fun, not really the answer you're looking for, taking the question title quite literally:



?- _ == _ .
false.


But dif/2 is not fouled (hint: each occurrence of the anonymous variable represents a different variable):



?- dif(_, _).
true.


Now, seriously. Starting with your tree minimum predicate example, there's a trivial alternative: the predicate can simply fail when the tree is empty. A better alternative may be to use optional or expected term libraries. The concepts behind these libraries are found in several programming languages, where they provide a better alternative to null. You have both libraries in Logtalk, which you can use with most Prolog systems. See:




  • https://logtalk.org/library/optional_0.html

  • https://logtalk.org/library/optional_1.html


and




  • https://logtalk.org/library/expected_0.html

  • https://logtalk.org/library/expected_1.html


You use one library or the other depending on your interpretation of "missing" meaning something that is optional (absence of a value is fine) or expected (absence of a value is an error). For example, assume that in your particular application it makes sense to use 0 as the minimum value of an empty tree when doing a specific computation (e.g. the sum of the minimums of a set of trees). If the tree minimum predicate returns an optional term reference, Ref, instead of an integer, you could do e.g.



...,
optional(Ref)::or_else(Minimum, 0),
Sum1 is Sum0 + Minimum,
...


This is a cleaner solution compared with using an if-then-else construct:



...,
( tree_minimum(Tree, Minimum) ->
Sum1 is Sum0 + Minimum
; Sum1 is Sum0
),
...


It also allows you to use different defaults for different computations. For example:



...,
optional(Ref)::or_else(Minimum, 1),
Product1 is Product0 * Minimum,
...


More important, it doesn't mask that you're processing an empty tree in the same way that a default value would do. For example, the following code will only write the minimum values of non-empty trees:



print_tree_minimums(Refs) :-
meta::map(print_tree_minimum, Refs).

print_tree_minimum(Ref) :-
optional(Ref)::if_present(write).


or, using a lambda expression:



print_tree_minimums(Refs) :-
meta::map([Ref]>>(optional(Ref)::if_present(write)), Refs).


This answer is getting long and I don't want to transform it into a general discussion of the pros and cons of optionals and expecteds. But descriptions on both concepts and libraries is easy to find. E.g.



https://en.wikipedia.org/wiki/Option_type



https://youtu.be/NhcHwkUPX7w






share|improve this answer





















  • 1





    Brilliant answer, thank you so much Paulo Moura! So this is somehow similar to optional in Java if I read correctly? It still does not solve problem but better than "ooh you cannot".

    – user10631003
    Nov 22 '18 at 4:21











  • Thanks. Java optionals indeed provide similar functionality.

    – Paulo Moura
    Nov 22 '18 at 9:13











  • I cannot yet accept answer because I now understand that no one really understood question of mine which was not very good. Still you give excellent answer and I will try to make better question and try your approach you explain in this answer.

    – user10631003
    Nov 22 '18 at 16:33











  • @MunDong it would be better in such a circumstance to accept Paulo's answer and then ask a new question, if it would be different enough to require a different answer.

    – Daniel Lyons
    Nov 22 '18 at 21:20











  • @DanielLyons Thank you very much for your kind suggestion. But if the asnwer does not answer the question or if I do not yet know certainly if it answers the question because I have not yet tried out the code on my own than you can kindly wait until I know for certain, then I can accept the answer.

    – user10631003
    Nov 23 '18 at 7:06













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405266%2fwhat-is-never-equal-to-itself%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Just for fun, not really the answer you're looking for, taking the question title quite literally:



?- _ == _ .
false.


But dif/2 is not fouled (hint: each occurrence of the anonymous variable represents a different variable):



?- dif(_, _).
true.


Now, seriously. Starting with your tree minimum predicate example, there's a trivial alternative: the predicate can simply fail when the tree is empty. A better alternative may be to use optional or expected term libraries. The concepts behind these libraries are found in several programming languages, where they provide a better alternative to null. You have both libraries in Logtalk, which you can use with most Prolog systems. See:




  • https://logtalk.org/library/optional_0.html

  • https://logtalk.org/library/optional_1.html


and




  • https://logtalk.org/library/expected_0.html

  • https://logtalk.org/library/expected_1.html


You use one library or the other depending on your interpretation of "missing" meaning something that is optional (absence of a value is fine) or expected (absence of a value is an error). For example, assume that in your particular application it makes sense to use 0 as the minimum value of an empty tree when doing a specific computation (e.g. the sum of the minimums of a set of trees). If the tree minimum predicate returns an optional term reference, Ref, instead of an integer, you could do e.g.



...,
optional(Ref)::or_else(Minimum, 0),
Sum1 is Sum0 + Minimum,
...


This is a cleaner solution compared with using an if-then-else construct:



...,
( tree_minimum(Tree, Minimum) ->
Sum1 is Sum0 + Minimum
; Sum1 is Sum0
),
...


It also allows you to use different defaults for different computations. For example:



...,
optional(Ref)::or_else(Minimum, 1),
Product1 is Product0 * Minimum,
...


More important, it doesn't mask that you're processing an empty tree in the same way that a default value would do. For example, the following code will only write the minimum values of non-empty trees:



print_tree_minimums(Refs) :-
meta::map(print_tree_minimum, Refs).

print_tree_minimum(Ref) :-
optional(Ref)::if_present(write).


or, using a lambda expression:



print_tree_minimums(Refs) :-
meta::map([Ref]>>(optional(Ref)::if_present(write)), Refs).


This answer is getting long and I don't want to transform it into a general discussion of the pros and cons of optionals and expecteds. But descriptions on both concepts and libraries is easy to find. E.g.



https://en.wikipedia.org/wiki/Option_type



https://youtu.be/NhcHwkUPX7w






share|improve this answer





















  • 1





    Brilliant answer, thank you so much Paulo Moura! So this is somehow similar to optional in Java if I read correctly? It still does not solve problem but better than "ooh you cannot".

    – user10631003
    Nov 22 '18 at 4:21











  • Thanks. Java optionals indeed provide similar functionality.

    – Paulo Moura
    Nov 22 '18 at 9:13











  • I cannot yet accept answer because I now understand that no one really understood question of mine which was not very good. Still you give excellent answer and I will try to make better question and try your approach you explain in this answer.

    – user10631003
    Nov 22 '18 at 16:33











  • @MunDong it would be better in such a circumstance to accept Paulo's answer and then ask a new question, if it would be different enough to require a different answer.

    – Daniel Lyons
    Nov 22 '18 at 21:20











  • @DanielLyons Thank you very much for your kind suggestion. But if the asnwer does not answer the question or if I do not yet know certainly if it answers the question because I have not yet tried out the code on my own than you can kindly wait until I know for certain, then I can accept the answer.

    – user10631003
    Nov 23 '18 at 7:06


















3














Just for fun, not really the answer you're looking for, taking the question title quite literally:



?- _ == _ .
false.


But dif/2 is not fouled (hint: each occurrence of the anonymous variable represents a different variable):



?- dif(_, _).
true.


Now, seriously. Starting with your tree minimum predicate example, there's a trivial alternative: the predicate can simply fail when the tree is empty. A better alternative may be to use optional or expected term libraries. The concepts behind these libraries are found in several programming languages, where they provide a better alternative to null. You have both libraries in Logtalk, which you can use with most Prolog systems. See:




  • https://logtalk.org/library/optional_0.html

  • https://logtalk.org/library/optional_1.html


and




  • https://logtalk.org/library/expected_0.html

  • https://logtalk.org/library/expected_1.html


You use one library or the other depending on your interpretation of "missing" meaning something that is optional (absence of a value is fine) or expected (absence of a value is an error). For example, assume that in your particular application it makes sense to use 0 as the minimum value of an empty tree when doing a specific computation (e.g. the sum of the minimums of a set of trees). If the tree minimum predicate returns an optional term reference, Ref, instead of an integer, you could do e.g.



...,
optional(Ref)::or_else(Minimum, 0),
Sum1 is Sum0 + Minimum,
...


This is a cleaner solution compared with using an if-then-else construct:



...,
( tree_minimum(Tree, Minimum) ->
Sum1 is Sum0 + Minimum
; Sum1 is Sum0
),
...


It also allows you to use different defaults for different computations. For example:



...,
optional(Ref)::or_else(Minimum, 1),
Product1 is Product0 * Minimum,
...


More important, it doesn't mask that you're processing an empty tree in the same way that a default value would do. For example, the following code will only write the minimum values of non-empty trees:



print_tree_minimums(Refs) :-
meta::map(print_tree_minimum, Refs).

print_tree_minimum(Ref) :-
optional(Ref)::if_present(write).


or, using a lambda expression:



print_tree_minimums(Refs) :-
meta::map([Ref]>>(optional(Ref)::if_present(write)), Refs).


This answer is getting long and I don't want to transform it into a general discussion of the pros and cons of optionals and expecteds. But descriptions on both concepts and libraries is easy to find. E.g.



https://en.wikipedia.org/wiki/Option_type



https://youtu.be/NhcHwkUPX7w






share|improve this answer





















  • 1





    Brilliant answer, thank you so much Paulo Moura! So this is somehow similar to optional in Java if I read correctly? It still does not solve problem but better than "ooh you cannot".

    – user10631003
    Nov 22 '18 at 4:21











  • Thanks. Java optionals indeed provide similar functionality.

    – Paulo Moura
    Nov 22 '18 at 9:13











  • I cannot yet accept answer because I now understand that no one really understood question of mine which was not very good. Still you give excellent answer and I will try to make better question and try your approach you explain in this answer.

    – user10631003
    Nov 22 '18 at 16:33











  • @MunDong it would be better in such a circumstance to accept Paulo's answer and then ask a new question, if it would be different enough to require a different answer.

    – Daniel Lyons
    Nov 22 '18 at 21:20











  • @DanielLyons Thank you very much for your kind suggestion. But if the asnwer does not answer the question or if I do not yet know certainly if it answers the question because I have not yet tried out the code on my own than you can kindly wait until I know for certain, then I can accept the answer.

    – user10631003
    Nov 23 '18 at 7:06
















3












3








3







Just for fun, not really the answer you're looking for, taking the question title quite literally:



?- _ == _ .
false.


But dif/2 is not fouled (hint: each occurrence of the anonymous variable represents a different variable):



?- dif(_, _).
true.


Now, seriously. Starting with your tree minimum predicate example, there's a trivial alternative: the predicate can simply fail when the tree is empty. A better alternative may be to use optional or expected term libraries. The concepts behind these libraries are found in several programming languages, where they provide a better alternative to null. You have both libraries in Logtalk, which you can use with most Prolog systems. See:




  • https://logtalk.org/library/optional_0.html

  • https://logtalk.org/library/optional_1.html


and




  • https://logtalk.org/library/expected_0.html

  • https://logtalk.org/library/expected_1.html


You use one library or the other depending on your interpretation of "missing" meaning something that is optional (absence of a value is fine) or expected (absence of a value is an error). For example, assume that in your particular application it makes sense to use 0 as the minimum value of an empty tree when doing a specific computation (e.g. the sum of the minimums of a set of trees). If the tree minimum predicate returns an optional term reference, Ref, instead of an integer, you could do e.g.



...,
optional(Ref)::or_else(Minimum, 0),
Sum1 is Sum0 + Minimum,
...


This is a cleaner solution compared with using an if-then-else construct:



...,
( tree_minimum(Tree, Minimum) ->
Sum1 is Sum0 + Minimum
; Sum1 is Sum0
),
...


It also allows you to use different defaults for different computations. For example:



...,
optional(Ref)::or_else(Minimum, 1),
Product1 is Product0 * Minimum,
...


More important, it doesn't mask that you're processing an empty tree in the same way that a default value would do. For example, the following code will only write the minimum values of non-empty trees:



print_tree_minimums(Refs) :-
meta::map(print_tree_minimum, Refs).

print_tree_minimum(Ref) :-
optional(Ref)::if_present(write).


or, using a lambda expression:



print_tree_minimums(Refs) :-
meta::map([Ref]>>(optional(Ref)::if_present(write)), Refs).


This answer is getting long and I don't want to transform it into a general discussion of the pros and cons of optionals and expecteds. But descriptions on both concepts and libraries is easy to find. E.g.



https://en.wikipedia.org/wiki/Option_type



https://youtu.be/NhcHwkUPX7w






share|improve this answer















Just for fun, not really the answer you're looking for, taking the question title quite literally:



?- _ == _ .
false.


But dif/2 is not fouled (hint: each occurrence of the anonymous variable represents a different variable):



?- dif(_, _).
true.


Now, seriously. Starting with your tree minimum predicate example, there's a trivial alternative: the predicate can simply fail when the tree is empty. A better alternative may be to use optional or expected term libraries. The concepts behind these libraries are found in several programming languages, where they provide a better alternative to null. You have both libraries in Logtalk, which you can use with most Prolog systems. See:




  • https://logtalk.org/library/optional_0.html

  • https://logtalk.org/library/optional_1.html


and




  • https://logtalk.org/library/expected_0.html

  • https://logtalk.org/library/expected_1.html


You use one library or the other depending on your interpretation of "missing" meaning something that is optional (absence of a value is fine) or expected (absence of a value is an error). For example, assume that in your particular application it makes sense to use 0 as the minimum value of an empty tree when doing a specific computation (e.g. the sum of the minimums of a set of trees). If the tree minimum predicate returns an optional term reference, Ref, instead of an integer, you could do e.g.



...,
optional(Ref)::or_else(Minimum, 0),
Sum1 is Sum0 + Minimum,
...


This is a cleaner solution compared with using an if-then-else construct:



...,
( tree_minimum(Tree, Minimum) ->
Sum1 is Sum0 + Minimum
; Sum1 is Sum0
),
...


It also allows you to use different defaults for different computations. For example:



...,
optional(Ref)::or_else(Minimum, 1),
Product1 is Product0 * Minimum,
...


More important, it doesn't mask that you're processing an empty tree in the same way that a default value would do. For example, the following code will only write the minimum values of non-empty trees:



print_tree_minimums(Refs) :-
meta::map(print_tree_minimum, Refs).

print_tree_minimum(Ref) :-
optional(Ref)::if_present(write).


or, using a lambda expression:



print_tree_minimums(Refs) :-
meta::map([Ref]>>(optional(Ref)::if_present(write)), Refs).


This answer is getting long and I don't want to transform it into a general discussion of the pros and cons of optionals and expecteds. But descriptions on both concepts and libraries is easy to find. E.g.



https://en.wikipedia.org/wiki/Option_type



https://youtu.be/NhcHwkUPX7w







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 18:15

























answered Nov 21 '18 at 10:35









Paulo MouraPaulo Moura

11.7k21325




11.7k21325








  • 1





    Brilliant answer, thank you so much Paulo Moura! So this is somehow similar to optional in Java if I read correctly? It still does not solve problem but better than "ooh you cannot".

    – user10631003
    Nov 22 '18 at 4:21











  • Thanks. Java optionals indeed provide similar functionality.

    – Paulo Moura
    Nov 22 '18 at 9:13











  • I cannot yet accept answer because I now understand that no one really understood question of mine which was not very good. Still you give excellent answer and I will try to make better question and try your approach you explain in this answer.

    – user10631003
    Nov 22 '18 at 16:33











  • @MunDong it would be better in such a circumstance to accept Paulo's answer and then ask a new question, if it would be different enough to require a different answer.

    – Daniel Lyons
    Nov 22 '18 at 21:20











  • @DanielLyons Thank you very much for your kind suggestion. But if the asnwer does not answer the question or if I do not yet know certainly if it answers the question because I have not yet tried out the code on my own than you can kindly wait until I know for certain, then I can accept the answer.

    – user10631003
    Nov 23 '18 at 7:06
















  • 1





    Brilliant answer, thank you so much Paulo Moura! So this is somehow similar to optional in Java if I read correctly? It still does not solve problem but better than "ooh you cannot".

    – user10631003
    Nov 22 '18 at 4:21











  • Thanks. Java optionals indeed provide similar functionality.

    – Paulo Moura
    Nov 22 '18 at 9:13











  • I cannot yet accept answer because I now understand that no one really understood question of mine which was not very good. Still you give excellent answer and I will try to make better question and try your approach you explain in this answer.

    – user10631003
    Nov 22 '18 at 16:33











  • @MunDong it would be better in such a circumstance to accept Paulo's answer and then ask a new question, if it would be different enough to require a different answer.

    – Daniel Lyons
    Nov 22 '18 at 21:20











  • @DanielLyons Thank you very much for your kind suggestion. But if the asnwer does not answer the question or if I do not yet know certainly if it answers the question because I have not yet tried out the code on my own than you can kindly wait until I know for certain, then I can accept the answer.

    – user10631003
    Nov 23 '18 at 7:06










1




1





Brilliant answer, thank you so much Paulo Moura! So this is somehow similar to optional in Java if I read correctly? It still does not solve problem but better than "ooh you cannot".

– user10631003
Nov 22 '18 at 4:21





Brilliant answer, thank you so much Paulo Moura! So this is somehow similar to optional in Java if I read correctly? It still does not solve problem but better than "ooh you cannot".

– user10631003
Nov 22 '18 at 4:21













Thanks. Java optionals indeed provide similar functionality.

– Paulo Moura
Nov 22 '18 at 9:13





Thanks. Java optionals indeed provide similar functionality.

– Paulo Moura
Nov 22 '18 at 9:13













I cannot yet accept answer because I now understand that no one really understood question of mine which was not very good. Still you give excellent answer and I will try to make better question and try your approach you explain in this answer.

– user10631003
Nov 22 '18 at 16:33





I cannot yet accept answer because I now understand that no one really understood question of mine which was not very good. Still you give excellent answer and I will try to make better question and try your approach you explain in this answer.

– user10631003
Nov 22 '18 at 16:33













@MunDong it would be better in such a circumstance to accept Paulo's answer and then ask a new question, if it would be different enough to require a different answer.

– Daniel Lyons
Nov 22 '18 at 21:20





@MunDong it would be better in such a circumstance to accept Paulo's answer and then ask a new question, if it would be different enough to require a different answer.

– Daniel Lyons
Nov 22 '18 at 21:20













@DanielLyons Thank you very much for your kind suggestion. But if the asnwer does not answer the question or if I do not yet know certainly if it answers the question because I have not yet tried out the code on my own than you can kindly wait until I know for certain, then I can accept the answer.

– user10631003
Nov 23 '18 at 7:06







@DanielLyons Thank you very much for your kind suggestion. But if the asnwer does not answer the question or if I do not yet know certainly if it answers the question because I have not yet tried out the code on my own than you can kindly wait until I know for certain, then I can accept the answer.

– user10631003
Nov 23 '18 at 7:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405266%2fwhat-is-never-equal-to-itself%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

WPF add header to Image with URL pettitions [duplicate]