Prolog: How to tell if a predicate is deterministic or not
Asked Answered
S

3

10

So from what I understand about deterministic predicates:

Deterministic predicate = 1 solution

Non-deterministic predicate = multiple solutions

Are there any type of rules as to how you can detect if the predicate is one or the other? Like looking at the search tree, etc.

Salic answered 19/10, 2014 at 11:45 Comment(2)
It's non-deterministicDeictic
Of interest: Programming language - Mercury Determinism categoriesDowie
D
14

There is no clear, generally accepted consensus about these notions. However, they are usually based rather on the observed answers and not based on the number of solutions. In certain contexts the notions are very implementation related. Non-determinate may mean: leaves a choice point open. And sometimes determinate means: never even creates a choice point.

Answers vs. solutions

To see the difference, consider the goal length(L, 1). How many solutions does it have? L = [a] is one, L = [23] another... but all of these solutions are compactly represented with a single answer substitution: L = [_] which thus contains infinitely many solutions. In any case, in all implementations I know of, length(L, 1) is a determinate goal.

Now consider the goal repeat which has exactly one solution, but infinitely many answers. This goal is considered non-determinate.

In case you are interested in constraints, things become even more evolved. In library(clpfd), the goal X #> Y, Y #> X has no solution, but still one answer. Combine this with repeat: infinitely many answers and no solution.

Further, the goal append(Xs, Ys, []) has exactly one solution and also exactly one answer, nevertheless it is considered non-determinate in many implementations, since in those implementations it leaves a choice point open.

In an ideal implementation, there would be no answers without solutions (except false), and there would be non-determinism only when there is more than one answer. But then, all of this is mostly undecidable in the general case.

So, whenever you are using these notions make sure on what level things are meant. Rather explicitly say: multiple answers, multiple solutions, leaves no (unnecessary) choice point open.

Deictic answered 19/10, 2014 at 12:27 Comment(0)
E
1

You need understand the difference between det, semidet and undet, it is more than just number of solutions.

Because there is no loop control operator in Prolog, looping (not recursion) is constructed as a 'sequence generating' predicate (undet) followed by the loop body. Also you can store solutions with some of findall-group predicates as a list and loop later with the member/2 predicate.

So, any piece of your program is either part of loop construction or part of usual flow. So, there is a difference in designing det and undet predicates almost in the intended usage. If you can work with a sequence you always do undet and comment it as so. There is a nice unit-test extension in swi-prolog which can check wheter your predicate always the same in mean of det/semidet/undet (semidet is for usage the same way as undet but as a head of 'if' construction).

So, the difference is pre-design, and this question should not be arised with already existing predicates. It is a good practice always comment the intended usage in a comment like.

% member(?El, ?List) is undet.
Enchorial answered 20/10, 2014 at 9:4 Comment(0)
C
-3

Deterministic: Always succeeds with a single answer that is always the same for the same input. Think a of a static list of three items, and you tell your function to return value one. You will get the same answer every time. Additionally, arithmetic functions. 1 + 1 = 2. X + Y = Z.

Semi-deterministic: Succeeds with a single answer that is always the same for the same input, but it can fail. Think of a function that takes a list of numbers, and you ask your function if some number exists in the list. It either does, or it doesn't, based on the contents of the list given and the number asked.

Non-deterministic: Succeeds with a single answer, but can exhibit different behaviors on different runs, even for the same input. Think any kind of math.random(min,max) function like random/3

In essence, this is entirely separate from the concept of choice points, as choice points are a function of Prolog. Where I think the Prolog confusion of these terms comes from is that Prolog can find a single answer, then go back and try for another solution, and you have to use the cut operator ! to tell it that you want to discard your choice points explicitly.

This is very useful to know when working with Prolog Unit Testing

Chiekochien answered 29/3, 2018 at 15:20 Comment(1)
Bad use of non-deterministic in the context of Prolog! The example you give (a real RNG) is considered extra-logical.Corrasion

© 2022 - 2024 — McMap. All rights reserved.