<commit_before>--------------------------------------------------------------------------------
module WhatMorphism.Fusion
    ( foldFoldFusion
    , isListFold
    ) where


--------------------------------------------------------------------------------
import           CoreSyn
import qualified PrelNames             as PrelNames
import           Type                  (Type)
import           Var                   (Var)
import qualified Var                   as Var


--------------------------------------------------------------------------------
import           WhatMorphism.Dump
import           WhatMorphism.RewriteM


--------------------------------------------------------------------------------
foldFoldFusion :: Expr Var -> RewriteM (Expr Var)
foldFoldFusion = undefined


--------------------------------------------------------------------------------
data FoldSpec = FoldSpec
    { foldReturnType :: Type
    , foldAlgebra    :: [Expr Var]
    , foldDestroys   :: Expr Var
    }


--------------------------------------------------------------------------------
instance Dump FoldSpec where
    dump (FoldSpec r a d) =
        "(FoldSpec " ++ dump r ++ " " ++ dump a ++ " " ++ dump d ++ ")"


--------------------------------------------------------------------------------
isListFold :: Expr Var -> Maybe FoldSpec

isListFold (App (App (App (App (App (Var foldrVar) _) rTyp) cons) nilF) d)
    | Var.varName foldrVar /= PrelNames.foldrName = Nothing
    | otherwise                                   = do
        rTyp' <- fromType rTyp
        return FoldSpec
            { foldReturnType = rTyp'
            , foldAlgebra    = [cons, nilF]
            , foldDestroys   = d
            }
  where
    fromType (Type t) = Just t
    fromType _        = Nothing

isListFold _                                      = Nothing<commit_msg>We need the actual variable as well<commit_after>--------------------------------------------------------------------------------
module WhatMorphism.Fusion
    ( foldFoldFusion
    , isListFold
    ) where


--------------------------------------------------------------------------------
import           CoreSyn
import qualified PrelNames             as PrelNames
import           Type                  (Type)
import           Var                   (Var)
import qualified Var                   as Var


--------------------------------------------------------------------------------
import           WhatMorphism.Dump
import           WhatMorphism.RewriteM


--------------------------------------------------------------------------------
foldFoldFusion :: Expr Var -> RewriteM (Expr Var)
foldFoldFusion = undefined


--------------------------------------------------------------------------------
data FoldSpec = FoldSpec
    { foldFunction   :: Var
    , foldReturnType :: Type
    , foldAlgebra    :: [Expr Var]
    , foldDestroys   :: Expr Var
    }


--------------------------------------------------------------------------------
instance Dump FoldSpec where
    dump (FoldSpec f r a d) =
        "(FoldSpec " ++ unwords [dump f, dump r, dump a, dump d] ++ ")"


--------------------------------------------------------------------------------
isListFold :: Expr Var -> Maybe FoldSpec

isListFold (App (App (App (App (App (Var foldrVar) _) rTyp) cons) nilF) d)
    | Var.varName foldrVar /= PrelNames.foldrName = Nothing
    | otherwise                                   = do
        rTyp' <- fromType rTyp
        return FoldSpec
            { foldFunction   = foldrVar
            , foldReturnType = rTyp'
            , foldAlgebra    = [cons, nilF]
            , foldDestroys   = d
            }
  where
    fromType (Type t) = Just t
    fromType _        = Nothing

isListFold _                                      = Nothing
{'fraction_non_alphanumeric': 0.34791776489193466, 'fraction_numerical': 0.0, 'mean_word_length': 0.7496542185338866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6947575', 'n_tokens_mistral': 843, 'n_tokens_neox': 792, 'n_words': 229}