Why does Prolog duplicate all the answers when the number of print directives is more than one? - duplicates

Following is my knowledge base:
parent(claudia, taiane).
parent(wl, cris).
parent(cris, jim).
parent(cris, pedro).
man(jim).
man(pedro).
man(w).
man(wl).
woman(taiane).
woman(claudia).
woman(cris).
When my main has just one print:
main:-
man(M),
print(M), nl,
fail.
main:- halt.
when I execute it with swipl -q -s exercise-family-tree.pl -g main I get nonduplicated results (all good).
On the other hand, when I query more in my main and have print two times with two different variables as its arguments in my main:
main:-
%which women have both a father and a son in the database?
woman(X), parent(X, S), parent(F, X), man(F), man(S), print(X), nl,
man(M),
print(M), nl,
fail.
main:- halt.
than the results get duplicated. Why does my code duplicate all the answers in the second case?
The issue is different from the one I raised earlier. Please let any interactive solutions that involve REPL be outside of the scope of this question.

than the results get duplicated. Why does my code duplicate all the answers in the second case?
I don't think your problem is anything to do with the top level or way you're running it from the command line, it's that you don't really understand Prolog search, choice points and backtracking and have written a nested loop which prints the same results twice.
The first code acts like (pseudocode):
for M in [jim, pedro, w, wl]:
print(M)
The second code acts like a nested loop:
for _Child in [jim, pedro]:
print(chris) % NB: same 'chris'
% for both choices of _Child
for M in [jim, pedro, w, wl]:
print(M)
In more detail, the first code with man(M), print(M), nl, fail runs like this:
man(jim), print(jim), fail, % backtrack to man(M)
man(pedro), print(pedro), fail, % backtrack to man(M)
man(w), print(w), fail, % backtrack to man(M)
man(wl), print(wl), fail. % backtrack to man(M)
% no more man(M) choices
The second case, this code:
woman(X), parent(X, S), parent(F, X), man(F), man(S), print(X), nl,
man(M),
print(M), nl,
fail.
runs like this:
woman(taiane), parent(taiane, ???) % implicit fail
woman(claudia), parent(claudia, ???) % implicit fail
woman(chris),parent(chris, jim),parent(wl, chris),man(wl),man(jim),print(chris)
% ~~~
% found a solution, now go forward through the man(M) code:
man(jim), print(jim), fail, % backtrack to man(M)
man(pedro), print(pedro), fail, % backtrack to man(M)
man(w), print(w), fail, % backtrack to man(M)
man(wl), print(wl), fail, % backtrack to man(M)
% no more man(M) choices, now the last choicepoint was
% at parent(chris, S) which had choice S=jim or S=pedro.
% Redo that:
woman(chris),parent(chris, pedro),parent(wl, chris),man(wl),man(jim),print(chris)
% ~~~~~
% found a solution, now go forward through the man(M) code:
man(jim), print(jim), fail, % backtrack to man(M)
man(pedro), print(pedro), fail, % backtrack to man(M)
man(w), print(w), fail, % backtrack to man(M)
man(wl), print(wl), fail. % backtrack to man(M)
% no more man(M) choices, now parent(chris, S) has
% no more choices, so end.
So you make two different choices for S from [jim,pedro] by X=chris, parent(X, S) but do not report them, and only report the other choices, for X and M, which are the same in both cases, so it looks like duplication.

Your problem is not where you think it is. First, let me show you how to run a program from the command line. This one is a "hello world":
Contents of hello.pl:
main :-
format("hello~n").
The way to run it from the command line without entering the top level:
$ swipl -g main -t halt hello.pl
hello
Now here is a program that on the interactive top level would backtrack and have multiple solutions. Just to demonstrate, here is the program, in a file print.pl:
main :-
between(1, 3, X),
format("~w~n", [X]).
and if you do it from the top level, you get:
?- [print].
true.
?- main.
1
true ;
2
true ;
3
true.
Fine. If we do it as the "hello world" above however:
$ swipl -g main -t halt print.pl
1
Now, I will add a "fail" like you did. Here is my new program in a file printfail.pl:
main :-
between(1, 3, X),
format("~w~n", [X]),
fail.
When I run this from the command line:
swipl -g main -t halt printfail.pl
1
2
3
ERROR: -g main: false
Bottom line: throwing away solutions by running the program non-interactively is throwing you off.
And a footnote: The fail causes your program to backtrack even when a solution was found. The backtracking undoes the bindings but cannot undo the side effect.

