What is the meaning of predicate "simple/1" in Prolog (SWI-Prolog)
Asked Answered
Z

2

0

I run into problem while reading a book. I see a program use predicate "simple" ( I guess simple/1 ). I don't know what is the meaning of this predicate, I can't find it with ?-help(simple) in the console. But when I tried with some queries in console, it worked something like:

5 ?- simple(p(x)).
false.

6 ?- simple(mia).
true.

7 ?- simple(Mia).
true.

8 ?- simple(f(Mia)).
false.

I guess it is some sort of predicate to determine if the argument was Terms(or Variables) or Complex Terms.

Zarah answered 28/1, 2012 at 4:20 Comment(0)
G
2

The swi-prolog manual has the following definition:

simple(@Term) is semidet Term is atomic or a variable.

the definition is in the quintus prolog compatibility library; in the quintus prolog documentation the definition is:

simple(+Term)

Term is currently instantiated to either an atom, a number, a database or a variable.

in any case, simple/1 is true if the argument is a simple term (not sure what the quintus manuals means by database; possibly a handler for an ODBC connection i guess)

Gawen answered 28/1, 2012 at 4:32 Comment(0)
A
0

translated to ISO predicates:

simple(T) :- var(T) ; atomic(T).

var/1 it's the most basic metaprogramming device, because it's impossible to predicate (i.e. execute code, binding variables) about any clause without instancing the variables, that are many times the essential part we are interested to.

Arissa answered 28/1, 2012 at 8:42 Comment(3)
As a general remark - a definition like simple/1 is not very helpful because it merges two levels of testing into one predicate.Pothouse
@false: do you mean that the disjunction introduces an useless choice point? I don't know what 'levels of testing' are...Arissa
When you write predicates that explicitly depend on the instantiation of an argument, it is a good idea to separate the testing for instantiations var(X), nonvar(X) from the other testing. In particular, it is a good idea to treat both cases explicitly. Otherwise, it gets pretty hard to reason about a predicate. simple/1 mixes both levels. And, btw. also atom/1, integer/1 etc do this. But they are less problematic, if you do a var/nonvar test before.Pothouse

© 2022 - 2024 — McMap. All rights reserved.