110 likes | 255 Views
Μεταφραστές. Λύσεις με τη βοήθεια του Χρήστη ( Query the User). solve (true) . solve ((A,B)) :- solve (A), solve (B). solve (A) :- clause (A, B), solve (B). solve (A) :- askable ( A), not known (A), ask (A, Answer), respond (Answer, A).
E N D
Λύσεις με τη βοήθεια του Χρήστη(Query the User) solve (true). solve ((A,B)) :- solve (A), solve (B). solve (A) :- clause (A, B), solve (B). solve (A) :- askable(A), not known (A), ask (A, Answer), respond (Answer, A). ask (A, Answer) :- write (A), write («?»), read (Answer). respond (yes, A) :- assert (A). respond (no, A) :- assert (untrue(A)), fail. known (A) :- A. known (A) :- untrue (A).
Παράδειγμα 1 (Query the User) entitled (X, Y) :- unemployed (X) age (X, Z), Z > 18, work_period (X, T), T > 6, Y is (200 * T) / 12. entitled (X, Y) :- student (X), identity_number (X, X1), passed_semester (X1), special_reading_applies (X, Z), calculate_sum (Z,Y). special_read_applies(X, economic) :- poor (X). special_read_applies(X, performance) :- grade (X) >> 8.5 calculate (economic, Y) :- Y=150. calculate (performance, Y) :- Y=75. askable (age (X,Υ)). askable (identity_number (X. Y)). askable (X >> Y).
Παράδειγμα 2 should_take (Person, Drug) :- complains_of (Person, Complaint), suppresses (Drug, Complaint), not unsuitable (Drug, Person). askable (complains_of (Χ, Y)). ?solve (should_take (peter, Drug)), . . . complains_of (Peter, Headache) ? YES / NO
Επεξήγηση Αποτυχίας %solve (Goal, Result, Proof) solve ((A, B), Result, (ProofA, ProofB)) :- solve_one (A, ResultA, ProofA), solve_and (ResultA, B, Result, ProofB). solve_one (A, yes, (A is a fact)) :- clause (A, B), B==true. solve_one (A, Result, (A follows from Proof)) :- clause (A, B), B/==true, solve (B, Result, Proof). solve_one (A, no, (no rule for A)) :- not clause (A, B)). solve_and (no, B, no, (B is unsearched)). solve_and (yes, B, Result, ProofB) :- solve (B, Result, ProofB).
Ερωτήσεις «γιατί» solve (G) :- solve (G, []). solve (true, Rules). solve ((A, B), Rules) :- solve (A, Rules), solve (B, Rules). solve (A, Rules) :- clause (A, B), solve (B, [rule (A, B) | Rules]) solve (A, Rules) :- askable (A), not known (A), ask (A, Answer), respond (Answer, A, Rules). ask (A, Answer) :- write (A), write (‘?’), read (Answer). respond (yes, A, Rules) :- assert (A). respond (no, A, Rules) :- assert (untrue (A)), fail respond (why, A, [Rule|Rules]) :- display_rule (Rule), ask (A, Answer), respond (Answer, A, Rules). respond (why, A, []) :- writeln ([‘No more explanation possible’]), ask (A, Answer), respond (Answer, A, []).
Ερωτήσεις «γιατί» respond (yes, A, Rules) :- assert (A). respond (no, A, Rules) :- assert (untrue (A)), fail. respond (why, A, [Rule|Rules]) :- display_rule (Rule), ask (A, Answer), respond (Answer, A, Rules). respond (why, A, []) :- writeln ([‘No more explanation possible’]), ask (A, Answer), respond (Answer, A, []).
Ερωτήσεις «γιατί» display (rule (A, B)) :- write (‘if’), write_conjuction (B), writeln ([‘Then’, A]). write_conjuction ((A, B)) :- write (A), write (‘and’), write_conjunction (B). write_conjuction (A) :- write (A), nl.
Bounded Solve % bounded_solve (Goal,Depth) – Ισχύει όταν ο στόχος Goal έχει απόδειξη βάθους μικρότερου ή ισου του Depth. bounded_solve (true,X) bounded_ solve ((A,B), X):- bounded_solve(A,X), bounded_solve(B,X). bounded_solve (A,X):- system (A),A. bounded_solve (A,X):- X ≥ 0, clause (A,B), X1 is X-1,bounded_solve(B,X1). % bounded_solve (Goal,Depth,Proof). bounded_solve (true,X,true). bounded_solve (A,B),X, (ProofA, ProofB)):- bounded_solve (A,X,ProofA), bounded_solve (B,X,ProofB). bounded_solve (A,X,(A is a system predicate)):- system(A),A. bounded_solve (A,X,(A follows from Proof)):- X ≥ 0, clause (A, B), X1 is X-1, bounded_solve (B,X1 ,Proof).
Προσομοίωση με επεξήγηση shell <-- prompt, read(G), do(G). prompt <-- write ('Next command?'). do (exit) < -- ! do(G) <-- ground(G), !, answer_ground(G), shell. do(G) <-- answer(G), shell. answer_ground(G) <-- solve2(G,Proof), !, write('Yes'), nl, write('Would you like an explanation?’), nl, read (Answer), act(Answer,Proof) . answer_ground(G) <-- write ('No'), nl.
Προσομοίωση με επεξήγηση (συν.) answer(G) <-- solve2(G,Proof), !, write(G), nl, write('Would you like an explanation?'), nl, read(Answer) , act(Answer, Proof), fail. answer(G) < -- write('No (more) solutions'), nl. act(yes,Proof) <-- present(Proof), nl. act(no, Proof). solve2(true,true) solve2((A,B), (ProofA, ProofB)): - solve2(A, Proof A), solve2 (B, ProofB). solve2((A, (A <-- true)):- clause(A,B), B==true, solve2(A, (A <-- Proof)):- clause(A,B), B=/=true,solve2(B,Proof).