Prolog beginner help get current time - prolog

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.

Related

Diagramming Decision Trees

I have been a self-taught coder for a long time, and I am asking for help with a conceptual problem here. I can solve this particular issue myself, but I feel like these problems always take me too long with trial & error solutions. I believe there is a way to Diagram this type of problem, but I don't know what it is called or how to look up this solution.
When I have multiple, often inter-dependent pre-conditions to a conditional outcome like as follows, it takes me forever to figure how to arrange my conditional statements:
I have a set of Values called: "Tab Numbers". Let's say I have 4 pre-conditions:
All Tab Numbers are blank
Some Tab Numbers are blank
A Session Boolean flag called SuppressPrompt is True
This Method is being called during the Open Session Event
These Pre-conditions determine my 3 desired outcomes:
A) Prompt for User Input
B) Auto-Populate Tab Numbers
C) Do Nothing
Now some of these pre-conditions affect other possible preconditions:
Eg.: The the Flag in Condition #3 cannot be set if the method is being called in the Open Method Condition #4.
I believe there is a good way to diagram these types of problems so that I don't have to puzzle through them with Trial & Error every time. Can anyone help point me to a resource to learn how to do this easier?
I am sorry if I am not posting this in the right place, but it is a problem that I keep running into. If anyone could just help point me in the right direction, I would really appreciate it.
Your truth table may look like this:
Preconditions Outcomes
AllBlank SomeBlank SupPrompt OpenSession UserInput AutoFill Notes
T T [T] T ? ? Impossible
T T T F ? ?
...
F F F F ? ?
You will hopefully see some non obvious correlations, which will allow you to simplify your table, possibly splitting it, until you get the logical expression(s) you need

PDE Mathematica

I'm newbie with Mathematica, so probably my problem is very easy to be solved.
I want to solve a PDE:
The first problem is that the program is not substituting the values inside w[x,y] and its second derivatives, to use them as boundary conditions.
It tells me that Tag Equal in ((w1^(2,0))[x,y]==0)[x,y] is Protected
The other problem is that it gives me also another type of error:
"{NDsolve[{(w1^(0,4))[x,y]+(w1^(2,2))[x,y]+(w1^(4,0))[x,y]==0,{w1[0,y]==0,w1[5,y]==0,((w1^(2,0))[x,y]==0)[0,y]==0,((w1^(2,0))[x,y]==0)[5,y]==0},{w1[x,0]==3,w1[x,5]==3,((w1^(0,2))[x,y]==0)[x,0]==0,((w1^(0,2))[x,y]==0)[x,10]==0}},w1[x,y],{x,0,10},{y,0,5}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing."
Thank you in advance.
I made some changes.
First of all I fixed the boundary conditions because they had a problem.
Moreover I changed also the equation.
Now it still gives me the error:
"{NDsolve[{(w^(0,4))[x,y]+(w^(2,2))[x,y]+(w^(4,0))[x,y]==2,{w[0,y]==0,w[5,y]==0,chix[0,y]==0,chix[5,y]==0},{w[x,0]==0,w[x,10]==0,chiy[x,0]==0,chiy[x,10]==0}},w,{x,0,5},{y,0,10}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing."
If we succeed in fixing this problem, Mathematica should give us a non zero solution.
Thank you very much!
Rewriting it, it doesn't provide any solution. And it also gives no errors. It looks like it is not even substitutiing the boundary conditions. I decided to change them, solving in this way another kind of problem. It still doesn't work. I think that it is very strange, since if the boundary conditions were wrong it should have provided either an indeterminate condition or an impossible one. Any idea?
Thank you very much for your help!

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 do I view how Prolog got to its result/the path it took?

I have a simple Prolog predicate that spits accepts a variable, Result, that calculates a result then stores it in Result to be viewed. Sometimes I'm unsure how it comes to the answer it stores in Result and I'm curious if there's a way in Prolog to get it to show how it got to the answer, I guess show the path it took and how it built it up?
The predicate is something simple like solve(Input, Result). and Result can sometimes be peculiar.

How can I reuse a Prolog data structure?

I am writing a small program using Prolog. There is a data structure that I want to reuse, so I tried assigning it to a variable:
CitizenProfile = voter_profile(citizen,not_in_prison).
Then I used it like this:
state(alabama, [CitizenProfile]).
However, I am encountering this error when I compile my file from the console:
**[No permission to modify static_procedure `(=)/2'][1]**
==
I even tried declaring the equal sign dynamic, but that didn't solve anything. :(
:- dynamic (=)/2.
The reason for the error is that it looks to Prolog like you're trying to do this:
=(CitizenProfile, voter_profile(citizen,not_in_prison)).
This looks just like any other fact definition. =/2 could just as easily be foobar/2:
foobar(CitizenProfile, voter_profile(citizen,not_in_prison)).
Now, if we were in the middle of some rule body, this might be a legitimate way to establish a variable binding. Then everything would be culminating in this:
foo :- ...,
CitizenProfile = voter_profile(citizen,not_in_prison),
state(alabama, [CitizenProfile]).
That would be the same as saying this:
foo :- ...,
state(alabama, [voter_profile(citizen,not_in_prison)]).
If this expansion is what you're trying to accomplish, there is unfortunately no way to create shorthand in a fact database like this. You could, as #hardmath says, use assertz/1 to accomplish it, which would look like this:
make_database :-
CitizenProfile = voter_profile(citizen,not_in_prison),
assertz(state(alabama, [CitizenProfile])).
This would be kind of sketchy behavior though, because you're putting static information into the dynamic store. In my experience, one doesn't usually want to build up large structures in the database. It's usually cleaner and easier to build several relations and "join" across them in a relational manner. I'm not sure what all you're going to want here, so this is just a sketch, but this is kind of what I'd expect to see:
voter_profile(voter1, alabama, citizen, not_in_prison).
voter_profile(voter2, alabama, citizen, in_prison).
voter_profile(voter3, new_mexico, citizen, not_in_prison).
rather than what I presume you'd be building (eventually), which I picture more like this:
state(alabama, [voter_profile(citizen,not_in_prison), voter_profile(citizen, in_prison)]).
state(new_mexico,[voter_profile(citizen,not_in_prison)]).
The temptation to create a bunch of lists is understandable, but Prolog's database can't really help you with processing them. You'll wind up resorting to a lot of member/2 and O(N) searching which will add up to pretty bad performance. By default, Prolog will index on the first argument, but each implementation defines some indexing declarations you can use to make it index the second or Nth arguments in addition or instead. You can then use bagof/3 or findall/3 to reconstitute the lists if you need all the results.
Probably what you want is to define a dynamic predicate voter_profile/2 and assertz new facts "dynamically" to be remembered by that predicate store (the clause database). I say "probably" because you haven't made it clear how a state (e.g. Alabama) should be related to a particular citizen profile.
See here for the SWI-Prolog builtin assertz/1 documentation and much more on database mechanisms of SWI-Prolog.

Resources