<commit_before>-- |
-- Module      : $Header$
-- Description : Passes over algorithm language terms to ensure certain invariants
-- Copyright   : (c) <NAME> 2017. All Rights Reserved.
-- License     : EPL-1.0
-- Maintainer  : <EMAIL>, <EMAIL>
-- Stability   : experimental
-- Portability : portable

-- This source code is licensed under the terms described in the associated LICENSE.TXT file
module Ohua.ALang.Passes where


import           Control.Monad.Except
import           Control.Monad.Reader
import           Control.Monad.RWS.Lazy
import           Control.Monad.State
import           Control.Monad.Writer
import qualified Data.HashMap.Strict    as HM
import qualified Data.HashSet           as HS
import           Ohua.ALang.Lang
import           Ohua.ALang.Util
import           Ohua.IR.Functions
import           Ohua.Monad
import           Ohua.Types



type Error = String


-- | Inline all references to lambdas.
-- Aka `let f = (\a -> E) in f N` -> `(\a -> E) N`
inlineLambdaRefs :: MonadError Error m => Expression -> m Expression
inlineLambdaRefs (Let assignment l@(Lambda _ _) body) =
    case assignment of
        Direct bnd -> inlineLambdaRefs $ substitute bnd l body
        _ -> throwError "invariant broken, cannot destructure lambda"
inlineLambdaRefs (Let assignment value body) = Let assignment <$> inlineLambdaRefs value <*> inlineLambdaRefs body
inlineLambdaRefs (Apply body argument) = liftM2 Apply (inlineLambdaRefs body) (inlineLambdaRefs argument)
inlineLambdaRefs (Lambda argument body) = Lambda argument <$> inlineLambdaRefs body
inlineLambdaRefs e = return e


-- | Reduce lambdas by simulating application
-- Aka `(\a -> E) N` -> `let a = N in E`
-- Assumes lambda refs have been inlined<commit_msg>prevent inlining for recursive lambdas.(issue #3)<commit_after>-- |
-- Module      : $Header$
-- Description : Passes over algorithm language terms to ensure certain invariants
-- Copyright   : (c) <NAME> 2017. All Rights Reserved.
-- License     : EPL-1.0
-- Maintainer  : <EMAIL>, <EMAIL>
-- Stability   : experimental
-- Portability : portable

-- This source code is licensed under the terms described in the associated LICENSE.TXT file
module Ohua.ALang.Passes where


import           Control.Monad.Except
import           Control.Monad.Reader
import           Control.Monad.RWS.Lazy
import           Control.Monad.State
import           Control.Monad.Writer
import qualified Data.HashMap.Strict    as HM
import qualified Data.HashSet           as HS
import           Ohua.ALang.Lang
import           Ohua.ALang.Util
import           Ohua.IR.Functions
import           Ohua.Monad
import           Ohua.Types



type Error = String


-- | Inline all references to lambdas.
-- Aka `let f = (\a -> E) in f N` -> `(\a -> E) N`
inlineLambdaRefs :: MonadError Error m => Expression -> m Expression
inlineLambdaRefs (Let assignment l@(Lambda _ _) body) =
    case assignment of
        Direct bnd -> inlineLambdaRefs $ substitute bnd l body
        Recursive bnd -> return $ Let assignment l body
        _ -> throwError "invariant broken, cannot destructure lambda"
inlineLambdaRefs (Let assignment value body) = Let assignment <$> inlineLambdaRefs value <*> inlineLambdaRefs body
inlineLambdaRefs (Apply body argument) = liftM2 Apply (inlineLambdaRefs body) (inlineLambdaRefs argument)
inlineLambdaRefs (Lambda argument body) = Lambda argument <$> inlineLambdaRefs body
inlineLambdaRefs e = return e


-- | Reduce lambdas by simulating application
-- Aka `(\a -> E) N` -> `let a = N in E`
-- Assumes lambda refs have been inlined
{'fraction_non_alphanumeric': 0.08884529147982063, 'fraction_numerical': 0.004204035874439462, 'mean_word_length': 2.866738894907909, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 34, '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': '2850104', 'n_tokens_mistral': 1027, 'n_tokens_neox': 990, 'n_words': 365}