Stop Prolog program with predicate - prolog

I am looking for something like the predicate halt/0 or abort/0.
I do not want to use abort/0 because of the % Execution aborted message at the console and I do not want to use halt/0 because it terminates the Sicstus Prolog program.
Is there any other predicates that can be used to stop the program execution?
I already checked the documentation for this topic and I do not seem to find the result I want to see.

Maybe throw an exception with throw/1?
– mat
I managed to achieve what I wanted using that predicate.
Thank you mat.

Related

Prolog existence_error for predicate in generated prolog file

I'm having a problem with a program that I'm writing. The program takes an input and generates a prolog program based on it. It generates something like this:
test(A):-condA(A),condB(A).
condA(val).
condB(val).
My problem is that sometimes, there is no condB(val), or condB anywhere in the program, except in the above definition of test. In that case, I got existence_error for condB, when I try to ask test(val), for example. Is there a way to add something to the prolog program that would define condB as false for all values of it's argument?
I'm sorry if this is stupid question, as I'm new to prolog.
You can tell the prolog processor that condB/1 is dynamic:
:-dynamic condB/1.
Answer to your question is simple.
condB(_):-fail.
the symbol '_' is free variable.

How to call facts and rules from another file

Case: I have a file, solution.pl, where I want to use the rules of another file (this is the definition of the program, not up to me, but I think it's somewhat common).
So I'm using Ciao Prolog and the syntax seems to be ensure_loaded('c:/Path').
but when I try to use the rules of the other file, it tells me he doesn't know what rule I'm trying to use.
On the other hand, if I write on the console that same lane, and then query again, it does work.
So I was wondering maybe the syntax for doing it on the console and on a file are different??
Could someone tell me a step by step so I can figure it out if I'm missing something? What I'm doing right now is loading the program as I would do with smaller programs, then try to query rules of the file included in the ensure_loaded command. And as I said, it does seem to work if write the command outside of the program.
in solution.pl, try
:- ensure_loaded('c:/Path').
or, if the source in c:/Path has a module directive - that is, it begins with :- module(name, [exported/arity, ...]).
:- use_module('c:/Path').

SWI-Prolog : "false" where?

Normally when the goal fail I get back "false" i.e. the goal was not satisfied.
Is there a way to make SWI-Prolog to print predicate, line and/or sequence number of the sub-goal where it failed.
I don't want to trace the whole program just want some feedback sort of like in other programming languages.
I know it is not error to return false, but when debugging it will be nice to have this feedback to pinpoint problems. If it is possible of course.
thanks
You might also want to consider using the "failure-slice" technique described many times by #false: it has its own tag.
Also, check out this great answer. You can use the code shown there to make your own "debugger" that lets you do (among other things) exactly what you are after.
maybe
...p1, p2, (p3;backtrace(10)), p4,...
could also be useful to start tracing after p3 is failing:
...p1, p2, (p3;backtrace(10),trace), p4,...

Prolog beginner help get current time

I am modifying the Eliza program http://cs.nyu.edu/courses/fall11/CSCI-GA.2110-003/documents/eliza.pl
trying to get it to print the system time when user asks - What Time is it?
After hours of reading through manual I got my get_date_time_value() function to work.
As in
get_date_time_value(Key, Value) :-
get_time(Stamp),
stamp_date_time(Stamp, DateTime, local),
date_time_value(Key, DateTime, Value).
However I am at a loss as to how do I call this function from my rule which is defined as
rules([[time,0],[
[1,[_],0,
[please,do,not,get_date_time_value(time,x),.],
['I',dont,know,the,time,.]]]]).
Yes this is a homework assignment and this might sound silly to experts ,but I am really new to Prolog programming even though I have quite some experience in object oriented and functional programming.
No matter what parameters I pass to the get_date_time_value(time,X) function I am always getting an error.
I spent all night on a hit an trial approach ,but nothing I do works.
Any pointers will be great!!
Thanks
From the structure I guess it should look something like this:
rules([[time,0],[
[1,[_],0,
[it,is,HourLiteral,oclock,.],
['I',dont,know,the,time,.]]]]) :- get_date_time_value(hour, HourNumber), number_codes(HourNumber, HourString), atom_string(HourLiteral, HourString) .
I do not know if it works. I did not test it.
You do not give any idea of what you mean by your rule. Maybe you are trying to have the current time in the list where the term get_date_time_value(time,x) appears: but, is that term a call to a function? Prolog does not support that: just look at the clause you give for the get_date_time_value/2 predicate (not function) and what you see there is a sequence of calls to predicates. So your rule probably must given in a clause that holds only if the call to your get_date_time_value/2 predicate also holds, and the clause head and the call share variable(s) to pass information between them.

what does" readin" predicate in prolog mean?

I have a question and I hope to help me:
what does" readin" predicate in prolog do?
I have searched in google but I have nothing
could you explain it to me with an example?
Regards.
I think you must be seeing readln not readin. The library predicate readln/1 reads tokens from an input line in the interactive SWI-Prolog console and makes a list of them. Most Prolog term reading predicates require a period to terminate input, but this predicate would include any terminating period in the list.
See here for a bit of documenation.

Resources