sInVars :: (a -> ST ()) -> a -> ST (Either ByteString ByteString)
isInVars parser x= ST(\(Stat(c,s,v))->
 let mf = trytofindEntireObject x c in
 case mf of
   Just  var ->  Right(Stat(c,s,v),Right var)
   Nothing ->
         let
            ST f= parser x
            Right (Stat (c',str,_), _) = f  (Stat(c,"",v))

         in Right(Stat(addc str c',s ,v), Left varname))
 where
  addc str c= insert ( hash) (st,unsafeCoerce x,  str) c
  (hash,st) = hasht x
  varname=  pack$ "v" ++ show hash

  trytofindEntireObject x c=
         case Data.RefSerialize.Serialize.lookup  hash  c  of
           Nothing -> Nothing
           Just _  -> Just varname



-- | deserialize a variable serialized with insertVar. Memory references are restored

isInVars :: (a -> ST ()) -> a -> ST (Either ByteString ByteString)
isInVars parser x= ST(\(Stat(c,s,v))->
 let mf = trytofindEntireObject x c in
 case mf of
   Just  var ->  Right(Stat(c,s,v),Right var)
   Nothing ->
         let
            ST f= parser x
            Right (Stat (c',str,_), _) = f  (Stat(c,"",v))

         in Right(Stat(addc str c',s ,v), Left varname))
 where
  addc str c= insert ( hash) (st,unsafeCoerce x,  str) c
  (hash,st) = hasht x
  varname=  pack$ "v" ++ show hash

  trytofindEntireObject x c=
         case Data.RefSerialize.Serialize.lookup  hash  c  of
           Nothing -> Nothing
           Just _  -> Just varname



-- | deserialize a variable serialized with insertVar. Memory references are restored
isInVars parser x= ST(\(Stat(c,s,v))->
 let mf = trytofindEntireObject x c in
 case mf of
   Just  var ->  Right(Stat(c,s,v),Right var)
   Nothing ->
         let
            ST f= parser x
            Right (Stat (c',str,_), _) = f  (Stat(c,"",v))

         in Right(Stat(addc str c',s ,v), Left varname))
 where
  addc str c= insert ( hash) (st,unsafeCoerce x,  str) c
  (hash,st) = hasht x
  varname=  pack$ "v" ++ show hash

  trytofindEntireObject x c=
         case Data.RefSerialize.Serialize.lookup  hash  c  of
           Nothing -> Nothing
           Just _  -> Just varname



-- | deserialize a variable serialized with insertVar. Memory references are restored