<commit_before>{-# LANGUAGE NoImplicitPrelude #-}
module FOL.Language.Tidy where

import FOL.Language.Common
import FOL.Language.Expression

import Data.List
import Data.Generics.Uniplate.Operations

import Control.Monad

tidyExpr :: Expr -> Expr
tidyExpr = rewrite rule
    where
      rule e = msum (map ($ e) tidyRules)

tidyRules :: [Expr -> Maybe Expr]
tidyRules = [tidyLetValues]

tidyLetValues :: Expr -> Maybe Expr
tidyLetValues e@(LetValues (Bindings bs) body)
    | null bs1
    = Nothing
    | otherwise
    = Just $ mkLet bs1' $ mkLetValues bs2 body
    where
      (bs1, bs2) = partition is_values bs
      bs1' = concat [zip xs es | (xs, Values es) <- bs1]
      is_values (_, Values _) = True
      is_values _             = False
tidyLetValues _ = Nothing

tidyDefn :: Defn -> Defn
tidyDefn (Defn proc args body) = Defn proc args (tidyExpr body)

tidyProg :: Prog -> Prog
tidyProg (Prog defns expr) = Prog (map tidyDefn defns) (tidyExpr expr)

tidy :: Prog -> Prog
tidy = tidyProg<commit_msg>Make the tidier flush unused bindings in LET and LET-VALUES.<commit_after>{-# LANGUAGE NoImplicitPrelude #-}
module FOL.Language.Tidy where

import FOL.Language.Common
import FOL.Language.Expression

import Data.List
import Data.Generics.Uniplate.Operations

import Control.Monad

tidyExpr :: Expr -> Expr
tidyExpr = rewrite rule
    where
      rule e = msum (map ($ e) tidyRules)

tidyRules :: [Expr -> Maybe Expr]
tidyRules = [tidyLetValues, flushUnusedBindings]

tidyLetValues :: Expr -> Maybe Expr
tidyLetValues e@(LetValues (Bindings bs) body)
    | null bs1
    = Nothing
    | otherwise
    = Just $ mkLet bs1' $ mkLetValues bs2 body
    where
      (bs1, bs2) = partition is_values bs
      bs1' = concat [zip xs es | (xs, Values es) <- bs1]
      is_values (_, Values _) = True
      is_values _             = False
tidyLetValues _ = Nothing

flushUnusedBindings :: Expr -> Maybe Expr
flushUnusedBindings (Let (Bindings bs) body)
    | bs == bs'
    = Nothing
    | otherwise
    = Just $ mkLet bs' body
    where
      fvs = fv body
      bs' = [(x, e) | (x, e) <- bs, x `elem` fvs]
flushUnusedBindings (LetValues (Bindings bs) body)
    | bs == bs'
    = Nothing
    | otherwise
    = Just $ mkLetValues bs' body
    where
      fvs = fv body
      bs' = [(xs, e) | (xs, e) <- bs, not (null (xs `intersect` fvs))]
flushUnusedBindings _ = Nothing

tidyDefn :: Defn -> Defn
tidyDefn (Defn proc args body) = Defn proc args (tidyExpr body)

tidyProg :: Prog -> Prog
tidyProg (Prog defns expr) = Prog (map tidyDefn defns) (tidyExpr expr)

tidy :: Prog -> Prog
tidy = tidyProg
{'fraction_non_alphanumeric': 0.10579150579150579, 'fraction_numerical': 0.005405405405405406, 'mean_word_length': 3.0421216848673946, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '6368811', 'n_tokens_mistral': 976, 'n_tokens_neox': 924, 'n_words': 283}