<commit_before>{-# OPTIONS_GHC -fno-warn-orphans -fno-warn-missing-methods #-}

module DFLowering where

import Ohua.Prelude

import Data.Graph.Inductive.Graph
import Data.Graph.Inductive.PatriciaTree
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntSet as IntSet
import Test.Hspec

import Ohua.ALang.Lang
import Ohua.ALang.Passes
import qualified Ohua.ALang.Refs as ALangRefs
import Ohua.DFGraph
import Ohua.DFLang.Lang
import Ohua.DFLang.PPrint
import Ohua.DFLang.Passes
import qualified Ohua.DFLang.Refs as Refs
import Ohua.Test (embedALang, embedDFLang, showWithPretty)
import Ohua.Test.DFGraph

shouldSatisfyRet :: Show a => IO a -> (a -> Bool) -> Expectation
shouldSatisfyRet action predicate = action >>= (`shouldSatisfy` predicate)

runLowering :: Expression -> IO DFExpr
runLowering =
    fmap (either error identity) .
    runSilentLoggingT .
    runFromExpr
        def
        (lowerALang <=<
         normalize <=< Ohua.ALang.Passes.runCorePasses <=< normalize)

-- | IMPORTANT: Both source and target expression must be in SSA form
shouldLowerTo :: Expression -> DFExpr -> Expectation
shouldLowerTo input expected = do
    lowered <- runLowering input
    -- traceM $ "expected:\n" <> (show $ prettyDFExpr expected)
    -- traceM $ "got:\n" <> (show $ prettyDFExpr lowered)
    showWithPretty lowered `shouldBe` showWithPretty expected
    -- let gr1 = (toFGLGraph . toGraph) lowered
    -- let gr2 = toFGLGraph $ toGraph expected
    -- (matchAndReport `on` unGr) gr1 gr2

lowerAndValidate :: Expression -> DFExpr -> String -> Spec
lowerAndValidate sourceExpr targetExpr statementType = do
    it ("correctly lowers an " ++ statementType ++ " statement") $
        sourceExpr `shouldLowerTo` targetExpr

smapLowering :: Spec
smapLowering =
    describe "lowering smap constructs" $ do
        let sourceExpr =
                [embedALang|
                  let coll = ohua.lang/id 0 in
                  let x = ohua.lang/smap
                              (\y -> let z = some.module/inc y in z)
                              coll in
                    x
                 |]
        let targetExpr =
                [embedDFLang|
                  let (coll) = ohua.lang/id<1> (0)  in
                  let (ctrls_0) = dataflow ohua.lang/smapFun<2> (coll)  in
                  let (d_0) = ohua.lang/nth<3> (0, 3, ctrls_0)  in
                  let (ctrl_0) = ohua.lang/nth<4> (1, 3, ctrls_0)  in
                  let (size_0) = ohua.lang/nth<5> (2, 3, ctrls_0)  in
                  let (ctrl_1) = dataflow ohua.lang/ctrl<6> (ctrl_0)  in<commit_msg>[#32] Removed an unused control from a test case<commit_after>{-# OPTIONS_GHC -fno-warn-orphans -fno-warn-missing-methods #-}

module DFLowering where

import Ohua.Prelude

import Data.Graph.Inductive.Graph
import Data.Graph.Inductive.PatriciaTree
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntSet as IntSet
import Test.Hspec

import Ohua.ALang.Lang
import Ohua.ALang.Passes
import qualified Ohua.ALang.Refs as ALangRefs
import Ohua.DFGraph
import Ohua.DFLang.Lang
import Ohua.DFLang.PPrint
import Ohua.DFLang.Passes
import qualified Ohua.DFLang.Refs as Refs
import Ohua.Test (embedALang, embedDFLang, showWithPretty)
import Ohua.Test.DFGraph

shouldSatisfyRet :: Show a => IO a -> (a -> Bool) -> Expectation
shouldSatisfyRet action predicate = action >>= (`shouldSatisfy` predicate)

runLowering :: Expression -> IO DFExpr
runLowering =
    fmap (either error identity) .
    runSilentLoggingT .
    runFromExpr
        def
        (lowerALang <=<
         normalize <=< Ohua.ALang.Passes.runCorePasses <=< normalize)

-- | IMPORTANT: Both source and target expression must be in SSA form
shouldLowerTo :: Expression -> DFExpr -> Expectation
shouldLowerTo input expected = do
    lowered <- runLowering input
    -- traceM $ "expected:\n" <> (show $ prettyDFExpr expected)
    -- traceM $ "got:\n" <> (show $ prettyDFExpr lowered)
    showWithPretty lowered `shouldBe` showWithPretty expected
    -- let gr1 = (toFGLGraph . toGraph) lowered
    -- let gr2 = toFGLGraph $ toGraph expected
    -- (matchAndReport `on` unGr) gr1 gr2

lowerAndValidate :: Expression -> DFExpr -> String -> Spec
lowerAndValidate sourceExpr targetExpr statementType = do
    it ("correctly lowers an " ++ statementType ++ " statement") $
        sourceExpr `shouldLowerTo` targetExpr

smapLowering :: Spec
smapLowering =
    describe "lowering smap constructs" $ do
        let sourceExpr =
                [embedALang|
                  let coll = ohua.lang/id 0 in
                  let x = ohua.lang/smap
                              (\y -> let z = some.module/inc y in z)
                              coll in
                    x
                 |]
        let targetExpr =
                [embedDFLang|
                  let (coll) = ohua.lang/id<1> (0)  in
                  let (ctrls_0) = dataflow ohua.lang/smapFun<2> (coll)  in
                  let (d_0) = ohua.lang/nth<3> (0, 3, ctrls_0)  in
                  let (ctrl_0) = ohua.lang/nth<4> (1, 3, ctrls_0)  in
                  let (size_0) = ohua.lang/nth<5> (2, 3, ctrls_0)  in
{'fraction_non_alphanumeric': 0.09883268482490272, 'fraction_numerical': 0.010311284046692607, 'mean_word_length': 2.420492348636061, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3676242', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1638, 'n_words': 451}