Jintao Wang
Jintao Wang

Reputation: 37

How to remove the quotation in prolog?

my(Selected_action,S,Result1):-
    Selected_action = move(X,Y),
    atomics_to_string([X, ",", Y], '', Tuple),
    atomics_to_string(["move(", Tuple, ")"], '', Action),
    atomics_to_string([Action, "scores", S], ' ', Result).

So when running the code like this:

?- my(move(3,4),5,X).
X = "move(3,4) scores 5".

But what I want is:

X = move(3,4) scores 5.

Upvotes: 1

Views: 528

Answers (2)

Kintalken
Kintalken

Reputation: 783

You can use the op functionality of prolog like this...

program

  ([user]) .

  :- op( 10'1 , 'xfy' , 'scores' ) .

  my(SELECTED_ACTION,SCORE,RESULT)
  :-
  (
     SELECTED_ACTION = move(X,Y) ,
     RESULT = SELECTED_ACTION scores SCORE
  ) .

  %^D

example usage

  ?-
  my(move(3,4),5,X)
  .

  %@ X = move(3,4) scores 5

alternate program

An alternate version shows the difference between an xfy op and a yfx op.

The x represents the domain and/\or the source. The y represents the codomain and/\or the target.

Terminology from https://en.wikipedia.org/wiki/Morphism#Definition.

...

  ([user]) .

  :- op( 10'1 , 'yfx' , 'scoreof' ) .

  my(SELECTED_ACTION,SCORE,RESULT)
  :-
  (
     SELECTED_ACTION = move(X,Y) ,
     RESULT = SCORE scoreof SELECTED_ACTION 
  ) .

  %^D

alternate example usage

  ?-
  my(move(3,4),5,X)
  .

  @% X = 5 scoreof move(3,4)

Upvotes: 1

Isabelle Newbie
Isabelle Newbie

Reputation: 9378

Note that the code you posted does not behave in the way you said; you have singleton variables Result1 and Result that should be the same.

As for your question, Prolog has (in a sense) two forms of providing "output". One is through the answer substitutions (the equations) that you see in the interactive shell. The idea of these substitutions is to give you equations that are syntactically valid Prolog fragments. But the version you ask for, X = move(3,4) scores 5. is not syntactically valid Prolog.

This form of output through answer substitutions is useful for programmers, but not necessarily for end users or for other tools that try to parse your program's output. The other form of output is therefore the printing of things to the terminal or to files, like the print functions of many other programming languages. In Prolog, you can do this, for example:

run_my(Query, Score) :-
    my(Query, Score, Result),
    format('~s~n', [Result]).

Which will hide the intermediate Result and just print the string without quotes:

?- run_my(move(3, 4), 5).
move(3,4) scores 5
true.

I implemented this predicate as a wrapper around your existing definition of my/3, which remains unchanged. This is the recommended approach: Do not mix terminal output with the essence of your computation, only add it in special wrappers.

Upvotes: 2

Related Questions