;; Sample base file for the circuit comparator exercises ;; See the project handout for details on what the classes represent ;; and how they can be used. ;; Kathi Fisler, March 2001 (require-library "function.ss") ;; interfaces (define NamedEltI (interface () get-name)) (define CircOutputI (interface () get-gate)) (define BinaryGateI (interface () get-in1 get-in2)) (define UnaryGateI (interface () get-in)) (define CircuitI (interface () get-inputs get-outputs get-input-names get-output-names find-output-elt)) ;; classes ;; inputs is a (Scheme) list of InputElt objects ;; outputs is a (Scheme) list of OutputElt objects (define Circuit% (class* object% (CircuitI) (inputs outputs) (public (get-inputs (lambda () inputs)) (get-outputs (lambda () outputs)) (get-input-names (lambda () (map (lambda (inp) (send inp get-name)) inputs))) (get-output-names (lambda () (map (lambda (outp) (send outp get-name)) outputs))) (find-output-elt (lambda (for-name) (let ([find-elts (filter (lambda (outp) (string=? for-name (send outp get-name))) outputs)]) (when (null? find-elts) (raise-no-matching-output-exn for-name)) (car find-elts))))) (sequence (super-init)))) ;; name is a string ;; circ is a CircElt objects (define OutputElt% (class* object% (NamedEltI) (name circ) (public (get-name (lambda () name)) (get-gate (lambda () circ))) (sequence (super-init)))) (define CircElt% (class* object% () () (sequence (super-init)))) ;; name is a string (define InputElt% (class* CircElt% (NamedEltI) (name) (public (get-name (lambda () name))) (sequence (super-init)))) (define Gate% (class* CircElt% () () (sequence (super-init)))) ;; in1 and in2 are CircElt objects (define BinaryGate% (class* Gate% (BinaryGateI) (in1 in2) (public (get-in1 (lambda () in1)) (get-in2 (lambda () in2))) (sequence (super-init)))) ;; in is a CircElt object (define UnaryGate% (class* Gate% (UnaryGateI) (in) (public (get-in (lambda () in))) (sequence (super-init)))) (define AndGate% (class* BinaryGate% () (in1 in2) (sequence (super-init in1 in2)))) (define OrGate% (class* BinaryGate% () (in1 in2) (sequence (super-init in1 in2)))) (define NotGate% (class* UnaryGate% () (in) (sequence (super-init in)))) ;; Variable assignments ;; A var-assign represents the assignment of a boolean value to a ;; variable. The variable is represented by a string (for its name) ;; and boolean values are reprsented by either 0 or 1. (define-struct var-assign (var val)) ;; exceptions ;; no matching output exceptions. Contain name of output that isn't ;; found in the circuit ;; exception raised when search for an output fails ;; name is the string for the name of the output that wasn't found (define-struct NoMatchingOutputExn (name)) (define (raise-no-matching-output-exn name) (raise (make-NoMatchingOutputExn name))) ;; exception raised when circuit equivalence fails because the two ;; circuits have different output variables ;; name-list is a list of names that are not common to both circuits (define-struct OutputMismatchExn (name-list)) (define (raise-output-mismatch-exn name-list) (raise (make-OutputMismatchExn name-list))) ;; exception raised when circuit equivalence fails because the two ;; circuits have different input variables ;; name-list is a list of names that are not common to both circuits (define-struct InputMismatchExn (name-list)) (define (raise-input-mismatch-exn name-list) (raise (make-InputMismatchExn name-list))) ;; exception raised when circuit equivalence fails because the two ;; circuits compute different functions for the same output ;; output-name is a string for the name of the output for which the ;; circuits are not equivalent ;; countereg is a (scheme) list of var-assigns (define-struct FunctionMismatchExn (output-name countereg)) (define (raise-function-mismatch-exn output-name countereg) (raise (make-FunctionMismatchExn output-name countereg)))