Try it with main as:
main:-
%which women have both a father and a son in the database?
%trace,
woman(X), parent(X, S), parent(F, X), man(F), man(S), print(X), nl,
%man(M),
%print(M), nl,
fail.
main:- halt.
This shows the duplicate:
$ swipl -q -s parent3.pl -g main
cris
cris
Can use https://www.swi-prolog.org/pldoc/doc_for?object=distinct/2 to remove the duplicate, i.e.:
distinct(X, (woman(X), parent(X, S), parent(F, X), man(F), man(S))), print(X), nl,

Related

How to run repeatedly a proc in Tcl

I have written a proc in tcl which takes one argument (a positive integer) und displays a text. Lets call it permu (for permutation). I would like to execute this proc permanently, so
puts [permu 3]
and with the same argument (here 3), lets say every 2 or 3 seconds or so, without removing the previous outcome of the code. How can I do this?
The second question: Same question as above but I would like to clear the screen when the new outcome of permu is displayed.
Third question: In case that I decide to stop a running code (I work with Linux), for example the one above, how can I do this?
Thanks in advance!
Here's one way to do the repeated output:
proc repeat_permu {arg delay} {
puts [permu $arg]
after $delay [list repeat_permu $arg $delay]
}
# Note that the delay is in milliseconds
repeat_permu 3 3000
# Now start the event loop
vwait forever
To clear the screen you need to send the appropriate escape sequence before each new output. Most terminal emulators will accept the vt100 code, so you would do puts "\x1b[2J".
Normally you could just stop your program running by typing control-c, or do you want some means of doing this programmatically?
Update: A simpler way to do the repetition if you don't need to process any other events in parallel is just: while 1 {puts [permu 3]; after 3000}

Uncrustify command for CUDA kernel

I would like to apply uncrustify (via beautify in the Atom editor and a config file) to CUDA code. However, I don't know how to tell uncrustify to recognize CUDA kernel calls which have the following structure:
kernelName <<<N,M>>> (arg0,arg1,...);
However, uncrustify has problems with the <<< >>> and applying it gives the following unpleasant result
kernelName << < N, M >> >
(arg0,arg1,...);
I would like to have it look more like a function call and also avoid the formatting of <<< to << <. Ideally, the result would look like
kernelName <<< N, M >>> (arg0,arg1,
...); // line break if argument list is too long
Which arguments can I add to my config.cfg to achieve the above result?
Thank you very much.
Looking through whole documentation of uncrustify, I have found 2 arguments that could influence in your CUDA kernel style:
sp_compare { Ignore, Add, Remove, Force }
Add or remove space around compare operator '<', '>', '==', etc
And:
align_left_shift { False, True }
Align lines that start with '<<' with previous '<<'. Default=true
You can try to play around with these parameters to be more closely to the solution although I would try something like:
sp_compare = Remove
align_left_shift = False

GNU make call function with multiple arguments and multiple commands

