Implement till 4.68

main
Felix Martin 2021-03-03 19:59:16 -05:00
parent a967a60994
commit 209e58d1c5
1 changed files with 78 additions and 9 deletions

View File

@ -49,13 +49,6 @@
(display "\nex-4.63 - genesis-family\n")
; Formulate rules such as ``If S is the son of F, and F is the son of G, then S
; is the grandson of G'' and ``If W is the wife of M, and S is the son of W,
; then S is the son of M'' (which was supposedly more true in biblical times
; than today) that will enable the query system to find the grandson of Cain;
; the sons of Lamech; the grandsons of Methushael. (See exercise 4.69 for some
; rules to deduce more complicated relationships.)
(eval-query '(rule (grandson ?grandparent ?grandson)
(and (nson ?grandparent ?son)
(nson ?son ?grandson))))
@ -70,7 +63,83 @@
(eval-query '(grandson Methushael ?x))
(newline)
(display "\nex-4.64\n")
(display "\nex-4.64 - outranked-loop\n")
(display "\nex-4.65\n")
(eval-query
'(rule (outranked-by-2 ?staff-person ?boss)
(or (supervisor ?staff-person ?boss)
(and (outranked-by-2 ?middle-manager ?boss)
(supervisor ?staff-person ?middle-manager)))))
; The first part of the or-clause finds a single stream. When evaluating the
; second part of the and clause ?middle-manager and ?boss are not assigned to
; any values yet. Therefore, in the recursive application of the rule
; outranked-by-2 is invoked again. Again, with no assignments. By swapping the
; two patterns in the and-clause the rule first creates frames and then invokes
; the recursive call. If supervisor does not return any results outranked-by-2
; is not invoked again and there is no endless-loop.
; (eval-query '(outranked-by-2 (Bitdiddle Ben) ?who))
(display "[answered]\n")
(display "\nex-4.65 - four-wheels\n")
;(rule (wheel ?person)
; (and (supervisor ?middle-manager ?person)
; (supervisor ?staff ?middle-manager)))
; The wheel rule returns a match for each staff person that is under a
; middle-manager. Ben and Eben are under Oliver. Ben supervises Alyssa, Cy D,
; and Lem E. Eben supervises Robert. Hence, there are four results for Oliver.
; Louis works under Alyssa who works under Ben, which is why Ben only shows up
; once.
(eval-query '(wheel ?who))
(display "\n[answered]\n")
(display "\nex-4.66 - accumulation-function\n")
; Ben's solution would yield wrong results for a query like wheel where the
; same entry is returned multiple times. For the example provided, everything
; would work fine. So, I am not sure if this is what Ben has realized. A way to
; salvage the situation would be to apply a filter so that each ?x only occurs
; once.
(display "[answered]\n")
(display "\nex-4.67 - avoid-loops\n")
; Each application of a query generates a message that contains the rule name,
; the variables, and the variable bindings. Before we apply message we check if
; the message is in the message list in which case we skip the evaluation.
; Otherwise, we add message to the list and continue the evaluation.
(display "[answered]\n")
(display "\nex-4.68 - reverse\n")
(eval-query '(rule (reverse () ())))
(eval-query
'(rule (reverse (?x . ?xs) ?rs)
(and (reverse ?xs ?ys)
(append-to-form ?ys (?x) ?rs))))
(eval-query '(reverse (1 2 3) ?x))
(newline)
; (eval-query '(reverse ?x (1 2 3))) ; infinite loop
(display "\nex-4.69 - greats\n")
; Beginning with the data base and the rules you formulated in exercise 4.63,
; devise a rule for adding ``greats'' to a grandson relationship. This should
; enable the system to deduce that Irad is the great-grandson of Adam, or that
; Jabal and Jubal are the great-great-great-great-great-grandsons of Adam.
; (Hint: Represent the fact about Irad, for example, as ((great grandson) Adam
; Irad). Write rules that determine if a list ends in the word grandson. Use
; this to express a rule that allows one to derive the relationship ((great .
; ?rel) ?x ?y), where ?rel is a list ending in grandson.) Check your rules on
; queries such as ((great grandson) ?g ?ggs) and (?relationship Adam Irad).
(display "\nex-4.70\n")