0

I have a DCG in Prolog that I can query like so:

q(Tree, [name, of, company], []).

and get a response that shows me the path taken to parse the query:

Tree = q(['company (Class)', 'name (Attribute)'])

Now I would like to pose a query such as:

q(Tree, [name, of, acme], []).

and failing to match the term acme, I would like to create a variable Acme so that I get something like:

Acme = company
Tree = q(['company (Class)', 'name (Attribute)'])

I am using SWI-Prolog and am querying it from another language, that is why the query is all lowercase. My other option is creating a lexicon of all the valid terms and replacing all unknows in a query with a variable, but am hoping for a Prolog solution.

Thank you.

false
  • 10,264
  • 13
  • 101
  • 209
Radek
  • 3,913
  • 3
  • 42
  • 37
  • 1
    I am not sure that I understand your question, but may be you can repeat the querying attempts as follows: q(Tree, [name, of, acme], []); q(Tree, [name, of|X], []); q(Tree, [name|X], []); q(Tree, X, []) and stop as soon as you get an answer? – Alexander Serebrenik Apr 10 '12 at 06:13
  • @AlexanderSerebrenik that will work! Now I will just need to constrain the X list size, as I can also do queries like `name of department of company`. – Radek Apr 10 '12 at 07:08
  • I'll write it as an answer since I cannot include code snippets otherwise. – Alexander Serebrenik Apr 10 '12 at 11:31

1 Answers1

1

Do I understand correctly that you need all prefix-based lists? Would the following work for you:

1 ?- p([name,of,company],L).
L = [name, of, company] ;
L = [name, of|_G456] ;
L = [name|_G453] ;
true.

2 ?- p([name,of,department,of,company],M).
M = [name, of, department, of, company] ;
M = [name, of, department, of|_G551] ;
M = [name, of, department|_G548] ;
M = [name, of|_G545] ;
M = [name|_G542] ;
true.

If this is the intended behavior, then the code that implements it can be

p([],[]).
p([X|Xs],[X|Ys]) :- p(Xs,Ys).
p([_|_],_).
Alexander Serebrenik
  • 3,567
  • 2
  • 16
  • 32