3

I need help writing a predicate ground( Term) that returns true if Term doesn't have any uninstantiated variables.

I know i need to use the built-in predicates functor, arg, and '=..' but I think i need help just getting started...

My idea is that I need to go down the Term (Term could be a list of multiple variables). Check the Head, Then recursively look at the the rest of the list and check if the rest of the list is uninstantiated.

But my problem is... How do i check if it's uninstantiated?

vzwick
  • 11,008
  • 5
  • 43
  • 63
Bob Smith
  • 55
  • 2
  • 4
  • I hate to mention that this is [kind of a duplicate of the question you asked 10 minutes ago](http://stackoverflow.com/questions/7856059/prolog-ground-term-true-if-no-uninstantiated-variables). You should also learn to add the `homework` tag to questions that are ... homework. On the other hand, kudos for the obvious effort put into the question this time. – vzwick Oct 21 '11 at 23:50
  • Ah sorry ya i'll add homework next time to the tags – Bob Smith Oct 21 '11 at 23:52
  • Also, [does this help you in any way](http://stackoverflow.com/questions/2984104/prolog-beginner-how-to-make-unique-values-for-each-variable-in-a-predicate/3000352#3000352)? I know about prolog nearly as much as I do about the specifics of submarine resource mining (== nothing) but it sounds like a very similar problem to me. – vzwick Oct 21 '11 at 23:54

1 Answers1

1

You can use the var/1 predicate to test whether a term is an uninstantiated variable.

?- var(X).
true.

?- var(x).
false.

?- var((X,Y)).
false.

?- var(t(Y,Z)).
false.
svick
  • 236,525
  • 50
  • 385
  • 514
  • I just discovered that just now as well! Ok so I'm getting closer. This is what I have so far: ground( []). ground( Term) :- Term =..L. var( L). What's wrong with that? I have two tests to do: ground( t(a,b,c,)) and ground( t(a,X,c)). The first one works correctly. Second one says that it is true as well, which it is not because X is uninstantiated. – Bob Smith Oct 22 '11 at 01:16
  • `var` works on single variables. You can't use it on the result of `=..` like that. You have to test each member of the list, and do that recursively (so that it works for something like `t(t(X))` too). – svick Oct 22 '11 at 01:51