計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.3(可変データでのモデル化)、3.3.4(デジタル回路のシミュレーター)、回線の表現、次第書き、シミュレーションの例、問題 3.31.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.31.
初期化が必要なのは、各回線のシグナルの値の初期値(0)を変更するために必要。追加した新しいアクション手続きが回線に追加された際に、この手続きを直ちに走らせないと各回線のシグナルの値が0のままで、正しく動作しない。
半加算器の例を、初期化しない場合どうなるかをトレース。
(define (probe name wire) (add-action! wire (lambda () (newline) (display name) (display " ") (display (current-time the-agenda)) (display " New-value = ") (display (get-signal wire))))) (define the-agenda (make-agenda)) ;; the-agenda 空っぽ (define inverter-delay 2) (define and-gate-delay 3) (define or-gate-delay 5) (define input-1 (make-wire)) ;; input-1: signal-value 0, action-procedures '() (define input-2 (make-wire)) ;; input-2: signal-value 0, action-procedures '() (define sum (make-wire)) ;; sum: signal-value 0, action-procedures '() (define carry (make-wire)) ;; carry: signal-value 0, action-procedures '() (prob 'sum sum) sum 0 New-value = 0 (probe 'carry carry) sum 0 New-value = 0 (half-adder input-1 input-2 sum carry) ;; d: signal-value 0, action-procedures '() ;; e: signal-value 0, action-procedures '() (or-gate input-1 input-2 d) (add-action! input-1 (lambda () (let ((new-value (logical-or (get-signal input-1) (get-signal input-2)))) (after-delay 5 (lambda () (set-signal! d new-value)))))) ;; input-1: ;; signal-value 0 ;; action-procedures ((lambda () ;; (let ((new-value ;; (logical-or (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 5 (lambda () ;; (set-signal! d new-value)))))) (add-action! input-2 or-action-procedure) ;; input-2: ;; signal-value 0 ;; action-procedures ((lambda () ;; (let ((new-value ;; (logical-or (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 5 (lambda () ;; (set-signal! d new-value)))))) (and-gate input-1 input-2 carry) (add-action! input-1 (lambda () (let ((new-value (logical-and (get-signal input-1) (get-signal input-2)))) (after-delay 3 (lambda () (set-signal! carry new-value)))))) ;; input-1: ;; signal-value 0 ;; action-procedures ((lambda () ;; (let ((new-value ;; (logical-and (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 3 ;; (lambda () ;; (set-signal! carry new-value))))) ;; (lambda () ;; (let ((new-value ;; (logical-or (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 5 (lambda () ;; (set-signal! d new-value)))))) (add-action! input-2 (lambda () (let ((new-value (logical-and (get-signal input-1) (get-signal input-2)))) (after-delay 3 (lambda () (set-signal! carry new-value)))))) ;; input-2: ;; signal-value 0 ;; action-procedures ((lambda () ;; (let ((new-value ;; (logical-and (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 3 ;; (lambda () ;; (set-signal! carry new-value))))) ;; (lambda () ;; (let ((new-value ;; (logical-or (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 5 (lambda () ;; (set-signal! d new-value)))))) (inverter carry e) (add-action! carry (lambda () (let ((new-value (logical-not (get-signal carry)))) (after-delay 2 (lambda () (set-signal! e new-value)))))) ;; carry: ;; signal-value 0 ;; action-procedures '((lambda () ;; (let ((new-value ;; (logical-not (get-signal carry)))) ;; (after-delay 2 ;; (lambda () ;; (set-signal! e new-value))))) ;; (lambda () ;; (newline) ;; (display 'carry) ;; (display " ") ;; (display (current-time carry)) ;; (display " New-value = ") ;; (display (get-signal carry)))) (and-gate d e sum) (add-action! d (lambda () (let ((new-value (logical-and (get-signal d) (get-signal e)))) (after-delay 3 (lambda () (set-signal! sum new-value)))))) ;; d: ;; signal-value 0 ;; action-procedures '((lambda () ;; (let ((new-value ;; (logical-and (get-signal d) ;; (get-signal e)))) ;; (after-delay 3 ;; (lambda () ;; (set-signal! sum new-value)))))) (add-action! e (lambda () (let ((new-value (logical-and (get-signal d) (get-signal e)))) (after-delay 3 (lambda () (set-signal! sum new-value)))))) ;; e: ;; signal-value 0 ;; action-procedures '((lambda () ;; (let ((new-value ;; (logical-and (get-signal d) ;; (get-signal e)))) ;; (after-delay 3 ;; (lambda () ;; (set-signal! sum new-value)))))) ok ;; input-1: ;; signal-value 0 ;; action-procedures ((lambda () ;; (let ((new-value ;; (logical-and (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 3 ;; (lambda () ;; (set-signal! carry new-value))))) ;; (lambda () ;; (let ((new-value ;; (logical-or (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 5 (lambda () ;; (set-signal! d new-value)))))) (set-signal! input-1 1) (set! signval-value 1) ;; input-1: ;; signal-value 1 ;; action-procedures ((lambda () ;; (let ((new-value ;; (logical-and (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 3 ;; (lambda () ;; (set-signal! carry new-value))))) ;; (lambda () ;; (let ((new-value ;; (logical-or (get-signal input-1) ;; (get-signal input-2)))) ;; (after-delay 5 (lambda () ;; (set-signal! d new-value)))))) ;; input-2: ;; signal-value 0 (call-each action-procedures) ;; new-value 0 (after-delay 3 (lambda () (set-signal! carry 0))) ;; the-agenda ;; 時間0 ;; 時間3, (lambda () (set-signal! carry 0)) ;; new-value 1 (after-delay 5 (lambda () (set-signal! d 1))) ;; the-agenda ;; 時間0 ;; 時間3、(lambda () (set-signal! carry 0)) ;; 時間5, (lambda () (set-signal! d 1)) done (propagate) ;; first-item 時間3、(lambda () (set-signal! carry 0)) ;; carry: ;; signal-value 0 (set-signal! carry 0) ;; d: ;; signal-value 0 (set-signal! d 1) (set! signal-value 1) ;; d: ;; signal-value 1 ;; action-procedures '((lambda () ;; (let ((new-value ;; (logical-and (get-signal d) ;; (get-signal e)))) ;; (after-delay 3 ;; (lambda () ;; (set-signal! sum new-value)))))) ;; e: ;; signal-value 0 (call-each action-procedures) ((lambda () (let ((new-value (get-signal d) (get-signal e))) (after-delay 3 (lambda () (set-signal! sum new-value)))))) ;; new-value 0 (after-delay 3 (lambda () (set-signal! sum 0))) ;; the-agenda ;; 時間0 ;; 時間3、(lambda () (set-signal! carry 0)) ;; 時間5, (lambda () (set-signal! d 1)) ;; 時間3, (lambda () (set-signal sum 0)) done ;; 各回線のシグナル値 ;; input-1 1 ;; input-2 0 ;; sum 0 ;; carry 0 ;; 本来なら、input-1 1, input-2 0なら、sum 1, carry 0とならなければならない
0 コメント:
コメントを投稿