Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.

Similar presentations


Presentation on theme: "Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language."— Presentation transcript:

1 Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language. Some Prolog specifics: Some Prolog specifics: short clauses, predicates short clauses, predicates If meaning in comments must use “and”, then break into separate preds. If meaning in comments must use “and”, then break into separate preds. meaningful names for predicates, constants, variables meaningful names for predicates, constants, variables helps programmer understand the meaning of their code helps programmer understand the meaning of their code good indentation good indentation use the same conventions throughout program use the same conventions throughout program cuts: use green cuts rather than red cuts: use green cuts rather than red if possible, use not, one, -> instead of cuts if possible, use not, one, -> instead of cuts try to avoid assert/retract unless absolutely required try to avoid assert/retract unless absolutely required be careful to restore program state (database) when necessary be careful to restore program state (database) when necessary comments: comments: I/O convention of predicates I/O convention of predicates essentials of algorithm essentials of algorithm special features, shortcomings, assumptions about input,... special features, shortcomings, assumptions about input,...

2 Efficiency issues Declarative vs procedural Declarative vs procedural a difficult balance: clearest solutions can be the most inefficient a difficult balance: clearest solutions can be the most inefficient sometimes must encode a clear solution efficiently, using procedural considerations sometimes must encode a clear solution efficiently, using procedural considerations Algorithm vs implementation Algorithm vs implementation a quicksort is faster than a bubble sort, no matter how you implement it! a quicksort is faster than a bubble sort, no matter how you implement it! A fast algorithm can be implemented inefficiently, however. A fast algorithm can be implemented inefficiently, however. Data structures Data structures related to algorithms related to algorithms a better data structure can naturally result in a faster implementation a better data structure can naturally result in a faster implementation eg. a sorted binary tree permits faster data extraction than an unsorted linear list eg. a sorted binary tree permits faster data extraction than an unsorted linear list Nondeterminism vs determinism Nondeterminism vs determinism reduce backtracking: avoid needless searching: once, if-then-else, cuts reduce backtracking: avoid needless searching: once, if-then-else, cuts Backtracking requires extra memory. Backtracking requires extra memory. consult vs compile consult vs compile depending on implementation, compiling programs can give upwards of a 10x speed increase depending on implementation, compiling programs can give upwards of a 10x speed increase (lose debugging info, however) (lose debugging info, however)

3 Memory and recursion recursion requires memory recursion requires memory stack used to save tree for return stack used to save tree for return extra usage for backtracking... extra usage for backtracking... p(...) :- do_things(...), % Memory used here to do_more_things(...), % save tree for backtracking. do_more_things(...), % save tree for backtracking. p(...). % Recursion. This is an examples of tail recursion: last goal is recursive call, and goals before it are deterministic (backtracking will fail). This is an examples of tail recursion: last goal is recursive call, and goals before it are deterministic (backtracking will fail). To optimize, do this: To optimize, do this: p(...) :- do_things(...), do_more_things(...), do_more_things(...), !, % This throws away tree before call. p(...). % Recursion.

4 Efficiency: memory This can still cause problems in very large computations. Sometimes, various memory usage before the cut can be enormous. This can still cause problems in very large computations. Sometimes, various memory usage before the cut can be enormous. Some Prologs such as Sicstus have a builtin memory management optimizer called “garbage_collect” Some Prologs such as Sicstus have a builtin memory management optimizer called “garbage_collect” garbage collection important for symbolic languages (and Java!): finds unused memory (eg. unused atoms, freed clauses, etc.), and puts it back on heap for later usage garbage collection important for symbolic languages (and Java!): finds unused memory (eg. unused atoms, freed clauses, etc.), and puts it back on heap for later usage done automatically at periodic times. Unfortunately, might not happen when required! done automatically at periodic times. Unfortunately, might not happen when required! calling garbage_collect lets programmer ensure of optimal memory use calling garbage_collect lets programmer ensure of optimal memory use p(...) :- do_things(...), garbage_collect, do_more_things(...), do_more_things(...),garbage_collect, !, % this throws away tree before call p(...). % recursion

5 Last-clause determinacy Sicstus compiler will automatically “insert” a cut into the first goal of the last clause of a recursive predicate. Sicstus compiler will automatically “insert” a cut into the first goal of the last clause of a recursive predicate. So this is not required... So this is not required... p(...) :-... p(...) :- % last clause of p. !, %  unnecessary! !, %  unnecessary! (do something), p(...). 5COSC 2P93 Prolog: Lisp

