-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_variable_method_chain.pl
More file actions
404 lines (338 loc) · 13.6 KB
/
demo_variable_method_chain.pl
File metadata and controls
404 lines (338 loc) · 13.6 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
% demo_variable_method_chain.pl
% Comprehensive demonstration of: B = Value, A is B>>operation>>operation
% Shows all combinations and configurations of variable-based method chaining
:- use_module(starlog).
demo_header(Title) :-
nl,
writeln('========================================'),
writeln(Title),
writeln('========================================').
demo_example(Description, Code) :-
write(' '), writeln(Description),
write(' ?- '), writeln(Code),
nl.
% ============================================================
% Section 1: Problem Statement - Core Pattern
% ============================================================
demo_problem_statement :-
demo_header('Section 1: Problem Statement - B = [3,2,2], A is B>>sort>>length'),
demo_example('Exact problem statement pattern:',
'B = [3,2,2], starlog_call(A is B>>sort>>length), A = 2'),
write(' Executing: '),
B1 = [3,2,2],
starlog_call(A1 is B1>>sort>>length),
format('B = ~w, A = ~w~n', [B1, A1]),
nl,
demo_example('Just sort (single operation):',
'B = [3,2,2], starlog_call(A is B>>sort), A = [2,3]'),
write(' Executing: '),
B2 = [3,2,2],
starlog_call(A2 is B2>>sort),
format('B = ~w, A = ~w~n', [B2, A2]),
nl,
demo_example('Just length (single operation):',
'B = [3,2,2], starlog_call(A is B>>length), A = 3'),
write(' Executing: '),
B3 = [3,2,2],
starlog_call(A3 is B3>>length),
format('B = ~w, A = ~w~n', [B3, A3]),
nl.
% ============================================================
% Section 2: List Variables with Different Method Chains
% ============================================================
demo_list_variables :-
demo_header('Section 2: List Variables with Different Method Chains'),
demo_example('Reverse a list:',
'B = [1,2,3], starlog_call(A is B>>reverse)'),
B1 = [1,2,3],
starlog_call(A1 is B1>>reverse),
format(' Result: A = ~w~n', [A1]),
nl,
demo_example('Sort then get length:',
'B = [5,1,3,2], starlog_call(A is B>>sort>>length)'),
B2 = [5,1,3,2],
starlog_call(A2 is B2>>sort>>length),
format(' Result: A = ~w~n', [A2]),
nl,
demo_example('Flatten nested lists:',
'B = [[1,2],[3,4]], starlog_call(A is B>>flatten)'),
B3 = [[1,2],[3,4]],
starlog_call(A3 is B3>>flatten),
format(' Result: A = ~w~n', [A3]),
nl,
demo_example('Flatten and reverse:',
'B = [[1],[2],[3]], starlog_call(A is B>>flatten>>reverse)'),
B4 = [[1],[2],[3]],
starlog_call(A4 is B4>>flatten>>reverse),
format(' Result: A = ~w~n', [A4]),
nl,
demo_example('Max of list:',
'B = [5,2,8,1], starlog_call(A is B>>max_list)'),
B5 = [5,2,8,1],
starlog_call(A5 is B5>>max_list),
format(' Result: A = ~w~n', [A5]),
nl,
demo_example('Sum of list:',
'B = [1,2,3,4], starlog_call(A is B>>sum_list)'),
B6 = [1,2,3,4],
starlog_call(A6 is B6>>sum_list),
format(' Result: A = ~w~n', [A6]),
nl.
% ============================================================
% Section 3: String Variables with Method Chains
% ============================================================
demo_string_variables :-
demo_header('Section 3: String Variables with Method Chains'),
demo_example('String length:',
'B = "hello", starlog_call(A is B>>string_length)'),
B1 = "hello",
starlog_call(A1 is B1>>string_length),
format(' Result: A = ~w~n', [A1]),
nl,
demo_example('String to uppercase:',
'B = "hello", starlog_call(A is B>>string_upper)'),
B2 = "hello",
starlog_call(A2 is B2>>string_upper),
format(' Result: A = ~w~n', [A2]),
nl,
demo_example('String to lowercase:',
'B = "WORLD", starlog_call(A is B>>string_lower)'),
B3 = "WORLD",
starlog_call(A3 is B3>>string_lower),
format(' Result: A = ~w~n', [A3]),
nl,
demo_example('Uppercase then length:',
'B = "hello", starlog_call(A is B>>string_upper>>string_length)'),
B4 = "hello",
starlog_call(A4 is B4>>string_upper>>string_length),
format(' Result: A = ~w~n', [A4]),
nl.
% ============================================================
% Section 4: Atom Variables with Method Chains
% ============================================================
demo_atom_variables :-
demo_header('Section 4: Atom Variables with Method Chains'),
demo_example('Atom length:',
'B = hello, starlog_call(A is B>>atom_length)'),
B1 = hello,
starlog_call(A1 is B1>>atom_length),
format(' Result: A = ~w~n', [A1]),
nl,
demo_example('Atom to characters:',
'B = world, starlog_call(A is B>>atom_chars)'),
B2 = world,
starlog_call(A2 is B2>>atom_chars),
format(' Result: A = ~w~n', [A2]),
nl,
demo_example('Atom to chars then length:',
'B = abc, starlog_call(A is B>>atom_chars>>length)'),
B3 = abc,
starlog_call(A3 is B3>>atom_chars>>length),
format(' Result: A = ~w~n', [A3]),
nl.
% ============================================================
% Section 5: Number Variables with Method Chains
% ============================================================
demo_number_variables :-
demo_header('Section 5: Number Variables with Method Chains'),
demo_example('Absolute value:',
'B = -42, starlog_call(A is B>>abs)'),
B1 = -42,
starlog_call(A1 is B1>>abs),
format(' Result: A = ~w~n', [A1]),
nl,
demo_example('Ceiling:',
'B = 3.7, starlog_call(A is B>>ceiling)'),
B2 = 3.7,
starlog_call(A2 is B2>>ceiling),
format(' Result: A = ~w~n', [A2]),
nl,
demo_example('Floor:',
'B = 3.2, starlog_call(A is B>>floor)'),
B3 = 3.2,
starlog_call(A3 is B3>>floor),
format(' Result: A = ~w~n', [A3]),
nl,
demo_example('Square root:',
'B = 16, starlog_call(A is B>>sqrt)'),
B4 = 16,
starlog_call(A4 is B4>>sqrt),
format(' Result: A = ~w~n', [A4]),
nl.
% ============================================================
% Section 6: Complex Chains (3+ operations)
% ============================================================
demo_complex_chains :-
demo_header('Section 6: Complex Chains (3+ operations)'),
demo_example('Sort, reverse, then length:',
'B = [3,1,2], starlog_call(A is B>>sort>>reverse>>length)'),
B1 = [3,1,2],
starlog_call(A1 is B1>>sort>>reverse>>length),
format(' Result: A = ~w~n', [A1]),
nl,
demo_example('Flatten, reverse, then length:',
'B = [[1,2],[3,4]], starlog_call(A is B>>flatten>>reverse>>length)'),
B2 = [[1,2],[3,4]],
starlog_call(A2 is B2>>flatten>>reverse>>length),
format(' Result: A = ~w~n', [A2]),
nl,
demo_example('Uppercase, to chars, then length:',
'B = "hello", starlog_call(A is B>>string_upper>>string_chars>>length)'),
B3 = "hello",
starlog_call(A3 is B3>>string_upper>>string_chars>>length),
format(' Result: A = ~w~n', [A3]),
nl.
% ============================================================
% Section 7: Variables from Starlog Operators
% ============================================================
demo_operator_variables :-
demo_header('Section 7: Variables from Starlog Operators'),
demo_example('List append then reverse:',
'starlog_call(B is [1,2]&[3,4]), starlog_call(A is B>>reverse)'),
starlog_call(B1 is [1,2]&[3,4]),
starlog_call(A1 is B1>>reverse),
format(' Result: B = ~w, A = ~w~n', [B1, A1]),
nl,
demo_example('List append then sort and length:',
'starlog_call(B is [1,2]&[3,4]), starlog_call(A is B>>sort>>length)'),
starlog_call(B2 is [1,2]&[3,4]),
starlog_call(A2 is B2>>sort>>length),
format(' Result: B = ~w, A = ~w~n', [B2, A2]),
nl,
demo_example('String concat then length:',
'starlog_call(B is "hello":"world"), starlog_call(A is B>>string_length)'),
starlog_call(B3 is "hello":"world"),
starlog_call(A3 is B3>>string_length),
format(' Result: B = ~w, A = ~w~n', [B3, A3]),
nl,
demo_example('Reverse then length:',
'starlog_call(B is reverse([1,2,3])), starlog_call(A is B>>length)'),
starlog_call(B4 is reverse([1,2,3])),
starlog_call(A4 is B4>>length),
format(' Result: B = ~w, A = ~w~n', [B4, A4]),
nl.
% ============================================================
% Section 8: Multiple Variables in Sequence
% ============================================================
demo_multiple_variables :-
demo_header('Section 8: Multiple Variables in Sequence'),
demo_example('Chain through 3 variables:',
'B = [3,2,2], C is B>>sort, A is C>>length'),
B1 = [3,2,2],
starlog_call(C1 is B1>>sort),
starlog_call(A1 is C1>>length),
format(' Result: B = ~w, C = ~w, A = ~w~n', [B1, C1, A1]),
nl,
demo_example('String processing chain:',
'B = "hello", C is B>>string_upper, A is C>>string_length'),
B2 = "hello",
starlog_call(C2 is B2>>string_upper),
starlog_call(A2 is C2>>string_length),
format(' Result: B = ~w, C = ~w, A = ~w~n', [B2, C2, A2]),
nl,
demo_example('List processing chain through 4 variables:',
'B = [1,2,3], C is B>>reverse, D is C>>sort, A is D>>length'),
B3 = [1,2,3],
starlog_call(C3 is B3>>reverse),
starlog_call(D3 is C3>>sort),
starlog_call(A3 is D3>>length),
format(' Result: B = ~w, C = ~w, D = ~w, A = ~w~n', [B3, C3, D3, A3]),
nl.
% ============================================================
% Section 9: Edge Cases
% ============================================================
demo_edge_cases :-
demo_header('Section 9: Edge Cases'),
demo_example('Empty list:',
'B = [], starlog_call(A is B>>length)'),
B1 = [],
starlog_call(A1 is B1>>length),
format(' Result: A = ~w~n', [A1]),
nl,
demo_example('Single element list:',
'B = [5], starlog_call(A is B>>sort>>length)'),
B2 = [5],
starlog_call(A2 is B2>>sort>>length),
format(' Result: A = ~w~n', [A2]),
nl,
demo_example('Empty string:',
'B = "", starlog_call(A is B>>string_length)'),
B3 = "",
starlog_call(A3 is B3>>string_length),
format(' Result: A = ~w~n', [A3]),
nl,
demo_example('Already sorted list:',
'B = [1,2,3], starlog_call(A is B>>sort)'),
B4 = [1,2,3],
starlog_call(A4 is B4>>sort),
format(' Result: A = ~w~n', [A4]),
nl.
% ============================================================
% Section 10: Summary
% ============================================================
demo_summary :-
demo_header('Summary: All Combinations and Configurations'),
writeln(' This demonstration covers:'),
writeln(''),
writeln(' 1. Problem Statement Pattern'),
writeln(' - B = [3,2,2], A is B>>sort>>length'),
writeln(' - Single and multiple operations'),
writeln(''),
writeln(' 2. List Variables'),
writeln(' - reverse, sort, flatten, length'),
writeln(' - max_list, min_list, sum_list'),
writeln(' - Multiple operations chained'),
writeln(''),
writeln(' 3. String Variables'),
writeln(' - string_length, string_upper, string_lower'),
writeln(' - string_chars'),
writeln(' - Chained string operations'),
writeln(''),
writeln(' 4. Atom Variables'),
writeln(' - atom_length, atom_chars'),
writeln(' - Chained atom operations'),
writeln(''),
writeln(' 5. Number Variables'),
writeln(' - abs, ceiling, floor, round, sqrt'),
writeln(' - Mathematical transformations'),
writeln(''),
writeln(' 6. Complex Chains'),
writeln(' - 3+ operations in a single chain'),
writeln(' - Mixed operation types'),
writeln(''),
writeln(' 7. Starlog Operator Variables'),
writeln(' - Variables assigned from &, :, and functions'),
writeln(' - Then used in method chains'),
writeln(''),
writeln(' 8. Multiple Variables in Sequence'),
writeln(' - Chaining results through multiple variables'),
writeln(' - Multi-step transformations'),
writeln(''),
writeln(' 9. Edge Cases'),
writeln(' - Empty lists and strings'),
writeln(' - Single element lists'),
writeln(' - Already processed data'),
writeln(''),
writeln(' All patterns demonstrated work correctly!'),
nl.
% ============================================================
% Main Demo
% ============================================================
main :-
writeln(''),
writeln('╔════════════════════════════════════════════════════════╗'),
writeln('║ Variable Method Chain Demonstration ║'),
writeln('║ Pattern: B = Value, A is B>>operation>>operation ║'),
writeln('╚════════════════════════════════════════════════════════╝'),
demo_problem_statement,
demo_list_variables,
demo_string_variables,
demo_atom_variables,
demo_number_variables,
demo_complex_chains,
demo_operator_variables,
demo_multiple_variables,
demo_edge_cases,
demo_summary,
nl.
:- initialization(main, main).