how do I increase the stack limit

Discussions related to Visual Prolog
User avatar
Thomas Linder Puls
VIP Member
Posts: 1168
Joined: 28 Feb 2000 0:01

Re: how do I increase the stack limit

Unread post by Thomas Linder Puls » 15 Dec 2017 10:24

There are many reoccurring lines:
main.pro(26072)
main.pro(32280)
main.pro(27268)
main.pro(30882)
main.pro(32295)
main.pro(22555)
main.pro(29139)
main.pro(30935)
main.pro(32295)
main.pro(27242)
main.pro(30882)
main.pro(32295)
main.pro(26832)
main.pro(30844)
main.pro(32295)
main.pro(27485)
main.pro(30882)
main.pro(32295)
main.pro(22555)
main.pro(28187)
main.pro(30912)
main.pro(33476)
main.pro(27074)
main.pro(30864)
main.pro(33476)
main.pro(27409)
main.pro(30882)
main.pro(32295)
main.pro(26588)
main.pro(30821)
main.pro(32095)
main.pro(33380)
main.pro(29968)
main.pro(31015)
main.pro(32295)
main.pro(26832)
main.pro(30844)
main.pro(32295)
main.pro(26832)
main.pro(30844)
main.pro(32295)
main.pro(26832)
main.pro(30844)
main.pro(32295)
main.pro(26832)
So for you should consider what happens in line 32295?

Why is it not a tail call? Is it a call to a nondeterm predicate followed by a cut? If so, should the predicate that is called really be nondeterm, or should it perhaps be determ?

There are also certain "standard tricks" to turn non tail calls into tail calls, for example to use accumulator arguments.

Consider calculating the sum of the numbers in an integer list, first using the straightforward way:

Code: Select all

class predicates     sumList : (integer* List) -> integer Sum. clauses     sumList([]) = 0.     sumList([H | T]) = H + sumList(T).
This predicate works fine but it cannot be tail call optimized, because the addition is made after the recursive call.

However (since addition is commutative) we can write an auxiliary predicate that uses an accumulator:

Code: Select all

class predicates     sumList : (integer* List) -> integer Sum. clauses     sumList(L) = sumList2(L, 0).   class predicates     sumList2 : (integer* List, integer Acc) -> integer Sum. clauses     sumList2([], Acc) = Acc.     sumList2([H | T], Acc) = sumList(T, H + Acc).
This predicate is tail call optimized.

But even when you have considered all such "local" things you may have to reconsider the overall structure of the algorithm. Is it really a necessary/good/correct that the algorithm keeps making these recursive calls? Couls/should it be structured in another way?
Regards Thomas Linder Puls
PDC

mdosnov2016
VIP Member
Posts: 73
Joined: 19 Nov 2016 7:58

Re: how do I increase the stack limit

Unread post by mdosnov2016 » 15 Dec 2017 16:11

Do the last-in-the-clause, cuts (!), cause the stack to increase?
PrologUser

mdosnov2016
VIP Member
Posts: 73
Joined: 19 Nov 2016 7:58

Re: how do I increase the stack limit

Unread post by mdosnov2016 » 15 Dec 2017 16:16

a lot of these lines, are ! cuts at the end of regular (non-recursive) clauses.
Shall I remove them?
PrologUser

User avatar
Thomas Linder Puls
VIP Member
Posts: 1168
Joined: 28 Feb 2000 0:01

Re: how do I increase the stack limit

Unread post by Thomas Linder Puls » 18 Dec 2017 14:33

If the last thing is a cut then no tail call optimization will be performed, because the cut is the "tail call".

Tail call optimization is not tied to recursion, any call that is the last thing that takes place in a clause is tail call. It will be subject of tail call optimization if certain other requirements are met (there cannot be any backtrack points in the current clause, the called predicate must have prolog calling convention, ...).

A general advise it only to have the cuts that are necessary and to place them in the correct place.
Regards Thomas Linder Puls
PDC

