useranswer, , , Prolog . 7 .
, useranswer . useranswer , , . , , , , .
. , . askable , , : askable X eats Y, ' ' eats 'Something').
, , :
?- useranswer (X eats Y, [], Answer). Is it true: Animal eats Something? yes.
Animal = peter. Something = meat. Answer = true X = peter Y = meat
useranswer, 16.4, format; Goal, ExternFormat, Question, VarsO, Variables)
Goal - , a ExternFormat Goal, : askable (Goal, ExternFormat)
, Question - Goal, ExternFormat. Variables , Goal ( ExternFormat), VarsO, , .
16.
■1,,^ yives 1What' to 'horn',
Question, [], Variables!.
Question - 'Who' gives documents to "Whom1,
Variables = [X/'Who1, Y/'Whom'].
16.4. : , "why"
%
!
I useranswer! Goal, Trace, Answer)
%
% , % Goal, .
s Trace - :- ,
useranswert Goal, Trace, Answer):-
askable; Goal, _), % ,
$ freshcopy(Goal, Copy!, Goal useranswer; Goal, Copy, Trace, Answer, 1).
|
|
% He ,
useranswer(Goal, _, _, _, N):-
N > 1, * ?
instantiate(Goal),!, % Goal
fail. % .
* , Goal 4 ?
useranswer! Goal, Copy, _, Answer, _):-wastold ' Copy, Answer, _), instance_of(Copy, Goal),!. Answer Goal
i Goal, % N
useranswer,. Goal, _, _, true, H):-wastcld; Goal, truerM),
:- Goal?
useranswerf Goal, Copy, _, Answer, _):-end_answers: Copy}, ins-ance_c£ { Copy, Goal},!, ;-
Goal fail.
% ()
useranswerf Goal, _, Trace, Answer, }:-
askuserf Goal, Trace, Answer, N). askusert Goal, Trace, Answer, K):-
askable(Goal, ExternFormat),
format (Goal, ExternFormat, Question, Variables),..
%
ask I Goal, Question, Variables, Trace, Answer, H).
ask(Goal, Question, Variables, Trace, Answer, H):-
nl,
(Variables - [],!,
wll'.<- ' 'Is it truer1) %
II. Prolog
write('Any (more) solution to:') %
),
write (Question), write('? '),
getreply(Reply),!, % yes/no/why
process! Reply, Goal, Question, Variables, Trace, Answer, N).
process(why, Goal, Question, Variables, Trace, Answer, N):-showtrace (Trace), ask(Goal, Question, Variables, Trace, Answer, N).
process) yes, Goal, _, Variables, Trace, true, N):-
nextindex (Next), % wastold
Nextl is Next + 1,
(askvars '.Variables),
assertz(wastold(Goal, true. Next)) %
freshcopyf Goal, Copy), % Goal
useranswer (Goal, Copy, Trace, Answer, Nextl) % ?).
process! no, Goal, _, _, _, false, N):-freshcopy(Goal, Copy),
wastold (Copy, true, _),!, % 'no' ,
assertzf end answers(Goal)), %
fail
nextindex (Next), % wastold
assertz { wastold(Goal, false, Next)). % 'no' ,
format' Var, Name, Name, Vars, [Var/Name | Vars]):-var (Var),!.
format(Atom, Name, Atom, Vars, Vars):-atomic(Atom),!, atomic(Name).
format(Goal, Form, Question, VarsO, Vars):-Goal =.. [Functor | Argsl], Form =.. [Functor | Forms],
formatall (Argsl, Forms, Args2, VarsO, Vars), Question =.. [Functor | Args2].
formatall ([], [], [], Vars, Vars).
formatall ([X | XL], [F | FL], [Q | QL], VarsO, Vars):-formatall(XL, FL, QL, VarsO, Varsl), formatt X, F, Q, Varsl, Vars).
askvars([ ]).
askvars([Variable/Name | Variables]):-
nl, write (Name), write(' = '),
read(Variable),
askvars Variables). showtrace([ ]): -
nl, write ('This was your question'), nl. showtrace([Goal by Rule | Trace]):-
nl, write ('To investigate, by '),
write (Rule), write (', '),
write (Goal),
|
|
showtrace(Trace). instantiate) Term):-
nurabervars (Term, 0, 0). % Term
16. 373
instance of(Tl, T2>: T2 - Tl; , 1 , 2
instance of[ Terra, Terml):- i Term lentil
freshcopyt Terml, Term2), l Terml
% numbervars! Term2, 0, _),!,
Term = Term2. % ,
I Terml - Term freshcopyt Term, FreshTerm):- % Term
% Term
asserts copyt Term)),
retract (copy! FreshTerm)},!.
nextindext Next):- wastold
retract! lastindex(Last)),!, Next is Last + 1,
assert; lastindexf Next)),
% lastindex/l, wastold/3, end_answers/i
:- assertz { lastindex(O)},
assertz! wastold (dummy, false, 0)), assertz(end answers! dummy)).
, , . , , . , , , : assert(wastold! gives documents to friends, true)).
, ,
, ,
. . ,
(
, ), , .
(X has } and % - Goall
has 1) and % - Goal2
, , ( ) Goall. -12. Goall, , Goai2 ( , , Goal2). , Goal2, - . GoaI2 . , . , Goall, Goall.
, , . :
wastold! Goal, Truthvalue, Index)
Index , . useranswer! Goal/ Trace, Answer)
374 II. Prolog
, . , useranswer, : useranswer(Goal, Trace, Answer, N)
N - . Goal, N . useranswer (Goal, Trace, Answer)
Goal. , 1, : useranswer (Goal, Trace, Answer):-useranswer (Goal, Trace, Answer, 1).
useranswer (Goal, Trace, Answer, N)
|
|
, Goal, , N . , , Goal, , . , , : end_answers(Goal)
, , : wastoldf Goal, false, Index)
useranswer .
. , . , , Goal, , , , Goal, . : wastold(Goal, false, _) .
useranswer, 16.4. , ( Goal), Goal, Goal. . : instantiated (Term)
, Term . instance_of(Term, Terml)
Terml Term; , Term , , , Terml, , . instance^of (X gives information to Y, gives information to Z) :
numbervars(Term, N, )
"" Term, ,
16.
"" N -1. , , : var/0, var/l, var/2,...
?- Term = f(X, t(a, Y, X)), numbervars (Term, 5, Ml.
: Term = f(var/5, t(a, var/6, var/5)) X = var/5 Y = var/6
M = 7
numbervars Prolog . , .
numbervars (Term, N, Nplusl):-
var (Term),!, % ?
Term = var/N,
Nplusl is N + 1. numbervars(Term, N, Mi:-
Term =.. [Functor I Args], %
numberargs { Args, , M). %
numberargs ([ ], N, ):-!. numberargs ([X I L], N, M):-
numbervars(X, , N1),
numberargs(L, N1, M).