How can I check if a key-value pair is not in a map of kframework? - kframework

I wrote something like
requires notBool(K |-> V in P)
But it does not seem to be the right syntax. What is the right way to check if a key-value pair does not exist in a map?

The syntax you want is notBool K in_keys(P) to check whether a key is in a map. If you want to also check whether the key is bound to a certain value, you can write notBool K in_keys(P) orBool P[K] =/=K V.

Related

Correct use of findall/3, especially the last result argument

I'm a beginner in Prolog and I am dealing with a problem that might seem stupid to you, but I really can't understand what I'm doing wrong! Ok, I have this file fruits.pl and inside that I have something like this:
fruit(apple,small,sweet).
fruit(lemon,small,nosweet).
fruit(melon,big,sweet).
I have already (inside that file made a coexist(X,Y) atom that checks if two fruits can be put together in a plate. It works fine! But now I can't create a suggest(X) that takes as a parameter a fruit and returns a list of fruits that can be put together in the same plate.
The thing is I was trying to make something like that
suggest(X) :- findall(Y,fruit(Y,_,_), List), coexist(X,Y).
What do you think? Every time I try to run this in swi prolog there is a warning 'singleton variable' and when I press
suggest(apple).
then it says false..
sorry for my english :/
Predicates in Prolog do not return anything. You have goals that are satisfied or not and you can interpret that as returning true or false.
Your predicate suggest(X) should contain another parameter that will be bound to the list of fruits that go together with X. An option would be: suggest(X, List) which describes the following relation: List represents all the fruits that go together with X. Then, you could ask:
?- suggest(apple, List).
List = [pear, cherry].
The goal findall(Y, ... , ...) uses the Y variable internally and Y is still unbound after the goal is satisfied. So, you should move coexist(X,Y) inside the second argument of findall/3 which is the goal that is satisfied in all possible ways. Th rule below works only if X is instantiated (suggest(+X, -List)).
suggest(X, List) :- findall(Y, (fruit(Y,_,_), coexist(X, Y)), List).
You can read this as follows: "List represents all fruits Y that coexist with X".
When you try to define a predicate in Prolog, first of all pretend that you have written that predicate already and start with imagining how you would use it. That is, what queries you would like to pose.
To me, it looks as if coexist/2 already describes what you want. BTW, may_coexist/2 might be a more descriptive name. Why do you want this in a separate list? And why using fruit/3 at all? But for the sake of the question let's assume that this makes sense. So essentially you would have now a relation fruit_compatible/2:
fruit_compatible(F, G) :-
fruit(F, _, _),
may_coexist(F, G),
fruit(G, _, _). % maybe you want to add this?
And now, let's assume you want this list too. So you would have a relation fruit_suggestions/2. How to use it?
?- fruit_suggestions(apple, L).
L = [cherry,pear].
or ... should it be rather L = [pear,cherry]? Or both?
?- fruit_suggestions(lemon, L).
L = [orange].
So every time I want a suggestion I have to think of a fruit. Always thinking: what fruit should it be? Fortunately there is a less demanding way in Prolog: Simply use a variable instead of the fruit! Now we should get all suggestions at once!
?- fruit_suggestions(F, L).
F = apple, L = [cherry, pear]
; F = lemon, L = [orange]
; F = cromulon, L = [embiggy, mushfruit].
So we need to implement it such that it will behave that way. findall/3 alone does not solve this. And implementing it manually is far from trivial. But there is setof/3 which handles variables in exactly that manner. Many of the tiny nitty-gritty design decisions have already been made, like that the list will be sorted ascendingly.
fruit_suggestions(F, L) :-
setof(G, fruit_compatible(F, G), L).
Edit: Due to the discussion below, here would be a solution that also permits empty lists. Note that this sounds trivial but it is not. To see this, consider the query:
?- fruit_suggestions(F, []).
What does it mean? What should F be? Also things that are no fruits at all? In that case we would have to produce solutions for everything. Like F = badger ; F = 42 ; .... Most probably this does not make much sense. What might be intended is those fruits that are incompatible with everything. To this end, we need to add a new rule:
fruit_suggestions(F, []) :-
setof(t,X^Y^fruit(F,X,Y),_),
\+ fruit_compatible(F, _).
fruit_suggestions(F, L) :-
setof(G, fruit_compatible(F, G), L).

Velocity, how to retrieve a hashmap value using another velocity variable

I have a HashMap in my bean:
HashMap<String, SomeObject> someHashMap;
Then in the velocity template I need to access the HashMap with a value that I have in velocity from other source (in fact I have many keys not only one that's why I need to get the values this way):
$key
How can I access the hashmap with this key? I'm trying:
$someHashMap.get($key)
and
${someHashMap.get($key)}
But those two only write the same thing to the output, meaning that with the first line I literally get:
$someHashMap.get($key)
In the webpage.
Which is the correct way/syntaxis to do this?
Thanks!
Both are correct syntax, and they should work.
Does $key have the right value? Print it.
Does $someHashMap indeed point to the map? Print it. If not, perhaps you forgot to put in the VelocityContext being used.
Is the value stored under that key null? The default behavior of Velocity is to print out the code that was called when the outcome is null. To make it not do that, use the silent notation: $!{someHashMap.get($key)}
I had this exact same problem. In my case I tried to do this:
$map.get($locale)
where $locale is e.g. "fi_FI". I solved it by adding quotes inside the brackets:
$map.get("$locale")
I'm not sure, but I think the rationale goes like this:
$map.get( $locale ) -> $map.get( fi_FI ) -> Velocity gets confused
$map.get("$locale") -> $map.get("fi_FI") -> Velocity retrieves correct value

SWI prolog make set of variables name with rbtrees or others means

I have got a term from which I want to get set of variables name.
Eg. input: my_m(aa,b,B,C,max(D,C),D)
output: [B,C,D] (no need to be ordered as order of appearance in input)
(That would call like set_variable_name(Input,Output).)
I can simply get [B,C,D,C,D] from the input, but don't know how to implement set (only one appearance in output). I've tried something like storing in rbtrees but that failed, because of
only_one([],T,T) :- !.
only_one([X|XS],B,C) :- rb_in(X,X,B), !, only_one(XS,B,C).
only_one([X|XS],B,C) :- rb_insert(B,X,X,U), only_one(XS,U,C).
it returns tree with only one node and unification like B=C, C=D.... I think I get it why - because of unification of X while questioning rb_in(..).
So, how to store only once that name of variable? Or is that fundamentally wrong idea because we are using logic programming? If you want to know why I need this, it's because we are asked to implement A* algorithm in Prolog and this is one part of making search space.
You can use sort/2, which also removes duplicates.

Is there a way to find only part of the key value in a dictionary object?

There a dictionary object that gets loaded with the following key values:
119
189a
189b
189c
197
201a
201b
In most situations, life is good and all the individual key values are needed/unique. But in certain situations, the keys with letters behind them (ie...189a, 189b, 189c) all mean the same thing (ie...189). So I need a way to see if a key value exists (like the containskey method) for only the first part of the key and then return true.
Any ideas on how to accomplish this?
Since you only sometimes need ignore the suffixed letter, for the greatest efficiency, I would recommend using an additional HashSet(T) to store the numeric portion. When you add/remove elements from your dictionary, also add/remove the numeric from the HashSet(T). HashSet(T).Contains method is O(1), so checking to see if an element exists will be quick.
Something like this?
dictionary.Keys.Any(Function(key) key.StartsWith("189"))
or you can use a Regex for more find-grained control:
dictionary.Keys.Any(Function(key) Regex.IsMatch(key, "^189[^\d]?")

How to view, find and remove key value pairs in CFMutable Dictionary

How do i find and remove the key value pairs of a particular key using CFMutableDictionaryRef.
I have added a value using CFMutableDictionary but i need to know how to search ,view and delete a keyvalue pair.
The value i have created is a structure pointer and key is an integer value.
Beata,
The CFMutableDictionaryRef documentation shown Here will guide you.
In the order of your question:
For finding an element, see CFDictionaryGetValue
For removing an element, see CFDictionaryRemoveValue
Note that the CFDictionary types are a 'toll-free-bridge' with NSDictionary.
Frank