<commit_before>{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}

module Rename where



import Control.Monad.State
import Control.Monad.Writer
import Data.Foldable (Foldable)
import qualified Data.Foldable as Foldable
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.List
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable (traverse)

import Graph
import Paper



data (f :&: ann) a = f a :&: ann
  deriving (Eq, Show, Functor)

-- | Variable aliasing environment
type Aliases = [(Name,Name)]  -- [(oldName, newName)]

-- | Record of previously renamed nodes<commit_msg>Comment on how the renamer works<commit_after>{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}

-- | This module implements a renamer for graphs such that each binder introduces a unique variable
-- name.
--
-- The algorithm works as follows:
--
-- * By default, the new graph is created by copying nodes from the original graph and renaming
--   binders (and corresponding variables) to be unique.
--
-- * An exception is made if the node has already been copied under a compatible set of renamings.
--   In that case, a reference to the previous copy is returned.
--
-- Without the exception, we would loose all sharing in the original graph (the new graph would
-- always be a tree). The exception makes sure that we retain sharing where possible. However, it is
-- generally not possible to retain all sharing, since shared sub-trees may appear in contexts with
-- incompatible renamings (see e.g. example @g3@ in Paper.hs).

module Rename where



import Control.Monad.State
import Control.Monad.Writer
import Data.Foldable (Foldable)
import qualified Data.Foldable as Foldable
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.List
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable (traverse)

import Graph
import Paper



data (f :&: ann) a = f a :&: ann
  deriving (Eq, Show, Functor)

-- | Variable aliasing environment
type Aliases = [(Name,Name)]  -- [(oldName, newName)]

-- | Record of previously renamed nodes
{'fraction_non_alphanumeric': 0.09165585819282317, 'fraction_numerical': 0.00043233895373973193, 'mean_word_length': 4.030434782608696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6141552', 'n_tokens_mistral': 699, 'n_tokens_neox': 674, 'n_words': 290}