Мы собираемся создать правило newrule(X) :- w,x,y,z(X)
.
Тело правила - это кортеж, конструкция в виде (w, x, y ...).
Для разных длин тела, начиная с без тела:
assert(goal).
assert(goal:-cond).
assert(goal:-(cond1,cond2)).
Оператор кортежа - запятая (`, '), как в ', '(a, b) ==(а, б) .
%%%%
%%%% Name: runtime.pl -- Runtime rule insertion.
%%%%
create_a_rule :-
Cond=[w,x,y,z(X)],
Head=newrule(X),
list_to_tuple(Cond,Body),
dynamic(Head),
assert(Head :- Body),
listing(Head).
/*
This is a [l,i,s,t], and this is a (t,u,p,l,e).
Convertng list to tuple:
[] -> undefined
[x] -> (x) == x
[x,y] -> (x,y).
[x,y,z..whatever] = (x,y,z..whatever)
*/
list_to_tuple([],_) :-
ValidDomain='[x|xs]',
Culprit='[]',
Formal=domain_error(ValidDomain, Culprit),
Context=context('list_to_tuple','Cannot create empty tuple!'),
throw(error(Formal,Context)).
list_to_tuple([X],X).
list_to_tuple([H|T],(H,Rest_Tuple)) :-
list_to_tuple(T,Rest_Tuple).
:- create_a_rule.
:- listing(newrule).
-
Есть два объявления.Первый листинг является результатом вызова listing()
в create_a_rule()
.Второй листинг взят из команды listing()
в последней строке источника.
?- [runtime].
:- dynamic newrule/1.
newrule(A) :-
w,
x,
y,
z(A).
:- dynamic newrule/1.
newrule(A) :-
w,
x,
y,
z(A).
% runtime compiled 0.01 sec, 1,448 bytes
true.