Skip to content

Commit

Permalink
manually handle recursion in HO effects
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Mar 1, 2024
1 parent b2ba490 commit 8544f91
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 312 deletions.
9 changes: 5 additions & 4 deletions src/Juvix/Compiler/Core/Transformation/UnrollRecursion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import Juvix.Compiler.Core.Transformation.Base
unrollRecursion :: (Member (Reader CoreOptions) r) => Module -> Sem r Module
unrollRecursion md = do
(mp, md') <-
runState @(HashMap Symbol Symbol) mempty $
execInfoTableBuilder md $
forM_ (buildSCCs (createCallGraph (md ^. moduleInfoTable))) goSCC
return $ mapIdentSymbols mp $ pruneInfoTable md'
runState @(HashMap Symbol Symbol) mempty
. execInfoTableBuilder md
. forM_ (buildSCCs (createCallGraph (md ^. moduleInfoTable)))
$ goSCC
return . mapIdentSymbols mp $ pruneInfoTable md'
where
mapIdentSymbols :: HashMap Symbol Symbol -> Module -> Module
mapIdentSymbols mp = over (moduleInfoTable . infoMain) adjustMain . mapAllNodes (umap go)
Expand Down
32 changes: 9 additions & 23 deletions src/Juvix/Compiler/Pipeline/Loader/PathResolver.hs
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,11 @@ runPathResolver2 st topEnv arg = do
arg
where
handler ::
forall t.
forall t localEs x.
(Members v t) =>
EffectHandler PathResolver (Reader ResolverEnv ': State ResolverState ': t)
LocalEnv localEs (Reader ResolverEnv ': State ResolverState ': t) ->
PathResolver (Sem localEs) x ->
Sem (Reader ResolverEnv ': State ResolverState ': t) x
handler localEnv = \case
RegisterDependencies forceUpdateLockfile -> registerDependencies' forceUpdateLockfile
ExpectedPathInfoTopModule m -> expectedPath' m
Expand All @@ -433,7 +435,6 @@ runPathResolver2 st topEnv arg = do
x :: Either PathResolverError (Path Abs Dir, Path Rel File) <- resolvePath' m
let y :: Sem localEs x = a x
oldroot <- asks (^. envRoot)
st' <- get
let root' = case x of
Left {} -> oldroot
Right (r, _) -> r
Expand All @@ -449,26 +450,11 @@ runPathResolver2 st topEnv arg = do
_envLockfileInfo = Nothing,
_envSingleFile
}

let runner ::
forall q handlerEs.
(handlerEs ~ (Reader ResolverEnv ': State ResolverState ': q)) =>
Sem handlerEs x ->
Sem q x
runner = evalState st' . runReader env'
helper :: (forall w. Sem localEs w -> Sem t w) -> Sem t x
helper unlift =
localSeqHandle2 @TaggedLock localEnv $ \env1 withTaggedLockEff ->
localSeqHandle2 @(Reader EntryPoint) env1 $ \env2 withEntryPoint ->
localSeqHandle2 @Files env2 $ \env3 withFiles ->
localSeqHandle2 @(Error JuvixError) env3 $ \env4 withJuvixError ->
localSeqHandle2 @(Error DependencyError) env4 $ \env5 withDependencyError ->
localSeqHandle2 @GitClone env5 $ \env6 withGitClone ->
localSeqHandle @EvalFileEff env6 $ \withEvalFileEff -> do
unlift . withTaggedLockEff . withEntryPoint . withFiles . withJuvixError . withDependencyError . withGitClone . withEvalFileEff $ impose runner handler (inject y)
inner :: Sem t x = localSeqUnlift localEnv helper

inject inner
localSeqUnlift localEnv $ \unlift -> local (const env') $ do
oldState <- get @ResolverState
res <- unlift y
put oldState
return res

runPathResolver :: (Members '[TaggedLock, Reader EntryPoint, Files, Error JuvixError, Error DependencyError, GitClone, EvalFileEff] r) => Path Abs Dir -> Sem (PathResolver ': r) a -> Sem r (ResolverState, a)
runPathResolver = runPathResolver' iniResolverState
Expand Down
10 changes: 0 additions & 10 deletions src/Juvix/Data/Effect/ExactPrint/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ runExactPrint cs = reinterpretH (runPrivateStateAsDoc (initialBuilder cs)) handl
}
return fx

t :: IO ()
t = putStrLn . toPlainText . fst . run . runExactPrint Nothing $ do
noLoc "hello 1\n"
region (\x -> "*" <> x <> "!") $ do
noLoc "hello 2\n"
noLoc "bye"

-- evalExactPrint' :: Builder -> Sem (ExactPrint ': r) a -> Sem r (Builder, a)
-- evalExactPrint' b = runState b . re

enqueue' :: forall r. (Members '[State Builder] r) => Doc Ann -> Sem r ()
enqueue' d = modify (over builderQueue (d :))

Expand Down
4 changes: 1 addition & 3 deletions src/Juvix/Prelude/Effects/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Juvix.Prelude.Effects.Base
module Effectful.TH,
module Effectful.Dispatch.Static,
module Effectful.Provider,
module Juvix.Prelude.Effects.Base.Internal,
)
where

Expand All @@ -26,7 +25,6 @@ import Effectful.State.Static.Local hiding (runState, state)
import Effectful.State.Static.Local qualified as State
import Effectful.TH
import Juvix.Prelude.Base.Foundation
import Juvix.Prelude.Effects.Base.Internal
import Language.Haskell.TH.Syntax qualified as GHC

type Sem = E.Eff
Expand Down Expand Up @@ -76,7 +74,7 @@ mapReader f s = do
e <- ask
runReader (f e) s

runState :: forall a s r. s -> Sem (State s ': r) a -> Sem r (s, a)
runState :: forall s r a. s -> Sem (State s ': r) a -> Sem r (s, a)
runState s = fmap swap . State.runState s

-- | TODO can we make it strict?
Expand Down
53 changes: 0 additions & 53 deletions src/Juvix/Prelude/Effects/Base/Internal.hs

This file was deleted.

48 changes: 0 additions & 48 deletions src/Juvix/Prelude/Effects/EffectfulExample.hs

This file was deleted.

51 changes: 0 additions & 51 deletions src/Juvix/Prelude/Effects/ExampleBad1.hs

This file was deleted.

28 changes: 0 additions & 28 deletions src/Juvix/Prelude/Effects/ExampleOk1.hs

This file was deleted.

42 changes: 0 additions & 42 deletions src/Juvix/Prelude/Effects/ExampleWork.hs

This file was deleted.

50 changes: 0 additions & 50 deletions src/Juvix/Prelude/Effects/LocalHandle.hs

This file was deleted.

0 comments on commit 8544f91

Please sign in to comment.