Chapter 4, Metalinguistic Abstraction
Exercise 4.71
simple-query example
In simple query, delay
ensures that we first find/match assertions and then on demand we evaluate rules. This behaviour will be same even if the underlying scheme is evaluates arguments right to left!
In some cases, using delay, postpones the infinite loop and even produce some result(if there are any assertions matched) while going into infinite loop.
Consider the following:
1
2
(assert! (tmp1 a))
(assert! (rule (tmp1 ?x) (tmp1 ?x)))
Evaluating the query (tmp1 a)
- With delay(original):
1
2
3
4
5
6
7
8
(tmp1 a)
(tmp1 a)
(tmp1 a)
(tmp1 a)
(tmp1 a)
(tmp1 a)
....
;;infinite loop
- Without delay(as suggested in exercise):
1
2
;;; Query results:
;Aborting!: maximum recursion depth exceeded
disjunction example
Our and
works procedurally in left to right order so should be the case with or
. Thus if any clause in or
can return some answer from a clause then instead of first evaluating all the clauses it would be better to first generate the first answer found!
Again like above this ensures left to right order as well as generates answers if found in earlier clauses even if later clauses result in infinite loop.
Consider the following example:
1
2
3
(assert! (tmp3 a))
(assert! (rule (tmp2 ?x) (or (tmp3 ?x)
(tmp2 ?x))))
Evaluating the query (tmp2 a)
- With delay(origninal):
1
2
3
4
5
6
7
8
(tmp2 a)
(tmp2 a)
(tmp2 a)
(tmp2 a)
(tmp2 a)
(tmp2 a)
....
;;infinite loop
- Without delay(as suggested in exercise):
1
2
;;; Query results:
;Aborting!: maximum recursion depth exceeded