Compare commits

...

2 Commits

2 changed files with 106 additions and 57 deletions

View File

@ -1,28 +1,22 @@
(define nouns '(noun student professor cat class)) (define nouns '(noun student professor cat class))
(define verbs '(verb studies lectures eats sleeps)) (define verbs '(verb studies lectures eats sleeps))
(define articles '(article the a)) (define articles '(article the a))
(define prepositions '(prep for to in by with))
;; output of parse (define (log message level)
'(sentence (noun-phrase (article the) (noun cat)) (define (output message level)
(verb eats)) (if (= level 0) message
(cons '_ (output message (- level 1)))))
(display (output message level))
(newline))
(define (parse-sentence) (define (parse-word word-list level)
(list 'sentence (log (list 'word-list word-list '*unparsed* *unparsed*) level)
(parse-noun-phrase)
(parse-word verbs)))
(define (parse-noun-phrase)
(list 'noun-phrase
(parse-word articles)
(parse-word nouns)))
(define (parse-word word-list)
(require (not (null? *unparsed*))) (require (not (null? *unparsed*)))
(require (memq (car *unparsed*) (cdr word-list))) (require (memq (car *unparsed*) (cdr word-list)))
(let ((found-word (car *unparsed*))) (let ((found-word (car *unparsed*)))
(set! *unparsed* (cdr *unparsed*)) (set! *unparsed* (cdr *unparsed*))
(log (list (car word-list) found-word) level)
(list (car word-list) found-word))) (list (car word-list) found-word)))
(define *unparsed* '()) (define *unparsed* '())
@ -33,43 +27,61 @@
(require (null? *unparsed*)) (require (null? *unparsed*))
sent)) sent))
;: (parse '(the cat eats))
;; output of parse
'(sentence (noun-phrase (article the) (noun cat)) (verb eats))
(define prepositions '(prep for to in by with))
(define (parse-prepositional-phrase)
(list 'prep-phrase
(parse-word prepositions)
(parse-noun-phrase)))
(define (parse-sentence) (define (parse-sentence)
(list 'sentence (list 'sentence
(parse-noun-phrase) ;;(parse-noun-phrase)
(parse-verb-phrase))) (parse-verb-phrase 1)))
(define (parse-verb-phrase) (define (parse-noun-phrase level)
(define (maybe-extend verb-phrase)
(amb verb-phrase
(maybe-extend (list 'verb-phrase
verb-phrase
(parse-prepositional-phrase)))))
(maybe-extend (parse-word verbs)))
(define (parse-simple-noun-phrase)
(list 'simple-noun-phrase
(parse-word articles)
(parse-word nouns)))
(define (parse-noun-phrase)
(define (maybe-extend noun-phrase) (define (maybe-extend noun-phrase)
(log '(maybe-extend-noun) level)
(amb noun-phrase (amb noun-phrase
(maybe-extend (list 'noun-phrase (maybe-extend (list 'noun-phrase
noun-phrase noun-phrase
(parse-prepositional-phrase))))) (parse-prepositional-phrase level)))))
(maybe-extend (parse-simple-noun-phrase))) (log '(parse-noun-phrase) level)
(maybe-extend (parse-simple-noun-phrase level)))
;; Note maybe-extend is tail recursive
(define (parse-verb-phrase level)
(define (maybe-extend verb-phrase)
(log '(maybe-extend-verb-phrase) level)
(amb verb-phrase
(maybe-extend (list 'verb-phrase
verb-phrase
(parse-prepositional-phrase level)))))
(log '(parse-verb-phrase) level)
(maybe-extend (parse-word verbs level)))
(define (parse-simple-noun-phrase level)
(log '(parse-simple-noun) level)
(list 'simple-noun-phrase
(parse-word articles level)
(parse-word nouns level)))
(define (parse-prepositional-phrase level)
(log '(parse-prep) level)
(list 'prep-phrase
(parse-word prepositions level)
(parse-noun-phrase level)))
;; Note: not tail recursive
(define (parse-verb-phrase level)
(log '(parse-verb-phrase) level)
(amb (parse-word verbs level)
(list 'verb-phrase
(parse-verb-phrase (+ level 1))
(parse-prepositional-phrase level))))
;; Exercise 4.45: Output of (parse '(the professor lectures to the student in the class with the cat)) ;; Exercise 4.45: Output of (parse '(the professor lectures to the student in the class with the cat))
@ -139,15 +151,51 @@
(sentence (sentence
(simple-noun-phrase (article the) (noun professor)) (simple-noun-phrase (article the) (noun professor))
(verb-phrase (verb lectures) (verb-phrase
(prep-phrase (prep to) (verb lectures)
(noun-phrase (simple-noun-phrase (article the) (noun student)) (prep-phrase (prep to)
(prep-phrase (prep in) (noun-phrase (simple-noun-phrase (article the) (noun student))
(noun-phrase (prep-phrase (prep in)
(simple-noun-phrase (article the) (noun-phrase
(noun class)) (simple-noun-phrase (article the)
(prep-phrase (prep with) (noun class))
(simple-noun-phrase (prep-phrase (prep with)
(article the) (simple-noun-phrase
(noun cat))))))))) (article the)
(noun cat)))))))))
;; The professor lectures to the student, who is in the class with the cat ;; The professor lectures to the student, who is in the class with the cat
;; Exercise 4.46
;; If amb evaluated its arguments in any order other than
;; left-to-right, then the recursive maybe-extend rules would never
;; terminate, as the second argument is a recursive call to the
;; procedure. We rely on the recursion stopping at each decision
;; point.
;;
;; Also, since parse has side-effects (namely, it removes a word from
;; the input), it is important that we parse the different parts of
;; the sentence in the correct order.
;; Exercise 4.47
(define (parse-verb-phrase level)
(log (list 'parse-verb-phrase level))
(amb (parse-word verbs)
(list 'verb-phrase
(parse-verb-phrase (+ level 1))
(parse-prepositional-phrase (+ level 1)))))
;; In this case, if (parse-word verbs) fails, then we evaluate the 2nd
;; argument, which calls parse-verb-phrase recursively, creating a new
;; amb, which tries (parse-word verbs), which still fails. In the
;; orinal procedure, we evaluate (parse-word verbs) once and bind it
;; to verb-phrase. We then keep extending the phrase until it either
;; succeeds or fails. If it fails, the whole amb call fails. The
;; version here never fails because it keeps calling parse-verb-phrase.
;; If we swap the order of the clauses in the amb, then we get an
;; infinite loop, as the first clause immediately calls
;; parse-verb-phrase recursively. There is nothing to limit this
;; recursion.

View File

@ -209,6 +209,7 @@
(begin (begin
(newline) (newline)
(display ";;; Starting a new problem ") (display ";;; Starting a new problem ")
(newline)
(set! start-time (current-process-milliseconds)) (set! start-time (current-process-milliseconds))
(ambeval input (ambeval input
env env