import ( qaqao:collections:Collection, qaqao:collections:structures:One-Way-Linked-Component, qaqao:cursors:Forward-Cursor ), // Cells are included in the standard environment interface ( [Stack (Item-type Type)] [Collection-Type ( supertypes « (Bounded-Collection (Item-type « Item-type)), instance « ( receives « ( [push (item Item-type)] ( ensures « ([¬[instance ← empty?]]) ), peek ( requires « ([¬[instance ← empty?]] else-throws No-Such-Element), replies Item-type ), pop ( requires « ([¬[instance ← empty?]] else-throws No-Such-Element), replies Item-type ) ) ) receives « ( make-stack (replies [Stack (Item-type « Item-type)]) ) )] ), implementation ( [Stack (Item-type Type)] [Collection-Type ( instance « ( representation « ( top [Cell (Item-type « [Stack-component (Item-type « Item-type)])], size [Cell (Item-type « Integer+)] ), receives « ( [push (item Item-type)] ( does « { top ← [set-item (item « make-filled-component (item « item, structure « [top ← get-item))]]; size ← [set-item (item « [[size ← get-item] + 1]] } ), peek ( replies « [top ← get-item] ), pop ( does « { component-to-pop « [top ← get-item]; top ← [set-item (item « component-to-pop:next)]; size ← [set-item (item « [[size ← get-item] − 1]] }, replies « component-to-pop:item ), size? (replies « size), get-Cursor ( replies [[top ← get-item] ← get-Cursor] ) ) ) receives « ( make-stack ( does « { stack « [memory ← [allocate ( top « [[Cell (Item-type « [Stack-component (Item-type « Item-type)])] ← make-cell], size « [[Cell (Item-type « Integer+)] ← make-cell] )]]; stack:top ← [set-item (item « [[Stack-component (Item-type « Item-type)] ← make-empty-component])]; stack:size ← 0 } replies « stack ) ) )], [[Stack-component (Item-type Type)] One-way-linked-component-Type] « [One-way-linked-component (Item-type « Item-type, Structure-type « Stack-component)] )