Prolog existence_error for predicate in generated prolog file - prolog

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.

Related

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').

Stop Prolog program with predicate

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.

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.

How can I provide Prolog asks questions to me

Assume that we have prolog knowledge base like this:
guilty(X) :-
commits(X,Y),
crime(Y).
crime(murder).
crime(theft)
When I ask this question:
?- guilty(john)
I want that Prolog asks me a question like that:
is commits(john, murder) ?
and I answer no then
is commits(john, theft) ?
if I answer yes Prolog says
**yes**
How can I make something like this?
Thanks..
You need a modified proof engine, that when encounters an unknown fact query the user about.
Doing it with some generality can be an interesting task, Google for metainterpreter Prolog, if you are interested in this argument, the first link provides you the valuable page A Couple of Meta-interpreters in Prolog by Markus Triska, where you can learn more.
For your question, would suffice a rule
commits(Person, Crime) :-
crime(Crime),
format('is ~w ?', [commits(Person, Crime)]),
read(yes).
test:
?- guilty(john).
is commits(john,murder) ?no.
is commits(john,theft) ?yes.
true.
note that read/1 requires a dot to terminate the input.
You want an 'interactive shell' for your little reasoner. Building one is not difficult, but beyond the scope of a stackoverflow question. This tutorial builds one in the 2nd or 3rd lesson and generally answers this question. It calls facts like your user answers 'working storage'.
http://www.amzi.com/ExpertSystemsInProlog/
Prolog "executes" things from left to right. Try:
guilty(X) :-
crime(Y),
commits(X,Y).
crime(murder).
crime(theft)
So then guilty(X) depends on commits(X,murder) and/or commits(X,theft)

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