6 Efficiency: execution profiling Profile: run-time execution statistics Profile: run-time execution statistics See sect. 9.2, Sicstus manual See sect. 9.2, Sicstus manual Scheme: Scheme: ?- [Load some code.] ?- prolog_flag(profiling,_,on). ?- [Run some queries.] ?- prolog_flag(profiling,_,off). ?- print_profile.

7 Profiling insns try/retry called name ---------------------------------------------------------------- *13694/14300 user:remove_nth/4 ← callers: remove_nth 13694 times *13694/14300 user:remove_nth/4 ← callers: remove_nth 13694 times *606/14300 user:select_random/3 ← select_random 606 times *606/14300 user:select_random/3 ← select_random 606 times 97676 606 *14300 user:remove_nth/4 ← remove_nth called 14300 times 97676 606 *14300 user:remove_nth/4 ← remove_nth called 14300 times *13694/14300 user:remove_nth/4 ← called preds: remove_nth 13694 times *13694/14300 user:remove_nth/4 ← called preds: remove_nth 13694 times---------------------------------------------------------------- *101/5050 user:lotto649/1 *101/5050 user:lotto649/1 *4949/5050 user:make_intlist/3 *4949/5050 user:make_intlist/3 74841 9999 *5050 user:make_intlist/3 74841 9999 *5050 user:make_intlist/3 *4949/5050 user:make_intlist/3 *4949/5050 user:make_intlist/3---------------------------------------------------------------- *101/2027 user:lotto_loop/9 *101/2027 user:lotto_loop/9 *1926/2027 user:writelist/1 *1926/2027 user:writelist/1 18417 6474 *2027 user:writelist/1 18417 6474 *2027 user:writelist/1 *1926/2027 user:writelist/1 *1926/2027 user:writelist/1--etc 7COSC 2P93 Prolog: Lisp

8 Efficiency: determinism Nondeterminacy: multiple solutions via backtracking Nondeterminacy: multiple solutions via backtracking a major feature of Prolog: search a major feature of Prolog: search also a source of inefficiency when multiple solutions not needed also a source of inefficiency when multiple solutions not needed possible to get erroneous solns on backtracking as well possible to get erroneous solns on backtracking as well Sicstus Prolog indexes clauses based on first argument Sicstus Prolog indexes clauses based on first argument a hash table created, with first arg value as hash key a hash table created, with first arg value as hash key interpreter will very quickly unify a call with its “hashed” clause; no need for unification on inappropriate calls. interpreter will very quickly unify a call with its “hashed” clause; no need for unification on inappropriate calls. But if a clause has a variable 1st arg, this won’t work But if a clause has a variable 1st arg, this won’t work When possible, make predicates determinate When possible, make predicates determinate each clause takes care of one case, given in 1st arg each clause takes care of one case, given in 1st arg Result: Result: faster execution faster execution less memory usage less memory usage

9 Example: determinism % make integer list... intlist(N, L) :- N > 0, N > 0, make_intlist(0, N, L). make_intlist(0, N, L). make_intlist(M, N, [ ]) :- M > N. M > N. make_intlist(M, N, [M|L2]) :- M =< N, M =< N, M2 is M+1, M2 is M+1, make_intlist(M2, N, L2). make_intlist(M2, N, L2). Notice how 1 st arg to make_intlist/3 is variable. Notice how 1 st arg to make_intlist/3 is variable. So Prolog must use inefficient unification to match calls with clauses. So Prolog must use inefficient unification to match calls with clauses. 9COSC 2P93 Prolog: Lisp

10 Determinism intlist(N, L) :- N > 0, N > 0, make_intlist(L, 0, N). make_intlist(L, 0, N). make_intlist([ ], M, N) :- M > N. M > N. make_intlist([M|L2], M, N) :- M =< N, M =< N, M2 is M+1, M2 is M+1, make_intlist(L2, M2, N). make_intlist(L2, M2, N). Here, we’ve moved 3 rd arg (solution list) to 1 st position. This version is faster: calls to make_intlist can be resolved instantly using 1 st argument value. 10COSC 2P93 Prolog: Lisp

11 Determinacy checker command: spdet file_name command: spdet file_name If we call it on “inefficient” make_intlist... If we call it on “inefficient” make_intlist... * Non-determinate: user:make_intlist/3 (clause 1) * Indexing cannot distinguish this from clause 2. spdet –D (file) will also generate declarations to use... spdet –D (file) will also generate declarations to use... :- nondet user:make_intlist/3. 11COSC 2P93 Prolog: Lisp


Download ppt "Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language."

Similar presentations


Ads by Google