These predicates enable a term to be assembled from its component parts, or split into its component parts, or copied.
functor/3
functor(Term, Name, Arity)
is true iff:
Term
is a compound term with a functor whose identifier is Name
and arity Arity
, orTerm
is an atomic term equal to Name
and Arity
0.
Templates and modes for the predicate are as follows:
functor(-nonvar, +atomic, +integer) functor(+nonvar, ?atomic, ?integer)
Let's start with some simple tests verifying success of failure of single goals.
jTrolog.engine.SimpleGoalFixture | ||
goal | success() | exception() |
functor(foo(a, b, c), foo, 3). | true | no exception |
functor(foo(a), foo, 2). | false | no exception |
functor(foo(a), fo, 1). | false | no exception |
functor([_|_], '.', 2). | true | no exception |
functor([], [], 0). | true | no exception |
functor(X, Y, 3). | false | instantiation_error |
functor(X, foo, N). | false | instantiation_error |
functor(X, foo, a). | false | type_error(integer, a) |
functor(F, 1.5, 1). | false | type_error(atom, 1.5) |
functor(F, foo(a), 1). | false | type_error(atomic, foo(a)) |
current_prolog_flag(max_arity, A), X is A + 1, functor(T, foo, X). | false | representation_error(max_arity) |
Minus_1 is 0 - 1, functor(F, foo, Minus_1). | false | domain_error(not_less_than_zero, -1) |
Now we run some tests also verifying the unification for some of the variables in goals.
First of all, let's start an appropriate fixture containing an engine.
fit.ActionFixture | |
start | jTrolog.engine.EngineFixture |
Then, ask the engine to solve a query, and check variable bindings.
fit.ActionFixture | ||
enter | query | functor(foo(a, b, c), X, Y). |
check | hasSolution | true |
enter | variable | X |
check | binding | foo |
enter | variable | Y |
check | binding | 3 |
enter | query | functor(X, foo, 3). |
check | hasSolution | true |
enter | variable | X |
check | binding | foo(_, _, _) |
enter | query | functor(X, foo, 0). |
check | hasSolution | true |
enter | variable | X |
check | binding | foo |
enter | query | functor(mats(A, B), A, B). |
check | hasSolution | true |
enter | variable | A |
check | binding | mats |
enter | variable | B |
check | binding | 2 |
enter | query | functor(1, X, Y). |
check | hasSolution | true |
enter | variable | X |
check | binding | 1 |
enter | variable | Y |
check | binding | 0 |
enter | query | functor(X, 1.1, 0). |
check | hasSolution | true |
enter | variable | X |
check | binding | 1.1 |
The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.
jTrolog.engine.PrologActionFixture | ||
enter | query | functor(X, Y, 3). |
check | hasSolution | false |
check | exception | instantiation_error |
enter | query | functor(X, foo, N). |
check | hasSolution | false |
check | exception | instantiation_error |
enter | query | functor(X, foo, a). |
check | hasSolution | false |
check | exception | type_error(integer, a) |
enter | query | functor(F, 1.5, 1). |
check | hasSolution | false |
check | exception | type_error(atom, 1.5) |
enter | query | functor(F, foo(a), 1). |
check | hasSolution | false |
check | exception | type_error(atomic, foo(a)) |
enter | query | current_prolog_flag(max_arity, A), X is A + 1, functor(T, foo, X). |
check | hasSolution | false |
check | exception | representation_error(max_arity) |
enter | query | Minus1 is 0 - 1, functor(F, foo, Minus1). |
check | hasSolution | false |
check | exception | domain_error(not_less_than_zero, -1) |
arg/3
arg(N, Term, Arg)
is true iff the N
th argument of Term
is Arg
.
Templates and modes for the predicate are as follows:
arg(+integer, +compound_term, ?term)
Let's start with some simple tests verifying success of failure of single goals.
jTrolog.engine.SimpleGoalFixture | ||
goal | success() | exception() |
arg(1, foo(a, b), a). | true | no exception |
arg(1, foo(a, b), b). | false | no exception |
arg(0, foo(a, b), foo). | false | no exception |
arg(3, foo(3, 4), N). | false | no exception |
arg(X, foo(a, b), a). | false | instantiation_error |
arg(1, X, a). | false | instantiation_error |
arg(0, atom, A). | false | type_error(compound, atom) |
arg(0, 3, A). | false | type_error(compound, 3) |
arg(1, foo(X), u(X)). |
Now we run some tests also verifying the unification for some of the variables in goals.
We first start an appropriate fixture containing an engine.
fit.ActionFixture | |
start | jTrolog.engine.EngineFixture |
Then, ask the engine to solve a query, and check variable bindings.
fit.ActionFixture | ||
enter | query | arg(1, foo(a, b), X). |
check | hasSolution | true |
enter | variable | X |
check | binding | a |
enter | query | arg(1, foo(X, b), a). |
check | hasSolution | true |
enter | variable | X |
check | binding | a |
enter | query | arg(1, foo(X, b), Y). |
check | hasSolution | true |
enter | variable | X |
check | binding | Y |
The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.
jTrolog.engine.PrologActionFixture | ||
enter | query | arg(X, foo(a, b), a). |
check | hasSolution | false |
check | exception | instantiation_error |
enter | query | arg(1, X, a). |
check | hasSolution | false |
check | exception | instantiation_error |
enter | query | arg(0, atom, a). |
check | hasSolution | false |
check | exception | type_error(compound, atom) |
enter | query | arg(0, 3, a). |
check | hasSolution | false |
check | exception | type_error(compound, 3) |
=../2
(univ)'=..'(Term, List)
is true iff:
Term
is an atomic term and List
is the list whose only element is Term
, orTerm
is a compound term and List
is the list whose head is the functor name of Term
and whose tail is a list of the arguments of Term
Templates and modes for the predicate are as follows:
'=..'(+nonvar, ?list) '=..'(-nonvar, +list)
Note that =..
is a predefined operator.
Let's start with some simple tests verifying success of failure of single goals.
jTrolog.engine.SimpleGoalFixture | ||
goal | success() | exception() |
'=..'(foo(a, b), [foo, a, b]). | true | no exception |
'=..'(1, [1]). | true | no exception |
'=..'(foo(a, b), [foo, b, a]). | false | no exception |
'=..'(X, Y). | false | instantiation_error |
'=..'(X, [foo, a | Y]). | false | instantiation_error |
'=..'(X, [foo|bar]). | false | type_error(list, [foo|bar]) |
'=..'(X, [Foo, bar]). | false | instantiation_error |
'=..'(X, [3, 1]). | false | type_error(atom, 3) |
'=..'(X, [1.1, foo]). | false | type_error(atom, 1.1) |
'=..'(X, [a(b), 1]). | false | type_error(atom, a(b)) |
'=..'(X, 4). | false | type_error(list, 4) |
'=..'(f(X), [f, u(X)]). |
Let's now run some tests also verifying the unification for some of the variables in goals.
We first start an appropriate fixture containing an engine.
fit.ActionFixture | |
start | jTrolog.engine.EngineFixture |
Then, ask the engine to solve a query, and check variable bindings.
fit.ActionFixture | ||
enter | query | '=..'(X, [foo, a, b]). |
check | hasSolution | true |
enter | variable | X |
check | binding | foo(a, b) |
enter | query | '=..'(foo(a, b), L). |
check | hasSolution | true |
enter | variable | L |
check | binding | [foo, a, b] |
enter | query | '=..'(foo(X, b), [foo, a, Y]). |
check | hasSolution | true |
enter | variable | X |
check | binding | a |
enter | variable | Y |
check | binding | b |
The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.
jTrolog.engine.PrologActionFixture | ||
enter | query | '=..'(X, Y). |
check | hasSolution | false |
check | exception | instantiation_error |
enter | query | '=..'(X, [foo, a | Y]). |
check | hasSolution | false |
check | exception | instantiation_error |
enter | query | '=..'(X, [foo | bar]). |
check | hasSolution | false |
check | exception | type_error(list, [foo|bar]) |
enter | query | '=..'(X, [Foo, bar]). |
check | hasSolution | false |
check | exception | instantiation_error |
enter | query | '=..'(X, [3, 1]). |
check | hasSolution | false |
check | exception | type_error(atom, 3) |
enter | query | '=..'(X, [1.1, foo]). |
check | hasSolution | false |
check | exception | type_error(atom, 1.1) |
enter | query | '=..'(X, [a(b), 1]). |
check | hasSolution | false |
check | exception | type_error(atom, a(b)) |
enter | query | '=..'(X, 4). |
check | hasSolution | false |
check | exception | type_error(list, 4) |
copy_term/2
copy_term(Term1, Term2)
is true iff Term2
unifies with a term T
which is a renamed copy of Term1
.
Templates and modes for the predicate are as follows:
copy_term(?term, ?term)
Let's start with some simple tests verifying success of failure of single goals.
jTrolog.engine.SimpleGoalFixture | ||
goal | success() | exception() |
copy_term(X, Y). | true | no exception |
copy_term(X, 3). | true | no exception |
copy_term(_, a). | true | no exception |
copy_term(_, _). | true | no exception |
copy_term(a, b). | false | no exception |
copy_term(a+X, X+b), copy_term(a+X, X+b). | false | no exception |
copy_term(demoen(X, X), demoen(Y, f(Y))). | no exception |
Let's now run some tests also verifying the unification for some of the variables in goals.
We first start an appropriate fixture containing an engine.
fit.ActionFixture | |
start | jTrolog.engine.EngineFixture |
Then, ask the engine to solve a query, and check variable bindings.
fit.ActionFixture | ||
enter | query | copy_term(a+X, X+b). |
check | hasSolution | true |
enter | variable | X |
check | binding | a |
enter | query | copy_term(X+X+Y, A+B+B). |
check | hasSolution | true |
enter | variable | A |
check | binding | B |
Note that there are no error or exception cases for this predicate.
The results of the tests for Term creation and decomposition are as follows:
fit.Summary |