Skip to content

Commit

Permalink
Var: reduce module
Browse files Browse the repository at this point in the history
See: #946
  • Loading branch information
Anton-Latukha committed May 31, 2021
1 parent fb10451 commit 9b02635
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 79 deletions.
17 changes: 11 additions & 6 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@

## [(diff)](https://github.com/haskell-nix/hnix/compare/0.13.1...master#files_bucket) WIP

Breaking:
* Breaking:

* `Nix.Effects`:
* rm `pathExits` in favour of `doesPathExist` (in `Nix.Render`: `class MonadFile`: `doesPathExist`)

* `Nix.Var`: was found being superflous ([report](https://github.com/haskell-nix/hnix/issues/946)), so reduced. use `Control.Monad.Ref` instead.

* `Nix.Normal`
* rename `opaque(,->Val)`, indicate that it is a literal.

* `Nix.Thunk`:
* `class MonadThunkId m => MonadThunk{,F} t m a`:
* rename `query(M->){,F}`

* Additional:

* `Nix.Utils`:
Expand All @@ -25,6 +34,7 @@ Breaking:
* `Nix.Type.Env`:
* added instances:
* `Env`: `{Semigroup,Monoid,One}`

* `Nix`:
* changed argument order:
* `nixEval`:
Expand All @@ -38,11 +48,6 @@ Breaking:

* `Nix.Normal`
* add `thunkVal` literal & use it where appropriate `{deThunk, removeEffects}`
* rename `opaque(,->Val)`, indicate that it is a literal.

* `Nix.Thunk`:
* `class MonadThunkId m => MonadThunk{,F} t m a`:
* rename `query(M->){,F}`


### [(diff)](https://github.com/haskell-nix/hnix/compare/0.13.0.1...0.13.1#files_bucket) 0.13.1 (2021-05-22)
Expand Down
6 changes: 3 additions & 3 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import Control.Comonad ( extract )
import qualified Control.DeepSeq as Deep
import qualified Control.Exception as Exc
import GHC.Err ( errorWithoutStackTrace )
import Control.Monad.Free
import Control.Monad.Ref ( MonadRef(readRef) )
import Control.Monad.Catch
import System.IO ( hPutStrLn, getContents )
import Control.Monad.Free
import qualified Data.HashMap.Lazy as M
import qualified Data.Map as Map
import Data.Maybe ( fromJust )
Expand All @@ -29,7 +30,6 @@ import Nix.Standard
import Nix.Thunk.Basic
import qualified Nix.Type.Env as Env
import qualified Nix.Type.Infer as HM
import Nix.Var
import Nix.Value.Monad
import Options.Applicative hiding ( ParserResult(..) )
import Prettyprinter hiding ( list )
Expand Down Expand Up @@ -184,7 +184,7 @@ main =
path = prefix <> k
(_, descend) = filterEntry path k

val <- readVar @(StandardT (StdIdT IO)) ref
val <- readRef @(StandardT (StdIdT IO)) ref
case val of
Computed _ -> pure (k, Nothing)
_ ->
Expand Down
11 changes: 5 additions & 6 deletions src/Nix/Fresh.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import Control.Monad.Catch ( MonadCatch
)
import Control.Monad.Except ( MonadFix )
import Control.Monad.Ref ( MonadAtomicRef(..)
, MonadRef()
, MonadRef(Ref)
)

import Nix.Var
import Nix.Thunk


newtype FreshIdT i m a = FreshIdT { unFreshIdT :: ReaderT (Var m i) m a }
newtype FreshIdT i m a = FreshIdT { unFreshIdT :: ReaderT (Ref m i) m a }
deriving
( Functor
, Applicative
Expand All @@ -46,7 +45,7 @@ instance MonadBase b m => MonadBase b (FreshIdT i m) where
liftBase = FreshIdT . liftBase

instance
( MonadVar m
( MonadAtomicRef m
, Eq i
, Ord i
, Show i
Expand All @@ -58,7 +57,7 @@ instance
type ThunkId (FreshIdT i m) = i
freshId = FreshIdT $ do
v <- ask
atomicModifyVar v (\i -> (succ i, i))
atomicModifyRef v (\i -> (succ i, i))

runFreshIdT :: Functor m => Var m i -> FreshIdT i m a -> m a
runFreshIdT :: Functor m => Ref m i -> FreshIdT i m a -> m a
runFreshIdT i m = runReaderT (unFreshIdT m) i
31 changes: 15 additions & 16 deletions src/Nix/Lint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import Nix.Options
import Nix.Scope
import Nix.Thunk
import Nix.Thunk.Basic
import Nix.Var
import Nix.Value.Monad

data TAtom
Expand Down Expand Up @@ -87,40 +86,40 @@ data NSymbolicF r

type SThunk (m :: * -> *) = NThunkF m (Symbolic m)

type SValue (m :: * -> *) = Var m (NSymbolicF (NTypeF m (Symbolic m)))
type SValue (m :: * -> *) = Ref m (NSymbolicF (NTypeF m (Symbolic m)))

data Symbolic m = SV { getSV :: SValue m } | ST { getST :: SThunk m }

instance Show (Symbolic m) where
show _ = "<symbolic>"

everyPossible
:: MonadVar m
:: MonadAtomicRef m
=> m (Symbolic m)
everyPossible = packSymbolic NAny

mkSymbolic
:: MonadVar m
:: MonadAtomicRef m
=> [NTypeF m (Symbolic m)]
-> m (Symbolic m)
mkSymbolic xs = packSymbolic (NMany xs)

packSymbolic
:: MonadVar m
:: MonadAtomicRef m
=> NSymbolicF (NTypeF m (Symbolic m))
-> m (Symbolic m)
packSymbolic = fmap SV . newVar
packSymbolic = fmap SV . newRef

unpackSymbolic
:: (MonadVar m, MonadThunkId m, MonadCatch m)
:: (MonadAtomicRef m, MonadThunkId m, MonadCatch m)
=> Symbolic m
-> m (NSymbolicF (NTypeF m (Symbolic m)))
unpackSymbolic = readVar . getSV <=< demand
unpackSymbolic = readRef . getSV <=< demand

type MonadLint e m =
( Scoped (Symbolic m) m
, Framed e m
, MonadVar m
, MonadAtomicRef m
, MonadCatch m
, MonadThunkId m
)
Expand Down Expand Up @@ -237,21 +236,21 @@ unify
-> Symbolic m
-> m (Symbolic m)
unify context (SV x) (SV y) = do
x' <- readVar x
y' <- readVar y
x' <- readRef x
y' <- readRef y
case (x', y') of
(NAny, _) -> do
writeVar x y'
writeRef x y'
pure $ SV y
(_, NAny) -> do
writeVar y x'
writeRef y x'
pure $ SV x
(NMany xs, NMany ys) -> do
m <- merge context xs ys
bool
(do
writeVar x (NMany m)
writeVar y (NMany m)
writeRef x (NMany m)
writeRef y (NMany m)
packSymbolic (NMany m)
)
(do
Expand Down Expand Up @@ -468,7 +467,7 @@ instance MonadCatch (Lint s) where

runLintM :: Options -> Lint s a -> ST s a
runLintM opts action = do
i <- newVar (1 :: Int)
i <- newRef (1 :: Int)
runFreshIdT i $ (`runReaderT` newContext opts) $ runLint action

symbolicBaseEnv
Expand Down
10 changes: 5 additions & 5 deletions src/Nix/Standard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module Nix.Standard where

import Control.Applicative
import Prelude hiding ( force )
import Control.Comonad ( Comonad )
import Control.Comonad.Env ( ComonadEnv )
import Control.Monad.Catch ( MonadThrow
Expand All @@ -21,7 +21,9 @@ import Control.Monad.Fail ( MonadFail )
#endif
import Control.Monad.Free ( Free(Pure, Free) )
import Control.Monad.Reader ( MonadFix )
import Control.Monad.Ref ( MonadAtomicRef )
import Control.Monad.Ref ( MonadRef(newRef)
, MonadAtomicRef
)
import qualified Text.Show
import Nix.Cited
import Nix.Cited.Basic
Expand All @@ -41,8 +43,6 @@ import Nix.Utils ( free )
import Nix.Utils.Fix1 ( Fix1T(Fix1T) )
import Nix.Value
import Nix.Value.Monad
import Nix.Var
import Prelude hiding (force)


newtype StdCited m a =
Expand Down Expand Up @@ -345,7 +345,7 @@ runWithBasicEffects opts =
go . (`evalStateT` mempty) . (`runReaderT` newContext opts) . runStandardT
where
go action = do
i <- newVar (1 :: Int)
i <- newRef (1 :: Int)
runFreshIdT i action

runWithBasicEffectsIO :: Options -> StandardT (StdIdT IO) a -> IO a
Expand Down
30 changes: 16 additions & 14 deletions src/Nix/Thunk/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ module Nix.Thunk.Basic

import Prelude hiding ( force )
import Relude.Extra ( dup )
import Control.Monad.Ref ( MonadRef(Ref, newRef, readRef, writeRef)
, MonadAtomicRef(atomicModifyRef)
)
import Control.Monad.Catch ( MonadCatch(..)
, MonadThrow(throwM)
)
import qualified Text.Show
import Nix.Thunk
import Nix.Var


-- * Data type @Deferred@
Expand All @@ -43,10 +45,10 @@ deferred f1 f2 =

-- | Thunk resource reference (@ref-tf: Ref m@), and as such also also hold
-- a @Bool@ lock flag.
type ThunkRef m = Var m Bool
type ThunkRef m = Ref m Bool

-- | Reference (@ref-tf: Ref m v@) to a value that the thunk holds.
type ThunkValueRef m v = Var m (Deferred m v)
type ThunkValueRef m v = Ref m (Deferred m v)

-- | @ref-tf@ lock instruction for @Ref m@ (@ThunkRef@).
lock :: Bool -> (Bool, Bool)
Expand All @@ -63,7 +65,7 @@ lockThunk
)
=> ThunkRef m
-> m Bool
lockThunk r = atomicModifyVar r lock
lockThunk r = atomicModifyRef r lock

-- | Takes @ref-tf: Ref m@ reference, returns Bool result of the operation.
unlockThunk
Expand All @@ -72,7 +74,7 @@ unlockThunk
)
=> ThunkRef m
-> m Bool
unlockThunk r = atomicModifyVar r unlock
unlockThunk r = atomicModifyRef r unlock


-- * Data type for thunks: @NThunkF@
Expand All @@ -87,7 +89,7 @@ instance (Eq v, Eq (ThunkId m)) => Eq (NThunkF m v) where
instance Show (NThunkF m v) where
show Thunk{} = "<thunk>"

type MonadBasicThunk m = (MonadThunkId m, MonadVar m)
type MonadBasicThunk m = (MonadThunkId m, MonadAtomicRef m)


-- ** @instance MonadThunk NThunkF@
Expand All @@ -103,13 +105,13 @@ instance (MonadBasicThunk m, MonadCatch m)
do
freshThunkId <- freshId
liftA2 (Thunk freshThunkId)
(newVar False )
(newVar $ Deferred action)
(newRef False )
(newRef $ Deferred action)

query :: m v -> NThunkF m v -> m v
query vStub (Thunk _ _ lTValRef) =
do
v <- readVar lTValRef
v <- readRef lTValRef
deferred pure (const vStub) v

force :: NThunkF m v -> m v
Expand All @@ -122,7 +124,7 @@ instance (MonadBasicThunk m, MonadCatch m)
further t@(Thunk _ _ ref) =
do
_ <-
atomicModifyVar
atomicModifyRef
ref
dup
pure t
Expand All @@ -142,7 +144,7 @@ forceMain
-> m v
forceMain (Thunk vTId vTRef vTValRef) =
do
v <- readVar vTValRef
v <- readRef vTValRef
deferred pure fCompute v
where
fCompute vDefferred =
Expand All @@ -152,7 +154,7 @@ forceMain (Thunk vTId vTRef vTValRef) =
fLockFailed
(do
v <- vDefferred `catch` fBindFailed
writeVar vTValRef $ Computed v -- Proclaim value computed
writeRef vTValRef $ Computed v -- Proclaim value computed
unlockRef
pure v
)
Expand Down Expand Up @@ -190,7 +192,7 @@ instance (MonadBasicThunk m, MonadCatch m)
where
go =
do
eres <- readVar thunkValRef
eres <- readRef thunkValRef
res <-
deferred
k
Expand All @@ -217,7 +219,7 @@ instance (MonadBasicThunk m, MonadCatch m)
-> m (NThunkF m v)
furtherF k t@(Thunk _ _ ref) =
do
_modifiedIt <- atomicModifyVar ref $
_modifiedIt <- atomicModifyRef ref $
\x ->
deferred
(const (x, x))
Expand Down
5 changes: 2 additions & 3 deletions src/Nix/Type/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import Nix.Type.Env
import qualified Nix.Type.Env as Env
import Nix.Type.Type
import Nix.Value.Monad
import Nix.Var


normalizeScheme :: Scheme -> Scheme
Expand Down Expand Up @@ -645,7 +644,7 @@ instance ActiveTypeVars a => ActiveTypeVars [a] where

type MonadInfer m
= ({- MonadThunkId m,-}
MonadVar m, MonadFix m)
MonadAtomicRef m, MonadFix m)

-- | Run the inference monad
runInfer' :: MonadInfer m => InferT s m a -> m (Either InferError a)
Expand All @@ -659,7 +658,7 @@ runInfer :: (forall s . InferT s (FreshIdT Int (ST s)) a) -> Either InferError a
runInfer m =
runST $
do
i <- newVar (1 :: Int)
i <- newRef (1 :: Int)
runFreshIdT i $ runInfer' m

inferType
Expand Down
Loading

0 comments on commit 9b02635

Please sign in to comment.