Variable is not completely bound

Discussions related to Visual Prolog
Kofds
Posts: 2
Joined: 2 Mar 2019 18:03

Variable is not completely bound

Unread post by Kofds » 2 Mar 2019 18:34

I have some issues with a program. This code should delete lines with the same length from file and keep only the first one.
Example:
  • Dallas
  • Boston
  • Houston
  • Chicago
to
  • Dallas
  • Houston

Code: Select all

implement main     open core, console, string   class predicates     write_list : (outputStream, string*).     read_list : (inputStream, string*) anyflow.     pr2 : (integer, string*, string*) nondeterm anyflow.     pr1 : (string*, string*) nondeterm anyflow.     clauses     read_list(In, []) :-         In:endOfStream(),         In:close(),         !.     read_list(In, [H | T]) :-         H = In:readLine(),         read_list(In, T).       write_list(_, []).     write_list(Out, [H | Tail]) :-         Out:write(H, "\n"),         write_list(Out, Tail).       pr2(X, L2, L1) :-         N = X,         L1 = list::filter(L2, { (E) :- not(N = string::length(E)) })         or         fail.       pr1([H | T], L2) :- error c609 : Variable 'L2' is not completely bound         L3 = L2, %error c609 : Variable 'L3' is not completely bound         L4 = list::append(L3, [H]),         N = length(H),         !,         pr2(N, T, L1),         pr1(L1, L4)         or         fail.     pr1([], []) :-         !.       run() :-         init(),         In = inputStream_file::openFile8("InFile.txt"),         read_list(In, L),         pr1(L, L2),         write(L2),         nl,         Out = outputStream_file::create8("OutFile.txt"),         write_list(Out, L2),         _ = readLine(),         !         or         console::write("Fail"),         _ = readLine().   end implement main   goal     mainExe::run(main::run).

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

Re: Variable is not completely bound

Unread post by Thomas Linder Puls » 3 Mar 2019 16:39

Your code contains "<something> or fail", but that does not really make any sense.

Your should eliminate they use of anyflow, and consider the actual flows in your code.

Here is pr1 after I have removed "_ or fail" and two superfluous cuts. And after I have eliminated anyflow and replaced it with the actual flow you use in your program:

Code: Select all

class predicates     pr1 : (string*, string* [out]) nondeterm. clauses     pr1([H | T], L2) :- %error c609 : Variable 'L2' is not completely bound         L3 = L2, %error c609 : Variable 'L3' is not completely bound         L4 = list::append(L3, [H]),         N = length(H),         pr2(N, T, L1),         pr1(L1, L4).     pr1([], []).
The important thing is that the second argument is an output argument, but you make operations that requires an input (=bound value) argument.

Furthermore, you should also use "nondeterm" correctly. As I understand your intensions pr1 is supposed to take the list of all lines in the file as first argument and return the list of the first lines of a certain length. That problem has one and only one solution, so the predicate should be a procedure.

I suggest you go through the "Lessons".
Regards Thomas Linder Puls
PDC

Kofds
Posts: 2
Joined: 2 Mar 2019 18:03

Re: Variable is not completely bound

Unread post by Kofds » 3 Mar 2019 18:43

Thank you very much! I was desperate, but I finally made it!

The final code:

Code: Select all

implement main     open core, console, string   class predicates     write_list : (outputStream, string*).     read_list : (inputStream, string*) anyflow.     pr2 : (integer, string*, string* [out]) nondeterm.     pr1 : (string*, string* [out]) nondeterm.   clauses     read_list(In, []) :-         In:endOfStream(),         In:close(),         !.     read_list(In, [H | T]) :-         H = In:readLine(),         read_list(In, T).       write_list(_, []).     write_list(Out, [H | Tail]) :-         Out:write(H, "\n"),         write_list(Out, Tail).       pr2(X, L2, L1) :-         N = X,         L1 = list::filter(L2, { (E) :- not(N = string::length(E)) })         or         fail.     pr2(_, [], []) :-         !.       pr1([H | T], L2) :-         N = length(H),         pr2(N, T, L1),         pr1(L1, L3),         L2 = list::append([H], L3).     pr1([], []).       run() :-         init(),         In = inputStream_file::openFile8("InFile.txt"),         read_list(In, L),         pr1(L, L2),         write(L2),         nl,         Out = outputStream_file::create8("OutFile.txt"),         write_list(Out, L2),         _ = readLine(),         !         or         console::write("Fail"),           _ = readLine().   end implement main   goal     mainExe::run(main::run).

Post Reply