calcSharedLocationsPure :: TypeEnv -> Exp -> Set Loc
calcSharedLocationsPure TypeEnv{..} e = converge (==) (Set.concatMap fetchLocs) origShVarLocs where
  nonLinearVars = calcNonLinearNonUpdateLocVariables e
  shVarTypes    = Set.mapMaybe (`Map.lookup` _variable) $ nonLinearVars
  origShVarLocs = onlyLocations . onlySimpleTys $ shVarTypes

  onlySimpleTys :: Set Type -> Set SimpleType
  onlySimpleTys tys = Set.fromList [ sty | T_SimpleType sty <- Set.toList tys ]

  onlyLocations :: Set SimpleType -> Set Loc
  onlyLocations stys = Set.fromList $ concat [ ls | T_Location ls <- Set.toList stys ]

  fetchLocs :: Loc -> Set Loc
  fetchLocs l = onlyLocations . fieldsFromNodeSet . fromMaybe (error msg) . (Vec.!?) _location $ l
    where msg = "Sharing: Invalid heap index: " ++ show l

  fieldsFromNodeSet :: NodeSet -> Set SimpleType
  fieldsFromNodeSet = Set.fromList . concatMap Vec.toList . Map.elems
calcSharedLocationsPure :: TypeEnv -> Exp -> Set Loc
calcSharedLocationsPure TypeEnv{..} e = converge (==) (Set.concatMap fetchLocs) origShVarLocs where
  nonLinearVars = calcNonLinearNonUpdateLocVariables e
  shVarTypes    = Set.mapMaybe (`Map.lookup` _variable) $ nonLinearVars
  origShVarLocs = onlyLocations . onlySimpleTys $ shVarTypes

  onlySimpleTys :: Set Type -> Set SimpleType
  onlySimpleTys tys = Set.fromList [ sty | T_SimpleType sty <- Set.toList tys ]

  onlyLocations :: Set SimpleType -> Set Loc
  onlyLocations stys = Set.fromList $ concat [ ls | T_Location ls <- Set.toList stys ]

  fetchLocs :: Loc -> Set Loc
  fetchLocs l = onlyLocations . fieldsFromNodeSet . fromMaybe (error msg) . (Vec.!?) _location $ l
    where msg = "Sharing: Invalid heap index: " ++ show l

  fieldsFromNodeSet :: NodeSet -> Set SimpleType
  fieldsFromNodeSet = Set.fromList . concatMap Vec.toList . Map.elems
calcSharedLocationsPure TypeEnv{..} e = converge (==) (Set.concatMap fetchLocs) origShVarLocs where
  nonLinearVars = calcNonLinearNonUpdateLocVariables e
  shVarTypes    = Set.mapMaybe (`Map.lookup` _variable) $ nonLinearVars
  origShVarLocs = onlyLocations . onlySimpleTys $ shVarTypes

  onlySimpleTys :: Set Type -> Set SimpleType
  onlySimpleTys tys = Set.fromList [ sty | T_SimpleType sty <- Set.toList tys ]

  onlyLocations :: Set SimpleType -> Set Loc
  onlyLocations stys = Set.fromList $ concat [ ls | T_Location ls <- Set.toList stys ]

  fetchLocs :: Loc -> Set Loc
  fetchLocs l = onlyLocations . fieldsFromNodeSet . fromMaybe (error msg) . (Vec.!?) _location $ l
    where msg = "Sharing: Invalid heap index: " ++ show l

  fieldsFromNodeSet :: NodeSet -> Set SimpleType
  fieldsFromNodeSet = Set.fromList . concatMap Vec.toList . Map.elems