(* 
 File: Identifiers.sml

 Globals etc to deal with rate identifiers, set identifiers etc
*)

structure Identifiers :> Identifiers =
struct
   
   type id = string;

   exception idnotintable;

(*    val deletemelist = ref ([]:string list); *)

   fun sameIDs (k1:id,k2:id) = (k1 = k2);

   val idtablesize = 40;

   (* crappy hash function! *)
   fun hashID k =  foldl (op +) 0 (map Char.ord (explode k));
      
   fun makeID s = s;
   fun printID s = s;

   val setids:((id * (Pepa.Identifier list)) list) ref =
(*       ((id, Pepa.Identifier list) *)
(* 	       Polyhash.hash_table) = *)
      (ref []);
(*       Polyhash.mkTable (hashID, sameIDs) *)
(*       (idtablesize, idnotintable); *)
      
(*    val expids:((id * (Expressions.Aexp)) list) ref = *)
(* (*       ((Id, Expressions.Aexp) *) *)
(* (* 	       Polyhash.Hash_table) = *) *)
(*       (ref []); *)
(* (*       Polyhash.mkTable (hashID, sameIDs) *) *)
(* (*       (idtablesize, idnotintable); *) *)
      
   val rewspecids:((id * (RewLang.RewSpec)) list) ref =
      (ref []);

   val attachmentids:((id * (RewLang.Attachment)) list) ref =
      (ref []);


   fun printSetIDs () = 
      (map
      (fn (k,d) =>
       let
	  val ks = printID k
	  val ds = foldl (fn (a,b) => ((HashTable.unhash(a))^", "^b)) "" d
       in
	  TextIO.output(TextIO.stdOut, "id "^ks^" is "^ds^"\n")
       end)
      (!setids); ());

(*    fun printExpIDs () =  *)
(*       (map *)
(*       (fn (k,d) => *)
(*        let *)
(* 	  val ks = printID k *)
(* 	  val ds = Expressions.toString d *)
(*        in *)
(* 	  TextIO.output(TextIO.stdOut, "id "^ks^" is "^ds^"\n") *)
(*        end) *)
(*       (!expids);()); *)

   fun getThing k [] = raise idnotintable
     | getThing k ((k2,d)::t) = 
       if (k=k2) then
	  d
       else
	  getThing k t
	  
   fun addSetID k d =
      (
       (* TextIO.output(TextIO.stdOut, "adding "^(printID k)^" which equals " *)
(* 		     (* ^(Prettyprinter.print Prettyprinter.verbose *) *)
(* (* 		       (Pepa.HIDING((Pepa.VAR *) *)
(* (* 				     (HashTable.hash "dummy")), d))) *)^"\n"); *)
(*       TextIO.flushOut(TextIO.stdOut); *)
      (setids := (k,d)::(!setids)));

   fun getSet k = getThing k (!setids)

(*    fun addExpID k d = *)
(*       (TextIO.output(TextIO.stdOut, "adding "^(printID k)^" which equals " *)
(* 		     ^(Expressions.toString d)^"\n"); *)
(*        TextIO.flushOut(TextIO.stdOut); *)
(*        (expids := (k,d)::(!expids))); *)

(*    fun getExp k = getThing k (!expids) *)

   fun addRewSpecID k d =
      (
       (* TextIO.output(TextIO.stdOut, "adding (rewspec) "^(printID k)^" which equals " *)
(* 		     ^(RewLang.rewSpecToString d)^"\n"); *)
(*        TextIO.flushOut(TextIO.stdOut); *)
       (rewspecids := (k,d)::(!rewspecids)));

   fun getRewSpec k = getThing k (!rewspecids)

   fun addAttachmentID k d =
      (
(*        TextIO.output(TextIO.stdOut, "adding (attach) "^(printID k)^" which equals " *)
(* 		     ^(RewLang.attachmentToString d)^"\n"); *)
(*        TextIO.flushOut(TextIO.stdOut); *)
       (attachmentids := (k,d)::(!attachmentids)));

   fun getAttachment k = getThing k (!attachmentids)



(*    fun subForIDs (Expressions.PLUS(e1,e2)) = (Expressions.PLUS((subForIDs e1), (subForIDs e2))) *)
(*      | subForIDs (Expressions.DIFF(e1,e2)) = (Expressions.DIFF((subForIDs e1), (subForIDs e2))) *)
(*      | subForIDs (Expressions.PROD(e1,e2)) = (Expressions.PROD((subForIDs e1), (subForIDs e2))) *)
(*      | subForIDs (Expressions.QUOT(e1,e2)) = (Expressions.QUOT((subForIDs e1), (subForIDs e2))) *)
(*      | subForIDs (Expressions.MIN(e1,e2)) = (Expressions.MIN((subForIDs e1), (subForIDs e2))) *)
(*      | subForIDs (e as (Expressions.IDENT s)) = *)
(*       ((getExp(makeID(s))) *)
(*        handle _ => e) *)
(*      | subForIDs x = x; *)
	 

   val ididx = ref 1;

   fun newDummyID () =
      let
	 val i = (!ididx)
	 val _ = (ididx := (!ididx) + 1)
      in
	 "gcla-dummy-"^(Int.toString i)
      end;

   (* gcla later - this has to be fixed - dependencies causing
   problems, this should NEVER be here!! *)

(*    val eval = Expressions.evaluate o Expressions.simplify o subForIDs *)

end;
