0

I'm trying to get a value returned through the variable A such as loves, however I'm getting a result like _382 instead.

Here's the query: ?- checksyn(likes,Result).

I would want Result to return loves, not _628. Is it not binding? I'm not sure.

Here's the code...

synonym(loves,[likes,adores]).
synonym(challenge,[problem]).

checksyn(X,A):-
      synonym(_,[X|_])
   ;  synonym(_,[_|X]),
      synonym(A,[X|_]),
      synonym(A,[_|X]).

Thanks in advance for any help :)

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Joseph
  • 120
  • 9
  • 1
    Your Prolog does not give singleton variable warnings? Are you ignoring them? – Tomas By Dec 13 '17 at 21:00
  • It's not clear how you're defining `checksyn(X, A)`. It says that `checksyn(X,A)` is true *if* `synonym(_, [X|_])` succeeds, OR if *each one* of `synonym(_,[_|X])`, `synonym(A,[X|_])`, and `synonym(A,[_|X])` succeed. If `synonum(_,[X|_])` succeeds, then `A` will not be instantiated and you'll get `_something` as a result for `A`. – lurker Dec 13 '17 at 21:04
  • No it doesn't because I do use the 'Result' in my program. Do you know how I can fix the issue of it not returning the word? – Joseph Dec 13 '17 at 21:04
  • Sorry @lurker, I was replying to Tomas. I understand what you mean. – Joseph Dec 13 '17 at 21:09
  • @Joseph no problem. I apologize for the misunderstanding. – lurker Dec 13 '17 at 21:09
  • Its okay. Didn't mean to sound ignorant :) – Joseph Dec 13 '17 at 21:10
  • But @TomasBy is right: you must be seeing a singleton warning on `checksyn(X,A)` since `A` is singleton in the logical path of `synonym(_,[X|_])`. The fact that it is singleton is what causes it to come out uninstantiated (and so `_nnn`). – lurker Dec 13 '17 at 21:10
  • Yes. You need to first define synonym logically. You really want to say that `X` is a synonym of `A` if (1) `synonym(X, List)` is true for `A` an element of `List`, or (2) `synonym(A, List)` is true for `X` an element of `List`. So in Prolog, that becomes: `checksyn(X,A) :- (synonym(X, L), member(A, L)) ; (synonym(A, L), member(X, L)).` – lurker Dec 13 '17 at 21:13
  • Thanks a lot. It works perfectly. You really helped! – Joseph Dec 13 '17 at 21:16
  • Is there a way do this with a list input, so that it would check for multiple synonyms like ?- checksyn([likes,problem],A). and return loves,challenge? – Joseph Dec 13 '17 at 21:20
  • 1
    Just apply `checksyn/2` as previously defined to each list element using `maplist`: `checksyn_list(Xs, As) :- maplist(checksyn, Xs, As).` That queries `checksyn(X, A)` for each `X` in `Xs` with each corresponding `A` in `As`. – lurker Dec 13 '17 at 21:27

0 Answers0