mdosnov2016
VIP Member
Posts: 73
Joined: 19 Nov 2016 7:58

Re: how do I increase the stack limit

Unread post by mdosnov2016 » 20 Dec 2017 18:08

Dear Thomas,
I introduced the cut as you can see after the facts matching calls.
Now I have the following problem,
-----------------------------------
generate_top() :-
not(file5x::existfile(itf_fact_dba)),
itf_not_found_message.

generate_top() :-
%file5x::openfile(fileSelector::screen),
%thread::start(go,maxStack),
file5x::writedevice(fileSelector::screen),
file5x::write("HLS CCC optimization started, please wait..."),
%outputStream::write(" backend HLS CCC optimization started, please wait..."),
file5x::existfile(itf_fact_dba),
%file5x::closefile(fileSelector::screen),
assertz(consecutive_106("false")),
assert_global_constraint_conditionally0,
check_for_program_name,
itf_found_message,
report_global_constraint,
%showBadTerms(itf_fact_dba),
file::consult(itf_fact_dba, backend_dbase),
hdl_style(Hdlform),
!,
extract_loops_from_all_modules_wrapper(2),
% don't process the ADA package
generate_hdl_recursive_wrapper(Hdlform, "synergy", 1),
file5x::writedevice(fileSelector::screen),
file5x::nl,
file5x::write(" CCC Backend synthesis completed! "),
end_time_message,
%openfile(stdout),
file5x::writedevice(fileSelector::screen),
file5x::write(" optimization completed, check HDL results!"),
%file5x::closefile(fileSelector::screen),
!
or
console::write("Main run failed!").
/*
run() :-
% console::init(),
% taskWindow::create(),
generate_top
,
!
or
console::write("Main run failed!")
.
*/
/*
run() :-
succeed. % place your own code here
*/

end implement main

goal

%console::runUtf8(main::run),
%console::runUtf8(backend::run),
console::runUtf8(main::generate_top),
!
or
console::write("Error").
-----------------------------------
generates the following error which I don't know how to handle:

Type Action Description Filename Path
e631 The predicate 'main::generate_top/0', which is declared as 'procedure', is actually 'determ' main.pro

what would be the correct syntax for my top-level call (inside goal) if the called predicate is a determ?
PrologUser

mdosnov2016
VIP Member
Posts: 73
Joined: 19 Nov 2016 7:58

Re: how do I increase the stack limit

Unread post by mdosnov2016 » 21 Dec 2017 7:30

I meant, that I know how to declare a predicate (generate_top) to be a determ,
the problem is that then the expression:

console::runUtf8(main::generate_top),

doesn't work or it produces syntax errors.
PrologUser

User avatar
Thomas Linder Puls
VIP Member
Posts: 1168
Joined: 28 Feb 2000 0:01

Re: how do I increase the stack limit

Unread post by Thomas Linder Puls » 21 Dec 2017 13:36

Please surround your code by [code]...[/code] tags, and please remove strange/superfluous comments that does not aid in the question. Clearly state what error you get and in which line you get it. And please start new topics when you have a new problem. You may consider this "next step" in the what originally was an "increase stack" problem, but clearly this question is about determinism, cuts and backtracking.
Regards Thomas Linder Puls
PDC

loveProlog
Posts: 8
Joined: 9 Jul 2014 12:53

Re:

Unread post by loveProlog » 28 May 2020 14:50

mdosnov2016 wrote:
13 Nov 2017 13:28
This is the message that I get when it crashes.

C:\let\ccc\vp8ccc\backend\Exe64>backend
HLS CCC optimization started, please wait...
===== 2017-11-13 15:28:11 =======================


----------------------------------------
Non prolog exception

TraceId = 00007FF9B3165DA4
TraceId_value = 140710428171684
nonPrologException (exception) in exception::getDescriptor_nd

----------------------------------------
OS: Windows 10.0 Build 15063
I am getting the "Non prolog exception". What was your root cause? How did you fix it?

Post Reply