Replies: 5 comments 6 replies
-
Did you have a look at In your code there is some implicit modedness I am quite unsure about. Some
|
Beta Was this translation helpful? Give feedback.
-
I believe that indeed state0_decision_noregrets2(S0, D, NoRegrets) :-
#S0 #> 0,
member(D, [ahead, back]),
call(
( state0_decision_state_regretted(S0, D, S)
; nope
),
Regretted),
reif:non(Regretted, NoRegrets).
state0_decision_state_regretted(S0, D, S, Regretted) :-
state0_decision_state(S0, D, S),
regret(S, Regretted).
regret(S, true) :- regret(S).
regret(S, false) :- \+ regret(S). % Negation of is_prime/1 : ℤ -> {true,false} seems safe.
nope(false). I will now have to try translating this back to the motivating application... |
Beta Was this translation helpful? Give feedback.
-
Why don't you test this to be sure? With e.g. |
Beta Was this translation helpful? Give feedback.
-
Here's a new version of selected parts of the above code: % 'Laws of motion' are that we are constrained to walk in the
% positive integers. After we have committed to the direction
% of our step, the size of our stride is 'chosen for us' to be
% either 1 or 2 steps.
state0_direction_stride_state(S0, ahead, Stride, S) :-
#S0 #> 0,
Stride in 1..2,
#S #= #S0 + Stride.
state0_direction_stride_state(S0, back, Stride, S) :-
Stride in 1..2,
#S #= #S0 - Stride,
#S #> 0.
% Note the subtle collapse of "direction_stride" to "decision",
% implying that the 'decision' now means choosing a *direction*
% on the understanding that the *stride* is nondeterministic.
% (It's also interesting to me that nondeterminism corresponds
% to the don't-care variable '_'.)
state0_decision_state(S0, D, S) :-
state0_direction_stride_state(S0, D, _, S).
% Walking according to these rules of motion, we have the further
% consideration that we would regret/1 stepping on a prime number.
regret(S) :- is_prime(S).
state0_decision_noregrets(S0, D, Truth) :-
#S0 #> 0,
member(D, [ahead, back]),
( state0_decision_state(S0, D, S),
regret(S) -> Truth = false % a single witness suffices
; Truth = true
).
%% What would this look like, if I reworked it along lines of `treememberd_t/3`?
state0_decision_noregrets3(S0, D, NoRegrets) :-
#S0 #> 0,
member(D, [ahead, back]),
call(
( state0_direction_stride_state_regretted(S0, D, 1, _) % NB: Here it's crucial to avoid
; state0_direction_stride_state_regretted(S0, D, 2, _) % repeating a singleton var S!
; nope
),
Regretted),
reif:non(Regretted, NoRegrets).
state0_direction_stride_state_regretted(S0, D, Stride, S, Regretted) :-
state0_direction_stride_state(S0, D, Stride, S),
regret(S, Regretted).
regret(S, true) :- indomain(S), regret(S).
regret(S, false) :- indomain(S), \+ regret(S).
nope(false).
%?- state0_decision_noregrets(27, D, NoRegrets).
%@ D = ahead, NoRegrets = false
%@ ; D = back, NoRegrets = true
%@ ; false.
%?- state0_decision_noregrets3(27, D, NoRegrets).
%@ D = ahead, NoRegrets = false
%@ ; D = back, NoRegrets = true
%@ ; false. In treememberd_t(_, nil, false).
treememberd_t(E, t(F,L,R), T) :-
call(
( E = F
; treememberd_t(E, L)
; treememberd_t(E, R)
),
T). In my toy example, there are just 2 alternatives: Admittedly, it's quite likely that valid expressions of |
Beta Was this translation helpful? Give feedback.
-
Just a general remark. Prolog's original pure base is first order logic only. Extensions to something higher-order took some time. In fact, higher-order programing via |
Beta Was this translation helpful? Give feedback.
-
In our collaborations toward executable specifications for dose-escalation trial designs, @triska and I have encountered a situation that may indicate the need for a new, pure construct for Prolog. (Compare Markus's Preparing Prolog video.) In this post, I'll try to strip away distracting detail specific to oncology dose-finding methods, while retaining enough of the original problem to motivate the code and exhibit our difficulty.
Let's suppose you are constrained to walk along the positive integers, and you do this in an alternating 2-player 'game' where your turn involves deciding whether to move
ahead
orback
, and your nondeterministic opponent decides whether your stride will be 1 or 2.As you make your decision, consider that you will
regret/1
stepping on a prime number:The predicate
state0_decision_noregrets/3
reifies our evaluation of decisionD
made while standing on integerS0
:Here is where the need lies. It is of the essence of 'regret', that regretting any single possible outcome of a decision causes us to avoid the decision altogether. ("A single witness suffices," one would say.) There is no need to catalogue all possible ways we might regret a decision; indeed if our program were to do so not only would it be inefficient but also (worse!) it would misrepresent essential logic of our problem.
But clearly the
(->)/2
used above is an impure construct. Using it safely requires that we ensureS0
,D
andS
are all ground. It would be so much nicer to have a pure construct that allowed us to 'not care' about multiple solutions forS
while still allowingS0
andD
to be variables over which backtracking would occur with no lost solutions. Is there already a construct or idiom that would achieve this?To complete the code, here is a DCG describing the paths that may occur in this game. It implements one final bit of core problem logic:
ahead
is preferred toback
:Beta Was this translation helpful? Give feedback.
All reactions