0
votes

Q: How can I grab every term in PROLOG, then write it out?

Q: something like : prolog_term(X,Functor,Arg1,Arg2) and prolog_term(X,Functor,Arg1) (I'm only interested in /1 and /2 arities).

I think that I want to know how to search-for-and-grab PROLOG terms, without having to RTFM everything.

[I have used Lisp and C (and assembler) extensively (+30 years), and am diving into the deep end of PROLOG now].

Example:

(1) Read in a JSON factbase, e.g.

{ fb: 
  [
      { relation: "line", subject: "line6", object: "don't care" },
      { relation: "x1", subject: "line6", object: "160" },
      { relation: "y1", subject: "line6", object: "240" },
      { relation: "x2", subject: "line6", object: "160" },
      { relation: "y2", subject: "line6", object: "360" }
  ]
}

(the factbase consists of triples (maybe doubles, when object is "don't care"))

(2) Convert to internal PROLOG format, e.g.

 line(line6).
 x1(line6,160).
 y1(line6,240).
 x2(line6,160).
 y2(line6,360).

(3) Run some PROLOG rules over the factbase.

(4) Write out all (or some) of the resulting facts in JSON format, e.g. something like:

   write_out([line, x1, y1, x2, y2, bounding_box_left, bounding_box_top, bounding_box_right, bounding_box_bottom])

(in this example, line/x1/y1/x2/y2 facts were read in, and bounding_box_* facts were generated in step (3)).

[further details available - I'm trying to keep the question short]

1
This is a bit of a lazy question (as you know yourself). Those things are quite straight-forward to do, if your Prolog has a JSON library. I will try to write an answer.TA_intern

1 Answers

0
votes

It is unclear what you are asking, but if you have the following facts in the Prolog database:

line(line6).
x1(line6,160).
y1(line6,240).
x2(line6,160).
y2(line6,360).

they are literally "facts of the universe" in which your Prolog program works, so you can "grab them" by trying to prove them:

?- x1(line6,X).
X = 160.

And you can "grab them" by using the collection predicate bagof/3, setof/3 and findall/3:

?- findall(x1(L,X),x1(L,X),All).

All = [x1(line6,160)].

Now the above has 5 different predicate names which is excessive, so one would like a fact expressing more with less:

% rectangle(Linename,X1,Y1,X2,Y2).

rectangle(rectanglename,line6,160,240,160,360). % needs a unique name

Just one fact!

If you are in SWI Prolog you can also deploy "dicts" to great advantage:

rectangle(rectanglename,r{x1:160,y1:240,x2:160,y2:360}).