# 1 Logic Programming School of Informatics, University of Edinburgh Forcing Determinacy: Example minimum(X, Y, X) :- X =< Y. minimum(X, Y, Y) :- X > Y.

## Presentation on theme: "1 Logic Programming School of Informatics, University of Edinburgh Forcing Determinacy: Example minimum(X, Y, X) :- X =< Y. minimum(X, Y, Y) :- X > Y."— Presentation transcript:

1 Logic Programming School of Informatics, University of Edinburgh Forcing Determinacy: Example minimum(X, Y, X) :- X =< Y. minimum(X, Y, Y) :- X > Y. minimum(1, 2, X) minimum(1, 2, 1)minimum(1, 2, 2) 1 =< 21 > 2

2 Logic Programming School of Informatics, University of Edinburgh Safe Determinacy minimum(X, Y, X) :- X =< Y, !. minimum(X, Y, Y) :- X > Y. minimum(1, 2, X) minimum(1, 2, 1)minimum(1, 2, 2) 1 =< 21 > 2 !

3 Logic Programming School of Informatics, University of Edinburgh Unsafe Determinacy – Looks OK minimum(X, Y, X) :- X =< Y, !. minimum(X, Y, Y). minimum(1, 2, X) minimum(1, 2, 1)minimum(1, 2, 2) 1 =< 2 !

4 Logic Programming School of Informatics, University of Edinburgh Unsafe Determinacy – Not Always OK minimum(X, Y, X) :- X =< Y, !. minimum(X, Y, Y). minimum(1, 2, 2) minimum(1, 2, 1)minimum(1, 2, 2) Fails to match Succeeds

5 Logic Programming School of Informatics, University of Edinburgh Forcing Determinacy: Cuts p(X) :- q(X), r(X), !, s(X). p(X) :- t(X). q(1). q(2). q(3). r(2). r(3). s(2). s(3). t(4). p(X) q(X)r(X)!s(X) q(1)q(2)q(3)r(2)r(3)s(2)s(3) t(X) t(4) q(2)r(2) s(2) p(2)

6 Logic Programming School of Informatics, University of Edinburgh Tail Recursion Optimisation delete([H|T], X, [H|Rest]):- \+ H = X, !, delete(T, X, Rest). delete([H|T], H, Result) :- delete(T, H, Result). delete([], _, []). delete([a,b,c], c, R) delete([a|[b,c]], c, [a|Rest]) delete([b,c], c, Rest) \+ a = c ! 1 2 3 2 3 delete([b|[c]], c, [b|Rest’]) delete([c], c, Rest’) \+ a = c ! 2 3 1 1 etc We only need to keep track of this goal

7 Logic Programming School of Informatics, University of Edinburgh Larger List Example: Agenda Search a bc d e f g [[a]] [[b,a],[c,a]] [[d,b,a],[f,b,a],[c,a]] [[e,d,b,a],[f,b,a],[c,a]] [[f,b,a],[c,a]] Depth first search looking for ‘f’

8 Logic Programming School of Informatics, University of Edinburgh Larger List Example: Agenda Search a bc d e f g [[a]] [[b,a],[c,a]] [[c,a],[d,b,a],[f,b,a]] [[d,b,a],[f,b,a],[g,c,a]] [[f,b,a],[g,c,a],[e,d,b,a]] Breadth first search looking for ‘f’

9 Logic Programming School of Informatics, University of Edinburgh Larger List Example: Agenda Search search([Agenda|T], _, Target) :- at_target(Agenda, Target). search([Agenda | T], Style, Target) :- \+ at_target(Agenda, Target), extend_agenda(Agenda, NewAgendas), merge_agendas(NewAgendas, T, Style, New), search(New, Style, Target).

10 Logic Programming School of Informatics, University of Edinburgh Larger List Example: Agenda Search at_target([Target | _], Target). extend_agenda([H | T], NewAgendas) :- setof([X,H | T], linked(H,X), NewAgendas). merge_agendas(A1, A2, df, New), append(A1, A2, New). merge_agendas(A1, A2, bf, New), append(A2, A1, New).

Similar presentations