I am trying to write a GNU make call function (example below) which has multiple shell commands to execute, such that it can be called with different arguments.
shell_commands = $(shell echo $(1); ls -ltr $(2))
try:
$(call shell_commands,$(FILE1),$(FILE2))
1) Is above the correct way to write a call function with multiple commands? By using a semi-colon to separate them? To make it readable, I write my targets as shown below. Is there a similar way to write a call function?
shell_commands:
echo $(1)
ls -ltr $(2)
2) I get this error from make when I execute make -B try. It looks like it is trying to execute /home/user/file1. But why?
make: execvp: /home/user/file1: Permission denied
make: *** [try] Error 127
3) Is it possible to pass variable number of parameters to a call function? Like pass in just the second parameter and not the first one.
$(call shell_commands,,$(FILE2))
I tried google-ing, searching on SO, and looking on gnu.org but I did not get any solutions. Will appreciate any answers or pointers to any resources which document call function with multiple optional arguments and commands.
Question 1: No, this is not right. The shell make function should NEVER be used inside a recipe: the recipe is already running in the shell, so why would you run another shell? It's just confusing. Second, it's generally recommended to use && between multiple commands in a recipe, so that if the first command fails the entire command will immediately fail, rather than continuing on and perhaps succeeding. Of course, that is not always correct either, it depends on what you're trying to do.
Question 2: This happens because the shell make function is like backticks in the shell: it expands to the output printed by the shell command it runs. Your shell command that make runs is:
echo $(1); ls -ltr $(2)
(where, one assumes, $1 expands to /home/user/file1) which prints the string /home/user/file1. After the expansion, that string is substituted into the recipe and make tries to run that recipe, giving the error you see above.
You want this, most likely:
shell_commands = echo $(1) && ls -ltr $(2)
try:
$(call shell_commands,$(FILE1),$(FILE2))
Now the call expands to the actual text, not an invocation of make's shell function, and then that text is run as the recipe.
Question 3: Sure, just using empty parameters means that the variable $1 (in this case) expands to the empty string.

How to use multiple 'commands' in the same function in Haskell

I'm trying to figure out how, if even possible, can you perform more than one command within a Haskell function? For example, if I had:
foo [[a]] = print (head a) --AND -- map (head of everything but the first value)
How would I go about doing something like that? Is there a function which I could use in place of --AND-- which would allow me to perform both commands on the list of lists?
You're probably looking for do notation, which combined with properly formed pattern matching would get your desired behavior:
foo :: Show a => [[a]] -> IO ()
foo xs = do
print $ head $ head xs -- first value in the 2D list
print $ map head $ tail xs -- Skip first sublist with tail
Control.Arrow module has many combinators for wiring inputs to functions in interesting ways. Fanouts, fan-ins, etc.
On my phone, but something like print (<<<) head (&&&) map ( f . tail) $ input . That is wrong functions and order of operations, but gives a hint of the flavor. Check the module docs.

Haskell call function onload

Good morning fellow programmers!
i'm working on a project using haskell, and i've wanted to know how to run a haskell function without having to type two lines on ghci, for example
ghci filename.hs function
This can only be done doing:
ghci filename.hs
function
????
I'm looking for something like the main () in C,which runs automatically when you compile the program
Is there something like that?
I've been checking the -e option on ghci, but i cant seem to get it to work!
Thank you very much!
Cheers!
You're probably looking for ghc -e instead:
> echo 'foo x y z = x+y*z' > foo.hs % let's make a foo.hs file
> ghc foo.hs -e 'foo 1 2 3' % call the function in foo.hs
=> 7
Also, note that you can also use the :reload command in ghci. Load the file in ghci, edit, type :reload and test again. Also, if this seems too tedious, you can also define a ghci macro which allows you to reload and test your function at the same time:
> :def test \x -> return (":reload\n" ++ x)
> :test foo 1 2 3
=> Ok, modules loaded: Foo.
7
If you're looking to build real programs instead of quickly testing
your functions, then you'd better read the other answers on writing
main functions.
I assume function has the type IO (). Then you can just let main = function, and use for example runhaskell modulename from the command line. As in C, main is a special function.
To clarify a bit, just in case: If your function is a pure one, i.e. one whose type does not invovle IO, you can't really "run it". I guess it's a simplification to say this, but essentially what GHCi does is to call print function. If you want to mimic this, you can use something like main = print function and use runhaskell. This assumes function's type is an instance of Show.