Using SWI-Prolog Interactively - Output Taken off - prolog

I'm using SWI-Prolog interactively. When I run my query, I get a prefix of the output and the rest is taken off (marked using the string ...|...). Is this normal or should I go back and fix my program?

The number of items shown is controlled by a prolog flag.
You can remove it by issuing
remove_max_depth:-
current_prolog_flag(toplevel_print_options,Options),
select(max_depth(_), Options, NOptions)->
set_prolog_flag(toplevel_print_options, NOptions); true.

Nothing to worry about. It's just abbreviating it visually... just press 'w' (write) to display the complete internal representation of the list.

Related

Why do we need the question mark in Immediate Window of VS?

So, reading this documentation:
https://learn.microsoft.com/en-us/visualstudio/ide/reference/immediate-window?view=vs-2022
It looks like the question mark (?) is an alias for the command >Debug.Print, which basically, will evaluate the expression and show the result.
So, in debug mode, instead of running this:
>Debug.Print DoSomething()
I can run this:
? DoSomething()
This is even better because I'm getting the autocomplete suggestions.
Now, the issue is that I can run the same line without a command at all, and it does exactly the same:
DoSomething()
So far looks like there is no need for the command >Debug.Print or the alias ?.
At first, I suspected that using ? will only print the result without changing the values, but this is not the case (When I assign a value to a variable using ? it is assigned and the new value is printed)
So, am I missing something here? Are there any other differences between these 3 options?
According to the documentation, if you want to use Visual Studio command, you need to add greater than sign before the command. If you run 'Debug.Print' without adding greater than sign, you will get an error.
My point is that the question mark ('?') is unnecessary if you in the Immediate Window, it is used to distinguish the typed expression from the result.

A "display" that always displays in prolog

I have a predicate check(Data,Res) that checksDats according to some rules and returns Res (a function result on Data, assuming Data answers to several criteria).
I have another function generate(N,Data) which generates a N-size Data.
My main program begins with generating many 1-size Data, then if none answered the criteria, we go on to 2-size Data and so on until we reach a certain M upper limit.
main(M):- next_number(N,M), generate(N,Data), check(Data,Res).
However, the program runs for very long time. I wanted to make sure it does not get stuck. For this, I wanted to print the generated Data each time before its being checked. But adding display did not assist, because it only actually displayed if the entire statement was true.
That's not what I want.
I want to keep track of the progran using display, similarly to System.out.println in Java.
Is there any other function that displays anyway? Or an idea how to use display in a way that will always display, regardless if the Data answered the criteria or not?
I thought to do:
(check(Data,Res) -> display(Data);display(Data)).
But I am not sure. Any ideas?
Your long process is likely to be within check - or more precisely, that check fails for most data, causing the system to backtrack repeatedly.
If you display a result in check, you'll have line upon line of tracing. Instead, you could add a write statement to generate, or even to your number generation:
main(M):-
next_number_and_tick(N,M),
generate(N,Data),
check(Data,Res).
next_number_and_tick(N,M) :-
next_number(N,M),
write('Tick - now doing '),
writeln(N).
Upon backtracking, the program will signal the data size it is now working on, giving you an idea of the volume of work it is doing.
The problem in the way you use display is that the entire statement must be true for it to display. Your idea of using "if-then" is good but not accurate. If you want to use it, you should "trick" prolog the following way:
new_check(Data,Res) :- (check(Data,Res) -> display('Victory!'),!; display('Failed Data: '), display(Data), nl, fail).
This way, if the check fails, you will get a report on which Data failed, and if it succeeded everything stops (assuming you want only 1 solution. If you want more, remoce the ! predicate).

Undefined procedure error in Prolog when using R-2-L words

I want to make an Arabic morphological analyzer using Prolog.
I have implemented the following code.
check(ي,1,male).
check(ت,1,female).
check(ا,1,me).
dict(لعب,3).
ending('',0,single).
ending(ون,2,plur).
parse([]).
parse(Word,Gender,Verb,Plurality):-
sub_atom(Word,0,LenHead,_,FirstCut),
check(FirstCut,LenHead,Gender),
sub_atom(Word,LenHead,_,LenAfter,Verb),
dict(Verb,LenOfVerb),
Location is LenHead+LenOfVerb,
sub_atom(Word,Location,LenAfter,_,EndOfWord),
ending(EndOfWord,_,Plurality).
This is called using:
parse(يلعب,A,S,D).
Expectation:
A = male
S = لعب
D = single
Explanation of code:
It should parse the word يلعب, note that in Arabic the ي (first letter to the right) indicates that it's masculine word. And لعب is a verb.
Error:
When running the code, I get the following error:
ERROR: parse/4: Undefined procedure: dict/2
Note that when mimicking the Arabic word using English letters, the code behaves as expected and doesn't produce this error.
How can I resolve such error, or make the Prolog understand R-to-L words?
Edit:
In the attached image, note that in the red box, it succeeded to match the ي to male. In the blue box, when it failed, it should have backtracked and starts to concatenate to try to match a new word, but instead it produces the error shown
You have to be careful when you are using SWI-Prolog on the Mac. There is a slight problem with copy paste. If you use [user], and then past multiple lines, it doesn't read all lines:
This happens all the time and isn't related to the arabic script or unicode, or somesuch. I have filed a bug report to SWI Prolog here. When you use [user], and do the lines one by one you get the right result.
In the above screenshot you see that I did a one by one paste, since there are multiple prompts '|:'. Other Prolog systems don't have necessarely this problem, for example I get in Jekejeke Prolog:
Best workaround for SWI-Prolog is probably to store the facts in a file, and consult them from there. In Jekejeke Prolog I have to investigate, why the space after the comma is showing on the wrong side.

How to view complete print output in a terminal?

I'm currently working with a software called CleGo , which is written in O'Caml and I use it in the Toplevel mode. This program computes all Clebsch-Gordan coefficients for a given group representation. Unfortuantely, I can't get the complete output in the terminal. I get:
[[[("-1", ("(0,0,0,0,0,0,1,0,)1", "(0,0,0,0,0,0,-1,0,)1"));
("1", ("(0,0,0,0,0,1,-1,0,)1", "(0,0,0,0,0,-1,1,0,)1"));
("-1", ("(0,0,0,0,1,-1,0,0,)1", "(0,0,0,0,-1,1,0,0,)1"));
...]]]
and I need the complete output that is indicated by "..." in the output. Is there some terminal restrictions that restricts the output to a certain length or is this a special problem of the software?
Any ideas or suggestions would be awesome!

less-like pager for (swi) prolog

The typical workflow in unix is to use a pipeline of filters ending up with a pager such as less. E.g. (omitting arguments)
grep | sed | awk | less
Now, one of the typical workflows in the swi-prolog's command line is asking it to give the set of solutions for a given conjunction like
foo(X),bar(X, Y),qux(buz, Y).
It readily gives me the set of soutions. Which can be much longer than the terminal window. Or a single query
give_me_long_list(X).
can give a very long list again not fitting on the screen. So I constantly find myself in situations where I want to slap |less at the end of the line.
What I am looking for is a facility to open in a pager a set of solutions or just a single large term. Something similar to:
give_me_long_list(X), pager(X).
or
pager([X,Y], (foo(X),bar(X, Y),qux(buz, Y))).
This is not a complete solution, but wouldn't it be rather easy to write your own pager predicate? Steps:
Create temp file
dump X into temp file with the help of these or those predicates
(I haven't done any I/O with Prolog yet, but it doesn't seem too messy)
make a system call to less <tempfile>

Resources