1 / 8

Mode Inference

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’.

neva
Download Presentation

Mode Inference

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mode Inference Given the definition of a predicate p/N infer a calling pattern p(M1, …, MN) where each Mi 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’ Transitions: gg ?? vv ?v ?g vg

  2. Modes Are Given For Built-in Predicates ?g atom(X) ?g gg N is E ?? A = B

  3. Inferring Modes = given gg ?g sum([], 0). sum([H|T], N) :- sum(T, NT), N is NT + 1. gg ?g gg vg ?g gg

  4. 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. 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. 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). unfold r_a([], A, R) :- append([], A, R). r_a([H|T], A, R) :- rev(T, T1), append(T1, [H|A], R). simplify r_a([], A, A). r_a([H|T], A, R) :- rev(T, T1), append(T1, [H|A], R). unfold r_a([], A, R). r_a([H|T], A, R) :- r_a(T, [H|A], R). fold

  7. Preserving a Common Control Structure 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([X|_], X, []). path([H|T], X, [H|R]) :- path(T, X, R). unfold + fold path_dist([X|_], X, [], 0). path_dist([H|T], X, [H|R], D) :- path_dist(T, X, R, Dt), D is Dt + 1. What is wrong with this picture?

  8. Preserving a Common Control Structure 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([X|_], X, []). path([H|T], X, [H|R]) :- path(T, X, R). unfold + fold 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). unfold + fold 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).

More Related