Prolog: Arguments not sufficiently instantiated - prolog

I am trying to compile and query this code:
got('no').
inMap(X,Y):- print("From inMap").
mask(1,3).
doctor(2,1).
verify(X,Y):-
(inMap(X,Y), ((\+(got('mask'); got('vaccine')),farCovid(X,Y)); True),
(print(X), print(Y); True),
(doctor(X,Y), print(X), print(Y); True)).
When my query is:
?- verify(2,1).
It returns:
"From inMap""From farCovid"2121
true .
But when my query is:
?- verify(1,1).
It returns:
"From inMap""From farCovid"11
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [10] verify(1,1)
ERROR: [9] <user>
Can somebody explain to me what is wrong here please, wasn't it supposed to pass in (1,1) in the second query to doctor(X,Y) and return false?
Thanks in advance!

Related

Predicate for has triplicate in a list?

I am writing a predicate to print true if a list contains 3 copies of an element and also to print the element. Also the predicate should print alternate solutions if they exist.
The predicate which I have written -
hasTriplicate(List):-hasTriplicateAcc(List,Element,0).
%hasTriplicateAcc is wrapped by predicate hasTriplicate since I wanted the arity of hasTriplicate to be 1.
hasTriplicateAcc(_,Element,3):-write(Element).
hasTriplicateAcc([H|T],H,Ct):-hasTriplicateAcc(T,H,Ct1),Ct1 is Ct+1.
hasTriplicateAcc([Z|T],H,Ct):-hasTriplicateAcc(T,H,Ct),Z=\=H.
The output
hasTriplicate([1,1,1]).
11
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [11] 3 is _10188+1
ERROR: [10] hasTriplicateAcc([1,1],1,_10218) at c:/users/user/desktop/code/hastriplicate.pl:3
ERROR: [9] hasTriplicateAcc([1,1|...],1,0) at c:/users/user/desktop/code/hastriplicate.pl:3
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
Exception: (10) hasTriplicateAcc([1, 1], 1, _10332)
I don't understand the error. It would be good if anyone tells me how to correct my code for the predicate.
I think this solution will work .
hascountminimal(_,_,0):-true,!.
hascountminimal([E|L],E,N):- ! , N1 is N - 1 , N >= 1 , hascountminimal(L,E,N1) .
hascountminimal([_|L],E,N):- hascountminimal(L,E,N).
intripple(L,E):-hascountminimal(L,E,3),write(E).

Prolog - is the following query satisfied?

I am doing exercise from the following link
Here is knowledge base:
house_elf(dobby).
witch(hermione).
witch(’McGonagall’).
witch(rita_skeeter).
magic(X):- house_elf(X).
magic(X):- wizard(X).
magic(X):- witch(X).
I am expecting the following query to return true:
?- magic(’McGonagall’).
However, my SWI-Prolog (AMD64, Multi-threaded, version 7.6.4) on Windows 7 returns the following:
ERROR: Stream user_input:450:4 Syntax error: Unexpected end of clause
?- magic('McGonagall').
ERROR: Undefined procedure: wizard/1
ERROR: In:
ERROR: [9] wizard('McGonagall')
ERROR: [8] magic('McGonagall') at c:/users/some_user/google drive/projects/nlp/prolog/code/ex2_2.pl:6
ERROR: [7] <user>
Exception: (9) wizard('McGonagall') ? creep
Exception: (8) magic('McGonagall') ? creep
?-
Why does it fail?
In the magic/1 predicate, you call wizard/1, which is not defined:
magic(X):- house_elf(X).
magic(X):- wizard(X).
magic(X):- witch(X).
The result is that Prolog errors, since it calls a predicate that is nowhere defined.
You can for example define a wizard/1 predicate that always fails:
% a world without wizards (if you do not specify extra wizards)
wizard(_) :- fail.
or populate your "world" with wizards, like:
wizard(dumbledore).
wizard(remus_lupin).
%% ...

How to make list constructors (e.g .(a,[]) == [a].) work in Prolog?

I am learning Prolog and .(a,[]) == [a]. should return true in SWI-Prolog, but it gives me an error saying
ERROR: Type error: `dict' expected, found `a' (an atom)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,a),_4020))
ERROR: [10] '$type_error'(dict,a) at /Applications/SWI-
Prolog.app/Contents/swipl/boot/init.pl:3369
ERROR: [9] '$dicts':'.'(a,[],_4086) at /Applications/SWI-
Prolog.app/Contents/swipl/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
Anyone knows how to fix this?
Start the SWI Prolog executable with the --traditional command line option (comment due to user:false).
Then it works:
1 ?- .(a,[]) == [a].
true.
2 ?- current_prolog_flag( traditional, X).
X = true.
3 ?- set_prolog_flag( traditional, false).
ERROR: set_prolog_flag/2: No permission to modify flag `traditional'
4 ?-
This is mentioned in the documentation here (see "traditional", near the bottom of the page).
As can be seen, attempting to change it from within the running SWI session, fails.

How to use list constructors (./2) in SWI-Prolog

I am trying to use list constructor in SWI-Prolog, but am getting 'dict' expected error.
For example,
.(a, []) == [a].
ERROR: Type error: `dict' expected, found `a' (an atom)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,a),_14808))
ERROR: [10] '$type_error'(dict,a) at /Applications/SWI-Prolog.app/Contents/swipl/boot/init.pl:3369
ERROR: [9] '$dicts':'.'(a,[],_14874) at /Applications/SWI-Prolog.app/Contents/swipl/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
Exception: (9) '.'(a, [], _14200) ?
Could anyone help me configure this functionality?
SWI-Prolog 7.x uses a different list constructor, '[|]'/2, instead of the traditional ./2 Prolog constructor:
?- '[|]'(1,[]) == [1].
true.
The change was motivated to free ./2 for other uses, notably in dict terms, as hinted in the error message you got for your query.
Better to use | in conventional notation,
?- X = '[|]'(1,[0]).
X = [1, 0].
can be write like this
?- X = [1|[0]].
X = [1, 0].

source_sink error in prolog using rdf_load

I want to load rdf url from this rule:
getActorFilms(Actor_Name,Films):-
my_replace(Actor_Name, ' ', '_', Correct_Syntax_Of_Actor_Name),
string_concat('http://dbpedia.org/data/',Correct_Syntax_Of_Actor_Name, URL_TO_LOAD),
string_concat(URL_TO_LOAD,'.rdf',RDF_URL),
rdf_load(RDF_URL),
rdf(Films, 'http://dbpedia.org/ontology/starring', Object)
.
when the input is e.g. for an example 'Hugh Jackman'
it tells the error:
ERROR: [Thread pdt_console_client_0_Default Process] source_sink `http://dbpedia.org/data/Hugh_Jackman.rdf' does not exist
I'm really concerned about the ` character between http and source_sink
I think it should be '
although the url is correct, so what to do with that error?
You should pass an atom to rdf_load, not a string. This behavior (the misleading error message, not accepting a string) is probably a bug.
This works for me:
?- use_module(library('semweb/rdf_db')).
true.
?- use_module(library('semweb/rdf_http_plugin')).
true.
?- rdf_load('http://dbpedia.org/data/Hugh_Jackman.rdf').
% Parsed "http://dbpedia.org/data/Hugh_Jackman.rdf" in 0.02 sec; 371 triples
true.
?- string_to_atom(S, 'http://dbpedia.org/data/Hugh_Jackman.rdf'), rdf_load(S).
ERROR: source_sink `http://dbpedia.org/data/Hugh_Jackman.rdf' does not exist
I hope this solves your problem.

Resources