I'm using LPA WIN-PROLOG and I want to get random values.
I looked for a random predicate and I don't find one that is already defined.
What I tried:
X is random(10)
random(1,10,X)
Can someone help me find the way to get a random value?
Thanks.
LPA Win-Prolog provides a rand/1 built-in function and a seed/1 predicate to get/set the seed of the random number generator. The rand/1 function returns a float between zero and its argument. For example:
?- seed(42), Random is rand(10).
Related
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.
Assume I made a function like below in swi-prolog.
function1(param) :- VALUE is 0, findValue(VALUE), write(VALUE).
However 0 is always printed out. findValue function is logically correct.'
Is it impossible to use calculated VALUE in function1?
Omit VALUE is 0.
By stating it, the function findValue will set the rest of the variables according to VALUE, because the claims you provide first are the axiom-level true ones, and the rest correspond to them.
For example, take
func(X):- X is 0.
If you query func(X). it will result in X=0 because it assumes you want to make an assignment.
However, if you query with a number like func(0). it will check if X==0 or not, resulting in a boolean answer.
I wanted to know how I could use tranpose and findall to list all the variables in a predictate and display it as matrix?
so this is the predicate with all the variables.
across(2,4,2,4).
across(2,10,2,4).
across(3,4,4,12).
across(3,10,2,6).
across(4,3,2,6).
across(4,6,4,10).
Probably easier to store it in a predicate.
getAcross(List) :- findall([A,B,C,D], across(A,B,C,D), List).
So getAcross(X) will store a list of [[A,B,C,D],[A,B,C,D]... etc
If you're talking about transposing a matrix for example, you may only need to that if you have down constraints as well. :)
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.
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.