File: parametric.lgt

package info (click to toggle)
yap 5.1.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,124 kB
  • ctags: 14,650
  • sloc: ansic: 122,796; perl: 22,545; sh: 3,768; java: 1,277; makefile: 1,191; xml: 739; tcl: 624; lisp: 142; awk: 9
file content (215 lines) | stat: -rw-r--r-- 4,636 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

/*	This example illustrates how to associate a set of predicates with a 
	compound term.   Parameters can be accessed from within an object by 
	using the execution-context built-in methods this/1 and parameter/2; 
	both alternatives are illustrated below.
*/



/* The first two parametric objects represent time and date values as 
	compound terms using the object's identifiers.
*/


:- object(date(_Year, _Month, _Day)).

	:- info([
		version is 1.1,
		author is 'Paulo Moura',
		date is 2005/9/5,
		comment is 'Dates as parametric objects.',
		parnames is ['Year', 'Month', 'Day']]).

	:- public(year/1).
	:- mode(year(?integer), one).

	:- public(month/1).
	:- mode(month(?integer), one).

	:- public(day/1).
	:- mode(day(?integer), one).

	:- public(today/0).
	:- mode(today, one).

	:- public(leap_year/0).
	:- mode(leap_year, zero_or_one).

	year(Year) :-
		parameter(1, Year).

	month(Month) :-
		parameter(2, Month).

	day(Day) :-
		parameter(3, Day).

	today :-
		{'$lgt_current_date'(Year, Month, Day)},	% defined in the config files
		parameter(1, Year),
		parameter(2, Month),
		parameter(3, Day).

/*	Alternative predicate definitions using this/1 instead of parameter/2
	(see the User Manual for the pros and cons of both alternatives):

	year(Year) :-
		this(date(Year, _, _)).

	month(Month) :-
		this(date(_, Month, _)).

	day(Day) :-
		this(date(_, _, Day)).

	today :-
		{'$lgt_current_date'(Year, Month, Day)},	% defined in the config files
		this(date(Year, Month, Day)).

*/

	leap_year :-
		parameter(1, Year),
		(0 =:= mod(Year, 4), 0 =\= mod(Year, 100)
		 ;
		 0 =:= mod(Year, 400)),
		!.

:- end_object.


:- object(time(_Hours, _Mins, _Secs)).

	:- info([
		version is 1.1,
		author is 'Paulo Moura',
		date is 2005/9/5,
		comment is 'Time as parametric objects.',
		parnames is ['Hours', 'Mins', 'Secs']]).

	:- public(hours/1).
	:- mode(hours(?integer), one).

	:- public(mins/1).
	:- mode(mins(?integer), one).

	:- public(secs/1).
	:- mode(secs(?integer), one).

	:- public(now/0).
	:- mode(now, one).

	hours(Hours) :-
		parameter(1, Hours).

	mins(Mins) :-
		parameter(2, Mins).

	secs(Secs) :-
		parameter(3, Secs).

	now :-
		{'$lgt_current_time'(Hours, Mins, Secs)},	% defined in the config files
		parameter(1, Hours),
		parameter(2, Mins),
		parameter(3, Secs).

/*	Alternative predicate definitions using this/1 instead of parameter/2
	(see the User Manual for the pros and cons of both alternatives):

	hours(Hours) :-
		this(time(Hours, _, _)).

	mins(Mins) :-
		this(time(_, Mins, _)).

	secs(Secs) :-
		this(time(_, _, Secs)).

	now :-
		{'$lgt_current_time'(Hours, Mins, Secs)},	% defined in the config files
		this(time(Hours, Mins, Secs)).

*/

:- end_object.



/*	The following parametric object illustrates a solution for implementing 
	modifiable object state. The idea is to represent object state by using 
	object parameters, defining "setter" predicates/methods that return the 
	updated object identifier.
*/

:- object(rectangle(_width, _height, _x, _y)).

	:- info([
		version is 1.0,
		author is 'Paulo Moura',
		date is 2005/9/5,
		comment is 'A simple implementation of a geometric rectangle using parametric objects.',
		parnames is ['Width', 'Height', 'X', 'Y']]).

	:- public(init/0).
	:- mode(init, one).
	:- info(init/0,
		[comment is 'Initialize rectangle position.']).

	:- public(area/1).
	:- mode(area(-integer), one).
	:- info(area/1,
		[comment is 'Rectangle area.',
		 argnames is ['Area']]).

	:- public(move/3).
	:- mode(move(+integer, +integer, -compound), one).
	:- info(move/3, [
		comment is 'Moves a rectangle to a new position, returning the updated rectangle.',
		argnames is ['X', 'Y', 'NewRectangle']]).

	:- public(position/2).
	:- mode(position(?integer, ?integer), zero_or_one).
	:- info(position/2, [
		comment is 'Rectangle current position.',
		argnames is ['X', 'Y']]).

	init :-
		parameter(1, 2),	% Width
		parameter(2, 1),	% Height
		parameter(3, 0),	% X
		parameter(4, 0).	% Y

	area(Area) :-
		parameter(1, Width),
		parameter(2, Height),
		Area is Width*Height.

	move(X, Y, rectangle(Width, Height, X, Y)) :-
		parameter(1, Width),
		parameter(2, Height).

	position(X, Y) :-
		parameter(3, X),
		parameter(4, Y).

/*	Alternative predicate definitions using this/1 instead of parameter/2
	(see the User Manual for the pros and cons of both alternatives):

	init :-
		this(rectangle(2, 1, 0, 0)).

	area(Area) :-
		this(rectangle(Width, Height, _, _)),
		Area is Width*Height.

	move(X, Y, rectangle(Width, Height, X, Y)) :-
		this(rectangle(Width, Height, _, _)).

	position(X, Y) :-
		this(rectangle(_, _, X, Y)).

*/

:- end_object.