Download presentation

Presentation is loading. Please wait.

Published byIsrael Belford Modified over 2 years ago

1
1 Logic Programming School of Informatics, University of Edinburgh Mode Inference Given the definition of a predicate p/N infer a calling pattern p(M 1, …, M N ) where each M i is the mode of the appropriate argument. A mode can be one of the following: ‘v’ Always a variable ‘g’ Always a ground term ‘?’ Not determined ‘v’ or ‘g’ ?g?gvgvggggg????vvvv?v?v Transitions:

2
2 Logic Programming School of Informatics, University of Edinburgh Modes Are Given For Built-in Predicates N is E gggg ?g?g atom(X) ?g?g A = B ????

3
3 Logic Programming School of Informatics, University of Edinburgh sum([], 0). sum([H|T], N) :- sum(T, NT), N is NT + 1. Inferring Modes gggg ?g?ggggg?g?ggggg ?g?g ggggvgvg = given

4
4 Logic Programming School of Informatics, University of Edinburgh Program Unfolding q([], []). q([H|T], R) :- t(H), q(T, R). p(L, R) :- q(L, X), r(X, R). p([], R) :- r([], R) p([H|T], R) :- t(H), q(T, X), r(X, R) unfold

5
5 Logic Programming School of Informatics, University of Edinburgh Program Folding s(A, B) :- q(A,C), r(C,B). p(A, B) :- q(A, C), r(C, B), t(B). p(A, B) :- s(A, B), t(B). fold

6
6 Logic Programming School of Informatics, University of Edinburgh Program Folding/Unfolding Example rev([], []). rev([H|T], R) :- rev(T, T1), append(T1, [H], R). append([], L, L). append([H|T], L, [H|R]) :- append(T, L, R). r_a(L, A, R) :- rev(L, L1), append(L1, A, R). r_a([], A, R) :- append([], A, R). r_a([H|T], A, R) :- rev(T, T1), append(T1, [H], L1), append(L1, A, R). r_a([], A, A). r_a([H|T], A, R) :- rev(T, T1), append(T1, [H|A], R). r_a([], A, R). r_a([H|T], A, R) :- r_a(T, [H|A], R). r_a([], A, R) :- append([], A, R). r_a([H|T], A, R) :- rev(T, T1), append(T1, [H|A], R). unfold simplify unfold fold

7
7 Logic Programming School of Informatics, University of Edinburgh Preserving a Common Control Structure path([X|_], X, []). path([H|T], X, [H|R]) :- path(T, X, R). dist([X|_], X, 0). dist([H|T], X, D) :- dist(T, X, Dt), D is Dt + 1. path_dist(L, X, P, D) :- path(L, X, P), dist(L, X, D). path_dist([X|_], X, [], 0). path_dist([H|T], X, [H|R], D) :- path_dist(T, X, R, Dt), D is Dt + 1. unfold + fold What is wrong with this picture?

8
8 Logic Programming School of Informatics, University of Edinburgh Preserving a Common Control Structure path([X|_], X, []). path([H|T], X, [H|R]) :- path(T, X, R). dist([X|_], X, 0). dist([H|T], X, D) :- dist(T, X, Dt), D is Dt + 1. path_dist(L, X, P, D) :- path(L, X, P), dist(L, X, D). path_dist([X|T], X, [], D) :- dist([X|T], X, D). path_dist([H|T], X, [H|R], D) :- path(T, X, R), dist([H|T], X, D). path_dist([X|T], X, [], 0). path_dist([X|T], X, [], D) :- dist(T, X, Dt), D is Dt + 1. path_dist([H|T], X, [H|R], D) :- path_dist(T, X, R, Dt), D is Dt + 1. path_dist([H|T], X, [H|R], D) :- path(T, X, R), dist([H|T], X, D). unfold + fold

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google