a) Can you make an example of a runtime check of some sort for choice points.
b) Which is a runtime check, that cannot be eliminated by a more clever compilation?
I have an example of such a runtime check, but it can be eliminated by a more clever compilation. Take the elevator example, the second clause of elevator/3:
elevator(N, X, Y) :-
M is N-1,
H is X+1,
elevator(M, H, Y).
And this are the instructions for this clause:
----------------------------------------
clause 2 (<clause>(00000000067BC950)):
----------------------------------------
0 i_enter
1 a_add_fc(3,0,-1)
5 a_add_fc(4,1,1)
9 l_nolco('L1')
11 l_var(0,3)
14 l_var(1,4)
17 i_tcall
L1: 18 b_var(3)
20 b_var(4)
22 b_var2
23 i_depart(user:elevator/3)
25 i_exit
The l_nolco
instruction, which does poll choice points can be eliminated by the following reasoning. Also what follows after label L1
can be removed. I am assuming that delayed goals are woken up at the call port of a goal:
-
The fresh argument variables
N
,X
,Y
will not instantiate an attributed variable,
therefore no delayed goals will be woken up beforeM is N-1
. -
The fresh variable
M
in the evaluationM is N-1
will not instantiate an attributed variable,
therefore no delayed goals will be woken up beforeH is X+1
. -
The fresh variable
H
in the evaluationH is X+1
will not instantiate an attributed variable,
therefore no delayed goals will be woken up beforeelevator(M, H, Y)
. -
In summary no goals were woken up before
elevator(M, H, Y)
therefore also
no new choice points will be found atelevator(M, H, Y)
. -
Because there are no new choice points at
elevator(M, H, Y)
the instruction
l_nolco('L1')
will never jump toL1
.
The above reasoning does not need to know whether something is a last clause. It solely works its way around the problem through freshness analysis of variables.
Why do such a thing?
-
It could speed up the Prolog VM execution, if switch is used
to implement the Prolog VM loop. -
It could also speed up the Prolog VM execution, if function array is
used to implement the Prolog VM loop. One less instruction, one
less function array roundtrip.