Thank you.
But, i am still confused, when trying to parse (with my brain) the explanation.
Perhaps " This implies all output unification must be done after the head unification.” – is key here – in the paragraph i mentioned above:
Edit: below paragraph is probably incorrect – so what and how exactly does subsume check
I guess, this is achieved by having Pattern built out of An independent variables – without any variables bound to each other, and having subsumes_term(Pattern ,Args) occur before, the Guard, and the assumption (understanding) that this requires Vn’s to be non-bound to each other, at this stage also, to succeed.
end of edit
(If this is the case, perhaps best if this could be added in one sentence in the documentation, since for me this didn’t jump out to me immediately, making the example “expansion” as an explanatory device obscure to me).
So, (party of the) semantics of => implicitly forces unification to occur in the body – although it may also occur in the Guard.
Now – why exactly is deferred unification so important, when creating a deterministic predicate?
There is the classic example presented in the documentation: max/3 – where delayed unification enables correct behavior – and a solution that build on unification of head, leads to an incorrect one-- but, what is the underlying and general reason – that delayed unification is necessary for deterministic predicates to achieve correctness.
Can an explanation be added – instead of citing just one example – what is the principle here?
Edit: i guess its somehow related to pattern matching vs. unification in the head – with pattern matching a weaker form of unification – perhaps a definition for pattern matching in the sense here would be very useful to have.
thanks,
Dan
p.s. I am not getting far when I read the paragraph:
Clause Head – seems to refers to p(V1,V2,…,Vn) or to Head or both?
** goal term ** – seems to refer to Body or – perhaps to a caller goal term, not shown here – perhaps
p(V1,V2,…,Vn) is a called term that is more general than the goal term – not shown but implied?
It is implied that Head, Guard and Body include (some of the) variables V1, … Vn.
Head, Guard => Body.
“This is semantically equivalent to the Prolog clause below. The subsumes_term/2 guarantees the clause head is more generic than the goal term and thus unifying the two does not affect any of the arguments of the goal. This implies all output unification must be done after the head unification.”
p(V1,V2,…,Vn) :-
- Pattern = p(A1,A2,…,An),*
- Args = p(V1,V2,…,Vn),*
- subsumes_term(Pattern, Args),*
- Pattern = Args,*
- Guard,*
- !,*
- Body.*
p.s.
Btw, the phrase Output Unification is also at first not fully clear – because – cognitively (and didactically), there are in my mind several jump here – here is what needs to be understood to understand this phrase:
- The head of a Prolog clause can have arguments that are variables
- Those variables can be treated by the caller as input – when bound by the caller – or output, when not bound by the caller
- when they are unbound by the caller, and hence treated as output variables, the expectation is that in head or the body of the prolog clause they will be unified (and hence bound).
- if the above occurs, then there will be an Output Unification occurring somewhere.
Taking all the above together, for the => semantics Output Unification must occur at or after the Guard.
Edit:
Note the term Output Unification doesn’t appear anywhere else in the Prolog documentation, so this is the first time this term is used (at least per the search) – a google search leads to a a hit within a book on programming language theory, with a foot note with a definition:
“In contrast to input matching one speaks of output unification to refer to a unification of two terms which appear in an output mode argument (of a literal w.r.t a given program). This … stresses that for the unification of terms occurring in output mode arguments there is no restriction of the direction of the binding (from goal to clause head (output) or vice-versa (input)”
So, this leaves the reader to make an (educated) guess what is meant here.
Edit: with all the above – perhaps I now start to understand why => is called single sided unification – since for correctness, unification is not allowed to occur in the head, but must occur in the Guard or Body of the overall rules clause.