SICP/ex-4_45-xx.scm

197 lines
5.7 KiB
Scheme
Raw Normal View History

2021-02-07 03:48:35 +01:00
(load "util.scm")
(load "misc/sicp-ambeval.scm")
(define the-global-environment (setup-environment))
(define result '())
(define (amball exp)
(set! result '()) ; reset result
(ambeval exp
the-global-environment
(lambda (value next)
(set! result (cons value result))
(next))
(lambda () result))
(set! result (reverse result))
result)
(amball '(begin
(define (require p)
(if (not p) (amb)))
2021-02-07 03:48:35 +01:00
(define nouns '(noun student professor cat class))
(define verbs '(verb studies lectures eats sleeps))
2021-02-07 03:48:35 +01:00
(define articles '(article the a))
(define (parse-sentence)
(list 'sentence
(parse-noun-phrase)
(parse-word verbs)))
2021-02-07 03:48:35 +01:00
(define (parse-noun-phrase)
(list 'noun-phrase
(parse-word articles)
(parse-word nouns)))
2021-02-07 03:48:35 +01:00
(define (parse-word word-list)
(require (not (null? *unparsed*)))
(require (memq (car *unparsed*) (cdr word-list)))
(let ((found-word (car *unparsed*)))
(set! *unparsed* (cdr *unparsed*))
(list (car word-list) found-word)))
(define *unparsed* '())
2021-02-07 03:48:35 +01:00
(define (parse input)
(set! *unparsed* input)
(let ((sent (parse-sentence)))
(require (null? *unparsed*))
sent))
(define prepositions '(prep for to in by with))
(define (parse-prepositional-phrase)
(list 'prep-phrase
(parse-word prepositions)
(parse-noun-phrase)))
(define (parse-sentence)
(list 'sentence
(parse-noun-phrase)
(parse-verb-phrase)))
(define (parse-verb-phrase)
(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)
(amb noun-phrase
(maybe-extend (list 'noun-phrase
noun-phrase
(parse-prepositional-phrase)))))
(maybe-extend (parse-simple-noun-phrase)))
(parse '(the professor lectures to the student with the cat))
))
;'The professor lectures to the student in the class with the cat.'
(assert
(first result)
'(sentence
(simple-noun-phrase (article the) (noun professor))
(verb-phrase
(verb-phrase
(verb lectures)
(prep-phrase (prep to)
(simple-noun-phrase
(article the) (noun student))))
(prep-phrase (prep with)
(simple-noun-phrase
(article the) (noun cat))))))
(assert
(second result)
'(sentence
(simple-noun-phrase (article the) (noun professor))
(verb-phrase
(verb lectures)
(prep-phrase (prep to)
(noun-phrase
(simple-noun-phrase
(article the) (noun student))
(prep-phrase (prep with)
(simple-noun-phrase
(article the) (noun cat))))))))
(display "\nex-4.45 - sentence-meanings\n")
(amball '(parse '(The professor lectures to the student in the class with the cat)))
(assert (length result) 5)
'(sentence
(simple-noun-phrase (article the) (noun professor))
(verb-phrase
(verb-phrase
(verb-phrase
(verb lectures)
(prep-phrase
(prep to) (simple-noun-phrase (article the) (noun student))))
(prep-phrase
(prep in) (simple-noun-phrase (article the) (noun class))))
(prep-phrase (prep with) (simple-noun-phrase (article the) (noun cat)))))
; The professor lectures (to the student) in the class with the cat.
'(sentence
(simple-noun-phrase (article the) (noun professor))
(verb-phrase
(verb-phrase
(verb lectures)
(prep-phrase (prep to) (simple-noun-phrase (article the) (noun student))))
(prep-phrase
(prep in)
(noun-phrase
(simple-noun-phrase (article the) (noun class))
(prep-phrase (prep with)
(simple-noun-phrase (article the) (noun cat)))))))
; The professor lectures to the student (in the class with the cat).
'(sentence
(simple-noun-phrase (article the) (noun professor))
(verb-phrase
(verb-phrase
(verb lectures)
(prep-phrase
(prep to)
(noun-phrase
(simple-noun-phrase (article the) (noun student))
(prep-phrase (prep in) (simple-noun-phrase (article the) (noun class))))))
(prep-phrase (prep with) (simple-noun-phrase (article the) (noun cat)))))
; The professor lectures (to the student in the class) with the cat.
'(sentence
(simple-noun-phrase (article the) (noun professor))
(verb-phrase
(verb lectures)
(prep-phrase
(prep to)
(noun-phrase
(noun-phrase
(simple-noun-phrase (article the) (noun student))
(prep-phrase (prep in)
(simple-noun-phrase (article the) (noun class))))
(prep-phrase (prep with) (simple-noun-phrase (article the) (noun cat)))))))
2021-02-07 03:48:35 +01:00
; The professor lectures ((to the student in the class) with the cat).
2021-02-07 03:48:35 +01:00
'(sentence
(simple-noun-phrase (article the) (noun professor))
(verb-phrase
(verb lectures)
(prep-phrase
(prep to)
(noun-phrase
(simple-noun-phrase (article the) (noun student))
(prep-phrase
(prep in)
(noun-phrase
(simple-noun-phrase (article the) (noun class))
(prep-phrase (prep with) (simple-noun-phrase (article the) (noun cat)))))))))
2021-02-07 03:48:35 +01:00
; The professor lectures (to the student (in the class with the cat)).
2021-02-07 03:48:35 +01:00
(display "[answered]\n")
2021-02-07 03:48:35 +01:00
(display "\nex-4.46 - evaluation-order\n")