I have a case where multifile predicates’ “fallback” clauses are defined before other, more specific ones. In a nutshell, imagine a system where we predefined some predicates that work for default cases, but we need to be able to override them with dynamically loaded files.
I was at first thinking about some voodoo-level tinkering of being able to reverse the order of specified predicates, but then I came up with a slightly more explicit solution, and then I ended up with this extremely unportable answer:
However, it has a serious drawback in that it disregards the fact that alternatives may not succeed due to their bodies, and therefore the predicate won’t succeed through this default clause in this case.
What’s a better solution to this (and perhaps something that doesn’t require a lot of tedious wrapping of every case where I need these defaults)?
Edited: I suppose it is better to leave it non-deterministic and try to find the “last” (more authoritative in my case) clause to use.
I know the construct if-then-else in Prolog (… → … ; …), but I didn’t know the existence of this variety with the asterisk: (… *-> … ; …)
What’s the difference? Thanx
I am now figuring out if I can generalize it to any predicate. Barring that, maybe I can write a meta-predicate to apply to these “setting” predicates.
In my case, the predicates with defaults most often have an arity different from 1 and may be of different arity. I have many such predicates and prefer not to have a different default predicate.
In a nutshell, imagine having a predicate object_property1(Obj, Prop), object_property2(Obj, Prop, Extra), etc. If I were to have, say, default(object_property1(Obj, Prop)) and default(object_property2(Obj, Prop, Extra)), I’d have a similar (but simpler) implementation of get but wouldn’t be able to see object_property1 solution provided by default if I did want to list all solutions.
But I do get your point. If we completely separate defaults from later-provided-values and don’t need to be able to list all solutions, including the default, the above may be a more efficient implementation.
I guess I need to figure out if I need to be able to list defaults. And if I do, maybe I need another predicate list_all that lists defaults and regular predicate values.