2005-12-24 18:07:41 +00:00
|
|
|
|
|
|
|
% example adopted from:
|
|
|
|
% Programming Language Prolog Part 2, Modules
|
|
|
|
% Committee Draft - January 14, 1998 X3J17/97/5
|
|
|
|
|
|
|
|
:- object(tracer).
|
|
|
|
|
|
|
|
:- info([
|
2006-11-07 17:11:47 +00:00
|
|
|
version is 2.1,
|
2005-12-24 18:07:41 +00:00
|
|
|
author is 'Paulo Moura',
|
2006-11-07 17:11:47 +00:00
|
|
|
date is 2006/9/17,
|
2005-12-24 18:07:41 +00:00
|
|
|
comment is 'Tracer for a goal call, exit, and fail ports.']).
|
|
|
|
|
|
|
|
:- public(trace/1).
|
2006-11-07 17:11:47 +00:00
|
|
|
:- meta_predicate(trace(::)). % changes interpretation of meta-calls on trace/1 clauses
|
2005-12-24 18:07:41 +00:00
|
|
|
:- mode(trace(+callable), zero_or_more).
|
|
|
|
:- info(trace/1, [
|
|
|
|
comment is 'Traces goal execution.',
|
|
|
|
argnames is ['Goal']]).
|
|
|
|
|
|
|
|
trace(Goal) :-
|
|
|
|
write('call: '), writeq(Goal), nl,
|
|
|
|
call(Goal), % Goal is called in the context of the object sending the message trace/1
|
|
|
|
write('exit: '), writeq(Goal), nl.
|
|
|
|
|
|
|
|
trace(Goal) :-
|
|
|
|
write('fail: '), writeq(Goal), nl,
|
|
|
|
fail.
|
|
|
|
|
|
|
|
:- end_object.
|
|
|
|
|
|
|
|
|
|
|
|
% sort code adopted from an example on the SICStus Prolog User Manual
|
2006-11-07 17:11:47 +00:00
|
|
|
% meta-predicate example taken from Prolog Part 2, Modules - Committee Draft
|
2005-12-24 18:07:41 +00:00
|
|
|
|
|
|
|
:- object(sort(_Type)).
|
|
|
|
|
|
|
|
:- info([
|
|
|
|
version is 1.0,
|
|
|
|
author is 'Paulo Moura',
|
|
|
|
date is 2000/7/24,
|
|
|
|
comment is 'List sorting parameterized by the type of the list elements.']).
|
|
|
|
|
|
|
|
:- uses(list, [append/3]). % calls to append(...) will be translated to list::append(...)
|
|
|
|
:- uses(tracer, [trace/1]). % calls to trace(...) will be translated to tracer::trace(...)
|
|
|
|
|
|
|
|
:- calls(comparingp).
|
|
|
|
|
|
|
|
:- public(sort/2).
|
|
|
|
:- mode(sort(+list, -list), one).
|
|
|
|
:- info(sort/2, [
|
|
|
|
comment is 'Sorts a list in ascending order (quicksort algorithm).',
|
|
|
|
argnames is ['List', 'Sorted']]).
|
|
|
|
|
|
|
|
:- private(partition/4).
|
|
|
|
:- mode(partition(+list, +nonvar, -list, -list), one).
|
|
|
|
:- info(partition/4, [
|
|
|
|
comment is 'Partition a list in two lists containing the elements smaller and larger than a pivot.',
|
|
|
|
argnames is ['List', 'Pivot', 'Small', 'Large']]).
|
|
|
|
|
|
|
|
sort([], []).
|
|
|
|
|
|
|
|
sort([Head| Tail], Sorted) :-
|
|
|
|
trace(partition(Tail, Head, Small, Large)),
|
|
|
|
trace(sort(Small, Sorted1)),
|
|
|
|
trace(sort(Large, Sorted2)),
|
|
|
|
append(Sorted1, [Head| Sorted2], Sorted).
|
|
|
|
|
|
|
|
partition([], _, [], []).
|
|
|
|
|
|
|
|
partition([Head| Tail], Pivot, Small, Large) :-
|
|
|
|
parameter(1, Type),
|
|
|
|
( Type::(Head < Pivot) ->
|
|
|
|
Small = [Head| Small1], Large = Large1
|
|
|
|
; Small = Small1, Large = [Head| Large1]
|
|
|
|
),
|
|
|
|
partition(Tail, Pivot, Small1, Large1).
|
|
|
|
|
|
|
|
:- end_object.
|