Prolog with inequalities - prolog

I found this inequality solving program from some Internet source.It works well.This is the program.
:-use_module(library(clpq)).
dec_inc(Left,Right):-
copy_term(Left-Right,CopyLeft-CopyRight).
tell_cs(CopyLeft).
max(CopyRight,Right,Leq).
tell_cs(Leq).
max([],[],[]).
max([E=<_|Ps],[_=<P1|P1s],[K=<P1|Ls]):-
sup(E,K),
max(Ps,P1s,Ls).
tell_cs([]).
tell_cs([C|Cs]):-
{C},
tell_cs(Cs).
The problem I have regarding this program is when I run the program using capital letter variable it works.But it doesn't work for simple letter variables.
This is the syntax i used to solve this problem.
eg:-
{2*X+3>=5}.
This works and gives the correct answer.
{2*x+3>=5}.
When i run this, Prolog says
ERROR: Unhandled exception: nf(y,_G3082): argument 1 must be a a numeric expression
I'm using SWI-Prolog version 6.6.0.What is the problem here and how can I solve it.

Related

ALC tableau algorithm example

I'm trying to solve the following example using the tableau algorithm for ALC. Given the following TBox T:
A⊆B
A⊆C
B⊆∃R.D̸
C⊆∃R.D
E⊆∀R.D̸
Tell whether the concept A is satisfiable. So I put A(a) in my ABox and I start the algorithm, getting:
A0={((A̸∨B)∩(A̸∨C)∩(B̸∨∃R.D̸)∩(C̸∨∃R.D)∩(E̸∨∀R.D̸))(a)}.
Then I get:
A1={((A̸∨B),(A̸∨C),(B̸∨∃R.D̸),(C̸∨∃R.D),(E̸∨∀R.D̸))(a)}.
which leads me to:
Ak={((A̸(a)∨B(a)),(A̸(a)∨C(a)),(B̸(a)∨R(a,b)D(b̸)),(C̸(a)∨R(a,c)D(c)),(E̸(a)∨D̸(b)))}.
A̸(a) is a clash with A(a), B̸(a) is a clash with B(a) and C̸(a) is a clash with C(a), so I have:
Ak={((B(a)),(C(a)),(R(a,b)D(b̸)),(R(b,c)D(c)),(E̸(a)∨D̸(b)))}.
Let's see what happens with b's expansion:
Ak+1={((A̸∨B),(A̸∨C),(B̸∨∃R.D̸),(C̸∨∃R.D),(E̸∨∀R.D̸))(b)}.
which leads me to:
Ak+m={((A̸(b)∨B(b)),(A̸(b)∨C(b)),(B̸(b)∨blocked),(C̸(b)∨blocked),(E̸(b)∨D̸(c)))}.
Now D̸(c) clashes with D(c) and B̸(b) clashes with B(b).
And I get it is unsatisfiable, which is wrong. Could somebody please show me where I'm wrong with the application of this tableau algorithm?

ERROR: Stream user_input:8:5 Syntax error: Operator expected in Prolog

I have to write a simple expert system in Prolog for scheduling of classes. In this code part, I want that user add an instructor to system. For this, reading two input value but I am getting this error.
addinstructor() :-
read(id),
read(courseid),
assert(instructor(id, courseid)),
write("added").
Query:
?- addinstructor().
5
cse102.
Then, I am getting operator expected error.
How do i fix this to work my code?
The predicate read/1 reads Prolog terms not raw data. Prolog terms end with a period.
So if instead of entering 5 if you enter 5. you will not get the error.
Instead of using the predicates found in Term reading and writing, e.g. read/1, you should use the predicates in Primitive character I/O for reading characters or Predicates that operate on strings for reading strings, e.g. read_string/3
To answer your next question of how do I implement this, see Prolog - Write out facts and reading a users input and then this.

Visual Prolog error c502: The expression does not produce a value

