freeDead :: Set.Set Name -> Set.Set Name -> GenState ()
freeDead prev curr = 
    do 
      mstate <- get 
      let (smem,gmem,nt,table) = (shared mstate, 
                                  global mstate, 
                                  mm mstate, 
                                  ct mstate)  
          (smem',gmem') = 
              freeAll smem gmem table nt 
                          (Set.toList (prev `Set.difference` curr))
      put (mstate {shared = smem', global = gmem'})
      return ()
    where 
      freeAll smem gmem _ _ [] = (smem,gmem)
      freeAll smem gmem ct nt (x:xs) = 
          if ("source" `isPrefixOf` x) 
          then freeAll smem gmem ct nt xs
          else freeAll smem' gmem' ct nt xs
          where 
            address = 
                case Map.lookup x nt of 
                  Nothing -> error $ "error: freeDead, "++ 
                                     show x ++ 
                                     "not found in nt"
                  Just a -> fst a
            (smem',gmem') = 
                case typeof x ct of  
                  Global_Array t -> (smem,free gmem address)
                  Shared_Array t -> (free smem address,gmem)
                  Constant_Array t -> error "Impossible to free constant arrays" 
          
-- -----------------------------------------------------------------------------
freeDead :: Set.Set Name -> Set.Set Name -> GenState ()
freeDead prev curr = 
    do 
      mstate <- get 
      let (smem,gmem,nt,table) = (shared mstate, 
                                  global mstate, 
                                  mm mstate, 
                                  ct mstate)  
          (smem',gmem') = 
              freeAll smem gmem table nt 
                          (Set.toList (prev `Set.difference` curr))
      put (mstate {shared = smem', global = gmem'})
      return ()
    where 
      freeAll smem gmem _ _ [] = (smem,gmem)
      freeAll smem gmem ct nt (x:xs) = 
          if ("source" `isPrefixOf` x) 
          then freeAll smem gmem ct nt xs
          else freeAll smem' gmem' ct nt xs
          where 
            address = 
                case Map.lookup x nt of 
                  Nothing -> error $ "error: freeDead, "++ 
                                     show x ++ 
                                     "not found in nt"
                  Just a -> fst a
            (smem',gmem') = 
                case typeof x ct of  
                  Global_Array t -> (smem,free gmem address)
                  Shared_Array t -> (free smem address,gmem)
                  Constant_Array t -> error "Impossible to free constant arrays" 
          
-- -----------------------------------------------------------------------------
freeDead prev curr = 
    do 
      mstate <- get 
      let (smem,gmem,nt,table) = (shared mstate, 
                                  global mstate, 
                                  mm mstate, 
                                  ct mstate)  
          (smem',gmem') = 
              freeAll smem gmem table nt 
                          (Set.toList (prev `Set.difference` curr))
      put (mstate {shared = smem', global = gmem'})
      return ()
    where 
      freeAll smem gmem _ _ [] = (smem,gmem)
      freeAll smem gmem ct nt (x:xs) = 
          if ("source" `isPrefixOf` x) 
          then freeAll smem gmem ct nt xs
          else freeAll smem' gmem' ct nt xs
          where 
            address = 
                case Map.lookup x nt of 
                  Nothing -> error $ "error: freeDead, "++ 
                                     show x ++ 
                                     "not found in nt"
                  Just a -> fst a
            (smem',gmem') = 
                case typeof x ct of  
                  Global_Array t -> (smem,free gmem address)
                  Shared_Array t -> (free smem address,gmem)
                  Constant_Array t -> error "Impossible to free constant arrays" 
          
-- -----------------------------------------------------------------------------