It will be quite a miracle if someone could help with the following...
I'm using SWI-Prolog to perform queries in a triples RDF file. The values can be queried, but instead of just a number, the word Literal shows up in front of them (e.g [literal(500000)] shows up for the value 500000). Now, the problem arises when I have a list of numbers that I want to add. I try to convert these Literals into atoms so that Prolog can recognize them as numbers, but get the following error
ERROR: atom_number/2: Type error: atom' expected, found [literal(500000)]'
Any clues would be appreciated. Thanks.
Just use unification to get the number. For instance
?- [literal(500000)] = [literal(N)].
N = 500000.
Related
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.
I'm working on a validator that validates turtle files. When working on a function to check if the cardinality that's stated is correct for each object, I can't figure out how to access the value of a literal.
The literal value is Card=literal(type(xsd:nonNegativeInteger, '1')) (or 1^^'http://www.w3.org/2001/XMLSchema#nonNegativeInteger').
I find a bag of properties of length L. How can I check that L == Card?
I already tried the following:
% L and Card are both 1
rdf_canonical_literal(L, LiteralL), rdf_compare(=, LiteralL, Card).
% false
rdf_canonical_literal(L, LiteralL).
% LiteralL = 1^^'http://www.w3.org/2001/XMLSchema#integer'.
The problem is that xsd:integer and xsd:nonNegativeInteger don't compare as equal.
However, the easiest thing to me would seem to get the value of Card but I really don't see how to do it. Any solutions or pointers where to find an example of this would be much appreciated!!
If you use library rdf11 then most common datatype IRIs are automatically interpreted as Prolog values. In other words: there is no need to convert from RDF literals to Prolog values at all. Example:
?- [library(semweb/rdf11)].
?- rdf_assert(rdf:a, rdf:b, 1^^xsd:int).
?- rdf(_S, _P, N^^xsd:int).
N = 1.
You can extend library rdf11 with a hook for less common datatype IRIs, e.g., I use a lot of geographic data (datatype IRI geo:wktLiteral) which I let rdf/[3,4] interpret as Prolog Well-Known Text (WKT) notation automatically.
Our textbook gave us this example of a structurer for a math equation in Prolog:
math(Result) --> number(Number1), operator(Operator), number(Number2), { Result = [Number1, Operator, Number2] }.
operator('+') --> ['+'].
number('number') --> ['NUMBER'].
I'm quite new to Prolog, however, and I have no idea how to use this example to get the output. I'm under the impression it restructures the input using Result and outputs it for use.
The only input I've tried that doesn't cause an error is math('number', '+', 'number'). but it always outputs false and I don't know why. Furthermore shouldn't it restructure it and give me the result in Result as well?
What should I be inputting here?
This example is a DCG. You should use the phrase/2 interface predicate to access DCGs.
To find out what the DCG describes, start with the most general query, relating the nonterminal math(R) to a list Ls that is described by the first argument:
?- phrase(math(R), Ls).
From the answer you get (very easy exercise!), you will notice that R is probably not what you meant it to be. Hint: Look up (=..)/2.
Notice in particular that you need not be "inputting" anything here: A DCG describes a list. The list can be specified, but need not be given: A variable will do too! Think in terms of relations between arbitrary terms.
I'm trying to learn prolog and i bumpt in this error which, i don't know why i get it so i am asking for your help.
code(TPROLOG#86):
trace
domains
item = integer
intList = item*
predicates
member(item,intList)
clauses
member(elm,[elm|_]).
member(elm,[_|T]):- %%% ***ELM is seen as wrong type, why?***
member(item,[T]).
goal
member(5,[1,2,3,4,5])
Any advice or hint is welcomed. Thank you.
You are confusing variables and atoms. Atoms start with a lower case letter, whereas variables start with an upper case letter.
Also, your member/2 definition seems wrong. It should read:
clauses
member(Elm,[Elm|_]).
member(Elm,[_|T]):-
member(Elm,T).
First clause matches the element with the head of the second list. Second clause skips the head of the second list and recursively calls member/2 to find another match in the tail of the list.
i know there is a build-in function findall/3 in prolog,
and im trying to find the total numbers of hours(Thrs) and store them in a list, then sum the list up. but it doesnt work for me. here is my code:
totalLecHrs(LN,THrs) :-
lecturer(LN,LId),
findall(Thrs, lectureSegmentHrs(CC,LId,B,E,THrs),L),
sumList(L,Thrs).
could you tell me what's wrong with it? thanks a lot.
You need to use a "dummy" variable for Hours in the findall/3 subgoal. What you wrote uses THrs both as the return value for sumList/2 and as the variable to be listed in L by findall/3. Use X as the first argument of findall and in the corresponding subgoal lectureSegmentHrs/5 as the last argument.
It looks like the problem is that you're using the same variable (Thrs) twice for different things. However it's hard to tell as you've also used different capitalisation in different places. Change the findall line so that the initial variable has the same capitalisation in the lectureSegmentHrs call. Then use a different variable completely to get the final output value (ie the one that appears in sumList and in the return slot of the entire predicate).
You need to use a different variable because Prolog does not support variable reassignment. In a logical language, the notion of reassigning a variable is inherently impossible. Something like the following may seem sensible...
...
X = 10,
X = 11,
...
But you have to remember that , in Prolog is the conjunction operator. You're effectively telling Prolog to find a solution to your problem where X is both 10 and 11 at the same time. So it's obviously going to tell you that that can't be done.
Instead you have to just make up new variable names as you go along. Sometimes this does get a bit annoying but it's just goes with the territory of a logical languages.