(* ---------- Storage   ---------- *)
(* 
fun deallocate sto loc:Location = sto;
*)

type Storable = int;
type Location = int;
exception fail;

datatype Sval  = stored of Storable | undef | unused;

	      (*          --bot---   --top---   ----data--------- *)
datatype Store = store of Location * Location * (Location -> Sval);

val sto_init  =  fn loc:Location => unused;
val sto_null  =  store( 1, 0, sto_init);

fun update (store(bot,top,data):Store, loc:Location, v:Storable):Store = 
	let fun new adr =  if adr=loc
			   then stored v else (data adr);
	in store( bot, top, new)
	end;
                                            
		(* fetch from store, and convert into Storable (=Value) *)
fun fetch  (store(bot,top,data), loc:Location): Storable  =
	let fun stored_value(stored stble) = stble
	      | stored_value(unused)       = raise fail
	      | stored_value(undef)        = raise fail
	in stored_value( data loc)
	end;

		(* create a new "undefined" location in a store *)
fun allocate ( store(bot,top,data) ): Store * Location  =
	let val newtop = top+1
	    fun new adr =  if adr=newtop
			   then undef else (data adr);
	in (store( bot, newtop, new), newtop)
	end;

(* 
 * These 2 functions retrieve the store and the location of a
 * new allocation
 *)
fun getStore ( store(bot, top, data), loc:Location) = store(bot, top, data);
fun getLoc   ( store(bot, top, data), loc:Location) = loc;


    Source: geocities.com/htrefftz/mum/cs505/smlExamples

               ( geocities.com/htrefftz/mum/cs505)                   ( geocities.com/htrefftz/mum)                   ( geocities.com/htrefftz)