Run prologue with swipl on command line

I'm looking for a swipl

similar function perl -e

Specifically, I want to run the prologue code this way:

swipl --wanted-flag "fact(a). message:-writeln('hello')." -g "message" -t halt

      

This can be done with

 swipl -f file -g "message" -t halt

      

where the prologue sentences are written in file

I am running swipl on the server side, which takes user input as prologue suggestions, so writing a file on the server is not recommended.

+3


source to share


1 answer


The only thing you can do is use with an option and load from standard input and not from an argument (you can still pass the entry point as an argument, I think): load_files/2

stream

Say in the file fromstdin.pl

that you have:

main :-
    load_files(stdin, [stream(user_input)]),
    current_prolog_flag(argv, [Goal|_]),
    call(Goal),
    halt.
main :- halt(1).

      

and with this you can do:

$ echo 'message :- format("hello~n").' | swipl -q -t main fromstdin.pl -- message
|: hello

      



@False comments on this answer and the question will tell you what it |:

is if you're wondering, but if it annoys you, just do:

$ echo 'message :- format("hello~n").' \
    | swipl -q -t main fromstdin.pl -- message \
    | cat
hello

      

instead.

This will allow you to read any prologue from standard input and call an arbitrary predicate from it. Whether this is a smart thing, I don't know. I also wouldn't be surprised if there is a much easier way to achieve the same.

+3


source







All Articles