I am trying to translate a simple Turbo Prolog problem to Visual Prolog 7.1
The original Turbo Prolog code is the following.
DOMAINS
s=string sl=s* sll=sl*
PREDICATES
select(sl,s,sl)
solve(sll)
CLAUSES
select([A|B],A,B).
select([A|B],C,[A|D]):- select(B,C,D).
solve([["Anna",A,A],["Kate",Vp,Vt], ["Natasha",Np,"green"]]):-
select(["white","green","blue"],A,ColPl),
select(["white","blue"],A,[Vt]), Vt<>"white",
select(ColPl,Vp,[Np]), Vp<>"white", Np<>"green".
And its resulting list is outputted with solve(Out) with provides a correct result to the Turbo Prolog console.
When trying to translate this to Visual Prolog, I get error c502 in line 33.
implement main
open core
constants
className = "main".
classVersion = "".
domains
s=string.
sl=s*.
sll=sl*.
%
class predicates
select:(sl,s,sl) nondeterm anyflow.
solve:(sll) nondeterm anyflow.
%
clauses
%
select([A|B],A,B).
select([A|B],C,[A|D]):- select(B,C,D).
%
solve([["Anna",A,A],["Kate",Vp,Vt],["Natasha",Np,"green"]]):-
select(["white","green","blue"],A,ColPl),
select(["white","blue"],A,[Vt]), Vt<>"white",
select(ColPl,Vp,[Np]), Vp<>"white", Np<>"green".
clauses
classInfo(className, classVersion).
clauses
run():-
console::init(),
%ERROR AFTER THIS LINE
stdIO::writef("%", solve(Out)),fail().
end implement main
goal
mainExe::run(main::run).
What I get from this error is that solve(Out) does not give anything to print. What I do not know is how to change the code to produce something to print.
I am a beginner in Prolog and I cannot figure out how to fix this problem and Google is not much of a help either, this seems to be very obscure problem.
Thank you!
I'm not familiar with Visual Prolog, but could you rewrite the offending line as:
solve(Out), stdIO::writef("%", Out),fail().
and try again?
Remember that predicates are not functions like in other programming languages; they do not have a return value.
EDIT to answer comment: a procedure predicate should succeed exactly one time. Here, main is calling your solve function which my fail or succeeds several time. To ensure that, you can try to wrap the call to solve into another predicate:
wrap_solve(S) :- solve(S), !.
wrap_solve([]).
The cut after the call to solve should ensure that you get only one solution if it succeeds.
If there's no solution (i.e., the call to solve fails), then the second clause will give a default value (an empty list in that case).
In main, you should call wrap_solve instead of solve.

Datalog code not working in DrRacket

I am trying to run this prolog code in DrRacket: http://www.anselm.edu/homepage/mmalita/culpro/graf1.html
#lang datalog
arc(a,b).
arc(b,c).
arc(a,c).
arc(a,d).
arc(b,e).
arc(e,f).
arc(b,f).
arc(f,g).
pathall(X,X,[]).
pathall(X,Y,[X,Z|L]):- arc(X,Z),pathall(Z,Y,L). % error on this line;
pathall(a,g)?
However, it is giving following error:
read: expected a `]' to close `['
I suspect '|' symbol is not being read as head-tail separator of the list. Additionally, [] is also giving error (if subsequent line is removed):
#%app: missing procedure expression;
probably originally (), which is an illegal empty application in: (#%app)
How can these be corrected so that the code works and searches for paths between a and g ?
The Datalog module in DrRacket is not an implementation of Prolog, and the syntax that you have used is not allowed (see the manual for the syntax allowed).
In particular terms cannot be data structures like lists ([]). To run a program like that of above you need a Prolog interpreter with data structures.
What you can do is define for instance a predicate path, like in the example that you have linked:
path(X,Y):- arc(X,Y).
path(X,Y):- arc(X,Z),path(Z,Y).
and, for instance, ask if a path exists or not, as in:
path(a,g)?
or print all the paths to a certain node with
path(X,g)?
etc.

A tuprolog syntax error.Syntax error at/before line -1

I am new to prolog. I have a .pl file consulting normally on swi-prolog, but when I consult it on tu-prolog, crazy things always happen. Here are parts of my codes.
:- dynamic(assignopT/6).
:- multifile(assignopT/6).
assignopT(30246,30244,30210,30247,+,30248).
When I consulted it on tu-prolog,it said syntax error at/before line 12219,which is the third line above. It work all right on swi-prolog. Then I thought maybe there is something wrong with +, so I changed it to this.
assignopT(30246,30244,30210,30247,'+',30248).
This time,it said syntax error at/before line -1. I really don't get this, what line -1 even suppose to mean. The .pl named swi2tu.pl is on https://drive.google.com/folderview?id=0B4KCEwRVmr_yWjQwOEp3LWpYdk0&usp=sharing
Try instead:
assignopT(30246,30244,30210,30247,(+),30248).
Writing an atom, such as +, between single quotes changes nothing and the error is possibly due to its operator status. Assuming that's the case, writing it between ()'s should fix the possible operator conflict when loading the code in tuProlog (not Turbo Prolog, I assume!).

Resources