| 1 | |
|---|
| 2 | % example adapted from: |
|---|
| 3 | % Programming Language Prolog Part 2, Modules |
|---|
| 4 | % Committee Draft - January 14, 1998 X3J17/97/5 |
|---|
| 5 | |
|---|
| 6 | :- object(tracer). |
|---|
| 7 | |
|---|
| 8 | :- info([ |
|---|
| 9 | version is 2.1, |
|---|
| 10 | author is 'Paulo Moura', |
|---|
| 11 | date is 2006/9/17, |
|---|
| 12 | comment is 'Tracer for a goal call, exit, and fail ports.']). |
|---|
| 13 | |
|---|
| 14 | :- public(trace/1). |
|---|
| 15 | :- meta_predicate(trace(::)). % changes interpretation of meta-calls on trace/1 clauses |
|---|
| 16 | :- mode(trace(+callable), zero_or_more). |
|---|
| 17 | :- info(trace/1, [ |
|---|
| 18 | comment is 'Traces goal execution.', |
|---|
| 19 | argnames is ['Goal']]). |
|---|
| 20 | |
|---|
| 21 | trace(Goal) :- |
|---|
| 22 | write('call: '), writeq(Goal), nl, |
|---|
| 23 | call(Goal), % Goal is called in the context of the object sending the message trace/1 |
|---|
| 24 | write('exit: '), writeq(Goal), nl. |
|---|
| 25 | |
|---|
| 26 | trace(Goal) :- |
|---|
| 27 | write('fail: '), writeq(Goal), nl, |
|---|
| 28 | fail. |
|---|
| 29 | |
|---|
| 30 | :- end_object. |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | % sort code adapted from an example on the SICStus Prolog User Manual |
|---|
| 34 | % meta-predicate example taken from Prolog Part 2, Modules - Committee Draft |
|---|
| 35 | |
|---|
| 36 | :- object(sort(_Type)). |
|---|
| 37 | |
|---|
| 38 | :- info([ |
|---|
| 39 | version is 1.0, |
|---|
| 40 | author is 'Paulo Moura', |
|---|
| 41 | date is 2000/7/24, |
|---|
| 42 | comment is 'List sorting parameterized by the type of the list elements.']). |
|---|
| 43 | |
|---|
| 44 | :- uses(list, [append/3]). % calls to append(...) will be translated to list::append(...) |
|---|
| 45 | :- uses(tracer, [trace/1]). % calls to trace(...) will be translated to tracer::trace(...) |
|---|
| 46 | |
|---|
| 47 | :- calls(comparingp). |
|---|
| 48 | |
|---|
| 49 | :- public(sort/2). |
|---|
| 50 | :- mode(sort(+list, -list), one). |
|---|
| 51 | :- info(sort/2, [ |
|---|
| 52 | comment is 'Sorts a list in ascending order (quicksort algorithm).', |
|---|
| 53 | argnames is ['List', 'Sorted']]). |
|---|
| 54 | |
|---|
| 55 | :- private(partition/4). |
|---|
| 56 | :- mode(partition(+list, +nonvar, -list, -list), one). |
|---|
| 57 | :- info(partition/4, [ |
|---|
| 58 | comment is 'Partition a list in two lists containing the elements smaller and larger than a pivot.', |
|---|
| 59 | argnames is ['List', 'Pivot', 'Small', 'Large']]). |
|---|
| 60 | |
|---|
| 61 | sort([], []). |
|---|
| 62 | |
|---|
| 63 | sort([Head| Tail], Sorted) :- |
|---|
| 64 | trace(partition(Tail, Head, Small, Large)), |
|---|
| 65 | trace(sort(Small, Sorted1)), |
|---|
| 66 | trace(sort(Large, Sorted2)), |
|---|
| 67 | append(Sorted1, [Head| Sorted2], Sorted). |
|---|
| 68 | |
|---|
| 69 | partition([], _, [], []). |
|---|
| 70 | |
|---|
| 71 | partition([Head| Tail], Pivot, Small, Large) :- |
|---|
| 72 | parameter(1, Type), |
|---|
| 73 | ( Type::(Head < Pivot) -> |
|---|
| 74 | Small = [Head| Small1], Large = Large1 |
|---|
| 75 | ; Small = Small1, Large = [Head| Large1] |
|---|
| 76 | ), |
|---|
| 77 | partition(Tail, Pivot, Small1, Large1). |
|---|
| 78 | |
|---|
| 79 | :- end_object. |
|---|