```    1% Simple illustration of the use of lazy evaluation
2%       a predicate to construct a line is called during
3%       the search to obtain a linear equation
4% For realistic examples a proper regression predicate will be needed
5%	(usually better to implement this in C, and call it from Prolog)
6% To run do the following:
9%       c. sat(1).
10%       d. reduce.
```

?- `sat(1)`,`reduce(A)`. */

```   14:- use_module(library(aleph)).   15:- if(current_predicate(use_rendering/1)).   16:- use_rendering(prolog).   17:- endif.   18:- aleph.   19
20:- mode(1,p(+indep,+dep)).   21:- mode(1,lin_regress1(+dep,+indep,#number,#number,#number)).
22
23:- determination(p/2,lin_regress1/5).   24
25:- lazy_evaluate(lin_regress1/5).   26
27% lazy evaluation does not need substitutions from negative examples
28
29:- positive_only(lin_regress1/5).   30
31:- aleph_set(portray_literals,true).   32
33aleph_portray(p(A,B)):-
34	format("~q",[p(A,B)]).
35aleph_portray(lin_regress1(Y,X,M,C,E)):-
36	format("~q",[Y]), write(' is '),
37	format("~q",[M]), write(' * '),
38	format("~q",[X]), write(' + '),
39	format("~q",[C]), write(' +/- '),
40	format("~q",[E]).
41
42:-begin_bg.   43
44nlist([]).
45nlist([_|_]).
46
47% definition for use during normal evaluation
48lin_regress1(Y,X,M,C,Error):-
49	number(X), number(Y),
50	nonvar(M), nonvar(C), nonvar(Error), !,
51	Y1 is M*X + C,
52	Diff is Y - Y1,
53	abs_val(Diff,D1),
54	D1 =< Error.
55
56% definition for use during lazy evaluation
57lin_regress1(Y,X,M,C,Error):-
58	nlist(X), nlist(Y),
59	var(M), var(C), !,
60	l_regress1(Y,X,M,C,Error).
61
62% otherwise: for bottom clause
63lin_regress1(Y,X,0.0,Y,0.0):-
64	X \= Y.
65
66
67% very simple line construction
68l_regress1([YVals|_],[XVals|_],M,C,0.0):-
69	YVals = [Y1,Y2|_],
70	XVals = [X1,X2|_],
71	M is (Y2-Y1)/(X2-X1),
72	C is Y1 - M*X1.
73
74abs_val(X,Y):- X < 0, !, Y is -X.
75abs_val(X,X):- X >= 0.
76
77:-end_bg.   78:-begin_in_pos.   79p(1,2).
80p(2,4).
81p(3,6).
82p(4,8).
83:-end_in_pos.   84:-begin_in_neg.   85p(1,0).
86p(2,5).
87p(3,9).
88:-end_in_neg.```