Search code examples
prologdcg

Prolog - Turning a list into a list of clauses


I am trying to use meta programming and DCGs to turn a list into a list of clauses using Prolog. For example, I would like to turn [a, man, is, a, human] into [ (human(X) :- man(X)) ]

I figured that I could use =.. for composing terms from a list of their constituent parts. For example, the call Term =.. [f,a,b,c] will bind Term to f(a,b,c).

My problem is trying to combine this using DCGs. So far, I have used DCGs to check whether a sentence is of a valid form:

 %% syllogism( +S )
  % Holds if the sentence S is one of four syllogisms

  % a B is a C
  syllogism  --> article, subject, is_, (article ; [] ), subject .

  % some B is a C 
  syllogism  --> some, subject, is_, (article ; [] ), subject .

  % no B is a C
  syllogism  --> no, subject, is_, (article ; [] ), subject .

  % some B is not a C
  syllogism  --> some, subject, is_, not, (article ; [] ), subject .

  subject   --> [X] .
  some      --> [some] .
  is_       --> [is] .
  article   --> [a] .
  article   --> [every] .
  not       --> [not] .
  no        --> [no] .

However I am trying to modify this so that I can produce a list of clauses whilst still relying on DCGs.

EDIT: Basically what I am trying to achieve is take a list L and produce a list of clauses: [a, man, is, a, human] should produce [man(X) :- human(X)]

Similarly: [no, B, is, a, C] should produce [ (false :- B(X),C(X)) ]

Thanks for your time.


Solution

  • It looks like you are going to need a meta-interpreter http://ktiml.mff.cuni.cz/~bartak/prolog/meta_interpret.html

    Ultimately all problems in Prolog come down to using the right kind of meta-interpreter.