; Jay again. What's on his mind now? ; ; ELECTRICAL SYSTEMS ; DIGITAL CIRCUITS ; Primitives and Means of Combination ; (define a (make-wire)) ; (define b (make-wire)) ; (define c (make-wire)) ; (define d (make-wire)) ; (define e (make-wire)) ; (define s (make-wire)) ; ; (or-gate a b d) ; (and-gate a b c) ; (inverter c e) ; (and-gate d e s) ; Means of Abstraction ; (define (half-adder a b s c) ; (let ((d (make-wire)) (e (make-wire))) ; (or-gate a b c) ; (and-gate a b c) ; (inverter c e) ; (and-gate d e s))) ; (define (full-adder a b c-in sum c-out) ; (let ((s (make-wire)) ; (c1 (make-wire)) ; (c2 (make-wire))) ; (half-adder b c-in s c1) ; (half-adder a s sum c2) ; (or-gate c1 c2 c-out))) ; Implementing a Primitive ; (define (inverter in out) ; (define (invert-in) ; (let ((new (logical-not (get-signal in)))) ; (after-delay inverter delay ; (lambda () ; (set-signal! out new))))) ; (add-action! in invert-in)) ; (define (logical-not s) ; (cond ((= s 0) 1) ; ((= s 1) 0) ; (else ; (error "invalid signal" s)))) ; (define (and-gate a1 a2 output) ; (define (and-action-procedure) ; (let ((new-value ; (logical-and (get-signal a1) ; (get-signal a2)))) ; (after delay and-gate-delay ; (lambda () ; (set-signal! output ; new-value))))) ; (add-action! a1 and-action-procedure) ; (add-action! a2 and-action-procedure)) ; 5B:(12m17s) => 5B:(18m5s) describing the underlying system. ; 5B:(19m20s) defining the mess (the wires). ; (define (make-wire) ; (let ((signal 0) (action-procs '())) ; (define (set-my-signal! new) ; (cond ((= signal new) 'done) ; (else ; (set! signal new) ; (call-each action-procs)))) ; (define (accept-action-proc proc) ; (set! action-procs ; (cons proc action-procs)) ; (proc)) ; (define (dispatch m) ; (cond ((eq? m 'get-signal) signal) ; ((eq? m 'set-signal!) ; set-my-signal!) ; ((eq? m 'add-action!) ; accept-action-proc) ; (else ; (error "bad message" m)))) ; (dispatch)) ; (define (call-each procedures) ; (cond ((null? procedures) 'done) ; (else ; ((car procedures)) ; (call-each (cdr procedures))))) ; (define (get-signal wire) ; (wire 'get-signal)) ; (define (set-signal! wire new-value) ; ((wire 'set-signal!) new-value)) ; (define (add-action! wire action-proc) ; ((wire 'add-action!) action-proc)) ; 5B:(26m47s) Defining the details. ; (define (after-delay delay action) ; (add-to-agenda! ; (+ delay (current-time the-agenda)) ; action ; the-agenda)) ; (define (propagate) ; (cond ((empty-agenda? the-agenda) ; 'done) ; (else ; ((first-item the-agenda)) ; (remove-first-item! the-agenda) ; (propagate)))) ; 5B:(27m) Running the simulator. ; BREAK 5B:(30m) ; AGENDAs ; (make-agenda) --> new agenda. ; (current-time agenda) --> time ; (empty-agenda? agenda) --> true/false ; (add-to-agenda! time action agenda) ; (first-item agenda) --> action ; (remove-first-item agenda) ; 5B:(35m32s) ; A thing (out of LISP-LISTS) ; 1.CAR -> *name* ; 1.CDR -> 2.CAR ; 2.CAR -> PAIR *segment* ; 1. -> *time* ; 2. -> STRUCT *queue* ; 2.CDR -> 3.CAR ; 3.CAR -> PAIR *segment* ; 1. -> *time* ; 2. -> STRUCT *queue* ; 3.CDR -> ; Now if you wanted to insert a new pair between 2.CAR and 3.CAR ; you would have to point the 2.CDR to 4.CAR and point 4.CDR to 3.CAR ; 1.CAR -> *name* ; 1.CDR -> 2.CAR ; 2.CAR -> PAIR *segment* ; 1. -> *time* ; 2. -> STRUCT *queue* ; 2.CDR -> 4.CAR ; 3.CAR -> PAIR *segment* ; 1. -> *time* ; 2. -> STRUCT *queue* ; 3.CDR -> ; 4.CAR -> PAIR *segment* ; 1. -> *time* ; 2. -> STRUCT *queue* ; 4.CDR -> 3.CAR ; 5B:(40m18s) implementing *queue* ;;; QUEUE ; (make-queue) --> new queue ; (insert-queue! queue item) ; (delete-queue! queue) ; (from-queue queue) ; (empty-queue? queue) ; 5B:(44m24s) ; (SET-CAR! ) ; (SET-CDR1 ) ; Perhaps this is a good time to open the TEXINFO-document and ; view node 3-3-1. It pretty neatly describes the mutation of lists with ; the new primitives. It also has some cool ASCII-Art. ; BREAK 5B:(45m45s) ; Jay states that a (CONS 1 2) has identity. ; And he terrifies his audience again... with more hot air. ; -> and the might of imagination ;;; Alonzo Church (+|1)04 June 1903 (-|0)11 August 1995 ;;; mathematician, logician, philosopher ;;; known for lambda-calculus. (DEFINE (CONS X Y) (LAMBDA (M) (M X Y))) (DEFINE (CAR X) (X (LAMBDA (A D) A))) (DEFINE (CDR X) (X (LAMBDA (A D) D))) ; he would not like the following, but we do it nevertheless: ;;; "Lambda Calculus" Mutable Data ; (define (cons x y) ; (lambda (m) ; (m x y ; (lambda (n) (set! x n)) ; (lambda (n) (set! y n))))) ; (define (car x) ; (x (lambda (a d sa sd) a))) ; (define (cdr x) ; (x (lambda (a d sa sd) d))) ; (define (set-car! x y) ; (x (lambda (a d sa sd) (sa y)))) ; (define (set-cdr! x y) ; (x (lambda (a d sa sd) (sd y)))) ; 5B:(53m2s) => 5B:(1h50s) Answering the question for this part: Is it ; possible to define them all with just SET! ? ; important QUESTIONS 5B:(1h1m) ; (END)