09-extra

09-extra - Enforcing ADTs Can we make our implementations...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Enforcing ADTs Can we make our implementations of ADTs resemble built-in types? An ADT implementation should not permit: tampering (accessing/changing values not using ADT operations); forgery (creating values not using ADT operations). CS 136 Winter 2009 09: Extra on other languages 1 A mutable stack ADT implementation ( define ( make-stack ) ( define stack empty ) ( lambda ( s ) ( cond [( symbol = ? s push ) ( lambda ( item ) ( set! stack ( cons item stack )))] [( symbol = ? s pop ) ( begin0 ( first stack ) ( set! stack ( rest stack )))] [( symbol = ? s top ) ( first stack )] [( symbol = ? s empty? ) ( empty? stack )] [ else ( error " unknown stack operation a \ n " s )]))) CS 136 Winter 2009 09: Extra on other languages 2 ( define ( push s item ) (( s push ) item )) ( define ( pop s ) ( s pop )) ( define ( top s ) ( s top )) ( define ( empty-stack? s ) ( s empty? )) This permits forgery, but not tampering. An alternative is provided by the combination of modules and opaque (non-transparent) structures in PLT Scheme. CS 136 Winter 2009 09: Extra on other languages 3 Heres an implementation of the immutable stack ADT. In file im-stack.ss : #lang scheme ( provide make-stack push pop top ) ( define-struct imst ( lst )) ( define ( make-stack ) ( make-imst empty )) ( define ( push s item ) ( make-imst ( cons item ( imst-lst s )))) ( define ( pop s ) ( make-imst ( rest ( imst-lst s )))) ( define ( top s ) ( first ( imst-lst s ))) ( define ( stack-empty? s ) ( empty? ( imst-lst s ))) CS 136 Winter 2009 09: Extra on other languages 4 In file use-im-stack.ss : ( require " im-stack.ss " ) ( define mystk1 ( make-stack )) ( define mystk2 ( push 3 mystk1 )) . . . This permits neither forgery nor tampering. In C, we can get close using typedef , incomplete types , and the fact that pointers all have the same size (see CP:AMA, chapter 19). CS 136 Winter 2009 09: Extra on other languages 5 Towards classes Advantages of our Scheme ADTs: We can declare local (state) variables which are not directly accessible, only indirectly by using functions we write; We can provide as many such functions as we wish. C++ is an extension of C designed to facilitate data abstraction. It provides these two advantages and more. In C++ structs, we can declare some fields to be private (not directly accessible), and we can associate both public and private functions with structs....
View Full Document

This note was uploaded on 08/31/2011 for the course CS 136 taught by Professor Becker during the Fall '08 term at Waterloo.

Page1 / 24

09-extra - Enforcing ADTs Can we make our implementations...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online