-
Notifications
You must be signed in to change notification settings - Fork 0
/
termexpansion.pl
81 lines (59 loc) · 2.18 KB
/
termexpansion.pl
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
:- module(termexpansion, []).
/*******************************
* Conditional Compilation *
*
* example implements conditional
* compilation as a
*******************************/
:- dynamic termexpansion:'$nocompile$'/0.
termexpansion:cond_compile :-
retractall(termexpansion:'$nocompile$').
termexpansion:cond_no_compile :-
asserta(termexpansion:'$nocompile$').
user:term_expansion(':-'(termexpansion:cond_compile), []) :-
retractall(termexpansion:'$nocompile$'),
writeln('*** cond_compile').
user:term_expansion(end_of_file, end_of_file) :-
retractall(termexpansion:'$nocompile$'),
writeln('*** end of file').
user:term_expansion(In, []) :-
termexpansion:'$nocompile$',
In \= ':-'(termexpansion:cond_compile),
In \= end_of_file,
format('false ~w~n', [In]).
/*******************************
* goal expansion
*******************************/
user:goal_expansion(flying_burrito(X), flying_taco(X)) :-
format('goal expand flying_burrito ~w~n', [X]).
/*******************************
* indexing
*******************************/
:- nb_setval(fun_saying_index, 0).
% don't need to be in user
term_expansion(fun_saying(Saying), fun_saying(Index, Saying)) :-
nb_getval(fun_saying_index, Index),
succ(Index, NIndex),
nb_setval(fun_saying_index, NIndex).
/*******************************
* test examples *
*******************************/
% conditional compilation
%
:- termexpansion:cond_no_compile.
foo(7). % wont be compiled
% bizarrely current_functor(foo, 1) succeeds
:- termexpansion:cond_compile.
:- ( current_predicate(foo/1)
-> writeln('OOOooops, foo should not be defined')
; writeln('this must be printed')
).
flying_taco(X) :-
format('I am the true flying taco ~w~n', [X]).
flying_burrito(7). % no expansion, not in body, but will expand goal.
flying_enchilada :-
flying_burrito(12).
fun_saying('Never give a Python programmer a line break').
fun_saying('There are 10 types of people in the world, those that understand binary and those that don\'t').
fun_saying('Endless Loop: n., see Loop, Endless.\nLoop, Endless: n., see Endless Loop.').
fun_saying('Never put off until run time what you can do at compile time.').