<commit_before>
module Transform.SortDefinitions (sortDefs, boundVars) where

import Control.Monad.State
import Control.Applicative ((<$>))
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified SourceSyntax.Type as ST
import SourceSyntax.Everything
import qualified Data.Graph as Graph
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe

boundVars :: Pattern -> Set.Set String
boundVars pattern =
    case pattern of
      PVar x -> Set.singleton x
      PAlias x p -> Set.insert x (boundVars p)
      PData _ ps -> Set.unions (map boundVars ps)
      PRecord fields -> Set.fromList fields
      PAnything -> Set.empty
      PLiteral _ -> Set.empty

free :: String -> State (Set.Set String) ()
free x = modify (Set.insert x)

bound :: Set.Set String -> State (Set.Set String) ()
bound boundVars = modify (\freeVars -> Set.difference freeVars boundVars)

sortDefs :: LExpr t v -> LExpr t v
sortDefs expr = evalState (reorder expr) Set.empty

reorder :: LExpr t v -> State (Set.Set String) (LExpr t v)<commit_msg>Add a way to flatten lets that have become nested<commit_after>
module Transform.SortDefinitions (sortDefs, boundVars, flattenLets) where

import Control.Monad.State
import Control.Applicative ((<$>))
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified SourceSyntax.Type as ST
import SourceSyntax.Everything
import qualified Data.Graph as Graph
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe

boundVars :: Pattern -> Set.Set String
boundVars pattern =
    case pattern of
      PVar x -> Set.singleton x
      PAlias x p -> Set.insert x (boundVars p)
      PData _ ps -> Set.unions (map boundVars ps)
      PRecord fields -> Set.fromList fields
      PAnything -> Set.empty
      PLiteral _ -> Set.empty

free :: String -> State (Set.Set String) ()
free x = modify (Set.insert x)

bound :: Set.Set String -> State (Set.Set String) ()
bound boundVars = modify (\freeVars -> Set.difference freeVars boundVars)

sortDefs :: LExpr t v -> LExpr t v
sortDefs expr = evalState (reorder expr) Set.empty

flattenLets defs lexpr@(L _ _ expr) =
    case expr of
      Let ds body -> flattenLets (defs ++ ds) body
      _ -> (defs, lexpr)


reorder :: LExpr t v -> State (Set.Set String) (LExpr t v)
{'fraction_non_alphanumeric': 0.09304347826086956, 'fraction_numerical': 0.0, 'mean_word_length': 3.4593023255813953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '2348890', 'n_tokens_mistral': 692, 'n_tokens_neox': 701, 'n_words': 269}