-
Three examples I have stumbled into back to back while researching I/O methods for Scryer Prolog. In each example, the most general query terminates continues to generate but doesn't infinitely backtrack with no output, and supplying the first argument terminates, but in all 3 cases, specifying the 2nd argument while keeping the first argument general immediately gets the correct result but then (upon The only thing I can think of is that the semicontext is pushing something onto the front of the list, which is causing the "empty list" termination rule to never happen. But that's purely speculation on my part, I haven't been able to conclusively come up with a reason. The following examples borrow heavily from @triska 's work on dcgs. numthings(Things, Num) :-
phrase(numthings_(nonterminal(Things)), [0], [Num]).
numthings_(nonterminal([])) --> [].
numthings_(terminal(X)), [N] --> [N0], { N #= N0 + 1 }.
numthings_(nonterminal([H|T])) -->
numthings_(terminal(H)),
numthings_(nonterminal(T)).
?- numthings(Things, N).
%@ Things = [], N = 0
%@ ; Things = [_A], N = 1
%@ ; Things = [_A,_B], N = 2
%@ ; Things = [_A,_B,_C], N = 3
%@ ; Things = [_A,_B,_C,_D], N = 4
%@ ; ... .
?- numthings("hey", N).
%@ N = 3
%@ ; false.
?- numthings(Things, 5).
%@ Things = [_A,_B,_C,_D,_E]
%@ ; y u no terminate?? strings2output([]) --> [].
strings2output(row(Row)), [Output] --> [Output0], { phrase(format_("~s\n~s", [Output0, Row]), Output) }.
strings2output([Row|Rows]) -->
strings2output(row(Row)),
strings2output(Rows).
strings2lines(Strings, Output) :-
phrase(strings2output(Strings), [""], [Output]).
?- strings2lines(["hey", "there", "sailor"], Output).
%@ Output = "\nhey\nthere\nsailor".
?- strings2lines(Input, Output).
%@ Input = [], Output = []
%@ ; Input = row([]), Output = "\n"
%@ ; Input = row([_A]), Output = ['\n',_A]
%@ ; Input = row([_A,_B]), Output = ['\n',_A,_B]
%@ ; Input = row([_A,_B,_C]), Output = ['\n',_A,_B,_C]
%@ ; ... .
?- strings2lines(Input, "\nhey\nthere\nsailor").
%@ Input = row("hey\nthere\nsailor")
%@ ; y u no terminate? num_leaves(Tree, N) :-
phrase(num_leaves_(Tree), [0], [N]).
num_leaves_(nil), [N] --> [N0], { N #= N0 + 1 }.
num_leaves_(node(_,Left,Right)) -->
num_leaves_(Left),
num_leaves_(Right).
?- num_leaves(node(a,node(b,nil,nil),
node(c,nil,
node(d,nil,nil))), N).
%@ N = 5.
?- num_leaves(T, N).
%@ T = nil, N = 1
%@ ; T = node(_A,nil,nil), N = 2
%@ ; T = node(_A,nil,node(_B,nil,nil)), N = 3
%@ ; T = node(_A,nil,node(_B,nil,node(_C,nil,nil))), N = 4
%@ ; T = node(_A,nil,node(_B,nil,node(_C,nil,node(_D,nil,nil)))), N = 5
%@ ; ... .
?- num_leaves(T, 3).
%@ T = node(_A,nil,node(_B,nil,nil))
%@ ; y u no terminate? I can't figure out why they don't terminate! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
This is very strange construction |
Beta Was this translation helpful? Give feedback.
This variant of your first example terminates:
UPDT: I pasted wrong chunk of text